• 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/builtins/builtins_promise.h"
19 #include "ecmascript/builtins/builtins_promise_handler.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/generator_helper.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/internal_call_params.h"
24 #include "ecmascript/js_promise.h"
25 #include "ecmascript/js_tagged_value-inl.h"
26 #include "ecmascript/object_factory.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     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
37 
38     JSHandle<JSTaggedValue> asyncCtxt(thread, asyncFuncObj->GetGeneratorContext());
39 
40     // 2.Let promiseCapability be ! NewPromiseCapability(%Promise%).
41     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
42     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
43     JSHandle<PromiseCapability> pcap =
44         JSPromise::NewPromiseCapability(thread, JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()));
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     InternalCallParams *arguments = thread->GetInternalCallParams();
50     arguments->MakeArgv(value);
51     [[maybe_unused]] JSTaggedValue res = JSFunction::Call(thread, resolve, thisArg, 1, arguments->GetArgv());
52 
53     // 4.Let onFulfilled be a new built-in function object as defined in AsyncFunction Awaited Fulfilled.
54     JSHandle<JSAsyncAwaitStatusFunction> fulFunc =
55         factory->NewJSAsyncAwaitStatusFunction(reinterpret_cast<void *>(BuiltinsPromiseHandler::AsyncAwaitFulfilled));
56 
57     // 5.Let onRejected be a new built-in function object as defined in AsyncFunction Awaited Rejected.
58     JSHandle<JSAsyncAwaitStatusFunction> rejFunc =
59         factory->NewJSAsyncAwaitStatusFunction(reinterpret_cast<void *>(BuiltinsPromiseHandler::AsyncAwaitRejected));
60 
61     // 6.Set onFulfilled.[[AsyncContext]] to asyncContext.
62     // 7.Set onRejected.[[AsyncContext]] to asyncContext.
63     fulFunc->SetAsyncContext(thread, asyncCtxt);
64     rejFunc->SetAsyncContext(thread, asyncCtxt);
65 
66     // 8.Let throwawayCapability be ! NewPromiseCapability(%Promise%).
67     // 9.Set throwawayCapability.[[Promise]].[[PromiseIsHandled]] to true.
68     JSHandle<PromiseCapability> tcap =
69         JSPromise::NewPromiseCapability(thread, JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()));
70     JSHandle<JSPromise>(thread, tcap->GetPromise())->SetPromiseIsHandled(true);
71 
72     // 10.Perform ! PerformPromiseThen(promiseCapability.[[Promise]], onFulfilled, onRejected, throwawayCapability).
73     JSHandle<JSPromise> promise(thread, pcap->GetPromise());
74     [[maybe_unused]] JSTaggedValue pres = BuiltinsPromise::PerformPromiseThen(
75         thread, promise, JSHandle<JSTaggedValue>::Cast(fulFunc), JSHandle<JSTaggedValue>::Cast(rejFunc), tcap);
76 
77     // 11.Remove asyncContext from the execution context stack and restore the execution context that
78     //    is at the top of the execution context stack as the running execution context.
79     // 12.Set the code evaluation state of asyncContext such that when evaluation is resumed with a Completion
80     //    resumptionValue the following steps will be performed:
81     //   a.Return resumptionValue.
82     // 13.Return.
83 }
84 
AsyncFunctionAwaitFulfilled(JSThread * thread,const JSHandle<JSAsyncAwaitStatusFunction> & func,const JSHandle<JSTaggedValue> & value)85 JSHandle<JSTaggedValue> JSAsyncAwaitStatusFunction::AsyncFunctionAwaitFulfilled(
86     JSThread *thread, const JSHandle<JSAsyncAwaitStatusFunction> &func, const JSHandle<JSTaggedValue> &value)
87 {
88     // 1.Let asyncContext be F.[[AsyncContext]].
89     JSHandle<GeneratorContext> asyncCtxt(thread, func->GetAsyncContext());
90 
91     // 2.Let prevContext be the running execution context.
92     GeneratorHelper::ChangeGenContext(thread, asyncCtxt);
93     // 3.Suspend prevContext.
94     // 4.Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
95     // 5.Resume the suspended evaluation of asyncContext using NormalCompletion(value) as the result of the
96     //   operation that suspended it. Let result be the value returned by the resumed computation.
97     GeneratorHelper::Next(thread, asyncCtxt, value.GetTaggedValue());
98     GeneratorHelper::ResumeContext(thread);
99     // 6.Assert: When we reach this step, asyncContext has already been removed from the execution context stack
100     //   and prevContext is the currently running execution context.
101 
102     // 7.Return Completion(result).
103     return JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined());
104 }
105 
AsyncFunctionAwaitRejected(JSThread * thread,const JSHandle<JSAsyncAwaitStatusFunction> & func,const JSHandle<JSTaggedValue> & reason)106 JSHandle<JSTaggedValue> JSAsyncAwaitStatusFunction::AsyncFunctionAwaitRejected(
107     JSThread *thread, const JSHandle<JSAsyncAwaitStatusFunction> &func, const JSHandle<JSTaggedValue> &reason)
108 {
109     // 1.Let asyncContext be F.[[AsyncContext]].
110     JSHandle<GeneratorContext> asyncCtxt(thread, func->GetAsyncContext());
111 
112     // 2.Let prevContext be the running execution context.
113     GeneratorHelper::ChangeGenContext(thread, asyncCtxt);
114     // 3.Suspend prevContext.
115     // 4.Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
116     // 5.Resume the suspended evaluation of asyncContext using Completion{[[Type]]: throw,
117     //   [[Value]]: reason, [[Target]]: empty} as the result of the operation that suspended it.
118     //   Let result be the value returned by the resumed computation.
119     JSHandle<JSObject> result = GeneratorHelper::Throw(thread, asyncCtxt, reason.GetTaggedValue());
120     GeneratorHelper::ResumeContext(thread);
121     // 6.Assert: When we reach this step, asyncContext has already been removed from the execution context stack
122     //   and prevContext is the currently running execution context.
123 
124     // 7.Return Completion(result).
125     return JSHandle<JSTaggedValue>::Cast(result);
126 }
127 }  // namespace panda::ecmascript
128