v8js_v8object_class.cc 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2017 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | http://www.opensource.org/licenses/mit-license.php MIT License |
  8. +----------------------------------------------------------------------+
  9. | Author: Jani Taskinen <[email protected]> |
  10. | Author: Patrick Reilly <[email protected]> |
  11. | Author: Stefan Siegl <[email protected]> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include "php_v8js_macros.h"
  18. #include "v8js_exceptions.h"
  19. #include "v8js_v8.h"
  20. #include "v8js_v8object_class.h"
  21. extern "C"
  22. {
  23. #include "ext/date/php_date.h"
  24. #include "ext/standard/php_string.h"
  25. #include "zend_interfaces.h"
  26. #include "zend_closures.h"
  27. #include "ext/spl/spl_exceptions.h"
  28. #include "zend_exceptions.h"
  29. }
  30. /* {{{ Class Entries */
  31. zend_class_entry *php_ce_v8object;
  32. zend_class_entry *php_ce_v8function;
  33. zend_class_entry *php_ce_v8generator;
  34. /* }}} */
  35. /* {{{ Object Handlers */
  36. static zend_object_handlers v8js_v8object_handlers;
  37. static zend_object_handlers v8js_v8generator_handlers;
  38. /* }}} */
  39. #define V8JS_V8_INVOKE_FUNC_NAME "V8Js::V8::Invoke"
  40. /* V8 Object handlers */
  41. static int v8js_v8object_has_property(SINCE80(zend_object, zval) *_object, SINCE80(zend_string, zval) *_member, int has_set_exists, void **cache_slot) /* {{{ */
  42. {
  43. zend_object *object = SINCE80(_object, Z_OBJ_P(_object));
  44. zend_string *member = SINCE80(_member, Z_STR_P(_member));
  45. /* param has_set_exists:
  46. * 0 (has) whether property exists and is not NULL - isset()
  47. * 1 (set) whether property exists and is true-ish - empty()
  48. * 2 (exists) whether property exists - property_exists()
  49. */
  50. int retval = false;
  51. v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ(object);
  52. if (!obj->ctx)
  53. {
  54. zend_throw_exception(php_ce_v8js_exception,
  55. "Can't access V8Object after V8Js instance is destroyed!", 0);
  56. return false;
  57. }
  58. V8JS_CTX_PROLOGUE_EX(obj->ctx, false);
  59. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  60. v8::Local<v8::Object> jsObj;
  61. if (!v8obj->IsObject() || !v8obj->ToObject(v8_context).ToLocal(&jsObj))
  62. {
  63. return false;
  64. }
  65. if (ZSTR_LEN(member) > std::numeric_limits<int>::max())
  66. {
  67. zend_throw_exception(php_ce_v8js_exception,
  68. "Member name length exceeds maximum supported length", 0);
  69. return false;
  70. }
  71. v8::Local<v8::String> jsKey = V8JS_ZSYM(member);
  72. /* Skip any prototype properties */
  73. if (!jsObj->HasRealNamedProperty(v8_context, jsKey).FromMaybe(false) && !jsObj->HasRealNamedCallbackProperty(v8_context, jsKey).FromMaybe(false))
  74. {
  75. return false;
  76. }
  77. if (has_set_exists == 2)
  78. {
  79. /* property_exists(), that's enough! */
  80. return true;
  81. }
  82. /* We need to look at the value. */
  83. v8::Local<v8::Value> jsVal = jsObj->Get(v8_context, jsKey).ToLocalChecked();
  84. if (has_set_exists == 0)
  85. {
  86. /* isset(): We make 'undefined' equivalent to 'null' */
  87. return !(jsVal->IsNull() || jsVal->IsUndefined());
  88. }
  89. /* empty() */
  90. retval = jsVal->BooleanValue(isolate);
  91. /* for PHP compatibility, [] should also be empty */
  92. if (jsVal->IsArray() && retval)
  93. {
  94. v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(jsVal);
  95. retval = (array->Length() != 0);
  96. }
  97. /* for PHP compatibility, '0' should also be empty */
  98. v8::Local<v8::String> str;
  99. if (jsVal->IsString() && retval && jsVal->ToString(v8_context).ToLocal(&str) && str->Length() == 1)
  100. {
  101. uint16_t c = 0;
  102. str->Write(isolate, &c, 0, 1);
  103. if (c == '0')
  104. {
  105. retval = false;
  106. }
  107. }
  108. return retval;
  109. }
  110. /* }}} */
  111. static zval *v8js_v8object_read_property(SINCE80(zend_object, zval) *_object, SINCE80(zend_string, zval) *_member, int type, void **cache_slot, zval *rv) /* {{{ */
  112. {
  113. zend_object *object = SINCE80(_object, Z_OBJ_P(_object));
  114. zend_string *member = SINCE80(_member, Z_STR_P(_member));
  115. zval *retval = rv;
  116. v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ(object);
  117. if (!obj->ctx)
  118. {
  119. zend_throw_exception(php_ce_v8js_exception,
  120. "Can't access V8Object after V8Js instance is destroyed!", 0);
  121. return retval;
  122. }
  123. V8JS_CTX_PROLOGUE_EX(obj->ctx, retval);
  124. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  125. if (v8obj->IsObject())
  126. {
  127. if (ZSTR_LEN(member) > std::numeric_limits<int>::max())
  128. {
  129. zend_throw_exception(php_ce_v8js_exception,
  130. "Member name length exceeds maximum supported length", 0);
  131. return retval;
  132. }
  133. v8::Local<v8::String> jsKey = V8JS_ZSYM(member);
  134. v8::Local<v8::Object> jsObj = v8obj->ToObject(v8_context).ToLocalChecked();
  135. /* Skip any prototype properties */
  136. if (jsObj->HasRealNamedProperty(v8_context, jsKey).FromMaybe(false) || jsObj->HasRealNamedCallbackProperty(v8_context, jsKey).FromMaybe(false))
  137. {
  138. v8::MaybeLocal<v8::Value> jsVal = jsObj->Get(v8_context, jsKey);
  139. if (!jsVal.IsEmpty() && v8js_to_zval(jsVal.ToLocalChecked(), retval, obj->flags, isolate) == SUCCESS)
  140. {
  141. return retval;
  142. }
  143. }
  144. }
  145. return retval;
  146. }
  147. /* }}} */
  148. static zval *v8js_v8object_get_property_ptr_ptr(SINCE80(zend_object, zval) *object, SINCE80(zend_string, zval) *member, int type, void **cache_slot) /* {{{ */
  149. {
  150. return NULL;
  151. }
  152. /* }}} */
  153. static SINCE74(zval *, void) v8js_v8object_write_property(SINCE80(zend_object, zval) *_object, SINCE80(zend_string, zval) *_member, zval *value, void **cache_slot) /* {{{ */
  154. {
  155. zend_object *object = SINCE80(_object, Z_OBJ_P(_object));
  156. zend_string *member = SINCE80(_member, Z_STR_P(_member));
  157. v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ(object);
  158. if (!obj->ctx)
  159. {
  160. zend_throw_exception(php_ce_v8js_exception,
  161. "Can't access V8Object after V8Js instance is destroyed!", 0);
  162. return SINCE74(value, );
  163. }
  164. V8JS_CTX_PROLOGUE_EX(obj->ctx, SINCE74(value, ));
  165. v8::Local<v8::Value> v8objHandle = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  166. if (ZSTR_LEN(member) > std::numeric_limits<int>::max())
  167. {
  168. zend_throw_exception(php_ce_v8js_exception,
  169. "Member name length exceeds maximum supported length", 0);
  170. return SINCE74(value, );
  171. }
  172. v8::Local<v8::Object> v8obj;
  173. if (v8objHandle->IsObject() && v8objHandle->ToObject(v8_context).ToLocal(&v8obj))
  174. {
  175. v8obj->CreateDataProperty(v8_context, V8JS_ZSYM(member), zval_to_v8js(value, isolate));
  176. }
  177. return SINCE74(value, );
  178. }
  179. /* }}} */
  180. static void v8js_v8object_unset_property(SINCE80(zend_object, zval) *_object, SINCE80(zend_string, zval) *_member, void **cache_slot) /* {{{ */
  181. {
  182. zend_object *object = SINCE80(_object, Z_OBJ_P(_object));
  183. zend_string *member = SINCE80(_member, Z_STR_P(_member));
  184. v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ(object);
  185. if (!obj->ctx)
  186. {
  187. zend_throw_exception(php_ce_v8js_exception,
  188. "Can't access V8Object after V8Js instance is destroyed!", 0);
  189. return;
  190. }
  191. V8JS_CTX_PROLOGUE(obj->ctx);
  192. v8::Local<v8::Value> v8objHandle = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  193. if (ZSTR_LEN(member) > std::numeric_limits<int>::max())
  194. {
  195. zend_throw_exception(php_ce_v8js_exception,
  196. "Member name length exceeds maximum supported length", 0);
  197. return;
  198. }
  199. v8::Local<v8::Object> v8obj;
  200. if (v8objHandle->IsObject() && v8objHandle->ToObject(v8_context).ToLocal(&v8obj))
  201. {
  202. v8obj->Delete(v8_context, V8JS_ZSYM(member));
  203. }
  204. }
  205. /* }}} */
  206. static HashTable *v8js_v8object_get_properties(SINCE80(zend_object, zval) *object) /* {{{ */
  207. {
  208. v8js_v8object *obj = SINCE80(Z_V8JS_V8OBJECT_OBJ, Z_V8JS_V8OBJECT_OBJ_P)(object);
  209. if (obj->properties == NULL)
  210. {
  211. #if PHP_VERSION_ID < 70300
  212. if (GC_G(gc_active))
  213. {
  214. /* the garbage collector is running, don't create more zvals */
  215. return NULL;
  216. }
  217. #endif
  218. ALLOC_HASHTABLE(obj->properties);
  219. zend_hash_init(obj->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  220. if (!obj->ctx)
  221. {
  222. /* Half-constructed object, probably due to unserialize call.
  223. * Just pass back properties hash so unserialize can write to
  224. * it (instead of crashing the engine). */
  225. return obj->properties;
  226. }
  227. }
  228. else if (!obj->properties->u.v.nIteratorsCount)
  229. {
  230. zend_hash_clean(obj->properties);
  231. }
  232. if (!obj->ctx)
  233. {
  234. zend_throw_exception(php_ce_v8js_exception,
  235. "Can't access V8Object after V8Js instance is destroyed!", 0);
  236. return NULL;
  237. }
  238. V8JS_CTX_PROLOGUE_EX(obj->ctx, NULL);
  239. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  240. if (v8js_get_properties_hash(v8obj, obj->properties, obj->flags, isolate) == SUCCESS)
  241. {
  242. return obj->properties;
  243. }
  244. return NULL;
  245. }
  246. /* }}} */
  247. static HashTable *v8js_v8object_get_debug_info(SINCE80(zend_object, zval) *object, int *is_temp) /* {{{ */
  248. {
  249. *is_temp = 0;
  250. return v8js_v8object_get_properties(object);
  251. }
  252. /* }}} */
  253. static ZEND_FUNCTION(zend_v8object_func)
  254. {
  255. RETVAL_STR_COPY(EX(func)->common.function_name);
  256. zval *argv = NULL;
  257. int argc = ZEND_NUM_ARGS();
  258. zend_string *method = EX(func)->common.function_name;
  259. zend_object *object = Z_OBJ_P(getThis());
  260. /* Cleanup trampoline */
  261. ZEND_ASSERT(EX(func)->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE);
  262. zend_string_release(EX(func)->common.function_name);
  263. zend_free_trampoline(EX(func));
  264. EX(func) = NULL;
  265. v8js_v8object *obj = v8js_v8object_fetch_object(object);
  266. if (!obj->ctx)
  267. {
  268. zend_throw_exception(php_ce_v8js_exception,
  269. "Can't access V8Object after V8Js instance is destroyed!", 0);
  270. return;
  271. }
  272. if (obj->v8obj.IsEmpty())
  273. {
  274. return;
  275. }
  276. if (ZSTR_LEN(method) > std::numeric_limits<int>::max())
  277. {
  278. zend_throw_exception(php_ce_v8js_exception,
  279. "Method name length exceeds maximum supported length", 0);
  280. return;
  281. }
  282. if (argc > 0)
  283. {
  284. argv = (zval *)safe_emalloc(sizeof(zval), argc, 0);
  285. zend_get_parameters_array_ex(argc, argv);
  286. }
  287. /* std::function relies on its dtor to be executed, otherwise it leaks
  288. * some memory on bailout. */
  289. {
  290. std::function<v8::MaybeLocal<v8::Value>(v8::Isolate *)> v8_call = [obj, method, argc, argv, object, &return_value](v8::Isolate *isolate)
  291. {
  292. int i = 0;
  293. v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
  294. v8::Local<v8::String> method_name = V8JS_SYML(ZSTR_VAL(method), static_cast<int>(ZSTR_LEN(method)));
  295. v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj)->ToObject(v8_context).ToLocalChecked();
  296. v8::Local<v8::Object> thisObj;
  297. v8::Local<v8::Function> cb;
  298. if (method_name->Equals(v8_context, V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME)).FromMaybe(false))
  299. {
  300. cb = v8::Local<v8::Function>::Cast(v8obj);
  301. }
  302. else
  303. {
  304. v8::Local<v8::Value> slot;
  305. if (!v8obj->Get(v8_context, method_name).ToLocal(&slot))
  306. {
  307. return v8::MaybeLocal<v8::Value>();
  308. }
  309. cb = v8::Local<v8::Function>::Cast(slot);
  310. }
  311. // If a method is invoked on V8Object, then set the object itself as
  312. // "this" on JS side. Otherwise fall back to global object.
  313. if (obj->std.ce == php_ce_v8object)
  314. {
  315. thisObj = v8obj;
  316. }
  317. else
  318. {
  319. thisObj = V8JS_GLOBAL(isolate);
  320. }
  321. v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
  322. for (i = 0; i < argc; i++)
  323. {
  324. new (&jsArgv[i]) v8::Local<v8::Value>;
  325. jsArgv[i] = v8::Local<v8::Value>::New(isolate, zval_to_v8js(&argv[i], isolate));
  326. }
  327. v8::MaybeLocal<v8::Value> result = cb->Call(v8_context, thisObj, argc, jsArgv);
  328. if (obj->std.ce == php_ce_v8object && !result.IsEmpty() && result.ToLocalChecked()->StrictEquals(thisObj))
  329. {
  330. /* JS code did "return this", retain object identity */
  331. ZVAL_OBJ(return_value, object);
  332. zval_copy_ctor(return_value);
  333. result = v8::MaybeLocal<v8::Value>();
  334. }
  335. return result;
  336. };
  337. v8js_v8_call(obj->ctx, &return_value, obj->flags, obj->ctx->time_limit, obj->ctx->memory_limit, v8_call);
  338. }
  339. if (argc > 0)
  340. {
  341. efree(argv);
  342. }
  343. if (V8JSG(fatal_error_abort))
  344. {
  345. /* Check for fatal error marker possibly set by v8js_error_handler; just
  346. * rethrow the error since we're now out of V8. */
  347. zend_bailout();
  348. }
  349. }
  350. static zend_function *v8js_v8object_get_method(zend_object **object_ptr, zend_string *method, const zval *key) /* {{{ */
  351. {
  352. v8js_v8object *obj = v8js_v8object_fetch_object(*object_ptr);
  353. SINCE80(zend_internal_function, zend_function) *f;
  354. if (!obj->ctx)
  355. {
  356. zend_throw_exception(php_ce_v8js_exception,
  357. "Can't access V8Object after V8Js instance is destroyed!", 0);
  358. return NULL;
  359. }
  360. if (ZSTR_LEN(method) > std::numeric_limits<int>::max())
  361. {
  362. zend_throw_exception(php_ce_v8js_exception,
  363. "Method name length exceeds maximum supported length", 0);
  364. return NULL;
  365. }
  366. V8JS_CTX_PROLOGUE_EX(obj->ctx, NULL);
  367. v8::Local<v8::String> jsKey = V8JS_STRL(ZSTR_VAL(method), static_cast<int>(ZSTR_LEN(method)));
  368. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  369. if (!obj->v8obj.IsEmpty() && v8obj->IsObject() && !v8obj->IsFunction())
  370. {
  371. v8::Local<v8::Object> jsObj;
  372. v8::Local<v8::Value> jsObjSlot;
  373. if (v8obj->ToObject(v8_context).ToLocal(&jsObj) && jsObj->Has(v8_context, jsKey).FromMaybe(false) && jsObj->Get(v8_context, jsKey).ToLocal(&jsObjSlot) && jsObjSlot->IsFunction())
  374. {
  375. #if PHP_VERSION_ID < 80000
  376. f = (zend_function *)ecalloc(1, sizeof(*f));
  377. f->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
  378. f->common.function_name = zend_string_copy(method);
  379. return f;
  380. #else
  381. f = (zend_internal_function *)ecalloc(1, sizeof(*f));
  382. f->type = ZEND_ACC_CALL_VIA_HANDLER;
  383. f->handler = ZEND_FN(zend_v8object_func);
  384. f->function_name = zend_string_copy(method);
  385. return (zend_function *)f;
  386. #endif
  387. }
  388. }
  389. return NULL;
  390. }
  391. /* }}} */
  392. static int v8js_v8object_call_method(zend_string *method, zend_object *object, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  393. {
  394. zval *argv = NULL;
  395. int argc = ZEND_NUM_ARGS();
  396. v8js_v8object *obj = v8js_v8object_fetch_object(object);
  397. if (!obj->ctx)
  398. {
  399. zend_throw_exception(php_ce_v8js_exception,
  400. "Can't access V8Object after V8Js instance is destroyed!", 0);
  401. return FAILURE;
  402. }
  403. if (obj->v8obj.IsEmpty())
  404. {
  405. return FAILURE;
  406. }
  407. if (ZSTR_LEN(method) > std::numeric_limits<int>::max())
  408. {
  409. zend_throw_exception(php_ce_v8js_exception,
  410. "Method name length exceeds maximum supported length", 0);
  411. return FAILURE;
  412. }
  413. if (argc > 0)
  414. {
  415. argv = (zval *)safe_emalloc(sizeof(zval), argc, 0);
  416. zend_get_parameters_array_ex(argc, argv);
  417. }
  418. /* std::function relies on its dtor to be executed, otherwise it leaks
  419. * some memory on bailout. */
  420. {
  421. std::function<v8::MaybeLocal<v8::Value>(v8::Isolate *)> v8_call = [obj, method, argc, argv, object, &return_value](v8::Isolate *isolate)
  422. {
  423. int i = 0;
  424. v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
  425. v8::Local<v8::String> method_name = V8JS_SYML(ZSTR_VAL(method), static_cast<int>(ZSTR_LEN(method)));
  426. v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj)->ToObject(v8_context).ToLocalChecked();
  427. v8::Local<v8::Object> thisObj;
  428. v8::Local<v8::Function> cb;
  429. if (method_name->Equals(v8_context, V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME)).FromMaybe(false))
  430. {
  431. cb = v8::Local<v8::Function>::Cast(v8obj);
  432. }
  433. else
  434. {
  435. v8::Local<v8::Value> slot;
  436. if (!v8obj->Get(v8_context, method_name).ToLocal(&slot))
  437. {
  438. return v8::MaybeLocal<v8::Value>();
  439. }
  440. cb = v8::Local<v8::Function>::Cast(slot);
  441. }
  442. // If a method is invoked on V8Object, then set the object itself as
  443. // "this" on JS side. Otherwise fall back to global object.
  444. if (obj->std.ce == php_ce_v8object)
  445. {
  446. thisObj = v8obj;
  447. }
  448. else
  449. {
  450. thisObj = V8JS_GLOBAL(isolate);
  451. }
  452. v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
  453. for (i = 0; i < argc; i++)
  454. {
  455. new (&jsArgv[i]) v8::Local<v8::Value>;
  456. jsArgv[i] = v8::Local<v8::Value>::New(isolate, zval_to_v8js(&argv[i], isolate));
  457. }
  458. v8::MaybeLocal<v8::Value> result = cb->Call(v8_context, thisObj, argc, jsArgv);
  459. if (obj->std.ce == php_ce_v8object && !result.IsEmpty() && result.ToLocalChecked()->StrictEquals(thisObj))
  460. {
  461. /* JS code did "return this", retain object identity */
  462. ZVAL_OBJ(return_value, object);
  463. zval_copy_ctor(return_value);
  464. result = v8::MaybeLocal<v8::Value>();
  465. }
  466. return result;
  467. };
  468. v8js_v8_call(obj->ctx, &return_value, obj->flags, obj->ctx->time_limit, obj->ctx->memory_limit, v8_call);
  469. }
  470. if (argc > 0)
  471. {
  472. efree(argv);
  473. }
  474. if (V8JSG(fatal_error_abort))
  475. {
  476. /* Check for fatal error marker possibly set by v8js_error_handler; just
  477. * rethrow the error since we're now out of V8. */
  478. zend_bailout();
  479. }
  480. return SUCCESS;
  481. }
  482. /* }}} */
  483. #if PHP_VERSION_ID >= 80000
  484. static int v8js_v8object_get_closure(zend_object *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **zobj_ptr, bool call) /* {{{ */
  485. #else
  486. static int v8js_v8object_get_closure(zval *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **zobj_ptr) /* {{{ */
  487. #endif
  488. {
  489. SINCE80(zend_internal_function, zend_function) *invoke;
  490. v8js_v8object *obj = SINCE80(Z_V8JS_V8OBJECT_OBJ, Z_V8JS_V8OBJECT_OBJ_P)(object);
  491. if (!obj->ctx)
  492. {
  493. zend_throw_exception(php_ce_v8js_exception,
  494. "Can't access V8Object after V8Js instance is destroyed!", 0);
  495. return FAILURE;
  496. }
  497. V8JS_CTX_PROLOGUE_EX(obj->ctx, FAILURE);
  498. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  499. if (!v8obj->IsFunction())
  500. {
  501. return FAILURE;
  502. }
  503. #if PHP_VERSION_ID < 80000
  504. invoke = (zend_function *)ecalloc(1, sizeof(*invoke));
  505. invoke->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
  506. invoke->common.function_name = zend_string_init(V8JS_V8_INVOKE_FUNC_NAME, sizeof(V8JS_V8_INVOKE_FUNC_NAME) - 1, 0);
  507. *fptr_ptr = invoke;
  508. #else
  509. invoke = (zend_internal_function *)ecalloc(1, sizeof(*invoke));
  510. invoke->type = ZEND_ACC_CALL_VIA_HANDLER;
  511. invoke->handler = ZEND_FN(zend_v8object_func);
  512. invoke->function_name = zend_string_init(V8JS_V8_INVOKE_FUNC_NAME, sizeof(V8JS_V8_INVOKE_FUNC_NAME) - 1, 0);
  513. *fptr_ptr = (zend_function *)invoke;
  514. #endif
  515. if (zobj_ptr)
  516. {
  517. *zobj_ptr = SINCE80(object, Z_OBJ_P(object));
  518. }
  519. *ce_ptr = NULL;
  520. return SUCCESS;
  521. }
  522. /* }}} */
  523. static void v8js_v8object_free_storage(zend_object *object) /* {{{ */
  524. {
  525. v8js_v8object *c = v8js_v8object_fetch_object(object);
  526. if (c->properties)
  527. {
  528. zend_hash_destroy(c->properties);
  529. FREE_HASHTABLE(c->properties);
  530. c->properties = NULL;
  531. }
  532. zend_object_std_dtor(&c->std);
  533. if (c->ctx)
  534. {
  535. c->v8obj.Reset();
  536. c->ctx->v8js_v8objects.remove(c);
  537. }
  538. }
  539. /* }}} */
  540. static zend_object *v8js_v8object_new(zend_class_entry *ce) /* {{{ */
  541. {
  542. v8js_v8object *c;
  543. c = (v8js_v8object *)ecalloc(1, sizeof(v8js_v8object) + zend_object_properties_size(ce));
  544. zend_object_std_init(&c->std, ce);
  545. c->std.handlers = &v8js_v8object_handlers;
  546. new (&c->v8obj) v8::Persistent<v8::Value>();
  547. return &c->std;
  548. }
  549. /* }}} */
  550. /* NOTE: We could also override v8js_v8object_handlers.get_constructor to throw
  551. * an exception when invoked, but doing so causes the half-constructed object
  552. * to leak -- this seems to be a PHP bug. So we'll define magic __construct
  553. * methods instead. */
  554. /* {{{ proto V8Object::__construct()
  555. */
  556. PHP_METHOD(V8Object, __construct)
  557. {
  558. zend_throw_exception(php_ce_v8js_exception,
  559. "Can't directly construct V8 objects!", 0);
  560. RETURN_FALSE;
  561. }
  562. /* }}} */
  563. /* {{{ proto V8Object::__sleep()
  564. */
  565. PHP_METHOD(V8Object, __sleep)
  566. {
  567. zend_throw_exception(php_ce_v8js_exception,
  568. "You cannot serialize or unserialize V8Object instances", 0);
  569. RETURN_FALSE;
  570. }
  571. /* }}} */
  572. /* {{{ proto V8Object::__wakeup()
  573. */
  574. PHP_METHOD(V8Object, __wakeup)
  575. {
  576. zend_throw_exception(php_ce_v8js_exception,
  577. "You cannot serialize or unserialize V8Object instances", 0);
  578. RETURN_FALSE;
  579. }
  580. /* }}} */
  581. /* {{{ proto V8Function::__construct()
  582. */
  583. PHP_METHOD(V8Function, __construct)
  584. {
  585. zend_throw_exception(php_ce_v8js_exception,
  586. "Can't directly construct V8 objects!", 0);
  587. RETURN_FALSE;
  588. }
  589. /* }}} */
  590. /* {{{ proto V8Function::__sleep()
  591. */
  592. PHP_METHOD(V8Function, __sleep)
  593. {
  594. zend_throw_exception(php_ce_v8js_exception,
  595. "You cannot serialize or unserialize V8Function instances", 0);
  596. RETURN_FALSE;
  597. }
  598. /* }}} */
  599. /* {{{ proto V8Function::__wakeup()
  600. */
  601. PHP_METHOD(V8Function, __wakeup)
  602. {
  603. zend_throw_exception(php_ce_v8js_exception,
  604. "You cannot serialize or unserialize V8Function instances", 0);
  605. RETURN_FALSE;
  606. }
  607. /* }}} */
  608. static void v8js_v8generator_free_storage(zend_object *object) /* {{{ */
  609. {
  610. v8js_v8generator *c = v8js_v8generator_fetch_object(object);
  611. zval_ptr_dtor(&c->value);
  612. v8js_v8object_free_storage(object);
  613. }
  614. /* }}} */
  615. static zend_object *v8js_v8generator_new(zend_class_entry *ce) /* {{{ */
  616. {
  617. v8js_v8generator *c;
  618. c = (v8js_v8generator *)ecalloc(1, sizeof(v8js_v8generator) + zend_object_properties_size(ce));
  619. zend_object_std_init(&c->v8obj.std, ce);
  620. c->v8obj.std.handlers = &v8js_v8generator_handlers;
  621. new (&c->v8obj.v8obj) v8::Persistent<v8::Value>();
  622. return &c->v8obj.std;
  623. }
  624. /* }}} */
  625. static void v8js_v8generator_next(v8js_v8generator *g) /* {{{ */
  626. {
  627. if (!g->v8obj.ctx)
  628. {
  629. zend_throw_exception(php_ce_v8js_exception,
  630. "Can't access V8Generator after V8Js instance is destroyed!", 0);
  631. return;
  632. }
  633. /* std::function relies on its dtor to be executed, otherwise it leaks
  634. * some memory on bailout. */
  635. {
  636. std::function<v8::MaybeLocal<v8::Value>(v8::Isolate *)> v8_call = [g](v8::Isolate *isolate)
  637. {
  638. v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
  639. v8::Local<v8::String> method_name = V8JS_SYM("next");
  640. v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, g->v8obj.v8obj)->ToObject(v8_context).ToLocalChecked();
  641. v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(v8obj->Get(v8_context, method_name).ToLocalChecked());
  642. ;
  643. v8::MaybeLocal<v8::Value> result = cb->Call(v8_context, v8obj, 0, NULL);
  644. if (result.IsEmpty())
  645. {
  646. /* cb->Call probably threw (and already threw a zend exception), just return */
  647. return V8JS_NULL;
  648. }
  649. if (!result.ToLocalChecked()->IsObject())
  650. {
  651. zend_throw_exception(php_ce_v8js_exception,
  652. "V8Generator returned non-object on next()", 0);
  653. return V8JS_NULL;
  654. }
  655. v8::Local<v8::Object> resultObj = result.ToLocalChecked()->ToObject(v8_context).ToLocalChecked();
  656. v8::Local<v8::Value> val = resultObj->Get(v8_context, V8JS_SYM("value")).ToLocalChecked();
  657. v8::Local<v8::Value> done = resultObj->Get(v8_context, V8JS_SYM("done")).ToLocalChecked();
  658. zval_ptr_dtor(&g->value);
  659. v8js_to_zval(val, &g->value, 0, isolate);
  660. g->done = done->IsTrue();
  661. g->primed = true;
  662. return V8JS_NULL;
  663. };
  664. v8js_v8_call(g->v8obj.ctx, NULL, g->v8obj.flags, g->v8obj.ctx->time_limit, g->v8obj.ctx->memory_limit, v8_call);
  665. }
  666. if (V8JSG(fatal_error_abort))
  667. {
  668. /* Check for fatal error marker possibly set by v8js_error_handler; just
  669. * rethrow the error since we're now out of V8. */
  670. zend_bailout();
  671. }
  672. }
  673. /* }}} */
  674. static zend_function *v8js_v8generator_get_method(zend_object **object_ptr, zend_string *method, const zval *key) /* {{{ */
  675. {
  676. zend_function *result = std_object_handlers.get_method(object_ptr, method, key);
  677. if (!result)
  678. {
  679. result = v8js_v8object_get_method(object_ptr, method, key);
  680. }
  681. return result;
  682. }
  683. /* }}} */
  684. /* {{{ proto V8Generator::__construct()
  685. */
  686. PHP_METHOD(V8Generator, __construct)
  687. {
  688. zend_throw_exception(php_ce_v8js_exception,
  689. "Can't directly construct V8 objects!", 0);
  690. RETURN_FALSE;
  691. }
  692. /* }}} */
  693. /* {{{ proto V8Generator::__sleep()
  694. */
  695. PHP_METHOD(V8Generator, __sleep)
  696. {
  697. zend_throw_exception(php_ce_v8js_exception,
  698. "You cannot serialize or unserialize V8Generator instances", 0);
  699. RETURN_FALSE;
  700. }
  701. /* }}} */
  702. /* {{{ proto V8Generator::__wakeup()
  703. */
  704. PHP_METHOD(V8Generator, __wakeup)
  705. {
  706. zend_throw_exception(php_ce_v8js_exception,
  707. "You cannot serialize or unserialize V8Generator instances", 0);
  708. RETURN_FALSE;
  709. }
  710. /* }}} */
  711. /* {{{ mixed V8Generator::current()
  712. */
  713. PHP_METHOD(V8Generator, current)
  714. {
  715. v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
  716. if (!g->primed)
  717. {
  718. v8js_v8generator_next(g);
  719. }
  720. RETVAL_ZVAL(&g->value, 1, 0);
  721. }
  722. /* }}} */
  723. /* {{{ scalar V8Generator::key()
  724. */
  725. PHP_METHOD(V8Generator, key)
  726. {
  727. RETURN_FALSE;
  728. }
  729. /* }}} */
  730. /* {{{ void V8Generator::next()
  731. */
  732. PHP_METHOD(V8Generator, next)
  733. {
  734. v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
  735. v8js_v8generator_next(g);
  736. }
  737. /* }}} */
  738. /* {{{ void V8Generator::rewind()
  739. */
  740. PHP_METHOD(V8Generator, rewind)
  741. {
  742. v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
  743. if (g->primed)
  744. {
  745. zend_throw_exception(php_ce_v8js_exception,
  746. "V8Generator::rewind not supported by ES6", 0);
  747. }
  748. RETURN_FALSE;
  749. }
  750. /* }}} */
  751. /* {{{ boolean V8Generator::valid()
  752. */
  753. PHP_METHOD(V8Generator, valid)
  754. {
  755. v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
  756. if (!g->primed)
  757. {
  758. v8js_v8generator_next(g);
  759. }
  760. RETVAL_BOOL(!g->done);
  761. }
  762. /* }}} */
  763. void v8js_v8object_create(zval *res, v8::Local<v8::Value> value, int flags, v8::Isolate *isolate) /* {{{ */
  764. {
  765. v8js_ctx *ctx = (v8js_ctx *)isolate->GetData(0);
  766. if (value->IsGeneratorObject())
  767. {
  768. object_init_ex(res, php_ce_v8generator);
  769. }
  770. else if (value->IsFunction())
  771. {
  772. object_init_ex(res, php_ce_v8function);
  773. }
  774. else
  775. {
  776. object_init_ex(res, php_ce_v8object);
  777. }
  778. v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(res);
  779. c->v8obj.Reset(isolate, value);
  780. c->flags = flags;
  781. c->ctx = ctx;
  782. ctx->v8js_v8objects.push_front(c);
  783. }
  784. /* }}} */
  785. ZEND_BEGIN_ARG_INFO(arginfo_v8object_construct, 0)
  786. ZEND_END_ARG_INFO()
  787. ZEND_BEGIN_ARG_INFO(arginfo_v8object_sleep, 0)
  788. ZEND_END_ARG_INFO()
  789. ZEND_BEGIN_ARG_INFO(arginfo_v8object_wakeup, 0)
  790. ZEND_END_ARG_INFO()
  791. static const zend_function_entry v8js_v8object_methods[] = {/* {{{ */
  792. PHP_ME(V8Object, __construct, arginfo_v8object_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
  793. PHP_ME(V8Object, __sleep, arginfo_v8object_sleep, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
  794. PHP_ME(V8Object, __wakeup, arginfo_v8object_wakeup, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL){NULL, NULL, NULL}};
  795. /* }}} */
  796. ZEND_BEGIN_ARG_INFO(arginfo_v8function_construct, 0)
  797. ZEND_END_ARG_INFO()
  798. ZEND_BEGIN_ARG_INFO(arginfo_v8function_sleep, 0)
  799. ZEND_END_ARG_INFO()
  800. ZEND_BEGIN_ARG_INFO(arginfo_v8function_wakeup, 0)
  801. ZEND_END_ARG_INFO()
  802. static const zend_function_entry v8js_v8function_methods[] = {/* {{{ */
  803. PHP_ME(V8Function, __construct, arginfo_v8function_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
  804. PHP_ME(V8Function, __sleep, arginfo_v8function_sleep, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
  805. PHP_ME(V8Function, __wakeup, arginfo_v8function_wakeup, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL){NULL, NULL, NULL}};
  806. /* }}} */
  807. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_construct, 0)
  808. ZEND_END_ARG_INFO()
  809. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_sleep, 0)
  810. ZEND_END_ARG_INFO()
  811. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_wakeup, 0)
  812. ZEND_END_ARG_INFO()
  813. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_current, 0)
  814. ZEND_END_ARG_INFO()
  815. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_key, 0)
  816. ZEND_END_ARG_INFO()
  817. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_next, 0)
  818. ZEND_END_ARG_INFO()
  819. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_rewind, 0)
  820. ZEND_END_ARG_INFO()
  821. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_valid, 0)
  822. ZEND_END_ARG_INFO()
  823. static const zend_function_entry v8js_v8generator_methods[] = {/* {{{ */
  824. PHP_ME(V8Generator, __construct, arginfo_v8generator_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
  825. PHP_ME(V8Generator, __sleep, arginfo_v8generator_sleep, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
  826. PHP_ME(V8Generator, __wakeup, arginfo_v8generator_wakeup, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
  827. PHP_ME(V8Generator, current, arginfo_v8generator_current, ZEND_ACC_PUBLIC)
  828. PHP_ME(V8Generator, key, arginfo_v8generator_key, ZEND_ACC_PUBLIC)
  829. PHP_ME(V8Generator, next, arginfo_v8generator_next, ZEND_ACC_PUBLIC)
  830. PHP_ME(V8Generator, rewind, arginfo_v8generator_rewind, ZEND_ACC_PUBLIC)
  831. PHP_ME(V8Generator, valid, arginfo_v8generator_valid, ZEND_ACC_PUBLIC)
  832. {NULL, NULL, NULL}};
  833. /* }}} */
  834. PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */
  835. {
  836. zend_class_entry ce;
  837. /* V8Object Class */
  838. INIT_CLASS_ENTRY(ce, "V8Object", v8js_v8object_methods);
  839. php_ce_v8object = zend_register_internal_class(&ce);
  840. php_ce_v8object->ce_flags |= ZEND_ACC_FINAL;
  841. php_ce_v8object->create_object = v8js_v8object_new;
  842. /* V8Function Class */
  843. INIT_CLASS_ENTRY(ce, "V8Function", v8js_v8function_methods);
  844. php_ce_v8function = zend_register_internal_class(&ce);
  845. php_ce_v8function->ce_flags |= ZEND_ACC_FINAL;
  846. php_ce_v8function->create_object = v8js_v8object_new;
  847. /* V8Generator Class */
  848. INIT_CLASS_ENTRY(ce, "V8Generator", v8js_v8generator_methods);
  849. php_ce_v8generator = zend_register_internal_class(&ce);
  850. php_ce_v8generator->ce_flags |= ZEND_ACC_FINAL;
  851. php_ce_v8generator->create_object = v8js_v8generator_new;
  852. zend_class_implements(php_ce_v8generator, 1, zend_ce_iterator);
  853. /* V8<Object|Function> handlers */
  854. memcpy(&v8js_v8object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  855. v8js_v8object_handlers.clone_obj = NULL;
  856. v8js_v8object_handlers.cast_object = NULL;
  857. v8js_v8object_handlers.get_property_ptr_ptr = v8js_v8object_get_property_ptr_ptr;
  858. v8js_v8object_handlers.has_property = v8js_v8object_has_property;
  859. v8js_v8object_handlers.read_property = v8js_v8object_read_property;
  860. v8js_v8object_handlers.write_property = v8js_v8object_write_property;
  861. v8js_v8object_handlers.unset_property = v8js_v8object_unset_property;
  862. v8js_v8object_handlers.get_properties = v8js_v8object_get_properties;
  863. v8js_v8object_handlers.get_method = v8js_v8object_get_method;
  864. SINCE80(, v8js_v8object_handlers.call_method = v8js_v8object_call_method);
  865. v8js_v8object_handlers.get_debug_info = v8js_v8object_get_debug_info;
  866. v8js_v8object_handlers.get_closure = v8js_v8object_get_closure;
  867. v8js_v8object_handlers.offset = XtOffsetOf(struct v8js_v8object, std);
  868. v8js_v8object_handlers.free_obj = v8js_v8object_free_storage;
  869. /* V8Generator handlers */
  870. memcpy(&v8js_v8generator_handlers, &v8js_v8object_handlers, sizeof(zend_object_handlers));
  871. v8js_v8generator_handlers.get_method = v8js_v8generator_get_method;
  872. v8js_v8generator_handlers.offset = XtOffsetOf(struct v8js_v8generator, v8obj.std);
  873. v8js_v8generator_handlers.free_obj = v8js_v8generator_free_storage;
  874. return SUCCESS;
  875. } /* }}} */
  876. /*
  877. * Local variables:
  878. * tab-width: 4
  879. * c-basic-offset: 4
  880. * indent-tabs-mode: t
  881. * End:
  882. * vim600: noet sw=4 ts=4 fdm=marker
  883. * vim<600: noet sw=4 ts=4
  884. */