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 BUILTINS_API_TRACE(argv->GetThread(), AsyncIterator, Constructor);
27 return JSTaggedValue::Undefined();
28 }
29
Next(EcmaRuntimeCallInfo * argv)30 JSTaggedValue BuiltinsAsyncIterator::Next([[maybe_unused]] EcmaRuntimeCallInfo *argv)
31 {
32 BUILTINS_API_TRACE(argv->GetThread(), AsyncIterator, Next);
33 return JSTaggedValue::Undefined();
34 }
35
Throw(EcmaRuntimeCallInfo * argv)36 JSTaggedValue BuiltinsAsyncIterator::Throw([[maybe_unused]] EcmaRuntimeCallInfo *argv)
37 {
38 BUILTINS_API_TRACE(argv->GetThread(), AsyncIterator, Throw);
39 return JSTaggedValue::Undefined();
40 }
41
Return(EcmaRuntimeCallInfo * argv)42 JSTaggedValue BuiltinsAsyncIterator::Return(EcmaRuntimeCallInfo *argv)
43 {
44 JSThread *thread = argv->GetThread();
45 BUILTINS_API_TRACE(thread, AsyncIterator, Return);
46 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
47 [[maybe_unused]] EcmaHandleScope handleScope(thread);
48 JSHandle<JSTaggedValue> promiseFunc = env->GetPromiseFunction();
49 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
50 JSHandle<PromiseCapability> pcap = JSPromise::NewPromiseCapability(thread, promiseFunc);
51 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
52 JSHandle<JSObject> iterResult = JSIterator::CreateIterResultObject(thread, value, true);
53 JSHandle<JSTaggedValue> iterResultVal(iterResult);
54 JSHandle<JSTaggedValue> resolve(thread, pcap->GetResolve());
55 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
56 EcmaRuntimeCallInfo* info = EcmaInterpreter::NewRuntimeCallInfo(thread, resolve, undefined, undefined, 1);
57 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
58 info->SetCallArg(iterResultVal.GetTaggedValue());
59 JSFunction::Call(info);
60 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
61 return pcap->GetPromise();
62 }
63
GetAsyncIteratorObj(EcmaRuntimeCallInfo * argv)64 JSTaggedValue BuiltinsAsyncIterator::GetAsyncIteratorObj(EcmaRuntimeCallInfo *argv)
65 {
66 BUILTINS_API_TRACE(argv->GetThread(), AsyncIterator, GetObj);
67 return base::BuiltinsBase::GetThis(argv).GetTaggedValue();
68 }
69 } // namespace panda::ecmascript::builtins
70