v8js_class.cc 38 KB

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