v8js_v8object_class.cc 22 KB

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