v8js_v8.cc 7.4 KB

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