v8js_v8object_class.cc 27 KB

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