v8js_class.cc 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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. #include <functional>
  18. #include <algorithm>
  19. #include "php_v8js_macros.h"
  20. #include "v8js_v8.h"
  21. #include "v8js_exceptions.h"
  22. #include "v8js_v8object_class.h"
  23. #include "v8js_object_export.h"
  24. #include "v8js_timer.h"
  25. extern "C" {
  26. #include "php.h"
  27. #include "ext/date/php_date.h"
  28. #include "ext/standard/php_string.h"
  29. #include "zend_interfaces.h"
  30. #include "zend_closures.h"
  31. #include "ext/spl/spl_exceptions.h"
  32. #include "zend_exceptions.h"
  33. }
  34. /* On Windows there are max and min macros, which would clobber the
  35. * method names of std::numeric_limits< > otherwise. */
  36. #undef max
  37. #undef min
  38. #define PHP_V8JS_SCRIPT_RES_NAME "V8Js script"
  39. /* {{{ Class Entries */
  40. static zend_class_entry *php_ce_v8js;
  41. /* }}} */
  42. /* {{{ Object Handlers */
  43. static zend_object_handlers v8js_object_handlers;
  44. /* }}} */
  45. /* Forward declare v8js_methods, actually "static" but not possible in C++ */
  46. extern const zend_function_entry v8js_methods[];
  47. typedef struct _v8js_script {
  48. char *name;
  49. v8js_ctx *ctx;
  50. v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>> *script;
  51. } v8js_script;
  52. static void v8js_script_free(v8js_script *res);
  53. int le_v8js_script;
  54. /* {{{ Extension container */
  55. struct v8js_jsext {
  56. zend_bool auto_enable;
  57. HashTable *deps_ht;
  58. const char **deps;
  59. int deps_count;
  60. zend_string *name;
  61. zend_string *source;
  62. v8::Extension *extension;
  63. };
  64. /* }}} */
  65. class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
  66. public:
  67. virtual void* Allocate(size_t length) {
  68. void* data = AllocateUninitialized(length);
  69. return data == NULL ? data : memset(data, 0, length);
  70. }
  71. virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
  72. virtual void Free(void* data, size_t) { free(data); }
  73. };
  74. static void v8js_free_storage(zend_object *object TSRMLS_DC) /* {{{ */
  75. {
  76. v8js_ctx *c = v8js_ctx_fetch_object(object);
  77. zend_object_std_dtor(&c->std TSRMLS_CC);
  78. zval_ptr_dtor(&c->pending_exception);
  79. zval_ptr_dtor(&c->module_normaliser);
  80. zval_ptr_dtor(&c->module_loader);
  81. /* Delete PHP global object from JavaScript */
  82. if (!c->context.IsEmpty()) {
  83. v8::Locker locker(c->isolate);
  84. v8::Isolate::Scope isolate_scope(c->isolate);
  85. v8::HandleScope handle_scope(c->isolate);
  86. v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(c->isolate, c->context);
  87. v8::Context::Scope context_scope(v8_context);
  88. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
  89. V8JS_GLOBAL(c->isolate)->Delete(object_name_js);
  90. }
  91. c->object_name.Reset();
  92. c->object_name.~Persistent();
  93. c->global_template.Reset();
  94. c->global_template.~Persistent();
  95. c->array_tmpl.Reset();
  96. c->array_tmpl.~Persistent();
  97. /* Clear persistent call_impl & method_tmpls templates */
  98. for (std::map<v8js_tmpl_t *, v8js_tmpl_t>::iterator it = c->call_impls.begin();
  99. it != c->call_impls.end(); ++it) {
  100. // No need to free it->first, as it is stored in c->template_cache and freed below
  101. it->second.Reset();
  102. }
  103. c->call_impls.~map();
  104. for (std::map<zend_function *, v8js_tmpl_t>::iterator it = c->method_tmpls.begin();
  105. it != c->method_tmpls.end(); ++it) {
  106. it->second.Reset();
  107. }
  108. c->method_tmpls.~map();
  109. /* Clear persistent handles in template cache */
  110. for (std::map<const zend_string *,v8js_tmpl_t>::iterator it = c->template_cache.begin();
  111. it != c->template_cache.end(); ++it) {
  112. it->second.Reset();
  113. }
  114. c->template_cache.~map();
  115. /* Clear contexts */
  116. for (std::vector<v8js_accessor_ctx*>::iterator it = c->accessor_list.begin();
  117. it != c->accessor_list.end(); ++it) {
  118. v8js_accessor_ctx_dtor(*it TSRMLS_CC);
  119. }
  120. c->accessor_list.~vector();
  121. /* Clear global object, dispose context */
  122. if (!c->context.IsEmpty()) {
  123. c->context.Reset();
  124. }
  125. c->context.~Persistent();
  126. /* Dispose yet undisposed weak refs */
  127. for (std::map<zend_object *, v8js_persistent_obj_t>::iterator it = c->weak_objects.begin();
  128. it != c->weak_objects.end(); ++it) {
  129. zend_object *object = it->first;
  130. zval value;
  131. ZVAL_OBJ(&value, object);
  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. zval_ptr_dtor(&c->zval_snapshot_blob);
  175. }
  176. /* }}} */
  177. static zend_object* v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  178. {
  179. v8js_ctx *c;
  180. c = (v8js_ctx *) ecalloc(1, sizeof(*c) + zend_object_properties_size(ce));
  181. zend_object_std_init(&c->std, ce TSRMLS_CC);
  182. object_properties_init(&c->std, ce);
  183. c->std.handlers = &v8js_object_handlers;
  184. TSRMLS_SET_CTX(c->zts_ctx);
  185. new(&c->object_name) v8::Persistent<v8::String>();
  186. new(&c->context) v8::Persistent<v8::Context>();
  187. new(&c->global_template) v8::Persistent<v8::FunctionTemplate>();
  188. new(&c->array_tmpl) v8::Persistent<v8::FunctionTemplate>();
  189. new(&c->modules_stack) std::vector<char*>();
  190. new(&c->modules_base) std::vector<char*>();
  191. new(&c->modules_loaded) std::map<char *, v8js_persistent_obj_t, cmp_str>;
  192. new(&c->template_cache) std::map<const zend_string *,v8js_tmpl_t>();
  193. new(&c->accessor_list) std::vector<v8js_accessor_ctx *>();
  194. new(&c->weak_closures) std::map<v8js_tmpl_t *, v8js_persistent_obj_t>();
  195. new(&c->weak_objects) std::map<zend_object *, v8js_persistent_obj_t>();
  196. new(&c->call_impls) std::map<v8js_tmpl_t *, v8js_tmpl_t>();
  197. new(&c->method_tmpls) std::map<zend_function *, v8js_tmpl_t>();
  198. new(&c->v8js_v8objects) std::list<v8js_v8object *>();
  199. new(&c->script_objects) std::vector<v8js_script *>();
  200. // @fixme following is const, run on startup
  201. v8js_object_handlers.offset = XtOffsetOf(struct v8js_ctx, std);
  202. v8js_object_handlers.free_obj = v8js_free_storage;
  203. c->average_object_size = 1024;
  204. return &c->std;
  205. }
  206. /* }}} */
  207. static void v8js_free_ext_strarr(const char **arr, int count) /* {{{ */
  208. {
  209. int i;
  210. if (arr) {
  211. for (i = 0; i < count; i++) {
  212. if (arr[i]) {
  213. free((void *) arr[i]);
  214. }
  215. }
  216. free(arr);
  217. }
  218. }
  219. /* }}} */
  220. static void v8js_jsext_free_storage(v8js_jsext *jsext) /* {{{ */
  221. {
  222. if (jsext->deps_ht) {
  223. zend_hash_destroy(jsext->deps_ht);
  224. free(jsext->deps_ht);
  225. }
  226. if (jsext->deps) {
  227. v8js_free_ext_strarr(jsext->deps, jsext->deps_count);
  228. }
  229. delete jsext->extension;
  230. // Free the persisted non-interned strings we allocated.
  231. zend_string_release(jsext->name);
  232. zend_string_release(jsext->source);
  233. free(jsext);
  234. }
  235. /* }}} */
  236. static void v8js_jsext_dtor(zval *zv) /* {{{ */
  237. {
  238. v8js_jsext_free_storage(reinterpret_cast<v8js_jsext *>(Z_PTR_P(zv)));
  239. }
  240. /* }}} */
  241. static int v8js_create_ext_strarr(const char ***retval, int count, HashTable *ht) /* {{{ */
  242. {
  243. const char **exts = NULL;
  244. HashPosition pos;
  245. zval *tmp;
  246. int i = 0;
  247. exts = (const char **) calloc(1, count * sizeof(char *));
  248. zend_hash_internal_pointer_reset_ex(ht, &pos);
  249. while ((tmp = zend_hash_get_current_data_ex(ht, &pos))) {
  250. if (Z_TYPE_P(tmp) == IS_STRING) {
  251. exts[i++] = zend_strndup(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  252. } else {
  253. v8js_free_ext_strarr(exts, i);
  254. return FAILURE;
  255. }
  256. zend_hash_move_forward_ex(ht, &pos);
  257. }
  258. *retval = exts;
  259. return SUCCESS;
  260. }
  261. /* }}} */
  262. static void v8js_fatal_error_handler(const char *location, const char *message) /* {{{ */
  263. {
  264. if (location) {
  265. zend_error(E_WARNING, "Fatal V8 error in %s: %s", location, message);
  266. } else {
  267. zend_error(E_WARNING, "Fatal V8 error: %s", message);
  268. }
  269. }
  270. /* }}} */
  271. #define IS_MAGIC_FUNC(mname) \
  272. ((ZSTR_LEN(key) == sizeof(mname) - 1) && \
  273. !strncasecmp(ZSTR_VAL(key), mname, ZSTR_LEN(key)))
  274. /* {{{ proto void V8Js::__construct([string object_name [, array variables [, array extensions [, bool report_uncaught_exceptions [, string snapshot_blob]]]]])
  275. __construct for V8Js */
  276. static PHP_METHOD(V8Js, __construct)
  277. {
  278. zend_string *object_name = NULL;
  279. zend_bool report_uncaught = 1;
  280. zval *vars_arr = NULL, *exts_arr = NULL;
  281. const char **exts = NULL;
  282. int exts_count = 0;
  283. zval *snapshot_blob = NULL;
  284. v8js_ctx *c = Z_V8JS_CTX_OBJ_P(getThis())
  285. if (!c->context.IsEmpty()) {
  286. /* called __construct() twice, bail out */
  287. return;
  288. }
  289. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|Saabz", &object_name, &vars_arr, &exts_arr, &report_uncaught, &snapshot_blob) == FAILURE) {
  290. return;
  291. }
  292. /* Initialize V8 */
  293. v8js_v8_init(TSRMLS_C);
  294. /* Throw PHP exception if uncaught exceptions exist */
  295. c->report_uncaught = report_uncaught;
  296. ZVAL_NULL(&c->pending_exception);
  297. c->in_execution = 0;
  298. new (&c->create_params) v8::Isolate::CreateParams();
  299. static ArrayBufferAllocator array_buffer_allocator;
  300. c->create_params.array_buffer_allocator = &array_buffer_allocator;
  301. new (&c->snapshot_blob) v8::StartupData();
  302. if (snapshot_blob) {
  303. if (Z_TYPE_P(snapshot_blob) == IS_STRING) {
  304. ZVAL_COPY(&c->zval_snapshot_blob, snapshot_blob);
  305. if (Z_STRLEN_P(snapshot_blob) > std::numeric_limits<int>::max()) {
  306. zend_throw_exception(php_ce_v8js_exception,
  307. "Snapshot size exceeds maximum supported length", 0);
  308. return;
  309. }
  310. c->snapshot_blob.data = Z_STRVAL_P(snapshot_blob);
  311. c->snapshot_blob.raw_size = static_cast<int>(Z_STRLEN_P(snapshot_blob));
  312. c->create_params.snapshot_blob = &c->snapshot_blob;
  313. } else {
  314. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument snapshot_blob expected to be of string type");
  315. }
  316. }
  317. c->isolate = v8::Isolate::New(c->create_params);
  318. c->isolate->SetData(0, c);
  319. c->time_limit = 0;
  320. c->time_limit_hit = false;
  321. c->memory_limit = 0;
  322. c->memory_limit_hit = false;
  323. ZVAL_NULL(&c->module_normaliser);
  324. ZVAL_NULL(&c->module_loader);
  325. /* Include extensions used by this context */
  326. /* Note: Extensions registered with auto_enable do not need to be added separately like this. */
  327. if (exts_arr)
  328. {
  329. exts_count = zend_hash_num_elements(Z_ARRVAL_P(exts_arr));
  330. if (v8js_create_ext_strarr(&exts, exts_count, Z_ARRVAL_P(exts_arr)) == FAILURE) {
  331. zend_throw_exception(php_ce_v8js_exception,
  332. "Invalid extensions array passed", 0);
  333. return;
  334. }
  335. }
  336. /* Declare configuration for extensions */
  337. v8::ExtensionConfiguration extension_conf(exts_count, exts);
  338. // Isolate execution
  339. v8::Isolate *isolate = c->isolate;
  340. v8::Locker locker(isolate);
  341. v8::Isolate::Scope isolate_scope(isolate);
  342. /* Handle scope */
  343. v8::HandleScope handle_scope(isolate);
  344. /* Redirect fatal errors to PHP error handler */
  345. // This needs to be done within the context isolate
  346. v8::V8::SetFatalErrorHandler(v8js_fatal_error_handler);
  347. /* Create global template for global object */
  348. // Now we are using multiple isolates this needs to be created for every context
  349. v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(c->isolate, 0);
  350. tpl->SetClassName(V8JS_SYM("V8Js"));
  351. c->global_template.Reset(isolate, tpl);
  352. /* Register builtin methods */
  353. v8js_register_methods(tpl->InstanceTemplate(), c);
  354. /* Create context */
  355. v8::Local<v8::Context> context = v8::Context::New(isolate, &extension_conf, tpl->InstanceTemplate());
  356. if (exts) {
  357. v8js_free_ext_strarr(exts, exts_count);
  358. }
  359. /* If extensions have errors, context will be empty. (NOTE: This is V8 stuff, they expect the passed sources to compile :) */
  360. if (context.IsEmpty()) {
  361. zend_throw_exception(php_ce_v8js_exception,
  362. "Failed to create V8 context. "
  363. "Check that registered extensions do not have errors.", 0);
  364. return;
  365. }
  366. context->SetAlignedPointerInEmbedderData(1, c);
  367. c->context.Reset(isolate, context);
  368. /* Enter context */
  369. v8::Context::Scope context_scope(context);
  370. /* Create the PHP container object's function template */
  371. v8::Local<v8::FunctionTemplate> php_obj_t = v8::FunctionTemplate::New(isolate, 0);
  372. /* Set class name for PHP object */
  373. zend_class_entry *ce = Z_OBJCE_P(getThis());
  374. if (ZSTR_LEN(ce->name) > std::numeric_limits<int>::max()) {
  375. zend_throw_exception(php_ce_v8js_exception,
  376. "PHP object class name exceeds maximum supported length", 0);
  377. return;
  378. }
  379. php_obj_t->SetClassName(V8JS_SYML(ZSTR_VAL(ce->name), static_cast<int>(ZSTR_LEN(ce->name))));
  380. /* Register Get accessor for passed variables */
  381. if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
  382. v8js_register_accessors(&c->accessor_list, php_obj_t, vars_arr, isolate TSRMLS_CC);
  383. }
  384. /* Set name for the PHP JS object */
  385. v8::Local<v8::String> object_name_js;
  386. if (object_name && ZSTR_LEN(object_name)) {
  387. if (ZSTR_LEN(object_name) > std::numeric_limits<int>::max()) {
  388. zend_throw_exception(php_ce_v8js_exception,
  389. "PHP JS object class name exceeds maximum supported length", 0);
  390. return;
  391. }
  392. object_name_js = v8::String::NewFromUtf8(isolate, ZSTR_VAL(object_name),
  393. v8::String::kInternalizedString, static_cast<int>(ZSTR_LEN(object_name)));
  394. }
  395. else {
  396. object_name_js = V8JS_SYM("PHP");
  397. }
  398. c->object_name.Reset(isolate, object_name_js);
  399. /* Add the PHP object into global object */
  400. php_obj_t->InstanceTemplate()->SetInternalFieldCount(2);
  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. zval *value;
  406. zend_string *member;
  407. ZEND_HASH_FOREACH_STR_KEY(properties, member) {
  408. zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1 TSRMLS_CC);
  409. if(property_info &&
  410. property_info != ZEND_WRONG_PROPERTY_INFO &&
  411. (property_info->flags & ZEND_ACC_PUBLIC)) {
  412. if (ZSTR_LEN(member) > std::numeric_limits<int>::max()) {
  413. zend_throw_exception(php_ce_v8js_exception,
  414. "Property name exceeds maximum supported length", 0);
  415. return;
  416. }
  417. v8::Local<v8::Value> key = v8::String::NewFromUtf8(isolate, ZSTR_VAL(member),
  418. v8::String::kInternalizedString, static_cast<int>(ZSTR_LEN(member)));
  419. /* Write value to PHP JS object */
  420. value = OBJ_PROP(Z_OBJ_P(getThis()), property_info->offset);
  421. php_obj->ForceSet(key, zval_to_v8js(value, isolate TSRMLS_CC), v8::ReadOnly);
  422. }
  423. } ZEND_HASH_FOREACH_END();
  424. /* Add pointer to zend object */
  425. php_obj->SetAlignedPointerInInternalField(1, Z_OBJ_P(getThis()));
  426. /* Export public methods */
  427. void *ptr;
  428. zend_string *key;
  429. ZEND_HASH_FOREACH_STR_KEY_PTR(&c->std.ce->function_table, key, ptr) {
  430. zend_function *method_ptr = reinterpret_cast<zend_function *>(ptr);
  431. if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
  432. /* Allow only public methods */
  433. continue;
  434. }
  435. if ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) {
  436. /* no __construct, __destruct(), or __clone() functions */
  437. continue;
  438. }
  439. /* hide (do not export) other PHP magic functions */
  440. if (IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME) ||
  441. IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) ||
  442. IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) ||
  443. IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) ||
  444. IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME) ||
  445. IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) ||
  446. IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME) ||
  447. IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME) ||
  448. IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME) ||
  449. IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME) ||
  450. IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) {
  451. continue;
  452. }
  453. const zend_function_entry *fe;
  454. for (fe = v8js_methods; fe->fname; fe ++) {
  455. if (strcmp(fe->fname, ZSTR_VAL(method_ptr->common.function_name)) == 0) {
  456. break;
  457. }
  458. }
  459. if(fe->fname) {
  460. /* Method belongs to \V8Js class itself, never export to V8, even if
  461. * it is overriden in a derived class. */
  462. continue;
  463. }
  464. if (ZSTR_LEN(method_ptr->common.function_name) > std::numeric_limits<int>::max()) {
  465. zend_throw_exception(php_ce_v8js_exception,
  466. "Method name exceeds maximum supported length", 0);
  467. return;
  468. }
  469. v8::Local<v8::String> method_name = v8::String::NewFromUtf8(isolate,
  470. ZSTR_VAL(method_ptr->common.function_name), v8::String::kInternalizedString,
  471. static_cast<int>(ZSTR_LEN(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. } ZEND_HASH_FOREACH_END();
  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 zend_string *str, const zend_string *identifier, 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. if (identifier && ZSTR_LEN(identifier) > std::numeric_limits<int>::max()) {
  514. zend_throw_exception(php_ce_v8js_exception,
  515. "Script identifier exceeds maximum supported length", 0);
  516. return;
  517. }
  518. v8::Local<v8::String> sname = identifier
  519. ? v8::String::NewFromUtf8(isolate, ZSTR_VAL(identifier), v8::String::kNormalString, static_cast<int>(ZSTR_LEN(identifier)))
  520. : V8JS_SYM("V8Js::compileString()");
  521. /* Compiles a string context independently. TODO: Add a php function which calls this and returns the result as resource which can be executed later. */
  522. if (ZSTR_LEN(str) > std::numeric_limits<int>::max()) {
  523. zend_throw_exception(php_ce_v8js_exception,
  524. "Script source exceeds maximum supported length", 0);
  525. return;
  526. }
  527. v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, ZSTR_VAL(str), v8::String::kNormalString, static_cast<int>(ZSTR_LEN(str)));
  528. v8::Local<v8::Script> script = v8::Script::Compile(source, sname);
  529. /* Compile errors? */
  530. if (script.IsEmpty()) {
  531. v8js_throw_script_exception(c->isolate, &try_catch TSRMLS_CC);
  532. return;
  533. }
  534. res = (v8js_script *)emalloc(sizeof(v8js_script));
  535. res->script = new v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>>(c->isolate, script);
  536. v8::String::Utf8Value _sname(sname);
  537. res->name = estrndup(ToCString(_sname), _sname.length());
  538. res->ctx = c;
  539. *ret = res;
  540. return;
  541. }
  542. static void v8js_execute_script(zval *this_ptr, v8js_script *res, long flags, long time_limit, long memory_limit, zval **return_value TSRMLS_DC)
  543. {
  544. v8js_ctx *c = Z_V8JS_CTX_OBJ_P(this_ptr);
  545. if (res->ctx != c) {
  546. zend_error(E_WARNING, "Script resource from wrong V8Js object passed");
  547. ZVAL_BOOL(*return_value, 0);
  548. return;
  549. }
  550. if (!c->in_execution && time_limit == 0) {
  551. time_limit = c->time_limit;
  552. }
  553. if (!c->in_execution && memory_limit == 0) {
  554. memory_limit = c->memory_limit;
  555. }
  556. /* std::function relies on its dtor to be executed, otherwise it leaks
  557. * some memory on bailout. */
  558. {
  559. std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [res](v8::Isolate *isolate) {
  560. v8::Local<v8::Script> script = v8::Local<v8::Script>::New(isolate, *res->script);
  561. return script->Run();
  562. };
  563. v8js_v8_call(c, return_value, flags, time_limit, memory_limit, v8_call TSRMLS_CC);
  564. }
  565. if(V8JSG(fatal_error_abort)) {
  566. /* Check for fatal error marker possibly set by v8js_error_handler; just
  567. * rethrow the error since we're now out of V8. */
  568. zend_bailout();
  569. }
  570. }
  571. /* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
  572. */
  573. static PHP_METHOD(V8Js, executeString)
  574. {
  575. zend_string *str = NULL, *identifier = NULL;
  576. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  577. v8js_script *res = NULL;
  578. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|Slll", &str, &identifier, &flags, &time_limit, &memory_limit) == FAILURE) {
  579. return;
  580. }
  581. v8js_compile_script(getThis(), str, identifier, &res TSRMLS_CC);
  582. if (!res) {
  583. RETURN_FALSE;
  584. }
  585. zend_try {
  586. v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value TSRMLS_CC);
  587. v8js_script_free(res);
  588. }
  589. zend_catch {
  590. v8js_script_free(res);
  591. zend_bailout();
  592. }
  593. zend_end_try()
  594. efree(res);
  595. }
  596. /* }}} */
  597. /* {{{ proto mixed V8Js::compileString(string script [, string identifier])
  598. */
  599. static PHP_METHOD(V8Js, compileString)
  600. {
  601. zend_string *str = NULL, *identifier = NULL;
  602. v8js_script *res = NULL;
  603. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|S", &str, &identifier) == FAILURE) {
  604. return;
  605. }
  606. v8js_compile_script(getThis(), str, identifier, &res TSRMLS_CC);
  607. if (res) {
  608. RETVAL_RES(zend_register_resource(res, le_v8js_script));
  609. v8js_ctx *ctx;
  610. ctx = Z_V8JS_CTX_OBJ_P(getThis());
  611. ctx->script_objects.push_back(res);
  612. }
  613. }
  614. /* }}} */
  615. /* {{{ proto mixed V8Js::executeScript(resource script [, int flags]])
  616. */
  617. static PHP_METHOD(V8Js, executeScript)
  618. {
  619. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  620. zval *zscript;
  621. v8js_script *res;
  622. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lll", &zscript, &flags, &time_limit, &memory_limit) == FAILURE) {
  623. return;
  624. }
  625. if((res = (v8js_script *)zend_fetch_resource(Z_RES_P(zscript), PHP_V8JS_SCRIPT_RES_NAME, le_v8js_script)) == NULL) {
  626. RETURN_FALSE;
  627. }
  628. v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value TSRMLS_CC);
  629. }
  630. /* }}} */
  631. /* {{{ proto mixed V8Js::checkString(string script)
  632. */
  633. static PHP_METHOD(V8Js, checkString)
  634. {
  635. zend_string *str = NULL;
  636. zend_string *identifier = zend_string_init("V8Js::checkString()", 19, 0);
  637. v8js_script *res = NULL;
  638. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &str) == FAILURE) {
  639. return;
  640. }
  641. v8js_compile_script(getThis(), str, identifier, &res TSRMLS_CC);
  642. zend_string_release(identifier);
  643. if (!res) {
  644. RETURN_FALSE;
  645. }
  646. v8js_script_free(res);
  647. efree(res);
  648. RETURN_TRUE;
  649. }
  650. /* }}} */
  651. /* {{{ proto mixed V8Js::getPendingException()
  652. */
  653. static PHP_METHOD(V8Js, getPendingException)
  654. {
  655. v8js_ctx *c;
  656. if (zend_parse_parameters_none() == FAILURE) {
  657. return;
  658. }
  659. c = Z_V8JS_CTX_OBJ_P(getThis());
  660. if (Z_TYPE(c->pending_exception) == IS_OBJECT) {
  661. RETURN_ZVAL(&c->pending_exception, 1, 0);
  662. }
  663. }
  664. /* }}} */
  665. /* {{{ proto void V8Js::clearPendingException()
  666. */
  667. static PHP_METHOD(V8Js, clearPendingException)
  668. {
  669. v8js_ctx *c;
  670. if (zend_parse_parameters_none() == FAILURE) {
  671. return;
  672. }
  673. c = Z_V8JS_CTX_OBJ_P(getThis());
  674. if (Z_TYPE(c->pending_exception) == IS_OBJECT) {
  675. zval_ptr_dtor(&c->pending_exception);
  676. ZVAL_NULL(&c->pending_exception);
  677. }
  678. }
  679. /* }}} */
  680. /* {{{ proto void V8Js::setModuleNormaliser(string base, string module_id)
  681. */
  682. static PHP_METHOD(V8Js, setModuleNormaliser)
  683. {
  684. v8js_ctx *c;
  685. zval *callable;
  686. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callable) == FAILURE) {
  687. return;
  688. }
  689. c = Z_V8JS_CTX_OBJ_P(getThis());
  690. ZVAL_COPY(&c->module_normaliser, callable);
  691. }
  692. /* }}} */
  693. /* {{{ proto void V8Js::setModuleLoader(string module)
  694. */
  695. static PHP_METHOD(V8Js, setModuleLoader)
  696. {
  697. v8js_ctx *c;
  698. zval *callable;
  699. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callable) == FAILURE) {
  700. return;
  701. }
  702. c = Z_V8JS_CTX_OBJ_P(getThis());
  703. ZVAL_COPY(&c->module_loader, callable);
  704. }
  705. /* }}} */
  706. /* {{{ proto void V8Js::setTimeLimit(int time_limit)
  707. */
  708. static PHP_METHOD(V8Js, setTimeLimit)
  709. {
  710. v8js_ctx *c;
  711. long time_limit = 0;
  712. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &time_limit) == FAILURE) {
  713. return;
  714. }
  715. c = Z_V8JS_CTX_OBJ_P(getThis());
  716. c->time_limit = time_limit;
  717. V8JSG(timer_mutex).lock();
  718. for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
  719. it != V8JSG(timer_stack).end(); it ++) {
  720. if((*it)->ctx == c && !(*it)->killed) {
  721. (*it)->time_limit = time_limit;
  722. // Calculate the time point when the time limit is exceeded
  723. std::chrono::milliseconds duration(time_limit);
  724. std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
  725. (*it)->time_point = from + duration;
  726. }
  727. }
  728. V8JSG(timer_mutex).unlock();
  729. if (c->in_execution && time_limit && !V8JSG(timer_thread)) {
  730. /* If timer thread is not started already and we now impose a time limit
  731. * finally install the timer. */
  732. V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js));
  733. }
  734. }
  735. /* }}} */
  736. /* {{{ proto void V8Js::setMemoryLimit(int memory_limit)
  737. */
  738. static PHP_METHOD(V8Js, setMemoryLimit)
  739. {
  740. v8js_ctx *c;
  741. long memory_limit = 0;
  742. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &memory_limit) == FAILURE) {
  743. return;
  744. }
  745. c = Z_V8JS_CTX_OBJ_P(getThis());
  746. c->memory_limit = memory_limit;
  747. V8JSG(timer_mutex).lock();
  748. for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
  749. it != V8JSG(timer_stack).end(); it ++) {
  750. if((*it)->ctx == c && !(*it)->killed) {
  751. (*it)->memory_limit = memory_limit;
  752. }
  753. }
  754. V8JSG(timer_mutex).unlock();
  755. if (c->in_execution && memory_limit && !V8JSG(timer_thread)) {
  756. /* If timer thread is not started already and we now impose a memory limit
  757. * finally install the timer. */
  758. V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js));
  759. }
  760. }
  761. /* }}} */
  762. /* {{{ proto void V8Js::setAverageObjectSize(average_object_size)
  763. */
  764. static PHP_METHOD(V8Js, setAverageObjectSize)
  765. {
  766. v8js_ctx *c;
  767. long average_object_size = 0;
  768. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &average_object_size) == FAILURE) {
  769. return;
  770. }
  771. c = Z_V8JS_CTX_OBJ_P(getThis());
  772. c->average_object_size = average_object_size;
  773. }
  774. /* }}} */
  775. static void v8js_persistent_zval_ctor(zval *p) /* {{{ */
  776. {
  777. assert(Z_TYPE_P(p) == IS_STRING);
  778. Z_STR_P(p) = zend_string_dup(Z_STR_P(p), 1);
  779. }
  780. /* }}} */
  781. static void v8js_persistent_zval_dtor(zval *p) /* {{{ */
  782. {
  783. assert(Z_TYPE_P(p) == IS_STRING);
  784. free(Z_STR_P(p));
  785. }
  786. /* }}} */
  787. static void v8js_script_free(v8js_script *res)
  788. {
  789. efree(res->name);
  790. delete res->script; // does Reset()
  791. }
  792. static void v8js_script_dtor(zend_resource *rsrc) /* {{{ */
  793. {
  794. v8js_script *res = (v8js_script *)rsrc->ptr;
  795. if (res) {
  796. if(res->ctx) {
  797. std::vector<v8js_script *>::iterator it = std::find(res->ctx->script_objects.begin(), res->ctx->script_objects.end(), res);
  798. res->ctx->script_objects.erase(it);
  799. }
  800. v8js_script_free(res);
  801. efree(res);
  802. }
  803. }
  804. /* }}} */
  805. static int v8js_register_extension(zend_string *name, zend_string *source, zval *deps_arr, zend_bool auto_enable TSRMLS_DC) /* {{{ */
  806. {
  807. v8js_jsext *jsext = NULL;
  808. #ifdef ZTS
  809. v8js_process_globals.lock.lock();
  810. #endif
  811. if (!v8js_process_globals.extensions) {
  812. v8js_process_globals.extensions = (HashTable *) malloc(sizeof(HashTable));
  813. zend_hash_init(v8js_process_globals.extensions, 1, NULL, v8js_jsext_dtor, 1);
  814. } else if (zend_hash_exists(v8js_process_globals.extensions, name)) {
  815. #ifdef ZTS
  816. v8js_process_globals.lock.unlock();
  817. #endif
  818. return FAILURE;
  819. }
  820. jsext = (v8js_jsext *) calloc(1, sizeof(v8js_jsext));
  821. if (deps_arr) {
  822. jsext->deps_count = zend_hash_num_elements(Z_ARRVAL_P(deps_arr));
  823. if (v8js_create_ext_strarr(&jsext->deps, jsext->deps_count, Z_ARRVAL_P(deps_arr)) == FAILURE) {
  824. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid dependency array passed");
  825. v8js_jsext_free_storage(jsext);
  826. #ifdef ZTS
  827. v8js_process_globals.lock.unlock();
  828. #endif
  829. return FAILURE;
  830. }
  831. }
  832. jsext->auto_enable = auto_enable;
  833. // Allocate a persistent string which will survive until module shutdown on both ZTS(Persistent) and NTS(Not interned, those would be cleaned up)
  834. // (zend_string_dup would return the original interned string, if interned, so we don't use that)
  835. jsext->name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 1);
  836. jsext->source = zend_string_init(ZSTR_VAL(source), ZSTR_LEN(source), 1);
  837. if (jsext->deps) {
  838. jsext->deps_ht = (HashTable *) malloc(sizeof(HashTable));
  839. zend_hash_init(jsext->deps_ht, jsext->deps_count, NULL, v8js_persistent_zval_dtor, 1);
  840. zend_hash_copy(jsext->deps_ht, Z_ARRVAL_P(deps_arr), v8js_persistent_zval_ctor);
  841. }
  842. jsext->extension = new v8::Extension(ZSTR_VAL(jsext->name), ZSTR_VAL(jsext->source), jsext->deps_count, jsext->deps);
  843. if (!zend_hash_add_ptr(v8js_process_globals.extensions, jsext->name, jsext)) {
  844. v8js_jsext_free_storage(jsext);
  845. #ifdef ZTS
  846. v8js_process_globals.lock.unlock();
  847. #endif
  848. return FAILURE;
  849. }
  850. #ifdef ZTS
  851. v8js_process_globals.lock.unlock();
  852. #endif
  853. jsext->extension->set_auto_enable(auto_enable ? true : false);
  854. v8::RegisterExtension(jsext->extension);
  855. return SUCCESS;
  856. }
  857. /* }}} */
  858. /* ## Static methods ## */
  859. /* {{{ proto bool V8Js::registerExtension(string ext_name, string script [, array deps [, bool auto_enable]])
  860. */
  861. static PHP_METHOD(V8Js, registerExtension)
  862. {
  863. zend_string *ext_name, *script;
  864. zval *deps_arr = NULL;
  865. zend_bool auto_enable = 0;
  866. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS|ab", &ext_name, &script, &deps_arr, &auto_enable) == FAILURE) {
  867. return;
  868. }
  869. if (!ZSTR_LEN(ext_name)) {
  870. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Extension name cannot be empty");
  871. } else if (!ZSTR_LEN(script)) {
  872. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Script cannot be empty");
  873. } else if (v8js_register_extension(ext_name, script, deps_arr, auto_enable TSRMLS_CC) == SUCCESS) {
  874. RETURN_TRUE;
  875. }
  876. RETURN_FALSE;
  877. }
  878. /* }}} */
  879. /* {{{ proto array V8Js::getExtensions()
  880. */
  881. static PHP_METHOD(V8Js, getExtensions)
  882. {
  883. v8js_jsext *jsext;
  884. zend_ulong index;
  885. zend_string *key;
  886. zval *val, ext;
  887. if (zend_parse_parameters_none() == FAILURE) {
  888. return;
  889. }
  890. array_init(return_value);
  891. #ifdef ZTS
  892. v8js_process_globals.lock.lock();
  893. #endif
  894. if (v8js_process_globals.extensions) {
  895. ZEND_HASH_FOREACH_KEY_VAL(v8js_process_globals.extensions, index, key, val) {
  896. if (key) {
  897. jsext = (v8js_jsext *) Z_PTR_P(val);
  898. array_init(&ext);
  899. add_assoc_bool_ex(&ext, ZEND_STRL("auto_enable"), jsext->auto_enable);
  900. if (jsext->deps_ht) {
  901. zval deps_arr;
  902. array_init(&deps_arr);
  903. zend_hash_copy(Z_ARRVAL_P(&deps_arr), jsext->deps_ht, (copy_ctor_func_t) zval_add_ref);
  904. add_assoc_zval_ex(&ext, ZEND_STRL("deps"), &deps_arr);
  905. }
  906. add_assoc_zval_ex(return_value, ZSTR_VAL(key), ZSTR_LEN(key), &ext);
  907. }
  908. } ZEND_HASH_FOREACH_END();
  909. }
  910. #ifdef ZTS
  911. v8js_process_globals.lock.unlock();
  912. #endif
  913. }
  914. /* }}} */
  915. /* {{{ proto string|bool V8Js::createSnapshot(string embed_source)
  916. */
  917. static PHP_METHOD(V8Js, createSnapshot)
  918. {
  919. zend_string *script;
  920. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &script) == FAILURE) {
  921. return;
  922. }
  923. if (!ZSTR_LEN(script)) {
  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(ZSTR_VAL(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);
  935. delete[] snapshot_blob.data;
  936. }
  937. /* }}} */
  938. /* {{{ arginfo */
  939. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0)
  940. ZEND_ARG_INFO(0, object_name)
  941. ZEND_ARG_INFO(0, variables)
  942. ZEND_ARG_INFO(0, extensions)
  943. ZEND_ARG_INFO(0, report_uncaught_exceptions)
  944. ZEND_ARG_INFO(0, snapshot_blob)
  945. ZEND_END_ARG_INFO()
  946. ZEND_BEGIN_ARG_INFO(arginfo_v8js_sleep, 0)
  947. ZEND_END_ARG_INFO()
  948. ZEND_BEGIN_ARG_INFO(arginfo_v8js_wakeup, 0)
  949. ZEND_END_ARG_INFO()
  950. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1)
  951. ZEND_ARG_INFO(0, script)
  952. ZEND_ARG_INFO(0, identifier)
  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_compilestring, 0, 0, 1)
  958. ZEND_ARG_INFO(0, script)
  959. ZEND_ARG_INFO(0, identifier)
  960. ZEND_END_ARG_INFO()
  961. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executescript, 0, 0, 1)
  962. ZEND_ARG_INFO(0, script)
  963. ZEND_ARG_INFO(0, flags)
  964. ZEND_ARG_INFO(0, time_limit)
  965. ZEND_ARG_INFO(0, memory_limit)
  966. ZEND_END_ARG_INFO()
  967. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_checkstring, 0, 0, 1)
  968. ZEND_ARG_INFO(0, script)
  969. ZEND_END_ARG_INFO()
  970. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getpendingexception, 0)
  971. ZEND_END_ARG_INFO()
  972. ZEND_BEGIN_ARG_INFO(arginfo_v8js_clearpendingexception, 0)
  973. ZEND_END_ARG_INFO()
  974. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmodulenormaliser, 0, 0, 2)
  975. ZEND_ARG_INFO(0, base)
  976. ZEND_ARG_INFO(0, module_id)
  977. ZEND_END_ARG_INFO()
  978. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
  979. ZEND_ARG_INFO(0, callable)
  980. ZEND_END_ARG_INFO()
  981. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setaverageobjectsize, 0, 0, 1)
  982. ZEND_ARG_INFO(0, average_object_size)
  983. ZEND_END_ARG_INFO()
  984. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_registerextension, 0, 0, 2)
  985. ZEND_ARG_INFO(0, extension_name)
  986. ZEND_ARG_INFO(0, script)
  987. ZEND_ARG_INFO(0, dependencies)
  988. ZEND_ARG_INFO(0, auto_enable)
  989. ZEND_END_ARG_INFO()
  990. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0)
  991. ZEND_END_ARG_INFO()
  992. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_createsnapshot, 0, 0, 1)
  993. ZEND_ARG_INFO(0, script)
  994. ZEND_END_ARG_INFO()
  995. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_settimelimit, 0, 0, 1)
  996. ZEND_ARG_INFO(0, time_limit)
  997. ZEND_END_ARG_INFO()
  998. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmemorylimit, 0, 0, 1)
  999. ZEND_ARG_INFO(0, memory_limit)
  1000. ZEND_END_ARG_INFO()
  1001. const zend_function_entry v8js_methods[] = { /* {{{ */
  1002. PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  1003. PHP_ME(V8Js, __sleep, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1004. PHP_ME(V8Js, __wakeup, arginfo_v8js_wakeup, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1005. PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC)
  1006. PHP_ME(V8Js, compileString, arginfo_v8js_compilestring, ZEND_ACC_PUBLIC)
  1007. PHP_ME(V8Js, executeScript, arginfo_v8js_executescript, ZEND_ACC_PUBLIC)
  1008. PHP_ME(V8Js, checkString, arginfo_v8js_checkstring, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
  1009. PHP_ME(V8Js, getPendingException, arginfo_v8js_getpendingexception, ZEND_ACC_PUBLIC)
  1010. PHP_ME(V8Js, clearPendingException, arginfo_v8js_clearpendingexception, ZEND_ACC_PUBLIC)
  1011. PHP_ME(V8Js, setModuleNormaliser, arginfo_v8js_setmodulenormaliser, ZEND_ACC_PUBLIC)
  1012. PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
  1013. PHP_ME(V8Js, setTimeLimit, arginfo_v8js_settimelimit, ZEND_ACC_PUBLIC)
  1014. PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC)
  1015. PHP_ME(V8Js, setAverageObjectSize, arginfo_v8js_setaverageobjectsize, ZEND_ACC_PUBLIC)
  1016. PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  1017. PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  1018. PHP_ME(V8Js, createSnapshot, arginfo_v8js_createsnapshot, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  1019. {NULL, NULL, NULL}
  1020. };
  1021. /* }}} */
  1022. /* V8Js object handlers */
  1023. static void v8js_write_property(zval *object, zval *member, zval *value, void **cache_slot TSRMLS_DC) /* {{{ */
  1024. {
  1025. V8JS_BEGIN_CTX(c, object)
  1026. /* Check whether member is public, if so, export to V8. */
  1027. zend_property_info *property_info = zend_get_property_info(c->std.ce, Z_STR_P(member), 1 TSRMLS_CC);
  1028. if(!property_info ||
  1029. (property_info != ZEND_WRONG_PROPERTY_INFO &&
  1030. (property_info->flags & ZEND_ACC_PUBLIC))) {
  1031. /* Global PHP JS object */
  1032. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  1033. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject();
  1034. if (Z_STRLEN_P(member) > std::numeric_limits<int>::max()) {
  1035. zend_throw_exception(php_ce_v8js_exception,
  1036. "Property name exceeds maximum supported length", 0);
  1037. return;
  1038. }
  1039. /* Write value to PHP JS object */
  1040. v8::Local<v8::Value> key = V8JS_SYML(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member)));
  1041. jsobj->ForceSet(key, zval_to_v8js(value, isolate TSRMLS_CC), v8::ReadOnly);
  1042. }
  1043. /* Write value to PHP object */
  1044. std_object_handlers.write_property(object, member, value, NULL);
  1045. }
  1046. /* }}} */
  1047. static void v8js_unset_property(zval *object, zval *member, void **cache_slot TSRMLS_DC) /* {{{ */
  1048. {
  1049. V8JS_BEGIN_CTX(c, object)
  1050. /* Global PHP JS object */
  1051. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  1052. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject();
  1053. if (Z_STRLEN_P(member) > std::numeric_limits<int>::max()) {
  1054. zend_throw_exception(php_ce_v8js_exception,
  1055. "Property name exceeds maximum supported length", 0);
  1056. return;
  1057. }
  1058. /* Delete value from PHP JS object */
  1059. v8::Local<v8::Value> key = V8JS_SYML(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member)));
  1060. jsobj->Delete(key);
  1061. /* Unset from PHP object */
  1062. std_object_handlers.unset_property(object, member, NULL);
  1063. }
  1064. /* }}} */
  1065. PHP_MINIT_FUNCTION(v8js_class) /* {{{ */
  1066. {
  1067. zend_class_entry ce;
  1068. /* V8Js Class */
  1069. INIT_CLASS_ENTRY(ce, "V8Js", v8js_methods);
  1070. php_ce_v8js = zend_register_internal_class(&ce TSRMLS_CC);
  1071. php_ce_v8js->create_object = v8js_new;
  1072. /* V8Js handlers */
  1073. memcpy(&v8js_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1074. v8js_object_handlers.clone_obj = NULL;
  1075. v8js_object_handlers.write_property = v8js_write_property;
  1076. v8js_object_handlers.unset_property = v8js_unset_property;
  1077. /* V8Js Class Constants */
  1078. zend_declare_class_constant_string(php_ce_v8js, ZEND_STRL("V8_VERSION"), PHP_V8_VERSION TSRMLS_CC);
  1079. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE TSRMLS_CC);
  1080. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY TSRMLS_CC);
  1081. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_PROPAGATE_PHP_EXCEPTIONS"), V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS TSRMLS_CC);
  1082. le_v8js_script = zend_register_list_destructors_ex(v8js_script_dtor, NULL, PHP_V8JS_SCRIPT_RES_NAME, module_number);
  1083. return SUCCESS;
  1084. } /* }}} */
  1085. /*
  1086. * Local variables:
  1087. * tab-width: 4
  1088. * c-basic-offset: 4
  1089. * indent-tabs-mode: t
  1090. * End:
  1091. * vim600: noet sw=4 ts=4 fdm=marker
  1092. * vim<600: noet sw=4 ts=4
  1093. */