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