 Chromium Code Reviews
 Chromium Code Reviews Issue 2487053002:
  [promises] Remove one runtime call to create_resolving_functions  (Closed)
    
  
    Issue 2487053002:
  [promises] Remove one runtime call to create_resolving_functions  (Closed) 
  | OLD | NEW | 
|---|---|
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 #include "src/builtins/builtins-utils.h" | 5 #include "src/builtins/builtins-utils.h" | 
| 5 #include "src/builtins/builtins.h" | 6 #include "src/builtins/builtins.h" | 
| 6 | 7 | 
| 8 #include "src/promise-utils.h" | |
| 9 | |
| 7 namespace v8 { | 10 namespace v8 { | 
| 8 namespace internal { | 11 namespace internal { | 
| 9 | 12 | 
| 10 enum PromiseResolvingFunctionContextSlot { | |
| 11 kAlreadyVisitedSlot = Context::MIN_CONTEXT_SLOTS, | |
| 12 kPromiseSlot, | |
| 13 kDebugEventSlot, | |
| 14 kPromiseContextLength, | |
| 15 }; | |
| 16 | |
| 17 // ES#sec-promise-resolve-functions | 13 // ES#sec-promise-resolve-functions | 
| 18 // Promise Resolve Functions | 14 // Promise Resolve Functions | 
| 19 BUILTIN(PromiseResolveClosure) { | 15 BUILTIN(PromiseResolveClosure) { | 
| 20 HandleScope scope(isolate); | 16 HandleScope scope(isolate); | 
| 21 | 17 | 
| 22 Handle<Context> context(isolate->context(), isolate); | 18 Handle<Context> context(isolate->context(), isolate); | 
| 23 Handle<Smi> already_visited(Smi::cast(context->get(kAlreadyVisitedSlot)), | 19 Handle<Smi> already_visited = | 
| 24 isolate); | 20 PromiseUtils::GetAlreadyVisited(isolate, context); | 
| 
adamk
2016/11/10 18:10:54
How about making GetAlreadyVisited() return a bool
 
gsathya
2016/11/11 02:34:36
Done.
 | |
| 25 | 21 | 
| 26 if (already_visited->value() != 0) { | 22 if (already_visited->value() != 0) { | 
| 27 return isolate->heap()->undefined_value(); | 23 return isolate->heap()->undefined_value(); | 
| 28 } | 24 } | 
| 29 | 25 | 
| 30 context->set(kAlreadyVisitedSlot, Smi::FromInt(1)); | 26 PromiseUtils::SetAlreadyVisitedToTrue(context); | 
| 31 Handle<JSObject> promise(JSObject::cast(context->get(kPromiseSlot)), isolate); | 27 Handle<JSObject> promise = PromiseUtils::GetPromise(isolate, context); | 
| 32 Handle<Object> value = args.atOrUndefined(isolate, 1); | 28 Handle<Object> value = args.atOrUndefined(isolate, 1); | 
| 33 | 29 | 
| 34 MaybeHandle<Object> maybe_result; | 30 MaybeHandle<Object> maybe_result; | 
| 35 Handle<Object> argv[] = {promise, value}; | 31 Handle<Object> argv[] = {promise, value}; | 
| 36 RETURN_FAILURE_ON_EXCEPTION( | 32 RETURN_FAILURE_ON_EXCEPTION( | 
| 37 isolate, Execution::Call(isolate, isolate->promise_resolve(), | 33 isolate, Execution::Call(isolate, isolate->promise_resolve(), | 
| 38 isolate->factory()->undefined_value(), | 34 isolate->factory()->undefined_value(), | 
| 39 arraysize(argv), argv)); | 35 arraysize(argv), argv)); | 
| 40 return isolate->heap()->undefined_value(); | 36 return isolate->heap()->undefined_value(); | 
| 41 } | 37 } | 
| 42 | 38 | 
| 43 // ES#sec-promise-reject-functions | 39 // ES#sec-promise-reject-functions | 
| 44 // Promise Reject Functions | 40 // Promise Reject Functions | 
| 45 BUILTIN(PromiseRejectClosure) { | 41 BUILTIN(PromiseRejectClosure) { | 
| 46 HandleScope scope(isolate); | 42 HandleScope scope(isolate); | 
| 47 | 43 | 
| 48 Handle<Context> context(isolate->context(), isolate); | 44 Handle<Context> context(isolate->context(), isolate); | 
| 49 Handle<Smi> already_visited(Smi::cast(context->get(kAlreadyVisitedSlot)), | 45 Handle<Smi> already_visited = | 
| 50 isolate); | 46 PromiseUtils::GetAlreadyVisited(isolate, context); | 
| 51 | 47 | 
| 52 if (already_visited->value() != 0) { | 48 if (already_visited->value() != 0) { | 
| 53 return isolate->heap()->undefined_value(); | 49 return isolate->heap()->undefined_value(); | 
| 54 } | 50 } | 
| 55 | 51 | 
| 56 context->set(kAlreadyVisitedSlot, Smi::FromInt(1)); | 52 PromiseUtils::SetAlreadyVisitedToTrue(context); | 
| 57 | |
| 58 Handle<Object> value = args.atOrUndefined(isolate, 1); | 53 Handle<Object> value = args.atOrUndefined(isolate, 1); | 
| 59 Handle<JSObject> promise(JSObject::cast(context->get(kPromiseSlot)), isolate); | 54 Handle<JSObject> promise = PromiseUtils::GetPromise(isolate, context); | 
| 60 Handle<Object> debug_event(context->get(kDebugEventSlot), isolate); | 55 Handle<Object> debug_event = PromiseUtils::GetDebugEvent(isolate, context); | 
| 61 MaybeHandle<Object> maybe_result; | 56 MaybeHandle<Object> maybe_result; | 
| 62 Handle<Object> argv[] = {promise, value, debug_event}; | 57 Handle<Object> argv[] = {promise, value, debug_event}; | 
| 63 RETURN_FAILURE_ON_EXCEPTION( | 58 RETURN_FAILURE_ON_EXCEPTION( | 
| 64 isolate, Execution::Call(isolate, isolate->promise_internal_reject(), | 59 isolate, Execution::Call(isolate, isolate->promise_internal_reject(), | 
| 65 isolate->factory()->undefined_value(), | 60 isolate->factory()->undefined_value(), | 
| 66 arraysize(argv), argv)); | 61 arraysize(argv), argv)); | 
| 67 return isolate->heap()->undefined_value(); | 62 return isolate->heap()->undefined_value(); | 
| 68 } | 63 } | 
| 69 | 64 | 
| 70 // ES#sec-createresolvingfunctions | 65 // ES#sec-createresolvingfunctions | 
| 71 // CreateResolvingFunctions ( promise ) | 66 // CreateResolvingFunctions ( promise ) | 
| 72 BUILTIN(CreateResolvingFunctions) { | 67 BUILTIN(CreateResolvingFunctions) { | 
| 73 HandleScope scope(isolate); | 68 HandleScope scope(isolate); | 
| 74 DCHECK_EQ(3, args.length()); | 69 DCHECK_EQ(3, args.length()); | 
| 75 | 70 | 
| 76 Handle<JSObject> promise = args.at<JSObject>(1); | 71 Handle<JSObject> promise = args.at<JSObject>(1); | 
| 77 Handle<Object> debug_event = args.at<Object>(2); | 72 Handle<Object> debug_event = args.at<Object>(2); | 
| 
adamk
2016/11/10 18:10:54
Can you add a DCHECK(debug_event->IsTrue() || debu
 
gsathya
2016/11/11 02:34:36
Done.
 | |
| 73 Handle<JSFunction> resolve, reject; | |
| 78 | 74 | 
| 79 Handle<Context> context = | 75 PromiseUtils::CreateResolvingFunctions(isolate, promise, debug_event, | 
| 80 isolate->factory()->NewPromiseResolvingFunctionContext( | 76 &resolve, &reject); | 
| 81 kPromiseContextLength); | |
| 82 context->set_native_context(*isolate->native_context()); | |
| 83 context->set(kAlreadyVisitedSlot, Smi::kZero); | |
| 84 context->set(kPromiseSlot, *promise); | |
| 85 context->set(kDebugEventSlot, *debug_event); | |
| 86 | |
| 87 Handle<SharedFunctionInfo> resolve_shared_fun( | |
| 88 isolate->native_context()->promise_resolve_shared_fun(), isolate); | |
| 89 Handle<JSFunction> resolve = | |
| 90 isolate->factory()->NewFunctionFromSharedFunctionInfo( | |
| 91 isolate->sloppy_function_without_prototype_map(), resolve_shared_fun, | |
| 92 isolate->native_context(), TENURED); | |
| 93 | |
| 94 Handle<SharedFunctionInfo> reject_shared_fun( | |
| 95 isolate->native_context()->promise_reject_shared_fun(), isolate); | |
| 96 Handle<JSFunction> reject = | |
| 97 isolate->factory()->NewFunctionFromSharedFunctionInfo( | |
| 98 isolate->sloppy_function_without_prototype_map(), reject_shared_fun, | |
| 99 isolate->native_context(), TENURED); | |
| 100 | |
| 101 resolve->set_context(*context); | |
| 102 reject->set_context(*context); | |
| 103 | 77 | 
| 104 Handle<FixedArray> result = isolate->factory()->NewFixedArray(2); | 78 Handle<FixedArray> result = isolate->factory()->NewFixedArray(2); | 
| 105 result->set(0, *resolve); | 79 result->set(0, *resolve); | 
| 106 result->set(1, *reject); | 80 result->set(1, *reject); | 
| 107 | 81 | 
| 108 return *isolate->factory()->NewJSArrayWithElements(result, FAST_ELEMENTS, 2, | 82 return *isolate->factory()->NewJSArrayWithElements(result, FAST_ELEMENTS, 2, | 
| 109 NOT_TENURED); | 83 NOT_TENURED); | 
| 110 } | 84 } | 
| 111 | 85 | 
| 112 } // namespace internal | 86 } // namespace internal | 
| 113 } // namespace v8 | 87 } // namespace v8 | 
| OLD | NEW |