v8js_v8object_class.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. static zend_result v8js_v8object_get_closure(zend_object *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **zobj_ptr, bool call) /* {{{ */
  387. {
  388. zend_internal_function *invoke;
  389. v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ(object);
  390. if (!obj->ctx)
  391. {
  392. zend_throw_exception(php_ce_v8js_exception,
  393. "Can't access V8Object after V8Js instance is destroyed!", 0);
  394. return FAILURE;
  395. }
  396. V8JS_CTX_PROLOGUE_EX(obj->ctx, FAILURE);
  397. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  398. if (!v8obj->IsFunction())
  399. {
  400. return FAILURE;
  401. }
  402. invoke = (zend_internal_function *)ecalloc(1, sizeof(*invoke));
  403. invoke->type = ZEND_INTERNAL_FUNCTION;
  404. invoke->fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
  405. invoke->scope = object->ce;
  406. invoke->handler = ZEND_FN(zend_v8object_func);
  407. invoke->function_name = zend_string_init(V8JS_V8_INVOKE_FUNC_NAME, sizeof(V8JS_V8_INVOKE_FUNC_NAME) - 1, 0);
  408. *fptr_ptr = (zend_function *)invoke;
  409. if (zobj_ptr)
  410. {
  411. *zobj_ptr = object;
  412. }
  413. *ce_ptr = NULL;
  414. return SUCCESS;
  415. }
  416. /* }}} */
  417. static void v8js_v8object_free_storage(zend_object *object) /* {{{ */
  418. {
  419. v8js_v8object *c = v8js_v8object_fetch_object(object);
  420. if (c->properties)
  421. {
  422. zend_hash_destroy(c->properties);
  423. FREE_HASHTABLE(c->properties);
  424. c->properties = NULL;
  425. }
  426. zend_object_std_dtor(&c->std);
  427. if (c->ctx)
  428. {
  429. c->v8obj.Reset();
  430. c->ctx->v8js_v8objects.remove(c);
  431. }
  432. }
  433. /* }}} */
  434. static zend_object *v8js_v8object_new(zend_class_entry *ce) /* {{{ */
  435. {
  436. v8js_v8object *c;
  437. c = (v8js_v8object *)ecalloc(1, sizeof(v8js_v8object) + zend_object_properties_size(ce));
  438. zend_object_std_init(&c->std, ce);
  439. c->std.handlers = &v8js_v8object_handlers;
  440. new (&c->v8obj) v8::Persistent<v8::Value>();
  441. return &c->std;
  442. }
  443. /* }}} */
  444. /* NOTE: We could also override v8js_v8object_handlers.get_constructor to throw
  445. * an exception when invoked, but doing so causes the half-constructed object
  446. * to leak -- this seems to be a PHP bug. So we'll define magic __construct
  447. * methods instead. */
  448. /* {{{ proto V8Object::__construct()
  449. */
  450. PHP_METHOD(V8Object, __construct)
  451. {
  452. zend_throw_exception(php_ce_v8js_exception,
  453. "Can't directly construct V8 objects!", 0);
  454. RETURN_FALSE;
  455. }
  456. /* }}} */
  457. /* {{{ proto V8Object::__sleep()
  458. */
  459. PHP_METHOD(V8Object, __sleep)
  460. {
  461. zend_throw_exception(php_ce_v8js_exception,
  462. "You cannot serialize or unserialize V8Object instances", 0);
  463. RETURN_FALSE;
  464. }
  465. /* }}} */
  466. /* {{{ proto V8Object::__wakeup()
  467. */
  468. PHP_METHOD(V8Object, __wakeup)
  469. {
  470. zend_throw_exception(php_ce_v8js_exception,
  471. "You cannot serialize or unserialize V8Object instances", 0);
  472. RETURN_FALSE;
  473. }
  474. /* }}} */
  475. /* {{{ proto V8Function::__construct()
  476. */
  477. PHP_METHOD(V8Function, __construct)
  478. {
  479. zend_throw_exception(php_ce_v8js_exception,
  480. "Can't directly construct V8 objects!", 0);
  481. RETURN_FALSE;
  482. }
  483. /* }}} */
  484. /* {{{ proto V8Function::__sleep()
  485. */
  486. PHP_METHOD(V8Function, __sleep)
  487. {
  488. zend_throw_exception(php_ce_v8js_exception,
  489. "You cannot serialize or unserialize V8Function instances", 0);
  490. RETURN_FALSE;
  491. }
  492. /* }}} */
  493. /* {{{ proto V8Function::__wakeup()
  494. */
  495. PHP_METHOD(V8Function, __wakeup)
  496. {
  497. zend_throw_exception(php_ce_v8js_exception,
  498. "You cannot serialize or unserialize V8Function instances", 0);
  499. RETURN_FALSE;
  500. }
  501. /* }}} */
  502. static void v8js_v8generator_free_storage(zend_object *object) /* {{{ */
  503. {
  504. v8js_v8generator *c = v8js_v8generator_fetch_object(object);
  505. zval_ptr_dtor(&c->value);
  506. v8js_v8object_free_storage(object);
  507. }
  508. /* }}} */
  509. static zend_object *v8js_v8generator_new(zend_class_entry *ce) /* {{{ */
  510. {
  511. v8js_v8generator *c;
  512. c = (v8js_v8generator *)ecalloc(1, sizeof(v8js_v8generator) + zend_object_properties_size(ce));
  513. zend_object_std_init(&c->v8obj.std, ce);
  514. c->v8obj.std.handlers = &v8js_v8generator_handlers;
  515. new (&c->v8obj.v8obj) v8::Persistent<v8::Value>();
  516. return &c->v8obj.std;
  517. }
  518. /* }}} */
  519. static void v8js_v8generator_next(v8js_v8generator *g) /* {{{ */
  520. {
  521. if (!g->v8obj.ctx)
  522. {
  523. zend_throw_exception(php_ce_v8js_exception,
  524. "Can't access V8Generator after V8Js instance is destroyed!", 0);
  525. return;
  526. }
  527. /* std::function relies on its dtor to be executed, otherwise it leaks
  528. * some memory on bailout. */
  529. {
  530. std::function<v8::MaybeLocal<v8::Value>(v8::Isolate *)> v8_call = [g](v8::Isolate *isolate)
  531. {
  532. v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
  533. v8::Local<v8::String> method_name = V8JS_SYM("next");
  534. v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, g->v8obj.v8obj)->ToObject(v8_context).ToLocalChecked();
  535. v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(v8obj->Get(v8_context, method_name).ToLocalChecked());
  536. ;
  537. v8::MaybeLocal<v8::Value> result = cb->Call(v8_context, v8obj, 0, NULL);
  538. if (result.IsEmpty())
  539. {
  540. /* cb->Call probably threw (and already threw a zend exception), just return */
  541. return V8JS_NULL;
  542. }
  543. if (!result.ToLocalChecked()->IsObject())
  544. {
  545. zend_throw_exception(php_ce_v8js_exception,
  546. "V8Generator returned non-object on next()", 0);
  547. return V8JS_NULL;
  548. }
  549. v8::Local<v8::Object> resultObj = result.ToLocalChecked()->ToObject(v8_context).ToLocalChecked();
  550. v8::Local<v8::Value> val = resultObj->Get(v8_context, V8JS_SYM("value")).ToLocalChecked();
  551. v8::Local<v8::Value> done = resultObj->Get(v8_context, V8JS_SYM("done")).ToLocalChecked();
  552. zval_ptr_dtor(&g->value);
  553. v8js_to_zval(val, &g->value, 0, isolate);
  554. g->done = done->IsTrue();
  555. g->primed = true;
  556. return V8JS_NULL;
  557. };
  558. v8js_v8_call(g->v8obj.ctx, NULL, g->v8obj.flags, g->v8obj.ctx->time_limit, g->v8obj.ctx->memory_limit, v8_call);
  559. }
  560. if (V8JSG(fatal_error_abort))
  561. {
  562. /* Check for fatal error marker possibly set by v8js_error_handler; just
  563. * rethrow the error since we're now out of V8. */
  564. zend_bailout();
  565. }
  566. }
  567. /* }}} */
  568. static zend_function *v8js_v8generator_get_method(zend_object **object_ptr, zend_string *method, const zval *key) /* {{{ */
  569. {
  570. zend_function *result = std_object_handlers.get_method(object_ptr, method, key);
  571. if (!result)
  572. {
  573. result = v8js_v8object_get_method(object_ptr, method, key);
  574. }
  575. return result;
  576. }
  577. /* }}} */
  578. /* {{{ proto V8Generator::__construct()
  579. */
  580. PHP_METHOD(V8Generator, __construct)
  581. {
  582. zend_throw_exception(php_ce_v8js_exception,
  583. "Can't directly construct V8 objects!", 0);
  584. RETURN_FALSE;
  585. }
  586. /* }}} */
  587. /* {{{ proto V8Generator::__sleep()
  588. */
  589. PHP_METHOD(V8Generator, __sleep)
  590. {
  591. zend_throw_exception(php_ce_v8js_exception,
  592. "You cannot serialize or unserialize V8Generator instances", 0);
  593. RETURN_FALSE;
  594. }
  595. /* }}} */
  596. /* {{{ proto V8Generator::__wakeup()
  597. */
  598. PHP_METHOD(V8Generator, __wakeup)
  599. {
  600. zend_throw_exception(php_ce_v8js_exception,
  601. "You cannot serialize or unserialize V8Generator instances", 0);
  602. RETURN_FALSE;
  603. }
  604. /* }}} */
  605. /* {{{ mixed V8Generator::current(): mixed
  606. */
  607. PHP_METHOD(V8Generator, current)
  608. {
  609. v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
  610. if (!g->primed)
  611. {
  612. v8js_v8generator_next(g);
  613. }
  614. RETVAL_ZVAL(&g->value, 1, 0);
  615. }
  616. /* }}} */
  617. /* {{{ scalar V8Generator::key(): mixed
  618. */
  619. PHP_METHOD(V8Generator, key)
  620. {
  621. RETURN_FALSE;
  622. }
  623. /* }}} */
  624. /* {{{ void V8Generator::next(): void
  625. */
  626. PHP_METHOD(V8Generator, next)
  627. {
  628. v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
  629. v8js_v8generator_next(g);
  630. }
  631. /* }}} */
  632. /* {{{ void V8Generator::rewind(): void
  633. */
  634. PHP_METHOD(V8Generator, rewind)
  635. {
  636. v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
  637. if (g->primed)
  638. {
  639. zend_throw_exception(php_ce_v8js_exception,
  640. "V8Generator::rewind not supported by ES6", 0);
  641. }
  642. RETURN_FALSE;
  643. }
  644. /* }}} */
  645. /* {{{ boolean V8Generator::valid(): bool
  646. */
  647. PHP_METHOD(V8Generator, valid)
  648. {
  649. v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
  650. if (!g->primed)
  651. {
  652. v8js_v8generator_next(g);
  653. }
  654. RETVAL_BOOL(!g->done);
  655. }
  656. /* }}} */
  657. void v8js_v8object_create(zval *res, v8::Local<v8::Value> value, int flags, v8::Isolate *isolate) /* {{{ */
  658. {
  659. v8js_ctx *ctx = (v8js_ctx *)isolate->GetData(0);
  660. if (value->IsGeneratorObject())
  661. {
  662. object_init_ex(res, php_ce_v8generator);
  663. }
  664. else if (value->IsFunction())
  665. {
  666. object_init_ex(res, php_ce_v8function);
  667. }
  668. else
  669. {
  670. object_init_ex(res, php_ce_v8object);
  671. }
  672. v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(res);
  673. c->v8obj.Reset(isolate, value);
  674. c->flags = flags;
  675. c->ctx = ctx;
  676. ctx->v8js_v8objects.push_front(c);
  677. }
  678. /* }}} */
  679. ZEND_BEGIN_ARG_INFO(arginfo_v8object_construct, 0)
  680. ZEND_END_ARG_INFO()
  681. ZEND_BEGIN_ARG_INFO(arginfo_v8object_sleep, 0)
  682. ZEND_END_ARG_INFO()
  683. ZEND_BEGIN_ARG_INFO(arginfo_v8object_wakeup, 0)
  684. ZEND_END_ARG_INFO()
  685. static const zend_function_entry v8js_v8object_methods[] = {/* {{{ */
  686. PHP_ME(V8Object, __construct, arginfo_v8object_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
  687. PHP_ME(V8Object, __sleep, arginfo_v8object_sleep, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
  688. PHP_ME(V8Object, __wakeup, arginfo_v8object_wakeup, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL){NULL, NULL, NULL}};
  689. /* }}} */
  690. ZEND_BEGIN_ARG_INFO(arginfo_v8function_construct, 0)
  691. ZEND_END_ARG_INFO()
  692. ZEND_BEGIN_ARG_INFO(arginfo_v8function_sleep, 0)
  693. ZEND_END_ARG_INFO()
  694. ZEND_BEGIN_ARG_INFO(arginfo_v8function_wakeup, 0)
  695. ZEND_END_ARG_INFO()
  696. static const zend_function_entry v8js_v8function_methods[] = {/* {{{ */
  697. PHP_ME(V8Function, __construct, arginfo_v8function_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
  698. PHP_ME(V8Function, __sleep, arginfo_v8function_sleep, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
  699. PHP_ME(V8Function, __wakeup, arginfo_v8function_wakeup, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL){NULL, NULL, NULL}};
  700. /* }}} */
  701. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_construct, 0)
  702. ZEND_END_ARG_INFO()
  703. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_sleep, 0)
  704. ZEND_END_ARG_INFO()
  705. ZEND_BEGIN_ARG_INFO(arginfo_v8generator_wakeup, 0)
  706. ZEND_END_ARG_INFO()
  707. ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8generator_current, 0, 0, IS_MIXED, 0)
  708. ZEND_END_ARG_INFO()
  709. ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8generator_key, 0, 0, IS_MIXED, 0)
  710. ZEND_END_ARG_INFO()
  711. ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8generator_next, 0, 0, IS_VOID, 0)
  712. ZEND_END_ARG_INFO()
  713. ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8generator_rewind, 0, 0, IS_VOID, 0)
  714. ZEND_END_ARG_INFO()
  715. ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_v8generator_valid, 0, 0, _IS_BOOL, 0)
  716. ZEND_END_ARG_INFO()
  717. static const zend_function_entry v8js_v8generator_methods[] = {/* {{{ */
  718. PHP_ME(V8Generator, __construct, arginfo_v8generator_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
  719. PHP_ME(V8Generator, __sleep, arginfo_v8generator_sleep, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
  720. PHP_ME(V8Generator, __wakeup, arginfo_v8generator_wakeup, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
  721. PHP_ME(V8Generator, current, arginfo_v8generator_current, ZEND_ACC_PUBLIC)
  722. PHP_ME(V8Generator, key, arginfo_v8generator_key, ZEND_ACC_PUBLIC)
  723. PHP_ME(V8Generator, next, arginfo_v8generator_next, ZEND_ACC_PUBLIC)
  724. PHP_ME(V8Generator, rewind, arginfo_v8generator_rewind, ZEND_ACC_PUBLIC)
  725. PHP_ME(V8Generator, valid, arginfo_v8generator_valid, ZEND_ACC_PUBLIC)
  726. {NULL, NULL, NULL}};
  727. /* }}} */
  728. PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */
  729. {
  730. zend_class_entry ce;
  731. /* V8Object Class */
  732. INIT_CLASS_ENTRY(ce, "V8Object", v8js_v8object_methods);
  733. php_ce_v8object = zend_register_internal_class(&ce);
  734. php_ce_v8object->ce_flags |= ZEND_ACC_FINAL;
  735. php_ce_v8object->create_object = v8js_v8object_new;
  736. /* V8Function Class */
  737. INIT_CLASS_ENTRY(ce, "V8Function", v8js_v8function_methods);
  738. php_ce_v8function = zend_register_internal_class(&ce);
  739. php_ce_v8function->ce_flags |= ZEND_ACC_FINAL;
  740. php_ce_v8function->create_object = v8js_v8object_new;
  741. /* V8Generator Class */
  742. INIT_CLASS_ENTRY(ce, "V8Generator", v8js_v8generator_methods);
  743. php_ce_v8generator = zend_register_internal_class(&ce);
  744. php_ce_v8generator->ce_flags |= ZEND_ACC_FINAL;
  745. php_ce_v8generator->create_object = v8js_v8generator_new;
  746. zend_class_implements(php_ce_v8generator, 1, zend_ce_iterator);
  747. /* V8<Object|Function> handlers */
  748. memcpy(&v8js_v8object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  749. v8js_v8object_handlers.clone_obj = NULL;
  750. v8js_v8object_handlers.cast_object = NULL;
  751. v8js_v8object_handlers.get_property_ptr_ptr = v8js_v8object_get_property_ptr_ptr;
  752. v8js_v8object_handlers.has_property = v8js_v8object_has_property;
  753. v8js_v8object_handlers.read_property = v8js_v8object_read_property;
  754. v8js_v8object_handlers.write_property = v8js_v8object_write_property;
  755. v8js_v8object_handlers.unset_property = v8js_v8object_unset_property;
  756. v8js_v8object_handlers.get_properties = v8js_v8object_get_properties;
  757. v8js_v8object_handlers.get_method = v8js_v8object_get_method;
  758. v8js_v8object_handlers.get_gc = v8js_v8object_get_gc;
  759. v8js_v8object_handlers.get_debug_info = v8js_v8object_get_debug_info;
  760. v8js_v8object_handlers.get_closure = v8js_v8object_get_closure;
  761. v8js_v8object_handlers.offset = XtOffsetOf(struct v8js_v8object, std);
  762. v8js_v8object_handlers.free_obj = v8js_v8object_free_storage;
  763. /* V8Generator handlers */
  764. memcpy(&v8js_v8generator_handlers, &v8js_v8object_handlers, sizeof(zend_object_handlers));
  765. v8js_v8generator_handlers.get_method = v8js_v8generator_get_method;
  766. v8js_v8generator_handlers.offset = XtOffsetOf(struct v8js_v8generator, v8obj.std);
  767. v8js_v8generator_handlers.free_obj = v8js_v8generator_free_storage;
  768. return SUCCESS;
  769. } /* }}} */
  770. /*
  771. * Local variables:
  772. * tab-width: 4
  773. * c-basic-offset: 4
  774. * indent-tabs-mode: t
  775. * End:
  776. * vim600: noet sw=4 ts=4 fdm=marker
  777. * vim<600: noet sw=4 ts=4
  778. */