v8js.cc 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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. /* {{{ INI Settings */
  33. static ZEND_INI_MH(v8js_OnUpdateMaxDisposedContexts) /* {{{ */
  34. {
  35. int max_disposed = zend_atoi(new_value, new_value_length);
  36. if (max_disposed <= 0) {
  37. return FAILURE;
  38. }
  39. V8JSG(max_disposed_contexts) = max_disposed;
  40. return SUCCESS;
  41. }
  42. /* }}} */
  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. /* }}} */
  58. ZEND_INI_BEGIN() /* {{{ */
  59. ZEND_INI_ENTRY("v8js.max_disposed_contexts", "25", ZEND_INI_ALL, v8js_OnUpdateMaxDisposedContexts)
  60. ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
  61. ZEND_INI_END()
  62. /* }}} */
  63. /* }}} INI */
  64. /* {{{ Class Entries */
  65. zend_class_entry *php_ce_v8_object;
  66. zend_class_entry *php_ce_v8_function;
  67. static zend_class_entry *php_ce_v8js;
  68. static zend_class_entry *php_ce_v8js_script_exception;
  69. static zend_class_entry *php_ce_v8js_time_limit_exception;
  70. static zend_class_entry *php_ce_v8js_memory_limit_exception;
  71. /* }}} */
  72. /* {{{ Object Handlers */
  73. static zend_object_handlers v8js_object_handlers;
  74. static zend_object_handlers v8_object_handlers;
  75. /* }}} */
  76. /* {{{ Extension container */
  77. struct php_v8js_jsext {
  78. zend_bool auto_enable;
  79. HashTable *deps_ht;
  80. const char **deps;
  81. int deps_count;
  82. char *name;
  83. char *source;
  84. };
  85. /* }}} */
  86. #ifdef ENABLE_DEBUGGER_SUPPORT
  87. static php_v8js_ctx *v8js_debug_context;
  88. static int v8js_debug_auto_break_mode;
  89. #endif
  90. #ifdef COMPILE_DL_V8JS
  91. ZEND_GET_MODULE(v8js)
  92. #endif
  93. /* {{{ Class: V8 */
  94. #define V8JS_V8_INVOKE_FUNC_NAME "V8Js::V8::Invoke"
  95. /* V8 Object handlers */
  96. static int php_v8js_v8_has_property(zval *object, zval *member, int has_set_exists ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  97. {
  98. /* param has_set_exists:
  99. * 0 (has) whether property exists and is not NULL - isset()
  100. * 1 (set) whether property exists and is true-ish - empty()
  101. * 2 (exists) whether property exists - property_exists()
  102. */
  103. int retval = false;
  104. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  105. v8::Locker locker(obj->isolate);
  106. v8::Isolate::Scope isolate_scope(obj->isolate);
  107. v8::HandleScope local_scope(obj->isolate);
  108. v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
  109. v8::Context::Scope temp_scope(temp_context);
  110. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
  111. if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction())
  112. {
  113. v8::Local<v8::Object> jsObj = v8obj->ToObject();
  114. v8::Local<v8::String> jsKey = V8JS_STRL(Z_STRVAL_P(member), Z_STRLEN_P(member));
  115. v8::Local<v8::Value> jsVal;
  116. /* Skip any prototype properties */
  117. if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
  118. if (has_set_exists == 2) {
  119. /* property_exists(), that's enough! */
  120. retval = true;
  121. } else {
  122. /* We need to look at the value. */
  123. jsVal = jsObj->Get(jsKey);
  124. if (has_set_exists == 0 ) {
  125. /* isset(): We make 'undefined' equivalent to 'null' */
  126. retval = !( jsVal->IsNull() || jsVal->IsUndefined() );
  127. } else {
  128. /* empty() */
  129. retval = jsVal->BooleanValue();
  130. /* for PHP compatibility, [] should also be empty */
  131. if (jsVal->IsArray() && retval) {
  132. v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(jsVal);
  133. retval = (array->Length() != 0);
  134. }
  135. /* for PHP compatibility, '0' should also be empty */
  136. if (jsVal->IsString() && retval) {
  137. v8::Local<v8::String> str = jsVal->ToString();
  138. if (str->Length() == 1) {
  139. uint16_t c = 0;
  140. str->Write(&c, 0, 1);
  141. if (c == '0') {
  142. retval = false;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. return retval;
  151. }
  152. /* }}} */
  153. static zval *php_v8js_v8_read_property(zval *object, zval *member, int type ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  154. {
  155. zval *retval = NULL;
  156. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  157. v8::Locker locker(obj->isolate);
  158. v8::Isolate::Scope isolate_scope(obj->isolate);
  159. v8::HandleScope local_scope(obj->isolate);
  160. v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
  161. v8::Context::Scope temp_scope(temp_context);
  162. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
  163. if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction())
  164. {
  165. v8::Local<v8::Object> jsObj = v8obj->ToObject();
  166. v8::Local<v8::String> jsKey = V8JS_STRL(Z_STRVAL_P(member), Z_STRLEN_P(member));
  167. v8::Local<v8::Value> jsVal;
  168. /* Skip any prototype properties */
  169. if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
  170. jsVal = jsObj->Get(jsKey);
  171. if (jsVal->IsObject()) {
  172. ALLOC_INIT_ZVAL(retval);
  173. Z_SET_REFCOUNT_P(retval, 0);
  174. } else {
  175. MAKE_STD_ZVAL(retval);
  176. }
  177. if (v8js_to_zval(jsVal, retval, obj->flags, obj->isolate TSRMLS_CC) == SUCCESS) {
  178. return retval;
  179. }
  180. }
  181. }
  182. ALLOC_INIT_ZVAL(retval);
  183. return retval;
  184. }
  185. /* }}} */
  186. static void php_v8js_v8_write_property(zval *object, zval *member, zval *value ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  187. {
  188. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  189. v8::Locker locker(obj->isolate);
  190. v8::Isolate::Scope isolate_scope(obj->isolate);
  191. v8::HandleScope local_scope(obj->isolate);
  192. v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
  193. v8::Context::Scope temp_scope(temp_context);
  194. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
  195. if (v8obj->IsObject() && !v8obj->IsFunction()) {
  196. v8obj->ToObject()->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, obj->isolate TSRMLS_CC));
  197. }
  198. }
  199. /* }}} */
  200. static void php_v8js_v8_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  201. {
  202. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  203. v8::Locker locker(obj->isolate);
  204. v8::Isolate::Scope isolate_scope(obj->isolate);
  205. v8::HandleScope local_scope(obj->isolate);
  206. v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
  207. v8::Context::Scope temp_scope(temp_context);
  208. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
  209. if (v8obj->IsObject() && !v8obj->IsFunction()) {
  210. v8obj->ToObject()->ForceDelete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
  211. }
  212. }
  213. /* }}} */
  214. int php_v8js_v8_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  215. {
  216. v8::Local<v8::Object> jsObj = jsValue->ToObject();
  217. if (!jsObj.IsEmpty()) {
  218. v8::Local<v8::Array> jsKeys = jsObj->GetPropertyNames();
  219. for (unsigned i = 0; i < jsKeys->Length(); i++)
  220. {
  221. v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString();
  222. /* Skip any prototype properties */
  223. if (!jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey) && !jsObj->HasRealIndexedProperty(i)) {
  224. continue;
  225. }
  226. v8::Local<v8::Value> jsVal = jsObj->Get(jsKey);
  227. v8::String::Utf8Value cstr(jsKey);
  228. const char *key = ToCString(cstr);
  229. zval *value = NULL;
  230. if(jsVal->IsObject()
  231. && !jsVal->IsFunction()
  232. && jsVal->ToObject()->InternalFieldCount() == 2) {
  233. /* This is a PHP object, passed to JS and back. */
  234. value = reinterpret_cast<zval *>(jsVal->ToObject()->GetAlignedPointerFromInternalField(0));
  235. Z_ADDREF_P(value);
  236. }
  237. else {
  238. MAKE_STD_ZVAL(value);
  239. if (v8js_to_zval(jsVal, value, flags, isolate TSRMLS_CC) == FAILURE) {
  240. zval_ptr_dtor(&value);
  241. return FAILURE;
  242. }
  243. }
  244. if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
  245. zend_symtable_update(retval, key, strlen(key) + 1, (void *)&value, sizeof(zval *), NULL);
  246. } else {
  247. zend_hash_update(retval, key, strlen(key) + 1, (void *) &value, sizeof(zval *), NULL);
  248. }
  249. }
  250. return SUCCESS;
  251. }
  252. return FAILURE;
  253. }
  254. /* }}} */
  255. static HashTable *php_v8js_v8_get_properties(zval *object TSRMLS_DC) /* {{{ */
  256. {
  257. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  258. HashTable *retval;
  259. ALLOC_HASHTABLE(retval);
  260. zend_hash_init(retval, 0, NULL, ZVAL_PTR_DTOR, 0);
  261. v8::Locker locker(obj->isolate);
  262. v8::Isolate::Scope isolate_scope(obj->isolate);
  263. v8::HandleScope local_scope(obj->isolate);
  264. v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
  265. v8::Context::Scope temp_scope(temp_context);
  266. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
  267. if (php_v8js_v8_get_properties_hash(v8obj, retval, obj->flags, obj->isolate TSRMLS_CC) == SUCCESS) {
  268. return retval;
  269. }
  270. return NULL;
  271. }
  272. /* }}} */
  273. static HashTable *php_v8js_v8_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
  274. {
  275. *is_temp = 1;
  276. return php_v8js_v8_get_properties(object TSRMLS_CC);
  277. }
  278. /* }}} */
  279. static zend_function *php_v8js_v8_get_method(zval **object_ptr, char *method, int method_len ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  280. {
  281. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(*object_ptr TSRMLS_CC);
  282. zend_function *f;
  283. v8::Locker locker(obj->isolate);
  284. v8::Isolate::Scope isolate_scope(obj->isolate);
  285. v8::HandleScope local_scope(obj->isolate);
  286. v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
  287. v8::Context::Scope temp_scope(temp_context);
  288. v8::Local<v8::String> jsKey = V8JS_STRL(method, method_len);
  289. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
  290. if (!obj->v8obj.IsEmpty() && v8obj->IsObject() && !v8obj->IsFunction()) {
  291. v8::Local<v8::Object> jsObj = v8obj->ToObject();
  292. if (jsObj->Has(jsKey) && jsObj->Get(jsKey)->IsFunction()) {
  293. f = (zend_function *) ecalloc(1, sizeof(*f));
  294. f->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
  295. f->common.function_name = estrndup(method, method_len);
  296. return f;
  297. }
  298. }
  299. return NULL;
  300. }
  301. /* }}} */
  302. #if PHP_VERSION_ID >= 50400
  303. static int php_v8js_v8_call_method(const char *method, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  304. #else
  305. static int php_v8js_v8_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
  306. #endif
  307. {
  308. zval *object = this_ptr, ***argv = NULL;
  309. int i = 0, argc = ZEND_NUM_ARGS();
  310. php_v8js_object *obj;
  311. obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  312. if (obj->v8obj.IsEmpty()) {
  313. zval_ptr_dtor(&object);
  314. return FAILURE;
  315. }
  316. if (argc > 0) {
  317. argv = (zval***)safe_emalloc(sizeof(zval**), argc, 0);
  318. zend_get_parameters_array_ex(argc, argv);
  319. }
  320. v8::Locker locker(obj->isolate);
  321. v8::Isolate::Scope isolate_scope(obj->isolate);
  322. v8::HandleScope local_scope(obj->isolate);
  323. v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
  324. v8::Context::Scope temp_scope(temp_context);
  325. v8::Local<v8::String> method_name = V8JS_SYML(method, strlen(method));
  326. v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj)->ToObject();
  327. v8::Local<v8::Function> cb;
  328. if (method_name->Equals(V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME))) {
  329. cb = v8::Local<v8::Function>::Cast(v8obj);
  330. } else {
  331. cb = v8::Local<v8::Function>::Cast(v8obj->Get(method_name));
  332. }
  333. v8::Local<v8::Value> *jsArgv = new v8::Local<v8::Value>[argc];
  334. v8::Local<v8::Value> js_retval;
  335. for (i = 0; i < argc; i++) {
  336. jsArgv[i] = v8::Local<v8::Value>::New(obj->isolate, zval_to_v8js(*argv[i], obj->isolate TSRMLS_CC));
  337. }
  338. js_retval = cb->Call(V8JS_GLOBAL, argc, jsArgv);
  339. zval_ptr_dtor(&object);
  340. if (argc > 0) {
  341. efree(argv);
  342. }
  343. if (return_value_used) {
  344. return v8js_to_zval(js_retval, return_value, obj->flags, obj->isolate TSRMLS_CC);
  345. }
  346. return SUCCESS;
  347. }
  348. /* }}} */
  349. static int php_v8js_v8_get_closure(zval *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC) /* {{{ */
  350. {
  351. zend_function *invoke;
  352. php_v8js_object *obj = (php_v8js_object *) zend_object_store_get_object(object TSRMLS_CC);
  353. v8::Locker locker(obj->isolate);
  354. v8::Isolate::Scope isolate_scope(obj->isolate);
  355. v8::HandleScope local_scope(obj->isolate);
  356. v8::Local<v8::Context> temp_context = v8::Context::New(obj->isolate);
  357. v8::Context::Scope temp_scope(temp_context);
  358. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(obj->isolate, obj->v8obj);
  359. if (!v8obj->IsFunction()) {
  360. return FAILURE;
  361. }
  362. invoke = (zend_function *) ecalloc(1, sizeof(*invoke));
  363. invoke->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
  364. invoke->common.function_name = estrndup(V8JS_V8_INVOKE_FUNC_NAME, sizeof(V8JS_V8_INVOKE_FUNC_NAME) - 1);
  365. *fptr_ptr = invoke;
  366. if (zobj_ptr) {
  367. *zobj_ptr = object;
  368. }
  369. *ce_ptr = NULL;
  370. return SUCCESS;
  371. }
  372. /* }}} */
  373. static void php_v8js_v8_free_storage(void *object, zend_object_handle handle TSRMLS_DC) /* {{{ */
  374. {
  375. php_v8js_object *c = (php_v8js_object *) object;
  376. zend_object_std_dtor(&c->std TSRMLS_CC);
  377. if (!c->v8obj.IsEmpty()) {
  378. c->v8obj.Dispose();
  379. }
  380. efree(object);
  381. }
  382. /* }}} */
  383. static zend_object_value php_v8js_v8_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  384. {
  385. zend_object_value retval;
  386. php_v8js_object *c;
  387. c = (php_v8js_object *) ecalloc(1, sizeof(*c));
  388. zend_object_std_init(&c->std, ce TSRMLS_CC);
  389. retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) php_v8js_v8_free_storage, NULL TSRMLS_CC);
  390. retval.handlers = &v8_object_handlers;
  391. return retval;
  392. }
  393. /* }}} */
  394. void php_v8js_create_v8(zval *res, v8::Handle<v8::Value> value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  395. {
  396. php_v8js_object *c;
  397. object_init_ex(res, value->IsFunction() ? php_ce_v8_function : php_ce_v8_object);
  398. c = (php_v8js_object *) zend_object_store_get_object(res TSRMLS_CC);
  399. c->v8obj.Reset(isolate, value);
  400. c->flags = flags;
  401. c->isolate = isolate;
  402. }
  403. /* }}} */
  404. /* }}} V8 */
  405. /* {{{ Class: V8Js */
  406. static void php_v8js_free_storage(void *object TSRMLS_DC) /* {{{ */
  407. {
  408. php_v8js_ctx *c = (php_v8js_ctx *) object;
  409. zend_object_std_dtor(&c->std TSRMLS_CC);
  410. if (c->pending_exception) {
  411. zval_ptr_dtor(&c->pending_exception);
  412. }
  413. c->object_name.Dispose();
  414. /* Clear global object, dispose context */
  415. if (!c->context.IsEmpty()) {
  416. c->context.Dispose();
  417. c->context.Clear();
  418. V8JSG(disposed_contexts) = v8::V8::ContextDisposedNotification();
  419. #if V8JS_DEBUG
  420. fprintf(stderr, "Context dispose notification sent (%d)\n", V8JSG(disposed_contexts));
  421. fflush(stderr);
  422. #endif
  423. }
  424. efree(object);
  425. }
  426. /* }}} */
  427. static zend_object_value php_v8js_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  428. {
  429. zend_object_value retval;
  430. php_v8js_ctx *c;
  431. c = (php_v8js_ctx *) ecalloc(1, sizeof(*c));
  432. zend_object_std_init(&c->std, ce TSRMLS_CC);
  433. #if PHP_VERSION_ID >= 50400
  434. object_properties_init(&c->std, ce);
  435. #else
  436. zval *tmp;
  437. zend_hash_copy(c->std.properties, &ce->default_properties,
  438. (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  439. #endif
  440. retval.handle = zend_objects_store_put(c, NULL, (zend_objects_free_object_storage_t) php_v8js_free_storage, NULL TSRMLS_CC);
  441. retval.handlers = &v8js_object_handlers;
  442. return retval;
  443. }
  444. /* }}} */
  445. static void _php_v8js_free_ext_strarr(const char **arr, int count) /* {{{ */
  446. {
  447. int i;
  448. if (arr) {
  449. for (i = 0; i < count; i++) {
  450. if (arr[i]) {
  451. free((void *) arr[i]);
  452. }
  453. }
  454. free(arr);
  455. }
  456. }
  457. /* }}} */
  458. static void php_v8js_jsext_dtor(php_v8js_jsext *jsext) /* {{{ */
  459. {
  460. if (jsext->deps_ht) {
  461. zend_hash_destroy(jsext->deps_ht);
  462. free(jsext->deps_ht);
  463. }
  464. if (jsext->deps) {
  465. _php_v8js_free_ext_strarr(jsext->deps, jsext->deps_count);
  466. }
  467. free(jsext->name);
  468. free(jsext->source);
  469. }
  470. /* }}} */
  471. static int _php_v8js_create_ext_strarr(const char ***retval, int count, HashTable *ht) /* {{{ */
  472. {
  473. const char **exts = NULL;
  474. HashPosition pos;
  475. zval **tmp;
  476. int i = 0;
  477. exts = (const char **) calloc(1, count * sizeof(char *));
  478. zend_hash_internal_pointer_reset_ex(ht, &pos);
  479. while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &pos) == SUCCESS) {
  480. if (Z_TYPE_PP(tmp) == IS_STRING) {
  481. exts[i++] = zend_strndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
  482. } else {
  483. _php_v8js_free_ext_strarr(exts, i);
  484. return FAILURE;
  485. }
  486. zend_hash_move_forward_ex(ht, &pos);
  487. }
  488. *retval = exts;
  489. return SUCCESS;
  490. }
  491. /* }}} */
  492. static void php_v8js_fatal_error_handler(const char *location, const char *message) /* {{{ */
  493. {
  494. if (location) {
  495. zend_error(E_ERROR, "%s %s", location, message);
  496. } else {
  497. zend_error(E_ERROR, "%s", message);
  498. }
  499. }
  500. /* }}} */
  501. #ifdef ENABLE_DEBUGGER_SUPPORT
  502. static void DispatchDebugMessages() { /* {{{ */
  503. if(v8js_debug_context == NULL) {
  504. return;
  505. }
  506. v8::Isolate* isolate = v8js_debug_context->isolate;
  507. v8::Isolate::Scope isolate_scope(isolate);
  508. v8::HandleScope handle_scope(isolate);
  509. v8::Local<v8::Context> context =
  510. v8::Local<v8::Context>::New(isolate, v8js_debug_context->context);
  511. v8::Context::Scope scope(context);
  512. v8::Debug::ProcessDebugMessages();
  513. }
  514. /* }}} */
  515. #endif /* ENABLE_DEBUGGER_SUPPORT */
  516. static void php_v8js_init(TSRMLS_D) /* {{{ */
  517. {
  518. /* Run only once */
  519. if (V8JSG(v8_initialized)) {
  520. return;
  521. }
  522. /* Set V8 command line flags (must be done before V8::Initialize()!) */
  523. if (V8JSG(v8_flags)) {
  524. v8::V8::SetFlagsFromString(V8JSG(v8_flags), strlen(V8JSG(v8_flags)));
  525. }
  526. /* Initialize V8 */
  527. v8::V8::Initialize();
  528. /* Run only once */
  529. V8JSG(v8_initialized) = 1;
  530. }
  531. /* }}} */
  532. /* {{{ proto void V8Js::__construct([string object_name [, array variables [, array extensions [, bool report_uncaught_exceptions]]])
  533. __construct for V8Js */
  534. static PHP_METHOD(V8Js, __construct)
  535. {
  536. char *object_name = NULL, *class_name = NULL;
  537. int object_name_len = 0, free = 0;
  538. zend_uint class_name_len = 0;
  539. zend_bool report_uncaught = 1;
  540. zval *vars_arr = NULL, *exts_arr = NULL;
  541. const char **exts = NULL;
  542. int exts_count = 0;
  543. php_v8js_ctx *c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  544. if (!c->context.IsEmpty()) {
  545. /* called __construct() twice, bail out */
  546. return;
  547. }
  548. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|saab", &object_name, &object_name_len, &vars_arr, &exts_arr, &report_uncaught) == FAILURE) {
  549. return;
  550. }
  551. /* Initialize V8 */
  552. php_v8js_init(TSRMLS_C);
  553. /* Throw PHP exception if uncaught exceptions exist */
  554. c->report_uncaught = report_uncaught;
  555. c->pending_exception = NULL;
  556. c->in_execution = 0;
  557. c->isolate = v8::Isolate::New();
  558. c->isolate->SetData(c);
  559. c->time_limit_hit = false;
  560. c->memory_limit_hit = false;
  561. c->module_loader = NULL;
  562. /* Include extensions used by this context */
  563. /* Note: Extensions registered with auto_enable do not need to be added separately like this. */
  564. if (exts_arr)
  565. {
  566. exts_count = zend_hash_num_elements(Z_ARRVAL_P(exts_arr));
  567. if (_php_v8js_create_ext_strarr(&exts, exts_count, Z_ARRVAL_P(exts_arr)) == FAILURE) {
  568. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid extensions array passed");
  569. return;
  570. }
  571. }
  572. /* Declare configuration for extensions */
  573. v8::ExtensionConfiguration extension_conf(exts_count, exts);
  574. // Isolate execution
  575. v8::Locker locker(c->isolate);
  576. v8::Isolate::Scope isolate_scope(c->isolate);
  577. /* Handle scope */
  578. v8::HandleScope handle_scope(c->isolate);
  579. /* Redirect fatal errors to PHP error handler */
  580. // This needs to be done within the context isolate
  581. v8::V8::SetFatalErrorHandler(php_v8js_fatal_error_handler);
  582. /* Create global template for global object */
  583. // Now we are using multiple isolates this needs to be created for every context
  584. v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New();
  585. tpl->SetClassName(V8JS_SYM("V8Js"));
  586. c->global_template.Reset(c->isolate, tpl);
  587. /* Register builtin methods */
  588. php_v8js_register_methods(tpl->InstanceTemplate(), c);
  589. /* Create context */
  590. v8::Local<v8::Context> context = v8::Context::New(c->isolate, &extension_conf, tpl->InstanceTemplate());
  591. context->SetAlignedPointerInEmbedderData(1, c);
  592. c->context.Reset(c->isolate, context);
  593. if (exts) {
  594. _php_v8js_free_ext_strarr(exts, exts_count);
  595. }
  596. /* If extensions have errors, context will be empty. (NOTE: This is V8 stuff, they expect the passed sources to compile :) */
  597. if (c->context.IsEmpty()) {
  598. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create V8 context. Check that registered extensions do not have errors.");
  599. ZVAL_NULL(getThis());
  600. return;
  601. }
  602. /* Enter context */
  603. v8::Context::Scope context_scope(context);
  604. /* Create the PHP container object's function template */
  605. v8::Local<v8::FunctionTemplate> php_obj_t = v8::FunctionTemplate::New();
  606. /* Set class name for PHP object */
  607. #if PHP_VERSION_ID >= 50400
  608. free = !zend_get_object_classname(getThis(), const_cast<const char**>(&class_name), &class_name_len TSRMLS_CC);
  609. #else
  610. free = !zend_get_object_classname(getThis(), &class_name, &class_name_len TSRMLS_CC);
  611. #endif
  612. php_obj_t->SetClassName(V8JS_SYML(class_name, class_name_len));
  613. if (free) {
  614. efree(class_name);
  615. }
  616. /* Register Get accessor for passed variables */
  617. if (vars_arr && zend_hash_num_elements(Z_ARRVAL_P(vars_arr)) > 0) {
  618. php_v8js_register_accessors(php_obj_t->InstanceTemplate(), vars_arr, c->isolate TSRMLS_CC);
  619. }
  620. /* Set name for the PHP JS object */
  621. v8::Local<v8::String> object_name_js = (object_name_len) ? V8JS_SYML(object_name, object_name_len) : V8JS_SYM("PHP");
  622. c->object_name.Reset(c->isolate, object_name_js);
  623. /* Add the PHP object into global object */
  624. v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance();
  625. V8JS_GLOBAL->Set(object_name_js, php_obj, v8::ReadOnly);
  626. /* Export public property values */
  627. HashTable *properties = zend_std_get_properties(getThis() TSRMLS_CC);
  628. HashPosition pos;
  629. zval **value;
  630. ulong index;
  631. char *member;
  632. uint member_len;
  633. for(zend_hash_internal_pointer_reset_ex(properties, &pos);
  634. zend_hash_get_current_data_ex(properties, (void **) &value, &pos) == SUCCESS;
  635. zend_hash_move_forward_ex(properties, &pos)) {
  636. if(zend_hash_get_current_key_ex(properties, &member, &member_len, &index, 0, &pos) != HASH_KEY_IS_STRING) {
  637. continue;
  638. }
  639. zval zmember;
  640. ZVAL_STRING(&zmember, member, 0);
  641. zend_property_info *property_info = zend_get_property_info(c->std.ce, &zmember, 1 TSRMLS_CC);
  642. if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
  643. /* Write value to PHP JS object */
  644. php_obj->ForceSet(V8JS_SYML(member, member_len - 1), zval_to_v8js(*value, c->isolate TSRMLS_CC), v8::ReadOnly);
  645. }
  646. }
  647. }
  648. /* }}} */
  649. #define V8JS_BEGIN_CTX(ctx, object) \
  650. php_v8js_ctx *(ctx); \
  651. \
  652. if (!V8JSG(v8_initialized)) { \
  653. zend_error(E_ERROR, "V8 not initialized"); \
  654. return; \
  655. } \
  656. \
  657. (ctx) = (php_v8js_ctx *) zend_object_store_get_object(object TSRMLS_CC); \
  658. v8::Locker locker((ctx)->isolate); \
  659. v8::Isolate::Scope isolate_scope((ctx)->isolate); \
  660. v8::HandleScope handle_scope((ctx)->isolate); \
  661. v8::Context::Scope context_scope((ctx)->isolate, (ctx)->context);
  662. static void php_v8js_timer_push(long time_limit, long memory_limit, php_v8js_ctx *c TSRMLS_DC)
  663. {
  664. V8JSG(timer_mutex).lock();
  665. // Create context for this timer
  666. php_v8js_timer_ctx *timer_ctx = (php_v8js_timer_ctx *)emalloc(sizeof(php_v8js_timer_ctx));
  667. // Calculate the time point when the time limit is exceeded
  668. std::chrono::milliseconds duration(time_limit);
  669. std::chrono::time_point<std::chrono::high_resolution_clock> from = std::chrono::high_resolution_clock::now();
  670. // Push the timer context
  671. timer_ctx->time_limit = time_limit;
  672. timer_ctx->memory_limit = memory_limit;
  673. timer_ctx->time_point = from + duration;
  674. timer_ctx->v8js_ctx = c;
  675. V8JSG(timer_stack).push(timer_ctx);
  676. V8JSG(timer_mutex).unlock();
  677. }
  678. static void php_v8js_timer_pop(TSRMLS_D)
  679. {
  680. V8JSG(timer_mutex).lock();
  681. if (V8JSG(timer_stack).size()) {
  682. // Free the timer context memory
  683. php_v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).top();
  684. efree(timer_ctx);
  685. // Remove the timer context from the stack
  686. V8JSG(timer_stack).pop();
  687. }
  688. V8JSG(timer_mutex).unlock();
  689. }
  690. static void php_v8js_terminate_execution(php_v8js_ctx *c TSRMLS_DC)
  691. {
  692. // Forcefully terminate the current thread of V8 execution in the isolate
  693. v8::V8::TerminateExecution(c->isolate);
  694. // Remove this timer from the stack
  695. php_v8js_timer_pop(TSRMLS_C);
  696. }
  697. static void php_v8js_timer_thread(TSRMLS_D)
  698. {
  699. while (!V8JSG(timer_stop)) {
  700. std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
  701. v8::HeapStatistics hs;
  702. if (V8JSG(timer_stack).size()) {
  703. // Get the current timer context
  704. php_v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).top();
  705. php_v8js_ctx *c = timer_ctx->v8js_ctx;
  706. // Get memory usage statistics for the isolate
  707. c->isolate->GetHeapStatistics(&hs);
  708. if (timer_ctx->time_limit > 0 && now > timer_ctx->time_point) {
  709. php_v8js_terminate_execution(c TSRMLS_CC);
  710. V8JSG(timer_mutex).lock();
  711. c->time_limit_hit = true;
  712. V8JSG(timer_mutex).unlock();
  713. }
  714. if (timer_ctx->memory_limit > 0 && hs.used_heap_size() > timer_ctx->memory_limit) {
  715. php_v8js_terminate_execution(c TSRMLS_CC);
  716. V8JSG(timer_mutex).lock();
  717. c->memory_limit_hit = true;
  718. V8JSG(timer_mutex).unlock();
  719. }
  720. }
  721. // Sleep for 10ms
  722. std::chrono::milliseconds duration(10);
  723. std::this_thread::sleep_for(duration);
  724. }
  725. }
  726. /* {{{ proto mixed V8Js::executeString(string script [, string identifier [, int flags]])
  727. */
  728. static PHP_METHOD(V8Js, executeString)
  729. {
  730. char *str = NULL, *identifier = NULL;
  731. int str_len = 0, identifier_len = 0;
  732. long flags = V8JS_FLAG_NONE, time_limit = 0, memory_limit = 0;
  733. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slll", &str, &str_len, &identifier, &identifier_len, &flags, &time_limit, &memory_limit) == FAILURE) {
  734. return;
  735. }
  736. V8JS_BEGIN_CTX(c, getThis())
  737. V8JSG(timer_mutex).lock();
  738. c->time_limit_hit = false;
  739. c->memory_limit_hit = false;
  740. V8JSG(timer_mutex).unlock();
  741. /* Catch JS exceptions */
  742. v8::TryCatch try_catch;
  743. /* Set script identifier */
  744. v8::Local<v8::String> sname = identifier_len ? V8JS_SYML(identifier, identifier_len) : V8JS_SYM("V8Js::executeString()");
  745. /* Compiles a string context independently. TODO: Add a php function which calls this and returns the result as resource which can be executed later. */
  746. v8::Local<v8::String> source = v8::String::New(str, str_len);
  747. v8::Local<v8::Script> script = v8::Script::New(source, sname);
  748. /* Compile errors? */
  749. if (script.IsEmpty()) {
  750. php_v8js_throw_script_exception(&try_catch TSRMLS_CC);
  751. return;
  752. }
  753. /* Set flags for runtime use */
  754. V8JS_GLOBAL_SET_FLAGS(flags);
  755. if (time_limit > 0 || memory_limit > 0) {
  756. // If timer thread is not running then start it
  757. if (!V8JSG(timer_thread)) {
  758. // If not, start timer thread
  759. V8JSG(timer_thread) = new std::thread(php_v8js_timer_thread TSRMLS_CC);
  760. }
  761. php_v8js_timer_push(time_limit, memory_limit, c TSRMLS_CC);
  762. }
  763. #ifdef ENABLE_DEBUGGER_SUPPORT
  764. if(c == v8js_debug_context && v8js_debug_auto_break_mode != V8JS_DEBUG_AUTO_BREAK_NEVER) {
  765. v8::Debug::DebugBreak(c->isolate);
  766. if(v8js_debug_auto_break_mode == V8JS_DEBUG_AUTO_BREAK_ONCE) {
  767. /* If break-once-mode was enabled, reset flag. */
  768. v8js_debug_auto_break_mode = V8JS_DEBUG_AUTO_BREAK_NEVER;
  769. }
  770. }
  771. #endif /* ENABLE_DEBUGGER_SUPPORT */
  772. /* Execute script */
  773. c->in_execution++;
  774. v8::Local<v8::Value> result = script->Run();
  775. c->in_execution--;
  776. if (time_limit > 0) {
  777. php_v8js_timer_pop(TSRMLS_C);
  778. }
  779. char exception_string[64];
  780. if (c->time_limit_hit) {
  781. // Execution has been terminated due to time limit
  782. sprintf(exception_string, "Script time limit of %lu milliseconds exceeded", time_limit);
  783. zend_throw_exception(php_ce_v8js_time_limit_exception, exception_string, 0 TSRMLS_CC);
  784. return;
  785. }
  786. if (c->memory_limit_hit) {
  787. // Execution has been terminated due to memory limit
  788. sprintf(exception_string, "Script memory limit of %lu bytes exceeded", memory_limit);
  789. zend_throw_exception(php_ce_v8js_memory_limit_exception, exception_string, 0 TSRMLS_CC);
  790. return;
  791. }
  792. if (!try_catch.CanContinue()) {
  793. // At this point we can't re-throw the exception
  794. return;
  795. }
  796. /* There was pending exception left from earlier executions -> throw to PHP */
  797. if (c->pending_exception) {
  798. zend_throw_exception_object(c->pending_exception TSRMLS_CC);
  799. c->pending_exception = NULL;
  800. }
  801. /* Handle runtime JS exceptions */
  802. if (try_catch.HasCaught()) {
  803. /* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
  804. if (c->in_execution < 1) {
  805. /* Report immediately if report_uncaught is true */
  806. if (c->report_uncaught) {
  807. php_v8js_throw_script_exception(&try_catch TSRMLS_CC);
  808. return;
  809. }
  810. /* Exception thrown from JS, preserve it for future execution */
  811. if (result.IsEmpty()) {
  812. MAKE_STD_ZVAL(c->pending_exception);
  813. php_v8js_create_script_exception(c->pending_exception, &try_catch TSRMLS_CC);
  814. }
  815. }
  816. /* Rethrow back to JS */
  817. try_catch.ReThrow();
  818. return;
  819. }
  820. /* Convert V8 value to PHP value */
  821. if (!result.IsEmpty()) {
  822. v8js_to_zval(result, return_value, flags, c->isolate TSRMLS_CC);
  823. }
  824. }
  825. /* }}} */
  826. #ifdef ENABLE_DEBUGGER_SUPPORT
  827. /* {{{ proto void V8Js::__destruct()
  828. __destruct for V8Js */
  829. static PHP_METHOD(V8Js, __destruct)
  830. {
  831. V8JS_BEGIN_CTX(c, getThis());
  832. if(v8js_debug_context == c) {
  833. v8::Debug::DisableAgent();
  834. v8js_debug_context = NULL;
  835. }
  836. }
  837. /* }}} */
  838. /* {{{ proto bool V8Js::startDebugAgent(string agent_name[, int port[, int auto_break]])
  839. */
  840. static PHP_METHOD(V8Js, startDebugAgent)
  841. {
  842. char *str = NULL;
  843. int str_len = 0;
  844. long port = 0, auto_break = 0;
  845. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sll", &str, &str_len, &port, &auto_break) == FAILURE) {
  846. return;
  847. }
  848. if(!port) {
  849. port = 9222;
  850. }
  851. V8JS_BEGIN_CTX(c, getThis());
  852. if(v8js_debug_context == c) {
  853. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Debug agent already started for this V8Js instance");
  854. RETURN_BOOL(0);
  855. }
  856. if(v8js_debug_context != NULL) {
  857. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Debug agent already started for a different V8Js instance");
  858. RETURN_BOOL(0);
  859. }
  860. v8js_debug_context = c;
  861. v8js_debug_auto_break_mode = auto_break;
  862. v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true);
  863. v8::Debug::EnableAgent(str_len ? str : "V8Js", port, auto_break > 0);
  864. if(auto_break) {
  865. /* v8::Debug::EnableAgent doesn't really do what we want it to do,
  866. since it only breaks processing on the default isolate.
  867. Hence just trigger another DebugBreak, no for our main isolate. */
  868. v8::Debug::DebugBreak(c->isolate);
  869. }
  870. RETURN_BOOL(1);
  871. }
  872. /* }}} */
  873. #endif /* ENABLE_DEBUGGER_SUPPORT */
  874. /* {{{ proto mixed V8Js::getPendingException()
  875. */
  876. static PHP_METHOD(V8Js, getPendingException)
  877. {
  878. php_v8js_ctx *c;
  879. if (zend_parse_parameters_none() == FAILURE) {
  880. return;
  881. }
  882. c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  883. if (c->pending_exception) {
  884. RETURN_ZVAL(c->pending_exception, 1, 0);
  885. }
  886. }
  887. /* }}} */
  888. /* {{{ proto void V8Js::setModuleLoader(string module)
  889. */
  890. static PHP_METHOD(V8Js, setModuleLoader)
  891. {
  892. php_v8js_ctx *c;
  893. zval *callable;
  894. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &callable) == FAILURE) {
  895. return;
  896. }
  897. c = (php_v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  898. c->module_loader = callable;
  899. Z_ADDREF_P(c->module_loader);
  900. }
  901. /* }}} */
  902. static void php_v8js_persistent_zval_ctor(zval **p) /* {{{ */
  903. {
  904. zval *orig_ptr = *p;
  905. *p = (zval *) malloc(sizeof(zval));
  906. **p = *orig_ptr;
  907. switch (Z_TYPE_P(*p)) {
  908. case IS_STRING:
  909. Z_STRVAL_P(*p) = (char *) zend_strndup(Z_STRVAL_P(*p), Z_STRLEN_P(*p));
  910. break;
  911. default:
  912. zend_bailout();
  913. }
  914. INIT_PZVAL(*p);
  915. }
  916. /* }}} */
  917. static void php_v8js_persistent_zval_dtor(zval **p) /* {{{ */
  918. {
  919. switch (Z_TYPE_P(*p)) {
  920. case IS_STRING:
  921. free(Z_STRVAL_P(*p));
  922. break;
  923. default:
  924. zend_bailout();
  925. }
  926. free(*p);
  927. }
  928. /* }}} */
  929. 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) /* {{{ */
  930. {
  931. php_v8js_jsext *jsext = NULL;
  932. if (!V8JSG(extensions)) {
  933. V8JSG(extensions) = (HashTable *) malloc(sizeof(HashTable));
  934. zend_hash_init(V8JSG(extensions), 1, NULL, (dtor_func_t) php_v8js_jsext_dtor, 1);
  935. } else if (zend_hash_exists(V8JSG(extensions), name, name_len + 1)) {
  936. return FAILURE;
  937. }
  938. jsext = (php_v8js_jsext *) calloc(1, sizeof(php_v8js_jsext));
  939. if (deps_arr) {
  940. jsext->deps_count = zend_hash_num_elements(Z_ARRVAL_P(deps_arr));
  941. if (_php_v8js_create_ext_strarr(&jsext->deps, jsext->deps_count, Z_ARRVAL_P(deps_arr)) == FAILURE) {
  942. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid dependancy array passed");
  943. php_v8js_jsext_dtor(jsext);
  944. free(jsext);
  945. return FAILURE;
  946. }
  947. }
  948. jsext->auto_enable = auto_enable;
  949. jsext->name = zend_strndup(name, name_len);
  950. jsext->source = zend_strndup(source, source_len);
  951. if (jsext->deps) {
  952. jsext->deps_ht = (HashTable *) malloc(sizeof(HashTable));
  953. zend_hash_init(jsext->deps_ht, jsext->deps_count, NULL, (dtor_func_t) php_v8js_persistent_zval_dtor, 1);
  954. zend_hash_copy(jsext->deps_ht, Z_ARRVAL_P(deps_arr), (copy_ctor_func_t) php_v8js_persistent_zval_ctor, NULL, sizeof(zval *));
  955. }
  956. if (zend_hash_add(V8JSG(extensions), name, name_len + 1, jsext, sizeof(php_v8js_jsext), NULL) == FAILURE) {
  957. php_v8js_jsext_dtor(jsext);
  958. free(jsext);
  959. return FAILURE;
  960. }
  961. v8::Extension *extension = new v8::Extension(jsext->name, jsext->source, jsext->deps_count, jsext->deps);
  962. extension->set_auto_enable(auto_enable ? true : false);
  963. v8::RegisterExtension(extension);
  964. free(jsext);
  965. return SUCCESS;
  966. }
  967. /* }}} */
  968. /* {{{ proto bool V8Js::registerExtension(string ext_name, string script [, array deps [, bool auto_enable]])
  969. */
  970. static PHP_METHOD(V8Js, registerExtension)
  971. {
  972. char *ext_name, *script;
  973. zval *deps_arr = NULL;
  974. int ext_name_len, script_len;
  975. zend_bool auto_enable = 0;
  976. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ab", &ext_name, &ext_name_len, &script, &script_len, &deps_arr, &auto_enable) == FAILURE) {
  977. return;
  978. }
  979. if (!ext_name_len) {
  980. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Extension name cannot be empty");
  981. } else if (!script_len) {
  982. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Script cannot be empty");
  983. } else if (php_v8js_register_extension(ext_name, ext_name_len, script, script_len, deps_arr, auto_enable TSRMLS_CC) == SUCCESS) {
  984. RETURN_TRUE;
  985. }
  986. RETURN_FALSE;
  987. }
  988. /* }}} */
  989. /* ## Static methods ## */
  990. /* {{{ proto array V8Js::getExtensions()
  991. */
  992. static PHP_METHOD(V8Js, getExtensions)
  993. {
  994. php_v8js_jsext *jsext;
  995. zval *ext, *deps_arr;
  996. HashPosition pos;
  997. ulong index;
  998. char *key;
  999. uint key_len;
  1000. if (zend_parse_parameters_none() == FAILURE) {
  1001. return;
  1002. }
  1003. array_init(return_value);
  1004. if (V8JSG(extensions)) {
  1005. zend_hash_internal_pointer_reset_ex(V8JSG(extensions), &pos);
  1006. while (zend_hash_get_current_data_ex(V8JSG(extensions), (void **) &jsext, &pos) == SUCCESS) {
  1007. if (zend_hash_get_current_key_ex(V8JSG(extensions), &key, &key_len, &index, 0, &pos) == HASH_KEY_IS_STRING) {
  1008. MAKE_STD_ZVAL(ext)
  1009. array_init(ext);
  1010. add_assoc_bool_ex(ext, ZEND_STRS("auto_enable"), jsext->auto_enable);
  1011. if (jsext->deps_ht) {
  1012. MAKE_STD_ZVAL(deps_arr);
  1013. array_init(deps_arr);
  1014. zend_hash_copy(Z_ARRVAL_P(deps_arr), jsext->deps_ht, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
  1015. add_assoc_zval_ex(ext, ZEND_STRS("deps"), deps_arr);
  1016. }
  1017. add_assoc_zval_ex(return_value, key, key_len, ext);
  1018. }
  1019. zend_hash_move_forward_ex(V8JSG(extensions), &pos);
  1020. }
  1021. }
  1022. }
  1023. /* }}} */
  1024. /* {{{ arginfo */
  1025. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_construct, 0, 0, 0)
  1026. ZEND_ARG_INFO(0, object_name)
  1027. ZEND_ARG_INFO(0, variables)
  1028. ZEND_ARG_INFO(0, extensions)
  1029. ZEND_ARG_INFO(0, report_uncaught_exceptions)
  1030. ZEND_END_ARG_INFO()
  1031. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_executestring, 0, 0, 1)
  1032. ZEND_ARG_INFO(0, script)
  1033. ZEND_ARG_INFO(0, identifier)
  1034. ZEND_ARG_INFO(0, flags)
  1035. ZEND_ARG_INFO(0, time_limit)
  1036. ZEND_ARG_INFO(0, memory_limit)
  1037. ZEND_END_ARG_INFO()
  1038. #ifdef ENABLE_DEBUGGER_SUPPORT
  1039. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_destruct, 0, 0, 0)
  1040. ZEND_END_ARG_INFO()
  1041. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_startdebugagent, 0, 0, 0)
  1042. ZEND_ARG_INFO(0, agentName)
  1043. ZEND_ARG_INFO(0, port)
  1044. ZEND_ARG_INFO(0, auto_break)
  1045. ZEND_END_ARG_INFO()
  1046. #endif /* ENABLE_DEBUGGER_SUPPORT */
  1047. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getpendingexception, 0)
  1048. ZEND_END_ARG_INFO()
  1049. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmoduleloader, 0, 0, 1)
  1050. ZEND_ARG_INFO(0, callable)
  1051. ZEND_END_ARG_INFO()
  1052. ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_registerextension, 0, 0, 2)
  1053. ZEND_ARG_INFO(0, extension_name)
  1054. ZEND_ARG_INFO(0, script)
  1055. ZEND_ARG_INFO(0, dependencies)
  1056. ZEND_ARG_INFO(0, auto_enable)
  1057. ZEND_END_ARG_INFO()
  1058. ZEND_BEGIN_ARG_INFO(arginfo_v8js_getextensions, 0)
  1059. ZEND_END_ARG_INFO()
  1060. ZEND_BEGIN_ARG_INFO(arginfo_v8jsscriptexception_no_args, 0)
  1061. ZEND_END_ARG_INFO()
  1062. /* }}} */
  1063. static const zend_function_entry v8js_methods[] = { /* {{{ */
  1064. PHP_ME(V8Js, __construct, arginfo_v8js_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  1065. PHP_ME(V8Js, executeString, arginfo_v8js_executestring, ZEND_ACC_PUBLIC)
  1066. PHP_ME(V8Js, getPendingException, arginfo_v8js_getpendingexception, ZEND_ACC_PUBLIC)
  1067. PHP_ME(V8Js, setModuleLoader, arginfo_v8js_setmoduleloader, ZEND_ACC_PUBLIC)
  1068. PHP_ME(V8Js, registerExtension, arginfo_v8js_registerextension, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  1069. PHP_ME(V8Js, getExtensions, arginfo_v8js_getextensions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
  1070. #ifdef ENABLE_DEBUGGER_SUPPORT
  1071. PHP_ME(V8Js, __destruct, arginfo_v8js_destruct, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
  1072. PHP_ME(V8Js, startDebugAgent, arginfo_v8js_startdebugagent, ZEND_ACC_PUBLIC)
  1073. #endif
  1074. {NULL, NULL, NULL}
  1075. };
  1076. /* }}} */
  1077. /* V8Js object handlers */
  1078. static void php_v8js_write_property(zval *object, zval *member, zval *value ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  1079. {
  1080. V8JS_BEGIN_CTX(c, object)
  1081. /* Check whether member is public, if so, export to V8. */
  1082. zend_property_info *property_info = zend_get_property_info(c->std.ce, member, 1 TSRMLS_CC);
  1083. if(property_info->flags & ZEND_ACC_PUBLIC) {
  1084. /* Global PHP JS object */
  1085. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
  1086. v8::Local<v8::Object> jsobj = V8JS_GLOBAL->Get(object_name_js)->ToObject();
  1087. /* Write value to PHP JS object */
  1088. jsobj->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, c->isolate TSRMLS_CC), v8::ReadOnly);
  1089. }
  1090. /* Write value to PHP object */
  1091. std_object_handlers.write_property(object, member, value ZEND_HASH_KEY_CC TSRMLS_CC);
  1092. }
  1093. /* }}} */
  1094. static void php_v8js_unset_property(zval *object, zval *member ZEND_HASH_KEY_DC TSRMLS_DC) /* {{{ */
  1095. {
  1096. V8JS_BEGIN_CTX(c, object)
  1097. /* Global PHP JS object */
  1098. v8::Local<v8::String> object_name_js = v8::Local<v8::String>::New(c->isolate, c->object_name);
  1099. v8::Local<v8::Object> jsobj = V8JS_GLOBAL->Get(object_name_js)->ToObject();
  1100. /* Delete value from PHP JS object */
  1101. jsobj->ForceDelete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
  1102. /* Unset from PHP object */
  1103. std_object_handlers.unset_property(object, member ZEND_HASH_KEY_CC TSRMLS_CC);
  1104. }
  1105. /* }}} */
  1106. /* }}} V8Js */
  1107. /* {{{ Class: V8JsScriptException */
  1108. static void php_v8js_create_script_exception(zval *return_value, v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  1109. {
  1110. v8::String::Utf8Value exception(try_catch->Exception());
  1111. const char *exception_string = ToCString(exception);
  1112. v8::Handle<v8::Message> tc_message = try_catch->Message();
  1113. const char *filename_string, *sourceline_string;
  1114. char *message_string;
  1115. int linenum, message_len;
  1116. object_init_ex(return_value, php_ce_v8js_script_exception);
  1117. #define PHPV8_EXPROP(type, name, value) \
  1118. zend_update_property##type(php_ce_v8js_script_exception, return_value, #name, sizeof(#name) - 1, value TSRMLS_CC);
  1119. if (tc_message.IsEmpty()) {
  1120. message_len = spprintf(&message_string, 0, "%s", exception_string);
  1121. }
  1122. else
  1123. {
  1124. v8::String::Utf8Value filename(tc_message->GetScriptResourceName());
  1125. filename_string = ToCString(filename);
  1126. PHPV8_EXPROP(_string, JsFileName, filename_string);
  1127. v8::String::Utf8Value sourceline(tc_message->GetSourceLine());
  1128. sourceline_string = ToCString(sourceline);
  1129. PHPV8_EXPROP(_string, JsSourceLine, sourceline_string);
  1130. linenum = tc_message->GetLineNumber();
  1131. PHPV8_EXPROP(_long, JsLineNumber, linenum);
  1132. message_len = spprintf(&message_string, 0, "%s:%d: %s", filename_string, linenum, exception_string);
  1133. v8::String::Utf8Value stacktrace(try_catch->StackTrace());
  1134. if (stacktrace.length() > 0) {
  1135. const char* stacktrace_string = ToCString(stacktrace);
  1136. PHPV8_EXPROP(_string, JsTrace, stacktrace_string);
  1137. }
  1138. }
  1139. PHPV8_EXPROP(_string, message, message_string);
  1140. efree(message_string);
  1141. }
  1142. /* }}} */
  1143. static void php_v8js_throw_script_exception(v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  1144. {
  1145. v8::String::Utf8Value exception(try_catch->Exception());
  1146. const char *exception_string = ToCString(exception);
  1147. zval *zexception = NULL;
  1148. if (try_catch->Message().IsEmpty()) {
  1149. zend_throw_exception(php_ce_v8js_script_exception, (char *) exception_string, 0 TSRMLS_CC);
  1150. } else {
  1151. MAKE_STD_ZVAL(zexception);
  1152. php_v8js_create_script_exception(zexception, try_catch TSRMLS_CC);
  1153. zend_throw_exception_object(zexception TSRMLS_CC);
  1154. }
  1155. }
  1156. /* }}} */
  1157. #define V8JS_EXCEPTION_METHOD(property) \
  1158. static PHP_METHOD(V8JsScriptException, get##property) \
  1159. { \
  1160. zval *value; \
  1161. \
  1162. if (zend_parse_parameters_none() == FAILURE) { \
  1163. return; \
  1164. } \
  1165. value = zend_read_property(php_ce_v8js_script_exception, getThis(), #property, sizeof(#property) - 1, 0 TSRMLS_CC); \
  1166. *return_value = *value; \
  1167. zval_copy_ctor(return_value); \
  1168. INIT_PZVAL(return_value); \
  1169. }
  1170. /* {{{ proto string V8JsEScriptxception::getJsFileName()
  1171. */
  1172. V8JS_EXCEPTION_METHOD(JsFileName);
  1173. /* }}} */
  1174. /* {{{ proto string V8JsScriptException::getJsLineNumber()
  1175. */
  1176. V8JS_EXCEPTION_METHOD(JsLineNumber);
  1177. /* }}} */
  1178. /* {{{ proto string V8JsScriptException::getJsSourceLine()
  1179. */
  1180. V8JS_EXCEPTION_METHOD(JsSourceLine);
  1181. /* }}} */
  1182. /* {{{ proto string V8JsScriptException::getJsTrace()
  1183. */
  1184. V8JS_EXCEPTION_METHOD(JsTrace);
  1185. /* }}} */
  1186. static const zend_function_entry v8js_script_exception_methods[] = { /* {{{ */
  1187. PHP_ME(V8JsScriptException, getJsFileName, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1188. PHP_ME(V8JsScriptException, getJsLineNumber, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1189. PHP_ME(V8JsScriptException, getJsSourceLine, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1190. PHP_ME(V8JsScriptException, getJsTrace, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  1191. {NULL, NULL, NULL}
  1192. };
  1193. /* }}} */
  1194. /* }}} V8JsScriptException */
  1195. /* {{{ Class: V8JsTimeLimitException */
  1196. static const zend_function_entry v8js_time_limit_exception_methods[] = { /* {{{ */
  1197. {NULL, NULL, NULL}
  1198. };
  1199. /* }}} */
  1200. /* }}} V8JsTimeLimitException */
  1201. /* {{{ Class: V8JsMemoryLimitException */
  1202. static const zend_function_entry v8js_memory_limit_exception_methods[] = { /* {{{ */
  1203. {NULL, NULL, NULL}
  1204. };
  1205. /* }}} */
  1206. /* }}} V8JsMemoryLimitException */
  1207. /* {{{ PHP_MINIT_FUNCTION
  1208. */
  1209. static PHP_MINIT_FUNCTION(v8js)
  1210. {
  1211. zend_class_entry ce;
  1212. #ifdef ZTS
  1213. std::mutex mutex;
  1214. memcpy(&V8JSG(timer_mutex), &mutex, sizeof(V8JSG(timer_mutex)));
  1215. #endif
  1216. /* V8Object Class */
  1217. INIT_CLASS_ENTRY(ce, "V8Object", NULL);
  1218. php_ce_v8_object = zend_register_internal_class(&ce TSRMLS_CC);
  1219. php_ce_v8_object->ce_flags |= ZEND_ACC_FINAL;
  1220. php_ce_v8_object->create_object = php_v8js_v8_new;
  1221. /* V8Function Class */
  1222. INIT_CLASS_ENTRY(ce, "V8Function", NULL);
  1223. php_ce_v8_function = zend_register_internal_class(&ce TSRMLS_CC);
  1224. php_ce_v8_function->ce_flags |= ZEND_ACC_FINAL;
  1225. php_ce_v8_function->create_object = php_v8js_v8_new;
  1226. /* V8<Object|Function> handlers */
  1227. memcpy(&v8_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1228. v8_object_handlers.clone_obj = NULL;
  1229. v8_object_handlers.cast_object = NULL;
  1230. v8_object_handlers.get_constructor = NULL;
  1231. v8_object_handlers.get_property_ptr_ptr = NULL;
  1232. v8_object_handlers.has_property = php_v8js_v8_has_property;
  1233. v8_object_handlers.read_property = php_v8js_v8_read_property;
  1234. v8_object_handlers.write_property = php_v8js_v8_write_property;
  1235. v8_object_handlers.unset_property = php_v8js_v8_unset_property;
  1236. v8_object_handlers.get_properties = php_v8js_v8_get_properties;
  1237. v8_object_handlers.get_method = php_v8js_v8_get_method;
  1238. v8_object_handlers.call_method = php_v8js_v8_call_method;
  1239. v8_object_handlers.get_debug_info = php_v8js_v8_get_debug_info;
  1240. v8_object_handlers.get_closure = php_v8js_v8_get_closure;
  1241. /* V8Js Class */
  1242. INIT_CLASS_ENTRY(ce, "V8Js", v8js_methods);
  1243. php_ce_v8js = zend_register_internal_class(&ce TSRMLS_CC);
  1244. php_ce_v8js->create_object = php_v8js_new;
  1245. /* V8Js handlers */
  1246. memcpy(&v8js_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  1247. v8js_object_handlers.clone_obj = NULL;
  1248. v8js_object_handlers.write_property = php_v8js_write_property;
  1249. v8js_object_handlers.unset_property = php_v8js_unset_property;
  1250. /* V8Js Class Constants */
  1251. zend_declare_class_constant_string(php_ce_v8js, ZEND_STRL("V8_VERSION"), PHP_V8_VERSION TSRMLS_CC);
  1252. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_NONE"), V8JS_FLAG_NONE TSRMLS_CC);
  1253. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("FLAG_FORCE_ARRAY"), V8JS_FLAG_FORCE_ARRAY TSRMLS_CC);
  1254. #ifdef ENABLE_DEBUGGER_SUPPORT
  1255. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("DEBUG_AUTO_BREAK_NEVER"), V8JS_DEBUG_AUTO_BREAK_NEVER TSRMLS_CC);
  1256. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("DEBUG_AUTO_BREAK_ONCE"), V8JS_DEBUG_AUTO_BREAK_ONCE TSRMLS_CC);
  1257. zend_declare_class_constant_long(php_ce_v8js, ZEND_STRL("DEBUG_AUTO_BREAK_ALWAYS"), V8JS_DEBUG_AUTO_BREAK_ALWAYS TSRMLS_CC);
  1258. #endif
  1259. /* V8JsScriptException Class */
  1260. INIT_CLASS_ENTRY(ce, "V8JsScriptException", v8js_script_exception_methods);
  1261. php_ce_v8js_script_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
  1262. php_ce_v8js_script_exception->ce_flags |= ZEND_ACC_FINAL;
  1263. /* Add custom JS specific properties */
  1264. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsFileName"), ZEND_ACC_PROTECTED TSRMLS_CC);
  1265. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsLineNumber"), ZEND_ACC_PROTECTED TSRMLS_CC);
  1266. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsSourceLine"), ZEND_ACC_PROTECTED TSRMLS_CC);
  1267. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsTrace"), ZEND_ACC_PROTECTED TSRMLS_CC);
  1268. /* V8JsTimeLimitException Class */
  1269. INIT_CLASS_ENTRY(ce, "V8JsTimeLimitException", v8js_time_limit_exception_methods);
  1270. php_ce_v8js_time_limit_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
  1271. php_ce_v8js_time_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  1272. /* V8JsMemoryLimitException Class */
  1273. INIT_CLASS_ENTRY(ce, "V8JsMemoryLimitException", v8js_memory_limit_exception_methods);
  1274. php_ce_v8js_memory_limit_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
  1275. php_ce_v8js_memory_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  1276. REGISTER_INI_ENTRIES();
  1277. return SUCCESS;
  1278. }
  1279. /* }}} */
  1280. static void php_v8js_force_v8_gc(void) /* {{{ */
  1281. {
  1282. #if V8JS_DEBUG
  1283. TSRMLS_FETCH();
  1284. fprintf(stderr, "############ Running V8 Idle notification: disposed/max_disposed %d/%d ############\n", V8JSG(disposed_contexts), V8JSG(max_disposed_contexts));
  1285. fflush(stderr);
  1286. #endif
  1287. while (!v8::V8::IdleNotification()) {
  1288. #if V8JS_DEBUG
  1289. fprintf(stderr, ".");
  1290. fflush(stderr);
  1291. #endif
  1292. }
  1293. #if V8JS_DEBUG
  1294. fprintf(stderr, "\n");
  1295. fflush(stderr);
  1296. #endif
  1297. }
  1298. /* }}} */
  1299. /* {{{ PHP_MSHUTDOWN_FUNCTION
  1300. */
  1301. static PHP_MSHUTDOWN_FUNCTION(v8js)
  1302. {
  1303. UNREGISTER_INI_ENTRIES();
  1304. if (V8JSG(extensions)) {
  1305. zend_hash_destroy(V8JSG(extensions));
  1306. free(V8JSG(extensions));
  1307. V8JSG(extensions) = NULL;
  1308. }
  1309. if (V8JSG(v8_flags)) {
  1310. free(V8JSG(v8_flags));
  1311. V8JSG(v8_flags) = NULL;
  1312. }
  1313. return SUCCESS;
  1314. }
  1315. /* }}} */
  1316. /* {{{ PHP_RSHUTDOWN_FUNCTION
  1317. */
  1318. static PHP_RSHUTDOWN_FUNCTION(v8js)
  1319. {
  1320. // If the timer thread is running then stop it
  1321. if (V8JSG(timer_thread)) {
  1322. V8JSG(timer_stop) = true;
  1323. V8JSG(timer_thread)->join();
  1324. }
  1325. #if V8JS_DEBUG
  1326. v8::HeapStatistics stats;
  1327. v8::V8::GetHeapStatistics(&stats);
  1328. float used = stats.used_heap_size() / 1024.0 / 1024.0;
  1329. float total = stats.total_heap_size() / 1024.0 / 1024.0;
  1330. fprintf(stderr, "### RSHUTDOWN ###\n");
  1331. fprintf(stderr, "############ Heap Used/Total %.2f/%.2f MB ############\n", used, total);
  1332. fflush(stderr);
  1333. #endif
  1334. /* Force V8 to do as much GC as possible */
  1335. if (V8JSG(max_disposed_contexts) && (V8JSG(disposed_contexts) > V8JSG(max_disposed_contexts))) {
  1336. php_v8js_force_v8_gc();
  1337. }
  1338. return SUCCESS;
  1339. }
  1340. /* }}} */
  1341. /* {{{ PHP_MINFO_FUNCTION
  1342. */
  1343. static PHP_MINFO_FUNCTION(v8js)
  1344. {
  1345. php_info_print_table_start();
  1346. php_info_print_table_header(2, "V8 Javascript Engine", "enabled");
  1347. php_info_print_table_header(2, "V8 Engine Compiled Version", PHP_V8_VERSION);
  1348. php_info_print_table_header(2, "V8 Engine Linked Version", v8::V8::GetVersion());
  1349. php_info_print_table_header(2, "Version", V8JS_VERSION);
  1350. php_info_print_table_end();
  1351. DISPLAY_INI_ENTRIES();
  1352. }
  1353. /* }}} */
  1354. /* {{{ PHP_GINIT_FUNCTION
  1355. */
  1356. static PHP_GINIT_FUNCTION(v8js)
  1357. {
  1358. v8js_globals->extensions = NULL;
  1359. v8js_globals->disposed_contexts = 0;
  1360. v8js_globals->v8_initialized = 0;
  1361. v8js_globals->v8_flags = NULL;
  1362. v8js_globals->timer_thread = NULL;
  1363. v8js_globals->timer_stop = false;
  1364. }
  1365. /* }}} */
  1366. /* {{{ v8js_functions[] */
  1367. static const zend_function_entry v8js_functions[] = {
  1368. {NULL, NULL, NULL}
  1369. };
  1370. /* }}} */
  1371. /* {{{ v8js_module_entry
  1372. */
  1373. zend_module_entry v8js_module_entry = {
  1374. STANDARD_MODULE_HEADER_EX,
  1375. NULL,
  1376. NULL,
  1377. "v8js",
  1378. v8js_functions,
  1379. PHP_MINIT(v8js),
  1380. PHP_MSHUTDOWN(v8js),
  1381. NULL,
  1382. PHP_RSHUTDOWN(v8js),
  1383. PHP_MINFO(v8js),
  1384. V8JS_VERSION,
  1385. PHP_MODULE_GLOBALS(v8js),
  1386. PHP_GINIT(v8js),
  1387. NULL,
  1388. NULL,
  1389. STANDARD_MODULE_PROPERTIES_EX
  1390. };
  1391. /* }}} */
  1392. /*
  1393. * Local variables:
  1394. * tab-width: 4
  1395. * c-basic-offset: 4
  1396. * End:
  1397. * vim600: noet sw=4 ts=4 fdm=marker
  1398. * vim<600: noet sw=4 ts=4
  1399. */