v8js_class.cc 38 KB

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