v8js_v8.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. extern "C" {
  17. #include "php.h"
  18. #include "ext/date/php_date.h"
  19. #include "ext/standard/php_string.h"
  20. #include "zend_interfaces.h"
  21. #include "zend_closures.h"
  22. #include "zend_exceptions.h"
  23. }
  24. #if !defined(_WIN32) && PHP_V8_API_VERSION >= 3029036
  25. #include <libplatform/libplatform.h>
  26. #endif
  27. #include "php_v8js_macros.h"
  28. #include "v8js_v8.h"
  29. #include "v8js_debug.h"
  30. #include "v8js_timer.h"
  31. #include "v8js_exceptions.h"
  32. void v8js_v8_init(TSRMLS_D) /* {{{ */
  33. {
  34. /* Run only once */
  35. if (V8JSG(v8_initialized)) {
  36. return;
  37. }
  38. #if !defined(_WIN32) && PHP_V8_API_VERSION >= 3029036
  39. V8JSG(v8_platform) = v8::platform::CreateDefaultPlatform();
  40. v8::V8::InitializePlatform(V8JSG(v8_platform));
  41. #endif
  42. /* Set V8 command line flags (must be done before V8::Initialize()!) */
  43. if (V8JSG(v8_flags)) {
  44. v8::V8::SetFlagsFromString(V8JSG(v8_flags), strlen(V8JSG(v8_flags)));
  45. }
  46. /* Initialize V8 */
  47. v8::V8::Initialize();
  48. /* Run only once */
  49. V8JSG(v8_initialized) = 1;
  50. }
  51. /* }}} */
  52. void v8js_v8_call(v8js_ctx *c, zval **return_value,
  53. long flags, long time_limit, long memory_limit,
  54. std::function< v8::Local<v8::Value>(v8::Isolate *) >& v8_call TSRMLS_DC) /* {{{ */
  55. {
  56. char *tz = NULL;
  57. V8JS_CTX_PROLOGUE(c);
  58. V8JSG(timer_mutex).lock();
  59. c->time_limit_hit = false;
  60. c->memory_limit_hit = false;
  61. V8JSG(timer_mutex).unlock();
  62. /* Catch JS exceptions */
  63. v8::TryCatch try_catch;
  64. /* Set flags for runtime use */
  65. V8JS_GLOBAL_SET_FLAGS(isolate, flags);
  66. /* Check if timezone has been changed and notify V8 */
  67. tz = getenv("TZ");
  68. if (tz != NULL) {
  69. if (c->tz == NULL) {
  70. c->tz = strdup(tz);
  71. }
  72. else if (strcmp(c->tz, tz) != 0) {
  73. v8::Date::DateTimeConfigurationChangeNotification(c->isolate);
  74. free(c->tz);
  75. c->tz = strdup(tz);
  76. }
  77. }
  78. if (time_limit > 0 || memory_limit > 0) {
  79. // If timer thread is not running then start it
  80. if (!V8JSG(timer_thread)) {
  81. // If not, start timer thread
  82. V8JSG(timer_thread) = new std::thread(v8js_timer_thread TSRMLS_CC);
  83. }
  84. }
  85. /* Always pass the timer to the stack so there can be follow-up changes to
  86. * the time & memory limit. */
  87. v8js_timer_push(time_limit, memory_limit, c TSRMLS_CC);
  88. #ifdef ENABLE_DEBUGGER_SUPPORT
  89. if(c == v8js_debug_context && v8js_debug_auto_break_mode != V8JS_DEBUG_AUTO_BREAK_NEVER) {
  90. v8::Debug::DebugBreak(c->isolate);
  91. if(v8js_debug_auto_break_mode == V8JS_DEBUG_AUTO_BREAK_ONCE) {
  92. /* If break-once-mode was enabled, reset flag. */
  93. v8js_debug_auto_break_mode = V8JS_DEBUG_AUTO_BREAK_NEVER;
  94. }
  95. }
  96. #endif /* ENABLE_DEBUGGER_SUPPORT */
  97. /* Execute script */
  98. c->in_execution++;
  99. v8::Local<v8::Value> result = v8_call(c->isolate);
  100. c->in_execution--;
  101. /* Pop our context from the stack and read (possibly updated) limits
  102. * into local variables. */
  103. V8JSG(timer_mutex).lock();
  104. v8js_timer_ctx *timer_ctx = V8JSG(timer_stack).front();
  105. V8JSG(timer_stack).pop_front();
  106. V8JSG(timer_mutex).unlock();
  107. time_limit = timer_ctx->time_limit;
  108. memory_limit = timer_ctx->memory_limit;
  109. efree(timer_ctx);
  110. /* Check for fatal error marker possibly set by v8js_error_handler; just
  111. * rethrow the error since we're now out of V8. */
  112. if(V8JSG(fatal_error_abort)) {
  113. zend_bailout();
  114. }
  115. char exception_string[64];
  116. if (c->time_limit_hit) {
  117. // Execution has been terminated due to time limit
  118. sprintf(exception_string, "Script time limit of %lu milliseconds exceeded", time_limit);
  119. zend_throw_exception(php_ce_v8js_time_limit_exception, exception_string, 0 TSRMLS_CC);
  120. return;
  121. }
  122. if (c->memory_limit_hit) {
  123. // Execution has been terminated due to memory limit
  124. sprintf(exception_string, "Script memory limit of %lu bytes exceeded", memory_limit);
  125. zend_throw_exception(php_ce_v8js_memory_limit_exception, exception_string, 0 TSRMLS_CC);
  126. return;
  127. }
  128. if (!try_catch.CanContinue()) {
  129. // At this point we can't re-throw the exception
  130. return;
  131. }
  132. /* There was pending exception left from earlier executions -> throw to PHP */
  133. if (c->pending_exception) {
  134. zend_throw_exception_object(c->pending_exception TSRMLS_CC);
  135. c->pending_exception = NULL;
  136. }
  137. /* Handle runtime JS exceptions */
  138. if (try_catch.HasCaught()) {
  139. /* Pending exceptions are set only in outer caller, inner caller exceptions are always rethrown */
  140. if (c->in_execution < 1) {
  141. /* Report immediately if report_uncaught is true */
  142. if (c->report_uncaught) {
  143. v8js_throw_script_exception(&try_catch TSRMLS_CC);
  144. return;
  145. }
  146. /* Exception thrown from JS, preserve it for future execution */
  147. if (result.IsEmpty()) {
  148. MAKE_STD_ZVAL(c->pending_exception);
  149. v8js_create_script_exception(c->pending_exception, &try_catch TSRMLS_CC);
  150. return;
  151. }
  152. }
  153. /* Rethrow back to JS */
  154. try_catch.ReThrow();
  155. return;
  156. }
  157. /* Convert V8 value to PHP value */
  158. if (!result.IsEmpty()) {
  159. v8js_to_zval(result, *return_value, flags, c->isolate TSRMLS_CC);
  160. }
  161. }
  162. /* }}} */
  163. void v8js_terminate_execution(v8js_ctx *c TSRMLS_DC) /* {{{ */
  164. {
  165. // Forcefully terminate the current thread of V8 execution in the isolate
  166. v8::V8::TerminateExecution(c->isolate);
  167. }
  168. /* }}} */
  169. int v8js_get_properties_hash(v8::Handle<v8::Value> jsValue, HashTable *retval, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  170. {
  171. v8::Local<v8::Object> jsObj = jsValue->ToObject();
  172. if (!jsObj.IsEmpty()) {
  173. v8::Local<v8::Array> jsKeys = jsObj->GetPropertyNames();
  174. for (unsigned i = 0; i < jsKeys->Length(); i++)
  175. {
  176. v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString();
  177. /* Skip any prototype properties */
  178. if (!jsObj->HasOwnProperty(jsKey) && !jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey)) {
  179. continue;
  180. }
  181. v8::Local<v8::Value> jsVal = jsObj->Get(jsKey);
  182. v8::String::Utf8Value cstr(jsKey);
  183. const char *key = ToCString(cstr);
  184. zval *value = NULL;
  185. v8::Local<v8::Value> php_object;
  186. if (jsVal->IsObject()) {
  187. php_object = v8::Local<v8::Object>::Cast(jsVal)->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  188. }
  189. if (!php_object.IsEmpty()) {
  190. /* This is a PHP object, passed to JS and back. */
  191. value = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  192. Z_ADDREF_P(value);
  193. }
  194. else {
  195. MAKE_STD_ZVAL(value);
  196. if (v8js_to_zval(jsVal, value, flags, isolate TSRMLS_CC) == FAILURE) {
  197. zval_ptr_dtor(&value);
  198. return FAILURE;
  199. }
  200. }
  201. if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
  202. zend_symtable_update(retval, key, strlen(key) + 1, (void *)&value, sizeof(zval *), NULL);
  203. } else {
  204. zend_hash_update(retval, key, strlen(key) + 1, (void *) &value, sizeof(zval *), NULL);
  205. }
  206. }
  207. return SUCCESS;
  208. }
  209. return FAILURE;
  210. }
  211. /* }}} */
  212. /*
  213. * Local variables:
  214. * tab-width: 4
  215. * c-basic-offset: 4
  216. * End:
  217. * vim600: noet sw=4 ts=4 fdm=marker
  218. * vim<600: noet sw=4 ts=4
  219. */