v8js.cc 36 KB

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