v8js_main.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2017 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. | Author: Stefan Siegl <[email protected]> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include "php_v8js_macros.h"
  18. extern "C" {
  19. #include "php_ini.h"
  20. #include "ext/standard/info.h"
  21. #include "ext/standard/php_string.h"
  22. }
  23. #include "v8js_class.h"
  24. #include "v8js_exceptions.h"
  25. #include "v8js_v8object_class.h"
  26. ZEND_DECLARE_MODULE_GLOBALS(v8js)
  27. struct _v8js_process_globals v8js_process_globals;
  28. /* {{{ INI Settings */
  29. static bool v8js_ini_string(char **field, const zend_string *new_value)/* {{{ */
  30. {
  31. bool immutable = false;
  32. #ifdef ZTS
  33. v8js_process_globals.lock.lock();
  34. if(v8js_process_globals.v8_initialized) {
  35. v8js_process_globals.lock.unlock();
  36. immutable = true;
  37. }
  38. v8js_process_globals.lock.unlock();
  39. #else
  40. immutable = V8JSG(v8_initialized);
  41. #endif
  42. if(immutable) {
  43. /* V8 already has been initialized -> cannot be changed anymore */
  44. return FAILURE;
  45. }
  46. if (new_value) {
  47. if (*field) {
  48. free(*field);
  49. *field = NULL;
  50. }
  51. if (!ZSTR_VAL(new_value)[0]) {
  52. return SUCCESS;
  53. }
  54. *field = zend_strndup(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
  55. }
  56. return SUCCESS;
  57. }
  58. /* }}} */
  59. static ZEND_INI_MH(v8js_OnUpdateV8Flags) /* {{{ */
  60. {
  61. return v8js_ini_string(&v8js_process_globals.v8_flags, new_value);
  62. }
  63. /* }}} */
  64. static ZEND_INI_MH(v8js_OnUpdateIcudatPath) /* {{{ */
  65. {
  66. return v8js_ini_string(&v8js_process_globals.icudtl_dat_path, new_value);
  67. }
  68. /* }}} */
  69. static bool v8js_ini_to_bool(const zend_string *new_value) /* {{{ */
  70. {
  71. if (ZSTR_LEN(new_value) == 2 && strcasecmp("on", ZSTR_VAL(new_value)) == 0) {
  72. return true;
  73. } else if (ZSTR_LEN(new_value) == 3 && strcasecmp("yes", ZSTR_VAL(new_value)) == 0) {
  74. return true;
  75. } else if (ZSTR_LEN(new_value) == 4 && strcasecmp("true", ZSTR_VAL(new_value)) == 0) {
  76. return true;
  77. } else {
  78. return 0 != atoi(ZSTR_VAL(new_value));
  79. }
  80. }
  81. /* }}} */
  82. static ZEND_INI_MH(v8js_OnUpdateUseDate) /* {{{ */
  83. {
  84. V8JSG(use_date) = v8js_ini_to_bool(new_value);
  85. return SUCCESS;
  86. }
  87. /* }}} */
  88. static ZEND_INI_MH(v8js_OnUpdateUseArrayAccess) /* {{{ */
  89. {
  90. V8JSG(use_array_access) = v8js_ini_to_bool(new_value);
  91. return SUCCESS;
  92. }
  93. /* }}} */
  94. static ZEND_INI_MH(v8js_OnUpdateCompatExceptions) /* {{{ */
  95. {
  96. V8JSG(compat_php_exceptions) = v8js_ini_to_bool(new_value);
  97. return SUCCESS;
  98. }
  99. /* }}} */
  100. ZEND_INI_BEGIN() /* {{{ */
  101. ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
  102. ZEND_INI_ENTRY("v8js.icudtl_dat_path", NULL, ZEND_INI_ALL, v8js_OnUpdateIcudatPath)
  103. ZEND_INI_ENTRY("v8js.use_date", "0", ZEND_INI_ALL, v8js_OnUpdateUseDate)
  104. ZEND_INI_ENTRY("v8js.use_array_access", "0", ZEND_INI_ALL, v8js_OnUpdateUseArrayAccess)
  105. ZEND_INI_ENTRY("v8js.compat_php_exceptions", "0", ZEND_INI_ALL, v8js_OnUpdateCompatExceptions)
  106. ZEND_INI_END()
  107. /* }}} */
  108. /* }}} INI */
  109. #ifdef COMPILE_DL_V8JS
  110. #ifdef ZTS
  111. ZEND_TSRMLS_CACHE_DEFINE();
  112. #endif
  113. ZEND_GET_MODULE(v8js)
  114. #endif
  115. /* {{{ PHP_MINIT_FUNCTION
  116. */
  117. PHP_MINIT_FUNCTION(v8js)
  118. {
  119. PHP_MINIT(v8js_class)(INIT_FUNC_ARGS_PASSTHRU);
  120. PHP_MINIT(v8js_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
  121. PHP_MINIT(v8js_v8object_class)(INIT_FUNC_ARGS_PASSTHRU);
  122. REGISTER_INI_ENTRIES();
  123. return SUCCESS;
  124. }
  125. /* }}} */
  126. /* {{{ PHP_MSHUTDOWN_FUNCTION
  127. */
  128. static PHP_MSHUTDOWN_FUNCTION(v8js)
  129. {
  130. UNREGISTER_INI_ENTRIES();
  131. bool v8_initialized;
  132. #ifdef ZTS
  133. v8_initialized = v8js_process_globals.v8_initialized;
  134. #else
  135. v8_initialized = V8JSG(v8_initialized);
  136. #endif
  137. if(v8_initialized) {
  138. v8::V8::Dispose();
  139. v8::V8::ShutdownPlatform();
  140. // @fixme call virtual destructor somehow
  141. //delete v8js_process_globals.v8_platform;
  142. }
  143. if (v8js_process_globals.v8_flags) {
  144. free(v8js_process_globals.v8_flags);
  145. v8js_process_globals.v8_flags = NULL;
  146. }
  147. if (v8js_process_globals.extensions) {
  148. zend_hash_destroy(v8js_process_globals.extensions);
  149. free(v8js_process_globals.extensions);
  150. v8js_process_globals.extensions = NULL;
  151. }
  152. return SUCCESS;
  153. }
  154. /* }}} */
  155. /* {{{ PHP_RSHUTDOWN_FUNCTION
  156. */
  157. static PHP_RSHUTDOWN_FUNCTION(v8js)
  158. {
  159. // If the timer thread is running then stop it
  160. if (V8JSG(timer_thread)) {
  161. V8JSG(timer_stop) = true;
  162. V8JSG(timer_thread)->join();
  163. V8JSG(timer_stop) = false;
  164. delete V8JSG(timer_thread);
  165. V8JSG(timer_thread) = NULL;
  166. }
  167. V8JSG(fatal_error_abort) = 0;
  168. return SUCCESS;
  169. }
  170. /* }}} */
  171. /* {{{ PHP_MINFO_FUNCTION
  172. */
  173. static PHP_MINFO_FUNCTION(v8js)
  174. {
  175. php_info_print_table_start();
  176. php_info_print_table_header(2, "V8 Javascript Engine", "enabled");
  177. php_info_print_table_header(2, "V8 Engine Compiled Version", PHP_V8_VERSION);
  178. php_info_print_table_header(2, "V8 Engine Linked Version", v8::V8::GetVersion());
  179. php_info_print_table_header(2, "Version", PHP_V8JS_VERSION);
  180. php_info_print_table_end();
  181. DISPLAY_INI_ENTRIES();
  182. }
  183. /* }}} */
  184. /* {{{ PHP_GINIT_FUNCTION
  185. */
  186. static PHP_GINIT_FUNCTION(v8js)
  187. {
  188. #if defined(COMPILE_DL_MYSQLI) && defined(ZTS)
  189. ZEND_TSRMLS_CACHE_UPDATE();
  190. #endif
  191. /*
  192. If ZTS is disabled, the v8js_globals instance is declared right
  193. in the BSS and hence automatically initialized by C++ compiler.
  194. Most of the variables are just zeroed.
  195. If ZTS is enabled however, v8js_globals just points to a freshly
  196. allocated, uninitialized piece of memory, hence we need to
  197. initialize all fields on our own. Likewise on shutdown we have to
  198. run the destructors manually.
  199. */
  200. #ifdef ZTS
  201. v8js_globals->v8_initialized = 0;
  202. v8js_globals->timer_thread = NULL;
  203. v8js_globals->timer_stop = false;
  204. new(&v8js_globals->timer_mutex) std::mutex;
  205. new(&v8js_globals->timer_stack) std::deque<v8js_timer_ctx *>;
  206. v8js_globals->fatal_error_abort = 0;
  207. #endif
  208. }
  209. /* }}} */
  210. /* {{{ PHP_GSHUTDOWN_FUNCTION
  211. */
  212. static PHP_GSHUTDOWN_FUNCTION(v8js)
  213. {
  214. #ifdef ZTS
  215. v8js_globals->timer_stack.~deque();
  216. v8js_globals->timer_mutex.~mutex();
  217. #endif
  218. }
  219. /* }}} */
  220. /* {{{ v8js_functions[] */
  221. static const zend_function_entry v8js_functions[] = {
  222. {NULL, NULL, NULL}
  223. };
  224. /* }}} */
  225. /* {{{ v8js_module_entry
  226. */
  227. zend_module_entry v8js_module_entry = {
  228. STANDARD_MODULE_HEADER_EX,
  229. NULL,
  230. NULL,
  231. "v8js",
  232. v8js_functions,
  233. PHP_MINIT(v8js),
  234. PHP_MSHUTDOWN(v8js),
  235. NULL,
  236. PHP_RSHUTDOWN(v8js),
  237. PHP_MINFO(v8js),
  238. PHP_V8JS_VERSION,
  239. PHP_MODULE_GLOBALS(v8js),
  240. PHP_GINIT(v8js),
  241. PHP_GSHUTDOWN(v8js),
  242. NULL,
  243. STANDARD_MODULE_PROPERTIES_EX
  244. };
  245. /* }}} */
  246. /*
  247. * Local variables:
  248. * tab-width: 4
  249. * c-basic-offset: 4
  250. * indent-tabs-mode: t
  251. * End:
  252. * vim600: noet sw=4 ts=4 fdm=marker
  253. * vim<600: noet sw=4 ts=4
  254. */