Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: src/builtins/builtins-promise.cc

Issue 2459283004: [promises] Move CreateResolvingFunctions to c++ (Closed)
Patch Set: address comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/builtins/builtins.h ('k') | src/contexts.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4 #include "src/builtins/builtins-utils.h"
5 #include "src/builtins/builtins.h"
6
7 namespace v8 {
8 namespace internal {
9
10 enum PromiseResolvingFunctionContextSlot {
11 kAlreadyVisitedSlot = Context::MIN_CONTEXT_SLOTS,
12 kPromiseSlot,
13 kDebugEventSlot,
14 kPromiseContextLength,
15 };
16
17 // ES#sec-promise-resolve-functions
18 // Promise Resolve Functions
19 BUILTIN(PromiseResolveClosure) {
20 HandleScope scope(isolate);
21
22 Handle<Context> context(isolate->context(), isolate);
23 Handle<Smi> already_visited(Smi::cast(context->get(kAlreadyVisitedSlot)),
24 isolate);
25
26 if (already_visited->value() != 0) {
27 return isolate->heap()->undefined_value();
28 }
29
30 context->set(kAlreadyVisitedSlot, Smi::FromInt(1));
31 Handle<JSObject> promise(JSObject::cast(context->get(kPromiseSlot)), isolate);
32 Handle<Object> value = args.atOrUndefined(isolate, 1);
33
34 MaybeHandle<Object> maybe_result;
35 Handle<Object> argv[] = {promise, value};
36 RETURN_FAILURE_ON_EXCEPTION(
37 isolate, Execution::Call(isolate, isolate->promise_resolve(),
38 isolate->factory()->undefined_value(),
39 arraysize(argv), argv));
40 return isolate->heap()->undefined_value();
41 }
42
43 // ES#sec-promise-reject-functions
44 // Promise Reject Functions
45 BUILTIN(PromiseRejectClosure) {
46 HandleScope scope(isolate);
47
48 Handle<Context> context(isolate->context(), isolate);
49 Handle<Smi> already_visited(Smi::cast(context->get(kAlreadyVisitedSlot)),
50 isolate);
51
52 if (already_visited->value() != 0) {
53 return isolate->heap()->undefined_value();
54 }
55
56 context->set(kAlreadyVisitedSlot, Smi::FromInt(1));
57
58 Handle<Object> value = args.atOrUndefined(isolate, 1);
59 Handle<JSObject> promise(JSObject::cast(context->get(kPromiseSlot)), isolate);
60 Handle<Object> debug_event(context->get(kDebugEventSlot), isolate);
61 MaybeHandle<Object> maybe_result;
62 Handle<Object> argv[] = {promise, value, debug_event};
63 RETURN_FAILURE_ON_EXCEPTION(
64 isolate, Execution::Call(isolate, isolate->promise_internal_reject(),
65 isolate->factory()->undefined_value(),
66 arraysize(argv), argv));
67 return isolate->heap()->undefined_value();
68 }
69
70 // ES#sec-createresolvingfunctions
71 // CreateResolvingFunctions ( promise )
72 BUILTIN(CreateResolvingFunctions) {
73 HandleScope scope(isolate);
74 DCHECK_EQ(3, args.length());
75
76 Handle<JSObject> promise = args.at<JSObject>(1);
77 Handle<Object> debug_event = args.at<Object>(2);
78
79 Handle<Context> context =
80 isolate->factory()->NewPromiseResolvingFunctionContext(
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
104 Handle<FixedArray> result = isolate->factory()->NewFixedArray(2);
105 result->set(0, *resolve);
106 result->set(1, *reject);
107
108 return *isolate->factory()->NewJSArrayWithElements(result, FAST_ELEMENTS, 2,
109 NOT_TENURED);
110 }
111
112 } // namespace internal
113 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins/builtins.h ('k') | src/contexts.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698