v8js_v8object_class.cc 24 KB

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