v8js_class.cc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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. #include <functional>
  18. #include <algorithm>
  19. #include "php_v8js_macros.h"
  20. #include "v8js_v8.h"
  21. #include "v8js_exceptions.h"
  22. #include "v8js_v8object_class.h"
  23. #include "v8js_object_export.h"
  24. #include "v8js_timer.h"
  25. extern "C" {
  26. #include "php.h"
  27. #include "ext/date/php_date.h"
  28. #include "ext/standard/php_string.h"
  29. #include "zend_interfaces.h"
  30. #include "zend_closures.h"
  31. #include "ext/spl/spl_exceptions.h"
  32. #include "zend_exceptions.h"
  33. }
  34. #define PHP_V8JS_SCRIPT_RES_NAME "V8Js script"
  35. /* {{{ Class Entries */
  36. static zend_class_entry *php_ce_v8js;
  37. /* }}} */
  38. /* {{{ Object Handlers */
  39. static zend_object_handlers v8js_object_handlers;
  40. /* }}} */
  41. /* Forward declare v8js_methods, actually "static" but not possible in C++ */
  42. extern const zend_function_entry v8js_methods[];
  43. typedef struct _v8js_script {
  44. char *name;
  45. v8js_ctx *ctx;
  46. v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>> *script;
  47. } v8js_script;
  48. static void v8js_script_free(v8js_script *res);
  49. int le_v8js_script;
  50. /* {{{ Extension container */
  51. struct v8js_jsext {
  52. zend_bool auto_enable;
  53. HashTable *deps_ht;
  54. const char **deps;
  55. int deps_count;
  56. zend_string *name;
  57. zend_string *source;
  58. v8::Extension *extension;
  59. };
  60. /* }}} */
  61. #if PHP_V8_API_VERSION >= 4004010
  62. class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
  63. public:
  64. virtual void* Allocate(size_t length) {
  65. void* data = AllocateUninitialized(length);
  66. return data == NULL ? data : memset(data, 0, length);
  67. }
  68. virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
  69. virtual void Free(void* data, size_t) { free(data); }
  70. };
  71. #endif
  72. static void v8js_free_storage(zend_object *object TSRMLS_DC) /* {{{ */
  73. {
  74. v8js_ctx *c = v8js_ctx_fetch_object(object);
  75. zend_object_std_dtor(&c->std TSRMLS_CC);
  76. zval_dtor(&c->pending_exception);
  77. zval_dtor(&c->module_normaliser);
  78. zval_dtor(&c->module_loader);
  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 zend_string *,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<zend_object *, v8js_persistent_obj_t>::iterator it = c->weak_objects.begin();
  126. it != c->weak_objects.end(); ++it) {
  127. zend_object *object = it->first;
  128. zval value;
  129. ZVAL_OBJ(&value, object);
  130. zval_dtor(&value);
  131. c->isolate->AdjustAmountOfExternalAllocatedMemory(-1024);
  132. it->second.Reset();
  133. }
  134. c->weak_objects.~map();
  135. for (std::map<v8js_tmpl_t *, v8js_persistent_obj_t>::iterator it = c->weak_closures.begin();
  136. it != c->weak_closures.end(); ++it) {
  137. v8js_tmpl_t *persist_tpl_ = it->first;
  138. persist_tpl_->Reset();
  139. delete persist_tpl_;
  140. it->second.Reset();
  141. }
  142. c->weak_closures.~map();
  143. for (std::list<v8js_v8object *>::iterator it = c->v8js_v8objects.begin();
  144. it != c->v8js_v8objects.end(); it ++) {
  145. (*it)->v8obj.Reset();
  146. (*it)->ctx = NULL;
  147. }
  148. c->v8js_v8objects.~list();
  149. for (std::vector<v8js_script *>::iterator it = c->script_objects.begin();
  150. it != c->script_objects.end(); it ++) {
  151. (*it)->ctx = NULL;
  152. (*it)->script->Reset();
  153. }
  154. c->script_objects.~vector();
  155. /* Clear persistent handles in module cache */
  156. for (std::map<char *, v8js_persistent_obj_t>::iterator it = c->modules_loaded.begin();
  157. it != c->modules_loaded.end(); ++it) {
  158. efree(it->first);
  159. it->second.Reset();
  160. }
  161. c->modules_loaded.~map();
  162. if(c->isolate) {
  163. /* c->isolate is initialized by V8Js::__construct, but __wakeup calls
  164. * are not fully constructed and hence this would cause a NPE. */
  165. c->isolate->Dispose();
  166. }
  167. if(c->tz != NULL) {
  168. free(c->tz);
  169. }
  170. c->modules_stack.~vector();
  171. c->modules_base.~vector();
  172. #if PHP_V8_API_VERSION >= 4003007
  173. zval_dtor(&c->zval_snapshot_blob);
  174. #endif
  175. }
  176. /* }}} */
  177. static zend_object* v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  178. {
  179. v8js_ctx *c;
  180. c = (v8js_ctx *) ecalloc(1, sizeof(*c) + zend_object_properties_size(ce));
  181. zend_object_std_init(&c->std, ce TSRMLS_CC);
  182. object_properties_init(&c->std, ce);
  183. c->std.handlers = &v8js_object_handlers;
  184. TSRMLS_SET_CTX(c->zts_ctx);
  185. new(&c->object_name) v8::Persistent<v8::String>();
  186. new(&c->context) v8::Persistent<v8::Context>();
  187. new(&c->global_template) v8::Persistent<v8::FunctionTemplate>();
  188. new(&c->array_tmpl) v8::Persistent<v8::FunctionTemplate>();
  189. new(&c->modules_stack) std::vector<char*>();
  190. new(&c->modules_base) std::vector<char*>();
  191. new(&c->modules_loaded) std::map<char *, v8js_persistent_obj_t, cmp_str>;
  192. new(&c->template_cache) std::map<const zend_string *,v8js_tmpl_t>();
  193. new(&c->accessor_list) std::vector<v8js_accessor_ctx *>();
  194. new(&c->weak_closures) std::map<v8js_tmpl_t *, v8js_persistent_obj_t>();
  195. new(&c->weak_objects) std::map<zend_object *, v8js_persistent_obj_t>();
  196. new(&c->call_impls) std::map<v8js_tmpl_t *, v8js_tmpl_t>();
  197. new(&c->method_tmpls) std::map<zend_function *, v8js_tmpl_t>();
  198. new(&c->v8js_v8objects) std::list<v8js_v8object *>();
  199. new(&c->script_objects) std::vector<v8js_script *>();
  200. // @fixme following is const, run on startup
  201. v8js_object_handlers.offset = XtOffsetOf(struct v8js_ctx, std);
  202. v8js_object_handlers.free_obj = v8js_free_storage;
  203. return &c->std;
  204. }
  205. /* }}} */
  206. static void v8js_free_ext_strarr(const char **arr, int count) /* {{{ */
  207. {
  208. int i;
  209. if (arr) {
  210. for (i = 0; i < count; i++) {
  211. if (arr[i]) {
  212. free((void *) arr[i]);
  213. }
  214. }
  215. free(arr);
  216. }
  217. }
  218. /* }}} */
  219. static void v8js_jsext_free_storage(v8js_jsext *jsext) /* {{{ */
  220. {
  221. if (jsext->deps_ht) {
  222. zend_hash_destroy(jsext->deps_ht);
  223. free(jsext->deps_ht);
  224. }
  225. if (jsext->deps) {
  226. v8js_free_ext_strarr(jsext->deps, jsext->deps_count);
  227. }
  228. delete jsext->extension;
  229. zend_string_release(jsext->name);
  230. zend_string_release(jsext->source);
  231. free(jsext);
  232. }
  233. /* }}} */
  234. static void v8js_jsext_dtor(zval *zv) /* {{{ */
  235. {
  236. v8js_jsext_free_storage(reinterpret_cast<v8js_jsext *>(Z_PTR_P(zv)));
  237. }
  238. /* }}} */
  239. static int v8js_create_ext_strarr(const char ***retval, int count, HashTable *ht) /* {{{ */
  240. {
  241. const char **exts = NULL;
  242. HashPosition pos;
  243. zval *tmp;
  244. int i = 0;
  245. exts = (const char **) calloc(1, count * sizeof(char *));
  246. zend_hash_internal_pointer_reset_ex(ht, &pos);
  247. while ((tmp = zend_hash_get_current_data_ex(ht, &pos))) {
  248. if (Z_TYPE_P(tmp) == IS_STRING) {
  249. exts[i++] = zend_strndup(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
  250. } else {
  251. v8js_free_ext_strarr(exts, i);
  252. return FAILURE;
  253. }
  254. zend_hash_move_forward_ex(ht, &pos);
  255. }
  256. *retval = exts;
  257. return SUCCESS;
  258. }
  259. /* }}} */
  260. static void v8js_fatal_error_handler(const char *location, const char *message) /* {{{ */
  261. {
  262. if (location) {
  263. zend_error(E_WARNING, "Fatal V8 error in %s: %s", location, message);
  264. } else {
  265. zend_error(E_WARNING, "Fatal V8 error: %s", message);
  266. }
  267. }
  268. /* }}} */
  269. #define IS_MAGIC_FUNC(mname) \
  270. ((ZSTR_LEN(key) == sizeof(mname) - 1) && \
  271. !strncasecmp(ZSTR_VAL(key), mname, ZSTR_LEN(key)))
  272. /* {{{ proto void V8Js::__construct([string object_name [, array variables [, array extensions [, bool report_uncaught_exceptions [, string snapshot_blob]]]]])
  273. __construct for V8Js */
  274. static PHP_METHOD(V8Js, __construct)
  275. {
  276. zend_string *object_name = NULL;
  277. zend_bool report_uncaught = 1;
  278. zval *vars_arr = NULL, *exts_arr = NULL;
  279. const char **exts = NULL;
  280. int exts_count = 0;
  281. zval *snapshot_blob = NULL;
  282. v8js_ctx *c = Z_V8JS_CTX_OBJ_P(getThis())
  283. if (!c->context.IsEmpty()) {
  284. /* called __construct() twice, bail out */
  285. return;
  286. }
  287. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|Saabz", &object_name, &vars_arr, &exts_arr, &report_uncaught, &snapshot_blob) == FAILURE) {
  288. return;
  289. }
  290. /* Initialize V8 */
  291. v8js_v8_init(TSRMLS_C);
  292. /* Throw PHP exception if uncaught exceptions exist */
  293. c->report_uncaught = report_uncaught;
  294. ZVAL_NULL(&c->pending_exception);
  295. c->in_execution = 0;
  296. #if PHP_V8_API_VERSION >= 4003007
  297. new (&c->create_params) v8::Isolate::CreateParams();
  298. #if PHP_V8_API_VERSION >= 4004044
  299. static ArrayBufferAllocator array_buffer_allocator;
  300. c->create_params.array_buffer_allocator = &array_buffer_allocator;
  301. #endif
  302. new (&c->snapshot_blob) v8::StartupData();
  303. if (snapshot_blob) {
  304. if (Z_TYPE_P(snapshot_blob) == IS_STRING) {
  305. ZVAL_COPY(&c->zval_snapshot_blob, snapshot_blob);
  306. c->snapshot_blob.data = Z_STRVAL_P(snapshot_blob);
  307. c->snapshot_blob.raw_size = Z_STRLEN_P(snapshot_blob);
  308. c->create_params.snapshot_blob = &c->snapshot_blob;
  309. } else {
  310. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument snapshot_blob expected to be of string type");
  311. }
  312. }
  313. c->isolate = v8::Isolate::New(c->create_params);
  314. #else /* PHP_V8_API_VERSION < 4003007 */
  315. c->isolate = v8::Isolate::New();
  316. #endif
  317. c->isolate->SetData(0, c);
  318. c->time_limit = 0;
  319. c->time_limit_hit = false;
  320. c->memory_limit = 0;
  321. c->memory_limit_hit = false;
  322. ZVAL_NULL(&c->module_normaliser);
  323. ZVAL_NULL(&c->module_loader);
  324. /* Include extensions used by this context */
  325. /* Note: Extensions registered with auto_enable do not need to be added separately like this. */
  326. if (exts_arr)
  327. {
  328. exts_count = zend_hash_num_elements(Z_ARRVAL_P(exts_arr));
  329. if (v8js_create_ext_strarr(&exts, exts_count, Z_ARRVAL_P(exts_arr)) == FAILURE) {
  330. zend_throw_exception(php_ce_v8js_exception,
  331. "Invalid extensions array passed", 0);
  332. return;
  333. }
  334. }
  335. /* Declare configuration for extensions */
  336. v8::ExtensionConfiguration extension_conf(exts_count, exts);
  337. // Isolate execution
  338. v8::Isolate *isolate = c->isolate;
  339. v8::Locker locker(isolate);
  340. v8::Isolate::Scope isolate_scope(isolate);
  341. /* Handle scope */
  342. v8::HandleScope handle_scope(isolate);
  343. /* Redirect fatal errors to PHP error handler */
  344. // This needs to be done within the context isolate
  345. v8::V8::SetFatalErrorHandler(v8js_fatal_error_handler);
  346. /* Create global template for global object */
  347. // Now we are using multiple isolates this needs to be created for every context
  348. v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(c->isolate, 0);
  349. tpl->SetClassName(V8JS_SYM("V8Js"));
  350. c->global_template.Reset(isolate, tpl);
  351. /* Register builtin methods */
  352. v8js_register_methods(tpl->InstanceTemplate(), c);
  353. /* Create context */
  354. v8::Local<v8::Context> context = v8::Context::New(isolate, &extension_conf, tpl->InstanceTemplate());
  355. if (exts) {
  356. v8js_free_ext_strarr(exts, exts_count);
  357. }
  358. /* If extensions have errors, context will be empty. (NOTE: This is V8 stuff, they expect the passed sources to compile :) */
  359. if (context.IsEmpty()) {
  360. zend_throw_exception(php_ce_v8js_exception,
  361. "Failed to create V8 context. "
  362. "Check that registered extensions do not have errors.", 0);
  363. return;
  364. }
  365. context->SetAlignedPointerInEmbedderData(1, c);
  366. c->context.Reset(isolate, context);
  367. /* Enter context */
  368. v8::Context::Scope context_scope(context);
  369. /* Create the PHP container object's function template */
  370. v8::Local<v8::FunctionTemplate> php_obj_t = v8::FunctionTemplate::New(isolate, 0);
  371. /* Set class name for PHP object */
  372. zend_class_entry *ce = Z_OBJCE_P(getThis());
  373. php_obj_t->SetClassName(V8JS_SYML(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name)));
  374. /* Register Get accessor for passed variables */
  375. if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
  376. v8js_register_accessors(&c->accessor_list, php_obj_t, vars_arr, isolate TSRMLS_CC);
  377. }
  378. /* Set name for the PHP JS object */
  379. v8::Local<v8::String> object_name_js = (object_name && ZSTR_LEN(object_name))
  380. ? V8JS_ZSYM(object_name)
  381. : V8JS_SYM("PHP");
  382. c->object_name.Reset(isolate, object_name_js);
  383. /* Add the PHP object into global object */
  384. v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance();
  385. V8JS_GLOBAL(isolate)->ForceSet(object_name_js, php_obj, v8::ReadOnly);
  386. /* Export public property values */
  387. HashTable *properties = zend_std_get_properties(getThis() TSRMLS_CC);
  388. zval *value;
  389. zend_string *member;
  390. ZEND_HASH_FOREACH_STR_KEY(properties, member) {
  391. zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1 TSRMLS_CC);
  392. if(property_info &&
  393. property_info != ZEND_WRONG_PROPERTY_INFO &&
  394. property_info->flags & ZEND_ACC_PUBLIC) {
  395. /* Write value to PHP JS object */
  396. value = OBJ_PROP(Z_OBJ_P(getThis()), property_info->offset);
  397. php_obj->ForceSet(V8JS_ZSYM(member), zval_to_v8js(value, isolate TSRMLS_CC), v8::ReadOnly);
  398. }
  399. } ZEND_HASH_FOREACH_END();
  400. /* Add pointer to zend object */
  401. php_obj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), v8::External::New(isolate, Z_OBJ_P(getThis())));
  402. /* Export public methods */
  403. void *ptr;
  404. zend_string *key;
  405. uint key_len;
  406. ZEND_HASH_FOREACH_STR_KEY_PTR(&c->std.ce->function_table, key, ptr) {
  407. zend_function *method_ptr = reinterpret_cast<zend_function *>(ptr);
  408. if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
  409. /* Allow only public methods */
  410. continue;
  411. }
  412. if ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) {
  413. /* no __construct, __destruct(), or __clone() functions */
  414. continue;
  415. }
  416. /* hide (do not export) other PHP magic functions */
  417. if (IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME) ||
  418. IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) ||
  419. IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) ||
  420. IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) ||
  421. IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME) ||
  422. IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) ||
  423. IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME) ||
  424. IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME) ||
  425. IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME) ||
  426. IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME) ||
  427. IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) {
  428. continue;
  429. }
  430. const zend_function_entry *fe;
  431. for (fe = v8js_methods; fe->fname; fe ++) {
  432. if (strcmp(fe->fname, ZSTR_VAL(method_ptr->common.function_name)) == 0) {
  433. break;
  434. }
  435. }
  436. if(fe->fname) {
  437. /* Method belongs to \V8Js class itself, never export to V8, even if
  438. * it is overriden in a derived class. */
  439. continue;
  440. }
  441. v8::Local<v8::String> method_name = V8JS_ZSTR(method_ptr->common.function_name);
  442. v8::Local<v8::FunctionTemplate> ft;
  443. /*try {
  444. ft = v8::Local<v8::FunctionTemplate>::New
  445. (isolate, c->method_tmpls.at(method_ptr));
  446. }
  447. catch (const std::out_of_range &) */ {
  448. ft = v8::FunctionTemplate::New(isolate, v8js_php_callback,
  449. v8::External::New((isolate), method_ptr));
  450. // @fixme add/check Signature v8::Signature::New((isolate), tmpl));
  451. v8js_tmpl_t *persistent_ft = &c->method_tmpls[method_ptr];
  452. persistent_ft->Reset(isolate, ft);
  453. }
  454. php_obj->ForceSet(method_name, ft->GetFunction());
  455. } ZEND_HASH_FOREACH_END();
  456. }
  457. /* }}} */
  458. /* {{{ proto V8JS::__sleep()
  459. */
  460. PHP_METHOD(V8Js, __sleep)
  461. {
  462. zend_throw_exception(php_ce_v8js_exception,
  463. "You cannot serialize or unserialize V8Js instances", 0 TSRMLS_CC);
  464. RETURN_FALSE;
  465. }
  466. /* }}} */
  467. /* {{{ proto V8JS::__wakeup()
  468. */
  469. PHP_METHOD(V8Js, __wakeup)
  470. {
  471. zend_throw_exception(php_ce_v8js_exception,
  472. "You cannot serialize or unserialize V8Js instances", 0 TSRMLS_CC);
  473. RETURN_FALSE;
  474. }
  475. /* }}} */
  476. static void v8js_compile_script(zval *this_ptr, zend_string *str, zend_string *identifier, v8js_script **ret TSRMLS_DC)
  477. {
  478. v8js_script *res = NULL;
  479. V8JS_BEGIN_CTX(c, this_ptr)
  480. /* Catch JS exceptions */
  481. v8::TryCatch try_catch;
  482. /* Set script identifier */
  483. v8::Local<v8::String> sname = identifier ? V8JS_ZSTR(identifier) : V8JS_SYM("V8Js::compileString()");
  484. /* Compiles a string context independently. TODO: Add a php function which calls this and returns the result as resource which can be executed later. */
  485. v8::Local<v8::String> source = V8JS_ZSTR(str);
  486. v8::Local<v8::Script> script = v8::Script::Compile(source, sname);
  487. /* Compile errors? */
  488. if (script.IsEmpty()) {
  489. v8js_throw_script_exception(c->isolate, &try_catch TSRMLS_CC);
  490. return;
  491. }
  492. res = (v8js_script *)emalloc(sizeof(v8js_script));
  493. res->script = new v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>>(c->isolate, script);
  494. v8::String::Utf8Value _sname(sname);
  495. res->name = estrndup(ToCString(_sname), _sname.length());
  496. res->ctx = c;
  497. *ret = res;
  498. return;
  499. }
  500. static void v8js_execute_script(zval *this_ptr, v8js_script *res, long flags, long time_limit, long memory_limit, zval **return_value TSRMLS_DC)
  501. {
  502. v8js_ctx *c = Z_V8JS_CTX_OBJ_P(this_ptr);
  503. if (res->ctx != c) {
  504. zend_error(E_WARNING, "Script resource from wrong V8Js object passed");
  505. ZVAL_BOOL(*return_value, 0);
  506. return;
  507. }
  508. if (!c->in_execution && time_limit == 0) {
  509. time_limit = c->time_limit;
  510. }
  511. if (!c->in_execution && memory_limit == 0) {
  512. memory_limit = c->memory_limit;
  513. }
  514. /* std::function relies on its dtor to be executed, otherwise it leaks
  515. * some memory on bailout. */
  516. {
  517. std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [res](v8::Isolate *isolate) {
  518. v8::Local<v8::Script> script = v8::Local<v8::Script>::New(isolate, *res->script);
  519. return script->Run();
  520. };
  521. v8js_v8_call(c, return_value, flags, time_limit, memory_limit, v8_call TSRMLS_CC);
  522. }
  523. if(V8JSG(fatal_error_abort)) {
  524. /* Check for fatal error marker possibly set by v8js_error_handler; just
  525. * rethrow the error since we're now out of V8. */
  526. zend_bailout();
  527. }
  528. }
  529. /* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
  530. */
  531. static PHP_METHOD(V8Js, executeString)
  532. {
  533. zend_string *str = NULL, *identifier = NULL;
  534. int str_len = 0, identifier_len = 0;
  535. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  536. v8js_script *res = NULL;
  537. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|Slll", &str, &identifier, &flags, &time_limit, &memory_limit) == FAILURE) {
  538. return;
  539. }
  540. v8js_compile_script(getThis(), str, identifier, &res TSRMLS_CC);
  541. if (!res) {
  542. RETURN_FALSE;
  543. }
  544. zend_try {
  545. v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value TSRMLS_CC);
  546. v8js_script_free(res);
  547. }
  548. zend_catch {
  549. v8js_script_free(res);
  550. zend_bailout();
  551. }
  552. zend_end_try()
  553. efree(res);
  554. }
  555. /* }}} */
  556. /* {{{ proto mixed V8Js::compileString(string script [, string identifier])
  557. */
  558. static PHP_METHOD(V8Js, compileString)
  559. {
  560. zend_string *str = NULL, *identifier = NULL;
  561. int str_len = 0, identifier_len = 0;
  562. v8js_script *res = NULL;
  563. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|S", &str, &identifier) == FAILURE) {
  564. return;
  565. }
  566. v8js_compile_script(getThis(), str, identifier, &res TSRMLS_CC);
  567. if (res) {
  568. RETVAL_RES(zend_register_resource(res, le_v8js_script));
  569. v8js_ctx *ctx;
  570. ctx = Z_V8JS_CTX_OBJ_P(getThis());
  571. ctx->script_objects.push_back(res);
  572. }
  573. return;
  574. }
  575. /* }}} */
  576. /* {{{ proto mixed V8Js::executeScript(resource script [, int flags]])
  577. */
  578. static PHP_METHOD(V8Js, executeScript)
  579. {
  580. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  581. zval *zscript;
  582. v8js_script *res;
  583. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lll", &zscript, &flags, &time_limit, &memory_limit) == FAILURE) {
  584. return;
  585. }
  586. if((res = (v8js_script *)zend_fetch_resource(Z_RES_P(zscript), PHP_V8JS_SCRIPT_RES_NAME, le_v8js_script)) == NULL) {
  587. RETURN_FALSE;
  588. }
  589. v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value TSRMLS_CC);
  590. }
  591. /* }}} */
  592. /* {{{ proto mixed V8Js::checkString(string script)
  593. */
  594. static PHP_METHOD(V8Js, checkString)
  595. {
  596. zend_string *str = NULL;
  597. int str_len = 0;
  598. zend_string *identifier = zend_string_init("V8Js::checkString()", 19, 0);
  599. v8js_script *res = NULL;
  600. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &str) == FAILURE) {
  601. return;
  602. }
  603. v8js_compile_script(getThis(), str, identifier, &res TSRMLS_CC);
  604. zend_string_release(identifier);
  605. if (!res) {
  606. RETURN_FALSE;
  607. }
  608. v8js_script_free(res);
  609. efree(res);
  610. RETURN_TRUE;
  611. }
  612. /* }}} */
  613. /* {{{ proto mixed V8Js::getPendingException()
  614. */
  615. static PHP_METHOD(V8Js, getPendingException)
  616. {
  617. v8js_ctx *c;
  618. if (zend_parse_parameters_none() == FAILURE) {
  619. return;
  620. }
  621. c = Z_V8JS_CTX_OBJ_P(getThis());
  622. if (Z_TYPE(c->pending_exception) == IS_OBJECT) {
  623. RETURN_ZVAL(&c->pending_exception, 1, 0);
  624. }
  625. }
  626. /* }}} */
  627. /* {{{ proto void V8Js::clearPendingException()
  628. */
  629. static PHP_METHOD(V8Js, clearPendingException)
  630. {
  631. v8js_ctx *c;
  632. if (zend_parse_parameters_none() == FAILURE) {
  633. return;
  634. }
  635. c = Z_V8JS_CTX_OBJ_P(getThis());
  636. if (Z_TYPE(c->pending_exception) == IS_OBJECT) {
  637. zval_dtor(&c->pending_exception);
  638. ZVAL_NULL(&c->pending_exception);
  639. }
  640. }
  641. /* }}} */
  642. /* {{{ proto void V8Js::setModuleNormaliser(string base, string module_id)
  643. */
  644. static PHP_METHOD(V8Js, setModuleNormaliser)
  645. {
  646. v8js_ctx *c;
  647. zval *callable;
  648. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callable) == FAILURE) {
  649. return;
  650. }
  651. c = Z_V8JS_CTX_OBJ_P(getThis());
  652. ZVAL_COPY(&c->module_normaliser, callable);
  653. }
  654. /* }}} */
  655. /* {{{ proto void V8Js::setModuleLoader(string module)
  656. */
  657. static PHP_METHOD(V8Js, setModuleLoader)
  658. {
  659. v8js_ctx *c;
  660. zval *callable;
  661. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callable) == FAILURE) {
  662. return;
  663. }
  664. c = Z_V8JS_CTX_OBJ_P(getThis());
  665. ZVAL_COPY(&c->module_loader, callable);
  666. }
  667. /* }}} */
  668. /* {{{ proto void V8Js::setTimeLimit(int time_limit)
  669. */
  670. static PHP_METHOD(V8Js, setTimeLimit)
  671. {
  672. v8js_ctx *c;
  673. long time_limit = 0;
  674. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &time_limit) == FAILURE) {
  675. return;
  676. }
  677. c = Z_V8JS_CTX_OBJ_P(getThis());
  678. c->time_limit = time_limit;
  679. V8JSG(timer_mutex).lock();
  680. for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
  681. it != V8JSG(timer_stack).end(); it ++) {
  682. if((*it)->ctx == c && !(*it)->killed) {
  683. (*it)->time_limit = time_limit;
  684. // Calculate the time point when the time limit is exceeded
  685. std::chrono::milliseconds duration(time_limit);
  686. std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
  687. (*it)->time_point = from + duration;
  688. }
  689. }
  690. V8JSG(timer_mutex).unlock();
  691. if (c->in_execution && time_limit && !V8JSG(timer_thread)) {
  692. /* If timer thread is not started already and we now impose a time limit
  693. * finally install the timer. */
  694. V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js));
  695. }
  696. }
  697. /* }}} */
  698. /* {{{ proto void V8Js::setMemoryLimit(int memory_limit)
  699. */
  700. static PHP_METHOD(V8Js, setMemoryLimit)
  701. {
  702. v8js_ctx *c;
  703. long memory_limit = 0;
  704. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &memory_limit) == FAILURE) {
  705. return;
  706. }
  707. c = Z_V8JS_CTX_OBJ_P(getThis());
  708. c->memory_limit = memory_limit;
  709. V8JSG(timer_mutex).lock();
  710. for (std::deque< v8js_timer_ctx* >::iterator it = V8JSG(timer_stack).begin();
  711. it != V8JSG(timer_stack).end(); it ++) {
  712. if((*it)->ctx == c && !(*it)->killed) {
  713. (*it)->memory_limit = memory_limit;
  714. }
  715. }
  716. V8JSG(timer_mutex).unlock();
  717. if (c->in_execution && memory_limit && !V8JSG(timer_thread)) {
  718. /* If timer thread is not started already and we now impose a memory limit
  719. * finally install the timer. */
  720. V8JSG(timer_thread) = new std::thread(v8js_timer_thread, ZEND_MODULE_GLOBALS_BULK(v8js));
  721. }
  722. }
  723. /* }}} */
  724. static void v8js_persistent_zval_ctor(zval *p) /* {{{ */
  725. {
  726. assert(Z_TYPE_P(p) == IS_STRING);
  727. Z_STR_P(p) = zend_string_dup(Z_STR_P(p), 1);
  728. }
  729. /* }}} */
  730. static void v8js_persistent_zval_dtor(zval *p) /* {{{ */
  731. {
  732. assert(Z_TYPE_P(p) == IS_STRING);
  733. free(Z_STR_P(p));
  734. }
  735. /* }}} */
  736. static void v8js_script_free(v8js_script *res)
  737. {
  738. efree(res->name);
  739. delete res->script; // does Reset()
  740. }
  741. static void v8js_script_dtor(zend_resource *rsrc) /* {{{ */
  742. {
  743. v8js_script *res = (v8js_script *)rsrc->ptr;
  744. if (res) {
  745. if(res->ctx) {
  746. std::vector<v8js_script *>::iterator it = std::find(res->ctx->script_objects.begin(), res->ctx->script_objects.end(), res);
  747. res->ctx->script_objects.erase(it);
  748. }
  749. v8js_script_free(res);
  750. efree(res);
  751. }
  752. }
  753. /* }}} */
  754. static int v8js_register_extension(zend_string *name, zend_string *source, zval *deps_arr, zend_bool auto_enable TSRMLS_DC) /* {{{ */
  755. {
  756. v8js_jsext *jsext = NULL;
  757. #ifdef ZTS
  758. v8js_process_globals.lock.lock();
  759. #endif
  760. if (!v8js_process_globals.extensions) {
  761. v8js_process_globals.extensions = (HashTable *) malloc(sizeof(HashTable));
  762. zend_hash_init(v8js_process_globals.extensions, 1, NULL, v8js_jsext_dtor, 1);
  763. } else if (zend_hash_exists(v8js_process_globals.extensions, name)) {
  764. #ifdef ZTS
  765. v8js_process_globals.lock.unlock();
  766. #endif
  767. return FAILURE;
  768. }
  769. jsext = (v8js_jsext *) calloc(1, sizeof(v8js_jsext));
  770. if (deps_arr) {
  771. jsext->deps_count = zend_hash_num_elements(Z_ARRVAL_P(deps_arr));
  772. if (v8js_create_ext_strarr(&jsext->deps, jsext->deps_count, Z_ARRVAL_P(deps_arr)) == FAILURE) {
  773. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid dependency array passed");
  774. v8js_jsext_free_storage(jsext);
  775. #ifdef ZTS
  776. v8js_process_globals.lock.unlock();
  777. #endif
  778. return FAILURE;
  779. }
  780. }
  781. jsext->auto_enable = auto_enable;
  782. jsext->name = zend_string_dup(name, 1);
  783. jsext->source = zend_string_dup(source, 1);
  784. if (jsext->deps) {
  785. jsext->deps_ht = (HashTable *) malloc(sizeof(HashTable));
  786. zend_hash_init(jsext->deps_ht, jsext->deps_count, NULL, v8js_persistent_zval_dtor, 1);
  787. zend_hash_copy(jsext->deps_ht, Z_ARRVAL_P(deps_arr), v8js_persistent_zval_ctor);
  788. }
  789. jsext->extension = new v8::Extension(ZSTR_VAL(jsext->name), ZSTR_VAL(jsext->source), jsext->deps_count, jsext->deps);
  790. if (!zend_hash_add_ptr(v8js_process_globals.extensions, jsext->name, jsext)) {
  791. v8js_jsext_free_storage(jsext);
  792. #ifdef ZTS
  793. v8js_process_globals.lock.unlock();
  794. #endif
  795. return FAILURE;
  796. }
  797. #ifdef ZTS
  798. v8js_process_globals.lock.unlock();
  799. #endif
  800. jsext->extension->set_auto_enable(auto_enable ? true : false);
  801. v8::RegisterExtension(jsext->extension);
  802. return SUCCESS;
  803. }
  804. /* }}} */
  805. /* ## Static methods ## */
  806. /* {{{ proto bool V8Js::registerExtension(string ext_name, string script [, array deps [, bool auto_enable]])
  807. */
  808. static PHP_METHOD(V8Js, registerExtension)
  809. {
  810. zend_string *ext_name, *script;
  811. zval *deps_arr = NULL;
  812. zend_bool auto_enable = 0;
  813. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS|ab", &ext_name, &script, &deps_arr, &auto_enable) == FAILURE) {
  814. return;
  815. }
  816. if (!ZSTR_LEN(ext_name)) {
  817. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Extension name cannot be empty");
  818. } else if (!ZSTR_LEN(script)) {
  819. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Script cannot be empty");
  820. } else if (v8js_register_extension(ext_name, script, deps_arr, auto_enable TSRMLS_CC) == SUCCESS) {
  821. RETURN_TRUE;
  822. }
  823. RETURN_FALSE;
  824. }
  825. /* }}} */
  826. /* {{{ proto array V8Js::getExtensions()
  827. */
  828. static PHP_METHOD(V8Js, getExtensions)
  829. {
  830. v8js_jsext *jsext;
  831. ulong index;
  832. zend_string *key;
  833. zval *val, ext;
  834. if (zend_parse_parameters_none() == FAILURE) {
  835. return;
  836. }
  837. array_init(return_value);
  838. #ifdef ZTS
  839. v8js_process_globals.lock.lock();
  840. #endif
  841. if (v8js_process_globals.extensions) {
  842. ZEND_HASH_FOREACH_KEY_VAL(v8js_process_globals.extensions, index, key, val) {
  843. if (key) {
  844. jsext = (v8js_jsext *) Z_PTR_P(val);
  845. array_init(&ext);
  846. add_assoc_bool_ex(&ext, ZEND_STRL("auto_enable"), jsext->auto_enable);
  847. if (jsext->deps_ht) {
  848. zval deps_arr;
  849. array_init(&deps_arr);
  850. zend_hash_copy(Z_ARRVAL_P(&deps_arr), jsext->deps_ht, (copy_ctor_func_t) zval_add_ref);
  851. add_assoc_zval_ex(&ext, ZEND_STRL("deps"), &deps_arr);
  852. }
  853. add_assoc_zval_ex(return_value, ZSTR_VAL(key), ZSTR_LEN(key), &ext);
  854. }
  855. } ZEND_HASH_FOREACH_END();
  856. }
  857. #ifdef ZTS
  858. v8js_process_globals.lock.unlock();
  859. #endif
  860. }
  861. /* }}} */
  862. #if PHP_V8_API_VERSION >= 4003007
  863. /* {{{ proto string|bool V8Js::createSnapshot(string embed_source)
  864. */
  865. static PHP_METHOD(V8Js, createSnapshot)
  866. {
  867. zend_string *script;
  868. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &script) == FAILURE) {
  869. return;
  870. }
  871. if (!ZSTR_LEN(script)) {
  872. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Script cannot be empty");
  873. RETURN_FALSE;
  874. }
  875. /* Initialize V8, if not already done. */
  876. v8js_v8_init(TSRMLS_C);
  877. v8::StartupData snapshot_blob = v8::V8::CreateSnapshotDataBlob(ZSTR_VAL(script));
  878. if (!snapshot_blob.data) {
  879. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create V8 heap snapshot. Check $embed_source for errors.");
  880. RETURN_FALSE;
  881. }
  882. RETVAL_STRINGL(snapshot_blob.data, snapshot_blob.raw_size);
  883. delete[] snapshot_blob.data;
  884. }
  885. /* }}} */
  886. #endif /* PHP_V8_API_VERSION >= 4003007 */
  887. /* {{{ arginfo */
  888. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0)
  889. ZEND_ARG_INFO(0, object_name)
  890. ZEND_ARG_INFO(0, variables)
  891. ZEND_ARG_INFO(0, extensions)
  892. ZEND_ARG_INFO(0, report_uncaught_exceptions)
  893. ZEND_ARG_INFO(0, snapshot_blob)
  894. ZEND_END_ARG_INFO()
  895. ZEND_BEGIN_ARG_INFO(arginfo_v8js_sleep, 0)
  896. ZEND_END_ARG_INFO()
  897. ZEND_BEGIN_ARG_INFO(arginfo_v8js_wakeup, 0)
  898. ZEND_END_ARG_INFO()
  899. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1)
  900. ZEND_ARG_INFO(0, script)
  901. ZEND_ARG_INFO(0, identifier)
  902. ZEND_ARG_INFO(0, flags)
  903. ZEND_ARG_INFO(0, time_limit)
  904. ZEND_ARG_INFO(0, memory_limit)
  905. ZEND_END_ARG_INFO()
  906. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_compilestring, 0, 0, 1)
  907. ZEND_ARG_INFO(0, script)
  908. ZEND_ARG_INFO(0, identifier)
  909. ZEND_END_ARG_INFO()
  910. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executescript, 0, 0, 1)
  911. ZEND_ARG_INFO(0, script)
  912. ZEND_ARG_INFO(0, flags)
  913. ZEND_ARG_INFO(0, time_limit)
  914. ZEND_ARG_INFO(0, memory_limit)
  915. ZEND_END_ARG_INFO()
  916. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_checkstring, 0, 0, 1)
  917. ZEND_ARG_INFO(0, script)
  918. ZEND_END_ARG_INFO()
  919. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getpendingexception, 0)
  920. ZEND_END_ARG_INFO()
  921. ZEND_BEGIN_ARG_INFO(arginfo_v8js_clearpendingexception, 0)
  922. ZEND_END_ARG_INFO()
  923. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmodulenormaliser, 0, 0, 2)
  924. ZEND_ARG_INFO(0, base)
  925. ZEND_ARG_INFO(0, module_id)
  926. ZEND_END_ARG_INFO()
  927. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
  928. ZEND_ARG_INFO(0, callable)
  929. ZEND_END_ARG_INFO()
  930. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_registerextension, 0, 0, 2)
  931. ZEND_ARG_INFO(0, extension_name)
  932. ZEND_ARG_INFO(0, script)
  933. ZEND_ARG_INFO(0, dependencies)
  934. ZEND_ARG_INFO(0, auto_enable)
  935. ZEND_END_ARG_INFO()
  936. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0)
  937. ZEND_END_ARG_INFO()
  938. #if PHP_V8_API_VERSION >= 4003007
  939. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_createsnapshot, 0, 0, 1)
  940. ZEND_ARG_INFO(0, script)
  941. ZEND_END_ARG_INFO()
  942. #endif
  943. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_settimelimit, 0, 0, 1)
  944. ZEND_ARG_INFO(0, time_limit)
  945. ZEND_END_ARG_INFO()
  946. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmemorylimit, 0, 0, 1)
  947. ZEND_ARG_INFO(0, memory_limit)
  948. ZEND_END_ARG_INFO()
  949. const zend_function_entry v8js_methods[] = { /* {{{ */
  950. PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  951. PHP_ME(V8Js, __sleep, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  952. PHP_ME(V8Js, __wakeup, arginfo_v8js_sleep, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  953. PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC)
  954. PHP_ME(V8Js, compileString, arginfo_v8js_compilestring, ZEND_ACC_PUBLIC)
  955. PHP_ME(V8Js, executeScript, arginfo_v8js_executescript, ZEND_ACC_PUBLIC)
  956. PHP_ME(V8Js, checkString, arginfo_v8js_checkstring, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
  957. PHP_ME(V8Js, getPendingException, arginfo_v8js_getpendingexception, ZEND_ACC_PUBLIC)
  958. PHP_ME(V8Js, clearPendingException, arginfo_v8js_clearpendingexception, ZEND_ACC_PUBLIC)
  959. PHP_ME(V8Js, setModuleNormaliser, arginfo_v8js_setmodulenormaliser, ZEND_ACC_PUBLIC)
  960. PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
  961. PHP_ME(V8Js, setTimeLimit, arginfo_v8js_settimelimit, ZEND_ACC_PUBLIC)
  962. PHP_ME(V8Js, setMemoryLimit, arginfo_v8js_setmemorylimit, ZEND_ACC_PUBLIC)
  963. PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  964. PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  965. #if PHP_V8_API_VERSION >= 4003007
  966. PHP_ME(V8Js, createSnapshot, arginfo_v8js_createsnapshot, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  967. #endif
  968. {NULL, NULL, NULL}
  969. };
  970. /* }}} */
  971. /* V8Js object handlers */
  972. static void v8js_write_property(zval *object, zval *member, zval *value, void **cache_slot TSRMLS_DC) /* {{{ */
  973. {
  974. V8JS_BEGIN_CTX(c, object)
  975. /* Check whether member is public, if so, export to V8. */
  976. zend_property_info *property_info = zend_get_property_info(c->std.ce, Z_STR_P(member), 1 TSRMLS_CC);
  977. if(!property_info ||
  978. (property_info != ZEND_WRONG_PROPERTY_INFO &&
  979. property_info->flags & ZEND_ACC_PUBLIC)) {
  980. /* Global PHP JS object */
  981. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  982. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject();
  983. /* Write value to PHP JS object */
  984. jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, isolate TSRMLS_CC), v8::ReadOnly);
  985. }
  986. /* Write value to PHP object */
  987. std_object_handlers.write_property(object, member, value, NULL);
  988. }
  989. /* }}} */
  990. static void v8js_unset_property(zval *object, zval *member, void **cache_slot TSRMLS_DC) /* {{{ */
  991. {
  992. V8JS_BEGIN_CTX(c, object)
  993. /* Global PHP JS object */
  994. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  995. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject();
  996. /* Delete value from PHP JS object */
  997. jsobj->Delete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
  998. /* Unset from PHP object */
  999. std_object_handlers.unset_property(object, member, NULL);
  1000. }
  1001. /* }}} */
  1002. PHP_MINIT_FUNCTION(v8js_class) /* {{{ */
  1003. {
  1004. zend_class_entry ce;
  1005. /* V8Js Class */
  1006. INIT_CLASS_ENTRY(ce, "V8Js", v8js_methods);
  1007. php_ce_v8js = zend_register_internal_class(&ce TSRMLS_CC);
  1008. php_ce_v8js->create_object = v8js_new;
  1009. /* V8Js handlers */
  1010. memcpy(&v8js_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1011. v8js_object_handlers.clone_obj = NULL;
  1012. v8js_object_handlers.write_property = v8js_write_property;
  1013. v8js_object_handlers.unset_property = v8js_unset_property;
  1014. /* V8Js Class Constants */
  1015. zend_declare_class_constant_string(php_ce_v8js, ZEND_STRL("V8_VERSION"), PHP_V8_VERSION TSRMLS_CC);
  1016. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE TSRMLS_CC);
  1017. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY TSRMLS_CC);
  1018. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_PROPAGATE_PHP_EXCEPTIONS"), V8JS_FLAG_PROPAGATE_PHP_EXCEPTIONS TSRMLS_CC);
  1019. le_v8js_script = zend_register_list_destructors_ex(v8js_script_dtor, NULL, PHP_V8JS_SCRIPT_RES_NAME, module_number);
  1020. #if PHP_V8_API_VERSION >= 4004010 && PHP_V8_API_VERSION < 4004044
  1021. static ArrayBufferAllocator array_buffer_allocator;
  1022. v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
  1023. #endif
  1024. return SUCCESS;
  1025. } /* }}} */
  1026. /*
  1027. * Local variables:
  1028. * tab-width: 4
  1029. * c-basic-offset: 4
  1030. * End:
  1031. * vim600: noet sw=4 ts=4 fdm=marker
  1032. * vim<600: noet sw=4 ts=4
  1033. */