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