v8js.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2012 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | [email protected] so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Jani Taskinen <[email protected]> |
  16. | Author: Patrick Reilly <[email protected]> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #define V8JS_DEBUG 0
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. extern "C" {
  25. #include "php.h"
  26. #include "php_ini.h"
  27. #include "ext/standard/info.h"
  28. #include "ext/standard/php_string.h"
  29. #include "ext/standard/php_smart_str.h"
  30. #include "zend_exceptions.h"
  31. #include "php_v8js.h"
  32. }
  33. #include <v8.h>
  34. #include "php_v8js_macros.h"
  35. /* Forward declarations */
  36. static void php_v8js_throw_exception(v8::TryCatch * TSRMLS_DC);
  37. static void php_v8js_create_exception(zval *, v8::TryCatch * TSRMLS_DC);
  38. /* Module globals */
  39. ZEND_BEGIN_MODULE_GLOBALS(v8js)
  40. int v8_initialized;
  41. HashTable *extensions;
  42. v8::Persistent<v8::FunctionTemplate> global_template;
  43. int disposed_contexts; /* Disposed contexts since last time V8 did GC */
  44. /* Ini globals */
  45. char *v8_flags; /* V8 command line flags */
  46. int max_disposed_contexts; /* Max disposed context allowed before forcing V8 GC */
  47. ZEND_END_MODULE_GLOBALS(v8js)
  48. #ifdef ZTS
  49. # define V8JSG(v) TSRMG(v8js_globals_id, zend_v8js_globals *, v)
  50. #else
  51. # define V8JSG(v) (v8js_globals.v)
  52. #endif
  53. ZEND_DECLARE_MODULE_GLOBALS(v8js)
  54. /* {{{ INI Settings */
  55. static ZEND_INI_MH(v8js_OnUpdateMaxDisposedContexts) /* {{{ */
  56. {
  57. int max_disposed = zend_atoi(new_value, new_value_length);
  58. if (max_disposed <= 0) {
  59. return FAILURE;
  60. }
  61. V8JSG(max_disposed_contexts) = max_disposed;
  62. return SUCCESS;
  63. }
  64. /* }}} */
  65. static ZEND_INI_MH(v8js_OnUpdateV8Flags) /* {{{ */
  66. {
  67. if (new_value) {
  68. if (V8JSG(v8_flags)) {
  69. free(V8JSG(v8_flags));
  70. V8JSG(v8_flags) = NULL;
  71. }
  72. if (!new_value[0]) {
  73. return FAILURE;
  74. }
  75. V8JSG(v8_flags) = zend_strndup(new_value, new_value_length);
  76. }
  77. return SUCCESS;
  78. }
  79. /* }}} */
  80. ZEND_INI_BEGIN() /* {{{ */
  81. ZEND_INI_ENTRY("v8js.max_disposed_contexts", "25", ZEND_INI_ALL, v8js_OnUpdateMaxDisposedContexts)
  82. ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
  83. ZEND_INI_END()
  84. /* }}} */
  85. /* }}} INI */
  86. /* {{{ Class Entries */
  87. zend_class_entry *php_ce_v8_object;
  88. zend_class_entry *php_ce_v8_function;
  89. static zend_class_entry *php_ce_v8js;
  90. static zend_class_entry *php_ce_v8js_exception;
  91. /* }}} */
  92. /* {{{ Object Handlers */
  93. static zend_object_handlers v8js_object_handlers;
  94. static zend_object_handlers v8_object_handlers;
  95. /* }}} */
  96. /* {{{ Extension container */
  97. struct php_v8js_jsext {
  98. zend_bool auto_enable;
  99. HashTable *deps_ht;
  100. const char **deps;
  101. int deps_count;
  102. char *name;
  103. char *source;
  104. };
  105. /* }}} */
  106. /* {{{ Object container */
  107. struct php_v8js_object {
  108. zend_object std;
  109. v8::Persistent<v8::Value> v8obj;
  110. int flags;
  111. };
  112. /* }}} */
  113. #ifdef COMPILE_DL_V8JS
  114. ZEND_GET_MODULE(v8js)
  115. #endif
  116. /* {{{ Class: V8 */
  117. #define V8JS_V8_INVOKE_FUNC_NAME "V8Js::V8::Invoke"
  118. /* V8 Object handlers */
  119. static zval *php_v8js_v8_read_property(zval *object, zval *member, int type ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  120. {
  121. zval *retval = NULL;
  122. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  123. if (Z_TYPE_P(member) == IS_STRING && obj->v8obj->IsObject() && !obj->v8obj->IsFunction())
  124. {
  125. v8::Local<v8::Object> jsObj = obj->v8obj->ToObject();
  126. v8::Local<v8::String> jsKey = V8JS_STRL(Z_STRVAL_P(member), Z_STRLEN_P(member));
  127. v8::Local<v8::Value> jsVal;
  128. /* Skip any prototype properties */
  129. if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
  130. jsVal = jsObj->Get(jsKey);
  131. if (jsVal->IsObject()) {
  132. ALLOC_INIT_ZVAL(retval);
  133. Z_SET_REFCOUNT_P(retval, 0);
  134. } else {
  135. MAKE_STD_ZVAL(retval);
  136. }
  137. if (v8js_to_zval(jsVal, retval, obj->flags TSRMLS_CC) == SUCCESS) {
  138. return retval;
  139. }
  140. }
  141. }
  142. ALLOC_INIT_ZVAL(retval);
  143. return retval;
  144. }
  145. /* }}} */
  146. static void php_v8js_v8_write_property(zval *object, zval *member, zval *value ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  147. {
  148. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  149. if (obj->v8obj->IsObject() && !obj->v8obj->IsFunction()) {
  150. obj->v8obj->ToObject()->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value TSRMLS_CC));
  151. }
  152. }
  153. /* }}} */
  154. static void php_v8js_v8_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  155. {
  156. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  157. if (obj->v8obj->IsObject() && !obj->v8obj->IsFunction()) {
  158. obj->v8obj->ToObject()->ForceDelete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
  159. }
  160. }
  161. /* }}} */
  162. int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *retval, int flags TSRMLS_DC) /* {{{ */
  163. {
  164. v8::Local<v8::Object> jsObj = jsValue->ToObject();
  165. if (!jsObj.IsEmpty()) {
  166. v8::Local<v8::Array> jsKeys = jsObj->GetPropertyNames();
  167. for (unsigned i = 0; i < jsKeys->Length(); i++)
  168. {
  169. v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString();
  170. /* Skip any prototype properties */
  171. if (!jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey) && !jsObj->HasRealIndexedProperty(i)) {
  172. continue;
  173. }
  174. v8::Local<v8::Value> jsVal = jsObj->Get(jsKey);
  175. v8::String::Utf8Value cstr(jsKey);
  176. const char *key = ToCString(cstr);
  177. zval *value = NULL;
  178. MAKE_STD_ZVAL(value);
  179. if (v8js_to_zval(jsVal, value, flags TSRMLS_CC) == FAILURE) {
  180. zval_ptr_dtor(&value);
  181. return FAILURE;
  182. }
  183. if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
  184. zend_symtable_update(retval, key, strlen(key) + 1, (void *)&value, sizeof(zval *), NULL);
  185. } else {
  186. zend_hash_update(retval, key, strlen(key) + 1, (void *) &value, sizeof(zval *), NULL);
  187. }
  188. }
  189. return SUCCESS;
  190. }
  191. return FAILURE;
  192. }
  193. /* }}} */
  194. static HashTable *php_v8js_v8_get_properties(zval *object TSRMLS_DC) /* {{{ */
  195. {
  196. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  197. HashTable *retval;
  198. ALLOC_HASHTABLE(retval);
  199. zend_hash_init(retval, 0, NULL, ZVAL_PTR_DTOR, 0);
  200. v8::HandleScope local_scope;
  201. v8::Handle<v8::Context> temp_context = v8::Context::New();
  202. v8::Context::Scope temp_scope(temp_context);
  203. if (php_v8js_v8_get_properties_hash(obj->v8obj, retval, obj->flags TSRMLS_CC) == SUCCESS) {
  204. return retval;
  205. }
  206. return NULL;
  207. }
  208. /* }}} */
  209. static HashTable *php_v8js_v8_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
  210. {
  211. *is_temp = 1;
  212. return php_v8js_v8_get_properties(object TSRMLS_CC);
  213. }
  214. /* }}} */
  215. static zend_function *php_v8js_v8_get_method(zval **object_ptr, char *method, int method_len ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  216. {
  217. zend_function *f;
  218. v8::Local<v8::String> jsKey = V8JS_STRL(method, method_len);
  219. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(*object_ptr TSRMLS_CC);
  220. if (!obj->v8obj.IsEmpty() && obj->v8obj->IsObject() && !obj->v8obj->IsFunction()) {
  221. v8::Local<v8::Object> jsObj = obj->v8obj->ToObject();
  222. if (jsObj->Has(jsKey) && jsObj->Get(jsKey)->IsFunction()) {
  223. f = (zend_function *) ecalloc(1, sizeof(*f));
  224. f->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
  225. f->common.function_name = estrndup(method, method_len);
  226. return f;
  227. }
  228. }
  229. return NULL;
  230. }
  231. /* }}} */
  232. #if PHP_VERSION_ID >= 50400
  233. static int php_v8js_v8_call_method(const char *method, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  234. #else
  235. static int php_v8js_v8_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  236. #endif
  237. {
  238. zval *object = this_ptr, ***argv = NULL;
  239. int i = 0, argc = ZEND_NUM_ARGS();
  240. php_v8js_object *obj;
  241. obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  242. if (obj->v8obj.IsEmpty()) {
  243. zval_ptr_dtor(&object);
  244. return FAILURE;
  245. }
  246. if (argc > 0) {
  247. argv = (zval***)safe_emalloc(sizeof(zval**), argc, 0);
  248. zend_get_parameters_array_ex(argc, argv);
  249. }
  250. v8::Local<v8::String> method_name = V8JS_SYML(method, strlen(method));
  251. v8::Local<v8::Object> v8obj = obj->v8obj->ToObject();
  252. v8::Local<v8::Function> cb;
  253. if (method_name->Equals(V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME))) {
  254. cb = v8::Local<v8::Function>::Cast(v8obj);
  255. } else {
  256. cb = v8::Local<v8::Function>::Cast(v8obj->Get(method_name));
  257. }
  258. v8::Local<v8::Value> *jsArgv = new v8::Local<v8::Value>[argc];
  259. v8::Local<v8::Value> js_retval;
  260. for (i = 0; i < argc; i++) {
  261. jsArgv[i] = v8::Local<v8::Value>::New(zval_to_v8js(*argv[i] TSRMLS_CC));
  262. }
  263. js_retval = cb->Call(V8JS_GLOBAL, argc, jsArgv);
  264. zval_ptr_dtor(&object);
  265. if (argc > 0) {
  266. efree(argv);
  267. }
  268. if (return_value_used) {
  269. return v8js_to_zval(js_retval, return_value, obj->flags TSRMLS_CC);
  270. }
  271. return SUCCESS;
  272. }
  273. /* }}} */
  274. static int php_v8js_v8_get_closure(zval *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC) /* {{{ */
  275. {
  276. zend_function *invoke;
  277. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  278. if (!obj->v8obj->IsFunction()) {
  279. return FAILURE;
  280. }
  281. invoke = (zend_function *) ecalloc(1, sizeof(*invoke));
  282. invoke->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
  283. invoke->common.function_name = estrndup(V8JS_V8_INVOKE_FUNC_NAME, sizeof(V8JS_V8_INVOKE_FUNC_NAME) - 1);
  284. *fptr_ptr = invoke;
  285. if (zobj_ptr) {
  286. *zobj_ptr = object;
  287. }
  288. *ce_ptr = NULL;
  289. return SUCCESS;
  290. }
  291. /* }}} */
  292. static void php_v8js_v8_free_storage(void *object, zend_object_handle handle TSRMLS_DC) /* {{{ */
  293. {
  294. php_v8js_object *c = (php_v8js_object *) object;
  295. zend_object_std_dtor(&c->std TSRMLS_CC);
  296. if (!c->v8obj.IsEmpty()) {
  297. c->v8obj.Dispose();
  298. }
  299. efree(object);
  300. }
  301. /* }}} */
  302. static zend_object_value php_v8js_v8_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  303. {
  304. zend_object_value retval;
  305. php_v8js_object *c;
  306. c = (php_v8js_object *) ecalloc(1, sizeof(*c));
  307. zend_object_std_init(&c->std, ce TSRMLS_CC);
  308. retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) php_v8js_v8_free_storage, NULL TSRMLS_CC);
  309. retval.handlers = &v8_object_handlers;
  310. return retval;
  311. }
  312. /* }}} */
  313. void php_v8js_create_v8(zval *res, v8::Handle<v8::Value> value, int flags TSRMLS_DC) /* {{{ */
  314. {
  315. php_v8js_object *c;
  316. object_init_ex(res, value->IsFunction() ? php_ce_v8_function : php_ce_v8_object);
  317. c = (php_v8js_object *) zend_object_store_get_object(res TSRMLS_CC);
  318. c->v8obj = v8::Persistent<v8::Value>::New(value);
  319. c->flags = flags;
  320. }
  321. /* }}} */
  322. /* }}} V8 */
  323. /* {{{ Class: V8Js */
  324. static void php_v8js_free_storage(void *object TSRMLS_DC) /* {{{ */
  325. {
  326. php_v8js_ctx *c = (php_v8js_ctx *) object;
  327. zend_object_std_dtor(&c->std TSRMLS_CC);
  328. if (c->pending_exception) {
  329. zval_ptr_dtor(&c->pending_exception);
  330. }
  331. c->object_name.Dispose();
  332. /* Clear global object, dispose context */
  333. if (!c->context.IsEmpty()) {
  334. c->context.Dispose();
  335. c->context.Clear();
  336. V8JSG(disposed_contexts) = v8::V8::ContextDisposedNotification();
  337. #if V8JS_DEBUG
  338. fprintf(stderr, "Context dispose notification sent (%d)\n", V8JSG(disposed_contexts));
  339. fflush(stderr);
  340. #endif
  341. }
  342. efree(object);
  343. }
  344. /* }}} */
  345. static zend_object_value php_v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  346. {
  347. zend_object_value retval;
  348. php_v8js_ctx *c;
  349. c = (php_v8js_ctx *) ecalloc(1, sizeof(*c));
  350. zend_object_std_init(&c->std, ce TSRMLS_CC);
  351. retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) php_v8js_free_storage, NULL TSRMLS_CC);
  352. retval.handlers = &v8js_object_handlers;
  353. return retval;
  354. }
  355. /* }}} */
  356. static void _php_v8js_free_ext_strarr(const char **arr, int count) /* {{{ */
  357. {
  358. int i;
  359. if (arr) {
  360. for (i = 0; i < count; i++) {
  361. if (arr[i]) {
  362. free((void *) arr[i]);
  363. }
  364. }
  365. free(arr);
  366. }
  367. }
  368. /* }}} */
  369. static void php_v8js_jsext_dtor(php_v8js_jsext *jsext) /* {{{ */
  370. {
  371. if (jsext->deps_ht) {
  372. zend_hash_destroy(jsext->deps_ht);
  373. free(jsext->deps_ht);
  374. }
  375. if (jsext->deps) {
  376. _php_v8js_free_ext_strarr(jsext->deps, jsext->deps_count);
  377. }
  378. free(jsext->name);
  379. free(jsext->source);
  380. }
  381. /* }}} */
  382. static int _php_v8js_create_ext_strarr(const char ***retval, int count, HashTable *ht) /* {{{ */
  383. {
  384. const char **exts = NULL;
  385. HashPosition pos;
  386. zval **tmp;
  387. int i = 0;
  388. exts = (const char **) calloc(1, count * sizeof(char *));
  389. zend_hash_internal_pointer_reset_ex(ht, &pos);
  390. while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &pos) == SUCCESS) {
  391. if (Z_TYPE_PP(tmp) == IS_STRING) {
  392. exts[i++] = zend_strndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
  393. } else {
  394. _php_v8js_free_ext_strarr(exts, i);
  395. return FAILURE;
  396. }
  397. zend_hash_move_forward_ex(ht, &pos);
  398. }
  399. *retval = exts;
  400. return SUCCESS;
  401. }
  402. /* }}} */
  403. static void php_v8js_fatal_error_handler(const char *location, const char *message) /* {{{ */
  404. {
  405. if (location) {
  406. zend_error(E_ERROR, "%s %s", location, message);
  407. } else {
  408. zend_error(E_ERROR, "%s", message);
  409. }
  410. }
  411. /* }}} */
  412. static void php_v8js_init(TSRMLS_D) /* {{{ */
  413. {
  414. /* Run only once */
  415. if (V8JSG(v8_initialized)) {
  416. return;
  417. }
  418. /* Set V8 command line flags (must be done before V8::Initialize()!) */
  419. if (V8JSG(v8_flags)) {
  420. v8::V8::SetFlagsFromString(V8JSG(v8_flags), strlen(V8JSG(v8_flags)));
  421. }
  422. /* Initialize V8 */
  423. v8::V8::Initialize();
  424. /* Redirect fatal errors to PHP error handler */
  425. v8::V8::SetFatalErrorHandler(php_v8js_fatal_error_handler);
  426. /* Run only once */
  427. V8JSG(v8_initialized) = 1;
  428. }
  429. /* }}} */
  430. /* {{{ proto void V8Js::__construct([string object_name [, array variables [, array extensions [, bool report_uncaught_exceptions]]])
  431. __construct for V8Js */
  432. static PHP_METHOD(V8Js, __construct)
  433. {
  434. char *object_name = NULL, *class_name = NULL;
  435. int object_name_len = 0, free = 0;
  436. zend_uint class_name_len = 0;
  437. zend_bool report_uncaught = 1;
  438. zval *vars_arr = NULL, *exts_arr = NULL;
  439. const char **exts = NULL;
  440. int exts_count = 0;
  441. php_v8js_ctx *c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  442. if (!c->context.IsEmpty()) {
  443. /* called __construct() twice, bail out */
  444. return;
  445. }
  446. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|saab", &object_name, &object_name_len, &vars_arr, &exts_arr, &report_uncaught) == FAILURE) {
  447. return;
  448. }
  449. /* Throw PHP exception if uncaught exceptions exist */
  450. c->report_uncaught = report_uncaught;
  451. c->pending_exception = NULL;
  452. c->in_execution = 0;
  453. c->module_loader = NULL;
  454. /* Initialize V8 */
  455. php_v8js_init(TSRMLS_C);
  456. /* Include extensions used by this context */
  457. /* Note: Extensions registered with auto_enable do not need to be added separately like this. */
  458. if (exts_arr)
  459. {
  460. exts_count = zend_hash_num_elements(Z_ARRVAL_P(exts_arr));
  461. if (_php_v8js_create_ext_strarr(&exts, exts_count, Z_ARRVAL_P(exts_arr)) == FAILURE) {
  462. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid extensions array passed");
  463. return;
  464. }
  465. }
  466. /* Declare configuration for extensions */
  467. v8::ExtensionConfiguration extension_conf(exts_count, exts);
  468. /* Handle scope */
  469. v8::HandleScope handle_scope;
  470. /* Create global template for global object */
  471. if (V8JSG(global_template).IsEmpty()) {
  472. V8JSG(global_template) = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
  473. V8JSG(global_template)->SetClassName(V8JS_SYM("V8Js"));
  474. /* Register builtin methods */
  475. php_v8js_register_methods(V8JSG(global_template)->InstanceTemplate(), c);
  476. }
  477. /* Create context */
  478. c->context = v8::Context::New(&extension_conf, V8JSG(global_template)->InstanceTemplate());
  479. if (exts) {
  480. _php_v8js_free_ext_strarr(exts, exts_count);
  481. }
  482. /* If extensions have errors, context will be empty. (NOTE: This is V8 stuff, they expect the passed sources to compile :) */
  483. if (c->context.IsEmpty()) {
  484. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create V8 context. Check that registered extensions do not have errors.");
  485. ZVAL_NULL(getThis());
  486. return;
  487. }
  488. /* Enter context */
  489. v8::Context::Scope context_scope(c->context);
  490. /* Create the PHP container object's function template */
  491. v8::Local<v8::FunctionTemplate> php_obj_t = v8::FunctionTemplate::New();
  492. /* Set class name for PHP object */
  493. #if PHP_VERSION_ID >= 50400
  494. free = !zend_get_object_classname(getThis(), const_cast<const char**>(&class_name), &class_name_len TSRMLS_CC);
  495. #else
  496. free = !zend_get_object_classname(getThis(), &class_name, &class_name_len TSRMLS_CC);
  497. #endif
  498. php_obj_t->SetClassName(V8JS_SYML(class_name, class_name_len));
  499. if (free) {
  500. efree(class_name);
  501. }
  502. /* Register Get accessor for passed variables */
  503. if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
  504. php_v8js_register_accessors(php_obj_t->InstanceTemplate(), vars_arr TSRMLS_CC);
  505. }
  506. /* Set name for the PHP JS object */
  507. c->object_name = v8::Persistent<v8::String>::New((object_name_len) ? V8JS_SYML(object_name, object_name_len) : V8JS_SYM("PHP"));
  508. /* Add the PHP object into global object */
  509. V8JS_GLOBAL->Set(c->object_name, php_obj_t->InstanceTemplate()->NewInstance(), v8::ReadOnly);
  510. }
  511. /* }}} */
  512. #define V8JS_BEGIN_CTX(ctx, object) \
  513. php_v8js_ctx *(ctx); \
  514. \
  515. if (!V8JSG(v8_initialized)) { \
  516. zend_error(E_ERROR, "V8 not initialized"); \
  517. return; \
  518. } \
  519. \
  520. (ctx) = (php_v8js_ctx *) zend_object_store_get_object(object TSRMLS_CC); \
  521. v8::Context::Scope context_scope((ctx)->context);
  522. /* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
  523. */
  524. static PHP_METHOD(V8Js, executeString)
  525. {
  526. char *str = NULL, *identifier = NULL;
  527. int str_len = 0, identifier_len = 0;
  528. long flags = V8JS_FLAG_NONE;
  529. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sl", &str, &str_len, &identifier, &identifier_len, &flags) == FAILURE) {
  530. return;
  531. }
  532. V8JS_BEGIN_CTX(c, getThis())
  533. /* Catch JS exceptions */
  534. v8::TryCatch try_catch;
  535. v8::HandleScope handle_scope;
  536. /* Set script identifier */
  537. v8::Local<v8::String> sname = identifier_len ? V8JS_SYML(identifier, identifier_len) : V8JS_SYM("V8Js::executeString()");
  538. /* Compiles a string context independently. TODO: Add a php function which calls this and returns the result as resource which can be executed later. */
  539. v8::Local<v8::String> source = v8::String::New(str, str_len);
  540. v8::Local<v8::Script> script = v8::Script::New(source, sname);
  541. /* Compile errors? */
  542. if (script.IsEmpty()) {
  543. php_v8js_throw_exception(&try_catch TSRMLS_CC);
  544. return;
  545. }
  546. /* Set flags for runtime use */
  547. V8JS_GLOBAL_SET_FLAGS(flags);
  548. /* Execute script */
  549. c->in_execution++;
  550. v8::Local<v8::Value> result = script->Run();
  551. c->in_execution--;
  552. /* Script possibly terminated, return immediately */
  553. if (!try_catch.CanContinue()) {
  554. /* TODO: throw PHP exception here? */
  555. return;
  556. }
  557. /* There was pending exception left from earlier executions -> throw to PHP */
  558. if (c->pending_exception) {
  559. zend_throw_exception_object(c->pending_exception TSRMLS_CC);
  560. c->pending_exception = NULL;
  561. }
  562. /* Handle runtime JS exceptions */
  563. if (try_catch.HasCaught()) {
  564. /* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
  565. if (c->in_execution < 1) {
  566. /* Report immediately if report_uncaught is true */
  567. if (c->report_uncaught) {
  568. php_v8js_throw_exception(&try_catch TSRMLS_CC);
  569. return;
  570. }
  571. /* Exception thrown from JS, preserve it for future execution */
  572. if (result.IsEmpty()) {
  573. MAKE_STD_ZVAL(c->pending_exception);
  574. php_v8js_create_exception(c->pending_exception, &try_catch TSRMLS_CC);
  575. }
  576. }
  577. /* Rethrow back to JS */
  578. try_catch.ReThrow();
  579. return;
  580. }
  581. /* Convert V8 value to PHP value */
  582. if (!result.IsEmpty()) {
  583. v8js_to_zval(result, return_value, flags TSRMLS_CC);
  584. }
  585. }
  586. /* }}} */
  587. /* {{{ proto mixed V8Js::getPendingException()
  588. */
  589. static PHP_METHOD(V8Js, getPendingException)
  590. {
  591. php_v8js_ctx *c;
  592. if (zend_parse_parameters_none() == FAILURE) {
  593. return;
  594. }
  595. c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  596. if (c->pending_exception) {
  597. RETURN_ZVAL(c->pending_exception, 1, 0);
  598. }
  599. }
  600. /* }}} */
  601. /* {{{ proto void V8Js::setModuleLoader(string module)
  602. */
  603. static PHP_METHOD(V8Js, setModuleLoader)
  604. {
  605. php_v8js_ctx *c;
  606. zval *callable;
  607. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callable) == FAILURE) {
  608. return;
  609. }
  610. c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  611. c->module_loader = callable;
  612. Z_ADDREF_P(c->module_loader);
  613. }
  614. /* }}} */
  615. static void php_v8js_persistent_zval_ctor(zval **p) /* {{{ */
  616. {
  617. zval *orig_ptr = *p;
  618. *p = (zval *) malloc(sizeof(zval));
  619. **p = *orig_ptr;
  620. switch (Z_TYPE_P(*p)) {
  621. case IS_STRING:
  622. Z_STRVAL_P(*p) = (char *) zend_strndup(Z_STRVAL_P(*p), Z_STRLEN_P(*p));
  623. break;
  624. default:
  625. zend_bailout();
  626. }
  627. INIT_PZVAL(*p);
  628. }
  629. /* }}} */
  630. static void php_v8js_persistent_zval_dtor(zval **p) /* {{{ */
  631. {
  632. switch (Z_TYPE_P(*p)) {
  633. case IS_STRING:
  634. free(Z_STRVAL_P(*p));
  635. break;
  636. default:
  637. zend_bailout();
  638. }
  639. free(*p);
  640. }
  641. /* }}} */
  642. static int php_v8js_register_extension(char *name, uint name_len, char *source, uint source_len, zval *deps_arr, zend_bool auto_enable TSRMLS_DC) /* {{{ */
  643. {
  644. php_v8js_jsext *jsext = NULL;
  645. if (!V8JSG(extensions)) {
  646. V8JSG(extensions) = (HashTable *) malloc(sizeof(HashTable));
  647. zend_hash_init(V8JSG(extensions), 1, NULL, (dtor_func_t) php_v8js_jsext_dtor, 1);
  648. } else if (zend_hash_exists(V8JSG(extensions), name, name_len + 1)) {
  649. return FAILURE;
  650. }
  651. jsext = (php_v8js_jsext *) calloc(1, sizeof(php_v8js_jsext));
  652. if (deps_arr) {
  653. jsext->deps_count = zend_hash_num_elements(Z_ARRVAL_P(deps_arr));
  654. if (_php_v8js_create_ext_strarr(&jsext->deps, jsext->deps_count, Z_ARRVAL_P(deps_arr)) == FAILURE) {
  655. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid dependancy array passed");
  656. php_v8js_jsext_dtor(jsext);
  657. free(jsext);
  658. return FAILURE;
  659. }
  660. }
  661. jsext->auto_enable = auto_enable;
  662. jsext->name = zend_strndup(name, name_len);
  663. jsext->source = zend_strndup(source, source_len);
  664. if (jsext->deps) {
  665. jsext->deps_ht = (HashTable *) malloc(sizeof(HashTable));
  666. zend_hash_init(jsext->deps_ht, jsext->deps_count, NULL, (dtor_func_t) php_v8js_persistent_zval_dtor, 1);
  667. zend_hash_copy(jsext->deps_ht, Z_ARRVAL_P(deps_arr), (copy_ctor_func_t) php_v8js_persistent_zval_ctor, NULL, sizeof(zval *));
  668. }
  669. if (zend_hash_add(V8JSG(extensions), name, name_len + 1, jsext, sizeof(php_v8js_jsext), NULL) == FAILURE) {
  670. php_v8js_jsext_dtor(jsext);
  671. free(jsext);
  672. return FAILURE;
  673. }
  674. v8::Extension *extension = new v8::Extension(jsext->name, jsext->source, jsext->deps_count, jsext->deps);
  675. extension->set_auto_enable(auto_enable ? true : false);
  676. v8::RegisterExtension(extension);
  677. free(jsext);
  678. return SUCCESS;
  679. }
  680. /* }}} */
  681. /* {{{ proto bool V8Js::registerExtension(string ext_name, string script [, array deps [, bool auto_enable]])
  682. */
  683. static PHP_METHOD(V8Js, registerExtension)
  684. {
  685. char *ext_name, *script;
  686. zval *deps_arr = NULL;
  687. int ext_name_len, script_len;
  688. zend_bool auto_enable = 0;
  689. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ab", &ext_name, &ext_name_len, &script, &script_len, &deps_arr, &auto_enable) == FAILURE) {
  690. return;
  691. }
  692. if (!ext_name_len) {
  693. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Extension name cannot be empty");
  694. } else if (!script_len) {
  695. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Script cannot be empty");
  696. } else if (php_v8js_register_extension(ext_name, ext_name_len, script, script_len, deps_arr, auto_enable TSRMLS_CC) == SUCCESS) {
  697. RETURN_TRUE;
  698. }
  699. RETURN_FALSE;
  700. }
  701. /* }}} */
  702. /* ## Static methods ## */
  703. /* {{{ proto array V8Js::getExtensions()
  704. */
  705. static PHP_METHOD(V8Js, getExtensions)
  706. {
  707. php_v8js_jsext *jsext;
  708. zval *ext, *deps_arr;
  709. HashPosition pos;
  710. ulong index;
  711. char *key;
  712. uint key_len;
  713. if (zend_parse_parameters_none() == FAILURE) {
  714. return;
  715. }
  716. array_init(return_value);
  717. if (V8JSG(extensions)) {
  718. zend_hash_internal_pointer_reset_ex(V8JSG(extensions), &pos);
  719. while (zend_hash_get_current_data_ex(V8JSG(extensions), (void **) &jsext, &pos) == SUCCESS) {
  720. if (zend_hash_get_current_key_ex(V8JSG(extensions), &key, &key_len, &index, 0, &pos) == HASH_KEY_IS_STRING) {
  721. MAKE_STD_ZVAL(ext)
  722. array_init(ext);
  723. add_assoc_bool_ex(ext, ZEND_STRS("auto_enable"), jsext->auto_enable);
  724. if (jsext->deps_ht) {
  725. MAKE_STD_ZVAL(deps_arr);
  726. array_init(deps_arr);
  727. zend_hash_copy(Z_ARRVAL_P(deps_arr), jsext->deps_ht, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
  728. add_assoc_zval_ex(ext, ZEND_STRS("deps"), deps_arr);
  729. }
  730. add_assoc_zval_ex(return_value, key, key_len, ext);
  731. }
  732. zend_hash_move_forward_ex(V8JSG(extensions), &pos);
  733. }
  734. }
  735. }
  736. /* }}} */
  737. /* {{{ arginfo */
  738. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0)
  739. ZEND_ARG_INFO(0, object_name)
  740. ZEND_ARG_INFO(0, variables)
  741. ZEND_ARG_INFO(0, extensions)
  742. ZEND_ARG_INFO(0, report_uncaught_exceptions)
  743. ZEND_END_ARG_INFO()
  744. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1)
  745. ZEND_ARG_INFO(0, script)
  746. ZEND_ARG_INFO(0, identifier)
  747. ZEND_ARG_INFO(0, flags)
  748. ZEND_END_ARG_INFO()
  749. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getpendingexception, 0)
  750. ZEND_END_ARG_INFO()
  751. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
  752. ZEND_ARG_INFO(0, callable)
  753. ZEND_END_ARG_INFO()
  754. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_registerextension, 0, 0, 2)
  755. ZEND_ARG_INFO(0, extension_name)
  756. ZEND_ARG_INFO(0, script)
  757. ZEND_ARG_INFO(0, dependencies)
  758. ZEND_ARG_INFO(0, auto_enable)
  759. ZEND_END_ARG_INFO()
  760. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0)
  761. ZEND_END_ARG_INFO()
  762. ZEND_BEGIN_ARG_INFO(arginfo_v8jsexception_no_args, 0)
  763. ZEND_END_ARG_INFO()
  764. /* }}} */
  765. static const zend_function_entry v8js_methods[] = { /* {{{ */
  766. PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  767. PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC)
  768. PHP_ME(V8Js, getPendingException, arginfo_v8js_getpendingexception, ZEND_ACC_PUBLIC)
  769. PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
  770. PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  771. PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  772. {NULL, NULL, NULL}
  773. };
  774. /* }}} */
  775. /* V8Js object handlers */
  776. static void php_v8js_write_property(zval *object, zval *member, zval *value ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  777. {
  778. V8JS_BEGIN_CTX(c, object)
  779. v8::HandleScope handle_scope;
  780. /* Global PHP JS object */
  781. v8::Local<v8::Object> jsobj = V8JS_GLOBAL->Get(c->object_name)->ToObject();
  782. /* Write value to PHP JS object */
  783. jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value TSRMLS_CC), v8::ReadOnly);
  784. /* Write value to PHP object */
  785. std_object_handlers.write_property(object, member, value ZEND_HASH_KEY_CC TSRMLS_CC);
  786. }
  787. /* }}} */
  788. static void php_v8js_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  789. {
  790. V8JS_BEGIN_CTX(c, object)
  791. v8::HandleScope handle_scope;
  792. /* Global PHP JS object */
  793. v8::Local<v8::Object> jsobj = V8JS_GLOBAL->Get(c->object_name)->ToObject();
  794. /* Delete value from PHP JS object */
  795. jsobj->ForceDelete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
  796. /* Unset from PHP object */
  797. std_object_handlers.unset_property(object, member ZEND_HASH_KEY_CC TSRMLS_CC);
  798. }
  799. /* }}} */
  800. /* }}} V8Js */
  801. /* {{{ Class: V8JsException */
  802. static void php_v8js_create_exception(zval *return_value, v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  803. {
  804. v8::String::Utf8Value exception(try_catch->Exception());
  805. const char *exception_string = ToCString(exception);
  806. v8::Handle<v8::Message> tc_message = try_catch->Message();
  807. const char *filename_string, *sourceline_string;
  808. char *message_string;
  809. int linenum, message_len;
  810. object_init_ex(return_value, php_ce_v8js_exception);
  811. #define PHPV8_EXPROP(type, name, value) \
  812. zend_update_property##type(php_ce_v8js_exception, return_value, #name, sizeof(#name) - 1, value TSRMLS_CC);
  813. if (tc_message.IsEmpty()) {
  814. message_len = spprintf(&message_string, 0, "%s", exception_string);
  815. }
  816. else
  817. {
  818. v8::String::Utf8Value filename(tc_message->GetScriptResourceName());
  819. filename_string = ToCString(filename);
  820. PHPV8_EXPROP(_string, JsFileName, filename_string);
  821. v8::String::Utf8Value sourceline(tc_message->GetSourceLine());
  822. sourceline_string = ToCString(sourceline);
  823. PHPV8_EXPROP(_string, JsSourceLine, sourceline_string);
  824. linenum = tc_message->GetLineNumber();
  825. PHPV8_EXPROP(_long, JsLineNumber, linenum);
  826. message_len = spprintf(&message_string, 0, "%s:%d: %s", filename_string, linenum, exception_string);
  827. v8::String::Utf8Value stacktrace(try_catch->StackTrace());
  828. if (stacktrace.length() > 0) {
  829. const char* stacktrace_string = ToCString(stacktrace);
  830. PHPV8_EXPROP(_string, JsTrace, stacktrace_string);
  831. }
  832. }
  833. PHPV8_EXPROP(_string, message, message_string);
  834. efree(message_string);
  835. }
  836. /* }}} */
  837. static void php_v8js_throw_exception(v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  838. {
  839. v8::String::Utf8Value exception(try_catch->Exception());
  840. const char *exception_string = ToCString(exception);
  841. zval *zexception = NULL;
  842. if (try_catch->Message().IsEmpty()) {
  843. zend_throw_exception(php_ce_v8js_exception, (char *) exception_string, 0 TSRMLS_CC);
  844. } else {
  845. MAKE_STD_ZVAL(zexception);
  846. php_v8js_create_exception(zexception, try_catch TSRMLS_CC);
  847. zend_throw_exception_object(zexception TSRMLS_CC);
  848. }
  849. }
  850. /* }}} */
  851. #define V8JS_EXCEPTION_METHOD(property) \
  852. static PHP_METHOD(V8JsException, get##property) \
  853. { \
  854. zval *value; \
  855. \
  856. if (zend_parse_parameters_none() == FAILURE) { \
  857. return; \
  858. } \
  859. value = zend_read_property(php_ce_v8js_exception, getThis(), #property, sizeof(#property) - 1, 0 TSRMLS_CC); \
  860. *return_value = *value; \
  861. zval_copy_ctor(return_value); \
  862. INIT_PZVAL(return_value); \
  863. }
  864. /* {{{ proto string V8JsException::getJsFileName()
  865. */
  866. V8JS_EXCEPTION_METHOD(JsFileName);
  867. /* }}} */
  868. /* {{{ proto string V8JsException::getJsLineNumber()
  869. */
  870. V8JS_EXCEPTION_METHOD(JsLineNumber);
  871. /* }}} */
  872. /* {{{ proto string V8JsException::getJsSourceLine()
  873. */
  874. V8JS_EXCEPTION_METHOD(JsSourceLine);
  875. /* }}} */
  876. /* {{{ proto string V8JsException::getJsTrace()
  877. */
  878. V8JS_EXCEPTION_METHOD(JsTrace);
  879. /* }}} */
  880. static const zend_function_entry v8js_exception_methods[] = { /* {{{ */
  881. PHP_ME(V8JsException, getJsFileName, arginfo_v8jsexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  882. PHP_ME(V8JsException, getJsLineNumber, arginfo_v8jsexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  883. PHP_ME(V8JsException, getJsSourceLine, arginfo_v8jsexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  884. PHP_ME(V8JsException, getJsTrace, arginfo_v8jsexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  885. {NULL, NULL, NULL}
  886. };
  887. /* }}} */
  888. /* }}} V8JsException */
  889. /* {{{ PHP_MINIT_FUNCTION
  890. */
  891. static PHP_MINIT_FUNCTION(v8js)
  892. {
  893. zend_class_entry ce;
  894. /* V8Object Class */
  895. INIT_CLASS_ENTRY(ce, "V8Object", NULL);
  896. php_ce_v8_object = zend_register_internal_class(&ce TSRMLS_CC);
  897. php_ce_v8_object->ce_flags |= ZEND_ACC_FINAL;
  898. php_ce_v8_object->create_object = php_v8js_v8_new;
  899. /* V8Function Class */
  900. INIT_CLASS_ENTRY(ce, "V8Function", NULL);
  901. php_ce_v8_function = zend_register_internal_class(&ce TSRMLS_CC);
  902. php_ce_v8_function->ce_flags |= ZEND_ACC_FINAL;
  903. php_ce_v8_function->create_object = php_v8js_v8_new;
  904. /* V8<Object|Function> handlers */
  905. memcpy(&v8_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  906. v8_object_handlers.clone_obj = NULL;
  907. v8_object_handlers.cast_object = NULL;
  908. v8_object_handlers.get_constructor = NULL;
  909. v8_object_handlers.get_property_ptr_ptr = NULL;
  910. // v8_object_handlers.has_property = php_v8js_v8_has_property; // Not implemented yet
  911. v8_object_handlers.read_property = php_v8js_v8_read_property;
  912. v8_object_handlers.write_property = php_v8js_v8_write_property;
  913. v8_object_handlers.unset_property = php_v8js_v8_unset_property;
  914. v8_object_handlers.get_properties = php_v8js_v8_get_properties;
  915. v8_object_handlers.get_method = php_v8js_v8_get_method;
  916. v8_object_handlers.call_method = php_v8js_v8_call_method;
  917. v8_object_handlers.get_debug_info = php_v8js_v8_get_debug_info;
  918. v8_object_handlers.get_closure = php_v8js_v8_get_closure;
  919. /* V8Js Class */
  920. INIT_CLASS_ENTRY(ce, "V8Js", v8js_methods);
  921. php_ce_v8js = zend_register_internal_class(&ce TSRMLS_CC);
  922. php_ce_v8js->create_object = php_v8js_new;
  923. /* V8Js handlers */
  924. memcpy(&v8js_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  925. v8js_object_handlers.clone_obj = NULL;
  926. v8js_object_handlers.write_property = php_v8js_write_property;
  927. v8js_object_handlers.unset_property = php_v8js_unset_property;
  928. /* V8Js Class Constants */
  929. zend_declare_class_constant_string(php_ce_v8js, ZEND_STRL("V8_VERSION"), PHP_V8_VERSION TSRMLS_CC);
  930. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE TSRMLS_CC);
  931. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY TSRMLS_CC);
  932. /* V8JsException Class */
  933. INIT_CLASS_ENTRY(ce, "V8JsException", v8js_exception_methods);
  934. php_ce_v8js_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
  935. php_ce_v8js_exception->ce_flags |= ZEND_ACC_FINAL;
  936. /* Add custom JS specific properties */
  937. zend_declare_property_null(php_ce_v8js_exception, ZEND_STRL("JsFileName"), ZEND_ACC_PROTECTED TSRMLS_CC);
  938. zend_declare_property_null(php_ce_v8js_exception, ZEND_STRL("JsLineNumber"), ZEND_ACC_PROTECTED TSRMLS_CC);
  939. zend_declare_property_null(php_ce_v8js_exception, ZEND_STRL("JsSourceLine"), ZEND_ACC_PROTECTED TSRMLS_CC);
  940. zend_declare_property_null(php_ce_v8js_exception, ZEND_STRL("JsTrace"), ZEND_ACC_PROTECTED TSRMLS_CC);
  941. REGISTER_INI_ENTRIES();
  942. return SUCCESS;
  943. }
  944. /* }}} */
  945. static void php_v8js_force_v8_gc(void) /* {{{ */
  946. {
  947. #if V8JS_DEBUG
  948. TSRMLS_FETCH();
  949. fprintf(stderr, "############ Running V8 Idle notification: disposed/max_disposed %d/%d ############\n", V8JSG(disposed_contexts), V8JSG(max_disposed_contexts));
  950. fflush(stderr);
  951. #endif
  952. while (!v8::V8::IdleNotification()) {
  953. #if V8JS_DEBUG
  954. fprintf(stderr, ".");
  955. fflush(stderr);
  956. #endif
  957. }
  958. #if V8JS_DEBUG
  959. fprintf(stderr, "\n");
  960. fflush(stderr);
  961. #endif
  962. }
  963. /* }}} */
  964. /* {{{ PHP_MSHUTDOWN_FUNCTION
  965. */
  966. static PHP_MSHUTDOWN_FUNCTION(v8js)
  967. {
  968. UNREGISTER_INI_ENTRIES();
  969. v8::V8::Dispose();
  970. if (V8JSG(extensions)) {
  971. zend_hash_destroy(V8JSG(extensions));
  972. free(V8JSG(extensions));
  973. V8JSG(extensions) = NULL;
  974. }
  975. if (V8JSG(v8_flags)) {
  976. free(V8JSG(v8_flags));
  977. V8JSG(v8_flags) = NULL;
  978. }
  979. return SUCCESS;
  980. }
  981. /* }}} */
  982. /* {{{ PHP_RSHUTDOWN_FUNCTION
  983. */
  984. static PHP_RSHUTDOWN_FUNCTION(v8js)
  985. {
  986. #if V8JS_DEBUG
  987. v8::HeapStatistics stats;
  988. v8::V8::GetHeapStatistics(&stats);
  989. float used = stats.used_heap_size() / 1024.0 / 1024.0;
  990. float total = stats.total_heap_size() / 1024.0 / 1024.0;
  991. fprintf(stderr, "### RSHUTDOWN ###\n");
  992. fprintf(stderr, "############ Heap Used/Total %.2f/%.2f MB ############\n", used, total);
  993. fflush(stderr);
  994. #endif
  995. /* Force V8 to do as much GC as possible */
  996. if (V8JSG(max_disposed_contexts) && (V8JSG(disposed_contexts) > V8JSG(max_disposed_contexts))) {
  997. php_v8js_force_v8_gc();
  998. }
  999. return SUCCESS;
  1000. }
  1001. /* }}} */
  1002. /* {{{ PHP_MINFO_FUNCTION
  1003. */
  1004. static PHP_MINFO_FUNCTION(v8js)
  1005. {
  1006. php_info_print_table_start();
  1007. php_info_print_table_header(2, "V8 Javascript Engine", "enabled");
  1008. php_info_print_table_header(2, "V8 Engine Compiled Version", PHP_V8_VERSION);
  1009. php_info_print_table_header(2, "V8 Engine Linked Version", v8::V8::GetVersion());
  1010. php_info_print_table_header(2, "Version", V8JS_VERSION);
  1011. php_info_print_table_end();
  1012. DISPLAY_INI_ENTRIES();
  1013. }
  1014. /* }}} */
  1015. /* {{{ PHP_GINIT_FUNCTION
  1016. */
  1017. static PHP_GINIT_FUNCTION(v8js)
  1018. {
  1019. v8js_globals->extensions = NULL;
  1020. v8js_globals->disposed_contexts = 0;
  1021. v8js_globals->v8_initialized = 0;
  1022. v8js_globals->v8_flags = NULL;
  1023. }
  1024. /* }}} */
  1025. /* {{{ v8js_functions[] */
  1026. static const zend_function_entry v8js_functions[] = {
  1027. {NULL, NULL, NULL}
  1028. };
  1029. /* }}} */
  1030. /* {{{ v8js_module_entry
  1031. */
  1032. zend_module_entry v8js_module_entry = {
  1033. STANDARD_MODULE_HEADER_EX,
  1034. NULL,
  1035. NULL,
  1036. "v8js",
  1037. v8js_functions,
  1038. PHP_MINIT(v8js),
  1039. PHP_MSHUTDOWN(v8js),
  1040. NULL,
  1041. PHP_RSHUTDOWN(v8js),
  1042. PHP_MINFO(v8js),
  1043. V8JS_VERSION,
  1044. PHP_MODULE_GLOBALS(v8js),
  1045. PHP_GINIT(v8js),
  1046. NULL,
  1047. NULL,
  1048. STANDARD_MODULE_PROPERTIES_EX
  1049. };
  1050. /* }}} */
  1051. /*
  1052. * Local variables:
  1053. * tab-width: 4
  1054. * c-basic-offset: 4
  1055. * End:
  1056. * vim600: noet sw=4 ts=4 fdm=marker
  1057. * vim<600: noet sw=4 ts=4
  1058. */