v8js.cc 5.9 KB

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