1 /*
2 * Copyright (c) 2022 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/builtins/builtins_async_iterator.h"
17 #include "ecmascript/base/builtins_base.h"
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_iterator.h"
21 #include "ecmascript/js_promise.h"
22
23 namespace panda::ecmascript::builtins {
AsyncIteratorConstructor(EcmaRuntimeCallInfo * argv)24 JSTaggedValue BuiltinsAsyncIterator::AsyncIteratorConstructor([[maybe_unused]] EcmaRuntimeCallInfo *argv)
25 {
26 return JSTaggedValue::Undefined();
27 }
28
Next(EcmaRuntimeCallInfo * argv)29 JSTaggedValue BuiltinsAsyncIterator::Next([[maybe_unused]] EcmaRuntimeCallInfo *argv)
30 {
31 return JSTaggedValue::Undefined();
32 }
33
Throw(EcmaRuntimeCallInfo * argv)34 JSTaggedValue BuiltinsAsyncIterator::Throw([[maybe_unused]] EcmaRuntimeCallInfo *argv)
35 {
36 return JSTaggedValue::Undefined();
37 }
38
Return(EcmaRuntimeCallInfo * argv)39 JSTaggedValue BuiltinsAsyncIterator::Return(EcmaRuntimeCallInfo *argv)
40 {
41 BUILTINS_API_TRACE(argv->GetThread(), AsyncIterator, Return);
42 JSThread *thread = argv->GetThread();
43 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
44 [[maybe_unused]] EcmaHandleScope handleScope(thread);
45 JSHandle<JSTaggedValue> promiseFunc = env->GetPromiseFunction();
46 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
47 JSHandle<PromiseCapability> pcap = JSPromise::NewPromiseCapability(thread, promiseFunc);
48 JSHandle<JSObject> iterResult = JSIterator::CreateIterResultObject(thread, value, true);
49 JSHandle<JSTaggedValue> iterResultVal(iterResult);
50 JSHandle<JSTaggedValue> resolve(thread, pcap->GetResolve());
51 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
52 EcmaRuntimeCallInfo* info = EcmaInterpreter::NewRuntimeCallInfo(thread, resolve, undefined, undefined, 1);
53 info->SetCallArg(iterResultVal.GetTaggedValue());
54 JSFunction::Call(info);
55 return pcap->GetPromise();
56 }
57
GetAsyncIteratorObj(EcmaRuntimeCallInfo * argv)58 JSTaggedValue BuiltinsAsyncIterator::GetAsyncIteratorObj(EcmaRuntimeCallInfo *argv)
59 {
60 return base::BuiltinsBase::GetThis(argv).GetTaggedValue();
61 }
62 } // namespace panda::ecmascript::builtins
63