v8js.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #define V8JS_DEBUG 0
  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. #include "ext/standard/php_smart_str.h"
  23. }
  24. #include "v8js_class.h"
  25. #include "v8js_exceptions.h"
  26. #include "v8js_v8object_class.h"
  27. ZEND_DECLARE_MODULE_GLOBALS(v8js)
  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 ZEND_INI_MH(v8js_OnUpdateUseDate) /* {{{ */
  44. {
  45. bool value;
  46. if (new_value_length==2 && strcasecmp("on", new_value)==0) {
  47. value = (bool) 1;
  48. } else if (new_value_length==3 && strcasecmp("yes", new_value)==0) {
  49. value = (bool) 1;
  50. } else if (new_value_length==4 && strcasecmp("true", new_value)==0) {
  51. value = (bool) 1;
  52. } else {
  53. value = (bool) atoi(new_value);
  54. }
  55. V8JSG(use_date) = value;
  56. return SUCCESS;
  57. }
  58. /* }}} */
  59. static ZEND_INI_MH(v8js_OnUpdateUseArrayAccess) /* {{{ */
  60. {
  61. bool value;
  62. if (new_value_length==2 && strcasecmp("on", new_value)==0) {
  63. value = (bool) 1;
  64. } else if (new_value_length==3 && strcasecmp("yes", new_value)==0) {
  65. value = (bool) 1;
  66. } else if (new_value_length==4 && strcasecmp("true", new_value)==0) {
  67. value = (bool) 1;
  68. } else {
  69. value = (bool) atoi(new_value);
  70. }
  71. V8JSG(use_array_access) = value;
  72. return SUCCESS;
  73. }
  74. /* }}} */
  75. ZEND_INI_BEGIN() /* {{{ */
  76. ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
  77. ZEND_INI_ENTRY("v8js.use_date", "0", ZEND_INI_ALL, v8js_OnUpdateUseDate)
  78. ZEND_INI_ENTRY("v8js.use_array_access", "0", ZEND_INI_ALL, v8js_OnUpdateUseArrayAccess)
  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. return SUCCESS;
  102. }
  103. /* }}} */
  104. /* {{{ PHP_RSHUTDOWN_FUNCTION
  105. */
  106. static PHP_RSHUTDOWN_FUNCTION(v8js)
  107. {
  108. // If the timer thread is running then stop it
  109. if (V8JSG(timer_thread)) {
  110. V8JSG(timer_stop) = true;
  111. V8JSG(timer_thread)->join();
  112. }
  113. #if V8JS_DEBUG
  114. v8::HeapStatistics stats;
  115. v8::V8::GetHeapStatistics(&stats);
  116. float used = stats.used_heap_size() / 1024.0 / 1024.0;
  117. float total = stats.total_heap_size() / 1024.0 / 1024.0;
  118. fprintf(stderr, "### RSHUTDOWN ###\n");
  119. fprintf(stderr, "############ Heap Used/Total %.2f/%.2f MB ############\n", used, total);
  120. fflush(stderr);
  121. #endif
  122. return SUCCESS;
  123. }
  124. /* }}} */
  125. /* {{{ PHP_MINFO_FUNCTION
  126. */
  127. static PHP_MINFO_FUNCTION(v8js)
  128. {
  129. php_info_print_table_start();
  130. php_info_print_table_header(2, "V8 Javascript Engine", "enabled");
  131. php_info_print_table_header(2, "V8 Engine Compiled Version", PHP_V8_VERSION);
  132. php_info_print_table_header(2, "V8 Engine Linked Version", v8::V8::GetVersion());
  133. php_info_print_table_header(2, "Version", PHP_V8JS_VERSION);
  134. php_info_print_table_end();
  135. DISPLAY_INI_ENTRIES();
  136. }
  137. /* }}} */
  138. /* {{{ PHP_GINIT_FUNCTION
  139. */
  140. static PHP_GINIT_FUNCTION(v8js)
  141. {
  142. /*
  143. If ZTS is disabled, the v8js_globals instance is declared right
  144. in the BSS and hence automatically initialized by C++ compiler.
  145. Most of the variables are just zeroed.
  146. If ZTS is enabled however, v8js_globals just points to a freshly
  147. allocated, uninitialized piece of memory, hence we need to
  148. initialize all fields on our own. Likewise on shutdown we have to
  149. run the destructors manually.
  150. */
  151. #ifdef ZTS
  152. v8js_globals->extensions = NULL;
  153. v8js_globals->v8_initialized = 0;
  154. v8js_globals->v8_flags = NULL;
  155. v8js_globals->timer_thread = NULL;
  156. v8js_globals->timer_stop = false;
  157. new(&v8js_globals->timer_mutex) std::mutex;
  158. new(&v8js_globals->timer_stack) std::deque<v8js_timer_ctx *>;
  159. v8js_globals->fatal_error_abort = 0;
  160. #endif
  161. }
  162. /* }}} */
  163. /* {{{ PHP_GSHUTDOWN_FUNCTION
  164. */
  165. static PHP_GSHUTDOWN_FUNCTION(v8js)
  166. {
  167. if (v8js_globals->extensions) {
  168. zend_hash_destroy(v8js_globals->extensions);
  169. free(v8js_globals->extensions);
  170. v8js_globals->extensions = NULL;
  171. }
  172. if (v8js_globals->v8_flags) {
  173. free(v8js_globals->v8_flags);
  174. v8js_globals->v8_flags = NULL;
  175. }
  176. #ifdef ZTS
  177. v8js_globals->timer_stack.~deque();
  178. v8js_globals->timer_mutex.~mutex();
  179. #endif
  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. */