v8js_v8object_class.cc 23 KB

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