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/js_api/js_api_stack_iterator.h"
17 #include "ecmascript/containers/containers_private.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_api/js_api_stack.h"
20 #include "ecmascript/tests/test_helper.h"
21
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::containers;
24
25 namespace panda::test {
26 class JSAPIStackIteratorTest : public testing::Test {
27 public:
SetUpTestCase()28 static void SetUpTestCase()
29 {
30 GTEST_LOG_(INFO) << "SetUpTestCase";
31 }
32
TearDownTestCase()33 static void TearDownTestCase()
34 {
35 GTEST_LOG_(INFO) << "TearDownCase";
36 }
37
SetUp()38 void SetUp() override
39 {
40 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
41 }
42
TearDown()43 void TearDown() override
44 {
45 TestHelper::DestroyEcmaVMWithScope(instance, scope);
46 }
47
48 EcmaVM *instance {nullptr};
49 ecmascript::EcmaHandleScope *scope {nullptr};
50 JSThread *thread {nullptr};
51
52 protected:
CreateJSApiStack(JSThread * thread)53 static JSHandle<JSAPIStack> CreateJSApiStack(JSThread *thread)
54 {
55 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
56 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
57
58 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
59 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
60 JSHandle<JSTaggedValue> value =
61 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
62
63 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
64 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
65 ecmaRuntimeCallInfo->SetThis(value.GetTaggedValue());
66 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::Stack)));
67
68 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
69 JSTaggedValue result = containers::ContainersPrivate::Load(ecmaRuntimeCallInfo);
70 TestHelper::TearDownFrame(thread, prev);
71
72 JSHandle<JSTaggedValue> constructor(thread, result);
73 JSHandle<JSAPIStack> jsStack(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
74 jsStack->SetTop(-1);
75 return jsStack;
76 }
77 };
78
79 /**
80 * @tc.name: Next
81 * @tc.desc: Create an iterator of JSAPIStack,and then loop through the elements of the iterator to check whether
82 * the elements are consistent through Next function.
83 * @tc.type: FUNC
84 * @tc.require:
85 */
HWTEST_F_L0(JSAPIStackIteratorTest,Next)86 HWTEST_F_L0(JSAPIStackIteratorTest, Next)
87 {
88 constexpr uint32_t DEFAULT_LENGTH = 9;
89 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
90 JSHandle<JSAPIStack> jsStack = CreateJSApiStack(thread);
91 EXPECT_TRUE(*jsStack != nullptr);
92 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
93 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
94 // insert value
95 std::string stackValue("keyvalue");
96 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
97 std::string ivalue = stackValue + std::to_string(i);
98 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
99 JSAPIStack::Push(thread, jsStack, value);
100 }
101 JSHandle<JSAPIStackIterator> stackIterator = factory->NewJSAPIStackIterator(jsStack);
102 for (uint32_t i = 0; i <= DEFAULT_LENGTH; i++) {
103 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
104 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
105 ecmaRuntimeCallInfo->SetThis(stackIterator.GetTaggedValue());
106
107 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
108 JSTaggedValue result = JSAPIStackIterator::Next(ecmaRuntimeCallInfo);
109 TestHelper::TearDownFrame(thread, prev);
110
111 JSHandle<JSObject> resultObj(thread, result);
112 if (i <= DEFAULT_LENGTH - 1U) {
113 EXPECT_EQ(stackIterator->GetNextIndex(), (i + 1U));
114 EXPECT_TRUE(JSObject::GetProperty(thread, resultObj, valueStr).GetValue()->IsString());
115 }
116 else {
117 EXPECT_TRUE(stackIterator->GetIteratedStack().IsUndefined());
118 EXPECT_TRUE(JSObject::GetProperty(thread, resultObj, valueStr).GetValue()->IsUndefined());
119 }
120 }
121 }
122
123 /**
124 * @tc.name: Next
125 * @tc.desc: test special return of Next, including throw exception and return undefined
126 * @tc.type: FUNC
127 * @tc.require:
128 */
HWTEST_F_L0(JSAPIStackIteratorTest,SpecialReturnOfNext)129 HWTEST_F_L0(JSAPIStackIteratorTest, SpecialReturnOfNext)
130 {
131 JSHandle<JSAPIStack> jsStack = CreateJSApiStack(thread);
132 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
133 JSHandle<JSAPIStackIterator> stackIterator = factory->NewJSAPIStackIterator(jsStack);
134 stackIterator->SetIteratedStack(thread, JSTaggedValue::Undefined());
135
136 // test Next exception
137 {
138 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
139 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
140 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
141
142 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
143 JSTaggedValue result = JSAPIStackIterator::Next(ecmaRuntimeCallInfo);
144 TestHelper::TearDownFrame(thread, prev);
145 EXPECT_EQ(result, JSTaggedValue::Exception());
146 EXPECT_EXCEPTION();
147 }
148
149 // test Next return undefined
150 {
151 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
152 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
153 ecmaRuntimeCallInfo->SetThis(stackIterator.GetTaggedValue());
154
155 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
156 JSTaggedValue result = JSAPIStackIterator::Next(ecmaRuntimeCallInfo);
157 TestHelper::TearDownFrame(thread, prev);
158 EXPECT_EQ(result, thread->GlobalConstants()->GetUndefinedIterResult());
159 }
160 }
161
162 /**
163 * @tc.name: SetIteratedStack
164 * @tc.desc: Call the "SetIteratedStack" function, check whether the result returned through "GetIteratedStack"
165 * function from the JSAPIStackIterator is within expectations.
166 * @tc.type: FUNC
167 * @tc.require:
168 */
HWTEST_F_L0(JSAPIStackIteratorTest,SetIteratedStack)169 HWTEST_F_L0(JSAPIStackIteratorTest, SetIteratedStack)
170 {
171 constexpr uint32_t DEFAULT_LENGTH = 9;
172 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
173 JSHandle<JSAPIStack> jsStack1 = CreateJSApiStack(thread);
174 JSHandle<JSAPIStack> jsStack2 = CreateJSApiStack(thread);
175 EXPECT_TRUE(*jsStack1 != nullptr);
176 EXPECT_TRUE(*jsStack2 != nullptr);
177 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
178 // insert value
179 std::string stackValue("keyvalue");
180 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
181 std::string ivalue = stackValue + std::to_string(i);
182 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
183 JSAPIStack::Push(thread, jsStack1, value);
184 }
185
186 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
187 std::string ivalue = stackValue + std::to_string(i + 2U);
188 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
189 JSAPIStack::Push(thread, jsStack2, value);
190 }
191 JSHandle<JSAPIStackIterator> stackIterator = factory->NewJSAPIStackIterator(jsStack1);
192 EXPECT_EQ(stackIterator->GetIteratedStack(), jsStack1.GetTaggedValue());
193
194 stackIterator->SetIteratedStack(thread, jsStack2.GetTaggedValue());
195 JSHandle<JSAPIStack> jsAPIStackTo(thread, JSAPIStack::Cast(stackIterator->GetIteratedStack().GetTaggedObject()));
196 EXPECT_EQ(jsAPIStackTo->GetSize(), static_cast<int>(DEFAULT_LENGTH - 1U));
197
198 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
199 std::string ivalue = stackValue + std::to_string(i + 2U);
200 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
201 EXPECT_EQ(jsAPIStackTo->Search(value), static_cast<int>(i));
202 }
203 }
204
205 /**
206 * @tc.name: SetNextIndex
207 * @tc.desc: Call the "SetNextIndex" function, check whether the result returned through "GetNextIndex"
208 * function from the JSAPIStackIterator is within expectations.
209 * @tc.type: FUNC
210 * @tc.require:
211 */
HWTEST_F_L0(JSAPIStackIteratorTest,SetNextIndex)212 HWTEST_F_L0(JSAPIStackIteratorTest, SetNextIndex)
213 {
214 constexpr uint32_t DEFAULT_LENGTH = 9;
215 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
216 JSHandle<JSAPIStack> jsStack = CreateJSApiStack(thread);
217 EXPECT_TRUE(*jsStack != nullptr);
218 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
219 // insert value
220 std::string stackValue("keyvalue");
221 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
222 std::string ivalue = stackValue + std::to_string(i);
223 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
224 JSAPIStack::Push(thread, jsStack, value);
225 }
226 JSHandle<JSAPIStackIterator> stackIterator = factory->NewJSAPIStackIterator(jsStack);
227 EXPECT_EQ(stackIterator->GetNextIndex(), 0U);
228
229 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
230 stackIterator->SetNextIndex(i);
231 EXPECT_EQ(stackIterator->GetNextIndex(), i);
232 }
233 }
234 } // namespace panda::ecmascript
235