v8js_v8object_class.cc 22 KB

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