v8js_v8object_class.cc 24 KB

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