v8js.cc 69 KB

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