v8js.cc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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, const zend_literal *key 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, const zend_literal *key 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, const zend_literal *key 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, const zend_literal *key 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(const 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;
  441. const char *class_name = NULL;
  442. int object_name_len = 0, free = 0;
  443. zend_uint class_name_len = 0;
  444. zend_bool report_uncaught = 1;
  445. zval *vars_arr = NULL, *exts_arr = NULL;
  446. const char **exts = NULL;
  447. int exts_count = 0;
  448. php_v8js_ctx *c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  449. if (!c->context.IsEmpty()) {
  450. /* called __construct() twice, bail out */
  451. return;
  452. }
  453. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|saab", &object_name, &object_name_len, &vars_arr, &exts_arr, &report_uncaught) == FAILURE) {
  454. return;
  455. }
  456. /* Throw PHP exception if uncaught exceptions exist */
  457. c->report_uncaught = report_uncaught;
  458. c->pending_exception = NULL;
  459. c->in_execution = 0;
  460. /* Initialize V8 */
  461. php_v8js_init(TSRMLS_C);
  462. /* Include extensions used by this context */
  463. /* Note: Extensions registered with auto_enable do not need to be added separately like this. */
  464. if (exts_arr)
  465. {
  466. exts_count = zend_hash_num_elements(Z_ARRVAL_P(exts_arr));
  467. if (_php_v8js_create_ext_strarr(&exts, exts_count, Z_ARRVAL_P(exts_arr)) == FAILURE) {
  468. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid extensions array passed");
  469. return;
  470. }
  471. }
  472. /* Declare configuration for extensions */
  473. v8::ExtensionConfiguration extension_conf(exts_count, exts);
  474. /* Handle scope */
  475. v8::HandleScope handle_scope;
  476. /* Create global template for global object */
  477. if (V8JSG(global_template).IsEmpty()) {
  478. V8JSG(global_template) = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
  479. V8JSG(global_template)->SetClassName(V8JS_SYM("V8Js"));
  480. /* Register builtin methods */
  481. php_v8js_register_methods(V8JSG(global_template)->InstanceTemplate());
  482. }
  483. /* Create context */
  484. c->context = v8::Context::New(&extension_conf, V8JSG(global_template)->InstanceTemplate());
  485. if (exts) {
  486. _php_v8js_free_ext_strarr(exts, exts_count);
  487. }
  488. /* If extensions have errors, context will be empty. (NOTE: This is V8 stuff, they expect the passed sources to compile :) */
  489. if (c->context.IsEmpty()) {
  490. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create V8 context. Check that registered extensions do not have errors.");
  491. ZVAL_NULL(getThis());
  492. return;
  493. }
  494. /* Enter context */
  495. v8::Context::Scope context_scope(c->context);
  496. /* Create the PHP container object's function template */
  497. v8::Local<v8::FunctionTemplate> php_obj_t = v8::FunctionTemplate::New();
  498. /* Set class name for PHP object */
  499. free = !zend_get_object_classname(getThis(), &class_name, &class_name_len TSRMLS_CC);
  500. php_obj_t->SetClassName(V8JS_SYML(class_name, class_name_len));
  501. if (free) {
  502. efree((char*)class_name);
  503. }
  504. /* Register Get accessor for passed variables */
  505. if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
  506. php_v8js_register_accessors(php_obj_t->InstanceTemplate(), vars_arr TSRMLS_CC);
  507. }
  508. /* Set name for the PHP JS object */
  509. c->object_name = v8::Persistent<v8::String>::New((object_name_len) ? V8JS_SYML(object_name, object_name_len) : V8JS_SYM("PHP"));
  510. /* Add the PHP object into global object */
  511. V8JS_GLOBAL->Set(c->object_name, php_obj_t->InstanceTemplate()->NewInstance(), v8::ReadOnly);
  512. }
  513. /* }}} */
  514. #define V8JS_BEGIN_CTX(ctx, object) \
  515. php_v8js_ctx *(ctx); \
  516. \
  517. if (!V8JSG(v8_initialized)) { \
  518. zend_error(E_ERROR, "V8 not initialized"); \
  519. return; \
  520. } \
  521. \
  522. (ctx) = (php_v8js_ctx *) zend_object_store_get_object(object TSRMLS_CC); \
  523. v8::Context::Scope context_scope((ctx)->context);
  524. /* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
  525. */
  526. static PHP_METHOD(V8Js, executeString)
  527. {
  528. char *str = NULL, *identifier = NULL;
  529. int str_len = 0, identifier_len = 0;
  530. long flags = V8JS_FLAG_NONE;
  531. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sl", &str, &str_len, &identifier, &identifier_len, &flags) == FAILURE) {
  532. return;
  533. }
  534. V8JS_BEGIN_CTX(c, getThis())
  535. /* Catch JS exceptions */
  536. v8::TryCatch try_catch;
  537. v8::HandleScope handle_scope;
  538. /* Set script identifier */
  539. v8::Local<v8::String> sname = identifier_len ? V8JS_SYML(identifier, identifier_len) : V8JS_SYM("V8Js::executeString()");
  540. /* Compiles a string context independently. TODO: Add a php function which calls this and returns the result as resource which can be executed later. */
  541. v8::Local<v8::String> source = v8::String::New(str, str_len);
  542. v8::Local<v8::Script> script = v8::Script::New(source, sname);
  543. /* Compile errors? */
  544. if (script.IsEmpty()) {
  545. php_v8js_throw_exception(&try_catch TSRMLS_CC);
  546. return;
  547. }
  548. /* Set flags for runtime use */
  549. V8JS_GLOBAL_SET_FLAGS(flags);
  550. /* Execute script */
  551. c->in_execution++;
  552. v8::Local<v8::Value> result = script->Run();
  553. c->in_execution--;
  554. /* Script possibly terminated, return immediately */
  555. if (!try_catch.CanContinue()) {
  556. /* TODO: throw PHP exception here? */
  557. return;
  558. }
  559. /* There was pending exception left from earlier executions -> throw to PHP */
  560. if (c->pending_exception) {
  561. zend_throw_exception_object(c->pending_exception TSRMLS_CC);
  562. c->pending_exception = NULL;
  563. }
  564. /* Handle runtime JS exceptions */
  565. if (try_catch.HasCaught()) {
  566. /* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
  567. if (c->in_execution < 1) {
  568. /* Report immediately if report_uncaught is true */
  569. if (c->report_uncaught) {
  570. php_v8js_throw_exception(&try_catch TSRMLS_CC);
  571. return;
  572. }
  573. /* Exception thrown from JS, preserve it for future execution */
  574. if (result.IsEmpty()) {
  575. MAKE_STD_ZVAL(c->pending_exception);
  576. php_v8js_create_exception(c->pending_exception, &try_catch TSRMLS_CC);
  577. }
  578. }
  579. /* Rethrow back to JS */
  580. try_catch.ReThrow();
  581. return;
  582. }
  583. /* Convert V8 value to PHP value */
  584. if (!result.IsEmpty()) {
  585. v8js_to_zval(result, return_value, flags TSRMLS_CC);
  586. }
  587. }
  588. /* }}} */
  589. /* {{{ proto mixed V8Js::getPendingException()
  590. */
  591. static PHP_METHOD(V8Js, getPendingException)
  592. {
  593. php_v8js_ctx *c;
  594. if (zend_parse_parameters_none() == FAILURE) {
  595. return;
  596. }
  597. c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  598. if (c->pending_exception) {
  599. RETURN_ZVAL(c->pending_exception, 1, 0);
  600. }
  601. }
  602. /* }}} */
  603. static void php_v8js_persistent_zval_ctor(zval **p) /* {{{ */
  604. {
  605. zval *orig_ptr = *p;
  606. *p = (zval *) malloc(sizeof(zval));
  607. **p = *orig_ptr;
  608. switch (Z_TYPE_P(*p)) {
  609. case IS_STRING:
  610. Z_STRVAL_P(*p) = (char *) zend_strndup(Z_STRVAL_P(*p), Z_STRLEN_P(*p));
  611. break;
  612. default:
  613. zend_bailout();
  614. }
  615. INIT_PZVAL(*p);
  616. }
  617. /* }}} */
  618. static void php_v8js_persistent_zval_dtor(zval **p) /* {{{ */
  619. {
  620. switch (Z_TYPE_P(*p)) {
  621. case IS_STRING:
  622. free(Z_STRVAL_P(*p));
  623. break;
  624. default:
  625. zend_bailout();
  626. }
  627. free(*p);
  628. }
  629. /* }}} */
  630. 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) /* {{{ */
  631. {
  632. php_v8js_jsext *jsext = NULL;
  633. if (!V8JSG(extensions)) {
  634. V8JSG(extensions) = (HashTable *) malloc(sizeof(HashTable));
  635. zend_hash_init(V8JSG(extensions), 1, NULL, (dtor_func_t) php_v8js_jsext_dtor, 1);
  636. } else if (zend_hash_exists(V8JSG(extensions), name, name_len + 1)) {
  637. return FAILURE;
  638. }
  639. jsext = (php_v8js_jsext *) calloc(1, sizeof(php_v8js_jsext));
  640. if (deps_arr) {
  641. jsext->deps_count = zend_hash_num_elements(Z_ARRVAL_P(deps_arr));
  642. if (_php_v8js_create_ext_strarr(&jsext->deps, jsext->deps_count, Z_ARRVAL_P(deps_arr)) == FAILURE) {
  643. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid dependancy array passed");
  644. php_v8js_jsext_dtor(jsext);
  645. free(jsext);
  646. return FAILURE;
  647. }
  648. }
  649. jsext->auto_enable = auto_enable;
  650. jsext->name = zend_strndup(name, name_len);
  651. jsext->source = zend_strndup(source, source_len);
  652. if (jsext->deps) {
  653. jsext->deps_ht = (HashTable *) malloc(sizeof(HashTable));
  654. zend_hash_init(jsext->deps_ht, jsext->deps_count, NULL, (dtor_func_t) php_v8js_persistent_zval_dtor, 1);
  655. zend_hash_copy(jsext->deps_ht, Z_ARRVAL_P(deps_arr), (copy_ctor_func_t) php_v8js_persistent_zval_ctor, NULL, sizeof(zval *));
  656. }
  657. if (zend_hash_add(V8JSG(extensions), name, name_len + 1, jsext, sizeof(php_v8js_jsext), NULL) == FAILURE) {
  658. php_v8js_jsext_dtor(jsext);
  659. free(jsext);
  660. return FAILURE;
  661. }
  662. v8::Extension *extension = new v8::Extension(jsext->name, jsext->source, jsext->deps_count, jsext->deps);
  663. extension->set_auto_enable(auto_enable ? true : false);
  664. v8::RegisterExtension(extension);
  665. free(jsext);
  666. return SUCCESS;
  667. }
  668. /* }}} */
  669. /* {{{ proto bool V8Js::registerExtension(string ext_name, string script [, array deps [, bool auto_enable]])
  670. */
  671. static PHP_METHOD(V8Js, registerExtension)
  672. {
  673. char *ext_name, *script;
  674. zval *deps_arr = NULL;
  675. int ext_name_len, script_len;
  676. zend_bool auto_enable = 0;
  677. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ab", &ext_name, &ext_name_len, &script, &script_len, &deps_arr, &auto_enable) == FAILURE) {
  678. return;
  679. }
  680. if (!ext_name_len) {
  681. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Extension name cannot be empty");
  682. } else if (!script_len) {
  683. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Script cannot be empty");
  684. } else if (php_v8js_register_extension(ext_name, ext_name_len, script, script_len, deps_arr, auto_enable TSRMLS_CC) == SUCCESS) {
  685. RETURN_TRUE;
  686. }
  687. RETURN_FALSE;
  688. }
  689. /* }}} */
  690. /* ## Static methods ## */
  691. /* {{{ proto array V8Js::getExtensions()
  692. */
  693. static PHP_METHOD(V8Js, getExtensions)
  694. {
  695. php_v8js_jsext *jsext;
  696. zval *ext, *deps_arr;
  697. HashPosition pos;
  698. ulong index;
  699. char *key;
  700. uint key_len;
  701. if (zend_parse_parameters_none() == FAILURE) {
  702. return;
  703. }
  704. array_init(return_value);
  705. if (V8JSG(extensions)) {
  706. zend_hash_internal_pointer_reset_ex(V8JSG(extensions), &pos);
  707. while (zend_hash_get_current_data_ex(V8JSG(extensions), (void **) &jsext, &pos) == SUCCESS) {
  708. if (zend_hash_get_current_key_ex(V8JSG(extensions), &key, &key_len, &index, 0, &pos) == HASH_KEY_IS_STRING) {
  709. MAKE_STD_ZVAL(ext)
  710. array_init(ext);
  711. add_assoc_bool_ex(ext, ZEND_STRS("auto_enable"), jsext->auto_enable);
  712. if (jsext->deps_ht) {
  713. MAKE_STD_ZVAL(deps_arr);
  714. array_init(deps_arr);
  715. zend_hash_copy(Z_ARRVAL_P(deps_arr), jsext->deps_ht, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
  716. add_assoc_zval_ex(ext, ZEND_STRS("deps"), deps_arr);
  717. }
  718. add_assoc_zval_ex(return_value, key, key_len, ext);
  719. }
  720. zend_hash_move_forward_ex(V8JSG(extensions), &pos);
  721. }
  722. }
  723. }
  724. /* }}} */
  725. /* {{{ arginfo */
  726. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0)
  727. ZEND_ARG_INFO(0, object_name)
  728. ZEND_ARG_INFO(0, variables)
  729. ZEND_ARG_INFO(0, extensions)
  730. ZEND_ARG_INFO(0, report_uncaught_exceptions)
  731. ZEND_END_ARG_INFO()
  732. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1)
  733. ZEND_ARG_INFO(0, script)
  734. ZEND_ARG_INFO(0, identifier)
  735. ZEND_ARG_INFO(0, flags)
  736. ZEND_END_ARG_INFO()
  737. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getpendingexception, 0)
  738. ZEND_END_ARG_INFO()
  739. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_registerextension, 0, 0, 2)
  740. ZEND_ARG_INFO(0, extension_name)
  741. ZEND_ARG_INFO(0, script)
  742. ZEND_ARG_INFO(0, dependencies)
  743. ZEND_ARG_INFO(0, auto_enable)
  744. ZEND_END_ARG_INFO()
  745. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0)
  746. ZEND_END_ARG_INFO()
  747. ZEND_BEGIN_ARG_INFO(arginfo_v8jsexception_no_args, 0)
  748. ZEND_END_ARG_INFO()
  749. /* }}} */
  750. static const zend_function_entry v8js_methods[] = { /* {{{ */
  751. PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  752. PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC)
  753. PHP_ME(V8Js, getPendingException, arginfo_v8js_getpendingexception, ZEND_ACC_PUBLIC)
  754. PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  755. PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  756. {NULL, NULL, NULL}
  757. };
  758. /* }}} */
  759. /* V8Js object handlers */
  760. static void php_v8js_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC) /* {{{ */
  761. {
  762. V8JS_BEGIN_CTX(c, object)
  763. v8::HandleScope handle_scope;
  764. /* Global PHP JS object */
  765. v8::Local<v8::Object> jsobj = V8JS_GLOBAL->Get(c->object_name)->ToObject();
  766. /* Write value to PHP JS object */
  767. jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value TSRMLS_CC), v8::ReadOnly);
  768. /* Write value to PHP object */
  769. std_object_handlers.write_property(object, member, value, key TSRMLS_CC);
  770. }
  771. /* }}} */
  772. static void php_v8js_unset_property(zval *object, zval *member, const zend_literal *key TSRMLS_DC) /* {{{ */
  773. {
  774. V8JS_BEGIN_CTX(c, object)
  775. v8::HandleScope handle_scope;
  776. /* Global PHP JS object */
  777. v8::Local<v8::Object> jsobj = V8JS_GLOBAL->Get(c->object_name)->ToObject();
  778. /* Delete value from PHP JS object */
  779. jsobj->ForceDelete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
  780. /* Unset from PHP object */
  781. std_object_handlers.unset_property(object, member, key TSRMLS_CC);
  782. }
  783. /* }}} */
  784. /* }}} V8Js */
  785. /* {{{ Class: V8JsException */
  786. static void php_v8js_create_exception(zval *return_value, v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  787. {
  788. v8::String::Utf8Value exception(try_catch->Exception());
  789. const char *exception_string = ToCString(exception);
  790. v8::Handle<v8::Message> tc_message = try_catch->Message();
  791. const char *filename_string, *sourceline_string;
  792. char *message_string;
  793. int linenum, message_len;
  794. object_init_ex(return_value, php_ce_v8js_exception);
  795. #define PHPV8_EXPROP(type, name, value) \
  796. zend_update_property##type(php_ce_v8js_exception, return_value, #name, sizeof(#name) - 1, value TSRMLS_CC);
  797. if (tc_message.IsEmpty()) {
  798. message_len = spprintf(&message_string, 0, "%s", exception_string);
  799. }
  800. else
  801. {
  802. v8::String::Utf8Value filename(tc_message->GetScriptResourceName());
  803. filename_string = ToCString(filename);
  804. PHPV8_EXPROP(_string, JsFileName, filename_string);
  805. v8::String::Utf8Value sourceline(tc_message->GetSourceLine());
  806. sourceline_string = ToCString(sourceline);
  807. PHPV8_EXPROP(_string, JsSourceLine, sourceline_string);
  808. linenum = tc_message->GetLineNumber();
  809. PHPV8_EXPROP(_long, JsLineNumber, linenum);
  810. message_len = spprintf(&message_string, 0, "%s:%d: %s", filename_string, linenum, exception_string);
  811. v8::String::Utf8Value stacktrace(try_catch->StackTrace());
  812. if (stacktrace.length() > 0) {
  813. const char* stacktrace_string = ToCString(stacktrace);
  814. PHPV8_EXPROP(_string, JsTrace, stacktrace_string);
  815. }
  816. }
  817. PHPV8_EXPROP(_string, message, message_string);
  818. efree(message_string);
  819. }
  820. /* }}} */
  821. static void php_v8js_throw_exception(v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  822. {
  823. v8::String::Utf8Value exception(try_catch->Exception());
  824. const char *exception_string = ToCString(exception);
  825. zval *zexception = NULL;
  826. if (try_catch->Message().IsEmpty()) {
  827. zend_throw_exception(php_ce_v8js_exception, (char *) exception_string, 0 TSRMLS_CC);
  828. } else {
  829. MAKE_STD_ZVAL(zexception);
  830. php_v8js_create_exception(zexception, try_catch TSRMLS_CC);
  831. zend_throw_exception_object(zexception TSRMLS_CC);
  832. }
  833. }
  834. /* }}} */
  835. #define V8JS_EXCEPTION_METHOD(property) \
  836. static PHP_METHOD(V8JsException, get##property) \
  837. { \
  838. zval *value; \
  839. \
  840. if (zend_parse_parameters_none() == FAILURE) { \
  841. return; \
  842. } \
  843. value = zend_read_property(php_ce_v8js_exception, getThis(), #property, sizeof(#property) - 1, 0 TSRMLS_CC); \
  844. *return_value = *value; \
  845. zval_copy_ctor(return_value); \
  846. INIT_PZVAL(return_value); \
  847. }
  848. /* {{{ proto string V8JsException::getJsFileName()
  849. */
  850. V8JS_EXCEPTION_METHOD(JsFileName);
  851. /* }}} */
  852. /* {{{ proto string V8JsException::getJsLineNumber()
  853. */
  854. V8JS_EXCEPTION_METHOD(JsLineNumber);
  855. /* }}} */
  856. /* {{{ proto string V8JsException::getJsSourceLine()
  857. */
  858. V8JS_EXCEPTION_METHOD(JsSourceLine);
  859. /* }}} */
  860. /* {{{ proto string V8JsException::getJsTrace()
  861. */
  862. V8JS_EXCEPTION_METHOD(JsTrace);
  863. /* }}} */
  864. static const zend_function_entry v8js_exception_methods[] = { /* {{{ */
  865. PHP_ME(V8JsException, getJsFileName, arginfo_v8jsexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  866. PHP_ME(V8JsException, getJsLineNumber, arginfo_v8jsexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  867. PHP_ME(V8JsException, getJsSourceLine, arginfo_v8jsexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  868. PHP_ME(V8JsException, getJsTrace, arginfo_v8jsexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  869. {NULL, NULL, NULL}
  870. };
  871. /* }}} */
  872. /* }}} V8JsException */
  873. /* {{{ PHP_MINIT_FUNCTION
  874. */
  875. static PHP_MINIT_FUNCTION(v8js)
  876. {
  877. zend_class_entry ce;
  878. /* V8Object Class */
  879. INIT_CLASS_ENTRY(ce, "V8Object", NULL);
  880. php_ce_v8_object = zend_register_internal_class(&ce TSRMLS_CC);
  881. php_ce_v8_object->ce_flags |= ZEND_ACC_FINAL;
  882. php_ce_v8_object->create_object = php_v8js_v8_new;
  883. /* V8Function Class */
  884. INIT_CLASS_ENTRY(ce, "V8Function", NULL);
  885. php_ce_v8_function = zend_register_internal_class(&ce TSRMLS_CC);
  886. php_ce_v8_function->ce_flags |= ZEND_ACC_FINAL;
  887. php_ce_v8_function->create_object = php_v8js_v8_new;
  888. /* V8<Object|Function> handlers */
  889. memcpy(&v8_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  890. v8_object_handlers.clone_obj = NULL;
  891. v8_object_handlers.cast_object = NULL;
  892. v8_object_handlers.get_constructor = NULL;
  893. v8_object_handlers.get_property_ptr_ptr = NULL;
  894. // v8_object_handlers.has_property = php_v8js_v8_has_property; // Not implemented yet
  895. v8_object_handlers.read_property = php_v8js_v8_read_property;
  896. v8_object_handlers.write_property = php_v8js_v8_write_property;
  897. v8_object_handlers.unset_property = php_v8js_v8_unset_property;
  898. v8_object_handlers.get_properties = php_v8js_v8_get_properties;
  899. v8_object_handlers.get_method = php_v8js_v8_get_method;
  900. v8_object_handlers.call_method = php_v8js_v8_call_method;
  901. v8_object_handlers.get_debug_info = php_v8js_v8_get_debug_info;
  902. v8_object_handlers.get_closure = php_v8js_v8_get_closure;
  903. /* V8Js Class */
  904. INIT_CLASS_ENTRY(ce, "V8Js", v8js_methods);
  905. php_ce_v8js = zend_register_internal_class(&ce TSRMLS_CC);
  906. php_ce_v8js->create_object = php_v8js_new;
  907. /* V8Js handlers */
  908. memcpy(&v8js_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  909. v8js_object_handlers.clone_obj = NULL;
  910. v8js_object_handlers.write_property = php_v8js_write_property;
  911. v8js_object_handlers.unset_property = php_v8js_unset_property;
  912. /* V8Js Class Constants */
  913. zend_declare_class_constant_string(php_ce_v8js, ZEND_STRL("V8_VERSION"), PHP_V8_VERSION TSRMLS_CC);
  914. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE TSRMLS_CC);
  915. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY TSRMLS_CC);
  916. /* V8JsException Class */
  917. INIT_CLASS_ENTRY(ce, "V8JsException", v8js_exception_methods);
  918. php_ce_v8js_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
  919. php_ce_v8js_exception->ce_flags |= ZEND_ACC_FINAL;
  920. /* Add custom JS specific properties */
  921. zend_declare_property_null(php_ce_v8js_exception, ZEND_STRL("JsFileName"), ZEND_ACC_PROTECTED TSRMLS_CC);
  922. zend_declare_property_null(php_ce_v8js_exception, ZEND_STRL("JsLineNumber"), ZEND_ACC_PROTECTED TSRMLS_CC);
  923. zend_declare_property_null(php_ce_v8js_exception, ZEND_STRL("JsSourceLine"), ZEND_ACC_PROTECTED TSRMLS_CC);
  924. zend_declare_property_null(php_ce_v8js_exception, ZEND_STRL("JsTrace"), ZEND_ACC_PROTECTED TSRMLS_CC);
  925. REGISTER_INI_ENTRIES();
  926. return SUCCESS;
  927. }
  928. /* }}} */
  929. static void php_v8js_force_v8_gc(void) /* {{{ */
  930. {
  931. #if V8JS_DEBUG
  932. TSRMLS_FETCH();
  933. fprintf(stderr, "############ Running V8 Idle notification: disposed/max_disposed %d/%d ############\n", V8JSG(disposed_contexts), V8JSG(max_disposed_contexts));
  934. fflush(stderr);
  935. #endif
  936. while (!v8::V8::IdleNotification()) {
  937. #if V8JS_DEBUG
  938. fprintf(stderr, ".");
  939. fflush(stderr);
  940. #endif
  941. }
  942. #if V8JS_DEBUG
  943. fprintf(stderr, "\n");
  944. fflush(stderr);
  945. #endif
  946. }
  947. /* }}} */
  948. /* {{{ PHP_MSHUTDOWN_FUNCTION
  949. */
  950. static PHP_MSHUTDOWN_FUNCTION(v8js)
  951. {
  952. UNREGISTER_INI_ENTRIES();
  953. v8::V8::Dispose();
  954. if (V8JSG(extensions)) {
  955. zend_hash_destroy(V8JSG(extensions));
  956. free(V8JSG(extensions));
  957. V8JSG(extensions) = NULL;
  958. }
  959. if (V8JSG(v8_flags)) {
  960. free(V8JSG(v8_flags));
  961. V8JSG(v8_flags) = NULL;
  962. }
  963. return SUCCESS;
  964. }
  965. /* }}} */
  966. /* {{{ PHP_RSHUTDOWN_FUNCTION
  967. */
  968. static PHP_RSHUTDOWN_FUNCTION(v8js)
  969. {
  970. #if V8JS_DEBUG
  971. v8::HeapStatistics stats;
  972. v8::V8::GetHeapStatistics(&stats);
  973. float used = stats.used_heap_size() / 1024.0 / 1024.0;
  974. float total = stats.total_heap_size() / 1024.0 / 1024.0;
  975. fprintf(stderr, "### RSHUTDOWN ###\n");
  976. fprintf(stderr, "############ Heap Used/Total %.2f/%.2f MB ############\n", used, total);
  977. fflush(stderr);
  978. #endif
  979. /* Force V8 to do as much GC as possible */
  980. if (V8JSG(max_disposed_contexts) && (V8JSG(disposed_contexts) > V8JSG(max_disposed_contexts))) {
  981. php_v8js_force_v8_gc();
  982. }
  983. return SUCCESS;
  984. }
  985. /* }}} */
  986. /* {{{ PHP_MINFO_FUNCTION
  987. */
  988. static PHP_MINFO_FUNCTION(v8js)
  989. {
  990. php_info_print_table_start();
  991. php_info_print_table_header(2, "V8 Javascript Engine", "enabled");
  992. php_info_print_table_header(2, "V8 Engine Compiled Version", PHP_V8_VERSION);
  993. php_info_print_table_header(2, "V8 Engine Linked Version", v8::V8::GetVersion());
  994. php_info_print_table_header(2, "Version", V8JS_VERSION);
  995. php_info_print_table_end();
  996. DISPLAY_INI_ENTRIES();
  997. }
  998. /* }}} */
  999. /* {{{ PHP_GINIT_FUNCTION
  1000. */
  1001. static PHP_GINIT_FUNCTION(v8js)
  1002. {
  1003. v8js_globals->extensions = NULL;
  1004. v8js_globals->disposed_contexts = 0;
  1005. v8js_globals->v8_initialized = 0;
  1006. v8js_globals->v8_flags = NULL;
  1007. }
  1008. /* }}} */
  1009. /* {{{ v8js_functions[] */
  1010. static const zend_function_entry v8js_functions[] = {
  1011. {NULL, NULL, NULL}
  1012. };
  1013. /* }}} */
  1014. /* {{{ v8js_module_entry
  1015. */
  1016. zend_module_entry v8js_module_entry = {
  1017. STANDARD_MODULE_HEADER_EX,
  1018. NULL,
  1019. NULL,
  1020. "v8js",
  1021. v8js_functions,
  1022. PHP_MINIT(v8js),
  1023. PHP_MSHUTDOWN(v8js),
  1024. NULL,
  1025. PHP_RSHUTDOWN(v8js),
  1026. PHP_MINFO(v8js),
  1027. V8JS_VERSION,
  1028. PHP_MODULE_GLOBALS(v8js),
  1029. PHP_GINIT(v8js),
  1030. NULL,
  1031. NULL,
  1032. STANDARD_MODULE_PROPERTIES_EX
  1033. };
  1034. /* }}} */
  1035. /*
  1036. * Local variables:
  1037. * tab-width: 4
  1038. * c-basic-offset: 4
  1039. * End:
  1040. * vim600: noet sw=4 ts=4 fdm=marker
  1041. * vim<600: noet sw=4 ts=4
  1042. */