v8js_class.cc 33 KB

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