v8js.cc 36 KB

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