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_generator.h"
17 #include "ecmascript/accessor_data.h"
18 #include "ecmascript/js_async_generator_object.h"
19 #include "ecmascript/js_function.h"
20 #include "ecmascript/js_tagged_value-inl.h"
21
22 namespace panda::ecmascript::builtins {
23 // ecma 27.6.1.1
AsyncGeneratorFunctionConstructor(EcmaRuntimeCallInfo * argv)24 JSTaggedValue BuiltinsAsyncGenerator::AsyncGeneratorFunctionConstructor(EcmaRuntimeCallInfo *argv)
25 {
26 BUILTINS_API_TRACE(argv->GetThread(), AsyncGenerator, Constructor);
27 // not support
28 THROW_TYPE_ERROR_AND_RETURN(argv->GetThread(), "Not support eval. Forbidden using new AsyncGeneratorFunction().",
29 JSTaggedValue::Exception());
30 }
31
32 // ecma 27.6.1.2 AsyncGenerator.prototype.next
AsyncGeneratorPrototypeNext(EcmaRuntimeCallInfo * argv)33 JSTaggedValue BuiltinsAsyncGenerator::AsyncGeneratorPrototypeNext(EcmaRuntimeCallInfo *argv)
34 {
35 JSThread *thread = argv->GetThread();
36 BUILTINS_API_TRACE(thread, AsyncGenerator, PrototypeNext);
37 // 1.Let g be the this value.
38 [[maybe_unused]] EcmaHandleScope handleScope(thread);
39 JSHandle<JSTaggedValue> msg = GetThis(argv);
40
41 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
42 // 2. Let completion be NormalCompletion(value).
43 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
44 JSHandle<CompletionRecord> completionRecord =
45 factory->NewCompletionRecord(CompletionRecordType::NORMAL, value);
46 // 3. Return ! AsyncGeneratorEnqueue(generator, completion, empty).
47 return JSAsyncGeneratorObject::AsyncGeneratorEnqueue(thread, msg, completionRecord);
48 }
49
50 // ecma 27.6.1.3 AsyncGenerator.prototype.return
AsyncGeneratorPrototypeReturn(EcmaRuntimeCallInfo * argv)51 JSTaggedValue BuiltinsAsyncGenerator::AsyncGeneratorPrototypeReturn(EcmaRuntimeCallInfo *argv)
52 {
53 JSThread *thread = argv->GetThread();
54 BUILTINS_API_TRACE(thread, AsyncGenerator, PrototypeReturn);
55 // 1.Let generator be the this value.
56 [[maybe_unused]] EcmaHandleScope handleScope(thread);
57 JSHandle<JSTaggedValue> msg = GetThis(argv);
58 // 2.Let completion be Completion { [[Type]]: return, [[Value]]: value, [[Target]]: empty }.
59 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
60 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
61 JSHandle<CompletionRecord> completionRecord =
62 factory->NewCompletionRecord(CompletionRecordType::RETURN, value);
63 // 3.Return ! AsyncGeneratorEnqueue(generator, completion, empty).
64 return JSAsyncGeneratorObject::AsyncGeneratorEnqueue(thread, msg, completionRecord);
65 }
66
67 // ecma 27.6.1.4 AsyncGenerator.prototype.throw
AsyncGeneratorPrototypeThrow(EcmaRuntimeCallInfo * argv)68 JSTaggedValue BuiltinsAsyncGenerator::AsyncGeneratorPrototypeThrow(EcmaRuntimeCallInfo *argv)
69 {
70 JSThread *thread = argv->GetThread();
71 BUILTINS_API_TRACE(thread, AsyncGenerator, PrototypeThrow);
72 // 1.Let generator be the this value.
73 [[maybe_unused]] EcmaHandleScope handleScope(thread);
74 JSHandle<JSTaggedValue> msg = GetThis(argv);
75 // 2.Let completion be ThrowCompletion(exception).
76 JSHandle<JSTaggedValue> exception = GetCallArg(argv, 0);
77 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
78 JSHandle<CompletionRecord> completionRecord =
79 factory->NewCompletionRecord(CompletionRecordType::THROW, exception);
80 // 3.Return ! AsyncGeneratorEnqueue(generator, completion, empty).
81 return JSAsyncGeneratorObject::AsyncGeneratorEnqueue(thread, msg, completionRecord);
82 }
83 } // namespace panda::ecmascript::builtins
84