v8js_v8object_class.cc 22 KB

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