v8js_v8object_class.cc 23 KB

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