1 /*
2 * Copyright (c) 2021 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 "builtins_generator.h"
17
18 #include "ecmascript/js_function.h"
19 #include "ecmascript/js_generator_object.h"
20
21 namespace panda::ecmascript::builtins {
22 // 26.2.1.1 GeneratorFunction(p1, p2, … , pn, body)
GeneratorFunctionConstructor(EcmaRuntimeCallInfo * argv)23 JSTaggedValue BuiltinsGenerator::GeneratorFunctionConstructor(EcmaRuntimeCallInfo *argv)
24 {
25 BUILTINS_API_TRACE(argv->GetThread(), Generator, Constructor);
26 // not support
27 THROW_TYPE_ERROR_AND_RETURN(argv->GetThread(), "Not support eval. Forbidden using new GeneratorFunction().",
28 JSTaggedValue::Exception());
29 }
30
31 // 26.4.1.2 Generator.prototype.next(value)
GeneratorPrototypeNext(EcmaRuntimeCallInfo * argv)32 JSTaggedValue BuiltinsGenerator::GeneratorPrototypeNext(EcmaRuntimeCallInfo *argv)
33 {
34 BUILTINS_API_TRACE(argv->GetThread(), Generator, PrototypeNext);
35 // 1.Let g be the this value.
36 JSThread *thread = argv->GetThread();
37 [[maybe_unused]] EcmaHandleScope handleScope(thread);
38 JSHandle<JSTaggedValue> msg = GetThis(argv);
39 if (!msg->IsGeneratorObject()) {
40 THROW_TYPE_ERROR_AND_RETURN(thread, "Not a generator object.", JSTaggedValue::Exception());
41 }
42 JSHandle<JSGeneratorObject> generator(thread, JSGeneratorObject::Cast(*JSTaggedValue::ToObject(thread, msg)));
43 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
44 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
45
46 // 2.Return ? GeneratorResume(g, value).
47 JSHandle<JSObject> result = JSGeneratorObject::GeneratorResume(thread, generator, value.GetTaggedValue());
48 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
49 return result.GetTaggedValue();
50 }
51
52 // 26.4.1.3 Generator.prototype.return(value)
GeneratorPrototypeReturn(EcmaRuntimeCallInfo * argv)53 JSTaggedValue BuiltinsGenerator::GeneratorPrototypeReturn(EcmaRuntimeCallInfo *argv)
54 {
55 BUILTINS_API_TRACE(argv->GetThread(), Generator, PrototypeReturn);
56 // 1.Let g be the this value.
57 JSThread *thread = argv->GetThread();
58 [[maybe_unused]] EcmaHandleScope handleScope(thread);
59 JSHandle<JSTaggedValue> msg = GetThis(argv);
60 if (!msg->IsGeneratorObject()) {
61 THROW_TYPE_ERROR_AND_RETURN(thread, "Not a generator object.", JSTaggedValue::Exception());
62 }
63 JSHandle<JSGeneratorObject> generator(thread, JSGeneratorObject::Cast(*JSTaggedValue::ToObject(thread, msg)));
64 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
65 // 2.Let C be Completion { [[Type]]: return, [[Value]]: value, [[Target]]: empty }.
66 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
67 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
68 JSHandle<CompletionRecord> completionRecord =
69 factory->NewCompletionRecord(CompletionRecordType::RETURN, value);
70
71 // 3.Return ? GeneratorResumeAbrupt(g, C).
72 JSHandle<JSObject> result = JSGeneratorObject::GeneratorResumeAbrupt(thread, generator, completionRecord);
73 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
74 return result.GetTaggedValue();
75 }
76
77 // 26.4.1.4 Generator.prototype.throw(exception)
GeneratorPrototypeThrow(EcmaRuntimeCallInfo * argv)78 JSTaggedValue BuiltinsGenerator::GeneratorPrototypeThrow(EcmaRuntimeCallInfo *argv)
79 {
80 BUILTINS_API_TRACE(argv->GetThread(), Generator, PrototypeThrow);
81 // 1.Let g be the this value.
82 JSThread *thread = argv->GetThread();
83 [[maybe_unused]] EcmaHandleScope handleScope(thread);
84 JSHandle<JSTaggedValue> msg = GetThis(argv);
85 if (!msg->IsGeneratorObject()) {
86 THROW_TYPE_ERROR_AND_RETURN(thread, "Not a generator object.", JSTaggedValue::Exception());
87 }
88 JSHandle<JSGeneratorObject> generator(thread, JSGeneratorObject::Cast(*JSTaggedValue::ToObject(thread, msg)));
89 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
90 // 2.Let C be ThrowCompletion(exception).
91 JSHandle<JSTaggedValue> exception = GetCallArg(argv, 0);
92 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
93 JSHandle<CompletionRecord> completionRecord =
94 factory->NewCompletionRecord(CompletionRecordType::THROW, exception);
95
96 // 3.Return ? GeneratorResumeAbrupt(g, C).
97 JSHandle<JSObject> result = JSGeneratorObject::GeneratorResumeAbrupt(thread, generator, completionRecord);
98 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
99 return result.GetTaggedValue();
100 }
101 } // namespace panda::ecmascript::builtins
102