v8js.cc 36 KB

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