1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "ecmascript/js_async_function.h"
17
18#include "ecmascript/async_generator_helper.h"
19#include "ecmascript/builtins/builtins_promise.h"
20#include "ecmascript/builtins/builtins_promise_handler.h"
21#include "ecmascript/generator_helper.h"
22#include "ecmascript/global_env.h"
23#include "ecmascript/interpreter/interpreter.h"
24#include "ecmascript/js_async_generator_object.h"
25
26namespace panda::ecmascript {
27using BuiltinsPromiseHandler = builtins::BuiltinsPromiseHandler;
28using BuiltinsPromise = builtins::BuiltinsPromise;
29
30void JSAsyncFunction::AsyncFunctionAwait(JSThread *thread, const JSHandle<JSAsyncFuncObject> &asyncFuncObj,
31                                         const JSHandle<JSTaggedValue> &value)
32{
33    // 1.Let asyncContext be the running execution context.
34    auto vm = thread->GetEcmaVM();
35    ObjectFactory *factory = vm->GetFactory();
36
37    JSHandle<JSTaggedValue> asyncCtxt(thread, asyncFuncObj->GetGeneratorContext());
38
39    // 2.Let promiseCapability be ! NewPromiseCapability(%Promise%).
40    JSHandle<GlobalEnv> env = vm->GetGlobalEnv();
41    const GlobalEnvConstants *globalConst = thread->GlobalConstants();
42    JSHandle<PromiseCapability> pcap =
43        JSPromise::NewPromiseCapability(thread, JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()));
44    RETURN_IF_ABRUPT_COMPLETION(thread);
45
46    // 3.Let resolveResult be ! Call(promiseCapability.[[Resolve]], undefined, « value »).
47    JSHandle<JSTaggedValue> resolve(thread, pcap->GetResolve());
48    JSHandle<JSTaggedValue> thisArg = globalConst->GetHandledUndefined();
49
50    JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
51    EcmaRuntimeCallInfo *info =
52        EcmaInterpreter::NewRuntimeCallInfo(thread, resolve, thisArg, undefined, 1);
53    RETURN_IF_ABRUPT_COMPLETION(thread);
54    info->SetCallArg(value.GetTaggedValue());
55    [[maybe_unused]] JSTaggedValue res = JSFunction::Call(info);
56    RETURN_IF_ABRUPT_COMPLETION(thread);
57
58    // 4.Let onFulfilled be a new built-in function object as defined in AsyncFunction Awaited Fulfilled.
59    JSHandle<JSAsyncAwaitStatusFunction> fulFunc = factory->NewJSAsyncAwaitStatusFunction(
60        MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_FULFILLED);
61
62    // 5.Let onRejected be a new built-in function object as defined in AsyncFunction Awaited Rejected.
63    JSHandle<JSAsyncAwaitStatusFunction> rejFunc = factory->NewJSAsyncAwaitStatusFunction(
64        MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_REJECTED);
65
66    // 6.Set onFulfilled.[[AsyncContext]] to asyncContext.
67    // 7.Set onRejected.[[AsyncContext]] to asyncContext.
68    fulFunc->SetAsyncContext(thread, asyncCtxt);
69    rejFunc->SetAsyncContext(thread, asyncCtxt);
70
71    // 8.Let throwawayCapability be ! NewPromiseCapability(%Promise%).
72    // 9.Set throwawayCapability.[[Promise]].[[PromiseIsHandled]] to true.
73    JSHandle<PromiseCapability> tcap =
74        JSPromise::NewPromiseCapability(thread, JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()));
75    RETURN_IF_ABRUPT_COMPLETION(thread);
76    JSHandle<JSPromise>(thread, tcap->GetPromise())->SetPromiseIsHandled(true);
77
78    // 10.Perform ! PerformPromiseThen(promiseCapability.[[Promise]], onFulfilled, onRejected, throwawayCapability).
79    JSHandle<JSPromise> promise(thread, pcap->GetPromise());
80    [[maybe_unused]] JSTaggedValue pres = BuiltinsPromise::PerformPromiseThen(
81        thread, promise, JSHandle<JSTaggedValue>::Cast(fulFunc), JSHandle<JSTaggedValue>::Cast(rejFunc), tcap);
82
83    // 11.Remove asyncContext from the execution context stack and restore the execution context that
84    //    is at the top of the execution context stack as the running execution context.
85    // 12.Set the code evaluation state of asyncContext such that when evaluation is resumed with a Completion
86    //    resumptionValue the following steps will be performed:
87    //   a.Return resumptionValue.
88    // 13.Return.
89}
90
91void JSAsyncFunction::AsyncFunctionAwait(JSThread *thread, const JSHandle<JSTaggedValue> &asyncFuncObj,
92                                         const JSHandle<JSTaggedValue> &value)
93{
94    // 1.Let asyncContext be the running execution context.
95    auto vm = thread->GetEcmaVM();
96    ObjectFactory *factory = vm->GetFactory();
97    JSHandle<JSTaggedValue> asyncCtxt;
98    if (asyncFuncObj->IsAsyncGeneratorObject()) {
99        JSHandle<JSObject> obj = JSTaggedValue::ToObject(thread, asyncFuncObj);
100        RETURN_IF_ABRUPT_COMPLETION(thread);
101        JSHandle<JSAsyncGeneratorObject> asyncGen = JSHandle<JSAsyncGeneratorObject>::Cast(obj);
102        asyncCtxt = JSHandle<JSTaggedValue>(thread, asyncGen->GetGeneratorContext());
103    } else {
104        JSHandle<JSObject> obj = JSTaggedValue::ToObject(thread, asyncFuncObj);
105        RETURN_IF_ABRUPT_COMPLETION(thread);
106        JSHandle<JSAsyncFuncObject> asyncFun = JSHandle<JSAsyncFuncObject>::Cast(obj);
107        asyncCtxt = JSHandle<JSTaggedValue>(thread, asyncFun->GetGeneratorContext());
108    }
109
110    // 2.Let promise be ? PromiseResolve(%Promise%, value).
111    JSHandle<GlobalEnv> env = vm->GetGlobalEnv();
112    JSHandle<JSTaggedValue> promiseValue =
113        builtins::BuiltinsPromiseHandler::PromiseResolve(thread,
114                                                         JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()),
115                                                         value);
116    RETURN_IF_ABRUPT_COMPLETION(thread);
117    // 4.Let onFulfilled be a new built-in function object as defined in AsyncFunction Awaited Fulfilled.
118    JSHandle<JSAsyncAwaitStatusFunction> fulFunc = factory->NewJSAsyncAwaitStatusFunction(
119        MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_FULFILLED);
120
121    // 5.Let onRejected be a new built-in function object as defined in AsyncFunction Awaited Rejected.
122    JSHandle<JSAsyncAwaitStatusFunction> rejFunc = factory->NewJSAsyncAwaitStatusFunction(
123        MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_REJECTED);
124
125    // 6.Set onFulfilled.[[AsyncContext]] to asyncContext.
126    // 7.Set onRejected.[[AsyncContext]] to asyncContext.
127    fulFunc->SetAsyncContext(thread, asyncCtxt);
128    rejFunc->SetAsyncContext(thread, asyncCtxt);
129
130    // 8.Let throwawayCapability be ! NewPromiseCapability(%Promise%).
131    // 9.Set throwawayCapability.[[Promise]].[[PromiseIsHandled]] to true.
132    JSHandle<PromiseCapability> tcap =
133        JSPromise::NewPromiseCapability(thread, JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()));
134    RETURN_IF_ABRUPT_COMPLETION(thread);
135    JSHandle<JSPromise>(thread, tcap->GetPromise())->SetPromiseIsHandled(true);
136
137    // 10.Perform ! PerformPromiseThen(promiseCapability.[[Promise]], onFulfilled, onRejected, throwawayCapability).
138    JSHandle<JSObject> promise = JSHandle<JSObject>::Cast(promiseValue);
139    BuiltinsPromise::PerformPromiseThen(thread, JSHandle<JSPromise>::Cast(promise),
140                                        JSHandle<JSTaggedValue>::Cast(fulFunc),
141                                        JSHandle<JSTaggedValue>::Cast(rejFunc), tcap);
142
143    // 11.Remove asyncContext from the execution context stack and restore the execution context that
144    //    is at the top of the execution context stack as the running execution context.
145    // 12.Set the code evaluation state of asyncContext such that when evaluation is resumed with a Completion
146    //    resumptionValue the following steps will be performed:
147    //   a.Return resumptionValue.
148    // 13.Return.
149}
150
151JSHandle<JSTaggedValue> JSAsyncAwaitStatusFunction::AsyncFunctionAwaitFulfilled(
152    JSThread *thread, const JSHandle<JSAsyncAwaitStatusFunction> &func, const JSHandle<JSTaggedValue> &value)
153{
154    // 1.Let asyncContext be F.[[AsyncContext]].
155    JSHandle<GeneratorContext> asyncCtxt(thread, func->GetAsyncContext());
156
157    JSHandle<JSTaggedValue> tagVal(thread, asyncCtxt->GetGeneratorObject());
158    if (tagVal->IsAsyncGeneratorObject()) {
159        AsyncGeneratorHelper::Next(thread, asyncCtxt, value.GetTaggedValue());
160        return JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined());
161    } else {
162        // 2.Let prevContext be the running execution context.
163        // 3.Suspend prevContext.
164        // 4.Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
165        // 5.Resume the suspended evaluation of asyncContext using NormalCompletion(value) as the result of the
166        //   operation that suspended it. Let result be the value returned by the resumed computation.
167        GeneratorHelper::Next(thread, asyncCtxt, value.GetTaggedValue());
168        // 6.Assert: When we reach this step, asyncContext has already been removed from the execution context stack
169        //   and prevContext is the currently running execution context.
170
171        // 7.Return Completion(result).
172        return JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined());
173    }
174}
175
176JSHandle<JSTaggedValue> JSAsyncAwaitStatusFunction::AsyncFunctionAwaitRejected(
177    JSThread *thread, const JSHandle<JSAsyncAwaitStatusFunction> &func, const JSHandle<JSTaggedValue> &reason)
178{
179    // 1.Let asyncContext be F.[[AsyncContext]].
180    JSHandle<GeneratorContext> asyncCtxt(thread, func->GetAsyncContext());
181
182    JSHandle<JSTaggedValue> tagVal(thread, asyncCtxt->GetGeneratorObject());
183    if (tagVal->IsAsyncGeneratorObject()) {
184        ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
185        JSHandle<CompletionRecord> completionRecord =
186        factory->NewCompletionRecord(CompletionRecordType::THROW, reason);
187        AsyncGeneratorHelper::Throw(thread, asyncCtxt, completionRecord);
188        thread->SetException(JSTaggedValue::Undefined());
189        return JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined());
190    } else {
191    // 2.Let prevContext be the running execution context.
192    // 3.Suspend prevContext.
193    // 4.Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
194    // 5.Resume the suspended evaluation of asyncContext using Completion{[[Type]]: throw,
195    //   [[Value]]: reason, [[Target]]: empty} as the result of the operation that suspended it.
196    //   Let result be the value returned by the resumed computation.
197    JSHandle<JSObject> result = GeneratorHelper::Throw(thread, asyncCtxt, reason.GetTaggedValue());
198    // 6.Assert: When we reach this step, asyncContext has already been removed from the execution context stack
199    //   and prevContext is the currently running execution context.
200    thread->SetException(result.GetTaggedValue());
201    // 7.Return Completion(result).
202    return JSHandle<JSTaggedValue>::Cast(result);
203    }
204}
205}  // namespace panda::ecmascript
206