v8js_v8object_class.cc 24 KB

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