v8js_class.cc 33 KB

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