OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/isolate.h" | 5 #include "src/isolate.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include <fstream> // NOLINT(readability/streams) | 9 #include <fstream> // NOLINT(readability/streams) |
10 #include <sstream> | 10 #include <sstream> |
11 | 11 |
12 #include "src/ast/context-slot-cache.h" | 12 #include "src/ast/context-slot-cache.h" |
13 #include "src/base/hashmap.h" | 13 #include "src/base/hashmap.h" |
14 #include "src/base/platform/platform.h" | 14 #include "src/base/platform/platform.h" |
15 #include "src/base/sys-info.h" | 15 #include "src/base/sys-info.h" |
16 #include "src/base/utils/random-number-generator.h" | 16 #include "src/base/utils/random-number-generator.h" |
17 #include "src/basic-block-profiler.h" | 17 #include "src/basic-block-profiler.h" |
18 #include "src/bootstrapper.h" | 18 #include "src/bootstrapper.h" |
19 #include "src/cancelable-task.h" | 19 #include "src/cancelable-task.h" |
20 #include "src/codegen.h" | 20 #include "src/codegen.h" |
21 #include "src/compilation-cache.h" | 21 #include "src/compilation-cache.h" |
22 #include "src/compilation-statistics.h" | 22 #include "src/compilation-statistics.h" |
23 #include "src/compiler-dispatcher/optimizing-compile-dispatcher.h" | 23 #include "src/compiler-dispatcher/optimizing-compile-dispatcher.h" |
24 #include "src/crankshaft/hydrogen.h" | 24 #include "src/crankshaft/hydrogen.h" |
25 #include "src/debug/debug.h" | 25 #include "src/debug/debug.h" |
26 #include "src/deoptimizer.h" | 26 #include "src/deoptimizer.h" |
27 #include "src/elements.h" | |
27 #include "src/external-reference-table.h" | 28 #include "src/external-reference-table.h" |
28 #include "src/frames-inl.h" | 29 #include "src/frames-inl.h" |
29 #include "src/ic/access-compiler-data.h" | 30 #include "src/ic/access-compiler-data.h" |
30 #include "src/ic/stub-cache.h" | 31 #include "src/ic/stub-cache.h" |
31 #include "src/interface-descriptors.h" | 32 #include "src/interface-descriptors.h" |
32 #include "src/interpreter/interpreter.h" | 33 #include "src/interpreter/interpreter.h" |
33 #include "src/isolate-inl.h" | 34 #include "src/isolate-inl.h" |
34 #include "src/libsampler/sampler.h" | 35 #include "src/libsampler/sampler.h" |
35 #include "src/log.h" | 36 #include "src/log.h" |
36 #include "src/messages.h" | 37 #include "src/messages.h" |
(...skipping 2964 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3001 if (promise_reject_callback_ == NULL) return; | 3002 if (promise_reject_callback_ == NULL) return; |
3002 Handle<JSArray> stack_trace; | 3003 Handle<JSArray> stack_trace; |
3003 if (event == v8::kPromiseRejectWithNoHandler && value->IsJSObject()) { | 3004 if (event == v8::kPromiseRejectWithNoHandler && value->IsJSObject()) { |
3004 stack_trace = GetDetailedStackTrace(Handle<JSObject>::cast(value)); | 3005 stack_trace = GetDetailedStackTrace(Handle<JSObject>::cast(value)); |
3005 } | 3006 } |
3006 promise_reject_callback_(v8::PromiseRejectMessage( | 3007 promise_reject_callback_(v8::PromiseRejectMessage( |
3007 v8::Utils::PromiseToLocal(promise), event, v8::Utils::ToLocal(value), | 3008 v8::Utils::PromiseToLocal(promise), event, v8::Utils::ToLocal(value), |
3008 v8::Utils::StackTraceToLocal(stack_trace))); | 3009 v8::Utils::StackTraceToLocal(stack_trace))); |
3009 } | 3010 } |
3010 | 3011 |
3012 namespace { | |
3013 class PromiseDebugEventHelper { | |
adamk
2016/10/12 16:18:11
We often name these things scopes, so how about Pr
gsathya
2016/10/12 20:08:53
Done.
| |
3014 public: | |
3015 PromiseDebugEventHelper(Isolate* isolate, Handle<Object> before, | |
adamk
2016/10/12 16:18:11
You could reduce the lines-of-code (or at least en
gsathya
2016/10/12 20:08:53
Done.
| |
3016 Handle<Object> after) | |
3017 : isolate_(isolate), before_(before), after_(after) { | |
3018 if (isolate_->debug()->is_active() && before_->IsJSObject()) { | |
3019 isolate_->debug()->OnAsyncTaskEvent(Handle<JSObject>::cast(before_)); | |
3020 } | |
3021 } | |
3022 | |
3023 ~PromiseDebugEventHelper() { | |
3024 if (isolate_->debug()->is_active() && after_->IsJSObject()) { | |
3025 isolate_->debug()->OnAsyncTaskEvent(Handle<JSObject>::cast(after_)); | |
3026 } | |
3027 } | |
3028 | |
3029 private: | |
3030 Isolate* isolate_; | |
3031 Handle<Object> before_; | |
3032 Handle<Object> after_; | |
3033 }; | |
3034 } // namespace | |
3035 | |
3036 void Isolate::PromiseReactionJob( | |
3037 Handle<PromiseReactionJobInfo> promise_reaction_job_info, | |
3038 MaybeHandle<Object>* result, MaybeHandle<Object>* maybe_exception) { | |
3039 Handle<Object> before(promise_reaction_job_info->before_debug_event(), this); | |
3040 Handle<Object> after(promise_reaction_job_info->after_debug_event(), this); | |
3041 PromiseDebugEventHelper helper(this, before, after); | |
3042 | |
3043 Handle<Object> value(promise_reaction_job_info->value(), this); | |
3044 Handle<Object> tasks(promise_reaction_job_info->tasks(), this); | |
3045 Handle<JSFunction> promise_handle_fn = promise_handle(); | |
3046 Handle<Object> undefined = factory()->undefined_value(); | |
3047 | |
3048 // If tasks is an array we have multiple onFulfilled/onRejected callbacks | |
3049 // associated with the promise. The deferred object for each callback | |
3050 // is attached to this array as well. | |
3051 // Otherwise, there is a single callback and the deferred object is attached | |
3052 // directly to PromiseReactionJobInfo. | |
3053 if (tasks->IsJSArray()) { | |
3054 Handle<JSArray> array = Handle<JSArray>::cast(tasks); | |
3055 DCHECK(array->length()->IsSmi()); | |
3056 int length = Smi::cast(array->length())->value(); | |
3057 ElementsAccessor* accessor = array->GetElementsAccessor(); | |
3058 DCHECK(length % 2 == 0); | |
3059 for (int i = 0; i < length; i += 2) { | |
3060 DCHECK(accessor->HasElement(array, i)); | |
3061 DCHECK(accessor->HasElement(array, i + 1)); | |
3062 Handle<Object> argv[] = {value, accessor->Get(array, i), | |
3063 accessor->Get(array, i + 1)}; | |
3064 *result = Execution::TryCall(this, promise_handle_fn, undefined, | |
3065 arraysize(argv), argv, maybe_exception); | |
3066 if (result->is_null() && maybe_exception->is_null()) { | |
adamk
2016/10/12 16:18:11
Copy and paste a comment with this, too:
// If ex
gsathya
2016/10/12 20:08:53
Done.
| |
3067 return; | |
3068 } | |
3069 } | |
3070 } else { | |
3071 Handle<Object> deferred(promise_reaction_job_info->deferred(), this); | |
3072 Handle<Object> argv[] = {value, tasks, deferred}; | |
3073 *result = | |
3074 Execution::TryCall(this, promise_handle(), factory()->undefined_value(), | |
adamk
2016/10/12 16:18:11
You can pass promise_handle_fn and undefined here
gsathya
2016/10/12 20:08:53
Done.
| |
3075 arraysize(argv), argv, maybe_exception); | |
3076 if (result->is_null() && maybe_exception->is_null()) { | |
adamk
2016/10/12 16:18:11
No need to do anything special in this case, right
gsathya
2016/10/12 20:08:53
Done.
| |
3077 return; | |
3078 } | |
3079 } | |
3080 } | |
3081 | |
3011 void Isolate::PromiseResolveThenableJob(Handle<PromiseContainer> container, | 3082 void Isolate::PromiseResolveThenableJob(Handle<PromiseContainer> container, |
3012 MaybeHandle<Object>* result, | 3083 MaybeHandle<Object>* result, |
3013 MaybeHandle<Object>* maybe_exception) { | 3084 MaybeHandle<Object>* maybe_exception) { |
3014 if (debug()->is_active()) { | 3085 Handle<Object> before(container->before_debug_event(), this); |
3015 Handle<Object> before_debug_event(container->before_debug_event(), this); | 3086 Handle<Object> after(container->after_debug_event(), this); |
3016 if (before_debug_event->IsJSObject()) { | 3087 PromiseDebugEventHelper helper(this, before, after); |
3017 debug()->OnAsyncTaskEvent(Handle<JSObject>::cast(before_debug_event)); | |
3018 } | |
3019 } | |
3020 | 3088 |
3021 Handle<JSReceiver> thenable(container->thenable(), this); | 3089 Handle<JSReceiver> thenable(container->thenable(), this); |
3022 Handle<JSFunction> resolve(container->resolve(), this); | 3090 Handle<JSFunction> resolve(container->resolve(), this); |
3023 Handle<JSFunction> reject(container->reject(), this); | 3091 Handle<JSFunction> reject(container->reject(), this); |
3024 Handle<JSReceiver> then(container->then(), this); | 3092 Handle<JSReceiver> then(container->then(), this); |
3025 Handle<Object> argv[] = {resolve, reject}; | 3093 Handle<Object> argv[] = {resolve, reject}; |
3026 *result = Execution::TryCall(this, then, thenable, arraysize(argv), argv, | 3094 *result = Execution::TryCall(this, then, thenable, arraysize(argv), argv, |
3027 maybe_exception); | 3095 maybe_exception); |
3028 | 3096 |
3029 Handle<Object> reason; | 3097 Handle<Object> reason; |
3030 if (maybe_exception->ToHandle(&reason)) { | 3098 if (maybe_exception->ToHandle(&reason)) { |
3031 DCHECK(result->is_null()); | 3099 DCHECK(result->is_null()); |
3032 Handle<Object> reason_arg[] = {reason}; | 3100 Handle<Object> reason_arg[] = {reason}; |
3033 *result = | 3101 *result = |
3034 Execution::TryCall(this, reject, factory()->undefined_value(), | 3102 Execution::TryCall(this, reject, factory()->undefined_value(), |
3035 arraysize(reason_arg), reason_arg, maybe_exception); | 3103 arraysize(reason_arg), reason_arg, maybe_exception); |
3036 } | 3104 } |
3037 | |
3038 if (debug()->is_active()) { | |
3039 Handle<Object> after_debug_event(container->after_debug_event(), this); | |
3040 if (after_debug_event->IsJSObject()) { | |
3041 debug()->OnAsyncTaskEvent(Handle<JSObject>::cast(after_debug_event)); | |
3042 } | |
3043 } | |
3044 } | 3105 } |
3045 | 3106 |
3046 void Isolate::EnqueueMicrotask(Handle<Object> microtask) { | 3107 void Isolate::EnqueueMicrotask(Handle<Object> microtask) { |
3047 DCHECK(microtask->IsJSFunction() || microtask->IsCallHandlerInfo() || | 3108 DCHECK(microtask->IsJSFunction() || microtask->IsCallHandlerInfo() || |
3048 microtask->IsPromiseContainer()); | 3109 microtask->IsPromiseContainer() || |
3110 microtask->IsPromiseReactionJobInfo()); | |
3049 Handle<FixedArray> queue(heap()->microtask_queue(), this); | 3111 Handle<FixedArray> queue(heap()->microtask_queue(), this); |
3050 int num_tasks = pending_microtask_count(); | 3112 int num_tasks = pending_microtask_count(); |
3051 DCHECK(num_tasks <= queue->length()); | 3113 DCHECK(num_tasks <= queue->length()); |
3052 if (num_tasks == 0) { | 3114 if (num_tasks == 0) { |
3053 queue = factory()->NewFixedArray(8); | 3115 queue = factory()->NewFixedArray(8); |
3054 heap()->set_microtask_queue(*queue); | 3116 heap()->set_microtask_queue(*queue); |
3055 } else if (num_tasks == queue->length()) { | 3117 } else if (num_tasks == queue->length()) { |
3056 queue = factory()->CopyFixedArrayAndGrow(queue, num_tasks); | 3118 queue = factory()->CopyFixedArrayAndGrow(queue, num_tasks); |
3057 heap()->set_microtask_queue(*queue); | 3119 heap()->set_microtask_queue(*queue); |
3058 } | 3120 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3090 | 3152 |
3091 if (microtask->IsCallHandlerInfo()) { | 3153 if (microtask->IsCallHandlerInfo()) { |
3092 Handle<CallHandlerInfo> callback_info = | 3154 Handle<CallHandlerInfo> callback_info = |
3093 Handle<CallHandlerInfo>::cast(microtask); | 3155 Handle<CallHandlerInfo>::cast(microtask); |
3094 v8::MicrotaskCallback callback = | 3156 v8::MicrotaskCallback callback = |
3095 v8::ToCData<v8::MicrotaskCallback>(callback_info->callback()); | 3157 v8::ToCData<v8::MicrotaskCallback>(callback_info->callback()); |
3096 void* data = v8::ToCData<void*>(callback_info->data()); | 3158 void* data = v8::ToCData<void*>(callback_info->data()); |
3097 callback(data); | 3159 callback(data); |
3098 } else { | 3160 } else { |
3099 SaveContext save(this); | 3161 SaveContext save(this); |
3100 Context* context = microtask->IsJSFunction() | 3162 Context* context; |
3101 ? Handle<JSFunction>::cast(microtask)->context() | 3163 if (microtask->IsJSFunction()) { |
3102 : Handle<PromiseContainer>::cast(microtask) | 3164 context = Handle<JSFunction>::cast(microtask)->context(); |
3103 ->resolve() | 3165 } else if (microtask->IsPromiseContainer()) { |
3104 ->context(); | 3166 context = |
3167 Handle<PromiseContainer>::cast(microtask)->resolve()->context(); | |
3168 } else { | |
3169 context = Handle<PromiseReactionJobInfo>::cast(microtask)->context(); | |
3170 } | |
3171 | |
3105 set_context(context->native_context()); | 3172 set_context(context->native_context()); |
3106 handle_scope_implementer_->EnterMicrotaskContext( | 3173 handle_scope_implementer_->EnterMicrotaskContext( |
3107 Handle<Context>(context, this)); | 3174 Handle<Context>(context, this)); |
3108 | 3175 |
3109 MaybeHandle<Object> result; | 3176 MaybeHandle<Object> result; |
3110 MaybeHandle<Object> maybe_exception; | 3177 MaybeHandle<Object> maybe_exception; |
3111 | 3178 |
3112 if (microtask->IsJSFunction()) { | 3179 if (microtask->IsJSFunction()) { |
3113 Handle<JSFunction> microtask_function = | 3180 Handle<JSFunction> microtask_function = |
3114 Handle<JSFunction>::cast(microtask); | 3181 Handle<JSFunction>::cast(microtask); |
3115 result = Execution::TryCall(this, microtask_function, | 3182 result = Execution::TryCall(this, microtask_function, |
3116 factory()->undefined_value(), 0, NULL, | 3183 factory()->undefined_value(), 0, NULL, |
3117 &maybe_exception); | 3184 &maybe_exception); |
3118 } else { | 3185 } else if (microtask->IsPromiseContainer()) { |
3119 PromiseResolveThenableJob(Handle<PromiseContainer>::cast(microtask), | 3186 PromiseResolveThenableJob(Handle<PromiseContainer>::cast(microtask), |
3120 &result, &maybe_exception); | 3187 &result, &maybe_exception); |
3188 } else { | |
3189 PromiseReactionJob(Handle<PromiseReactionJobInfo>::cast(microtask), | |
3190 &result, &maybe_exception); | |
3121 } | 3191 } |
3122 | 3192 |
3123 handle_scope_implementer_->LeaveMicrotaskContext(); | 3193 handle_scope_implementer_->LeaveMicrotaskContext(); |
3124 | 3194 |
3125 // If execution is terminating, just bail out. | 3195 // If execution is terminating, just bail out. |
3126 if (result.is_null() && maybe_exception.is_null()) { | 3196 if (result.is_null() && maybe_exception.is_null()) { |
3127 // Clear out any remaining callbacks in the queue. | 3197 // Clear out any remaining callbacks in the queue. |
3128 heap()->set_microtask_queue(heap()->empty_fixed_array()); | 3198 heap()->set_microtask_queue(heap()->empty_fixed_array()); |
3129 set_pending_microtask_count(0); | 3199 set_pending_microtask_count(0); |
3130 return; | 3200 return; |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3323 // Then check whether this scope intercepts. | 3393 // Then check whether this scope intercepts. |
3324 if ((flag & intercept_mask_)) { | 3394 if ((flag & intercept_mask_)) { |
3325 intercepted_flags_ |= flag; | 3395 intercepted_flags_ |= flag; |
3326 return true; | 3396 return true; |
3327 } | 3397 } |
3328 return false; | 3398 return false; |
3329 } | 3399 } |
3330 | 3400 |
3331 } // namespace internal | 3401 } // namespace internal |
3332 } // namespace v8 | 3402 } // namespace v8 |
OLD | NEW |