v8js.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. //#define PHP_V8_VERSION "0.1.4"
  15. #ifdef HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif
  18. #include <v8-debug.h>
  19. #include "php_v8js_macros.h"
  20. extern "C" {
  21. #include "php_ini.h"
  22. #include "ext/standard/info.h"
  23. #include "ext/standard/php_string.h"
  24. #include "ext/standard/php_smart_str.h"
  25. }
  26. #include "v8js_class.h"
  27. #include "v8js_exceptions.h"
  28. #include "v8js_v8object_class.h"
  29. ZEND_DECLARE_MODULE_GLOBALS(v8js)
  30. /* {{{ INI Settings */
  31. static ZEND_INI_MH(v8js_OnUpdateV8Flags) /* {{{ */
  32. {
  33. if (new_value) {
  34. if (V8JSG(v8_flags)) {
  35. free(V8JSG(v8_flags));
  36. V8JSG(v8_flags) = NULL;
  37. }
  38. if (!new_value[0]) {
  39. return FAILURE;
  40. }
  41. V8JSG(v8_flags) = zend_strndup(new_value, new_value_length);
  42. }
  43. return SUCCESS;
  44. }
  45. static ZEND_INI_MH(v8js_OnUpdateUseDate) /* {{{ */
  46. {
  47. bool value;
  48. if (new_value_length==2 && strcasecmp("on", new_value)==0) {
  49. value = (bool) 1;
  50. } else if (new_value_length==3 && strcasecmp("yes", new_value)==0) {
  51. value = (bool) 1;
  52. } else if (new_value_length==4 && strcasecmp("true", new_value)==0) {
  53. value = (bool) 1;
  54. } else {
  55. value = (bool) atoi(new_value);
  56. }
  57. V8JSG(use_date) = value;
  58. return SUCCESS;
  59. }
  60. /* }}} */
  61. static ZEND_INI_MH(v8js_OnUpdateUseArrayAccess) /* {{{ */
  62. {
  63. bool value;
  64. if (new_value_length==2 && strcasecmp("on", new_value)==0) {
  65. value = (bool) 1;
  66. } else if (new_value_length==3 && strcasecmp("yes", new_value)==0) {
  67. value = (bool) 1;
  68. } else if (new_value_length==4 && strcasecmp("true", new_value)==0) {
  69. value = (bool) 1;
  70. } else {
  71. value = (bool) atoi(new_value);
  72. }
  73. V8JSG(use_array_access) = value;
  74. return SUCCESS;
  75. }
  76. /* }}} */
  77. ZEND_INI_BEGIN() /* {{{ */
  78. ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
  79. ZEND_INI_ENTRY("v8js.use_date", "0", ZEND_INI_ALL, v8js_OnUpdateUseDate)
  80. ZEND_INI_ENTRY("v8js.use_array_access", "0", ZEND_INI_ALL, v8js_OnUpdateUseArrayAccess)
  81. ZEND_INI_END()
  82. /* }}} */
  83. /* }}} INI */
  84. #ifdef COMPILE_DL_V8JS
  85. ZEND_GET_MODULE(v8js)
  86. #endif
  87. /* {{{ PHP_MINIT_FUNCTION
  88. */
  89. PHP_MINIT_FUNCTION(v8js)
  90. {
  91. PHP_MINIT(v8js_class)(INIT_FUNC_ARGS_PASSTHRU);
  92. PHP_MINIT(v8js_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
  93. PHP_MINIT(v8js_v8object_class)(INIT_FUNC_ARGS_PASSTHRU);
  94. REGISTER_INI_ENTRIES();
  95. return SUCCESS;
  96. }
  97. /* }}} */
  98. /* {{{ PHP_MSHUTDOWN_FUNCTION
  99. */
  100. static PHP_MSHUTDOWN_FUNCTION(v8js)
  101. {
  102. UNREGISTER_INI_ENTRIES();
  103. return SUCCESS;
  104. }
  105. /* }}} */
  106. /* {{{ PHP_RSHUTDOWN_FUNCTION
  107. */
  108. static PHP_RSHUTDOWN_FUNCTION(v8js)
  109. {
  110. // If the timer thread is running then stop it
  111. if (V8JSG(timer_thread)) {
  112. V8JSG(timer_stop) = true;
  113. V8JSG(timer_thread)->join();
  114. }
  115. #if V8JS_DEBUG
  116. v8::HeapStatistics stats;
  117. v8::V8::GetHeapStatistics(&stats);
  118. float used = stats.used_heap_size() / 1024.0 / 1024.0;
  119. float total = stats.total_heap_size() / 1024.0 / 1024.0;
  120. fprintf(stderr, "### RSHUTDOWN ###\n");
  121. fprintf(stderr, "############ Heap Used/Total %.2f/%.2f MB ############\n", used, total);
  122. fflush(stderr);
  123. #endif
  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", 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. 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. */