v8js.cc 6.0 KB

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