• 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 
49     // 4.Let iteratorRecord be the Record {[[Iterator]]: asyncIterator, [[NextMethod]]: nextMethod, [[Done]]: false}.
50     JSHandle<AsyncIteratorRecord> iteratorRecord = factory->NewAsyncIteratorRecord(tmpAsyncIterator, nextMethod, false);
51     return JSHandle<JSTaggedValue>(thread, iteratorRecord->GetIterator());
52 }
53 
AsyncFromSyncIteratorContinuation(JSThread * thread,JSHandle<JSTaggedValue> & result,JSHandle<PromiseCapability> & promiseCapability)54 JSTaggedValue JSAsyncFromSyncIterator::AsyncFromSyncIteratorContinuation(JSThread *thread,
55                                                                          JSHandle<JSTaggedValue> &result,
56                                                                          JSHandle<PromiseCapability> &promiseCapability)
57 {
58     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
59     // 1.Let done be IteratorComplete(result).
60     bool done = JSIterator::IteratorComplete(thread, result);
61     // 2.IfAbruptRejectPromise(done, promiseCapability).
62     JSHandle<JSTaggedValue> tmpDone(thread, JSTaggedValue(done));
63     RETURN_REJECT_PROMISE_IF_ABRUPT(thread, tmpDone, promiseCapability);
64     // 3.Let value be IteratorValue(result).
65     JSHandle<JSTaggedValue> value = JSIterator::IteratorValue(thread, result);
66     // 4.IfAbruptRejectPromise(value, promiseCapability).
67     RETURN_REJECT_PROMISE_IF_ABRUPT(thread, value, promiseCapability);
68     // 5.Let valueWrapper be PromiseResolve(%Promise%, value).
69     // 6.IfAbruptRejectPromise(valueWrapper, promiseCapability).
70     // 7.Let steps be the algorithm steps defined in Async-from-Sync Iterator Value Unwrap Functions.
71     // 8.Let length be the number of non-optional parameters of the function definition
72     // in Async-from-Sync Iterator Value Unwrap Functions.
73     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
74     JSHandle<JSTaggedValue> valueWrapper =
75         builtins::BuiltinsPromiseHandler::PromiseResolve(thread,
76                                                          JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()),
77                                                          value);
78     RETURN_REJECT_PROMISE_IF_ABRUPT(thread, valueWrapper, promiseCapability);
79     JSHandle<JSObject> promise = JSHandle<JSObject>::Cast(valueWrapper);
80 
81     // 9.Let onFulfilled be ! CreateBuiltinFunction(steps, length, "", « [[Done]] »).
82     JSHandle<JSAsyncFromSyncIterUnwarpFunction> onFulfilled = factory->NewJSAsyncFromSyncIterUnwarpFunction();
83     // 10.Set onFulfilled.[[Done]] to done.
84 
85     onFulfilled->SetDone(thread, JSTaggedValue(done));
86     // 11.Perform ! PerformPromiseThen(valueWrapper, onFulfilled, undefined, promiseCapability).
87     JSHandle<JSTaggedValue> onFulRejected(thread, JSTaggedValue::Undefined());
88     builtins::BuiltinsPromise::PerformPromiseThen(thread, JSHandle<JSPromise>::Cast(promise),
89                                                   JSHandle<JSTaggedValue>::Cast(onFulfilled),
90                                                   onFulRejected, promiseCapability);
91     // 12.Return promiseCapability.[[Promise]].
92     return promiseCapability->GetPromise();
93 }
94 
AsyncFromSyncIterUnwarpFunction(EcmaRuntimeCallInfo * argv)95 JSTaggedValue JSAsyncFromSyncIterator::AsyncFromSyncIterUnwarpFunction(EcmaRuntimeCallInfo *argv)
96 {
97     // 1. Let F be the active function object.
98     JSThread *thread = argv->GetThread();
99     JSHandle<JSAsyncFromSyncIterUnwarpFunction> unwarpFunction =
100         JSHandle<JSAsyncFromSyncIterUnwarpFunction>::Cast((base::BuiltinsBase::GetConstructor(argv)));
101 
102     // 2.Return ! CreateIterResultObject(value, F.[[Done]]).
103     JSHandle<JSTaggedValue> value = base::BuiltinsBase::GetCallArg(argv, 0);
104     bool done = unwarpFunction->GetDone().ToBoolean();
105     return JSIterator::CreateIterResultObject(thread, value, done).GetTaggedValue();
106 }
107 }  // namespace panda::ecmascript
108