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/global_env.h"
17 #include "ecmascript/js_generator_object.h"
18 #include "ecmascript/interpreter/slow_runtime_stub.h"
19 #include "ecmascript/tests/test_helper.h"
20
21 using namespace panda;
22 using namespace panda::ecmascript;
23
24 namespace panda::test {
25 class JSGeneratorObjectTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "TearDownCase";
35 }
36
SetUp()37 void SetUp() override
38 {
39 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
40 }
41
TearDown()42 void TearDown() override
43 {
44 TestHelper::DestroyEcmaVMWithScope(instance, scope);
45 }
46
47 EcmaVM *instance {nullptr};
48 ecmascript::EcmaHandleScope *scope {nullptr};
49 JSThread *thread {nullptr};
50 };
51
52 /**
53 * @tc.name: GeneratorValidate
54 * @tc.desc: Get the current status of the generator and return it. If the generator is executing, an exception will
55 * be thrown.
56 * @tc.type: FUNC
57 * @tc.require:
58 */
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorValidate_001)59 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorValidate_001)
60 {
61 auto vm = thread->GetEcmaVM();
62 auto factory = vm->GetFactory();
63 auto env = vm->GetGlobalEnv();
64 JSHandle<JSGeneratorObject> genOjb = factory->NewJSGeneratorObject(env->GetGeneratorFunctionFunction());
65 JSGeneratorState state = JSGeneratorObject::GeneratorValidate(thread, JSHandle<JSTaggedValue>::Cast(genOjb));
66 EXPECT_EQ(state, JSGeneratorState::UNDEFINED);
67 }
68
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorValidate_002)69 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorValidate_002)
70 {
71 auto vm = thread->GetEcmaVM();
72 auto env = vm->GetGlobalEnv();
73 JSTaggedValue genObjTagVal =
74 SlowRuntimeStub::CreateGeneratorObj(thread, env->GetGeneratorFunctionFunction().GetTaggedValue());
75 JSHandle<JSGeneratorObject> genObj(thread, genObjTagVal);
76 JSGeneratorState state = JSGeneratorObject::GeneratorValidate(thread, JSHandle<JSTaggedValue>::Cast(genObj));
77 EXPECT_EQ(state, JSGeneratorState::SUSPENDED_START);
78 }
79
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorValidate_003)80 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorValidate_003)
81 {
82 auto vm = thread->GetEcmaVM();
83 auto factory = vm->GetFactory();
84 auto env = vm->GetGlobalEnv();
85 JSHandle<JSHClass> genClass =
86 factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_GENERATOR_OBJECT, env->GetGeneratorFunctionPrototype());
87 TaggedObject *genObjectHeader = factory->NewObject(genClass);
88 JSHandle<JSGeneratorObject> genObj(thread, JSGeneratorObject::Cast(genObjectHeader));
89 JSGeneratorState state = JSGeneratorObject::GeneratorValidate(thread, JSHandle<JSTaggedValue>::Cast(genObj));
90 EXPECT_EQ(state, JSGeneratorState::SUSPENDED_YIELD);
91
92 genObj->SetGeneratorState(JSGeneratorState::COMPLETED);
93 state = JSGeneratorObject::GeneratorValidate(thread, JSHandle<JSTaggedValue>::Cast(genObj));
94 EXPECT_EQ(state, JSGeneratorState::COMPLETED);
95 }
96
97 /**
98 * @tc.name: GeneratorResume
99 * @tc.desc: Gets the next iteration value of the generator. If the generator has completed, returns
100 * object(Undefined, True).
101 * @tc.type: FUNC
102 * @tc.require:
103 */
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorResume)104 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorResume)
105 {
106 auto vm = thread->GetEcmaVM();
107 auto env = vm->GetGlobalEnv();
108 JSHandle<JSTaggedValue> valueKey = thread->GlobalConstants()->GetHandledValueString();
109 JSHandle<JSTaggedValue> doneKey = thread->GlobalConstants()->GetHandledDoneString();
110 JSTaggedValue genObjTagVal =
111 SlowRuntimeStub::CreateGeneratorObj(thread, env->GetGeneratorFunctionFunction().GetTaggedValue());
112 JSHandle<JSGeneratorObject> genObj(thread, genObjTagVal);
113
114 // If state is completed, return object(undefined, true).
115 genObj->SetGeneratorState(JSGeneratorState::COMPLETED);
116 JSHandle<JSObject> result = JSGeneratorObject::GeneratorResume(thread, genObj, JSTaggedValue::Undefined());
117 EXPECT_EQ((JSObject::GetProperty(thread, result, valueKey).GetValue()).GetTaggedValue(),
118 JSTaggedValue::Undefined());
119 EXPECT_EQ((JSObject::GetProperty(thread, result, doneKey).GetValue()).GetTaggedValue(), JSTaggedValue::True());
120 }
121
122 /**
123 * @tc.name: GeneratorResumeAbrupt
124 * @tc.desc: If the status of the generator is completed, the value in abruptcompletion is returned. Otherwise, the
125 * generator is in a suspended state, and the Return or Throw value of the generator is returned.
126 * @tc.type: FUNC
127 * @tc.require:
128 */
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorResumeAbrupt)129 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorResumeAbrupt)
130 {
131 auto vm = thread->GetEcmaVM();
132 auto factory = vm->GetFactory();
133 auto env = vm->GetGlobalEnv();
134 JSHandle<JSTaggedValue> valueKey = thread->GlobalConstants()->GetHandledValueString();
135 JSHandle<JSTaggedValue> doneKey = thread->GlobalConstants()->GetHandledDoneString();
136 JSTaggedValue genObjTagVal =
137 SlowRuntimeStub::CreateGeneratorObj(thread, env->GetGeneratorFunctionFunction().GetTaggedValue());
138 JSHandle<JSGeneratorObject> genObj(thread, genObjTagVal);
139 genObj->SetGeneratorState(JSGeneratorState::COMPLETED);
140 CompletionRecordType type = CompletionRecordType::RETURN;
141 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
142 JSHandle<CompletionRecord> abruptCompletion = factory->NewCompletionRecord(type, value);
143 JSHandle<JSObject> result = JSGeneratorObject::GeneratorResumeAbrupt(thread, genObj, abruptCompletion);
144 EXPECT_EQ((JSObject::GetProperty(thread, result, valueKey).GetValue()).GetTaggedValue(), JSTaggedValue(1));
145 EXPECT_EQ((JSObject::GetProperty(thread, result, doneKey).GetValue()).GetTaggedValue(), JSTaggedValue::True());
146 }
147 } // namespace panda::test