v8js_class.cc 38 KB

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