• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_from_sync_iterator.h"
17 
18 #include "ecmascript/base/builtins_base.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/global_env.h"
23 #include "ecmascript/js_tagged_value.h"
24 #include "ecmascript/js_iterator.h"
25 #include "ecmascript/js_promise.h"
26 #include "ecmascript/js_function.h"
27 #include "ecmascript/object_factory.h"
28 
29 #include "libpandabase/macros.h"
30 
31 namespace panda::ecmascript {
CreateAsyncFromSyncIterator(JSThread * thread,JSHandle<AsyncIteratorRecord> & syncIteratorRecord)32 JSHandle<JSTaggedValue> JSAsyncFromSyncIterator::CreateAsyncFromSyncIterator(JSThread *thread,
33     JSHandle<AsyncIteratorRecord> &syncIteratorRecord)
34 {
35     // 1.Let asyncIterator be ! OrdinaryObjectCreate(%AsyncFromSyncIteratorPrototype%, « [[SyncIteratorRecord]] »).
36     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
37     JSHandle<JSFunction> IterFunc(env->GetAsyncFromSyncIterator());
38     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
39     JSHandle<JSAsyncFromSyncIterator> asyncIterator(factory->NewJSObjectByConstructor(IterFunc));
40 
41     // 2.Set asyncIterator.[[SyncIteratorRecord]] to syncIteratorRecord.
42     asyncIterator->SetSyncIteratorRecord(thread, syncIteratorRecord);
43 
44     // 3.Let nextMethod be ! Get(asyncIterator, "next").
45     JSHandle<JSTaggedValue> nextStr = thread->GlobalConstants()->GetHandledNextString();
46     JSHandle<JSTaggedValue> tmpAsyncIterator(thread, asyncIterator.GetTaggedValue());
47     JSHandle<JSTaggedValue> nextMethod = JSTaggedValue::GetProperty(thread, tmpAsyncIterator, nextStr).GetValue();
48     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
49 
50     // 4.Let iteratorRecord be the Record {[[Iterator]]: asyncIterator, [[NextMethod]]: nextMethod, [[Done]]: false}.
51     JSHandle<AsyncIteratorRecord> iteratorRecord = factory->NewAsyncIteratorRecord(tmpAsyncIterator, nextMethod, false);
52     return JSHandle<JSTaggedValue>(thread, iteratorRecord->GetIterator());
53 }
54 
AsyncFromSyncIteratorContinuation(JSThread * thread,JSHandle<JSTaggedValue> & result,JSHandle<PromiseCapability> & promiseCapability)55 JSTaggedValue JSAsyncFromSyncIterator::AsyncFromSyncIteratorContinuation(JSThread *thread,
56                                                                          JSHandle<JSTaggedValue> &result,
57                                                                          JSHandle<PromiseCapability> &promiseCapability)
58 {
59     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
60     // 1.Let done be IteratorComplete(result).
61     bool done = JSIterator::IteratorComplete(thread, result);
62     // 2.IfAbruptRejectPromise(done, promiseCapability).
63     JSHandle<JSTaggedValue> tmpDone(thread, JSTaggedValue(done));
64     RETURN_REJECT_PROMISE_IF_ABRUPT(thread, tmpDone, promiseCapability);
65     // 3.Let value be IteratorValue(result).
66     JSHandle<JSTaggedValue> value = JSIterator::IteratorValue(thread, result);
67     // 4.IfAbruptRejectPromise(value, promiseCapability).
68     RETURN_REJECT_PROMISE_IF_ABRUPT(thread, value, promiseCapability);
69     // 5.Let valueWrapper be PromiseResolve(%Promise%, value).
70     // 6.IfAbruptRejectPromise(valueWrapper, promiseCapability).
71     // 7.Let steps be the algorithm steps defined in Async-from-Sync Iterator Value Unwrap Functions.
72     // 8.Let length be the number of non-optional parameters of the function definition
73     // in Async-from-Sync Iterator Value Unwrap Functions.
74     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
75     JSHandle<JSTaggedValue> valueWrapper =
76         builtins::BuiltinsPromiseHandler::PromiseResolve(thread,
77                                                          JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()),
78                                                          value);
79     RETURN_REJECT_PROMISE_IF_ABRUPT(thread, valueWrapper, promiseCapability);
80     JSHandle<JSObject> promise = JSHandle<JSObject>::Cast(valueWrapper);
81 
82     // 9.Let onFulfilled be ! CreateBuiltinFunction(steps, length, "", « [[Done]] »).
83     JSHandle<JSAsyncFromSyncIterUnwarpFunction> onFulfilled = factory->NewJSAsyncFromSyncIterUnwarpFunction();
84     // 10.Set onFulfilled.[[Done]] to done.
85 
86     onFulfilled->SetDone(thread, JSTaggedValue(done));
87     // 11.Perform ! PerformPromiseThen(valueWrapper, onFulfilled, undefined, promiseCapability).
88     JSHandle<JSTaggedValue> onFulRejected(thread, JSTaggedValue::Undefined());
89     builtins::BuiltinsPromise::PerformPromiseThen(thread, JSHandle<JSPromise>::Cast(promise),
90                                                   JSHandle<JSTaggedValue>::Cast(onFulfilled),
91                                                   onFulRejected, promiseCapability);
92     // 12.Return promiseCapability.[[Promise]].
93     return promiseCapability->GetPromise();
94 }
95 
AsyncFromSyncIterUnwarpFunction(EcmaRuntimeCallInfo * argv)96 JSTaggedValue JSAsyncFromSyncIterator::AsyncFromSyncIterUnwarpFunction(EcmaRuntimeCallInfo *argv)
97 {
98     // 1. Let F be the active function object.
99     JSThread *thread = argv->GetThread();
100     JSHandle<JSAsyncFromSyncIterUnwarpFunction> unwarpFunction =
101         JSHandle<JSAsyncFromSyncIterUnwarpFunction>::Cast((base::BuiltinsBase::GetConstructor(argv)));
102 
103     // 2.Return ! CreateIterResultObject(value, F.[[Done]]).
104     JSHandle<JSTaggedValue> value = base::BuiltinsBase::GetCallArg(argv, 0);
105     bool done = unwarpFunction->GetDone().ToBoolean();
106     return JSIterator::CreateIterResultObject(thread, value, done).GetTaggedValue();
107 }
108 }  // namespace panda::ecmascript
109