v8js_class.cc 38 KB

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