v8js_class.cc 36 KB

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