v8js_class.cc 34 KB

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