v8js_main.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #if PHP_V8_API_VERSION >= 10000000
  133. v8::V8::DisposePlatform();
  134. #else
  135. v8::V8::ShutdownPlatform();
  136. #endif
  137. // @fixme call virtual destructor somehow
  138. //delete v8js_process_globals.v8_platform;
  139. }
  140. if (v8js_process_globals.v8_flags) {
  141. free(v8js_process_globals.v8_flags);
  142. v8js_process_globals.v8_flags = NULL;
  143. }
  144. if (v8js_process_globals.extensions) {
  145. zend_hash_destroy(v8js_process_globals.extensions);
  146. free(v8js_process_globals.extensions);
  147. v8js_process_globals.extensions = NULL;
  148. }
  149. return SUCCESS;
  150. }
  151. /* }}} */
  152. /* {{{ PHP_RSHUTDOWN_FUNCTION
  153. */
  154. static PHP_RSHUTDOWN_FUNCTION(v8js)
  155. {
  156. // If the timer thread is running then stop it
  157. if (V8JSG(timer_thread)) {
  158. V8JSG(timer_stop) = true;
  159. V8JSG(timer_thread)->join();
  160. V8JSG(timer_stop) = false;
  161. delete V8JSG(timer_thread);
  162. V8JSG(timer_thread) = NULL;
  163. }
  164. V8JSG(fatal_error_abort) = 0;
  165. return SUCCESS;
  166. }
  167. /* }}} */
  168. /* {{{ PHP_MINFO_FUNCTION
  169. */
  170. static PHP_MINFO_FUNCTION(v8js)
  171. {
  172. php_info_print_table_start();
  173. php_info_print_table_header(2, "V8 Javascript Engine", "enabled");
  174. php_info_print_table_header(2, "V8 Engine Compiled Version", PHP_V8_VERSION);
  175. php_info_print_table_header(2, "V8 Engine Linked Version", v8::V8::GetVersion());
  176. php_info_print_table_header(2, "Version", PHP_V8JS_VERSION);
  177. php_info_print_table_end();
  178. DISPLAY_INI_ENTRIES();
  179. }
  180. /* }}} */
  181. /* {{{ PHP_GINIT_FUNCTION
  182. */
  183. static PHP_GINIT_FUNCTION(v8js)
  184. {
  185. #if defined(COMPILE_DL_MYSQLI) && defined(ZTS)
  186. ZEND_TSRMLS_CACHE_UPDATE();
  187. #endif
  188. /*
  189. If ZTS is disabled, the v8js_globals instance is declared right
  190. in the BSS and hence automatically initialized by C++ compiler.
  191. Most of the variables are just zeroed.
  192. If ZTS is enabled however, v8js_globals just points to a freshly
  193. allocated, uninitialized piece of memory, hence we need to
  194. initialize all fields on our own. Likewise on shutdown we have to
  195. run the destructors manually.
  196. */
  197. #ifdef ZTS
  198. v8js_globals->v8_initialized = 0;
  199. v8js_globals->timer_thread = NULL;
  200. v8js_globals->timer_stop = false;
  201. new(&v8js_globals->timer_mutex) std::mutex;
  202. new(&v8js_globals->timer_stack) std::deque<v8js_timer_ctx *>;
  203. v8js_globals->fatal_error_abort = 0;
  204. #endif
  205. }
  206. /* }}} */
  207. /* {{{ PHP_GSHUTDOWN_FUNCTION
  208. */
  209. static PHP_GSHUTDOWN_FUNCTION(v8js)
  210. {
  211. #ifdef ZTS
  212. v8js_globals->timer_stack.~deque();
  213. v8js_globals->timer_mutex.~mutex();
  214. #endif
  215. }
  216. /* }}} */
  217. /* {{{ v8js_functions[] */
  218. static const zend_function_entry v8js_functions[] = {
  219. {NULL, NULL, NULL}
  220. };
  221. /* }}} */
  222. /* {{{ v8js_module_entry
  223. */
  224. zend_module_entry v8js_module_entry = {
  225. STANDARD_MODULE_HEADER_EX,
  226. NULL,
  227. NULL,
  228. "v8js",
  229. v8js_functions,
  230. PHP_MINIT(v8js),
  231. PHP_MSHUTDOWN(v8js),
  232. NULL,
  233. PHP_RSHUTDOWN(v8js),
  234. PHP_MINFO(v8js),
  235. PHP_V8JS_VERSION,
  236. PHP_MODULE_GLOBALS(v8js),
  237. PHP_GINIT(v8js),
  238. PHP_GSHUTDOWN(v8js),
  239. NULL,
  240. STANDARD_MODULE_PROPERTIES_EX
  241. };
  242. /* }}} */
  243. /*
  244. * Local variables:
  245. * tab-width: 4
  246. * c-basic-offset: 4
  247. * indent-tabs-mode: t
  248. * End:
  249. * vim600: noet sw=4 ts=4 fdm=marker
  250. * vim<600: noet sw=4 ts=4
  251. */