v8js_v8object_class.cc 26 KB

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