v8js.cc 6.3 KB

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