v8js_class.cc 38 KB

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