v8js_main.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. ZEND_INI_BEGIN() /* {{{ */
  95. ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
  96. ZEND_INI_ENTRY("v8js.icudtl_dat_path", NULL, ZEND_INI_ALL, v8js_OnUpdateIcudatPath)
  97. ZEND_INI_ENTRY("v8js.use_date", "0", ZEND_INI_ALL, v8js_OnUpdateUseDate)
  98. ZEND_INI_ENTRY("v8js.use_array_access", "0", ZEND_INI_ALL, v8js_OnUpdateUseArrayAccess)
  99. ZEND_INI_END()
  100. /* }}} */
  101. /* }}} INI */
  102. #ifdef COMPILE_DL_V8JS
  103. #ifdef ZTS
  104. ZEND_TSRMLS_CACHE_DEFINE();
  105. #endif
  106. ZEND_GET_MODULE(v8js)
  107. #endif
  108. /* {{{ PHP_MINIT_FUNCTION
  109. */
  110. PHP_MINIT_FUNCTION(v8js)
  111. {
  112. PHP_MINIT(v8js_class)(INIT_FUNC_ARGS_PASSTHRU);
  113. PHP_MINIT(v8js_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
  114. PHP_MINIT(v8js_v8object_class)(INIT_FUNC_ARGS_PASSTHRU);
  115. REGISTER_INI_ENTRIES();
  116. return SUCCESS;
  117. }
  118. /* }}} */
  119. /* {{{ PHP_MSHUTDOWN_FUNCTION
  120. */
  121. static PHP_MSHUTDOWN_FUNCTION(v8js)
  122. {
  123. UNREGISTER_INI_ENTRIES();
  124. bool v8_initialized;
  125. #ifdef ZTS
  126. v8_initialized = v8js_process_globals.v8_initialized;
  127. #else
  128. v8_initialized = V8JSG(v8_initialized);
  129. #endif
  130. if(v8_initialized) {
  131. v8::V8::Dispose();
  132. v8::V8::ShutdownPlatform();
  133. // @fixme call virtual destructor somehow
  134. //delete v8js_process_globals.v8_platform;
  135. }
  136. if (v8js_process_globals.v8_flags) {
  137. free(v8js_process_globals.v8_flags);
  138. v8js_process_globals.v8_flags = NULL;
  139. }
  140. if (v8js_process_globals.extensions) {
  141. zend_hash_destroy(v8js_process_globals.extensions);
  142. free(v8js_process_globals.extensions);
  143. v8js_process_globals.extensions = NULL;
  144. }
  145. return SUCCESS;
  146. }
  147. /* }}} */
  148. /* {{{ PHP_RSHUTDOWN_FUNCTION
  149. */
  150. static PHP_RSHUTDOWN_FUNCTION(v8js)
  151. {
  152. // If the timer thread is running then stop it
  153. if (V8JSG(timer_thread)) {
  154. V8JSG(timer_stop) = true;
  155. V8JSG(timer_thread)->join();
  156. V8JSG(timer_stop) = false;
  157. delete V8JSG(timer_thread);
  158. V8JSG(timer_thread) = NULL;
  159. }
  160. V8JSG(fatal_error_abort) = 0;
  161. return SUCCESS;
  162. }
  163. /* }}} */
  164. /* {{{ PHP_MINFO_FUNCTION
  165. */
  166. static PHP_MINFO_FUNCTION(v8js)
  167. {
  168. php_info_print_table_start();
  169. php_info_print_table_header(2, "V8 Javascript Engine", "enabled");
  170. php_info_print_table_header(2, "V8 Engine Compiled Version", PHP_V8_VERSION);
  171. php_info_print_table_header(2, "V8 Engine Linked Version", v8::V8::GetVersion());
  172. php_info_print_table_header(2, "Version", PHP_V8JS_VERSION);
  173. php_info_print_table_end();
  174. DISPLAY_INI_ENTRIES();
  175. }
  176. /* }}} */
  177. /* {{{ PHP_GINIT_FUNCTION
  178. */
  179. static PHP_GINIT_FUNCTION(v8js)
  180. {
  181. #if defined(COMPILE_DL_MYSQLI) && defined(ZTS)
  182. ZEND_TSRMLS_CACHE_UPDATE();
  183. #endif
  184. /*
  185. If ZTS is disabled, the v8js_globals instance is declared right
  186. in the BSS and hence automatically initialized by C++ compiler.
  187. Most of the variables are just zeroed.
  188. If ZTS is enabled however, v8js_globals just points to a freshly
  189. allocated, uninitialized piece of memory, hence we need to
  190. initialize all fields on our own. Likewise on shutdown we have to
  191. run the destructors manually.
  192. */
  193. #ifdef ZTS
  194. v8js_globals->v8_initialized = 0;
  195. v8js_globals->timer_thread = NULL;
  196. v8js_globals->timer_stop = false;
  197. new(&v8js_globals->timer_mutex) std::mutex;
  198. new(&v8js_globals->timer_stack) std::deque<v8js_timer_ctx *>;
  199. v8js_globals->fatal_error_abort = 0;
  200. #endif
  201. }
  202. /* }}} */
  203. /* {{{ PHP_GSHUTDOWN_FUNCTION
  204. */
  205. static PHP_GSHUTDOWN_FUNCTION(v8js)
  206. {
  207. #ifdef ZTS
  208. v8js_globals->timer_stack.~deque();
  209. v8js_globals->timer_mutex.~mutex();
  210. #endif
  211. }
  212. /* }}} */
  213. /* {{{ v8js_functions[] */
  214. static const zend_function_entry v8js_functions[] = {
  215. {NULL, NULL, NULL}
  216. };
  217. /* }}} */
  218. /* {{{ v8js_module_entry
  219. */
  220. zend_module_entry v8js_module_entry = {
  221. STANDARD_MODULE_HEADER_EX,
  222. NULL,
  223. NULL,
  224. "v8js",
  225. v8js_functions,
  226. PHP_MINIT(v8js),
  227. PHP_MSHUTDOWN(v8js),
  228. NULL,
  229. PHP_RSHUTDOWN(v8js),
  230. PHP_MINFO(v8js),
  231. PHP_V8JS_VERSION,
  232. PHP_MODULE_GLOBALS(v8js),
  233. PHP_GINIT(v8js),
  234. PHP_GSHUTDOWN(v8js),
  235. NULL,
  236. STANDARD_MODULE_PROPERTIES_EX
  237. };
  238. /* }}} */
  239. /*
  240. * Local variables:
  241. * tab-width: 4
  242. * c-basic-offset: 4
  243. * indent-tabs-mode: t
  244. * End:
  245. * vim600: noet sw=4 ts=4 fdm=marker
  246. * vim<600: noet sw=4 ts=4
  247. */