• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 BaseTestWithScope<false> {
26 };
27 
28 /**
29  * @tc.name: GeneratorValidate
30  * @tc.desc: Get the current status of the generator and return it. If the generator is executing, an exception will
31  *           be thrown.
32  * @tc.type: FUNC
33  * @tc.require:
34  */
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorValidate_001)35 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorValidate_001)
36 {
37     auto vm = thread->GetEcmaVM();
38     auto factory = vm->GetFactory();
39     auto env = vm->GetGlobalEnv();
40     JSHandle<JSGeneratorObject> genOjb = factory->NewJSGeneratorObject(env->GetGeneratorFunctionFunction());
41     JSGeneratorState state = JSGeneratorObject::GeneratorValidate(thread, JSHandle<JSTaggedValue>::Cast(genOjb));
42     EXPECT_EQ(state, JSGeneratorState::UNDEFINED);
43 }
44 
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorValidate_002)45 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorValidate_002)
46 {
47     auto vm = thread->GetEcmaVM();
48     auto env = vm->GetGlobalEnv();
49     JSTaggedValue genObjTagVal =
50         SlowRuntimeStub::CreateGeneratorObj(thread, env->GetGeneratorFunctionFunction().GetTaggedValue());
51     JSHandle<JSGeneratorObject> genObj(thread, genObjTagVal);
52     JSGeneratorState state = JSGeneratorObject::GeneratorValidate(thread, JSHandle<JSTaggedValue>::Cast(genObj));
53     EXPECT_EQ(state, JSGeneratorState::SUSPENDED_START);
54 }
55 
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorValidate_003)56 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorValidate_003)
57 {
58     auto vm = thread->GetEcmaVM();
59     auto factory = vm->GetFactory();
60     auto env = vm->GetGlobalEnv();
61     JSHandle<JSHClass> genClass =
62         factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_GENERATOR_OBJECT, env->GetGeneratorFunctionPrototype());
63     TaggedObject *genObjectHeader = factory->NewObject(genClass);
64     JSHandle<JSGeneratorObject> genObj(thread, JSGeneratorObject::Cast(genObjectHeader));
65     genObj->InitializeHash();
66     genObj->SetElements(thread, factory->EmptyArray(), SKIP_BARRIER);
67     genObj->SetProperties(thread, factory->EmptyArray(), SKIP_BARRIER);
68     genObj->SetGeneratorContext(thread, JSTaggedValue::Undefined());
69     genObj->SetResumeResult(thread, JSTaggedValue::Undefined());
70     JSGeneratorState state = JSGeneratorObject::GeneratorValidate(thread, JSHandle<JSTaggedValue>::Cast(genObj));
71 
72     genObj->SetGeneratorState(JSGeneratorState::COMPLETED);
73     state = JSGeneratorObject::GeneratorValidate(thread, JSHandle<JSTaggedValue>::Cast(genObj));
74     EXPECT_EQ(state, JSGeneratorState::COMPLETED);
75 }
76 
77 /**
78  * @tc.name: GeneratorResume
79  * @tc.desc: Gets the next iteration value of the generator. If the generator has completed, returns
80  *           object(Undefined, True).
81  * @tc.type: FUNC
82  * @tc.require:
83  */
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorResume)84 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorResume)
85 {
86     auto vm = thread->GetEcmaVM();
87     auto env = vm->GetGlobalEnv();
88     JSHandle<JSTaggedValue> valueKey = thread->GlobalConstants()->GetHandledValueString();
89     JSHandle<JSTaggedValue> doneKey = thread->GlobalConstants()->GetHandledDoneString();
90     JSTaggedValue genObjTagVal =
91         SlowRuntimeStub::CreateGeneratorObj(thread, env->GetGeneratorFunctionFunction().GetTaggedValue());
92     JSHandle<JSGeneratorObject> genObj(thread, genObjTagVal);
93 
94     // If state is completed, return object(undefined, true).
95     genObj->SetGeneratorState(JSGeneratorState::COMPLETED);
96     JSHandle<JSObject> result = JSGeneratorObject::GeneratorResume(thread, genObj, JSTaggedValue::Undefined());
97     EXPECT_EQ((JSObject::GetProperty(thread, result, valueKey).GetValue()).GetTaggedValue(),
98         JSTaggedValue::Undefined());
99     EXPECT_EQ((JSObject::GetProperty(thread, result, doneKey).GetValue()).GetTaggedValue(), JSTaggedValue::True());
100 }
101 
102 /**
103  * @tc.name: GeneratorResumeAbrupt
104  * @tc.desc: If the status of the generator is completed, the value in abruptcompletion is returned. Otherwise, the
105  *           generator is in a suspended state, and the Return or Throw value of the generator is returned.
106  * @tc.type: FUNC
107  * @tc.require:
108  */
HWTEST_F_L0(JSGeneratorObjectTest,GeneratorResumeAbrupt)109 HWTEST_F_L0(JSGeneratorObjectTest, GeneratorResumeAbrupt)
110 {
111     auto vm = thread->GetEcmaVM();
112     auto factory = vm->GetFactory();
113     auto env = vm->GetGlobalEnv();
114     JSHandle<JSTaggedValue> valueKey = thread->GlobalConstants()->GetHandledValueString();
115     JSHandle<JSTaggedValue> doneKey = thread->GlobalConstants()->GetHandledDoneString();
116     JSTaggedValue genObjTagVal =
117         SlowRuntimeStub::CreateGeneratorObj(thread, env->GetGeneratorFunctionFunction().GetTaggedValue());
118     JSHandle<JSGeneratorObject> genObj(thread, genObjTagVal);
119     genObj->SetGeneratorState(JSGeneratorState::COMPLETED);
120     CompletionRecordType type = CompletionRecordType::RETURN;
121     JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
122     JSHandle<CompletionRecord> abruptCompletion = factory->NewCompletionRecord(type, value);
123     JSHandle<JSObject> result = JSGeneratorObject::GeneratorResumeAbrupt(thread, genObj, abruptCompletion);
124     EXPECT_EQ((JSObject::GetProperty(thread, result, valueKey).GetValue()).GetTaggedValue(), JSTaggedValue(1));
125     EXPECT_EQ((JSObject::GetProperty(thread, result, doneKey).GetValue()).GetTaggedValue(), JSTaggedValue::True());
126 }
127 }  // namespace panda::test