v8js_main.cc 6.6 KB

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