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