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
5 #include "src/builtins/builtins-utils.h"
6 #include "src/builtins/builtins.h"
7
8 #include "src/promise-utils.h"
9
10 namespace v8 {
11 namespace internal {
12
13 // ES#sec-promise-resolve-functions
14 // Promise Resolve Functions
BUILTIN(PromiseResolveClosure)15 BUILTIN(PromiseResolveClosure) {
16 HandleScope scope(isolate);
17
18 Handle<Context> context(isolate->context(), isolate);
19
20 if (PromiseUtils::HasAlreadyVisited(context)) {
21 return isolate->heap()->undefined_value();
22 }
23
24 PromiseUtils::SetAlreadyVisited(context);
25 Handle<JSObject> promise = handle(PromiseUtils::GetPromise(context), isolate);
26 Handle<Object> value = args.atOrUndefined(isolate, 1);
27
28 MaybeHandle<Object> maybe_result;
29 Handle<Object> argv[] = {promise, value};
30 RETURN_FAILURE_ON_EXCEPTION(
31 isolate, Execution::Call(isolate, isolate->promise_resolve(),
32 isolate->factory()->undefined_value(),
33 arraysize(argv), argv));
34 return isolate->heap()->undefined_value();
35 }
36
37 // ES#sec-promise-reject-functions
38 // Promise Reject Functions
BUILTIN(PromiseRejectClosure)39 BUILTIN(PromiseRejectClosure) {
40 HandleScope scope(isolate);
41
42 Handle<Context> context(isolate->context(), isolate);
43
44 if (PromiseUtils::HasAlreadyVisited(context)) {
45 return isolate->heap()->undefined_value();
46 }
47
48 PromiseUtils::SetAlreadyVisited(context);
49 Handle<Object> value = args.atOrUndefined(isolate, 1);
50 Handle<JSObject> promise = handle(PromiseUtils::GetPromise(context), isolate);
51 Handle<Object> debug_event =
52 handle(PromiseUtils::GetDebugEvent(context), isolate);
53 MaybeHandle<Object> maybe_result;
54 Handle<Object> argv[] = {promise, value, debug_event};
55 RETURN_FAILURE_ON_EXCEPTION(
56 isolate, Execution::Call(isolate, isolate->promise_internal_reject(),
57 isolate->factory()->undefined_value(),
58 arraysize(argv), argv));
59 return isolate->heap()->undefined_value();
60 }
61
62 // ES#sec-createresolvingfunctions
63 // CreateResolvingFunctions ( promise )
BUILTIN(CreateResolvingFunctions)64 BUILTIN(CreateResolvingFunctions) {
65 HandleScope scope(isolate);
66 DCHECK_EQ(3, args.length());
67
68 Handle<JSObject> promise = args.at<JSObject>(1);
69 Handle<Object> debug_event = args.at<Object>(2);
70 Handle<JSFunction> resolve, reject;
71
72 PromiseUtils::CreateResolvingFunctions(isolate, promise, debug_event,
73 &resolve, &reject);
74
75 Handle<FixedArray> result = isolate->factory()->NewFixedArray(2);
76 result->set(0, *resolve);
77 result->set(1, *reject);
78
79 return *isolate->factory()->NewJSArrayWithElements(result, FAST_ELEMENTS, 2,
80 NOT_TENURED);
81 }
82
83 } // namespace internal
84 } // namespace v8
85