v8js.cc 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2013 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | http://www.opensource.org/licenses/mit-license.php MIT License |
  8. +----------------------------------------------------------------------+
  9. | Author: Jani Taskinen <[email protected]> |
  10. | Author: Patrick Reilly <[email protected]> |
  11. +----------------------------------------------------------------------+
  12. */
  13. /* $Id$ */
  14. #define V8JS_DEBUG 0
  15. //#define PHP_V8_VERSION "0.1.4"
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include <v8-debug.h>
  20. #include "php_v8js_macros.h"
  21. extern "C" {
  22. #include "php_ini.h"
  23. #include "ext/standard/info.h"
  24. #include "ext/standard/php_string.h"
  25. #include "ext/standard/php_smart_str.h"
  26. #include "zend_exceptions.h"
  27. }
  28. /* Forward declarations */
  29. static void php_v8js_throw_script_exception(v8::TryCatch * TSRMLS_DC);
  30. static void php_v8js_create_script_exception(zval *, v8::TryCatch * TSRMLS_DC);
  31. ZEND_DECLARE_MODULE_GLOBALS(v8js)
  32. int le_v8js_script;
  33. #define PHP_V8JS_SCRIPT_RES_NAME "V8Js script"
  34. /* {{{ INI Settings */
  35. static ZEND_INI_MH(v8js_OnUpdateV8Flags) /* {{{ */
  36. {
  37. if (new_value) {
  38. if (V8JSG(v8_flags)) {
  39. free(V8JSG(v8_flags));
  40. V8JSG(v8_flags) = NULL;
  41. }
  42. if (!new_value[0]) {
  43. return FAILURE;
  44. }
  45. V8JSG(v8_flags) = zend_strndup(new_value, new_value_length);
  46. }
  47. return SUCCESS;
  48. }
  49. static ZEND_INI_MH(v8js_OnUpdateUseDate) /* {{{ */
  50. {
  51. bool value;
  52. if (new_value_length==2 && strcasecmp("on", new_value)==0) {
  53. value = (bool) 1;
  54. } else if (new_value_length==3 && strcasecmp("yes", new_value)==0) {
  55. value = (bool) 1;
  56. } else if (new_value_length==4 && strcasecmp("true", new_value)==0) {
  57. value = (bool) 1;
  58. } else {
  59. value = (bool) atoi(new_value);
  60. }
  61. V8JSG(use_date) = value;
  62. return SUCCESS;
  63. }
  64. /* }}} */
  65. ZEND_INI_BEGIN() /* {{{ */
  66. ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
  67. ZEND_INI_ENTRY("v8js.use_date", "0", ZEND_INI_ALL, v8js_OnUpdateUseDate)
  68. ZEND_INI_END()
  69. /* }}} */
  70. /* }}} INI */
  71. /* {{{ Class Entries */
  72. zend_class_entry *php_ce_v8_object;
  73. zend_class_entry *php_ce_v8_function;
  74. static zend_class_entry *php_ce_v8js;
  75. static zend_class_entry *php_ce_v8js_script_exception;
  76. static zend_class_entry *php_ce_v8js_time_limit_exception;
  77. static zend_class_entry *php_ce_v8js_memory_limit_exception;
  78. /* }}} */
  79. /* {{{ Object Handlers */
  80. static zend_object_handlers v8js_object_handlers;
  81. static zend_object_handlers v8_object_handlers;
  82. /* }}} */
  83. /* {{{ Extension container */
  84. struct php_v8js_jsext {
  85. zend_bool auto_enable;
  86. HashTable *deps_ht;
  87. const char **deps;
  88. int deps_count;
  89. char *name;
  90. char *source;
  91. };
  92. /* }}} */
  93. #ifdef ENABLE_DEBUGGER_SUPPORT
  94. static php_v8js_ctx *v8js_debug_context;
  95. static int v8js_debug_auto_break_mode;
  96. #endif
  97. #ifdef COMPILE_DL_V8JS
  98. ZEND_GET_MODULE(v8js)
  99. #endif
  100. /* {{{ Class: V8 */
  101. #define V8JS_V8_INVOKE_FUNC_NAME "V8Js::V8::Invoke"
  102. /* V8 Object handlers */
  103. static int php_v8js_v8_has_property(zval *object, zval *member, int has_set_exists ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  104. {
  105. /* param has_set_exists:
  106. * 0 (has) whether property exists and is not NULL - isset()
  107. * 1 (set) whether property exists and is true-ish - empty()
  108. * 2 (exists) whether property exists - property_exists()
  109. */
  110. int retval = false;
  111. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  112. if (!obj->ctx) {
  113. zend_throw_exception(php_ce_v8js_script_exception,
  114. "Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
  115. return retval;
  116. }
  117. v8::Isolate *isolate = obj->ctx->isolate;
  118. v8::Locker locker(isolate);
  119. v8::Isolate::Scope isolate_scope(isolate);
  120. v8::HandleScope local_scope(isolate);
  121. v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
  122. v8::Context::Scope temp_scope(temp_context);
  123. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  124. if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction())
  125. {
  126. v8::Local<v8::Object> jsObj = v8obj->ToObject();
  127. v8::Local<v8::String> jsKey = V8JS_STRL(Z_STRVAL_P(member), Z_STRLEN_P(member));
  128. v8::Local<v8::Value> jsVal;
  129. /* Skip any prototype properties */
  130. if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
  131. if (has_set_exists == 2) {
  132. /* property_exists(), that's enough! */
  133. retval = true;
  134. } else {
  135. /* We need to look at the value. */
  136. jsVal = jsObj->Get(jsKey);
  137. if (has_set_exists == 0 ) {
  138. /* isset(): We make 'undefined' equivalent to 'null' */
  139. retval = !( jsVal->IsNull() || jsVal->IsUndefined() );
  140. } else {
  141. /* empty() */
  142. retval = jsVal->BooleanValue();
  143. /* for PHP compatibility, [] should also be empty */
  144. if (jsVal->IsArray() && retval) {
  145. v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(jsVal);
  146. retval = (array->Length() != 0);
  147. }
  148. /* for PHP compatibility, '0' should also be empty */
  149. if (jsVal->IsString() && retval) {
  150. v8::Local<v8::String> str = jsVal->ToString();
  151. if (str->Length() == 1) {
  152. uint16_t c = 0;
  153. str->Write(&c, 0, 1);
  154. if (c == '0') {
  155. retval = false;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. return retval;
  164. }
  165. /* }}} */
  166. static zval *php_v8js_v8_read_property(zval *object, zval *member, int type ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  167. {
  168. zval *retval = NULL;
  169. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  170. if (!obj->ctx) {
  171. zend_throw_exception(php_ce_v8js_script_exception,
  172. "Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
  173. return retval;
  174. }
  175. v8::Isolate *isolate = obj->ctx->isolate;
  176. v8::Locker locker(isolate);
  177. v8::Isolate::Scope isolate_scope(isolate);
  178. v8::HandleScope local_scope(isolate);
  179. v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
  180. v8::Context::Scope temp_scope(temp_context);
  181. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  182. if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction())
  183. {
  184. v8::Local<v8::Object> jsObj = v8obj->ToObject();
  185. v8::Local<v8::String> jsKey = V8JS_STRL(Z_STRVAL_P(member), Z_STRLEN_P(member));
  186. v8::Local<v8::Value> jsVal;
  187. /* Skip any prototype properties */
  188. if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
  189. jsVal = jsObj->Get(jsKey);
  190. if (jsVal->IsObject()) {
  191. ALLOC_INIT_ZVAL(retval);
  192. Z_SET_REFCOUNT_P(retval, 0);
  193. } else {
  194. MAKE_STD_ZVAL(retval);
  195. }
  196. if (v8js_to_zval(jsVal, retval, obj->flags, isolate TSRMLS_CC) == SUCCESS) {
  197. return retval;
  198. }
  199. }
  200. }
  201. ALLOC_INIT_ZVAL(retval);
  202. return retval;
  203. }
  204. /* }}} */
  205. static void php_v8js_v8_write_property(zval *object, zval *member, zval *value ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  206. {
  207. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  208. if (!obj->ctx) {
  209. zend_throw_exception(php_ce_v8js_script_exception,
  210. "Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
  211. return;
  212. }
  213. v8::Isolate *isolate = obj->ctx->isolate;
  214. v8::Locker locker(isolate);
  215. v8::Isolate::Scope isolate_scope(isolate);
  216. v8::HandleScope local_scope(isolate);
  217. v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
  218. v8::Context::Scope temp_scope(temp_context);
  219. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  220. if (v8obj->IsObject() && !v8obj->IsFunction()) {
  221. v8obj->ToObject()->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, isolate TSRMLS_CC));
  222. }
  223. }
  224. /* }}} */
  225. static void php_v8js_v8_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  226. {
  227. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  228. if (!obj->ctx) {
  229. zend_throw_exception(php_ce_v8js_script_exception,
  230. "Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
  231. return;
  232. }
  233. v8::Isolate *isolate = obj->ctx->isolate;
  234. v8::Locker locker(isolate);
  235. v8::Isolate::Scope isolate_scope(isolate);
  236. v8::HandleScope local_scope(isolate);
  237. v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
  238. v8::Context::Scope temp_scope(temp_context);
  239. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  240. if (v8obj->IsObject() && !v8obj->IsFunction()) {
  241. v8obj->ToObject()->ForceDelete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
  242. }
  243. }
  244. /* }}} */
  245. int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  246. {
  247. v8::Local<v8::Object> jsObj = jsValue->ToObject();
  248. if (!jsObj.IsEmpty()) {
  249. v8::Local<v8::Array> jsKeys = jsObj->GetPropertyNames();
  250. for (unsigned i = 0; i < jsKeys->Length(); i++)
  251. {
  252. v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString();
  253. /* Skip any prototype properties */
  254. if (!jsObj->HasOwnProperty(jsKey) && !jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey)) {
  255. continue;
  256. }
  257. v8::Local<v8::Value> jsVal = jsObj->Get(jsKey);
  258. v8::String::Utf8Value cstr(jsKey);
  259. const char *key = ToCString(cstr);
  260. zval *value = NULL;
  261. v8::Local<v8::Value> php_object;
  262. if (jsVal->IsObject()) {
  263. php_object = v8::Local<v8::Object>::Cast(jsVal)->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  264. }
  265. if (!php_object.IsEmpty()) {
  266. /* This is a PHP object, passed to JS and back. */
  267. value = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  268. Z_ADDREF_P(value);
  269. }
  270. else {
  271. MAKE_STD_ZVAL(value);
  272. if (v8js_to_zval(jsVal, value, flags, isolate TSRMLS_CC) == FAILURE) {
  273. zval_ptr_dtor(&value);
  274. return FAILURE;
  275. }
  276. }
  277. if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
  278. zend_symtable_update(retval, key, strlen(key) + 1, (void *)&value, sizeof(zval *), NULL);
  279. } else {
  280. zend_hash_update(retval, key, strlen(key) + 1, (void *) &value, sizeof(zval *), NULL);
  281. }
  282. }
  283. return SUCCESS;
  284. }
  285. return FAILURE;
  286. }
  287. /* }}} */
  288. static HashTable *php_v8js_v8_get_properties(zval *object TSRMLS_DC) /* {{{ */
  289. {
  290. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  291. HashTable *retval;
  292. if (obj->properties == NULL) {
  293. if (GC_G(gc_active)) {
  294. /* the garbage collector is running, don't create more zvals */
  295. return NULL;
  296. }
  297. if (obj->ctx == NULL) {
  298. /* Half-constructed object. Shouldn't happen, but be safe. */
  299. return NULL;
  300. }
  301. ALLOC_HASHTABLE(obj->properties);
  302. zend_hash_init(obj->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  303. } else {
  304. zend_hash_clean(obj->properties);
  305. }
  306. if (!obj->ctx) {
  307. zend_throw_exception(php_ce_v8js_script_exception,
  308. "Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
  309. return NULL;
  310. }
  311. v8::Isolate *isolate = obj->ctx->isolate;
  312. v8::Locker locker(isolate);
  313. v8::Isolate::Scope isolate_scope(isolate);
  314. v8::HandleScope local_scope(isolate);
  315. v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
  316. v8::Context::Scope temp_scope(temp_context);
  317. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  318. if (php_v8js_v8_get_properties_hash(v8obj, obj->properties, obj->flags, isolate TSRMLS_CC) == SUCCESS) {
  319. return obj->properties;
  320. }
  321. return NULL;
  322. }
  323. /* }}} */
  324. static HashTable *php_v8js_v8_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
  325. {
  326. *is_temp = 0;
  327. return php_v8js_v8_get_properties(object TSRMLS_CC);
  328. }
  329. /* }}} */
  330. static zend_function *php_v8js_v8_get_method(zval **object_ptr, char *method, int method_len ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  331. {
  332. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(*object_ptr TSRMLS_CC);
  333. zend_function *f;
  334. if (!obj->ctx) {
  335. zend_throw_exception(php_ce_v8js_script_exception,
  336. "Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
  337. return NULL;
  338. }
  339. v8::Isolate *isolate = obj->ctx->isolate;
  340. v8::Locker locker(isolate);
  341. v8::Isolate::Scope isolate_scope(isolate);
  342. v8::HandleScope local_scope(isolate);
  343. v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
  344. v8::Context::Scope temp_scope(temp_context);
  345. v8::Local<v8::String> jsKey = V8JS_STRL(method, method_len);
  346. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  347. if (!obj->v8obj.IsEmpty() && v8obj->IsObject() && !v8obj->IsFunction()) {
  348. v8::Local<v8::Object> jsObj = v8obj->ToObject();
  349. if (jsObj->Has(jsKey) && jsObj->Get(jsKey)->IsFunction()) {
  350. f = (zend_function *) ecalloc(1, sizeof(*f));
  351. f->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
  352. f->common.function_name = estrndup(method, method_len);
  353. return f;
  354. }
  355. }
  356. return NULL;
  357. }
  358. /* }}} */
  359. #if PHP_VERSION_ID >= 50400
  360. static int php_v8js_v8_call_method(const char *method, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  361. #else
  362. static int php_v8js_v8_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  363. #endif
  364. {
  365. zval *object = this_ptr, ***argv = NULL;
  366. int i = 0, argc = ZEND_NUM_ARGS();
  367. php_v8js_object *obj;
  368. obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  369. if (!obj->ctx) {
  370. zend_throw_exception(php_ce_v8js_script_exception,
  371. "Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
  372. return FAILURE;
  373. }
  374. if (obj->v8obj.IsEmpty()) {
  375. zval_ptr_dtor(&object);
  376. return FAILURE;
  377. }
  378. if (argc > 0) {
  379. argv = (zval***)safe_emalloc(sizeof(zval**), argc, 0);
  380. zend_get_parameters_array_ex(argc, argv);
  381. }
  382. v8::Isolate *isolate = obj->ctx->isolate;
  383. v8::Locker locker(isolate);
  384. v8::Isolate::Scope isolate_scope(isolate);
  385. v8::HandleScope local_scope(isolate);
  386. v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
  387. v8::Context::Scope temp_scope(temp_context);
  388. v8::Local<v8::String> method_name = V8JS_SYML(method, strlen(method));
  389. v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj)->ToObject();
  390. v8::Local<v8::Function> cb;
  391. if (method_name->Equals(V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME))) {
  392. cb = v8::Local<v8::Function>::Cast(v8obj);
  393. } else {
  394. cb = v8::Local<v8::Function>::Cast(v8obj->Get(method_name));
  395. }
  396. v8::Local<v8::Value> jsArgv[argc];
  397. v8::Local<v8::Value> js_retval;
  398. for (i = 0; i < argc; i++) {
  399. jsArgv[i] = v8::Local<v8::Value>::New(isolate, zval_to_v8js(*argv[i], isolate TSRMLS_CC));
  400. }
  401. js_retval = cb->Call(V8JS_GLOBAL(isolate), argc, jsArgv);
  402. zval_ptr_dtor(&object);
  403. if (argc > 0) {
  404. efree(argv);
  405. }
  406. if (return_value_used) {
  407. return v8js_to_zval(js_retval, return_value, obj->flags, isolate TSRMLS_CC);
  408. }
  409. return SUCCESS;
  410. }
  411. /* }}} */
  412. static int php_v8js_v8_get_closure(zval *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC) /* {{{ */
  413. {
  414. zend_function *invoke;
  415. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  416. if (!obj->ctx) {
  417. zend_throw_exception(php_ce_v8js_script_exception,
  418. "Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
  419. return FAILURE;
  420. }
  421. v8::Isolate *isolate = obj->ctx->isolate;
  422. v8::Locker locker(isolate);
  423. v8::Isolate::Scope isolate_scope(isolate);
  424. v8::HandleScope local_scope(isolate);
  425. v8::Local<v8::Context> temp_context = v8::Context::New(isolate);
  426. v8::Context::Scope temp_scope(temp_context);
  427. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
  428. if (!v8obj->IsFunction()) {
  429. return FAILURE;
  430. }
  431. invoke = (zend_function *) ecalloc(1, sizeof(*invoke));
  432. invoke->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
  433. invoke->common.function_name = estrndup(V8JS_V8_INVOKE_FUNC_NAME, sizeof(V8JS_V8_INVOKE_FUNC_NAME) - 1);
  434. *fptr_ptr = invoke;
  435. if (zobj_ptr) {
  436. *zobj_ptr = object;
  437. }
  438. *ce_ptr = NULL;
  439. return SUCCESS;
  440. }
  441. /* }}} */
  442. static void php_v8js_v8_free_storage(void *object, zend_object_handle handle TSRMLS_DC) /* {{{ */
  443. {
  444. php_v8js_object *c = (php_v8js_object *) object;
  445. if (c->properties) {
  446. zend_hash_destroy(c->properties);
  447. FREE_HASHTABLE(c->properties);
  448. c->properties = NULL;
  449. }
  450. zend_object_std_dtor(&c->std TSRMLS_CC);
  451. if(c->ctx) {
  452. c->v8obj.Reset();
  453. c->ctx->php_v8js_objects.remove(c);
  454. }
  455. efree(object);
  456. }
  457. /* }}} */
  458. static zend_object_value php_v8js_v8_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  459. {
  460. zend_object_value retval;
  461. php_v8js_object *c;
  462. c = (php_v8js_object *) ecalloc(1, sizeof(*c));
  463. zend_object_std_init(&c->std, ce TSRMLS_CC);
  464. new(&c->v8obj) v8::Persistent<v8::Value>();
  465. retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) php_v8js_v8_free_storage, NULL TSRMLS_CC);
  466. retval.handlers = &v8_object_handlers;
  467. return retval;
  468. }
  469. /* }}} */
  470. /* NOTE: We could also override v8_object_handlers.get_constructor to throw
  471. * an exception when invoked, but doing so causes the half-constructed object
  472. * to leak -- this seems to be a PHP bug. So we'll define magic __construct
  473. * methods instead. */
  474. /* {{{ proto V8Object::__construct()
  475. */
  476. PHP_METHOD(V8Object,__construct)
  477. {
  478. zend_throw_exception(php_ce_v8js_script_exception,
  479. "Can't directly construct V8 objects!", 0 TSRMLS_CC);
  480. RETURN_FALSE;
  481. }
  482. /* }}} */
  483. /* {{{ proto V8Function::__construct()
  484. */
  485. PHP_METHOD(V8Function,__construct)
  486. {
  487. zend_throw_exception(php_ce_v8js_script_exception,
  488. "Can't directly construct V8 objects!", 0 TSRMLS_CC);
  489. RETURN_FALSE;
  490. }
  491. /* }}} */
  492. void php_v8js_create_v8(zval *res, v8::Handle<v8::Value> value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  493. {
  494. #if PHP_V8_API_VERSION <= 3023008
  495. /* Until V8 3.23.8 Isolate could only take one external pointer. */
  496. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData();
  497. #else
  498. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
  499. #endif
  500. php_v8js_object *c;
  501. object_init_ex(res, value->IsFunction() ? php_ce_v8_function : php_ce_v8_object);
  502. c = (php_v8js_object *) zend_object_store_get_object(res TSRMLS_CC);
  503. c->v8obj.Reset(isolate, value);
  504. c->flags = flags;
  505. c->ctx = ctx;
  506. c->properties = NULL;
  507. ctx->php_v8js_objects.push_front(c);
  508. }
  509. /* }}} */
  510. /* }}} V8 */
  511. /* {{{ Class: V8Js */
  512. static void php_v8js_free_storage(void *object TSRMLS_DC) /* {{{ */
  513. {
  514. php_v8js_ctx *c = (php_v8js_ctx *) object;
  515. zend_object_std_dtor(&c->std TSRMLS_CC);
  516. if (c->pending_exception) {
  517. zval_ptr_dtor(&c->pending_exception);
  518. }
  519. if (c->module_loader) {
  520. zval_ptr_dtor(&c->module_loader);
  521. }
  522. /* Delete PHP global object from JavaScript */
  523. if (!c->context.IsEmpty()) {
  524. v8::Locker locker(c->isolate);
  525. v8::Isolate::Scope isolate_scope(c->isolate);
  526. v8::HandleScope handle_scope(c->isolate);
  527. v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(c->isolate, c->context);
  528. v8::Context::Scope context_scope(v8_context);
  529. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
  530. V8JS_GLOBAL(c->isolate)->Delete(object_name_js);
  531. }
  532. c->object_name.Reset();
  533. c->object_name.~Persistent();
  534. c->global_template.Reset();
  535. c->global_template.~Persistent();
  536. /* Clear persistent handles in template cache */
  537. for (std::map<const char *,v8js_tmpl_t>::iterator it = c->template_cache.begin();
  538. it != c->template_cache.end(); ++it) {
  539. it->second.Reset();
  540. }
  541. c->template_cache.~map();
  542. /* Clear contexts */
  543. for (std::vector<php_v8js_accessor_ctx*>::iterator it = c->accessor_list.begin();
  544. it != c->accessor_list.end(); ++it) {
  545. php_v8js_accessor_ctx_dtor(*it TSRMLS_CC);
  546. }
  547. c->accessor_list.~vector();
  548. /* Clear global object, dispose context */
  549. if (!c->context.IsEmpty()) {
  550. c->context.Reset();
  551. }
  552. c->context.~Persistent();
  553. /* Force garbage collection on our isolate, this is needed that V8 triggers
  554. * our MakeWeak callbacks. Without these we won't remove our references
  555. * on the PHP objects leading to memory leaks in PHP context.
  556. */
  557. {
  558. v8::Locker locker(c->isolate);
  559. v8::Isolate::Scope isolate_scope(c->isolate);
  560. #if PHP_V8_API_VERSION < 3028036
  561. while(!v8::V8::IdleNotification()) {};
  562. #else
  563. while(!c->isolate->IdleNotification(500)) {};
  564. #endif
  565. }
  566. /* Dispose yet undisposed weak refs */
  567. for (std::map<zval *, v8js_persistent_obj_t>::iterator it = c->weak_objects.begin();
  568. it != c->weak_objects.end(); ++it) {
  569. zval *value = it->first;
  570. zval_ptr_dtor(&value);
  571. c->isolate->AdjustAmountOfExternalAllocatedMemory(-1024);
  572. it->second.Reset();
  573. }
  574. c->weak_objects.~map();
  575. for (std::map<v8js_tmpl_t *, v8js_persistent_obj_t>::iterator it = c->weak_closures.begin();
  576. it != c->weak_closures.end(); ++it) {
  577. v8js_tmpl_t *persist_tpl_ = it->first;
  578. persist_tpl_->Reset();
  579. delete persist_tpl_;
  580. it->second.Reset();
  581. }
  582. c->weak_closures.~map();
  583. for (std::list<php_v8js_object *>::iterator it = c->php_v8js_objects.begin();
  584. it != c->php_v8js_objects.end(); it ++) {
  585. (*it)->v8obj.Reset();
  586. (*it)->ctx = NULL;
  587. }
  588. c->php_v8js_objects.~list();
  589. c->isolate->Dispose();
  590. if(c->tz != NULL) {
  591. free(c->tz);
  592. }
  593. c->modules_stack.~vector();
  594. c->modules_base.~vector();
  595. efree(object);
  596. }
  597. /* }}} */
  598. static zend_object_value php_v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  599. {
  600. zend_object_value retval;
  601. php_v8js_ctx *c;
  602. c = (php_v8js_ctx *) ecalloc(1, sizeof(*c));
  603. zend_object_std_init(&c->std, ce TSRMLS_CC);
  604. TSRMLS_SET_CTX(c->zts_ctx);
  605. #if PHP_VERSION_ID >= 50400
  606. object_properties_init(&c->std, ce);
  607. #else
  608. zval *tmp;
  609. zend_hash_copy(c->std.properties, &ce->default_properties,
  610. (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  611. #endif
  612. new(&c->object_name) v8::Persistent<v8::String>();
  613. new(&c->context) v8::Persistent<v8::Context>();
  614. new(&c->global_template) v8::Persistent<v8::FunctionTemplate>();
  615. new(&c->modules_stack) std::vector<char*>();
  616. new(&c->modules_base) std::vector<char*>();
  617. new(&c->template_cache) std::map<const char *,v8js_tmpl_t>();
  618. new(&c->accessor_list) std::vector<php_v8js_accessor_ctx *>();
  619. new(&c->weak_closures) std::map<v8js_tmpl_t *, v8js_persistent_obj_t>();
  620. new(&c->weak_objects) std::map<zval *, v8js_persistent_obj_t>();
  621. new(&c->php_v8js_objects) std::list<php_v8js_object *>();
  622. retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) php_v8js_free_storage, NULL TSRMLS_CC);
  623. retval.handlers = &v8js_object_handlers;
  624. return retval;
  625. }
  626. /* }}} */
  627. static void _php_v8js_free_ext_strarr(const char **arr, int count) /* {{{ */
  628. {
  629. int i;
  630. if (arr) {
  631. for (i = 0; i < count; i++) {
  632. if (arr[i]) {
  633. free((void *) arr[i]);
  634. }
  635. }
  636. free(arr);
  637. }
  638. }
  639. /* }}} */
  640. static void php_v8js_jsext_dtor(php_v8js_jsext *jsext) /* {{{ */
  641. {
  642. if (jsext->deps_ht) {
  643. zend_hash_destroy(jsext->deps_ht);
  644. free(jsext->deps_ht);
  645. }
  646. if (jsext->deps) {
  647. _php_v8js_free_ext_strarr(jsext->deps, jsext->deps_count);
  648. }
  649. free(jsext->name);
  650. free(jsext->source);
  651. }
  652. /* }}} */
  653. static int _php_v8js_create_ext_strarr(const char ***retval, int count, HashTable *ht) /* {{{ */
  654. {
  655. const char **exts = NULL;
  656. HashPosition pos;
  657. zval **tmp;
  658. int i = 0;
  659. exts = (const char **) calloc(1, count * sizeof(char *));
  660. zend_hash_internal_pointer_reset_ex(ht, &pos);
  661. while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &pos) == SUCCESS) {
  662. if (Z_TYPE_PP(tmp) == IS_STRING) {
  663. exts[i++] = zend_strndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
  664. } else {
  665. _php_v8js_free_ext_strarr(exts, i);
  666. return FAILURE;
  667. }
  668. zend_hash_move_forward_ex(ht, &pos);
  669. }
  670. *retval = exts;
  671. return SUCCESS;
  672. }
  673. /* }}} */
  674. static void php_v8js_fatal_error_handler(const char *location, const char *message) /* {{{ */
  675. {
  676. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  677. if (isolate) {
  678. isolate->Exit();
  679. }
  680. if (location) {
  681. zend_error(E_ERROR, "%s %s", location, message);
  682. } else {
  683. zend_error(E_ERROR, "%s", message);
  684. }
  685. }
  686. /* }}} */
  687. #ifdef ENABLE_DEBUGGER_SUPPORT
  688. static void DispatchDebugMessages() { /* {{{ */
  689. if(v8js_debug_context == NULL) {
  690. return;
  691. }
  692. v8::Isolate* isolate = v8js_debug_context->isolate;
  693. v8::Isolate::Scope isolate_scope(isolate);
  694. v8::HandleScope handle_scope(isolate);
  695. v8::Local<v8::Context> context =
  696. v8::Local<v8::Context>::New(isolate, v8js_debug_context->context);
  697. v8::Context::Scope scope(context);
  698. v8::Debug::ProcessDebugMessages();
  699. }
  700. /* }}} */
  701. #endif /* ENABLE_DEBUGGER_SUPPORT */
  702. static void php_v8js_init(TSRMLS_D) /* {{{ */
  703. {
  704. /* Run only once */
  705. if (V8JSG(v8_initialized)) {
  706. return;
  707. }
  708. /* Set V8 command line flags (must be done before V8::Initialize()!) */
  709. if (V8JSG(v8_flags)) {
  710. v8::V8::SetFlagsFromString(V8JSG(v8_flags), strlen(V8JSG(v8_flags)));
  711. }
  712. /* Initialize V8 */
  713. v8::V8::Initialize();
  714. /* Run only once */
  715. V8JSG(v8_initialized) = 1;
  716. }
  717. /* }}} */
  718. /* {{{ proto void V8Js::__construct([string object_name [, array variables [, array extensions [, bool report_uncaught_exceptions]]])
  719. __construct for V8Js */
  720. static PHP_METHOD(V8Js, __construct)
  721. {
  722. char *object_name = NULL, *class_name = NULL;
  723. int object_name_len = 0, free = 0;
  724. zend_uint class_name_len = 0;
  725. zend_bool report_uncaught = 1;
  726. zval *vars_arr = NULL, *exts_arr = NULL;
  727. const char **exts = NULL;
  728. int exts_count = 0;
  729. php_v8js_ctx *c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  730. if (!c->context.IsEmpty()) {
  731. /* called __construct() twice, bail out */
  732. return;
  733. }
  734. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|saab", &object_name, &object_name_len, &vars_arr, &exts_arr, &report_uncaught) == FAILURE) {
  735. return;
  736. }
  737. /* Initialize V8 */
  738. php_v8js_init(TSRMLS_C);
  739. /* Throw PHP exception if uncaught exceptions exist */
  740. c->report_uncaught = report_uncaught;
  741. c->pending_exception = NULL;
  742. c->in_execution = 0;
  743. c->isolate = v8::Isolate::New();
  744. #if PHP_V8_API_VERSION <= 3023008
  745. /* Until V8 3.23.8 Isolate could only take one external pointer. */
  746. c->isolate->SetData(c);
  747. #else
  748. c->isolate->SetData(0, c);
  749. #endif
  750. c->time_limit_hit = false;
  751. c->memory_limit_hit = false;
  752. c->module_loader = NULL;
  753. /* Include extensions used by this context */
  754. /* Note: Extensions registered with auto_enable do not need to be added separately like this. */
  755. if (exts_arr)
  756. {
  757. exts_count = zend_hash_num_elements(Z_ARRVAL_P(exts_arr));
  758. if (_php_v8js_create_ext_strarr(&exts, exts_count, Z_ARRVAL_P(exts_arr)) == FAILURE) {
  759. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid extensions array passed");
  760. return;
  761. }
  762. }
  763. /* Declare configuration for extensions */
  764. v8::ExtensionConfiguration extension_conf(exts_count, exts);
  765. // Isolate execution
  766. v8::Isolate *isolate = c->isolate;
  767. v8::Locker locker(isolate);
  768. v8::Isolate::Scope isolate_scope(isolate);
  769. /* Handle scope */
  770. v8::HandleScope handle_scope(isolate);
  771. /* Redirect fatal errors to PHP error handler */
  772. // This needs to be done within the context isolate
  773. v8::V8::SetFatalErrorHandler(php_v8js_fatal_error_handler);
  774. /* Create global template for global object */
  775. // Now we are using multiple isolates this needs to be created for every context
  776. v8::Local<v8::FunctionTemplate> tpl = V8JS_NEW(v8::FunctionTemplate, c->isolate, 0);
  777. tpl->SetClassName(V8JS_SYM("V8Js"));
  778. c->global_template.Reset(isolate, tpl);
  779. /* Register builtin methods */
  780. php_v8js_register_methods(tpl->InstanceTemplate(), c);
  781. /* Create context */
  782. v8::Local<v8::Context> context = v8::Context::New(isolate, &extension_conf, tpl->InstanceTemplate());
  783. context->SetAlignedPointerInEmbedderData(1, c);
  784. c->context.Reset(isolate, context);
  785. if (exts) {
  786. _php_v8js_free_ext_strarr(exts, exts_count);
  787. }
  788. /* If extensions have errors, context will be empty. (NOTE: This is V8 stuff, they expect the passed sources to compile :) */
  789. if (c->context.IsEmpty()) {
  790. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create V8 context. Check that registered extensions do not have errors.");
  791. ZVAL_NULL(getThis());
  792. return;
  793. }
  794. /* Enter context */
  795. v8::Context::Scope context_scope(context);
  796. /* Create the PHP container object's function template */
  797. v8::Local<v8::FunctionTemplate> php_obj_t = V8JS_NEW(v8::FunctionTemplate, isolate, 0);
  798. /* Set class name for PHP object */
  799. #if PHP_VERSION_ID >= 50400
  800. free = !zend_get_object_classname(getThis(), const_cast<const char**>(&class_name), &class_name_len TSRMLS_CC);
  801. #else
  802. free = !zend_get_object_classname(getThis(), &class_name, &class_name_len TSRMLS_CC);
  803. #endif
  804. php_obj_t->SetClassName(V8JS_SYML(class_name, class_name_len));
  805. if (free) {
  806. efree(class_name);
  807. }
  808. /* Register Get accessor for passed variables */
  809. if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
  810. php_v8js_register_accessors(&c->accessor_list, php_obj_t, vars_arr, isolate TSRMLS_CC);
  811. }
  812. /* Set name for the PHP JS object */
  813. v8::Local<v8::String> object_name_js = (object_name_len) ? V8JS_SYML(object_name, object_name_len) : V8JS_SYM("PHP");
  814. c->object_name.Reset(isolate, object_name_js);
  815. /* Add the PHP object into global object */
  816. v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance();
  817. V8JS_GLOBAL(isolate)->ForceSet(object_name_js, php_obj, v8::ReadOnly);
  818. /* Export public property values */
  819. HashTable *properties = zend_std_get_properties(getThis() TSRMLS_CC);
  820. HashPosition pos;
  821. zval **value;
  822. ulong index;
  823. char *member;
  824. uint member_len;
  825. for(zend_hash_internal_pointer_reset_ex(properties, &pos);
  826. zend_hash_get_current_data_ex(properties, (void **) &value, &pos) == SUCCESS;
  827. zend_hash_move_forward_ex(properties, &pos)) {
  828. if(zend_hash_get_current_key_ex(properties, &member, &member_len, &index, 0, &pos) != HASH_KEY_IS_STRING) {
  829. continue;
  830. }
  831. zval zmember;
  832. INIT_ZVAL(zmember);
  833. ZVAL_STRING(&zmember, member, 0);
  834. zend_property_info *property_info = zend_get_property_info(c->std.ce, &zmember, 1 TSRMLS_CC);
  835. if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
  836. /* Write value to PHP JS object */
  837. php_obj->ForceSet(V8JS_SYML(member, member_len - 1), zval_to_v8js(*value, isolate TSRMLS_CC), v8::ReadOnly);
  838. }
  839. }
  840. }
  841. /* }}} */
  842. #define V8JS_BEGIN_CTX(ctx, object) \
  843. php_v8js_ctx *(ctx); \
  844. \
  845. if (!V8JSG(v8_initialized)) { \
  846. zend_error(E_ERROR, "V8 not initialized"); \
  847. return; \
  848. } \
  849. \
  850. (ctx) = (php_v8js_ctx *) zend_object_store_get_object(object TSRMLS_CC); \
  851. v8::Isolate *isolate = (ctx)->isolate; \
  852. v8::Locker locker(isolate); \
  853. v8::Isolate::Scope isolate_scope(isolate); \
  854. v8::HandleScope handle_scope(isolate); \
  855. v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, (ctx)->context); \
  856. v8::Context::Scope context_scope(v8_context);
  857. static void php_v8js_timer_push(long time_limit, long memory_limit, php_v8js_ctx *c TSRMLS_DC)
  858. {
  859. V8JSG(timer_mutex).lock();
  860. // Create context for this timer
  861. php_v8js_timer_ctx *timer_ctx = (php_v8js_timer_ctx *)emalloc(sizeof(php_v8js_timer_ctx));
  862. // Calculate the time point when the time limit is exceeded
  863. std::chrono::milliseconds duration(time_limit);
  864. std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
  865. // Push the timer context
  866. timer_ctx->time_limit = time_limit;
  867. timer_ctx->memory_limit = memory_limit;
  868. timer_ctx->time_point = from + duration;
  869. timer_ctx->v8js_ctx = c;
  870. timer_ctx->killed = false;
  871. V8JSG(timer_stack).push(timer_ctx);
  872. V8JSG(timer_mutex).unlock();
  873. }
  874. static void php_v8js_timer_pop(TSRMLS_D)
  875. {
  876. V8JSG(timer_mutex).lock();
  877. if (V8JSG(timer_stack).size()) {
  878. php_v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).top();
  879. // Remove the timer context from the stack
  880. V8JSG(timer_stack).pop();
  881. // Free the timer context memory
  882. efree(timer_ctx);
  883. }
  884. V8JSG(timer_mutex).unlock();
  885. }
  886. static void php_v8js_terminate_execution(php_v8js_ctx *c TSRMLS_DC)
  887. {
  888. // Forcefully terminate the current thread of V8 execution in the isolate
  889. v8::V8::TerminateExecution(c->isolate);
  890. // This timer will be removed from stack by the parent thread.
  891. }
  892. static void php_v8js_timer_thread(TSRMLS_D)
  893. {
  894. while (!V8JSG(timer_stop)) {
  895. std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
  896. v8::HeapStatistics hs;
  897. if (V8JSG(timer_stack).size()) {
  898. // Get the current timer context
  899. php_v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).top();
  900. php_v8js_ctx *c = timer_ctx->v8js_ctx;
  901. // Get memory usage statistics for the isolate
  902. c->isolate->GetHeapStatistics(&hs);
  903. if (timer_ctx->time_limit > 0 && now > timer_ctx->time_point &&
  904. !timer_ctx->killed) {
  905. timer_ctx->killed = true;
  906. php_v8js_terminate_execution(c TSRMLS_CC);
  907. V8JSG(timer_mutex).lock();
  908. c->time_limit_hit = true;
  909. V8JSG(timer_mutex).unlock();
  910. }
  911. if (timer_ctx->memory_limit > 0 && hs.used_heap_size() > timer_ctx->memory_limit &&
  912. !timer_ctx->killed) {
  913. timer_ctx->killed = true;
  914. php_v8js_terminate_execution(c TSRMLS_CC);
  915. V8JSG(timer_mutex).lock();
  916. c->memory_limit_hit = true;
  917. V8JSG(timer_mutex).unlock();
  918. }
  919. }
  920. // Sleep for 10ms
  921. std::chrono::milliseconds duration(10);
  922. std::this_thread::sleep_for(duration);
  923. }
  924. }
  925. static void php_v8js_compile_script(zval *this_ptr, const char *str, int str_len, const char *identifier, int identifier_len, php_v8js_script **ret)
  926. {
  927. php_v8js_script *res = NULL;
  928. V8JS_BEGIN_CTX(c, this_ptr)
  929. /* Catch JS exceptions */
  930. v8::TryCatch try_catch;
  931. /* Set script identifier */
  932. v8::Local<v8::String> sname = identifier_len ? V8JS_SYML(identifier, identifier_len) : V8JS_SYM("V8Js::compileString()");
  933. /* Compiles a string context independently. TODO: Add a php function which calls this and returns the result as resource which can be executed later. */
  934. v8::Local<v8::String> source = V8JS_STRL(str, str_len);
  935. v8::Local<v8::Script> script = v8::Script::Compile(source, sname);
  936. /* Compile errors? */
  937. if (script.IsEmpty()) {
  938. php_v8js_throw_script_exception(&try_catch TSRMLS_CC);
  939. return;
  940. }
  941. res = (php_v8js_script *)emalloc(sizeof(php_v8js_script));
  942. res->script = new v8::Persistent<v8::Script, v8::CopyablePersistentTraits<v8::Script>>(c->isolate, script);
  943. v8::String::Utf8Value _sname(sname);
  944. res->name = estrndup(ToCString(_sname), _sname.length());
  945. res->isolate = c->isolate;
  946. *ret = res;
  947. return;
  948. }
  949. static void php_v8js_execute_script(zval *this_ptr, php_v8js_script *res, long flags, long time_limit, long memory_limit, zval **return_value)
  950. {
  951. char *tz = NULL;
  952. V8JS_BEGIN_CTX(c, this_ptr)
  953. if (res->isolate != c->isolate) {
  954. zend_error(E_WARNING, "Script resource from wrong V8Js object passed");
  955. ZVAL_BOOL(*return_value, 0);
  956. return;
  957. }
  958. V8JSG(timer_mutex).lock();
  959. c->time_limit_hit = false;
  960. c->memory_limit_hit = false;
  961. V8JSG(timer_mutex).unlock();
  962. /* Catch JS exceptions */
  963. v8::TryCatch try_catch;
  964. /* Set flags for runtime use */
  965. V8JS_GLOBAL_SET_FLAGS(isolate, flags);
  966. /* Check if timezone has been changed and notify V8 */
  967. tz = getenv("TZ");
  968. if (tz != NULL) {
  969. if (c->tz == NULL) {
  970. c->tz = strdup(tz);
  971. }
  972. else if (strcmp(c->tz, tz) != 0) {
  973. #if PHP_V8_API_VERSION <= 3023012
  974. v8::Date::DateTimeConfigurationChangeNotification();
  975. #else
  976. v8::Date::DateTimeConfigurationChangeNotification(c->isolate);
  977. #endif
  978. free(c->tz);
  979. c->tz = strdup(tz);
  980. }
  981. }
  982. if (time_limit > 0 || memory_limit > 0) {
  983. // If timer thread is not running then start it
  984. if (!V8JSG(timer_thread)) {
  985. // If not, start timer thread
  986. V8JSG(timer_thread) = new std::thread(php_v8js_timer_thread TSRMLS_CC);
  987. }
  988. php_v8js_timer_push(time_limit, memory_limit, c TSRMLS_CC);
  989. }
  990. #ifdef ENABLE_DEBUGGER_SUPPORT
  991. if(c == v8js_debug_context && v8js_debug_auto_break_mode != V8JS_DEBUG_AUTO_BREAK_NEVER) {
  992. v8::Debug::DebugBreak(c->isolate);
  993. if(v8js_debug_auto_break_mode == V8JS_DEBUG_AUTO_BREAK_ONCE) {
  994. /* If break-once-mode was enabled, reset flag. */
  995. v8js_debug_auto_break_mode = V8JS_DEBUG_AUTO_BREAK_NEVER;
  996. }
  997. }
  998. #endif /* ENABLE_DEBUGGER_SUPPORT */
  999. /* Execute script */
  1000. c->in_execution++;
  1001. v8::Local<v8::Script> script = v8::Local<v8::Script>::New(c->isolate, *res->script);
  1002. v8::Local<v8::Value> result = script->Run();
  1003. c->in_execution--;
  1004. if (time_limit > 0 || memory_limit > 0) {
  1005. php_v8js_timer_pop(TSRMLS_C);
  1006. }
  1007. /* Check for fatal error marker possibly set by php_v8js_error_handler; just
  1008. * rethrow the error since we're now out of V8. */
  1009. if(V8JSG(fatal_error_abort)) {
  1010. zend_error(V8JSG(error_num), "%s", V8JSG(error_message));
  1011. }
  1012. char exception_string[64];
  1013. if (c->time_limit_hit) {
  1014. // Execution has been terminated due to time limit
  1015. sprintf(exception_string, "Script time limit of %lu milliseconds exceeded", time_limit);
  1016. zend_throw_exception(php_ce_v8js_time_limit_exception, exception_string, 0 TSRMLS_CC);
  1017. return;
  1018. }
  1019. if (c->memory_limit_hit) {
  1020. // Execution has been terminated due to memory limit
  1021. sprintf(exception_string, "Script memory limit of %lu bytes exceeded", memory_limit);
  1022. zend_throw_exception(php_ce_v8js_memory_limit_exception, exception_string, 0 TSRMLS_CC);
  1023. return;
  1024. }
  1025. if (!try_catch.CanContinue()) {
  1026. // At this point we can't re-throw the exception
  1027. return;
  1028. }
  1029. /* There was pending exception left from earlier executions -> throw to PHP */
  1030. if (c->pending_exception) {
  1031. zend_throw_exception_object(c->pending_exception TSRMLS_CC);
  1032. c->pending_exception = NULL;
  1033. }
  1034. /* Handle runtime JS exceptions */
  1035. if (try_catch.HasCaught()) {
  1036. /* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
  1037. if (c->in_execution < 1) {
  1038. /* Report immediately if report_uncaught is true */
  1039. if (c->report_uncaught) {
  1040. php_v8js_throw_script_exception(&try_catch TSRMLS_CC);
  1041. return;
  1042. }
  1043. /* Exception thrown from JS, preserve it for future execution */
  1044. if (result.IsEmpty()) {
  1045. MAKE_STD_ZVAL(c->pending_exception);
  1046. php_v8js_create_script_exception(c->pending_exception, &try_catch TSRMLS_CC);
  1047. return;
  1048. }
  1049. }
  1050. /* Rethrow back to JS */
  1051. try_catch.ReThrow();
  1052. return;
  1053. }
  1054. /* Convert V8 value to PHP value */
  1055. if (!result.IsEmpty()) {
  1056. v8js_to_zval(result, *return_value, flags, c->isolate TSRMLS_CC);
  1057. }
  1058. }
  1059. /* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
  1060. */
  1061. static PHP_METHOD(V8Js, executeString)
  1062. {
  1063. char *str = NULL, *identifier = NULL, *tz = NULL;
  1064. int str_len = 0, identifier_len = 0;
  1065. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  1066. php_v8js_script *res = NULL;
  1067. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slll", &str, &str_len, &identifier, &identifier_len, &flags, &time_limit, &memory_limit) == FAILURE) {
  1068. return;
  1069. }
  1070. php_v8js_compile_script(getThis(), str, str_len, identifier, identifier_len, &res);
  1071. if (!res) {
  1072. RETURN_FALSE;
  1073. }
  1074. php_v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value);
  1075. php_v8js_script_free(res, true);
  1076. }
  1077. /* }}} */
  1078. /* {{{ proto mixed V8Js::compileString(string script [, string identifier])
  1079. */
  1080. static PHP_METHOD(V8Js, compileString)
  1081. {
  1082. char *str = NULL, *identifier = NULL;
  1083. int str_len = 0, identifier_len = 0;
  1084. php_v8js_script *res = NULL;
  1085. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len, &identifier, &identifier_len) == FAILURE) {
  1086. return;
  1087. }
  1088. php_v8js_compile_script(getThis(), str, str_len, identifier, identifier_len, &res);
  1089. if (res) {
  1090. ZEND_REGISTER_RESOURCE(return_value, res, le_v8js_script);
  1091. }
  1092. return;
  1093. }
  1094. /* }}} */
  1095. /* {{{ proto mixed V8Js::executeScript(resource script [, int flags]])
  1096. */
  1097. static PHP_METHOD(V8Js, executeScript)
  1098. {
  1099. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  1100. zval *zscript;
  1101. php_v8js_script *res;
  1102. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|lll", &zscript, &flags, &time_limit, &memory_limit) == FAILURE) {
  1103. return;
  1104. }
  1105. ZEND_FETCH_RESOURCE(res, php_v8js_script*, &zscript, -1, PHP_V8JS_SCRIPT_RES_NAME, le_v8js_script);
  1106. ZEND_VERIFY_RESOURCE(res);
  1107. php_v8js_execute_script(getThis(), res, flags, time_limit, memory_limit, &return_value);
  1108. }
  1109. /* }}} */
  1110. /* {{{ proto mixed V8Js::checkString(string script)
  1111. */
  1112. static PHP_METHOD(V8Js, checkString)
  1113. {
  1114. char *str = NULL;
  1115. int str_len = 0;
  1116. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  1117. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  1118. return;
  1119. }
  1120. V8JS_BEGIN_CTX(c, getThis())
  1121. /* Catch JS exceptions */
  1122. v8::TryCatch try_catch;
  1123. /* Set script identifier */
  1124. v8::Local<v8::String> sname = V8JS_SYM("V8Js::checkString()");
  1125. /* Compiles a string context independently. TODO: Add a php function which calls this and returns the result as resource which can be executed later. */
  1126. v8::Local<v8::String> source = V8JS_STRL(str, str_len);
  1127. v8::Local<v8::Script> script = v8::Script::Compile(source, sname);
  1128. /* Compile errors? */
  1129. if (script.IsEmpty()) {
  1130. php_v8js_throw_script_exception(&try_catch TSRMLS_CC);
  1131. return;
  1132. }
  1133. RETURN_TRUE;
  1134. }
  1135. /* }}} */
  1136. #ifdef ENABLE_DEBUGGER_SUPPORT
  1137. /* {{{ proto void V8Js::__destruct()
  1138. __destruct for V8Js */
  1139. static PHP_METHOD(V8Js, __destruct)
  1140. {
  1141. V8JS_BEGIN_CTX(c, getThis());
  1142. if(v8js_debug_context == c) {
  1143. v8::Debug::DisableAgent();
  1144. v8js_debug_context = NULL;
  1145. }
  1146. }
  1147. /* }}} */
  1148. /* {{{ proto bool V8Js::startDebugAgent(string agent_name[, int port[, int auto_break]])
  1149. */
  1150. static PHP_METHOD(V8Js, startDebugAgent)
  1151. {
  1152. char *str = NULL;
  1153. int str_len = 0;
  1154. long port = 0, auto_break = 0;
  1155. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sll", &str, &str_len, &port, &auto_break) == FAILURE) {
  1156. return;
  1157. }
  1158. if(!port) {
  1159. port = 9222;
  1160. }
  1161. V8JS_BEGIN_CTX(c, getThis());
  1162. if(v8js_debug_context == c) {
  1163. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Debug agent already started for this V8Js instance");
  1164. RETURN_BOOL(0);
  1165. }
  1166. if(v8js_debug_context != NULL) {
  1167. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Debug agent already started for a different V8Js instance");
  1168. RETURN_BOOL(0);
  1169. }
  1170. v8js_debug_context = c;
  1171. v8js_debug_auto_break_mode = auto_break;
  1172. v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true);
  1173. v8::Debug::EnableAgent(str_len ? str : "V8Js", port, auto_break > 0);
  1174. if(auto_break) {
  1175. /* v8::Debug::EnableAgent doesn't really do what we want it to do,
  1176. since it only breaks processing on the default isolate.
  1177. Hence just trigger another DebugBreak, no for our main isolate. */
  1178. v8::Debug::DebugBreak(c->isolate);
  1179. }
  1180. RETURN_BOOL(1);
  1181. }
  1182. /* }}} */
  1183. #endif /* ENABLE_DEBUGGER_SUPPORT */
  1184. /* {{{ proto mixed V8Js::getPendingException()
  1185. */
  1186. static PHP_METHOD(V8Js, getPendingException)
  1187. {
  1188. php_v8js_ctx *c;
  1189. if (zend_parse_parameters_none() == FAILURE) {
  1190. return;
  1191. }
  1192. c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  1193. if (c->pending_exception) {
  1194. RETURN_ZVAL(c->pending_exception, 1, 0);
  1195. }
  1196. }
  1197. /* }}} */
  1198. /* {{{ proto void V8Js::setModuleLoader(string module)
  1199. */
  1200. static PHP_METHOD(V8Js, setModuleLoader)
  1201. {
  1202. php_v8js_ctx *c;
  1203. zval *callable;
  1204. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callable) == FAILURE) {
  1205. return;
  1206. }
  1207. c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  1208. c->module_loader = callable;
  1209. Z_ADDREF_P(c->module_loader);
  1210. }
  1211. /* }}} */
  1212. static void php_v8js_persistent_zval_ctor(zval **p) /* {{{ */
  1213. {
  1214. zval *orig_ptr = *p;
  1215. *p = (zval *) malloc(sizeof(zval));
  1216. **p = *orig_ptr;
  1217. switch (Z_TYPE_P(*p)) {
  1218. case IS_STRING:
  1219. Z_STRVAL_P(*p) = (char *) zend_strndup(Z_STRVAL_P(*p), Z_STRLEN_P(*p));
  1220. break;
  1221. default:
  1222. zend_bailout();
  1223. }
  1224. INIT_PZVAL(*p);
  1225. }
  1226. /* }}} */
  1227. static void php_v8js_persistent_zval_dtor(zval **p) /* {{{ */
  1228. {
  1229. switch (Z_TYPE_P(*p)) {
  1230. case IS_STRING:
  1231. free(Z_STRVAL_P(*p));
  1232. break;
  1233. default:
  1234. zend_bailout();
  1235. }
  1236. free(*p);
  1237. }
  1238. /* }}} */
  1239. static void php_v8js_script_free(php_v8js_script *res, bool dispose_persistent)
  1240. {
  1241. if (res->name) {
  1242. efree(res->name);
  1243. res->name = NULL;
  1244. }
  1245. if (dispose_persistent) {
  1246. res->script->~Persistent(); // does Reset()
  1247. res->script = NULL;
  1248. }
  1249. }
  1250. static void php_v8js_script_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
  1251. {
  1252. php_v8js_script *res = (php_v8js_script *)rsrc->ptr;
  1253. if (res) {
  1254. php_v8js_script_free(res, false);
  1255. efree(res);
  1256. }
  1257. }
  1258. /* }}} */
  1259. 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) /* {{{ */
  1260. {
  1261. php_v8js_jsext *jsext = NULL;
  1262. if (!V8JSG(extensions)) {
  1263. V8JSG(extensions) = (HashTable *) malloc(sizeof(HashTable));
  1264. zend_hash_init(V8JSG(extensions), 1, NULL, (dtor_func_t) php_v8js_jsext_dtor, 1);
  1265. } else if (zend_hash_exists(V8JSG(extensions), name, name_len + 1)) {
  1266. return FAILURE;
  1267. }
  1268. jsext = (php_v8js_jsext *) calloc(1, sizeof(php_v8js_jsext));
  1269. if (deps_arr) {
  1270. jsext->deps_count = zend_hash_num_elements(Z_ARRVAL_P(deps_arr));
  1271. if (_php_v8js_create_ext_strarr(&jsext->deps, jsext->deps_count, Z_ARRVAL_P(deps_arr)) == FAILURE) {
  1272. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid dependancy array passed");
  1273. php_v8js_jsext_dtor(jsext);
  1274. free(jsext);
  1275. return FAILURE;
  1276. }
  1277. }
  1278. jsext->auto_enable = auto_enable;
  1279. jsext->name = zend_strndup(name, name_len);
  1280. jsext->source = zend_strndup(source, source_len);
  1281. if (jsext->deps) {
  1282. jsext->deps_ht = (HashTable *) malloc(sizeof(HashTable));
  1283. zend_hash_init(jsext->deps_ht, jsext->deps_count, NULL, (dtor_func_t) php_v8js_persistent_zval_dtor, 1);
  1284. zend_hash_copy(jsext->deps_ht, Z_ARRVAL_P(deps_arr), (copy_ctor_func_t) php_v8js_persistent_zval_ctor, NULL, sizeof(zval *));
  1285. }
  1286. if (zend_hash_add(V8JSG(extensions), name, name_len + 1, jsext, sizeof(php_v8js_jsext), NULL) == FAILURE) {
  1287. php_v8js_jsext_dtor(jsext);
  1288. free(jsext);
  1289. return FAILURE;
  1290. }
  1291. v8::Extension *extension = new v8::Extension(jsext->name, jsext->source, jsext->deps_count, jsext->deps);
  1292. extension->set_auto_enable(auto_enable ? true : false);
  1293. v8::RegisterExtension(extension);
  1294. free(jsext);
  1295. return SUCCESS;
  1296. }
  1297. /* }}} */
  1298. /* {{{ proto bool V8Js::registerExtension(string ext_name, string script [, array deps [, bool auto_enable]])
  1299. */
  1300. static PHP_METHOD(V8Js, registerExtension)
  1301. {
  1302. char *ext_name, *script;
  1303. zval *deps_arr = NULL;
  1304. int ext_name_len, script_len;
  1305. zend_bool auto_enable = 0;
  1306. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ab", &ext_name, &ext_name_len, &script, &script_len, &deps_arr, &auto_enable) == FAILURE) {
  1307. return;
  1308. }
  1309. if (!ext_name_len) {
  1310. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Extension name cannot be empty");
  1311. } else if (!script_len) {
  1312. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Script cannot be empty");
  1313. } else if (php_v8js_register_extension(ext_name, ext_name_len, script, script_len, deps_arr, auto_enable TSRMLS_CC) == SUCCESS) {
  1314. RETURN_TRUE;
  1315. }
  1316. RETURN_FALSE;
  1317. }
  1318. /* }}} */
  1319. /* ## Static methods ## */
  1320. /* {{{ proto array V8Js::getExtensions()
  1321. */
  1322. static PHP_METHOD(V8Js, getExtensions)
  1323. {
  1324. php_v8js_jsext *jsext;
  1325. zval *ext, *deps_arr;
  1326. HashPosition pos;
  1327. ulong index;
  1328. char *key;
  1329. uint key_len;
  1330. if (zend_parse_parameters_none() == FAILURE) {
  1331. return;
  1332. }
  1333. array_init(return_value);
  1334. if (V8JSG(extensions)) {
  1335. zend_hash_internal_pointer_reset_ex(V8JSG(extensions), &pos);
  1336. while (zend_hash_get_current_data_ex(V8JSG(extensions), (void **) &jsext, &pos) == SUCCESS) {
  1337. if (zend_hash_get_current_key_ex(V8JSG(extensions), &key, &key_len, &index, 0, &pos) == HASH_KEY_IS_STRING) {
  1338. MAKE_STD_ZVAL(ext)
  1339. array_init(ext);
  1340. add_assoc_bool_ex(ext, ZEND_STRS("auto_enable"), jsext->auto_enable);
  1341. if (jsext->deps_ht) {
  1342. MAKE_STD_ZVAL(deps_arr);
  1343. array_init(deps_arr);
  1344. zend_hash_copy(Z_ARRVAL_P(deps_arr), jsext->deps_ht, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
  1345. add_assoc_zval_ex(ext, ZEND_STRS("deps"), deps_arr);
  1346. }
  1347. add_assoc_zval_ex(return_value, key, key_len, ext);
  1348. }
  1349. zend_hash_move_forward_ex(V8JSG(extensions), &pos);
  1350. }
  1351. }
  1352. }
  1353. /* }}} */
  1354. /* {{{ arginfo */
  1355. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0)
  1356. ZEND_ARG_INFO(0, object_name)
  1357. ZEND_ARG_INFO(0, variables)
  1358. ZEND_ARG_INFO(0, extensions)
  1359. ZEND_ARG_INFO(0, report_uncaught_exceptions)
  1360. ZEND_END_ARG_INFO()
  1361. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1)
  1362. ZEND_ARG_INFO(0, script)
  1363. ZEND_ARG_INFO(0, identifier)
  1364. ZEND_ARG_INFO(0, flags)
  1365. ZEND_ARG_INFO(0, time_limit)
  1366. ZEND_ARG_INFO(0, memory_limit)
  1367. ZEND_END_ARG_INFO()
  1368. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_compilestring, 0, 0, 1)
  1369. ZEND_ARG_INFO(0, script)
  1370. ZEND_ARG_INFO(0, identifier)
  1371. ZEND_END_ARG_INFO()
  1372. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executescript, 0, 0, 1)
  1373. ZEND_ARG_INFO(0, script)
  1374. ZEND_ARG_INFO(0, flags)
  1375. ZEND_ARG_INFO(0, time_limit)
  1376. ZEND_ARG_INFO(0, memory_limit)
  1377. ZEND_END_ARG_INFO()
  1378. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_checkstring, 0, 0, 1)
  1379. ZEND_ARG_INFO(0, script)
  1380. ZEND_END_ARG_INFO()
  1381. #ifdef ENABLE_DEBUGGER_SUPPORT
  1382. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_destruct, 0, 0, 0)
  1383. ZEND_END_ARG_INFO()
  1384. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_startdebugagent, 0, 0, 0)
  1385. ZEND_ARG_INFO(0, agentName)
  1386. ZEND_ARG_INFO(0, port)
  1387. ZEND_ARG_INFO(0, auto_break)
  1388. ZEND_END_ARG_INFO()
  1389. #endif /* ENABLE_DEBUGGER_SUPPORT */
  1390. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getpendingexception, 0)
  1391. ZEND_END_ARG_INFO()
  1392. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
  1393. ZEND_ARG_INFO(0, callable)
  1394. ZEND_END_ARG_INFO()
  1395. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_registerextension, 0, 0, 2)
  1396. ZEND_ARG_INFO(0, extension_name)
  1397. ZEND_ARG_INFO(0, script)
  1398. ZEND_ARG_INFO(0, dependencies)
  1399. ZEND_ARG_INFO(0, auto_enable)
  1400. ZEND_END_ARG_INFO()
  1401. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0)
  1402. ZEND_END_ARG_INFO()
  1403. ZEND_BEGIN_ARG_INFO(arginfo_v8jsscriptexception_no_args, 0)
  1404. ZEND_END_ARG_INFO()
  1405. /* }}} */
  1406. static const zend_function_entry v8_object_methods[] = { /* {{{ */
  1407. PHP_ME(V8Object, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  1408. {NULL, NULL, NULL}
  1409. };
  1410. /* }}} */
  1411. static const zend_function_entry v8_function_methods[] = { /* {{{ */
  1412. PHP_ME(V8Function, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  1413. {NULL, NULL, NULL}
  1414. };
  1415. /* }}} */
  1416. static const zend_function_entry v8js_methods[] = { /* {{{ */
  1417. PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  1418. PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC)
  1419. PHP_ME(V8Js, compileString, arginfo_v8js_compilestring, ZEND_ACC_PUBLIC)
  1420. PHP_ME(V8Js, executeScript, arginfo_v8js_executescript, ZEND_ACC_PUBLIC)
  1421. PHP_ME(V8Js, checkString, arginfo_v8js_checkstring, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
  1422. PHP_ME(V8Js, getPendingException, arginfo_v8js_getpendingexception, ZEND_ACC_PUBLIC)
  1423. PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
  1424. PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  1425. PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  1426. #ifdef ENABLE_DEBUGGER_SUPPORT
  1427. PHP_ME(V8Js, __destruct, arginfo_v8js_destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
  1428. PHP_ME(V8Js, startDebugAgent, arginfo_v8js_startdebugagent, ZEND_ACC_PUBLIC)
  1429. #endif
  1430. {NULL, NULL, NULL}
  1431. };
  1432. /* }}} */
  1433. /* V8Js object handlers */
  1434. static void php_v8js_write_property(zval *object, zval *member, zval *value ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  1435. {
  1436. V8JS_BEGIN_CTX(c, object)
  1437. /* Check whether member is public, if so, export to V8. */
  1438. zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1 TSRMLS_CC);
  1439. if(property_info->flags & ZEND_ACC_PUBLIC) {
  1440. /* Global PHP JS object */
  1441. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  1442. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject();
  1443. /* Write value to PHP JS object */
  1444. jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, isolate TSRMLS_CC), v8::ReadOnly);
  1445. }
  1446. /* Write value to PHP object */
  1447. std_object_handlers.write_property(object, member, value ZEND_HASH_KEY_CC TSRMLS_CC);
  1448. }
  1449. /* }}} */
  1450. static void php_v8js_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  1451. {
  1452. V8JS_BEGIN_CTX(c, object)
  1453. /* Global PHP JS object */
  1454. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(isolate, c->object_name);
  1455. v8::Local<v8::Object> jsobj = V8JS_GLOBAL(isolate)->Get(object_name_js)->ToObject();
  1456. /* Delete value from PHP JS object */
  1457. jsobj->ForceDelete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
  1458. /* Unset from PHP object */
  1459. std_object_handlers.unset_property(object, member ZEND_HASH_KEY_CC TSRMLS_CC);
  1460. }
  1461. /* }}} */
  1462. /* }}} V8Js */
  1463. /* {{{ Class: V8JsScriptException */
  1464. static void php_v8js_create_script_exception(zval *return_value, v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  1465. {
  1466. v8::String::Utf8Value exception(try_catch->Exception());
  1467. const char *exception_string = ToCString(exception);
  1468. v8::Handle<v8::Message> tc_message = try_catch->Message();
  1469. const char *filename_string, *sourceline_string;
  1470. char *message_string;
  1471. int linenum, message_len;
  1472. object_init_ex(return_value, php_ce_v8js_script_exception);
  1473. #define PHPV8_EXPROP(type, name, value) \
  1474. zend_update_property##type(php_ce_v8js_script_exception, return_value, #name, sizeof(#name) - 1, value TSRMLS_CC);
  1475. if (tc_message.IsEmpty()) {
  1476. message_len = spprintf(&message_string, 0, "%s", exception_string);
  1477. }
  1478. else
  1479. {
  1480. v8::String::Utf8Value filename(tc_message->GetScriptResourceName());
  1481. filename_string = ToCString(filename);
  1482. PHPV8_EXPROP(_string, JsFileName, filename_string);
  1483. v8::String::Utf8Value sourceline(tc_message->GetSourceLine());
  1484. sourceline_string = ToCString(sourceline);
  1485. PHPV8_EXPROP(_string, JsSourceLine, sourceline_string);
  1486. linenum = tc_message->GetLineNumber();
  1487. PHPV8_EXPROP(_long, JsLineNumber, linenum);
  1488. message_len = spprintf(&message_string, 0, "%s:%d: %s", filename_string, linenum, exception_string);
  1489. v8::String::Utf8Value stacktrace(try_catch->StackTrace());
  1490. if (stacktrace.length() > 0) {
  1491. const char* stacktrace_string = ToCString(stacktrace);
  1492. PHPV8_EXPROP(_string, JsTrace, stacktrace_string);
  1493. }
  1494. }
  1495. PHPV8_EXPROP(_string, message, message_string);
  1496. efree(message_string);
  1497. }
  1498. /* }}} */
  1499. static void php_v8js_throw_script_exception(v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  1500. {
  1501. v8::String::Utf8Value exception(try_catch->Exception());
  1502. const char *exception_string = ToCString(exception);
  1503. zval *zexception = NULL;
  1504. if (try_catch->Message().IsEmpty()) {
  1505. zend_throw_exception(php_ce_v8js_script_exception, (char *) exception_string, 0 TSRMLS_CC);
  1506. } else {
  1507. MAKE_STD_ZVAL(zexception);
  1508. php_v8js_create_script_exception(zexception, try_catch TSRMLS_CC);
  1509. zend_throw_exception_object(zexception TSRMLS_CC);
  1510. }
  1511. }
  1512. /* }}} */
  1513. #define V8JS_EXCEPTION_METHOD(property) \
  1514. static PHP_METHOD(V8JsScriptException, get##property) \
  1515. { \
  1516. zval *value; \
  1517. \
  1518. if (zend_parse_parameters_none() == FAILURE) { \
  1519. return; \
  1520. } \
  1521. value = zend_read_property(php_ce_v8js_script_exception, getThis(), #property, sizeof(#property) - 1, 0 TSRMLS_CC); \
  1522. *return_value = *value; \
  1523. zval_copy_ctor(return_value); \
  1524. INIT_PZVAL(return_value); \
  1525. }
  1526. /* {{{ proto string V8JsEScriptxception::getJsFileName()
  1527. */
  1528. V8JS_EXCEPTION_METHOD(JsFileName);
  1529. /* }}} */
  1530. /* {{{ proto string V8JsScriptException::getJsLineNumber()
  1531. */
  1532. V8JS_EXCEPTION_METHOD(JsLineNumber);
  1533. /* }}} */
  1534. /* {{{ proto string V8JsScriptException::getJsSourceLine()
  1535. */
  1536. V8JS_EXCEPTION_METHOD(JsSourceLine);
  1537. /* }}} */
  1538. /* {{{ proto string V8JsScriptException::getJsTrace()
  1539. */
  1540. V8JS_EXCEPTION_METHOD(JsTrace);
  1541. /* }}} */
  1542. static const zend_function_entry v8js_script_exception_methods[] = { /* {{{ */
  1543. PHP_ME(V8JsScriptException, getJsFileName, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1544. PHP_ME(V8JsScriptException, getJsLineNumber, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1545. PHP_ME(V8JsScriptException, getJsSourceLine, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1546. PHP_ME(V8JsScriptException, getJsTrace, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1547. {NULL, NULL, NULL}
  1548. };
  1549. /* }}} */
  1550. /* }}} V8JsScriptException */
  1551. /* {{{ Class: V8JsTimeLimitException */
  1552. static const zend_function_entry v8js_time_limit_exception_methods[] = { /* {{{ */
  1553. {NULL, NULL, NULL}
  1554. };
  1555. /* }}} */
  1556. /* }}} V8JsTimeLimitException */
  1557. /* {{{ Class: V8JsMemoryLimitException */
  1558. static const zend_function_entry v8js_memory_limit_exception_methods[] = { /* {{{ */
  1559. {NULL, NULL, NULL}
  1560. };
  1561. /* }}} */
  1562. /* }}} V8JsMemoryLimitException */
  1563. /* {{{ PHP_MINIT_FUNCTION
  1564. */
  1565. static PHP_MINIT_FUNCTION(v8js)
  1566. {
  1567. zend_class_entry ce;
  1568. /* V8Object Class */
  1569. INIT_CLASS_ENTRY(ce, "V8Object", v8_object_methods);
  1570. php_ce_v8_object = zend_register_internal_class(&ce TSRMLS_CC);
  1571. php_ce_v8_object->ce_flags |= ZEND_ACC_FINAL;
  1572. php_ce_v8_object->create_object = php_v8js_v8_new;
  1573. /* V8Function Class */
  1574. INIT_CLASS_ENTRY(ce, "V8Function", v8_function_methods);
  1575. php_ce_v8_function = zend_register_internal_class(&ce TSRMLS_CC);
  1576. php_ce_v8_function->ce_flags |= ZEND_ACC_FINAL;
  1577. php_ce_v8_function->create_object = php_v8js_v8_new;
  1578. /* V8<Object|Function> handlers */
  1579. memcpy(&v8_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1580. v8_object_handlers.clone_obj = NULL;
  1581. v8_object_handlers.cast_object = NULL;
  1582. v8_object_handlers.get_property_ptr_ptr = NULL;
  1583. v8_object_handlers.has_property = php_v8js_v8_has_property;
  1584. v8_object_handlers.read_property = php_v8js_v8_read_property;
  1585. v8_object_handlers.write_property = php_v8js_v8_write_property;
  1586. v8_object_handlers.unset_property = php_v8js_v8_unset_property;
  1587. v8_object_handlers.get_properties = php_v8js_v8_get_properties;
  1588. v8_object_handlers.get_method = php_v8js_v8_get_method;
  1589. v8_object_handlers.call_method = php_v8js_v8_call_method;
  1590. v8_object_handlers.get_debug_info = php_v8js_v8_get_debug_info;
  1591. v8_object_handlers.get_closure = php_v8js_v8_get_closure;
  1592. /* V8Js Class */
  1593. INIT_CLASS_ENTRY(ce, "V8Js", v8js_methods);
  1594. php_ce_v8js = zend_register_internal_class(&ce TSRMLS_CC);
  1595. php_ce_v8js->create_object = php_v8js_new;
  1596. /* V8Js handlers */
  1597. memcpy(&v8js_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1598. v8js_object_handlers.clone_obj = NULL;
  1599. v8js_object_handlers.write_property = php_v8js_write_property;
  1600. v8js_object_handlers.unset_property = php_v8js_unset_property;
  1601. /* V8Js Class Constants */
  1602. zend_declare_class_constant_string(php_ce_v8js, ZEND_STRL("V8_VERSION"), PHP_V8_VERSION TSRMLS_CC);
  1603. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE TSRMLS_CC);
  1604. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY TSRMLS_CC);
  1605. #ifdef ENABLE_DEBUGGER_SUPPORT
  1606. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("DEBUG_AUTO_BREAK_NEVER"), V8JS_DEBUG_AUTO_BREAK_NEVER TSRMLS_CC);
  1607. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("DEBUG_AUTO_BREAK_ONCE"), V8JS_DEBUG_AUTO_BREAK_ONCE TSRMLS_CC);
  1608. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("DEBUG_AUTO_BREAK_ALWAYS"), V8JS_DEBUG_AUTO_BREAK_ALWAYS TSRMLS_CC);
  1609. #endif
  1610. /* V8JsScriptException Class */
  1611. INIT_CLASS_ENTRY(ce, "V8JsScriptException", v8js_script_exception_methods);
  1612. php_ce_v8js_script_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
  1613. php_ce_v8js_script_exception->ce_flags |= ZEND_ACC_FINAL;
  1614. /* Add custom JS specific properties */
  1615. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsFileName"), ZEND_ACC_PROTECTED TSRMLS_CC);
  1616. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsLineNumber"), ZEND_ACC_PROTECTED TSRMLS_CC);
  1617. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsSourceLine"), ZEND_ACC_PROTECTED TSRMLS_CC);
  1618. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsTrace"), ZEND_ACC_PROTECTED TSRMLS_CC);
  1619. /* V8JsTimeLimitException Class */
  1620. INIT_CLASS_ENTRY(ce, "V8JsTimeLimitException", v8js_time_limit_exception_methods);
  1621. php_ce_v8js_time_limit_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
  1622. php_ce_v8js_time_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  1623. /* V8JsMemoryLimitException Class */
  1624. INIT_CLASS_ENTRY(ce, "V8JsMemoryLimitException", v8js_memory_limit_exception_methods);
  1625. php_ce_v8js_memory_limit_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
  1626. php_ce_v8js_memory_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  1627. le_v8js_script = zend_register_list_destructors_ex(php_v8js_script_dtor, NULL, PHP_V8JS_SCRIPT_RES_NAME, module_number);
  1628. REGISTER_INI_ENTRIES();
  1629. return SUCCESS;
  1630. }
  1631. /* }}} */
  1632. /* {{{ PHP_MSHUTDOWN_FUNCTION
  1633. */
  1634. static PHP_MSHUTDOWN_FUNCTION(v8js)
  1635. {
  1636. UNREGISTER_INI_ENTRIES();
  1637. return SUCCESS;
  1638. }
  1639. /* }}} */
  1640. /* {{{ PHP_RSHUTDOWN_FUNCTION
  1641. */
  1642. static PHP_RSHUTDOWN_FUNCTION(v8js)
  1643. {
  1644. // If the timer thread is running then stop it
  1645. if (V8JSG(timer_thread)) {
  1646. V8JSG(timer_stop) = true;
  1647. V8JSG(timer_thread)->join();
  1648. }
  1649. #if V8JS_DEBUG
  1650. v8::HeapStatistics stats;
  1651. v8::V8::GetHeapStatistics(&stats);
  1652. float used = stats.used_heap_size() / 1024.0 / 1024.0;
  1653. float total = stats.total_heap_size() / 1024.0 / 1024.0;
  1654. fprintf(stderr, "### RSHUTDOWN ###\n");
  1655. fprintf(stderr, "############ Heap Used/Total %.2f/%.2f MB ############\n", used, total);
  1656. fflush(stderr);
  1657. #endif
  1658. return SUCCESS;
  1659. }
  1660. /* }}} */
  1661. /* {{{ PHP_MINFO_FUNCTION
  1662. */
  1663. static PHP_MINFO_FUNCTION(v8js)
  1664. {
  1665. php_info_print_table_start();
  1666. php_info_print_table_header(2, "V8 Javascript Engine", "enabled");
  1667. php_info_print_table_header(2, "V8 Engine Compiled Version", PHP_V8_VERSION);
  1668. php_info_print_table_header(2, "V8 Engine Linked Version", v8::V8::GetVersion());
  1669. php_info_print_table_header(2, "Version", V8JS_VERSION);
  1670. php_info_print_table_end();
  1671. DISPLAY_INI_ENTRIES();
  1672. }
  1673. /* }}} */
  1674. /* {{{ PHP_GINIT_FUNCTION
  1675. */
  1676. static PHP_GINIT_FUNCTION(v8js)
  1677. {
  1678. /*
  1679. If ZTS is disabled, the v8js_globals instance is declared right
  1680. in the BSS and hence automatically initialized by C++ compiler.
  1681. Most of the variables are just zeroed.
  1682. If ZTS is enabled however, v8js_globals just points to a freshly
  1683. allocated, uninitialized piece of memory, hence we need to
  1684. initialize all fields on our own. Likewise on shutdown we have to
  1685. run the destructors manually.
  1686. */
  1687. #ifdef ZTS
  1688. v8js_globals->extensions = NULL;
  1689. v8js_globals->v8_initialized = 0;
  1690. v8js_globals->v8_flags = NULL;
  1691. v8js_globals->timer_thread = NULL;
  1692. v8js_globals->timer_stop = false;
  1693. new(&v8js_globals->timer_mutex) std::mutex;
  1694. new(&v8js_globals->timer_stack) std::stack<php_v8js_timer_ctx *>;
  1695. new(&v8js_globals->modules_loaded) std::map<char *, v8::Handle<v8::Object>>;
  1696. v8js_globals->fatal_error_abort = 0;
  1697. v8js_globals->error_num = 0;
  1698. v8js_globals->error_message = 0;
  1699. v8js_globals->unwind_env = NULL;
  1700. v8js_globals->old_error_handler = NULL;
  1701. #endif
  1702. }
  1703. /* }}} */
  1704. /* {{{ PHP_GSHUTDOWN_FUNCTION
  1705. */
  1706. static PHP_GSHUTDOWN_FUNCTION(v8js)
  1707. {
  1708. if (v8js_globals->extensions) {
  1709. zend_hash_destroy(v8js_globals->extensions);
  1710. free(v8js_globals->extensions);
  1711. v8js_globals->extensions = NULL;
  1712. }
  1713. if (v8js_globals->v8_flags) {
  1714. free(v8js_globals->v8_flags);
  1715. v8js_globals->v8_flags = NULL;
  1716. }
  1717. #ifdef ZTS
  1718. v8js_globals->timer_stack.~stack();
  1719. v8js_globals->timer_mutex.~mutex();
  1720. v8js_globals->modules_loaded.~map();
  1721. #endif
  1722. }
  1723. /* }}} */
  1724. /* {{{ v8js_functions[] */
  1725. static const zend_function_entry v8js_functions[] = {
  1726. {NULL, NULL, NULL}
  1727. };
  1728. /* }}} */
  1729. /* {{{ v8js_module_entry
  1730. */
  1731. zend_module_entry v8js_module_entry = {
  1732. STANDARD_MODULE_HEADER_EX,
  1733. NULL,
  1734. NULL,
  1735. "v8js",
  1736. v8js_functions,
  1737. PHP_MINIT(v8js),
  1738. PHP_MSHUTDOWN(v8js),
  1739. NULL,
  1740. PHP_RSHUTDOWN(v8js),
  1741. PHP_MINFO(v8js),
  1742. V8JS_VERSION,
  1743. PHP_MODULE_GLOBALS(v8js),
  1744. PHP_GINIT(v8js),
  1745. PHP_GSHUTDOWN(v8js),
  1746. NULL,
  1747. STANDARD_MODULE_PROPERTIES_EX
  1748. };
  1749. /* }}} */
  1750. /*
  1751. * Local variables:
  1752. * tab-width: 4
  1753. * c-basic-offset: 4
  1754. * End:
  1755. * vim600: noet sw=4 ts=4 fdm=marker
  1756. * vim<600: noet sw=4 ts=4
  1757. */