v8js_class.cc 33 KB

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