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