v8js_v8object_class.cc 22 KB

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