v8js_class.cc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2015 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. extern "C" {
  18. #include "php.h"
  19. #include "ext/date/php_date.h"
  20. #include "ext/standard/php_string.h"
  21. #include "zend_interfaces.h"
  22. #include "zend_closures.h"
  23. #include "ext/spl/spl_exceptions.h"
  24. #include "zend_exceptions.h"
  25. }
  26. #include "php_v8js_macros.h"
  27. #include "v8js_v8.h"
  28. #include "v8js_exceptions.h"
  29. #include "v8js_v8object_class.h"
  30. #include "v8js_object_export.h"
  31. #include "v8js_timer.h"
  32. #include <functional>
  33. #include <algorithm>
  34. #define PHP_V8JS_SCRIPT_RES_NAME "V8Js script"
  35. /* {{{ Class Entries */
  36. static zend_class_entry *php_ce_v8js;
  37. /* }}} */
  38. /* {{{ Object Handlers */
  39. static zend_object_handlers v8js_object_handlers;
  40. /* }}} */
  41. typedef struct _v8js_script {
  42. char *name;
  43. v8js_ctx *ctx;
  44. v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>> *script;
  45. } v8js_script;
  46. static void v8js_script_free(v8js_script *res);
  47. int le_v8js_script;
  48. /* {{{ Extension container */
  49. struct v8js_jsext {
  50. zend_bool auto_enable;
  51. HashTable *deps_ht;
  52. const char **deps;
  53. int deps_count;
  54. char *name;
  55. char *source;
  56. v8::Extension *extension;
  57. };
  58. /* }}} */
  59. #if PHP_V8_API_VERSION >= 4004010
  60. class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
  61. public:
  62. virtual void* Allocate(size_t length) {
  63. void* data = AllocateUninitialized(length);
  64. return data == NULL ? data : memset(data, 0, length);
  65. }
  66. virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
  67. virtual void Free(void* data, size_t) { free(data); }
  68. };
  69. #endif
  70. static void v8js_free_storage(void *object TSRMLS_DC) /* {{{ */
  71. {
  72. v8js_ctx *c = (v8js_ctx *) object;
  73. zend_object_std_dtor(&c->std TSRMLS_CC);
  74. if (c->pending_exception) {
  75. zval_ptr_dtor(&c->pending_exception);
  76. }
  77. if (c->module_normaliser) {
  78. zval_ptr_dtor(&c->module_normaliser);
  79. }
  80. if (c->module_loader) {
  81. zval_ptr_dtor(&c->module_loader);
  82. }
  83. /* Delete PHP global object from JavaScript */
  84. if (!c->context.IsEmpty()) {
  85. v8::Locker locker(c->isolate);
  86. v8::Isolate::Scope isolate_scope(c->isolate);
  87. v8::HandleScope handle_scope(c->isolate);
  88. v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(c->isolate, c->context);
  89. v8::Context::Scope context_scope(v8_context);
  90. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
  91. V8JS_GLOBAL(c->isolate)->Delete(object_name_js);
  92. }
  93. c->object_name.Reset();
  94. c->object_name.~Persistent();
  95. c->global_template.Reset();
  96. c->global_template.~Persistent();
  97. c->array_tmpl.Reset();
  98. c->array_tmpl.~Persistent();
  99. /* Clear persistent call_impl & method_tmpls templates */
  100. for (std::map<v8js_tmpl_t *, v8js_tmpl_t>::iterator it = c->call_impls.begin();
  101. it != c->call_impls.end(); ++it) {
  102. // No need to free it->first, as it is stored in c->template_cache and freed below
  103. it->second.Reset();
  104. }
  105. c->call_impls.~map();
  106. for (std::map<zend_function *, v8js_tmpl_t>::iterator it = c->method_tmpls.begin();
  107. it != c->method_tmpls.end(); ++it) {
  108. it->second.Reset();
  109. }
  110. c->method_tmpls.~map();
  111. /* Clear persistent handles in template cache */
  112. for (std::map<const char *,v8js_tmpl_t>::iterator it = c->template_cache.begin();
  113. it != c->template_cache.end(); ++it) {
  114. it->second.Reset();
  115. }
  116. c->template_cache.~map();
  117. /* Clear contexts */
  118. for (std::vector<v8js_accessor_ctx*>::iterator it = c->accessor_list.begin();
  119. it != c->accessor_list.end(); ++it) {
  120. v8js_accessor_ctx_dtor(*it TSRMLS_CC);
  121. }
  122. c->accessor_list.~vector();
  123. /* Clear global object, dispose context */
  124. if (!c->context.IsEmpty()) {
  125. c->context.Reset();
  126. }
  127. c->context.~Persistent();
  128. /* Dispose yet undisposed weak refs */
  129. for (std::map<zval *, v8js_persistent_obj_t>::iterator it = c->weak_objects.begin();
  130. it != c->weak_objects.end(); ++it) {
  131. zval *value = it->first;
  132. zval_ptr_dtor(&value);
  133. c->isolate->AdjustAmountOfExternalAllocatedMemory(-1024);
  134. it->second.Reset();
  135. }
  136. c->weak_objects.~map();
  137. for (std::map<v8js_tmpl_t *, v8js_persistent_obj_t>::iterator it = c->weak_closures.begin();
  138. it != c->weak_closures.end(); ++it) {
  139. v8js_tmpl_t *persist_tpl_ = it->first;
  140. persist_tpl_->Reset();
  141. delete persist_tpl_;
  142. it->second.Reset();
  143. }
  144. c->weak_closures.~map();
  145. for (std::list<v8js_v8object *>::iterator it = c->v8js_v8objects.begin();
  146. it != c->v8js_v8objects.end(); it ++) {
  147. (*it)->v8obj.Reset();
  148. (*it)->ctx = NULL;
  149. }
  150. c->v8js_v8objects.~list();
  151. for (std::vector<v8js_script *>::iterator it = c->script_objects.begin();
  152. it != c->script_objects.end(); it ++) {
  153. (*it)->ctx = NULL;
  154. (*it)->script->Reset();
  155. }
  156. c->script_objects.~vector();
  157. /* Clear persistent handles in module cache */
  158. for (std::map<char *, v8js_persistent_obj_t>::iterator it = c->modules_loaded.begin();
  159. it != c->modules_loaded.end(); ++it) {
  160. efree(it->first);
  161. it->second.Reset();
  162. }
  163. c->modules_loaded.~map();
  164. if(c->isolate) {
  165. /* c->isolate is initialized by V8Js::__construct, but __wakeup calls
  166. * are not fully constructed and hence this would cause a NPE. */
  167. c->isolate->Dispose();
  168. }
  169. if(c->tz != NULL) {
  170. free(c->tz);
  171. }
  172. c->modules_stack.~vector();
  173. c->modules_base.~vector();
  174. efree(object);
  175. }
  176. /* }}} */
  177. static zend_object_value v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  178. {
  179. zend_object_value retval;
  180. v8js_ctx *c;
  181. c = (v8js_ctx *) ecalloc(1, sizeof(*c));
  182. zend_object_std_init(&c->std, ce TSRMLS_CC);
  183. TSRMLS_SET_CTX(c->zts_ctx);
  184. #if PHP_VERSION_ID >= 50400
  185. object_properties_init(&c->std, ce);
  186. #else
  187. zval *tmp;
  188. zend_hash_copy(c->std.properties, &ce->default_properties,
  189. (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  190. #endif
  191. new(&c->object_name) v8::Persistent<v8::String>();
  192. new(&c->context) v8::Persistent<v8::Context>();
  193. new(&c->global_template) v8::Persistent<v8::FunctionTemplate>();
  194. new(&c->array_tmpl) v8::Persistent<v8::FunctionTemplate>();
  195. new(&c->modules_stack) std::vector<char*>();
  196. new(&c->modules_base) std::vector<char*>();
  197. new(&c->modules_loaded) std::map<char *, v8js_persistent_obj_t, cmp_str>;
  198. new(&c->template_cache) std::map<const char *,v8js_tmpl_t>();
  199. new(&c->accessor_list) std::vector<v8js_accessor_ctx *>();
  200. new(&c->weak_closures) std::map<v8js_tmpl_t *, v8js_persistent_obj_t>();
  201. new(&c->weak_objects) std::map<zval *, v8js_persistent_obj_t>();
  202. new(&c->call_impls) std::map<v8js_tmpl_t *, v8js_tmpl_t>();
  203. new(&c->method_tmpls) std::map<zend_function *, v8js_tmpl_t>();
  204. new(&c->v8js_v8objects) std::list<v8js_v8object *>();
  205. new(&c->script_objects) std::vector<v8js_script *>();
  206. retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) v8js_free_storage, NULL TSRMLS_CC);
  207. retval.handlers = &v8js_object_handlers;
  208. return retval;
  209. }
  210. /* }}} */
  211. static void v8js_free_ext_strarr(const char **arr, int count) /* {{{ */
  212. {
  213. int i;
  214. if (arr) {
  215. for (i = 0; i < count; i++) {
  216. if (arr[i]) {
  217. free((void *) arr[i]);
  218. }
  219. }
  220. free(arr);
  221. }
  222. }
  223. /* }}} */
  224. static void v8js_jsext_dtor(v8js_jsext *jsext) /* {{{ */
  225. {
  226. if (jsext->deps_ht) {
  227. zend_hash_destroy(jsext->deps_ht);
  228. free(jsext->deps_ht);
  229. }
  230. if (jsext->deps) {
  231. v8js_free_ext_strarr(jsext->deps, jsext->deps_count);
  232. }
  233. delete jsext->extension;
  234. free(jsext->name);
  235. free(jsext->source);
  236. }
  237. /* }}} */
  238. static int v8js_create_ext_strarr(const char ***retval, int count, HashTable *ht) /* {{{ */
  239. {
  240. const char **exts = NULL;
  241. HashPosition pos;
  242. zval **tmp;
  243. int i = 0;
  244. exts = (const char **) calloc(1, count * sizeof(char *));
  245. zend_hash_internal_pointer_reset_ex(ht, &pos);
  246. while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &pos) == SUCCESS) {
  247. if (Z_TYPE_PP(tmp) == IS_STRING) {
  248. exts[i++] = zend_strndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
  249. } else {
  250. v8js_free_ext_strarr(exts, i);
  251. return FAILURE;
  252. }
  253. zend_hash_move_forward_ex(ht, &pos);
  254. }
  255. *retval = exts;
  256. return SUCCESS;
  257. }
  258. /* }}} */
  259. static void v8js_fatal_error_handler(const char *location, const char *message) /* {{{ */
  260. {
  261. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  262. if (isolate) {
  263. isolate->Exit();
  264. }
  265. if (location) {
  266. zend_error(E_ERROR, "%s %s", location, message);
  267. } else {
  268. zend_error(E_ERROR, "%s", message);
  269. }
  270. }
  271. /* }}} */
  272. #define IS_MAGIC_FUNC(mname) \
  273. ((key_len == sizeof(mname)) && \
  274. !strncasecmp(key, mname, key_len - 1))
  275. /* {{{ proto void V8Js::__construct([string object_name [, array variables [, array extensions [, bool report_uncaught_exceptions]]])
  276. __construct for V8Js */
  277. static PHP_METHOD(V8Js, __construct)
  278. {
  279. char *object_name = NULL, *class_name = NULL;
  280. int object_name_len = 0, free = 0;
  281. zend_uint class_name_len = 0;
  282. zend_bool report_uncaught = 1;
  283. zval *vars_arr = NULL, *exts_arr = NULL;
  284. const char **exts = NULL;
  285. int exts_count = 0;
  286. v8js_ctx *c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  287. if (!c->context.IsEmpty()) {
  288. /* called __construct() twice, bail out */
  289. return;
  290. }
  291. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|saab", &object_name, &object_name_len, &vars_arr, &exts_arr, &report_uncaught) == FAILURE) {
  292. return;
  293. }
  294. /* Initialize V8 */
  295. v8js_v8_init(TSRMLS_C);
  296. /* Throw PHP exception if uncaught exceptions exist */
  297. c->report_uncaught = report_uncaught;
  298. c->pending_exception = NULL;
  299. c->in_execution = 0;
  300. #if PHP_V8_API_VERSION >= 4004044
  301. static ArrayBufferAllocator array_buffer_allocator;
  302. static v8::Isolate::CreateParams create_params;
  303. create_params.array_buffer_allocator = &array_buffer_allocator;
  304. c->isolate = v8::Isolate::New(create_params);
  305. #else
  306. c->isolate = v8::Isolate::New();
  307. #endif
  308. c->isolate->SetData(0, c);
  309. c->time_limit = 0;
  310. c->time_limit_hit = false;
  311. c->memory_limit = 0;
  312. c->memory_limit_hit = false;
  313. c->module_normaliser = NULL;
  314. c->module_loader = NULL;
  315. /* Include extensions used by this context */
  316. /* Note: Extensions registered with auto_enable do not need to be added separately like this. */
  317. if (exts_arr)
  318. {
  319. exts_count = zend_hash_num_elements(Z_ARRVAL_P(exts_arr));
  320. if (v8js_create_ext_strarr(&exts, exts_count, Z_ARRVAL_P(exts_arr)) == FAILURE) {
  321. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid extensions array passed");
  322. return;
  323. }
  324. }
  325. /* Declare configuration for extensions */
  326. v8::ExtensionConfiguration extension_conf(exts_count, exts);
  327. // Isolate execution
  328. v8::Isolate *isolate = c->isolate;
  329. v8::Locker locker(isolate);
  330. v8::Isolate::Scope isolate_scope(isolate);
  331. /* Handle scope */
  332. v8::HandleScope handle_scope(isolate);
  333. /* Redirect fatal errors to PHP error handler */
  334. // This needs to be done within the context isolate
  335. v8::V8::SetFatalErrorHandler(v8js_fatal_error_handler);
  336. /* Create global template for global object */
  337. // Now we are using multiple isolates this needs to be created for every context
  338. v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(c->isolate, 0);
  339. tpl->SetClassName(V8JS_SYM("V8Js"));
  340. c->global_template.Reset(isolate, tpl);
  341. /* Register builtin methods */
  342. v8js_register_methods(tpl->InstanceTemplate(), c);
  343. /* Create context */
  344. v8::Local<v8::Context> context = v8::Context::New(isolate, &extension_conf, tpl->InstanceTemplate());
  345. if (exts) {
  346. v8js_free_ext_strarr(exts, exts_count);
  347. }
  348. /* If extensions have errors, context will be empty. (NOTE: This is V8 stuff, they expect the passed sources to compile :) */
  349. if (context.IsEmpty()) {
  350. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create V8 context. Check that registered extensions do not have errors.");
  351. ZVAL_NULL(getThis());
  352. return;
  353. }
  354. context->SetAlignedPointerInEmbedderData(1, c);
  355. c->context.Reset(isolate, context);
  356. /* Enter context */
  357. v8::Context::Scope context_scope(context);
  358. /* Create the PHP container object's function template */
  359. v8::Local<v8::FunctionTemplate> php_obj_t = v8::FunctionTemplate::New(isolate, 0);
  360. /* Set class name for PHP object */
  361. #if PHP_VERSION_ID >= 50400
  362. free = !zend_get_object_classname(getThis(), const_cast<const char**>(&class_name), &class_name_len TSRMLS_CC);
  363. #else
  364. free = !zend_get_object_classname(getThis(), &class_name, &class_name_len TSRMLS_CC);
  365. #endif
  366. php_obj_t->SetClassName(V8JS_SYML(class_name, class_name_len));
  367. if (free) {
  368. efree(class_name);
  369. }
  370. /* Register Get accessor for passed variables */
  371. if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
  372. v8js_register_accessors(&c->accessor_list, php_obj_t, vars_arr, isolate TSRMLS_CC);
  373. }
  374. /* Set name for the PHP JS object */
  375. v8::Local<v8::String> object_name_js = (object_name_len) ? V8JS_SYML(object_name, object_name_len) : V8JS_SYM("PHP");
  376. c->object_name.Reset(isolate, object_name_js);
  377. /* Add the PHP object into global object */
  378. v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance();
  379. V8JS_GLOBAL(isolate)->ForceSet(object_name_js, php_obj, v8::ReadOnly);
  380. /* Export public property values */
  381. HashTable *properties = zend_std_get_properties(getThis() TSRMLS_CC);
  382. HashPosition pos;
  383. zval **value;
  384. ulong index;
  385. char *member;
  386. uint member_len;
  387. for(zend_hash_internal_pointer_reset_ex(properties, &pos);
  388. zend_hash_get_current_data_ex(properties, (void **) &value, &pos) == SUCCESS;
  389. zend_hash_move_forward_ex(properties, &pos)) {
  390. if(zend_hash_get_current_key_ex(properties, &member, &member_len, &index, 0, &pos) != HASH_KEY_IS_STRING) {
  391. continue;
  392. }
  393. zval zmember;
  394. INIT_ZVAL(zmember);
  395. ZVAL_STRING(&zmember, member, 0);
  396. zend_property_info *property_info = zend_get_property_info(c->std.ce, &zmember, 1 TSRMLS_CC);
  397. if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
  398. /* Write value to PHP JS object */
  399. php_obj->ForceSet(V8JS_SYML(member, member_len - 1), zval_to_v8js(*value, isolate TSRMLS_CC), v8::ReadOnly);
  400. }
  401. }
  402. /* Add pointer to zend object */
  403. php_obj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), v8::External::New(isolate, getThis()));
  404. /* Export public methods */
  405. zend_function *method_ptr;
  406. char *key = NULL;
  407. uint key_len;
  408. zend_hash_internal_pointer_reset_ex(&c->std.ce->function_table, &pos);
  409. for (;; zend_hash_move_forward_ex(&c->std.ce->function_table, &pos)) {
  410. if (zend_hash_get_current_key_ex(&c->std.ce->function_table, &key, &key_len, &index, 0, &pos) != HASH_KEY_IS_STRING ||
  411. zend_hash_get_current_data_ex(&c->std.ce->function_table, (void **) &method_ptr, &pos) == FAILURE
  412. ) {
  413. break;
  414. }
  415. if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
  416. /* Allow only public methods */
  417. continue;
  418. }
  419. if ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) {
  420. /* no __construct, __destruct(), or __clone() functions */
  421. continue;
  422. }
  423. /* hide (do not export) other PHP magic functions */
  424. if (IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME) ||
  425. IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) ||
  426. IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) ||
  427. IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) ||
  428. IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME) ||
  429. IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) ||
  430. IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME) ||
  431. IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME) ||
  432. IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME) ||
  433. IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME) ||
  434. IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) {
  435. continue;
  436. }
  437. v8::Local<v8::String> method_name = V8JS_STR(method_ptr->common.function_name);
  438. v8::Local<v8::FunctionTemplate> ft;
  439. try {
  440. ft = v8::Local<v8::FunctionTemplate>::New
  441. (isolate, c->method_tmpls.at(method_ptr));
  442. }
  443. catch (const std::out_of_range &) {
  444. ft = v8::FunctionTemplate::New(isolate, v8js_php_callback,
  445. v8::External::New((isolate), method_ptr));
  446. // @fixme add/check Signature v8::Signature::New((isolate), tmpl));
  447. v8js_tmpl_t *persistent_ft = &c->method_tmpls[method_ptr];
  448. persistent_ft->Reset(isolate, ft);
  449. }
  450. php_obj->ForceSet(method_name, ft->GetFunction());
  451. }
  452. }
  453. /* }}} */
  454. /* {{{ proto V8JS::__sleep()
  455. */
  456. PHP_METHOD(V8Js, __sleep)
  457. {
  458. zend_throw_exception(php_ce_v8js_exception,
  459. "You cannot serialize or unserialize V8Js instances", 0 TSRMLS_CC);
  460. RETURN_FALSE;
  461. }
  462. /* }}} */
  463. /* {{{ proto V8JS::__wakeup()
  464. */
  465. PHP_METHOD(V8Js, __wakeup)
  466. {
  467. zend_throw_exception(php_ce_v8js_exception,
  468. "You cannot serialize or unserialize V8Js instances", 0 TSRMLS_CC);
  469. RETURN_FALSE;
  470. }
  471. /* }}} */
  472. static void v8js_compile_script(zval *this_ptr, const char *str, int str_len, const char *identifier, int identifier_len, v8js_script **ret TSRMLS_DC)
  473. {
  474. v8js_script *res = NULL;
  475. V8JS_BEGIN_CTX(c, this_ptr)
  476. /* Catch JS exceptions */
  477. v8::TryCatch try_catch;
  478. /* Set script identifier */
  479. v8::Local<v8::String> sname = identifier_len ? V8JS_SYML(identifier, identifier_len) : V8JS_SYM("V8Js::compileString()");
  480. /* Compiles a string context independently. TODO: Add a php function which calls this and returns the result as resource which can be executed later. */
  481. v8::Local<v8::String> source = V8JS_STRL(str, str_len);
  482. v8::Local<v8::Script> script = v8::Script::Compile(source, sname);
  483. /* Compile errors? */
  484. if (script.IsEmpty()) {
  485. v8js_throw_script_exception(c->isolate, &try_catch TSRMLS_CC);
  486. return;
  487. }
  488. res = (v8js_script *)emalloc(sizeof(v8js_script));
  489. res->script = new v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>>(c->isolate, script);
  490. v8::String::Utf8Value _sname(sname);
  491. res->name = estrndup(ToCString(_sname), _sname.length());
  492. res->ctx = c;
  493. *ret = res;
  494. return;
  495. }
  496. static void v8js_execute_script(zval *this_ptr, v8js_script *res, long flags, long time_limit, long memory_limit, zval **return_value TSRMLS_DC)
  497. {
  498. v8js_ctx *c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  499. if (res->ctx != c) {
  500. zend_error(E_WARNING, "Script resource from wrong V8Js object passed");
  501. ZVAL_BOOL(*return_value, 0);
  502. return;
  503. }
  504. if (!c->in_execution && time_limit == 0) {
  505. time_limit = c->time_limit;
  506. }
  507. if (!c->in_execution && memory_limit == 0) {
  508. memory_limit = c->memory_limit;
  509. }
  510. std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [res](v8::Isolate *isolate) {
  511. v8::Local<v8::Script> script = v8::Local<v8::Script>::New(isolate, *res->script);
  512. return script->Run();
  513. };
  514. v8js_v8_call(c, return_value, flags, time_limit, memory_limit, v8_call TSRMLS_CC);
  515. }
  516. /* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
  517. */
  518. static PHP_METHOD(V8Js, executeString)
  519. {
  520. char *str = NULL, *identifier = NULL, *tz = NULL;
  521. int str_len = 0, identifier_len = 0;
  522. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  523. v8js_script *res = NULL;
  524. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slll", &str, &str_len, &identifier, &identifier_len, &flags, &time_limit, &memory_limit) == FAILURE) {
  525. return;
  526. }
  527. v8js_compile_script(getThis(), str, str_len, identifier, identifier_len, &res TSRMLS_CC);
  528. if (!res) {
  529. RETURN_FALSE;
  530. }
  531. v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value TSRMLS_CC);
  532. v8js_script_free(res);
  533. efree(res);
  534. }
  535. /* }}} */
  536. /* {{{ proto mixed V8Js::compileString(string script [, string identifier])
  537. */
  538. static PHP_METHOD(V8Js, compileString)
  539. {
  540. char *str = NULL, *identifier = NULL;
  541. int str_len = 0, identifier_len = 0;
  542. v8js_script *res = NULL;
  543. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len, &identifier, &identifier_len) == FAILURE) {
  544. return;
  545. }
  546. v8js_compile_script(getThis(), str, str_len, identifier, identifier_len, &res TSRMLS_CC);
  547. if (res) {
  548. ZEND_REGISTER_RESOURCE(return_value, res, le_v8js_script);
  549. v8js_ctx *ctx;
  550. ctx = (v8js_ctx *) zend_object_store_get_object(this_ptr TSRMLS_CC);
  551. ctx->script_objects.push_back(res);
  552. }
  553. return;
  554. }
  555. /* }}} */
  556. /* {{{ proto mixed V8Js::executeScript(resource script [, int flags]])
  557. */
  558. static PHP_METHOD(V8Js, executeScript)
  559. {
  560. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  561. zval *zscript;
  562. v8js_script *res;
  563. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lll", &zscript, &flags, &time_limit, &memory_limit) == FAILURE) {
  564. return;
  565. }
  566. ZEND_FETCH_RESOURCE(res, v8js_script*, &zscript, -1, PHP_V8JS_SCRIPT_RES_NAME, le_v8js_script);
  567. ZEND_VERIFY_RESOURCE(res);
  568. v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value TSRMLS_CC);
  569. }
  570. /* }}} */
  571. /* {{{ proto mixed V8Js::checkString(string script)
  572. */
  573. static PHP_METHOD(V8Js, checkString)
  574. {
  575. char *str = NULL;
  576. int str_len = 0;
  577. const char *identifier = "V8Js::checkString()";
  578. int identifier_len = 19;
  579. v8js_script *res = NULL;
  580. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  581. return;
  582. }
  583. v8js_compile_script(getThis(), str, str_len, identifier, identifier_len, &res TSRMLS_CC);
  584. if (!res) {
  585. RETURN_FALSE;
  586. }
  587. v8js_script_free(res);
  588. efree(res);
  589. RETURN_TRUE;
  590. }
  591. /* }}} */
  592. /* {{{ proto mixed V8Js::getPendingException()
  593. */
  594. static PHP_METHOD(V8Js, getPendingException)
  595. {
  596. v8js_ctx *c;
  597. if (zend_parse_parameters_none() == FAILURE) {
  598. return;
  599. }
  600. c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  601. if (c->pending_exception) {
  602. RETURN_ZVAL(c->pending_exception, 1, 0);
  603. }
  604. }
  605. /* }}} */
  606. /* {{{ proto void V8Js::clearPendingException()
  607. */
  608. static PHP_METHOD(V8Js, clearPendingException)
  609. {
  610. v8js_ctx *c;
  611. if (zend_parse_parameters_none() == FAILURE) {
  612. return;
  613. }
  614. c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  615. if (c->pending_exception) {
  616. zval_ptr_dtor(&c->pending_exception);
  617. c->pending_exception = NULL;
  618. }
  619. }
  620. /* }}} */
  621. /* {{{ proto void V8Js::setModuleNormaliser(string base, string module_id)
  622. */
  623. static PHP_METHOD(V8Js, setModuleNormaliser)
  624. {
  625. v8js_ctx *c;
  626. zval *callable;
  627. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callable) == FAILURE) {
  628. return;
  629. }
  630. c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  631. c->module_normaliser = callable;
  632. Z_ADDREF_P(c->module_normaliser);
  633. }
  634. /* }}} */
  635. /* {{{ proto void V8Js::setModuleLoader(string module)
  636. */
  637. static PHP_METHOD(V8Js, setModuleLoader)
  638. {
  639. v8js_ctx *c;
  640. zval *callable;
  641. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callable) == FAILURE) {
  642. return;
  643. }
  644. c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  645. c->module_loader = callable;
  646. Z_ADDREF_P(c->module_loader);
  647. }
  648. /* }}} */
  649. /* {{{ proto void V8Js::setTimeLimit(int time_limit)
  650. */
  651. static PHP_METHOD(V8Js, setTimeLimit)
  652. {
  653. v8js_ctx *c;
  654. long time_limit = 0;
  655. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &time_limit) == FAILURE) {
  656. return;
  657. }
  658. c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  659. c->time_limit = time_limit;
  660. V8JSG(timer_mutex).lock();
  661. for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
  662. it != V8JSG(timer_stack).end(); it ++) {
  663. if((*it)->ctx == c && !(*it)->killed) {
  664. (*it)->time_limit = time_limit;
  665. // Calculate the time point when the time limit is exceeded
  666. std::chrono::milliseconds duration(time_limit);
  667. std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
  668. (*it)->time_point = from + duration;
  669. }
  670. }
  671. V8JSG(timer_mutex).unlock();
  672. if (c->in_execution && time_limit && !V8JSG(timer_thread)) {
  673. /* If timer thread is not started already and we now impose a time limit
  674. * finally install the timer. */
  675. V8JSG(timer_thread) = new std::thread(v8js_timer_thread TSRMLS_CC);
  676. }
  677. }
  678. /* }}} */
  679. /* {{{ proto void V8Js::setMemoryLimit(int memory_limit)
  680. */
  681. static PHP_METHOD(V8Js, setMemoryLimit)
  682. {
  683. v8js_ctx *c;
  684. long memory_limit = 0;
  685. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &memory_limit) == FAILURE) {
  686. return;
  687. }
  688. c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  689. c->memory_limit = memory_limit;
  690. V8JSG(timer_mutex).lock();
  691. for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
  692. it != V8JSG(timer_stack).end(); it ++) {
  693. if((*it)->ctx == c && !(*it)->killed) {
  694. (*it)->memory_limit = memory_limit;
  695. }
  696. }
  697. V8JSG(timer_mutex).unlock();
  698. if (c->in_execution && memory_limit && !V8JSG(timer_thread)) {
  699. /* If timer thread is not started already and we now impose a memory limit
  700. * finally install the timer. */
  701. V8JSG(timer_thread) = new std::thread(v8js_timer_thread TSRMLS_CC);
  702. }
  703. }
  704. /* }}} */
  705. static void v8js_persistent_zval_ctor(zval **p) /* {{{ */
  706. {
  707. zval *orig_ptr = *p;
  708. *p = (zval *) malloc(sizeof(zval));
  709. **p = *orig_ptr;
  710. switch (Z_TYPE_P(*p)) {
  711. case IS_STRING:
  712. Z_STRVAL_P(*p) = (char *) zend_strndup(Z_STRVAL_P(*p), Z_STRLEN_P(*p));
  713. break;
  714. default:
  715. zend_bailout();
  716. }
  717. INIT_PZVAL(*p);
  718. }
  719. /* }}} */
  720. static void v8js_persistent_zval_dtor(zval **p) /* {{{ */
  721. {
  722. switch (Z_TYPE_P(*p)) {
  723. case IS_STRING:
  724. free(Z_STRVAL_P(*p));
  725. break;
  726. default:
  727. zend_bailout();
  728. }
  729. free(*p);
  730. }
  731. /* }}} */
  732. static void v8js_script_free(v8js_script *res)
  733. {
  734. efree(res->name);
  735. delete res->script; // does Reset()
  736. }
  737. static void v8js_script_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
  738. {
  739. v8js_script *res = (v8js_script *)rsrc->ptr;
  740. if (res) {
  741. if(res->ctx) {
  742. std::vector<v8js_script *>::iterator it = std::find(res->ctx->script_objects.begin(), res->ctx->script_objects.end(), res);
  743. res->ctx->script_objects.erase(it);
  744. }
  745. v8js_script_free(res);
  746. efree(res);
  747. }
  748. }
  749. /* }}} */
  750. static int v8js_register_extension(char *name, uint name_len, char *source, uint source_len, zval *deps_arr, zend_bool auto_enable TSRMLS_DC) /* {{{ */
  751. {
  752. v8js_jsext *jsext = NULL;
  753. #ifdef ZTS
  754. v8js_process_globals.lock.lock();
  755. #endif
  756. if (!v8js_process_globals.extensions) {
  757. v8js_process_globals.extensions = (HashTable *) malloc(sizeof(HashTable));
  758. zend_hash_init(v8js_process_globals.extensions, 1, NULL, (dtor_func_t) v8js_jsext_dtor, 1);
  759. } else if (zend_hash_exists(v8js_process_globals.extensions, name, name_len + 1)) {
  760. #ifdef ZTS
  761. v8js_process_globals.lock.unlock();
  762. #endif
  763. return FAILURE;
  764. }
  765. jsext = (v8js_jsext *) calloc(1, sizeof(v8js_jsext));
  766. if (deps_arr) {
  767. jsext->deps_count = zend_hash_num_elements(Z_ARRVAL_P(deps_arr));
  768. if (v8js_create_ext_strarr(&jsext->deps, jsext->deps_count, Z_ARRVAL_P(deps_arr)) == FAILURE) {
  769. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid dependency array passed");
  770. v8js_jsext_dtor(jsext);
  771. free(jsext);
  772. #ifdef ZTS
  773. v8js_process_globals.lock.unlock();
  774. #endif
  775. return FAILURE;
  776. }
  777. }
  778. jsext->auto_enable = auto_enable;
  779. jsext->name = zend_strndup(name, name_len);
  780. jsext->source = zend_strndup(source, source_len);
  781. if (jsext->deps) {
  782. jsext->deps_ht = (HashTable *) malloc(sizeof(HashTable));
  783. zend_hash_init(jsext->deps_ht, jsext->deps_count, NULL, (dtor_func_t) v8js_persistent_zval_dtor, 1);
  784. zend_hash_copy(jsext->deps_ht, Z_ARRVAL_P(deps_arr), (copy_ctor_func_t) v8js_persistent_zval_ctor, NULL, sizeof(zval *));
  785. }
  786. jsext->extension = new v8::Extension(jsext->name, jsext->source, jsext->deps_count, jsext->deps);
  787. if (zend_hash_add(v8js_process_globals.extensions, name, name_len + 1, jsext, sizeof(v8js_jsext), NULL) == FAILURE) {
  788. v8js_jsext_dtor(jsext);
  789. free(jsext);
  790. #ifdef ZTS
  791. v8js_process_globals.lock.unlock();
  792. #endif
  793. return FAILURE;
  794. }
  795. #ifdef ZTS
  796. v8js_process_globals.lock.unlock();
  797. #endif
  798. jsext->extension->set_auto_enable(auto_enable ? true : false);
  799. v8::RegisterExtension(jsext->extension);
  800. free(jsext);
  801. return SUCCESS;
  802. }
  803. /* }}} */
  804. /* {{{ proto bool V8Js::registerExtension(string ext_name, string script [, array deps [, bool auto_enable]])
  805. */
  806. static PHP_METHOD(V8Js, registerExtension)
  807. {
  808. char *ext_name, *script;
  809. zval *deps_arr = NULL;
  810. int ext_name_len, script_len;
  811. zend_bool auto_enable = 0;
  812. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ab", &ext_name, &ext_name_len, &script, &script_len, &deps_arr, &auto_enable) == FAILURE) {
  813. return;
  814. }
  815. if (!ext_name_len) {
  816. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Extension name cannot be empty");
  817. } else if (!script_len) {
  818. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Script cannot be empty");
  819. } else if (v8js_register_extension(ext_name, ext_name_len, script, script_len, deps_arr, auto_enable TSRMLS_CC) == SUCCESS) {
  820. RETURN_TRUE;
  821. }
  822. RETURN_FALSE;
  823. }
  824. /* }}} */
  825. /* ## Static methods ## */
  826. /* {{{ proto array V8Js::getExtensions()
  827. */
  828. static PHP_METHOD(V8Js, getExtensions)
  829. {
  830. v8js_jsext *jsext;
  831. zval *ext, *deps_arr;
  832. HashPosition pos;
  833. ulong index;
  834. char *key;
  835. uint key_len;
  836. if (zend_parse_parameters_none() == FAILURE) {
  837. return;
  838. }
  839. array_init(return_value);
  840. #ifdef ZTS
  841. v8js_process_globals.lock.lock();
  842. #endif
  843. if (v8js_process_globals.extensions) {
  844. zend_hash_internal_pointer_reset_ex(v8js_process_globals.extensions, &pos);
  845. while (zend_hash_get_current_data_ex(v8js_process_globals.extensions, (void **) &jsext, &pos) == SUCCESS) {
  846. if (zend_hash_get_current_key_ex(v8js_process_globals.extensions, &key, &key_len, &index, 0, &pos) == HASH_KEY_IS_STRING) {
  847. MAKE_STD_ZVAL(ext)
  848. array_init(ext);
  849. add_assoc_bool_ex(ext, ZEND_STRS("auto_enable"), jsext->auto_enable);
  850. if (jsext->deps_ht) {
  851. MAKE_STD_ZVAL(deps_arr);
  852. array_init(deps_arr);
  853. zend_hash_copy(Z_ARRVAL_P(deps_arr), jsext->deps_ht, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
  854. add_assoc_zval_ex(ext, ZEND_STRS("deps"), deps_arr);
  855. }
  856. add_assoc_zval_ex(return_value, key, key_len, ext);
  857. }
  858. zend_hash_move_forward_ex(v8js_process_globals.extensions, &pos);
  859. }
  860. }
  861. #ifdef ZTS
  862. v8js_process_globals.lock.unlock();
  863. #endif
  864. }
  865. /* }}} */
  866. /* {{{ arginfo */
  867. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0)
  868. ZEND_ARG_INFO(0, object_name)
  869. ZEND_ARG_INFO(0, variables)
  870. ZEND_ARG_INFO(0, extensions)
  871. ZEND_ARG_INFO(0, report_uncaught_exceptions)
  872. ZEND_END_ARG_INFO()
  873. ZEND_BEGIN_ARG_INFO(arginfo_v8js_sleep, 0)
  874. ZEND_END_ARG_INFO()
  875. ZEND_BEGIN_ARG_INFO(arginfo_v8js_wakeup, 0)
  876. ZEND_END_ARG_INFO()
  877. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1)
  878. ZEND_ARG_INFO(0, script)
  879. ZEND_ARG_INFO(0, identifier)
  880. ZEND_ARG_INFO(0, flags)
  881. ZEND_ARG_INFO(0, time_limit)
  882. ZEND_ARG_INFO(0, memory_limit)
  883. ZEND_END_ARG_INFO()
  884. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_compilestring, 0, 0, 1)
  885. ZEND_ARG_INFO(0, script)
  886. ZEND_ARG_INFO(0, identifier)
  887. ZEND_END_ARG_INFO()
  888. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executescript, 0, 0, 1)
  889. ZEND_ARG_INFO(0, script)
  890. ZEND_ARG_INFO(0, flags)
  891. ZEND_ARG_INFO(0, time_limit)
  892. ZEND_ARG_INFO(0, memory_limit)
  893. ZEND_END_ARG_INFO()
  894. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_checkstring, 0, 0, 1)
  895. ZEND_ARG_INFO(0, script)
  896. ZEND_END_ARG_INFO()
  897. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getpendingexception, 0)
  898. ZEND_END_ARG_INFO()
  899. ZEND_BEGIN_ARG_INFO(arginfo_v8js_clearpendingexception, 0)
  900. ZEND_END_ARG_INFO()
  901. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmodulenormaliser, 0, 0, 2)
  902. ZEND_ARG_INFO(0, base)
  903. ZEND_ARG_INFO(0, module_id)
  904. ZEND_END_ARG_INFO()
  905. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
  906. ZEND_ARG_INFO(0, callable)
  907. ZEND_END_ARG_INFO()
  908. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_registerextension, 0, 0, 2)
  909. ZEND_ARG_INFO(0, extension_name)
  910. ZEND_ARG_INFO(0, script)
  911. ZEND_ARG_INFO(0, dependencies)
  912. ZEND_ARG_INFO(0, auto_enable)
  913. ZEND_END_ARG_INFO()
  914. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0)
  915. ZEND_END_ARG_INFO()
  916. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_settimelimit, 0, 0, 1)
  917. ZEND_ARG_INFO(0, time_limit)
  918. ZEND_END_ARG_INFO()
  919. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmemorylimit, 0, 0, 1)
  920. ZEND_ARG_INFO(0, memory_limit)
  921. ZEND_END_ARG_INFO()
  922. static const zend_function_entry v8js_methods[] = { /* {{{ */
  923. PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  924. PHP_ME(V8Js, __sleep, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  925. PHP_ME(V8Js, __wakeup, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  926. PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC)
  927. PHP_ME(V8Js, compileString, arginfo_v8js_compilestring, ZEND_ACC_PUBLIC)
  928. PHP_ME(V8Js, executeScript, arginfo_v8js_executescript, ZEND_ACC_PUBLIC)
  929. PHP_ME(V8Js, checkString, arginfo_v8js_checkstring, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
  930. PHP_ME(V8Js, getPendingException, arginfo_v8js_getpendingexception, ZEND_ACC_PUBLIC)
  931. PHP_ME(V8Js, clearPendingException, arginfo_v8js_clearpendingexception, ZEND_ACC_PUBLIC)
  932. PHP_ME(V8Js, setModuleNormaliser, arginfo_v8js_setmodulenormaliser, ZEND_ACC_PUBLIC)
  933. PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
  934. PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  935. PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  936. PHP_ME(V8Js, setTimeLimit, arginfo_v8js_settimelimit, ZEND_ACC_PUBLIC)
  937. PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC)
  938. {NULL, NULL, NULL}
  939. };
  940. /* }}} */
  941. /* V8Js object handlers */
  942. static void v8js_write_property(zval *object, zval *member, zval *value ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  943. {
  944. V8JS_BEGIN_CTX(c, object)
  945. /* Check whether member is public, if so, export to V8. */
  946. zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1 TSRMLS_CC);
  947. if(property_info->flags & ZEND_ACC_PUBLIC) {
  948. /* Global PHP JS object */
  949. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  950. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject();
  951. /* Write value to PHP JS object */
  952. jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, isolate TSRMLS_CC), v8::ReadOnly);
  953. }
  954. /* Write value to PHP object */
  955. std_object_handlers.write_property(object, member, value ZEND_HASH_KEY_CC TSRMLS_CC);
  956. }
  957. /* }}} */
  958. static void v8js_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  959. {
  960. V8JS_BEGIN_CTX(c, object)
  961. /* Global PHP JS object */
  962. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  963. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject();
  964. /* Delete value from PHP JS object */
  965. jsobj->Delete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
  966. /* Unset from PHP object */
  967. std_object_handlers.unset_property(object, member ZEND_HASH_KEY_CC TSRMLS_CC);
  968. }
  969. /* }}} */
  970. PHP_MINIT_FUNCTION(v8js_class) /* {{{ */
  971. {
  972. zend_class_entry ce;
  973. /* V8Js Class */
  974. INIT_CLASS_ENTRY(ce, "V8Js", v8js_methods);
  975. php_ce_v8js = zend_register_internal_class(&ce TSRMLS_CC);
  976. php_ce_v8js->create_object = v8js_new;
  977. /* V8Js handlers */
  978. memcpy(&v8js_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  979. v8js_object_handlers.clone_obj = NULL;
  980. v8js_object_handlers.write_property = v8js_write_property;
  981. v8js_object_handlers.unset_property = v8js_unset_property;
  982. /* V8Js Class Constants */
  983. zend_declare_class_constant_string(php_ce_v8js, ZEND_STRL("V8_VERSION"), PHP_V8_VERSION TSRMLS_CC);
  984. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE TSRMLS_CC);
  985. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY TSRMLS_CC);
  986. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_PROPAGATE_PHP_EXCEPTIONS"), V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS TSRMLS_CC);
  987. le_v8js_script = zend_register_list_destructors_ex(v8js_script_dtor, NULL, PHP_V8JS_SCRIPT_RES_NAME, module_number);
  988. #if PHP_V8_API_VERSION >= 4004010 && PHP_V8_API_VERSION < 4004044
  989. static ArrayBufferAllocator array_buffer_allocator;
  990. v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
  991. #endif
  992. return SUCCESS;
  993. } /* }}} */
  994. /*
  995. * Local variables:
  996. * tab-width: 4
  997. * c-basic-offset: 4
  998. * End:
  999. * vim600: noet sw=4 ts=4 fdm=marker
  1000. * vim<600: noet sw=4 ts=4
  1001. */