v8js_class.cc 39 KB

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