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