Chromium Code Reviews| 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> | 
| (...skipping 2941 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2952 if (promise_reject_callback_ == NULL) return; | 2952 if (promise_reject_callback_ == NULL) return; | 
| 2953 Handle<JSArray> stack_trace; | 2953 Handle<JSArray> stack_trace; | 
| 2954 if (event == v8::kPromiseRejectWithNoHandler && value->IsJSObject()) { | 2954 if (event == v8::kPromiseRejectWithNoHandler && value->IsJSObject()) { | 
| 2955 stack_trace = GetDetailedStackTrace(Handle<JSObject>::cast(value)); | 2955 stack_trace = GetDetailedStackTrace(Handle<JSObject>::cast(value)); | 
| 2956 } | 2956 } | 
| 2957 promise_reject_callback_(v8::PromiseRejectMessage( | 2957 promise_reject_callback_(v8::PromiseRejectMessage( | 
| 2958 v8::Utils::PromiseToLocal(promise), event, v8::Utils::ToLocal(value), | 2958 v8::Utils::PromiseToLocal(promise), event, v8::Utils::ToLocal(value), | 
| 2959 v8::Utils::StackTraceToLocal(stack_trace))); | 2959 v8::Utils::StackTraceToLocal(stack_trace))); | 
| 2960 } | 2960 } | 
| 2961 | 2961 | 
| 2962 void Isolate::PromiseResolveThenableJob(Handle<PromiseContainer> container, | |
| 2963 MaybeHandle<Object>* result, | |
| 2964 MaybeHandle<Object>* maybe_exception) { | |
| 2965 if (debug()->is_active()) { | |
| 2966 Handle<Object> before_debug_event(container->before_debug_event(), this); | |
| 2967 if (before_debug_event->IsJSObject()) { | |
| 2968 debug()->OnAsyncTaskEvent(Handle<JSObject>::cast(before_debug_event)); | |
| 2969 } | |
| 2970 } | |
| 2971 | |
| 2972 Handle<JSReceiver> thenable(container->thenable(), this); | |
| 2973 Handle<JSFunction> resolve(container->resolve(), this); | |
| 2974 Handle<JSFunction> reject(container->reject(), this); | |
| 2975 Handle<JSFunction> then(container->then(), this); | |
| 2976 Handle<Object> argv[] = {resolve, reject}; | |
| 2977 *result = Execution::TryCall(this, then, thenable, arraysize(argv), argv, | |
| 2978 maybe_exception); | |
| 2979 | |
| 2980 Handle<Object> reason; | |
| 2981 if (maybe_exception->ToHandle(&reason)) { | |
| 2982 Handle<Object> reason_arg[] = {reason}; | |
| 2983 Execution::TryCall(this, reject, factory()->undefined_value(), | |
| 
 
adamk
2016/09/20 20:08:29
Should this be assigned to *result?
 
 | |
| 2984 arraysize(reason_arg), reason_arg); | |
| 
 
adamk
2016/09/20 20:08:29
I think you want to pass maybe_reason here too
 
 | |
| 2985 } | |
| 2986 | |
| 2987 if (debug()->is_active()) { | |
| 2988 Handle<Object> after_debug_event(container->after_debug_event(), this); | |
| 2989 if (after_debug_event->IsJSObject()) { | |
| 2990 debug()->OnAsyncTaskEvent(Handle<JSObject>::cast(after_debug_event)); | |
| 2991 } | |
| 2992 } | |
| 2993 } | |
| 2962 | 2994 | 
| 2963 void Isolate::EnqueueMicrotask(Handle<Object> microtask) { | 2995 void Isolate::EnqueueMicrotask(Handle<Object> microtask) { | 
| 2964 DCHECK(microtask->IsJSFunction() || microtask->IsCallHandlerInfo()); | 2996 DCHECK(microtask->IsJSFunction() || microtask->IsCallHandlerInfo() || | 
| 2997 microtask->IsPromiseContainer()); | |
| 2965 Handle<FixedArray> queue(heap()->microtask_queue(), this); | 2998 Handle<FixedArray> queue(heap()->microtask_queue(), this); | 
| 2966 int num_tasks = pending_microtask_count(); | 2999 int num_tasks = pending_microtask_count(); | 
| 2967 DCHECK(num_tasks <= queue->length()); | 3000 DCHECK(num_tasks <= queue->length()); | 
| 2968 if (num_tasks == 0) { | 3001 if (num_tasks == 0) { | 
| 2969 queue = factory()->NewFixedArray(8); | 3002 queue = factory()->NewFixedArray(8); | 
| 2970 heap()->set_microtask_queue(*queue); | 3003 heap()->set_microtask_queue(*queue); | 
| 2971 } else if (num_tasks == queue->length()) { | 3004 } else if (num_tasks == queue->length()) { | 
| 2972 queue = factory()->CopyFixedArrayAndGrow(queue, num_tasks); | 3005 queue = factory()->CopyFixedArrayAndGrow(queue, num_tasks); | 
| 2973 heap()->set_microtask_queue(*queue); | 3006 heap()->set_microtask_queue(*queue); | 
| 2974 } | 3007 } | 
| (...skipping 21 matching lines...) Expand all Loading... | |
| 2996 HandleScope scope(this); | 3029 HandleScope scope(this); | 
| 2997 int num_tasks = pending_microtask_count(); | 3030 int num_tasks = pending_microtask_count(); | 
| 2998 Handle<FixedArray> queue(heap()->microtask_queue(), this); | 3031 Handle<FixedArray> queue(heap()->microtask_queue(), this); | 
| 2999 DCHECK(num_tasks <= queue->length()); | 3032 DCHECK(num_tasks <= queue->length()); | 
| 3000 set_pending_microtask_count(0); | 3033 set_pending_microtask_count(0); | 
| 3001 heap()->set_microtask_queue(heap()->empty_fixed_array()); | 3034 heap()->set_microtask_queue(heap()->empty_fixed_array()); | 
| 3002 | 3035 | 
| 3003 Isolate* isolate = this; | 3036 Isolate* isolate = this; | 
| 3004 FOR_WITH_HANDLE_SCOPE(isolate, int, i = 0, i, i < num_tasks, i++, { | 3037 FOR_WITH_HANDLE_SCOPE(isolate, int, i = 0, i, i < num_tasks, i++, { | 
| 3005 Handle<Object> microtask(queue->get(i), this); | 3038 Handle<Object> microtask(queue->get(i), this); | 
| 3006 if (microtask->IsJSFunction()) { | 3039 | 
| 3007 Handle<JSFunction> microtask_function = | 3040 if (microtask->IsCallHandlerInfo()) { | 
| 3008 Handle<JSFunction>::cast(microtask); | 3041 Handle<CallHandlerInfo> callback_info = | 
| 3042 Handle<CallHandlerInfo>::cast(microtask); | |
| 3043 v8::MicrotaskCallback callback = | |
| 3044 v8::ToCData<v8::MicrotaskCallback>(callback_info->callback()); | |
| 3045 void* data = v8::ToCData<void*>(callback_info->data()); | |
| 3046 callback(data); | |
| 3047 } else { | |
| 3009 SaveContext save(this); | 3048 SaveContext save(this); | 
| 3010 set_context(microtask_function->context()->native_context()); | 3049 Context* context = | 
| 3050 microtask->IsJSFunction() | |
| 3051 ? Handle<JSFunction>::cast(microtask)->context() | |
| 3052 : Handle<PromiseContainer>::cast(microtask)->then()->context(); | |
| 3053 set_context(context->native_context()); | |
| 3011 handle_scope_implementer_->EnterMicrotaskContext( | 3054 handle_scope_implementer_->EnterMicrotaskContext( | 
| 3012 handle(microtask_function->context(), this)); | 3055 Handle<Context>(context, this)); | 
| 3056 | |
| 3057 MaybeHandle<Object> result; | |
| 3013 MaybeHandle<Object> maybe_exception; | 3058 MaybeHandle<Object> maybe_exception; | 
| 3014 MaybeHandle<Object> result = Execution::TryCall( | 3059 | 
| 3015 this, microtask_function, factory()->undefined_value(), 0, NULL, | 3060 if (microtask->IsJSFunction()) { | 
| 3016 &maybe_exception); | 3061 Handle<JSFunction> microtask_function = | 
| 3062 Handle<JSFunction>::cast(microtask); | |
| 3063 result = Execution::TryCall(this, microtask_function, | |
| 3064 factory()->undefined_value(), 0, NULL, | |
| 3065 &maybe_exception); | |
| 3066 } | |
| 3067 | |
| 3068 if (microtask->IsPromiseContainer()) { | |
| 
 
adamk
2016/09/20 20:08:29
Why if/if? shouldn't this be if/else?
 
 | |
| 3069 PromiseResolveThenableJob(Handle<PromiseContainer>::cast(microtask), | |
| 3070 &result, &maybe_exception); | |
| 3071 } | |
| 3072 | |
| 3017 handle_scope_implementer_->LeaveMicrotaskContext(); | 3073 handle_scope_implementer_->LeaveMicrotaskContext(); | 
| 3074 | |
| 3018 // If execution is terminating, just bail out. | 3075 // If execution is terminating, just bail out. | 
| 3019 if (result.is_null() && maybe_exception.is_null()) { | 3076 if (result.is_null() && maybe_exception.is_null()) { | 
| 3020 // Clear out any remaining callbacks in the queue. | 3077 // Clear out any remaining callbacks in the queue. | 
| 3021 heap()->set_microtask_queue(heap()->empty_fixed_array()); | 3078 heap()->set_microtask_queue(heap()->empty_fixed_array()); | 
| 3022 set_pending_microtask_count(0); | 3079 set_pending_microtask_count(0); | 
| 3023 return; | 3080 return; | 
| 3024 } | 3081 } | 
| 3025 } else { | |
| 3026 Handle<CallHandlerInfo> callback_info = | |
| 3027 Handle<CallHandlerInfo>::cast(microtask); | |
| 3028 v8::MicrotaskCallback callback = | |
| 3029 v8::ToCData<v8::MicrotaskCallback>(callback_info->callback()); | |
| 3030 void* data = v8::ToCData<void*>(callback_info->data()); | |
| 3031 callback(data); | |
| 3032 } | 3082 } | 
| 3033 }); | 3083 }); | 
| 3034 } | 3084 } | 
| 3035 } | 3085 } | 
| 3036 | 3086 | 
| 3037 | 3087 | 
| 3038 void Isolate::AddMicrotasksCompletedCallback( | 3088 void Isolate::AddMicrotasksCompletedCallback( | 
| 3039 MicrotasksCompletedCallback callback) { | 3089 MicrotasksCompletedCallback callback) { | 
| 3040 for (int i = 0; i < microtasks_completed_callbacks_.length(); i++) { | 3090 for (int i = 0; i < microtasks_completed_callbacks_.length(); i++) { | 
| 3041 if (callback == microtasks_completed_callbacks_.at(i)) return; | 3091 if (callback == microtasks_completed_callbacks_.at(i)) return; | 
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3208 // Then check whether this scope intercepts. | 3258 // Then check whether this scope intercepts. | 
| 3209 if ((flag & intercept_mask_)) { | 3259 if ((flag & intercept_mask_)) { | 
| 3210 intercepted_flags_ |= flag; | 3260 intercepted_flags_ |= flag; | 
| 3211 return true; | 3261 return true; | 
| 3212 } | 3262 } | 
| 3213 return false; | 3263 return false; | 
| 3214 } | 3264 } | 
| 3215 | 3265 | 
| 3216 } // namespace internal | 3266 } // namespace internal | 
| 3217 } // namespace v8 | 3267 } // namespace v8 | 
| OLD | NEW |