v8js.cc 5.8 KB

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