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