v8js_class.cc 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2017 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. #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. #ifdef USE_INTERNAL_ALLOCATOR
  51. class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
  52. public:
  53. virtual void* Allocate(size_t length) {
  54. void* data = AllocateUninitialized(length);
  55. return data == NULL ? data : memset(data, 0, length);
  56. }
  57. virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
  58. virtual void Free(void* data, size_t) { free(data); }
  59. };
  60. #endif /** USE_INTERNAL_ALLOCATOR */
  61. static void v8js_free_storage(zend_object *object) /* {{{ */
  62. {
  63. v8js_ctx *c = v8js_ctx_fetch_object(object);
  64. zend_object_std_dtor(&c->std);
  65. zval_ptr_dtor(&c->pending_exception);
  66. zval_ptr_dtor(&c->module_normaliser);
  67. zval_ptr_dtor(&c->module_loader);
  68. /* Delete PHP global object from JavaScript */
  69. if (!c->context.IsEmpty()) {
  70. v8::Locker locker(c->isolate);
  71. v8::Isolate::Scope isolate_scope(c->isolate);
  72. v8::HandleScope handle_scope(c->isolate);
  73. v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(c->isolate, c->context);
  74. v8::Context::Scope context_scope(v8_context);
  75. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
  76. V8JS_GLOBAL(c->isolate)->Delete(v8_context, object_name_js);
  77. }
  78. c->object_name.Reset();
  79. c->object_name.~Persistent();
  80. c->global_template.Reset();
  81. c->global_template.~Persistent();
  82. c->array_tmpl.Reset();
  83. c->array_tmpl.~Persistent();
  84. /* Clear persistent call_impl & method_tmpls templates */
  85. for (std::map<v8js_function_tmpl_t *, v8js_function_tmpl_t>::iterator it = c->call_impls.begin();
  86. it != c->call_impls.end(); ++it) {
  87. // No need to free it->first, as it is stored in c->template_cache and freed below
  88. it->second.Reset();
  89. }
  90. c->call_impls.~map();
  91. for (std::map<std::pair<zend_class_entry *, zend_function *>, v8js_function_tmpl_t>::iterator it = c->method_tmpls.begin();
  92. it != c->method_tmpls.end(); ++it) {
  93. it->second.Reset();
  94. }
  95. c->method_tmpls.~map();
  96. /* Clear persistent handles in template cache */
  97. for (std::map<const zend_string *,v8js_function_tmpl_t>::iterator it = c->template_cache.begin();
  98. it != c->template_cache.end(); ++it) {
  99. it->second.Reset();
  100. }
  101. c->template_cache.~map();
  102. /* Clear contexts */
  103. for (std::vector<v8js_accessor_ctx*>::iterator it = c->accessor_list.begin();
  104. it != c->accessor_list.end(); ++it) {
  105. v8js_accessor_ctx_dtor(*it);
  106. }
  107. c->accessor_list.~vector();
  108. /* Clear global object, dispose context */
  109. if (!c->context.IsEmpty()) {
  110. c->context.Reset();
  111. }
  112. c->context.~Persistent();
  113. /* Dispose yet undisposed weak refs */
  114. for (std::map<zend_object *, v8js_persistent_obj_t>::iterator it = c->weak_objects.begin();
  115. it != c->weak_objects.end(); ++it) {
  116. zend_object *object = it->first;
  117. zval value;
  118. ZVAL_OBJ(&value, object);
  119. zval_ptr_dtor(&value);
  120. c->isolate->AdjustAmountOfExternalAllocatedMemory(-c->average_object_size);
  121. it->second.Reset();
  122. }
  123. c->weak_objects.~map();
  124. for (std::map<v8js_function_tmpl_t *, v8js_persistent_obj_t>::iterator it = c->weak_closures.begin();
  125. it != c->weak_closures.end(); ++it) {
  126. v8js_function_tmpl_t *persist_tpl_ = it->first;
  127. persist_tpl_->Reset();
  128. delete persist_tpl_;
  129. it->second.Reset();
  130. }
  131. c->weak_closures.~map();
  132. for (std::list<v8js_v8object *>::iterator it = c->v8js_v8objects.begin();
  133. it != c->v8js_v8objects.end(); it ++) {
  134. (*it)->v8obj.Reset();
  135. (*it)->ctx = NULL;
  136. }
  137. c->v8js_v8objects.~list();
  138. for (std::vector<v8js_script *>::iterator it = c->script_objects.begin();
  139. it != c->script_objects.end(); it ++) {
  140. (*it)->ctx = NULL;
  141. (*it)->script->Reset();
  142. }
  143. c->script_objects.~vector();
  144. /* Clear persistent handles in module cache */
  145. for (std::map<char *, v8js_persistent_value_t>::iterator it = c->modules_loaded.begin();
  146. it != c->modules_loaded.end(); ++it) {
  147. efree(it->first);
  148. it->second.Reset();
  149. }
  150. c->modules_loaded.~map();
  151. if(c->isolate) {
  152. /* c->isolate is initialized by V8Js::__construct, but __wakeup calls
  153. * are not fully constructed and hence this would cause a NPE. */
  154. c->isolate->Dispose();
  155. }
  156. if(c->tz != NULL) {
  157. free(c->tz);
  158. }
  159. c->modules_stack.~vector();
  160. zval_ptr_dtor(&c->zval_snapshot_blob);
  161. #ifndef USE_INTERNAL_ALLOCATOR
  162. delete c->create_params.array_buffer_allocator;
  163. #endif
  164. }
  165. /* }}} */
  166. static zend_object* v8js_new(zend_class_entry *ce) /* {{{ */
  167. {
  168. v8js_ctx *c;
  169. c = (v8js_ctx *) ecalloc(1, sizeof(*c) + zend_object_properties_size(ce));
  170. zend_object_std_init(&c->std, ce);
  171. object_properties_init(&c->std, ce);
  172. c->std.handlers = &v8js_object_handlers;
  173. new(&c->object_name) v8::Persistent<v8::String>();
  174. new(&c->context) v8::Persistent<v8::Context>();
  175. new(&c->global_template) v8::Persistent<v8::FunctionTemplate>();
  176. new(&c->array_tmpl) v8::Persistent<v8::FunctionTemplate>();
  177. new(&c->modules_stack) std::vector<char*>();
  178. new(&c->modules_loaded) std::map<char *, v8js_persistent_value_t, cmp_str>;
  179. new(&c->template_cache) std::map<const zend_string *,v8js_function_tmpl_t>();
  180. new(&c->accessor_list) std::vector<v8js_accessor_ctx *>();
  181. new(&c->weak_closures) std::map<v8js_function_tmpl_t *, v8js_persistent_obj_t>();
  182. new(&c->weak_objects) std::map<zend_object *, v8js_persistent_obj_t>();
  183. new(&c->call_impls) std::map<v8js_function_tmpl_t *, v8js_function_tmpl_t>();
  184. new(&c->method_tmpls) std::map<std::pair<zend_class_entry *, zend_function *>, v8js_function_tmpl_t>();
  185. new(&c->v8js_v8objects) std::list<v8js_v8object *>();
  186. new(&c->script_objects) std::vector<v8js_script *>();
  187. // @fixme following is const, run on startup
  188. v8js_object_handlers.offset = XtOffsetOf(struct v8js_ctx, std);
  189. v8js_object_handlers.free_obj = v8js_free_storage;
  190. c->average_object_size = 1024;
  191. return &c->std;
  192. }
  193. /* }}} */
  194. static void v8js_fatal_error_handler(const char *location, const char *message) /* {{{ */
  195. {
  196. if (location) {
  197. zend_error(E_WARNING, "Fatal V8 error in %s: %s", location, message);
  198. } else {
  199. zend_error(E_WARNING, "Fatal V8 error: %s", message);
  200. }
  201. }
  202. /* }}} */
  203. #define IS_MAGIC_FUNC(mname) \
  204. ((ZSTR_LEN(key) == sizeof(mname) - 1) && \
  205. !strncasecmp(ZSTR_VAL(key), mname, ZSTR_LEN(key)))
  206. /* {{{ proto void V8Js::__construct([string object_name [, array variables [, bool report_uncaught_exceptions [, string snapshot_blob]]]])
  207. __construct for V8Js */
  208. static PHP_METHOD(V8Js, __construct)
  209. {
  210. zend_string *object_name = NULL;
  211. zend_bool report_uncaught = 1;
  212. zval *vars_arr = NULL;
  213. zval *snapshot_blob = NULL;
  214. v8js_ctx *c = Z_V8JS_CTX_OBJ_P(getThis())
  215. if (!c->context.IsEmpty()) {
  216. /* called __construct() twice, bail out */
  217. return;
  218. }
  219. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!abz", &object_name, &vars_arr, &report_uncaught, &snapshot_blob) == FAILURE) {
  220. return;
  221. }
  222. /* Initialize V8 */
  223. v8js_v8_init();
  224. /* Throw PHP exception if uncaught exceptions exist */
  225. c->report_uncaught = report_uncaught;
  226. ZVAL_NULL(&c->pending_exception);
  227. c->in_execution = 0;
  228. if (report_uncaught != 1) {
  229. php_error_docref(NULL, E_DEPRECATED, "Disabling exception reporting is deprecated, $report_uncaught_exceptions != true");
  230. }
  231. new (&c->create_params) v8::Isolate::CreateParams();
  232. #ifdef USE_INTERNAL_ALLOCATOR
  233. static ArrayBufferAllocator array_buffer_allocator;
  234. c->create_params.array_buffer_allocator = &array_buffer_allocator;
  235. #else
  236. c->create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
  237. #endif
  238. new (&c->snapshot_blob) v8::StartupData();
  239. if (snapshot_blob) {
  240. if (Z_TYPE_P(snapshot_blob) == IS_STRING) {
  241. ZVAL_COPY(&c->zval_snapshot_blob, snapshot_blob);
  242. if (Z_STRLEN_P(snapshot_blob) > std::numeric_limits<int>::max()) {
  243. zend_throw_exception(php_ce_v8js_exception,
  244. "Snapshot size exceeds maximum supported length", 0);
  245. return;
  246. }
  247. c->snapshot_blob.data = Z_STRVAL_P(snapshot_blob);
  248. c->snapshot_blob.raw_size = static_cast<int>(Z_STRLEN_P(snapshot_blob));
  249. c->create_params.snapshot_blob = &c->snapshot_blob;
  250. } else {
  251. php_error_docref(NULL, E_WARNING, "Argument snapshot_blob expected to be of string type");
  252. }
  253. }
  254. c->isolate = v8::Isolate::New(c->create_params);
  255. c->isolate->SetData(0, c);
  256. c->time_limit = 0;
  257. c->time_limit_hit = false;
  258. c->memory_limit = 0;
  259. c->memory_limit_hit = false;
  260. ZVAL_NULL(&c->module_normaliser);
  261. ZVAL_NULL(&c->module_loader);
  262. // Isolate execution
  263. v8::Isolate *isolate = c->isolate;
  264. v8::Locker locker(isolate);
  265. v8::Isolate::Scope isolate_scope(isolate);
  266. /* Handle scope */
  267. v8::HandleScope handle_scope(isolate);
  268. /* Redirect fatal errors to PHP error handler */
  269. isolate->SetFatalErrorHandler(v8js_fatal_error_handler);
  270. /* Create global template for global object */
  271. // Now we are using multiple isolates this needs to be created for every context
  272. v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(c->isolate);
  273. c->global_template.Reset(isolate, global_template);
  274. /* Register builtin methods */
  275. v8js_register_methods(global_template, c);
  276. /* Create context */
  277. v8::Local<v8::Context> context = v8::Context::New(isolate, nullptr, global_template);
  278. if (context.IsEmpty()) {
  279. zend_throw_exception(php_ce_v8js_exception, "Failed to create V8 context.", 0);
  280. return;
  281. }
  282. context->SetAlignedPointerInEmbedderData(1, c);
  283. context->Global()->Set(context, V8JS_SYM("global"), context->Global());
  284. c->context.Reset(isolate, context);
  285. /* Enter context */
  286. v8::Context::Scope context_scope(context);
  287. /* Create the PHP container object's function template */
  288. v8::Local<v8::FunctionTemplate> php_obj_t = v8::FunctionTemplate::New(isolate, 0);
  289. /* Set class name for PHP object */
  290. zend_class_entry *ce = Z_OBJCE_P(getThis());
  291. if (ZSTR_LEN(ce->name) > std::numeric_limits<int>::max()) {
  292. zend_throw_exception(php_ce_v8js_exception,
  293. "PHP object class name exceeds maximum supported length", 0);
  294. return;
  295. }
  296. php_obj_t->SetClassName(V8JS_SYML(ZSTR_VAL(ce->name), static_cast<int>(ZSTR_LEN(ce->name))));
  297. /* Register Get accessor for passed variables */
  298. if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
  299. v8js_register_accessors(&c->accessor_list, php_obj_t, vars_arr, isolate);
  300. }
  301. /* Set name for the PHP JS object */
  302. v8::Local<v8::String> object_name_js;
  303. if (object_name && ZSTR_LEN(object_name)) {
  304. if (ZSTR_LEN(object_name) > std::numeric_limits<int>::max()) {
  305. zend_throw_exception(php_ce_v8js_exception,
  306. "PHP JS object class name exceeds maximum supported length", 0);
  307. return;
  308. }
  309. object_name_js = V8JS_ZSYM(object_name);
  310. }
  311. else {
  312. object_name_js = V8JS_SYM("PHP");
  313. }
  314. c->object_name.Reset(isolate, object_name_js);
  315. /* Add the PHP object into global object */
  316. php_obj_t->InstanceTemplate()->SetInternalFieldCount(2);
  317. v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance(context).ToLocalChecked();
  318. V8JS_GLOBAL(isolate)->DefineOwnProperty(context, object_name_js, php_obj, v8::ReadOnly);
  319. /* Export public property values */
  320. HashTable *properties = zend_std_get_properties(Z_OBJ_P(getThis()));
  321. zval *value;
  322. zend_string *member;
  323. ZEND_HASH_FOREACH_STR_KEY(properties, member) {
  324. zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1);
  325. if(property_info &&
  326. property_info != ZEND_WRONG_PROPERTY_INFO &&
  327. (property_info->flags & ZEND_ACC_PUBLIC)) {
  328. if (ZSTR_LEN(member) > std::numeric_limits<int>::max()) {
  329. zend_throw_exception(php_ce_v8js_exception,
  330. "Property name exceeds maximum supported length", 0);
  331. return;
  332. }
  333. v8::Local<v8::Name> key = V8JS_ZSYM(member);
  334. /* Write value to PHP JS object */
  335. value = OBJ_PROP(Z_OBJ_P(getThis()), property_info->offset);
  336. php_obj->DefineOwnProperty(context, key, zval_to_v8js(value, isolate), v8::ReadOnly);
  337. }
  338. } ZEND_HASH_FOREACH_END();
  339. /* Add pointer to zend object */
  340. php_obj->SetAlignedPointerInInternalField(1, Z_OBJ_P(getThis()));
  341. /* Export public methods */
  342. void *ptr;
  343. zend_string *key;
  344. ZEND_HASH_FOREACH_STR_KEY_PTR(&c->std.ce->function_table, key, ptr) {
  345. zend_function *method_ptr = reinterpret_cast<zend_function *>(ptr);
  346. if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
  347. /* Allow only public methods */
  348. continue;
  349. }
  350. if ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR)) != 0) {
  351. /* no __construct, __destruct(), or __clone() functions */
  352. continue;
  353. }
  354. /* hide (do not export) other PHP magic functions */
  355. if (IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME) ||
  356. IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) ||
  357. IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) ||
  358. IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) ||
  359. IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME) ||
  360. IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) ||
  361. IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME) ||
  362. IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME) ||
  363. IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME) ||
  364. IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME) ||
  365. IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) {
  366. continue;
  367. }
  368. const zend_function_entry *fe;
  369. for (fe = v8js_methods; fe->fname; fe ++) {
  370. if (strcmp(fe->fname, ZSTR_VAL(method_ptr->common.function_name)) == 0) {
  371. break;
  372. }
  373. }
  374. if(fe->fname) {
  375. /* Method belongs to \V8Js class itself, never export to V8, even if
  376. * it is overriden in a derived class. */
  377. continue;
  378. }
  379. if (ZSTR_LEN(method_ptr->common.function_name) > std::numeric_limits<int>::max()) {
  380. zend_throw_exception(php_ce_v8js_exception,
  381. "Method name exceeds maximum supported length", 0);
  382. return;
  383. }
  384. v8::Local<v8::String> method_name = V8JS_ZSYM(method_ptr->common.function_name);
  385. v8::Local<v8::FunctionTemplate> ft;
  386. ft = v8::FunctionTemplate::New(isolate, v8js_php_callback,
  387. v8::External::New((isolate), method_ptr));
  388. // @fixme add/check Signature v8::Signature::New((isolate), tmpl));
  389. v8js_function_tmpl_t *persistent_ft = &c->method_tmpls[std::make_pair(ce, method_ptr)];
  390. persistent_ft->Reset(isolate, ft);
  391. php_obj->CreateDataProperty(context, method_name, ft->GetFunction(context).ToLocalChecked());
  392. } ZEND_HASH_FOREACH_END();
  393. }
  394. /* }}} */
  395. /* {{{ proto V8JS::__sleep()
  396. */
  397. PHP_METHOD(V8Js, __sleep)
  398. {
  399. zend_throw_exception(php_ce_v8js_exception,
  400. "You cannot serialize or unserialize V8Js instances", 0);
  401. RETURN_FALSE;
  402. }
  403. /* }}} */
  404. /* {{{ proto V8JS::__wakeup()
  405. */
  406. PHP_METHOD(V8Js, __wakeup)
  407. {
  408. zend_throw_exception(php_ce_v8js_exception,
  409. "You cannot serialize or unserialize V8Js instances", 0);
  410. RETURN_FALSE;
  411. }
  412. /* }}} */
  413. static void v8js_compile_script(zval *this_ptr, const zend_string *str, const zend_string *identifier, v8js_script **ret)
  414. {
  415. v8js_script *res = NULL;
  416. V8JS_BEGIN_CTX(c, this_ptr)
  417. /* Catch JS exceptions */
  418. v8::TryCatch try_catch(isolate);
  419. /* Set script identifier */
  420. if (identifier && ZSTR_LEN(identifier) > std::numeric_limits<int>::max()) {
  421. zend_throw_exception(php_ce_v8js_exception,
  422. "Script identifier exceeds maximum supported length", 0);
  423. return;
  424. }
  425. v8::Local<v8::String> sname = identifier
  426. ? V8JS_ZSTR(identifier)
  427. : V8JS_SYM("V8Js::compileString()");
  428. v8::ScriptOrigin origin(sname);
  429. if (ZSTR_LEN(str) > std::numeric_limits<int>::max()) {
  430. zend_throw_exception(php_ce_v8js_exception,
  431. "Script source exceeds maximum supported length", 0);
  432. return;
  433. }
  434. v8::Local<v8::String> source = V8JS_ZSTR(str);
  435. v8::MaybeLocal<v8::Script> script = v8::Script::Compile(v8::Local<v8::Context>::New(isolate, c->context), source, &origin);
  436. /* Compile errors? */
  437. if (script.IsEmpty()) {
  438. v8js_throw_script_exception(c->isolate, &try_catch);
  439. return;
  440. }
  441. res = (v8js_script *)emalloc(sizeof(v8js_script));
  442. res->script = new v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>>(c->isolate, script.ToLocalChecked());
  443. v8::String::Utf8Value _sname(isolate, sname);
  444. res->name = estrndup(ToCString(_sname), _sname.length());
  445. res->ctx = c;
  446. *ret = res;
  447. return;
  448. }
  449. static void v8js_execute_script(zval *this_ptr, v8js_script *res, long flags, long time_limit, size_t memory_limit, zval **return_value)
  450. {
  451. v8js_ctx *c = Z_V8JS_CTX_OBJ_P(this_ptr);
  452. if (res->ctx != c) {
  453. zend_error(E_WARNING, "Script resource from wrong V8Js object passed");
  454. ZVAL_BOOL(*return_value, 0);
  455. return;
  456. }
  457. if (!c->in_execution && time_limit == 0) {
  458. time_limit = c->time_limit;
  459. }
  460. if (!c->in_execution && memory_limit == 0) {
  461. memory_limit = c->memory_limit;
  462. }
  463. /* std::function relies on its dtor to be executed, otherwise it leaks
  464. * some memory on bailout. */
  465. {
  466. std::function< v8::MaybeLocal<v8::Value>(v8::Isolate *) > v8_call = [c, res](v8::Isolate *isolate) {
  467. v8::Local<v8::Script> script = v8::Local<v8::Script>::New(isolate, *res->script);
  468. return script->Run(v8::Local<v8::Context>::New(isolate, c->context));
  469. };
  470. v8js_v8_call(c, return_value, flags, time_limit, memory_limit, v8_call);
  471. }
  472. if(V8JSG(fatal_error_abort)) {
  473. /* Check for fatal error marker possibly set by v8js_error_handler; just
  474. * rethrow the error since we're now out of V8. */
  475. zend_bailout();
  476. }
  477. }
  478. /* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
  479. */
  480. static PHP_METHOD(V8Js, executeString)
  481. {
  482. zend_string *str = NULL, *identifier = NULL;
  483. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  484. v8js_script *res = NULL;
  485. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!lll", &str, &identifier, &flags, &time_limit, &memory_limit) == FAILURE) {
  486. return;
  487. }
  488. if (memory_limit < 0) {
  489. zend_throw_exception(php_ce_v8js_exception,
  490. "memory_limit must not be negative", 0);
  491. return;
  492. }
  493. v8js_compile_script(getThis(), str, identifier, &res);
  494. if (!res) {
  495. RETURN_FALSE;
  496. }
  497. zend_try {
  498. v8js_execute_script(getThis(), res, flags, time_limit, static_cast<size_t>(memory_limit), &return_value);
  499. v8js_script_free(res);
  500. }
  501. zend_catch {
  502. v8js_script_free(res);
  503. zend_bailout();
  504. }
  505. zend_end_try()
  506. efree(res);
  507. }
  508. /* }}} */
  509. /* {{{ proto mixed V8Js::compileString(string script [, string identifier])
  510. */
  511. static PHP_METHOD(V8Js, compileString)
  512. {
  513. zend_string *str = NULL, *identifier = NULL;
  514. v8js_script *res = NULL;
  515. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S", &str, &identifier) == FAILURE) {
  516. return;
  517. }
  518. v8js_compile_script(getThis(), str, identifier, &res);
  519. if (res) {
  520. RETVAL_RES(zend_register_resource(res, le_v8js_script));
  521. v8js_ctx *ctx;
  522. ctx = Z_V8JS_CTX_OBJ_P(getThis());
  523. ctx->script_objects.push_back(res);
  524. }
  525. }
  526. /* }}} */
  527. /* {{{ proto mixed V8Js::executeScript(resource script [, int flags]])
  528. */
  529. static PHP_METHOD(V8Js, executeScript)
  530. {
  531. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  532. zval *zscript;
  533. v8js_script *res;
  534. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|lll", &zscript, &flags, &time_limit, &memory_limit) == FAILURE) {
  535. return;
  536. }
  537. if (memory_limit < 0) {
  538. zend_throw_exception(php_ce_v8js_exception,
  539. "memory_limit must not be negative", 0);
  540. return;
  541. }
  542. if((res = (v8js_script *)zend_fetch_resource(Z_RES_P(zscript), PHP_V8JS_SCRIPT_RES_NAME, le_v8js_script)) == NULL) {
  543. RETURN_FALSE;
  544. }
  545. v8js_execute_script(getThis(), res, flags, time_limit, static_cast<size_t>(memory_limit), &return_value);
  546. }
  547. /* }}} */
  548. /* {{{ proto mixed V8Js::checkString(string script)
  549. */
  550. static PHP_METHOD(V8Js, checkString)
  551. {
  552. zend_string *str = NULL;
  553. zend_string *identifier = zend_string_init("V8Js::checkString()", 19, 0);
  554. v8js_script *res = NULL;
  555. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &str) == FAILURE) {
  556. return;
  557. }
  558. v8js_compile_script(getThis(), str, identifier, &res);
  559. zend_string_release(identifier);
  560. if (!res) {
  561. RETURN_FALSE;
  562. }
  563. v8js_script_free(res);
  564. efree(res);
  565. RETURN_TRUE;
  566. }
  567. /* }}} */
  568. /* {{{ proto mixed V8Js::getPendingException()
  569. */
  570. static PHP_METHOD(V8Js, getPendingException)
  571. {
  572. v8js_ctx *c;
  573. if (zend_parse_parameters_none() == FAILURE) {
  574. return;
  575. }
  576. c = Z_V8JS_CTX_OBJ_P(getThis());
  577. if (Z_TYPE(c->pending_exception) == IS_OBJECT) {
  578. RETURN_ZVAL(&c->pending_exception, 1, 0);
  579. }
  580. }
  581. /* }}} */
  582. /* {{{ proto void V8Js::clearPendingException()
  583. */
  584. static PHP_METHOD(V8Js, clearPendingException)
  585. {
  586. v8js_ctx *c;
  587. if (zend_parse_parameters_none() == FAILURE) {
  588. return;
  589. }
  590. c = Z_V8JS_CTX_OBJ_P(getThis());
  591. if (Z_TYPE(c->pending_exception) == IS_OBJECT) {
  592. zval_ptr_dtor(&c->pending_exception);
  593. ZVAL_NULL(&c->pending_exception);
  594. }
  595. }
  596. /* }}} */
  597. /* {{{ proto void V8Js::setModuleNormaliser(string base, string module_id)
  598. */
  599. static PHP_METHOD(V8Js, setModuleNormaliser)
  600. {
  601. v8js_ctx *c;
  602. zval *callable;
  603. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callable) == FAILURE) {
  604. return;
  605. }
  606. c = Z_V8JS_CTX_OBJ_P(getThis());
  607. ZVAL_COPY(&c->module_normaliser, callable);
  608. }
  609. /* }}} */
  610. /* {{{ proto void V8Js::setModuleLoader(string module)
  611. */
  612. static PHP_METHOD(V8Js, setModuleLoader)
  613. {
  614. v8js_ctx *c;
  615. zval *callable;
  616. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &callable) == FAILURE) {
  617. return;
  618. }
  619. c = Z_V8JS_CTX_OBJ_P(getThis());
  620. ZVAL_COPY(&c->module_loader, callable);
  621. }
  622. /* }}} */
  623. /* {{{ proto void V8Js::setTimeLimit(int time_limit)
  624. */
  625. static PHP_METHOD(V8Js, setTimeLimit)
  626. {
  627. v8js_ctx *c;
  628. long time_limit = 0;
  629. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &time_limit) == FAILURE) {
  630. return;
  631. }
  632. c = Z_V8JS_CTX_OBJ_P(getThis());
  633. c->time_limit = time_limit;
  634. V8JSG(timer_mutex).lock();
  635. for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
  636. it != V8JSG(timer_stack).end(); it ++) {
  637. if((*it)->ctx == c && !(*it)->killed) {
  638. (*it)->time_limit = time_limit;
  639. // Calculate the time point when the time limit is exceeded
  640. std::chrono::milliseconds duration(time_limit);
  641. std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
  642. (*it)->time_point = from + duration;
  643. }
  644. }
  645. V8JSG(timer_mutex).unlock();
  646. if (c->in_execution && time_limit && !V8JSG(timer_thread)) {
  647. /* If timer thread is not started already and we now impose a time limit
  648. * finally install the timer. */
  649. V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js));
  650. }
  651. }
  652. /* }}} */
  653. /* {{{ proto void V8Js::setMemoryLimit(int memory_limit)
  654. */
  655. static PHP_METHOD(V8Js, setMemoryLimit)
  656. {
  657. v8js_ctx *c;
  658. long memory_limit = 0;
  659. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &memory_limit) == FAILURE) {
  660. return;
  661. }
  662. if (memory_limit < 0) {
  663. zend_throw_exception(php_ce_v8js_exception,
  664. "memory_limit must not be negative", 0);
  665. return;
  666. }
  667. c = Z_V8JS_CTX_OBJ_P(getThis());
  668. c->memory_limit = static_cast<size_t>(memory_limit);
  669. V8JSG(timer_mutex).lock();
  670. for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
  671. it != V8JSG(timer_stack).end(); it ++) {
  672. if((*it)->ctx == c && !(*it)->killed) {
  673. (*it)->memory_limit = static_cast<size_t>(memory_limit);
  674. }
  675. }
  676. V8JSG(timer_mutex).unlock();
  677. if (c->in_execution && memory_limit && !V8JSG(timer_thread)) {
  678. /* If timer thread is not started already and we now impose a memory limit
  679. * finally install the timer. */
  680. V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js));
  681. }
  682. }
  683. /* }}} */
  684. /* {{{ proto void V8Js::setAverageObjectSize(average_object_size)
  685. */
  686. static PHP_METHOD(V8Js, setAverageObjectSize)
  687. {
  688. v8js_ctx *c;
  689. long average_object_size = 0;
  690. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &average_object_size) == FAILURE) {
  691. return;
  692. }
  693. c = Z_V8JS_CTX_OBJ_P(getThis());
  694. c->average_object_size = average_object_size;
  695. }
  696. /* }}} */
  697. static void v8js_persistent_zval_ctor(zval *p) /* {{{ */
  698. {
  699. assert(Z_TYPE_P(p) == IS_STRING);
  700. Z_STR_P(p) = zend_string_dup(Z_STR_P(p), 1);
  701. }
  702. /* }}} */
  703. static void v8js_persistent_zval_dtor(zval *p) /* {{{ */
  704. {
  705. assert(Z_TYPE_P(p) == IS_STRING);
  706. if (!ZSTR_IS_INTERNED(Z_STR_P(p))) {
  707. free(Z_STR_P(p));
  708. }
  709. }
  710. /* }}} */
  711. static void v8js_script_free(v8js_script *res)
  712. {
  713. efree(res->name);
  714. delete res->script; // does Reset()
  715. }
  716. static void v8js_script_dtor(zend_resource *rsrc) /* {{{ */
  717. {
  718. v8js_script *res = (v8js_script *)rsrc->ptr;
  719. if (res) {
  720. if(res->ctx) {
  721. std::vector<v8js_script *>::iterator it = std::find(res->ctx->script_objects.begin(), res->ctx->script_objects.end(), res);
  722. res->ctx->script_objects.erase(it);
  723. }
  724. v8js_script_free(res);
  725. efree(res);
  726. }
  727. }
  728. /* }}} */
  729. /* ## Static methods ## */
  730. static v8::StartupData createSnapshotDataBlob(v8::SnapshotCreator *snapshot_creator, zend_string *str) /* {{{ */
  731. {
  732. v8::Isolate *isolate = snapshot_creator->GetIsolate();
  733. {
  734. v8::HandleScope scope(isolate);
  735. v8::Local<v8::Context> context = v8::Context::New(isolate);
  736. v8::Context::Scope context_scope(context);
  737. v8::TryCatch try_catch(isolate);
  738. v8::Local<v8::String> source = V8JS_ZSTR(str);
  739. v8::MaybeLocal<v8::Script> script = v8::Script::Compile(context, source);
  740. if (script.IsEmpty() || script.ToLocalChecked()->Run(context).IsEmpty())
  741. {
  742. return {nullptr, 0};
  743. }
  744. snapshot_creator->SetDefaultContext(context);
  745. }
  746. return snapshot_creator->CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
  747. } /* }}} */
  748. /* {{{ proto string|bool V8Js::createSnapshot(string embed_source)
  749. */
  750. static PHP_METHOD(V8Js, createSnapshot)
  751. {
  752. zend_string *script;
  753. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &script) == FAILURE) {
  754. return;
  755. }
  756. if (!ZSTR_LEN(script)) {
  757. php_error_docref(NULL, E_WARNING, "Script cannot be empty");
  758. RETURN_FALSE;
  759. }
  760. /* Initialize V8, if not already done. */
  761. v8js_v8_init();
  762. v8::Isolate *isolate = v8::Isolate::Allocate();
  763. v8::SnapshotCreator snapshot_creator(isolate);
  764. v8::StartupData snapshot_blob = createSnapshotDataBlob(&snapshot_creator, script);
  765. if (!snapshot_blob.data) {
  766. php_error_docref(NULL, E_WARNING, "Failed to create V8 heap snapshot. Check $embed_source for errors.");
  767. RETURN_FALSE;
  768. }
  769. RETVAL_STRINGL(snapshot_blob.data, snapshot_blob.raw_size);
  770. delete[] snapshot_blob.data;
  771. }
  772. /* }}} */
  773. /* {{{ arginfo */
  774. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0)
  775. ZEND_ARG_INFO(0, object_name)
  776. ZEND_ARG_INFO(0, variables)
  777. ZEND_ARG_INFO(0, report_uncaught_exceptions)
  778. ZEND_ARG_INFO(0, snapshot_blob)
  779. ZEND_END_ARG_INFO()
  780. ZEND_BEGIN_ARG_INFO(arginfo_v8js_sleep, 0)
  781. ZEND_END_ARG_INFO()
  782. ZEND_BEGIN_ARG_INFO(arginfo_v8js_wakeup, 0)
  783. ZEND_END_ARG_INFO()
  784. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1)
  785. ZEND_ARG_INFO(0, script)
  786. ZEND_ARG_INFO(0, identifier)
  787. ZEND_ARG_INFO(0, flags)
  788. ZEND_ARG_INFO(0, time_limit)
  789. ZEND_ARG_INFO(0, memory_limit)
  790. ZEND_END_ARG_INFO()
  791. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_compilestring, 0, 0, 1)
  792. ZEND_ARG_INFO(0, script)
  793. ZEND_ARG_INFO(0, identifier)
  794. ZEND_END_ARG_INFO()
  795. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executescript, 0, 0, 1)
  796. ZEND_ARG_INFO(0, script)
  797. ZEND_ARG_INFO(0, flags)
  798. ZEND_ARG_INFO(0, time_limit)
  799. ZEND_ARG_INFO(0, memory_limit)
  800. ZEND_END_ARG_INFO()
  801. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_checkstring, 0, 0, 1)
  802. ZEND_ARG_INFO(0, script)
  803. ZEND_END_ARG_INFO()
  804. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getpendingexception, 0)
  805. ZEND_END_ARG_INFO()
  806. ZEND_BEGIN_ARG_INFO(arginfo_v8js_clearpendingexception, 0)
  807. ZEND_END_ARG_INFO()
  808. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmodulenormaliser, 0, 0, 2)
  809. ZEND_ARG_INFO(0, base)
  810. ZEND_ARG_INFO(0, module_id)
  811. ZEND_END_ARG_INFO()
  812. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
  813. ZEND_ARG_INFO(0, callable)
  814. ZEND_END_ARG_INFO()
  815. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setaverageobjectsize, 0, 0, 1)
  816. ZEND_ARG_INFO(0, average_object_size)
  817. ZEND_END_ARG_INFO()
  818. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_createsnapshot, 0, 0, 1)
  819. ZEND_ARG_INFO(0, script)
  820. ZEND_END_ARG_INFO()
  821. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_settimelimit, 0, 0, 1)
  822. ZEND_ARG_INFO(0, time_limit)
  823. ZEND_END_ARG_INFO()
  824. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmemorylimit, 0, 0, 1)
  825. ZEND_ARG_INFO(0, memory_limit)
  826. ZEND_END_ARG_INFO()
  827. const zend_function_entry v8js_methods[] = { /* {{{ */
  828. PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  829. PHP_ME(V8Js, __sleep, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  830. PHP_ME(V8Js, __wakeup, arginfo_v8js_wakeup, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  831. PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC)
  832. PHP_ME(V8Js, compileString, arginfo_v8js_compilestring, ZEND_ACC_PUBLIC)
  833. PHP_ME(V8Js, executeScript, arginfo_v8js_executescript, ZEND_ACC_PUBLIC)
  834. PHP_ME(V8Js, checkString, arginfo_v8js_checkstring, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
  835. PHP_ME(V8Js, getPendingException, arginfo_v8js_getpendingexception, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
  836. PHP_ME(V8Js, clearPendingException, arginfo_v8js_clearpendingexception, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
  837. PHP_ME(V8Js, setModuleNormaliser, arginfo_v8js_setmodulenormaliser, ZEND_ACC_PUBLIC)
  838. PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
  839. PHP_ME(V8Js, setTimeLimit, arginfo_v8js_settimelimit, ZEND_ACC_PUBLIC)
  840. PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC)
  841. PHP_ME(V8Js, setAverageObjectSize, arginfo_v8js_setaverageobjectsize, ZEND_ACC_PUBLIC)
  842. PHP_ME(V8Js, createSnapshot, arginfo_v8js_createsnapshot, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  843. {NULL, NULL, NULL}
  844. };
  845. /* }}} */
  846. /* V8Js object handlers */
  847. static zval* v8js_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot) /* {{{ */
  848. {
  849. v8js_ctx *c = Z_V8JS_CTX_OBJ(object);
  850. V8JS_CTX_PROLOGUE_EX(c, value);
  851. /* Check whether member is public, if so, export to V8. */
  852. zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1);
  853. if(!property_info ||
  854. (property_info != ZEND_WRONG_PROPERTY_INFO &&
  855. (property_info->flags & ZEND_ACC_PUBLIC))) {
  856. /* Global PHP JS object */
  857. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  858. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(v8_context, object_name_js).ToLocalChecked()->ToObject(v8_context).ToLocalChecked();
  859. if (ZSTR_LEN(member) > std::numeric_limits<int>::max()) {
  860. zend_throw_exception(php_ce_v8js_exception,
  861. "Property name exceeds maximum supported length", 0);
  862. return value;
  863. }
  864. /* Write value to PHP JS object */
  865. v8::Local<v8::Name> key = V8JS_SYML(ZSTR_VAL(member), static_cast<int>(ZSTR_LEN(member)));
  866. jsobj->DefineOwnProperty(v8_context, key, zval_to_v8js(value, isolate), v8::ReadOnly);
  867. }
  868. /* Write value to PHP object */
  869. return std_object_handlers.write_property(object, member, value, NULL);
  870. }
  871. /* }}} */
  872. static void v8js_unset_property(zend_object *object, zend_string *member, void **cache_slot) /* {{{ */
  873. {
  874. V8JS_BEGIN_CTX_OBJ(c, object);
  875. /* Global PHP JS object */
  876. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  877. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(v8_context, object_name_js).ToLocalChecked()->ToObject(v8_context).ToLocalChecked();
  878. if (ZSTR_LEN(member) > std::numeric_limits<int>::max()) {
  879. zend_throw_exception(php_ce_v8js_exception,
  880. "Property name exceeds maximum supported length", 0);
  881. return;
  882. }
  883. /* Delete value from PHP JS object */
  884. v8::Local<v8::Value> key = V8JS_SYML(ZSTR_VAL(member), static_cast<int>(ZSTR_LEN(member)));
  885. jsobj->Delete(v8_context, key);
  886. /* Unset from PHP object */
  887. std_object_handlers.unset_property(object, member, NULL);
  888. }
  889. /* }}} */
  890. PHP_MINIT_FUNCTION(v8js_class) /* {{{ */
  891. {
  892. zend_class_entry ce;
  893. /* V8Js Class */
  894. INIT_CLASS_ENTRY(ce, "V8Js", v8js_methods);
  895. php_ce_v8js = zend_register_internal_class(&ce);
  896. php_ce_v8js->create_object = v8js_new;
  897. /* V8Js handlers */
  898. memcpy(&v8js_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  899. v8js_object_handlers.clone_obj = NULL;
  900. v8js_object_handlers.write_property = v8js_write_property;
  901. v8js_object_handlers.unset_property = v8js_unset_property;
  902. /* V8Js Class Constants */
  903. zend_declare_class_constant_string(php_ce_v8js, ZEND_STRL("V8_VERSION"), PHP_V8_VERSION);
  904. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE);
  905. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY);
  906. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_PROPAGATE_PHP_EXCEPTIONS"), V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS);
  907. le_v8js_script = zend_register_list_destructors_ex(v8js_script_dtor, NULL, PHP_V8JS_SCRIPT_RES_NAME, module_number);
  908. return SUCCESS;
  909. } /* }}} */
  910. /*
  911. * Local variables:
  912. * tab-width: 4
  913. * c-basic-offset: 4
  914. * indent-tabs-mode: t
  915. * End:
  916. * vim600: noet sw=4 ts=4 fdm=marker
  917. * vim<600: noet sw=4 ts=4
  918. */