1 /*
2 * Copyright (c) 2021-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 <thread>
17
18 #include "assembler/assembly-emitter.h"
19 #include "assembler/assembly-parser.h"
20 #include "libpandabase/utils/utf.h"
21 #include "libpandafile/class_data_accessor-inl.h"
22
23 #include "ecmascript/builtins/builtins_arraybuffer.h"
24 #include "ecmascript/ecma_vm.h"
25 #include "ecmascript/global_env.h"
26 #include "ecmascript/js_array.h"
27 #include "ecmascript/js_arraybuffer.h"
28 #include "ecmascript/js_hclass.h"
29 #include "ecmascript/js_regexp.h"
30 #include "ecmascript/js_serializer.h"
31 #include "ecmascript/js_set.h"
32 #include "ecmascript/js_thread.h"
33 #include "ecmascript/js_typed_array.h"
34 #include "ecmascript/jspandafile/js_pandafile.h"
35 #include "ecmascript/jspandafile/js_pandafile_manager.h"
36 #include "ecmascript/linked_hash_table.h"
37 #include "ecmascript/mem/c_containers.h"
38 #include "ecmascript/object_factory.h"
39 #include "ecmascript/tests/test_helper.h"
40
41 using namespace panda::ecmascript;
42 using namespace testing::ext;
43 using namespace panda::ecmascript::builtins;
44
45 namespace panda::test {
46 class JSDeserializerTest {
47 public:
JSDeserializerTest()48 JSDeserializerTest() : ecmaVm(nullptr), scope(nullptr), thread(nullptr) {}
Init()49 void Init()
50 {
51 JSRuntimeOptions options;
52 options.SetEnableForceGC(true);
53 ecmaVm = JSNApi::CreateEcmaVM(options);
54 ecmaVm->SetEnableForceGC(true);
55 EXPECT_TRUE(ecmaVm != nullptr) << "Cannot create Runtime";
56 thread = ecmaVm->GetJSThread();
57 scope = new EcmaHandleScope(thread);
58 }
Destroy()59 void Destroy()
60 {
61 delete scope;
62 scope = nullptr;
63 ecmaVm->SetEnableForceGC(false);
64 thread->ClearException();
65 JSNApi::DestroyJSVM(ecmaVm);
66 }
67
JSSpecialValueTest(std::pair<uint8_t *,size_t> data)68 void JSSpecialValueTest(std::pair<uint8_t *, size_t> data)
69 {
70 Init();
71 JSHandle<JSTaggedValue> jsTrue(thread, JSTaggedValue::True());
72 JSHandle<JSTaggedValue> jsFalse(thread, JSTaggedValue::False());
73 JSHandle<JSTaggedValue> jsUndefined(thread, JSTaggedValue::Undefined());
74 JSHandle<JSTaggedValue> jsNull(thread, JSTaggedValue::Null());
75 JSHandle<JSTaggedValue> jsHole(thread, JSTaggedValue::Hole());
76
77 JSDeserializer deserializer(thread, data.first, data.second);
78 JSHandle<JSTaggedValue> retTrue = deserializer.Deserialize();
79 EXPECT_TRUE(JSTaggedValue::SameValue(jsTrue, retTrue)) << "Not same value for JS_TRUE";
80 JSHandle<JSTaggedValue> retFalse = deserializer.Deserialize();
81 EXPECT_TRUE(JSTaggedValue::SameValue(jsFalse, retFalse)) << "Not same value for JS_FALSE";
82 JSHandle<JSTaggedValue> retUndefined = deserializer.Deserialize();
83 JSHandle<JSTaggedValue> retNull = deserializer.Deserialize();
84 JSHandle<JSTaggedValue> retHole = deserializer.Deserialize();
85
86 EXPECT_TRUE(JSTaggedValue::SameValue(jsUndefined, retUndefined)) << "Not same value for JS_UNDEFINED";
87 EXPECT_TRUE(JSTaggedValue::SameValue(jsNull, retNull)) << "Not same value for JS_NULL";
88 EXPECT_TRUE(JSTaggedValue::SameValue(jsHole, retHole)) << "Not same value for JS_HOLE";
89 Destroy();
90 }
91
JSPlainObjectTest1(std::pair<uint8_t *,size_t> data)92 void JSPlainObjectTest1(std::pair<uint8_t *, size_t> data)
93 {
94 Init();
95 JSDeserializer deserializer(thread, data.first, data.second);
96 JSHandle<JSTaggedValue> objValue1 = deserializer.Deserialize();
97 JSHandle<JSTaggedValue> objValue2 = deserializer.Deserialize();
98
99 JSHandle<JSObject> retObj1 = JSHandle<JSObject>::Cast(objValue1);
100 JSHandle<JSObject> retObj2 = JSHandle<JSObject>::Cast(objValue2);
101 EXPECT_FALSE(retObj1.IsEmpty());
102 EXPECT_FALSE(retObj2.IsEmpty());
103
104 JSHandle<TaggedArray> array1 = JSObject::GetOwnPropertyKeys(thread, retObj1);
105 uint32_t length1 = array1->GetLength();
106 EXPECT_EQ(length1, 4U); // 4 : test case
107 double sum1 = 0.0;
108 for (uint32_t i = 0; i < length1; i++) {
109 JSHandle<JSTaggedValue> key(thread, array1->Get(i));
110 double a = JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(retObj1), key).GetValue()->GetNumber();
111 sum1 += a;
112 }
113 EXPECT_EQ(sum1, 10); // 10 : test case
114
115 JSHandle<TaggedArray> array2 = JSObject::GetOwnPropertyKeys(thread, retObj2);
116 uint32_t length2 = array2->GetLength();
117 EXPECT_EQ(length2, 4U); // 4 : test case
118 double sum2 = 0.0;
119 for (uint32_t i = 0; i < length2; i++) {
120 JSHandle<JSTaggedValue> key(thread, array2->Get(i));
121 double a = JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(retObj2), key).GetValue()->GetNumber();
122 sum2 += a;
123 }
124 EXPECT_EQ(sum2, 26); // 26 : test case
125 Destroy();
126 }
127
JSPlainObjectTest2(std::pair<uint8_t *,size_t> data)128 void JSPlainObjectTest2(std::pair<uint8_t *, size_t> data)
129 {
130 Init();
131 ObjectFactory *factory = ecmaVm->GetFactory();
132 JSDeserializer deserializer(thread, data.first, data.second);
133 JSHandle<JSTaggedValue> objValue1 = deserializer.Deserialize();
134
135 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("detectResultItems"));
136 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("adviceId"));
137 OperationResult result = JSObject::GetProperty(thread, objValue1, key1);
138 JSHandle<JSTaggedValue> value1 = result.GetRawValue();
139 EXPECT_TRUE(value1->IsJSArray());
140 JSHandle<JSTaggedValue> value2 = JSArray::FastGetPropertyByValue(thread, value1, 0);
141 JSHandle<JSObject> obj = JSHandle<JSObject>::Cast(value2);
142 bool res = JSObject::HasProperty(thread, obj, key2);
143 EXPECT_TRUE(res);
144 OperationResult result1 = JSObject::GetProperty(thread, value2, key2);
145 JSHandle<JSTaggedValue> value3 = result1.GetRawValue();
146 JSHandle<JSTaggedValue> value4 = JSHandle<JSTaggedValue>::Cast(factory->GetEmptyString());
147 EXPECT_EQ(value3, value4);
148 Destroy();
149 }
150
NativeBindingObjectTest1(std::pair<uint8_t *,size_t> data)151 void NativeBindingObjectTest1(std::pair<uint8_t *, size_t> data)
152 {
153 Init();
154 JSDeserializer deserializer(thread, data.first, data.second);
155 [[maybe_unused]] JSHandle<JSTaggedValue> objValue1 = deserializer.Deserialize();
156 JSHandle<JSTaggedValue> objValue2 = deserializer.Deserialize();
157 [[maybe_unused]] JSHandle<JSTaggedValue> objValue3 = deserializer.Deserialize();
158
159 JSHandle<JSObject> retObj2 = JSHandle<JSObject>::Cast(objValue2);
160 EXPECT_FALSE(retObj2.IsEmpty());
161
162 JSHandle<TaggedArray> array2 = JSObject::GetOwnPropertyKeys(thread, retObj2);
163 uint32_t length2 = array2->GetLength();
164 EXPECT_EQ(length2, 6U); // 6 : test case
165 Destroy();
166 }
167
NativeBindingObjectTest2(std::pair<uint8_t *,size_t> data)168 void NativeBindingObjectTest2(std::pair<uint8_t *, size_t> data)
169 {
170 Init();
171 ObjectFactory *factory = ecmaVm->GetFactory();
172 JSDeserializer deserializer(thread, data.first, data.second);
173 JSHandle<JSTaggedValue> objValue1 = deserializer.Deserialize();
174 [[maybe_unused]] JSHandle<JSTaggedValue> objValue2 = deserializer.Deserialize();
175
176 JSHandle<JSObject> retObj1 = JSHandle<JSObject>::Cast(objValue1);
177 EXPECT_FALSE(retObj1.IsEmpty());
178 JSHandle<TaggedArray> array1 = JSObject::GetOwnPropertyKeys(thread, retObj1);
179 uint32_t length1 = array1->GetLength();
180 EXPECT_EQ(length1, 4U); // 4 : test case
181 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("abc"));
182 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("8"));
183 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(6)); // 6 : test case
184 JSHandle<JSTaggedValue> value2 = JSHandle<JSTaggedValue>::Cast(factory->GetEmptyString());
185 OperationResult res1 = JSObject::GetProperty(thread, objValue1, key1);
186 JSHandle<JSTaggedValue> resValue1 = res1.GetRawValue();
187 EXPECT_EQ(resValue1, value1);
188 OperationResult res2 = JSObject::GetProperty(thread, objValue1, key2);
189 JSHandle<JSTaggedValue> resValue2 = res2.GetRawValue();
190 EXPECT_EQ(resValue2, value2);
191 Destroy();
192 }
193
NativeBindingObjectTest3(std::pair<uint8_t *,size_t> data)194 void NativeBindingObjectTest3(std::pair<uint8_t *, size_t> data)
195 {
196 Init();
197 ObjectFactory *factory = ecmaVm->GetFactory();
198 JSDeserializer deserializer(thread, data.first, data.second);
199 [[maybe_unused]] JSHandle<JSTaggedValue> objValue1 = deserializer.Deserialize();
200 JSHandle<JSTaggedValue> objValue2 = deserializer.Deserialize();
201
202 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("root1"));
203 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("root2"));
204 JSHandle<JSTaggedValue> key3(factory->NewFromASCII("root3"));
205 JSHandle<JSTaggedValue> key4(factory->NewFromASCII("name"));
206 JSHandle<JSTaggedValue> key5(factory->NewFromASCII("version"));
207 JSHandle<JSTaggedValue> key6(factory->NewFromASCII("product"));
208 JSHandle<JSTaggedValue> key7(factory->NewFromASCII("os"));
209 JSHandle<JSTaggedValue> key8(factory->NewFromASCII("system"));
210 JSHandle<JSTaggedValue> value1(factory->NewFromASCII(""));
211 OperationResult res1 = JSObject::GetProperty(thread, objValue2, key1);
212 JSHandle<JSTaggedValue> resValue1 = res1.GetRawValue();
213 EXPECT_EQ(resValue1, value1);
214 OperationResult res2 = JSObject::GetProperty(thread, objValue2, key2);
215 JSHandle<JSTaggedValue> resValue2 = res2.GetRawValue();
216 EXPECT_EQ(resValue2, value1);
217 OperationResult res3 = JSObject::GetProperty(thread, objValue2, key3);
218 JSHandle<JSTaggedValue> resValue3 = res3.GetRawValue();
219 EXPECT_EQ(resValue3, value1);
220 OperationResult res4 = JSObject::GetProperty(thread, objValue2, key4);
221 JSHandle<JSTaggedValue> resValue4 = res4.GetRawValue();
222 EXPECT_EQ(resValue4, value1);
223 OperationResult res5 = JSObject::GetProperty(thread, objValue2, key5);
224 JSHandle<JSTaggedValue> resValue5 = res5.GetRawValue();
225 EXPECT_EQ(resValue5, value1);
226 OperationResult res6 = JSObject::GetProperty(thread, objValue2, key6);
227 JSHandle<JSTaggedValue> resValue6 = res6.GetRawValue();
228 EXPECT_EQ(resValue6, value1);
229 OperationResult res7 = JSObject::GetProperty(thread, objValue2, key7);
230 JSHandle<JSTaggedValue> resValue7 = res7.GetRawValue();
231 EXPECT_EQ(resValue7, value1);
232 OperationResult res8 = JSObject::GetProperty(thread, objValue2, key8);
233 JSHandle<JSTaggedValue> resValue8 = res8.GetRawValue();
234 EXPECT_EQ(resValue8, value1);
235 Destroy();
236 }
237
DescriptionTest(std::pair<uint8_t *,size_t> data)238 void DescriptionTest(std::pair<uint8_t *, size_t> data)
239 {
240 Init();
241 ObjectFactory *factory = ecmaVm->GetFactory();
242 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("x"));
243 JSHandle<JSTaggedValue> key2(thread->GetEcmaVM()->GetFactory()->NewFromASCII("y"));
244 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(1));
245 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(2)); // 2 : test case
246
247 JSDeserializer deserializer(thread, data.first, data.second);
248 JSHandle<JSTaggedValue> objValue = deserializer.Deserialize();
249 JSHandle<JSObject> retObj = JSHandle<JSObject>::Cast(objValue);
250
251 PropertyDescriptor desc3(thread);
252 PropertyDescriptor desc4(thread);
253
254 JSHandle<TaggedArray> array1 = JSObject::GetOwnPropertyKeys(thread, retObj);
255 JSHandle<JSTaggedValue> retKey1(thread, array1->Get(0));
256 JSHandle<JSTaggedValue> retKey2(thread, array1->Get(1));
257 JSObject::GetOwnProperty(thread, retObj, retKey1, desc3);
258 JSObject::GetOwnProperty(thread, retObj, retKey2, desc4);
259 EXPECT_EQ(key1.GetTaggedValue().GetRawData(), retKey1.GetTaggedValue().GetRawData());
260
261 EXPECT_EQ(desc3.GetValue().GetTaggedValue().GetRawData(), value1.GetTaggedValue().GetRawData());
262 EXPECT_TRUE(desc3.IsWritable());
263 EXPECT_FALSE(desc3.IsEnumerable());
264 EXPECT_TRUE(desc3.IsConfigurable());
265
266 EXPECT_EQ(desc4.GetValue().GetTaggedValue().GetRawData(), value2.GetTaggedValue().GetRawData());
267 EXPECT_FALSE(desc4.IsWritable());
268 EXPECT_TRUE(desc4.IsEnumerable());
269 EXPECT_FALSE(desc4.IsConfigurable());
270 Destroy();
271 }
272
JSSetTest(std::pair<uint8_t *,size_t> data)273 void JSSetTest(std::pair<uint8_t *, size_t> data)
274 {
275 Init();
276 ObjectFactory *factory = ecmaVm->GetFactory();
277 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(7)); // 7 : test case
278 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(9)); // 9 : test case
279 JSHandle<JSTaggedValue> value3(factory->NewFromASCII("x"));
280 JSHandle<JSTaggedValue> value4(factory->NewFromASCII("y"));
281
282 JSDeserializer deserializer(thread, data.first, data.second);
283 JSHandle<JSTaggedValue> setValue = deserializer.Deserialize();
284 EXPECT_TRUE(!setValue.IsEmpty());
285 JSHandle<JSSet> retSet = JSHandle<JSSet>::Cast(setValue);
286 JSHandle<TaggedArray> array = JSObject::GetOwnPropertyKeys(thread, JSHandle<JSObject>::Cast(retSet));
287 uint32_t propertyLength = array->GetLength();
288 EXPECT_EQ(propertyLength, 2U); // 2 : test case
289 int sum = 0;
290 for (uint32_t i = 0; i < propertyLength; i++) {
291 JSHandle<JSTaggedValue> key(thread, array->Get(i));
292 double a = JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(retSet), key).GetValue()->GetNumber();
293 sum += a;
294 }
295 EXPECT_EQ(sum, 16); // 16 : test case
296
297 EXPECT_EQ(retSet->GetSize(), 4); // 4 : test case
298 EXPECT_TRUE(retSet->Has(value1.GetTaggedValue()));
299 EXPECT_TRUE(retSet->Has(value2.GetTaggedValue()));
300 EXPECT_TRUE(retSet->Has(value3.GetTaggedValue()));
301 EXPECT_TRUE(retSet->Has(value4.GetTaggedValue()));
302 Destroy();
303 }
304
JSArrayTest(std::pair<uint8_t *,size_t> data)305 void JSArrayTest(std::pair<uint8_t *, size_t> data)
306 {
307 Init();
308 JSDeserializer deserializer(thread, data.first, data.second);
309 JSHandle<JSTaggedValue> arrayValue = deserializer.Deserialize();
310 EXPECT_TRUE(!arrayValue.IsEmpty());
311
312 JSHandle<JSArray> retArray = JSHandle<JSArray>::Cast(arrayValue);
313
314 JSHandle<TaggedArray> keyArray = JSObject::GetOwnPropertyKeys(thread, JSHandle<JSObject>(retArray));
315 uint32_t propertyLength = keyArray->GetLength();
316 EXPECT_EQ(propertyLength, 23U); // 23 : test case
317 int sum = 0;
318 for (uint32_t i = 0; i < propertyLength; i++) {
319 JSHandle<JSTaggedValue> key(thread, keyArray->Get(i));
320 double a = JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(retArray), key).GetValue()->GetNumber();
321 sum += a;
322 }
323 EXPECT_EQ(sum, 226); // 226 : test case
324
325 // test get value from array
326 for (int i = 0; i < 20; i++) { // 20 : test case
327 JSHandle<JSTaggedValue> value = JSArray::FastGetPropertyByValue(thread, arrayValue, i);
328 EXPECT_EQ(i, value.GetTaggedValue().GetInt());
329 }
330 Destroy();
331 }
332
ObjectsPropertyReferenceTest(std::pair<uint8_t *,size_t> data)333 void ObjectsPropertyReferenceTest(std::pair<uint8_t *, size_t> data)
334 {
335 Init();
336 JSDeserializer deserializer(thread, data.first, data.second);
337 JSHandle<JSTaggedValue> objValue1 = deserializer.Deserialize();
338 JSHandle<JSTaggedValue> objValue2 = deserializer.Deserialize();
339 EXPECT_TRUE(!objValue1.IsEmpty()) << "[Empty] Deserialize obj1 fail";
340 EXPECT_TRUE(!objValue2.IsEmpty()) << "[Empty] Deserialize obj2 fail";
341 Destroy();
342 }
343
EcmaStringTest1(std::pair<uint8_t *,size_t> data)344 void EcmaStringTest1(std::pair<uint8_t *, size_t> data)
345 {
346 Init();
347 const char *rawStr = "this is a test ecmaString";
348 JSHandle<EcmaString> ecmaString = thread->GetEcmaVM()->GetFactory()->NewFromASCII(rawStr);
349
350 JSDeserializer deserializer(thread, data.first, data.second);
351 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
352 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize ecmaString fail";
353 EXPECT_TRUE(res->IsString()) << "[NotString] Deserialize ecmaString fail";
354 JSHandle<EcmaString> resEcmaString = JSHandle<EcmaString>::Cast(res);
355 auto ecmaStringCode = EcmaStringAccessor(ecmaString).GetHashcode();
356 auto resEcmaStringCode = EcmaStringAccessor(resEcmaString).GetHashcode();
357 EXPECT_TRUE(ecmaStringCode == resEcmaStringCode) << "Not same HashCode";
358 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*ecmaString, *resEcmaString)) << "Not same EcmaString";
359 Destroy();
360 }
361
EcmaStringTest2(std::pair<uint8_t *,size_t> data)362 void EcmaStringTest2(std::pair<uint8_t *, size_t> data)
363 {
364 Init();
365 const char *rawStr = "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"\
366 "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"\
367 "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"\
368 "ssssss";
369 JSHandle<EcmaString> ecmaString = thread->GetEcmaVM()->GetFactory()->NewFromASCII(rawStr);
370
371 JSDeserializer deserializer(thread, data.first, data.second);
372 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
373 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize ecmaString fail";
374 EXPECT_TRUE(res->IsString()) << "[NotString] Deserialize ecmaString fail";
375 JSHandle<EcmaString> resEcmaString = JSHandle<EcmaString>::Cast(res);
376 auto ecmaStringCode = EcmaStringAccessor(ecmaString).GetHashcode();
377 auto resEcmaStringCode = EcmaStringAccessor(resEcmaString).GetHashcode();
378 EXPECT_TRUE(ecmaStringCode == resEcmaStringCode) << "Not same HashCode";
379 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*ecmaString, *resEcmaString)) << "Not same EcmaString";
380 Destroy();
381 }
382
EcmaStringTest3(std::pair<uint8_t *,size_t> data)383 void EcmaStringTest3(std::pair<uint8_t *, size_t> data)
384 {
385 Init();
386 JSHandle<EcmaString> ecmaString = thread->GetEcmaVM()->GetFactory()->GetEmptyString();
387
388 JSDeserializer deserializer(thread, data.first, data.second);
389 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
390 EXPECT_TRUE(res->IsString()) << "[NotString] Deserialize ecmaString fail";
391 JSHandle<EcmaString> resEcmaString = JSHandle<EcmaString>::Cast(res);
392 auto ecmaStringCode = EcmaStringAccessor(ecmaString).GetHashcode();
393 auto resEcmaStringCode = EcmaStringAccessor(resEcmaString).GetHashcode();
394 EXPECT_TRUE(ecmaStringCode == resEcmaStringCode) << "Not same HashCode";
395 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*ecmaString, *resEcmaString)) << "Not same EcmaString";
396 Destroy();
397 }
398
EcmaStringTest4(std::pair<uint8_t *,size_t> data)399 void EcmaStringTest4(std::pair<uint8_t *, size_t> data)
400 {
401 Init();
402 JSHandle<EcmaString> ecmaString = thread->GetEcmaVM()->GetFactory()->NewFromStdString("你好,世界");
403 JSHandle<EcmaString> ecmaString1 = thread->GetEcmaVM()->GetFactory()->NewFromStdString("你好,世界");
404 auto ecmaStringCode1 = EcmaStringAccessor(ecmaString).GetHashcode();
405 auto ecmaString1Code = EcmaStringAccessor(ecmaString1).GetHashcode();
406 EXPECT_TRUE(ecmaStringCode1 == ecmaString1Code) << "Not same HashCode";
407 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*ecmaString, *ecmaString1)) << "Not same EcmaString";
408
409 JSDeserializer deserializer(thread, data.first, data.second);
410 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
411 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize ecmaString fail";
412 EXPECT_TRUE(res->IsString()) << "[NotString] Deserialize ecmaString fail";
413 JSHandle<EcmaString> resEcmaString = JSHandle<EcmaString>::Cast(res);
414 auto ecmaStringCode2 = EcmaStringAccessor(ecmaString).GetHashcode();
415 auto resEcmaStringCode = EcmaStringAccessor(resEcmaString).GetHashcode();
416 EXPECT_TRUE(ecmaStringCode2 == resEcmaStringCode) << "Not same HashCode";
417 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*ecmaString, *resEcmaString)) << "Not same EcmaString";
418 Destroy();
419 }
420
Int32Test(std::pair<uint8_t *,size_t> data)421 void Int32Test(std::pair<uint8_t *, size_t> data)
422 {
423 Init();
424 int32_t a = 64;
425 int32_t min = -2147483648;
426 int32_t b = -63;
427 JSDeserializer deserializer(thread, data.first, data.second);
428 JSHandle<JSTaggedValue> resA = deserializer.Deserialize();
429 JSHandle<JSTaggedValue> resMin = deserializer.Deserialize();
430 JSHandle<JSTaggedValue> resB = deserializer.Deserialize();
431 EXPECT_TRUE(!resA.IsEmpty() && !resMin.IsEmpty() && !resB.IsEmpty()) << "[Empty] Deserialize Int32 fail";
432 EXPECT_TRUE(resA->IsInt() && resMin->IsInt() && resB->IsInt()) << "[NotInt] Deserialize Int32 fail";
433 EXPECT_TRUE(JSTaggedValue::ToInt32(thread, resA) == a) << "Not Same Value";
434 EXPECT_TRUE(JSTaggedValue::ToInt32(thread, resMin) == min) << "Not Same Value";
435 EXPECT_TRUE(JSTaggedValue::ToInt32(thread, resB) == b) << "Not Same Value";
436 Destroy();
437 }
438
DoubleTest(std::pair<uint8_t *,size_t> data)439 void DoubleTest(std::pair<uint8_t *, size_t> data)
440 {
441 Init();
442 double a = 3.1415926535;
443 double b = -3.1415926535;
444 JSDeserializer deserializer(thread, data.first, data.second);
445 JSHandle<JSTaggedValue> resA = deserializer.Deserialize();
446 JSHandle<JSTaggedValue> resB = deserializer.Deserialize();
447 EXPECT_TRUE(!resA.IsEmpty() && !resB.IsEmpty()) << "[Empty] Deserialize double fail";
448 EXPECT_TRUE(resA->IsDouble() && resB->IsDouble()) << "[NotInt] Deserialize double fail";
449 EXPECT_TRUE(resA->GetDouble() == a) << "Not Same Value";
450 EXPECT_TRUE(resB->GetDouble() == b) << "Not Same Value";
451 Destroy();
452 }
453
JSDateTest(std::pair<uint8_t *,size_t> data)454 void JSDateTest(std::pair<uint8_t *, size_t> data)
455 {
456 Init();
457 double tm = 28 * 60 * 60 * 1000; // 28 * 60 * 60 * 1000 : test case
458 JSDeserializer deserializer(thread, data.first, data.second);
459 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
460 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize JSDate fail";
461 EXPECT_TRUE(res->IsDate()) << "[NotJSDate] Deserialize JSDate fail";
462 JSHandle<JSDate> resDate = JSHandle<JSDate>(res);
463 EXPECT_TRUE(resDate->GetTimeValue() == JSTaggedValue(tm)) << "Not Same Time Value";
464 Destroy();
465 }
466
JSMapTest(std::pair<uint8_t *,size_t> data,const JSHandle<JSMap> & originMap)467 void JSMapTest(std::pair<uint8_t *, size_t> data, const JSHandle<JSMap> &originMap)
468 {
469 Init();
470 JSDeserializer deserializer(thread, data.first, data.second);
471 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
472 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize JSMap fail";
473 EXPECT_TRUE(res->IsJSMap()) << "[NotJSMap] Deserialize JSMap fail";
474 JSHandle<JSMap> resMap = JSHandle<JSMap>::Cast(res);
475 EXPECT_TRUE(originMap->GetSize() == resMap->GetSize()) << "the map size Not equal";
476 uint32_t resSize = static_cast<uint32_t>(resMap->GetSize());
477 for (uint32_t i = 0; i < resSize; i++) {
478 JSHandle<JSTaggedValue> resKey(thread, resMap->GetKey(i));
479 JSHandle<JSTaggedValue> resValue(thread, resMap->GetValue(i));
480 JSHandle<JSTaggedValue> key(thread, originMap->GetKey(i));
481 JSHandle<JSTaggedValue> value(thread, originMap->GetValue(i));
482
483 JSHandle<EcmaString> resKeyStr = JSHandle<EcmaString>::Cast(resKey);
484 JSHandle<EcmaString> keyStr = JSHandle<EcmaString>::Cast(key);
485 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*resKeyStr, *keyStr)) << "Not same map key";
486 EXPECT_TRUE(JSTaggedValue::ToInt32(thread, resValue) == JSTaggedValue::ToInt32(thread, value))
487 << "Not same map value";
488 }
489 Destroy();
490 }
491
JSArrayBufferTest(std::pair<uint8_t *,size_t> data,const JSHandle<JSArrayBuffer> & originArrayBuffer,int32_t byteLength,const char * msg)492 void JSArrayBufferTest(std::pair<uint8_t *, size_t> data,
493 const JSHandle<JSArrayBuffer> &originArrayBuffer, int32_t byteLength, const char *msg)
494 {
495 Init();
496 JSDeserializer deserializer(thread, data.first, data.second);
497 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
498 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize JSArrayBuffer fail";
499 EXPECT_TRUE(res->IsArrayBuffer()) << "[NotJSArrayBuffer] Deserialize JSArrayBuffer fail";
500 JSHandle<JSArrayBuffer> resJSArrayBuffer = JSHandle<JSArrayBuffer>::Cast(res);
501 int32_t resByteLength = static_cast<int32_t>(resJSArrayBuffer->GetArrayBufferByteLength());
502 EXPECT_TRUE(resByteLength == byteLength) << "Not Same ByteLength"; // 10 : test case
503
504 JSHandle<JSTaggedValue> bufferData(thread, originArrayBuffer->GetArrayBufferData());
505 auto np = JSHandle<JSNativePointer>::Cast(bufferData);
506 void *buffer = np->GetExternalPointer();
507 ASSERT_NE(buffer, nullptr);
508 JSHandle<JSTaggedValue> resBufferData(thread, resJSArrayBuffer->GetArrayBufferData());
509 JSHandle<JSNativePointer> resNp = JSHandle<JSNativePointer>::Cast(resBufferData);
510 void *resBuffer = resNp->GetExternalPointer();
511 ASSERT_NE(resBuffer, nullptr);
512
513 for (int32_t i = 0; i < resByteLength; i++) {
514 EXPECT_TRUE(static_cast<char *>(resBuffer)[i] == static_cast<char *>(buffer)[i]) << "Not Same Buffer";
515 }
516
517 if (msg != nullptr) {
518 if (memcpy_s(resBuffer, byteLength, msg, byteLength) != EOK) {
519 EXPECT_TRUE(false) << " memcpy error!";
520 }
521 }
522 Destroy();
523 }
524
JSSharedArrayBufferTest(std::pair<uint8_t *,size_t> data,const JSHandle<JSArrayBuffer> & originArrayBuffer,int32_t byteLength,const char * msg)525 void JSSharedArrayBufferTest(std::pair<uint8_t *, size_t> data,
526 const JSHandle<JSArrayBuffer> &originArrayBuffer, int32_t byteLength, const char *msg)
527 {
528 Init();
529 JSDeserializer deserializer(thread, data.first, data.second);
530 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
531 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize JSArrayBuffer fail";
532 EXPECT_TRUE(res->IsSharedArrayBuffer()) << "[NotJSArrayBuffer] Deserialize JSArrayBuffer fail";
533 JSHandle<JSArrayBuffer> resJSArrayBuffer = JSHandle<JSArrayBuffer>::Cast(res);
534 int32_t resByteLength = static_cast<int32_t>(resJSArrayBuffer->GetArrayBufferByteLength());
535 EXPECT_TRUE(resByteLength == byteLength) << "Not Same ByteLength";
536 JSHandle<JSTaggedValue> bufferData(thread, originArrayBuffer->GetArrayBufferData());
537 auto np = JSHandle<JSNativePointer>::Cast(bufferData);
538 void *buffer = np->GetExternalPointer();
539 ASSERT_NE(buffer, nullptr);
540 JSHandle<JSTaggedValue> resBufferData(thread, resJSArrayBuffer->GetArrayBufferData());
541 JSHandle<JSNativePointer> resNp = JSHandle<JSNativePointer>::Cast(resBufferData);
542 void *resBuffer = resNp->GetExternalPointer();
543 ASSERT_NE(resBuffer, nullptr);
544 EXPECT_TRUE((uint64_t)buffer == (uint64_t)resBuffer) << "Not Same pointer!";
545 for (int32_t i = 0; i < resByteLength; i++) {
546 EXPECT_TRUE(static_cast<char *>(resBuffer)[i] == static_cast<char *>(buffer)[i]) << "Not Same Buffer";
547 }
548
549 if (msg != nullptr) {
550 if (memcpy_s(resBuffer, byteLength, msg, byteLength) != EOK) {
551 EXPECT_TRUE(false) << " memcpy error!";
552 }
553 }
554 Destroy();
555 }
556
JSSharedArrayBufferTest1(std::pair<uint8_t *,size_t> data,int32_t byteLength,const char * msg)557 void JSSharedArrayBufferTest1(std::pair<uint8_t *, size_t> data, int32_t byteLength, const char *msg)
558 {
559 Init();
560 JSDeserializer deserializer(thread, data.first, data.second);
561 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
562 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize JSArrayBuffer fail";
563 EXPECT_TRUE(res->IsSharedArrayBuffer()) << "[NotJSArrayBuffer] Deserialize JSArrayBuffer fail";
564 JSHandle<JSArrayBuffer> resJSArrayBuffer = JSHandle<JSArrayBuffer>::Cast(res);
565 JSHandle<JSTaggedValue> resBufferData(thread, resJSArrayBuffer->GetArrayBufferData());
566 JSHandle<JSNativePointer> resNp = JSHandle<JSNativePointer>::Cast(resBufferData);
567 void *resBuffer = resNp->GetExternalPointer();
568 if (msg != nullptr) {
569 if (memcpy_s(resBuffer, byteLength, msg, byteLength) != EOK) {
570 EXPECT_TRUE(false) << " memcpy error!";
571 }
572 }
573 Destroy();
574 }
575
JSRegexpTest(std::pair<uint8_t *,size_t> data)576 void JSRegexpTest(std::pair<uint8_t *, size_t> data)
577 {
578 Init();
579 JSHandle<EcmaString> pattern = thread->GetEcmaVM()->GetFactory()->NewFromASCII("key2");
580 JSHandle<EcmaString> flags = thread->GetEcmaVM()->GetFactory()->NewFromASCII("i");
581 char buffer[] = "1234567"; // use char buffer to simulate byteCodeBuffer
582 uint32_t bufferSize = 7;
583
584 JSDeserializer deserializer(thread, data.first, data.second);
585 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
586 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize JSRegExp fail";
587 EXPECT_TRUE(res->IsJSRegExp()) << "[NotJSRegexp] Deserialize JSRegExp fail";
588 JSHandle<JSRegExp> resJSRegexp(res);
589
590 uint32_t resBufferSize = resJSRegexp->GetLength();
591 EXPECT_TRUE(resBufferSize == bufferSize) << "Not Same Length";
592 JSHandle<JSTaggedValue> originalSource(thread, resJSRegexp->GetOriginalSource());
593 EXPECT_TRUE(originalSource->IsString());
594 JSHandle<JSTaggedValue> originalFlags(thread, resJSRegexp->GetOriginalFlags());
595 EXPECT_TRUE(originalFlags->IsString());
596 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*JSHandle<EcmaString>(originalSource), *pattern));
597 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*JSHandle<EcmaString>(originalFlags), *flags));
598
599 JSHandle<JSTaggedValue> resBufferData(thread, resJSRegexp->GetByteCodeBuffer());
600 JSHandle<JSNativePointer> resNp = JSHandle<JSNativePointer>::Cast(resBufferData);
601 void *resBuffer = resNp->GetExternalPointer();
602 ASSERT_NE(resBuffer, nullptr);
603
604 for (uint32_t i = 0; i < resBufferSize; i++) {
605 EXPECT_TRUE(static_cast<char *>(resBuffer)[i] == buffer[i]) << "Not Same ByteCode";
606 }
607 Destroy();
608 }
609
TypedArrayTest1(std::pair<uint8_t *,size_t> data)610 void TypedArrayTest1(std::pair<uint8_t *, size_t> data)
611 {
612 Init();
613 JSHandle<JSTaggedValue> originTypedArrayName(thread, thread->GlobalConstants()->GetInt8ArrayString());
614 JSDeserializer deserializer(thread, data.first, data.second);
615 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
616 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize TypedArray fail";
617 EXPECT_TRUE(res->IsJSInt8Array()) << "[NotJSInt8Array] Deserialize TypedArray fail";
618 JSHandle<JSTypedArray> resJSInt8Array = JSHandle<JSTypedArray>::Cast(res);
619
620 JSHandle<JSTaggedValue> typedArrayName(thread, resJSInt8Array->GetTypedArrayName());
621 uint32_t byteLength = resJSInt8Array->GetByteLength();
622 uint32_t byteOffset = resJSInt8Array->GetByteOffset();
623 uint32_t arrayLength = resJSInt8Array->GetArrayLength();
624 ContentType contentType = resJSInt8Array->GetContentType();
625 JSHandle<JSTaggedValue> viewedArrayBuffer(thread, resJSInt8Array->GetViewedArrayBufferOrByteArray());
626
627 EXPECT_TRUE(typedArrayName->IsString());
628 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*JSHandle<EcmaString>(typedArrayName),
629 *JSHandle<EcmaString>(originTypedArrayName)));
630 EXPECT_EQ(byteLength, 10) << "Not Same ByteLength"; // 10: bufferLength
631 EXPECT_EQ(byteOffset, 0) << "Not Same ByteOffset";
632 EXPECT_EQ(arrayLength, 10) << "Not Same ArrayLength"; // 10: arrayLength
633 EXPECT_TRUE(contentType == ContentType::Number) << "Not Same ContentType";
634
635 // check arrayBuffer
636 EXPECT_TRUE(viewedArrayBuffer->IsArrayBuffer());
637 JSHandle<JSArrayBuffer> resJSArrayBuffer(viewedArrayBuffer);
638 uint32_t resTaggedLength = resJSArrayBuffer->GetArrayBufferByteLength();
639 EXPECT_EQ(resTaggedLength, 10) << "Not same viewedBuffer length"; // 10: bufferLength
640 JSHandle<JSTaggedValue> resBufferData(thread, resJSArrayBuffer->GetArrayBufferData());
641 JSHandle<JSNativePointer> resNp = JSHandle<JSNativePointer>::Cast(resBufferData);
642 void *resBuffer = resNp->GetExternalPointer();
643 for (uint32_t i = 0; i < resTaggedLength; i++) {
644 EXPECT_EQ(static_cast<uint8_t *>(resBuffer)[i], i) << "Not same viewedBuffer";
645 }
646 Destroy();
647 }
648
TypedArrayTest2(std::pair<uint8_t *,size_t> data)649 void TypedArrayTest2(std::pair<uint8_t *, size_t> data)
650 {
651 Init();
652 JSHandle<JSTaggedValue> originTypedArrayName(thread, thread->GlobalConstants()->GetInt8ArrayString());
653 JSDeserializer deserializer(thread, data.first, data.second);
654 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
655 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize TypedArray fail";
656 EXPECT_TRUE(res->IsJSInt8Array()) << "[NotJSInt8Array] Deserialize TypedArray fail";
657 JSHandle<JSTypedArray> resJSInt8Array = JSHandle<JSTypedArray>::Cast(res);
658
659 JSHandle<JSTaggedValue> typedArrayName(thread, resJSInt8Array->GetTypedArrayName());
660 uint32_t byteLength = resJSInt8Array->GetByteLength();
661 uint32_t byteOffset = resJSInt8Array->GetByteOffset();
662 uint32_t arrayLength = resJSInt8Array->GetArrayLength();
663 ContentType contentType = resJSInt8Array->GetContentType();
664 JSHandle<JSTaggedValue> byteArray(thread, resJSInt8Array->GetViewedArrayBufferOrByteArray());
665
666 EXPECT_TRUE(typedArrayName->IsString());
667 EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*JSHandle<EcmaString>(typedArrayName),
668 *JSHandle<EcmaString>(originTypedArrayName)));
669 EXPECT_EQ(byteLength, 10) << "Not Same ByteLength"; // 10: bufferLength
670 EXPECT_EQ(byteOffset, 0) << "Not Same ByteOffset";
671 EXPECT_EQ(arrayLength, 10) << "Not Same ArrayLength"; // 10: arrayLength
672 EXPECT_TRUE(contentType == ContentType::Number) << "Not Same ContentType";
673
674 // check byteArray
675 EXPECT_TRUE(byteArray->IsByteArray());
676 JSHandle<ByteArray> resByteArray(byteArray);
677 uint32_t resTaggedLength = resByteArray->GetArrayLength();
678 EXPECT_EQ(resTaggedLength, 10) << "Not same viewedBuffer length"; // 10: bufferLength
679 uint32_t resElementSize = resByteArray->GetByteLength();
680 EXPECT_EQ(resElementSize, 1) << "Not same byteArray size";
681 for (uint32_t i = 0; i < resTaggedLength; i++) {
682 JSHandle<JSTaggedValue> taggedVal(thread, resByteArray->Get(thread, i, DataViewType::UINT8));
683 int32_t byteArrayVal = JSTaggedValue::ToInt32(thread, taggedVal);
684 EXPECT_EQ(byteArrayVal, 255) << "Not same byteArray value"; // 255: value in byteArray
685 }
686 Destroy();
687 }
688
TaggedArrayTest(std::pair<uint8_t *,size_t> data)689 void TaggedArrayTest(std::pair<uint8_t *, size_t> data)
690 {
691 Init();
692 JSDeserializer deserializer(thread, data.first, data.second);
693 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
694 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize TaggedArray fail";
695 EXPECT_TRUE(res.GetTaggedValue().IsTaggedArray()) << "[NotTaggedArray] Deserialize TaggedArray fail";
696
697 // check taggedArray
698 JSHandle<TaggedArray> taggedArray = JSHandle<TaggedArray>::Cast(res);
699 EXPECT_EQ(taggedArray->GetLength(), 4U); // 4 : test case
700 EcmaString *str11 = reinterpret_cast<EcmaString *>(taggedArray->Get(0).GetTaggedObject());
701 EcmaString *str22 = reinterpret_cast<EcmaString *>(taggedArray->Get(1).GetTaggedObject());
702 EXPECT_EQ(std::strcmp(EcmaStringAccessor(str11).ToCString().c_str(), "str11"), 0);
703 EXPECT_EQ(std::strcmp(EcmaStringAccessor(str22).ToCString().c_str(), "str22"), 0);
704 EXPECT_TRUE(taggedArray->Get(2).IsUndefined()); // 2: the second index
705 Destroy();
706 }
707
MethodTest(std::pair<uint8_t *,size_t> data)708 void MethodTest(std::pair<uint8_t *, size_t> data)
709 {
710 Init();
711 JSDeserializer deserializer(thread, data.first, data.second);
712 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
713 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize Method fail";
714 EXPECT_TRUE(res.GetTaggedValue().IsMethod()) << "[NotMethod] Deserialize Method fail";
715
716 JSHandle<Method> method = JSHandle<Method>::Cast(res);
717 EXPECT_TRUE(method->IsNativeWithCallField());
718 Destroy();
719 }
720
MethodTest1(std::pair<uint8_t *,size_t> data)721 void MethodTest1(std::pair<uint8_t *, size_t> data)
722 {
723 Init();
724 JSDeserializer deserializer(thread, data.first, data.second);
725 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
726 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize Method fail";
727 EXPECT_TRUE(res.GetTaggedValue().IsMethod()) << "[NotMethod] Deserialize Method fail";
728
729 JSHandle<Method> method = JSHandle<Method>::Cast(res);
730 EXPECT_FALSE(method->IsNativeWithCallField());
731
732 JSHandle<ConstantPool> constpool(thread, method->GetConstantPool());
733 EXPECT_EQ(constpool->GetLength(), 3U);
734 EXPECT_EQ(constpool->GetCacheLength(), 1U);
735 const JSPandaFile *jsPandaFile = constpool->GetJSPandaFile();
736 EXPECT_TRUE(jsPandaFile != nullptr);
737 const CString &desc = jsPandaFile->GetJSPandaFileDesc();
738 EXPECT_EQ(desc, "test.pa");
739 Destroy();
740 }
741
ConcurrentFunctionTest(std::pair<uint8_t *,size_t> data)742 void ConcurrentFunctionTest(std::pair<uint8_t *, size_t> data)
743 {
744 Init();
745 JSDeserializer deserializer(thread, data.first, data.second);
746 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
747 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize ConcurrentFunction fail";
748 EXPECT_TRUE(res->IsJSFunction()) << "[NotJSFunction] Deserialize ConcurrentFunction fail";
749 Destroy();
750 }
751
BigIntTest(std::pair<uint8_t *,size_t> data)752 void BigIntTest(std::pair<uint8_t *, size_t> data)
753 {
754 Init();
755 JSDeserializer deserialzier(thread, data.first, data.second);
756 JSHandle<JSTaggedValue> res1 = deserialzier.Deserialize();
757 JSHandle<JSTaggedValue> res2 = deserialzier.Deserialize();
758 JSHandle<JSTaggedValue> res3 = deserialzier.Deserialize();
759 JSHandle<JSTaggedValue> res4 = deserialzier.Deserialize();
760 JSHandle<JSTaggedValue> res5 = deserialzier.Deserialize();
761 JSHandle<JSTaggedValue> res6 = deserialzier.Deserialize();
762 JSHandle<JSTaggedValue> res7 = deserialzier.Deserialize();
763 JSHandle<JSTaggedValue> res8 = deserialzier.Deserialize();
764 JSHandle<JSTaggedValue> res9 = deserialzier.Deserialize();
765 JSHandle<JSTaggedValue> res10 = deserialzier.Deserialize();
766 JSHandle<JSTaggedValue> res11 = deserialzier.Deserialize();
767 JSHandle<JSTaggedValue> res12 = deserialzier.Deserialize();
768 JSHandle<JSTaggedValue> res13 = deserialzier.Deserialize();
769 JSHandle<JSTaggedValue> res14 = deserialzier.Deserialize();
770 EXPECT_TRUE(!res1.IsEmpty() && !res14.IsEmpty()) << "[Empty] Deserialize BigInt fail";
771 EXPECT_TRUE(res3->IsBigInt() && res12->IsBigInt()) << "[NotBigInt] Deserialize BigInt fail";
772
773 CString str1 = "100000000000000000000000000000000000000000000000000000"; // Binary: 2 ^ 53
774 CString str2 = "111111111111111111111111111111111111111111111111111111"; // Binary: 2 ^ 54 - 1
775 CString str3 = "-9007199254740991012345";
776 CString str4 = "1234567890987654321"; // Decimal
777 JSHandle<BigInt> bigInt1 = BigIntHelper::SetBigInt(thread, str1);
778 JSHandle<BigInt> bigInt2 = BigIntHelper::SetBigInt(thread, str2);
779 JSHandle<BigInt> bigInt3 = BigIntHelper::SetBigInt(thread, str3);
780 JSHandle<BigInt> bigInt4 = BigInt::Uint32ToBigInt(thread, 0); // 0 : test case
781 JSHandle<BigInt> bigInt5 = BigIntHelper::SetBigInt(thread, str4, BigInt::DECIMAL);
782 JSHandle<BigInt> bigInt6 = BigInt::Uint64ToBigInt(thread, ULLONG_MAX);
783 JSHandle<BigInt> bigInt7 = BigInt::Uint64ToBigInt(thread, UINT_MAX);
784 JSHandle<BigInt> bigInt8 = BigInt::Uint32ToBigInt(thread, std::numeric_limits<uint32_t>::max());
785 JSHandle<BigInt> bigInt9 = BigInt::Uint32ToBigInt(thread, std::numeric_limits<uint32_t>::min());
786 JSHandle<BigInt> bigInt10 = BigInt::Int64ToBigInt(thread, LLONG_MAX);
787 JSHandle<BigInt> bigInt11 = BigInt::Int64ToBigInt(thread, LLONG_MIN);
788 JSHandle<BigInt> bigInt12 = BigInt::Int64ToBigInt(thread, INT_MAX);
789 JSHandle<BigInt> bigInt13 = BigInt::Int64ToBigInt(thread, INT_MIN);
790 JSHandle<BigInt> bigInt14 = BigInt::Int32ToBigInt(thread, -1); // -1 : test case
791 EXPECT_TRUE(BigInt::Equal(res1.GetTaggedValue(), bigInt1.GetTaggedValue())) << "Not Same BigInt";
792 EXPECT_TRUE(BigInt::Equal(res2.GetTaggedValue(), bigInt2.GetTaggedValue())) << "Not Same BigInt";
793 EXPECT_TRUE(BigInt::Equal(res3.GetTaggedValue(), bigInt3.GetTaggedValue())) << "Not Same BigInt";
794 EXPECT_TRUE(BigInt::Equal(res4.GetTaggedValue(), bigInt4.GetTaggedValue())) << "Not Same BigInt";
795 EXPECT_TRUE(BigInt::Equal(res5.GetTaggedValue(), bigInt5.GetTaggedValue())) << "Not Same BigInt";
796 EXPECT_TRUE(BigInt::Equal(res6.GetTaggedValue(), bigInt6.GetTaggedValue())) << "Not Same BigInt";
797 EXPECT_TRUE(BigInt::Equal(res7.GetTaggedValue(), bigInt7.GetTaggedValue())) << "Not Same BigInt";
798 EXPECT_TRUE(BigInt::Equal(res8.GetTaggedValue(), bigInt8.GetTaggedValue())) << "Not Same BigInt";
799 EXPECT_TRUE(BigInt::Equal(res9.GetTaggedValue(), bigInt9.GetTaggedValue())) << "Not Same BigInt";
800 EXPECT_TRUE(BigInt::Equal(res10.GetTaggedValue(), bigInt10.GetTaggedValue())) << "Not Same BigInt";
801 EXPECT_TRUE(BigInt::Equal(res11.GetTaggedValue(), bigInt11.GetTaggedValue())) << "Not Same BigInt";
802 EXPECT_TRUE(BigInt::Equal(res12.GetTaggedValue(), bigInt12.GetTaggedValue())) << "Not Same BigInt";
803 EXPECT_TRUE(BigInt::Equal(res13.GetTaggedValue(), bigInt13.GetTaggedValue())) << "Not Same BigInt";
804 EXPECT_TRUE(BigInt::Equal(res14.GetTaggedValue(), bigInt14.GetTaggedValue())) << "Not Same BigInt";
805 Destroy();
806 }
807
ObjectWithConcurrentFunctionTest(std::pair<uint8_t *,size_t> data)808 void ObjectWithConcurrentFunctionTest(std::pair<uint8_t *, size_t> data)
809 {
810 Init();
811 ObjectFactory *factory = ecmaVm->GetFactory();
812 JSDeserializer deserializer(thread, data.first, data.second);
813 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
814 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize ObjectWithConcurrentFunction fail";
815 EXPECT_TRUE(res->IsObject()) <<
816 "[NotObjectWithConcurrentFunction] Deserialize ObjectWithConcurrentFunction fail";
817
818 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("2"));
819 OperationResult result1 = JSObject::GetProperty(thread, res, key1);
820 JSHandle<JSTaggedValue> value1 = result1.GetRawValue();
821 EXPECT_TRUE(value1->IsJSFunction());
822 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("abc"));
823 OperationResult result2 = JSObject::GetProperty(thread, res, key2);
824 JSHandle<JSTaggedValue> value2 = result2.GetRawValue();
825 EXPECT_TRUE(value2->IsString());
826 JSHandle<JSTaggedValue> key3(factory->NewFromASCII("4"));
827 OperationResult result3 = JSObject::GetProperty(thread, res, key3);
828 JSHandle<JSTaggedValue> value3 = result3.GetRawValue();
829 EXPECT_TRUE(value3->IsJSFunction());
830 JSHandle<JSTaggedValue> key4(factory->NewFromASCII("key"));
831 OperationResult result4 = JSObject::GetProperty(thread, res, key4);
832 JSHandle<JSTaggedValue> value4 = result4.GetRawValue();
833 EXPECT_TRUE(value4->IsString());
834 Destroy();
835 }
836
TransferJSArrayBufferTest1(std::pair<uint8_t *,size_t> data,uintptr_t bufferAddrCheck)837 void TransferJSArrayBufferTest1(std::pair<uint8_t *, size_t> data, uintptr_t bufferAddrCheck)
838 {
839 Init();
840 JSDeserializer deserializer(thread, data.first, data.second);
841 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
842 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize TransferJSArrayBuffer1 fail";
843 EXPECT_TRUE(res->IsArrayBuffer()) << "[NotJSArrayBuffer] Deserialize TransferJSArrayBuffer1 fail";
844
845 JSHandle<JSArrayBuffer> arrBuf = JSHandle<JSArrayBuffer>::Cast(res);
846 EXPECT_EQ(arrBuf->GetArrayBufferByteLength(), 5); // 5: bufferLength
847 JSHandle<JSTaggedValue> nativePtr(thread, arrBuf->GetArrayBufferData());
848 EXPECT_TRUE(nativePtr->IsJSNativePointer()) << "[NotJSNativePointer] Deserialize TransferJSArrayBuffer1 fail";
849 JSHandle<JSNativePointer> np = JSHandle<JSNativePointer>::Cast(nativePtr);
850 uintptr_t bufferAddr = reinterpret_cast<uintptr_t>(np->GetExternalPointer());
851 // The deserialized C buffer pointer shall be same to the original one
852 EXPECT_EQ(static_cast<uint64_t>(bufferAddr), static_cast<uint64_t>(bufferAddrCheck));
853 Destroy();
854 }
855
TransferJSArrayBufferTest2(std::pair<uint8_t *,size_t> data,uintptr_t bufferAddrCheck)856 void TransferJSArrayBufferTest2(std::pair<uint8_t *, size_t> data, uintptr_t bufferAddrCheck)
857 {
858 Init();
859 JSDeserializer deserializer(thread, data.first, data.second);
860 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
861 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize TransferJSArrayBuffer2 fail";
862 EXPECT_TRUE(res->IsArrayBuffer()) << "[NotJSArrayBuffer] Deserialize TransferJSArrayBuffer2 fail";
863
864 JSHandle<JSArrayBuffer> arrBuf = JSHandle<JSArrayBuffer>::Cast(res);
865 EXPECT_EQ(arrBuf->GetArrayBufferByteLength(), 5); // 5: bufferLength
866 JSHandle<JSTaggedValue> nativePtr(thread, arrBuf->GetArrayBufferData());
867 EXPECT_TRUE(nativePtr->IsJSNativePointer()) << "[NotJSNativePointer] Deserialize TransferJSArrayBuffer2 fail";
868 JSHandle<JSNativePointer> np = JSHandle<JSNativePointer>::Cast(nativePtr);
869 uintptr_t bufferAddr = reinterpret_cast<uintptr_t>(np->GetExternalPointer());
870 // The deserialized C buffer pointer shall be different to the original one
871 EXPECT_NE(static_cast<uint64_t>(bufferAddr), static_cast<uint64_t>(bufferAddrCheck));
872 Destroy();
873 }
874
TransferJSArrayBufferTest3(std::pair<uint8_t *,size_t> data)875 void TransferJSArrayBufferTest3(std::pair<uint8_t *, size_t> data)
876 {
877 Init();
878 JSDeserializer deserializer(thread, data.first, data.second);
879 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
880 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize TransferJSArrayBuffer3 fail";
881 EXPECT_TRUE(res->IsArrayBuffer()) << "[NotJSArrayBuffer] Deserialize TransferJSArrayBuffer3 fail";
882
883 JSHandle<JSArrayBuffer> arrBuf = JSHandle<JSArrayBuffer>::Cast(res);
884 EXPECT_EQ(arrBuf->GetArrayBufferByteLength(), 0);
885 JSHandle<JSTaggedValue> nativePtr(thread, arrBuf->GetArrayBufferData());
886 EXPECT_TRUE(nativePtr->IsUndefined()) << "[NotJSNativePointer] Deserialize TransferJSArrayBuffer3 fail";
887 Destroy();
888 }
889
TransferJSArrayBufferTest4(std::pair<uint8_t *,size_t> data,uintptr_t bufferAddrCheck)890 void TransferJSArrayBufferTest4(std::pair<uint8_t *, size_t> data, uintptr_t bufferAddrCheck)
891 {
892 Init();
893 JSDeserializer deserializer(thread, data.first, data.second);
894 JSHandle<JSTaggedValue> res = deserializer.Deserialize();
895 EXPECT_TRUE(!res.IsEmpty()) << "[Empty] Deserialize TransferJSArrayBuffer4 fail";
896 EXPECT_TRUE(res->IsJSInt8Array()) << "[NotJSArrayBuffer] Deserialize TransferJSArrayBuffer4 fail";
897
898 JSHandle<JSTypedArray> resJSInt8Array = JSHandle<JSTypedArray>::Cast(res);
899
900 JSHandle<JSTaggedValue> typedArrayName(thread, resJSInt8Array->GetTypedArrayName());
901 uint32_t byteLength = resJSInt8Array->GetByteLength();
902 uint32_t byteOffset = resJSInt8Array->GetByteOffset();
903 uint32_t arrayLength = resJSInt8Array->GetArrayLength();
904 ContentType contentType = resJSInt8Array->GetContentType();
905 JSHandle<JSTaggedValue> viewedArrayBuffer(thread, resJSInt8Array->GetViewedArrayBufferOrByteArray());
906 EXPECT_EQ(byteLength, 10) << "Not Same ByteLength"; // 10: bufferLength
907 EXPECT_EQ(byteOffset, 0) << "Not Same ByteOffset";
908 EXPECT_EQ(arrayLength, 10) << "Not Same ArrayLength"; // 10: arrayLength
909 EXPECT_TRUE(contentType == ContentType::Number) << "Not Same ContentType";
910
911 // check arrayBuffer
912 EXPECT_TRUE(viewedArrayBuffer->IsArrayBuffer());
913 JSHandle<JSArrayBuffer> resJSArrayBuffer(viewedArrayBuffer);
914 uint32_t resTaggedLength = resJSArrayBuffer->GetArrayBufferByteLength();
915 EXPECT_EQ(resTaggedLength, 10) << "Not same viewedBuffer length"; // 10: bufferLength
916 JSHandle<JSTaggedValue> resBufferData(thread, resJSArrayBuffer->GetArrayBufferData());
917 JSHandle<JSNativePointer> resNp = JSHandle<JSNativePointer>::Cast(resBufferData);
918 void *resBuffer = resNp->GetExternalPointer();
919 for (uint32_t i = 0; i < resTaggedLength; i++) {
920 EXPECT_EQ(static_cast<uint8_t *>(resBuffer)[i], i) << "Not same viewedBuffer";
921 }
922 JSHandle<JSTaggedValue> nativePtr(thread, resJSArrayBuffer->GetArrayBufferData());
923 EXPECT_TRUE(nativePtr->IsJSNativePointer()) << "[NotJSNativePointer] Deserialize TransferJSArrayBuffer4 fail";
924 JSHandle<JSNativePointer> np = JSHandle<JSNativePointer>::Cast(nativePtr);
925 uintptr_t bufferAddr = reinterpret_cast<uintptr_t>(np->GetExternalPointer());
926 // The deserialized C buffer pointer shall be same to the original one
927 EXPECT_EQ(static_cast<uint64_t>(bufferAddr), static_cast<uint64_t>(bufferAddrCheck));
928 Destroy();
929 }
930
931 private:
932 EcmaVM *ecmaVm = nullptr;
933 EcmaHandleScope *scope = nullptr;
934 JSThread *thread = nullptr;
935 };
936
937 class JSSerializerTest : public testing::Test {
938 public:
SetUpTestCase()939 static void SetUpTestCase()
940 {
941 GTEST_LOG_(INFO) << "SetUpTestCase";
942 }
943
TearDownTestCase()944 static void TearDownTestCase()
945 {
946 GTEST_LOG_(INFO) << "TearDownCase";
947 }
948
SetUp()949 void SetUp() override
950 {
951 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
952 }
953
TearDown()954 void TearDown() override
955 {
956 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
957 }
958
959 JSThread *thread {nullptr};
960 EcmaVM *ecmaVm {nullptr};
961 EcmaHandleScope *scope {nullptr};
962 };
963
HWTEST_F_L0(JSSerializerTest,SerializeJSSpecialValue)964 HWTEST_F_L0(JSSerializerTest, SerializeJSSpecialValue)
965 {
966 JSSerializer *serializer = new JSSerializer(thread);
967 JSHandle<JSTaggedValue> jsTrue(thread, JSTaggedValue::True());
968 JSHandle<JSTaggedValue> jsFalse(thread, JSTaggedValue::False());
969 JSHandle<JSTaggedValue> jsUndefined(thread, JSTaggedValue::Undefined());
970 JSHandle<JSTaggedValue> jsNull(thread, JSTaggedValue::Null());
971 JSHandle<JSTaggedValue> jsHole(thread, JSTaggedValue::Hole());
972 serializer->SerializeJSTaggedValue(jsTrue);
973 serializer->SerializeJSTaggedValue(jsFalse);
974 serializer->SerializeJSTaggedValue(jsUndefined);
975 serializer->SerializeJSTaggedValue(jsNull);
976 serializer->SerializeJSTaggedValue(jsHole);
977 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
978 JSDeserializerTest jsDeserializerTest;
979 std::thread t1(&JSDeserializerTest::JSSpecialValueTest, jsDeserializerTest, data);
980 t1.join();
981 delete serializer;
982 };
983
HWTEST_F_L0(JSSerializerTest,SerializeJSPlainObject1)984 HWTEST_F_L0(JSSerializerTest, SerializeJSPlainObject1)
985 {
986 ObjectFactory *factory = ecmaVm->GetFactory();
987 JSHandle<JSObject> obj1 = factory->NewEmptyJSObject();
988 JSHandle<JSObject> obj2 = factory->NewEmptyJSObject();
989
990 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("2"));
991 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("3"));
992 JSHandle<JSTaggedValue> key3(factory->NewFromASCII("x"));
993 JSHandle<JSTaggedValue> key4(factory->NewFromASCII("y"));
994 JSHandle<JSTaggedValue> key5(factory->NewFromASCII("a"));
995 JSHandle<JSTaggedValue> key6(factory->NewFromASCII("b"));
996 JSHandle<JSTaggedValue> key7(factory->NewFromASCII("5"));
997 JSHandle<JSTaggedValue> key8(factory->NewFromASCII("6"));
998 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(1));
999 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(2));
1000 JSHandle<JSTaggedValue> value3(thread, JSTaggedValue(3));
1001 JSHandle<JSTaggedValue> value4(thread, JSTaggedValue(4));
1002 JSHandle<JSTaggedValue> value5(thread, JSTaggedValue(5));
1003 JSHandle<JSTaggedValue> value6(thread, JSTaggedValue(6));
1004 JSHandle<JSTaggedValue> value7(thread, JSTaggedValue(7));
1005 JSHandle<JSTaggedValue> value8(thread, JSTaggedValue(8));
1006
1007 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key1, value1);
1008 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key2, value2);
1009 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key3, value3);
1010 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key4, value4);
1011 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key5, value5);
1012 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key6, value6);
1013 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key7, value7);
1014 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key8, value8);
1015
1016 JSSerializer *serializer = new JSSerializer(thread);
1017 bool success1 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj1));
1018 bool success2 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj2));
1019
1020 EXPECT_TRUE(success1);
1021 EXPECT_TRUE(success2);
1022 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1023 JSDeserializerTest jsDeserializerTest;
1024 std::thread t1(&JSDeserializerTest::JSPlainObjectTest1, jsDeserializerTest, data);
1025 t1.join();
1026 delete serializer;
1027 };
1028
HWTEST_F_L0(JSSerializerTest,SerializeJSPlainObject2)1029 HWTEST_F_L0(JSSerializerTest, SerializeJSPlainObject2)
1030 {
1031 ObjectFactory *factory = ecmaVm->GetFactory();
1032 JSHandle<JSObject> obj1 = factory->NewEmptyJSObject();
1033 JSHandle<JSObject> obj2 = factory->NewEmptyJSObject();
1034
1035 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("diagnosisItem"));
1036 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("detectStatus"));
1037 JSHandle<JSTaggedValue> key3(factory->NewFromASCII("detectResultItems"));
1038 JSHandle<JSTaggedValue> key4(factory->NewFromASCII("faultId"));
1039 JSHandle<JSTaggedValue> key5(factory->NewFromASCII("faultContent"));
1040 JSHandle<JSTaggedValue> key6(factory->NewFromASCII("faultContentParams"));
1041 JSHandle<JSTaggedValue> key7(factory->NewFromASCII("adviceId"));
1042 JSHandle<JSTaggedValue> key8(factory->NewFromASCII("adviceContent"));
1043 JSHandle<JSTaggedValue> key9(factory->NewFromASCII("adviceContentParams"));
1044 JSHandle<JSTaggedValue> value1 = JSHandle<JSTaggedValue>::Cast(factory->NewFromASCII("VoiceDetect1"));
1045 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(1));
1046 JSHandle<JSTaggedValue> value3 = JSHandle<JSTaggedValue>::Cast(factory->NewJSArray());
1047 JSHandle<JSTaggedValue> value4 = JSHandle<JSTaggedValue>::Cast(factory->NewFromASCII("80000001"));
1048 JSHandle<JSTaggedValue> value5 = JSHandle<JSTaggedValue>::Cast(factory->GetEmptyString());
1049 JSHandle<JSTaggedValue> value6 = JSHandle<JSTaggedValue>::Cast(factory->NewJSArray());
1050 JSHandle<JSTaggedValue> value7 = JSHandle<JSTaggedValue>::Cast(factory->GetEmptyString());
1051 JSHandle<JSTaggedValue> value8 = JSHandle<JSTaggedValue>::Cast(factory->GetEmptyString());
1052 JSHandle<JSTaggedValue> value9 = JSHandle<JSTaggedValue>::Cast(factory->NewJSArray());
1053
1054 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key4, value4);
1055 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key5, value5);
1056 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key6, value6);
1057 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key7, value7);
1058 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key8, value8);
1059 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key9, value9);
1060 JSArray::FastSetPropertyByValue(thread, value3, 0, JSHandle<JSTaggedValue>(obj2));
1061
1062 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key1, value1);
1063 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key2, value2);
1064 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key3, value3);
1065
1066 JSSerializer *serializer = new JSSerializer(thread);
1067 bool success1 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj1));
1068
1069 EXPECT_TRUE(success1);
1070 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1071 JSDeserializerTest jsDeserializerTest;
1072 std::thread t1(&JSDeserializerTest::JSPlainObjectTest2, jsDeserializerTest, data);
1073 t1.join();
1074 delete serializer;
1075 };
1076
detach(void * param1,void * param2,void * hint)1077 static void* detach(void *param1, void *param2, void *hint)
1078 {
1079 GTEST_LOG_(INFO) << "detach is running";
1080 if (param1 == nullptr && param2 == nullptr) {
1081 GTEST_LOG_(INFO) << "detach: two params is nullptr";
1082 }
1083 if (hint == nullptr) {
1084 GTEST_LOG_(INFO) << "detach: hint is nullptr";
1085 }
1086 return nullptr;
1087 }
1088
attach(void * enginePointer,void * buffer,void * hint)1089 static void* attach([[maybe_unused]] void *enginePointer, [[maybe_unused]] void *buffer, [[maybe_unused]] void *hint)
1090 {
1091 GTEST_LOG_(INFO) << "attach is running";
1092 return nullptr;
1093 }
1094
HWTEST_F_L0(JSSerializerTest,SerializeNativeBindingObject1)1095 HWTEST_F_L0(JSSerializerTest, SerializeNativeBindingObject1)
1096 {
1097 ObjectFactory *factory = ecmaVm->GetFactory();
1098 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
1099 JSHandle<JSObject> obj1 = factory->NewEmptyJSObject();
1100 JSHandle<JSObject> obj2 = factory->NewEmptyJSObject();
1101 JSHandle<JSObject> obj3 = factory->NewEmptyJSObject();
1102
1103 JSHandle<JSTaggedValue> key1 = env->GetDetachSymbol();
1104 JSHandle<JSTaggedValue> key2 = env->GetAttachSymbol();
1105 JSHandle<JSTaggedValue> key3(factory->NewFromASCII("x"));
1106 JSHandle<JSTaggedValue> key4(factory->NewFromASCII("y"));
1107 JSHandle<JSTaggedValue> key5(factory->NewFromASCII("a"));
1108 JSHandle<JSTaggedValue> key6(factory->NewFromASCII("b"));
1109 JSHandle<JSTaggedValue> key7(factory->NewFromASCII("5"));
1110 JSHandle<JSTaggedValue> key8(factory->NewFromASCII("6"));
1111 JSHandle<JSTaggedValue> value1(factory->NewJSNativePointer(reinterpret_cast<void*>(detach)));
1112 JSHandle<JSTaggedValue> value2(factory->NewJSNativePointer(reinterpret_cast<void*>(attach)));
1113 JSHandle<JSTaggedValue> value3(thread, JSTaggedValue(1));
1114 JSHandle<JSTaggedValue> value4(thread, JSTaggedValue(2));
1115 JSHandle<JSTaggedValue> value5(thread, JSTaggedValue(3));
1116 JSHandle<JSTaggedValue> value6(thread, JSTaggedValue(4));
1117 JSHandle<JSTaggedValue> value7(thread, JSTaggedValue(5));
1118 JSHandle<JSTaggedValue> value8(thread, JSTaggedValue(6));
1119
1120 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key1, value1);
1121 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key2, value2);
1122 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key3, value3);
1123 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key4, value4);
1124 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key5, value5);
1125 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key6, value6);
1126 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key7, value7);
1127 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key8, value8);
1128 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj3), key1, value1);
1129 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj3), key2, value2);
1130 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj3), key3, value3);
1131 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj3), key4, value4);
1132
1133 JSSerializer *serializer = new JSSerializer(thread);
1134 bool success1 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj1));
1135 bool success2 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj2));
1136 bool success3 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj3));
1137
1138 EXPECT_TRUE(success1);
1139 EXPECT_TRUE(success2);
1140 EXPECT_TRUE(success3);
1141 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1142 JSDeserializerTest jsDeserializerTest;
1143 std::thread t1(&JSDeserializerTest::NativeBindingObjectTest1, jsDeserializerTest, data);
1144 t1.join();
1145 delete serializer;
1146 }
1147
1148 // not support native object without detach and attach
HWTEST_F_L0(JSSerializerTest,SerializeNativeBindingObject2)1149 HWTEST_F_L0(JSSerializerTest, SerializeNativeBindingObject2)
1150 {
1151 ObjectFactory *factory = ecmaVm->GetFactory();
1152 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
1153 JSHandle<JSObject> obj1 = factory->NewEmptyJSObject();
1154 JSHandle<JSObject> obj2 = factory->NewEmptyJSObject();
1155
1156 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("abc"));
1157 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("xyz"));
1158 JSHandle<JSTaggedValue> key3(factory->NewFromASCII("6"));
1159 JSHandle<JSTaggedValue> key4(factory->NewFromASCII("8"));
1160 JSHandle<JSTaggedValue> key5 = env->GetAttachSymbol();
1161 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(6));
1162 JSHandle<JSTaggedValue> value2 = JSHandle<JSTaggedValue>::Cast(factory->NewFromASCII("VALUE"));
1163 JSHandle<JSTaggedValue> value3 = JSHandle<JSTaggedValue>::Cast(factory->NewJSArray());
1164 JSHandle<JSTaggedValue> value4 = JSHandle<JSTaggedValue>::Cast(factory->GetEmptyString());
1165 JSHandle<JSTaggedValue> value5(factory->NewJSNativePointer(reinterpret_cast<void*>(attach)));
1166
1167 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key1, value1);
1168 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key2, value2);
1169 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key3, value3);
1170 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key4, value4);
1171 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key5, value5);
1172
1173 JSSerializer *serializer = new JSSerializer(thread);
1174 bool success1 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj1));
1175 bool success2 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj2));
1176
1177 EXPECT_TRUE(success1);
1178 EXPECT_FALSE(success2);
1179 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1180 JSDeserializerTest jsDeserializerTest;
1181 std::thread t1(&JSDeserializerTest::NativeBindingObjectTest2, jsDeserializerTest, data);
1182 t1.join();
1183 delete serializer;
1184 }
1185
HWTEST_F_L0(JSSerializerTest,SerializeNativeBindingObject3)1186 HWTEST_F_L0(JSSerializerTest, SerializeNativeBindingObject3)
1187 {
1188 ObjectFactory *factory = ecmaVm->GetFactory();
1189 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
1190 JSHandle<JSObject> obj1 = factory->NewEmptyJSObject();
1191 JSHandle<JSObject> obj2 = factory->NewEmptyJSObject();
1192
1193 JSHandle<JSTaggedValue> key1 = env->GetDetachSymbol();
1194 JSHandle<JSTaggedValue> key2 = env->GetAttachSymbol();
1195 JSHandle<JSTaggedValue> key3(factory->NewFromASCII("root1"));
1196 JSHandle<JSTaggedValue> key4(factory->NewFromASCII("root2"));
1197 JSHandle<JSTaggedValue> key5(factory->NewFromASCII("root3"));
1198 JSHandle<JSTaggedValue> key6(factory->NewFromASCII("name"));
1199 JSHandle<JSTaggedValue> key7(factory->NewFromASCII("version"));
1200 JSHandle<JSTaggedValue> key8(factory->NewFromASCII("product"));
1201 JSHandle<JSTaggedValue> key9(factory->NewFromASCII("os"));
1202 JSHandle<JSTaggedValue> key10(factory->NewFromASCII("system"));
1203 JSHandle<JSTaggedValue> value1(factory->NewJSNativePointer(reinterpret_cast<void*>(detach)));
1204 JSHandle<JSTaggedValue> value2(factory->NewJSNativePointer(reinterpret_cast<void*>(attach)));
1205 JSHandle<JSTaggedValue> value3(factory->NewFromASCII(""));
1206
1207 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key1, value1);
1208 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj1), key2, value2);
1209 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key3, value3);
1210 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key4, value3);
1211 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key5, value3);
1212 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key6, value3);
1213 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key7, value3);
1214 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key8, value3);
1215 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key9, value3);
1216 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj2), key10, value3);
1217
1218 JSSerializer *serializer = new JSSerializer(thread);
1219 bool success1 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj1));
1220 bool success2 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj2));
1221 EXPECT_TRUE(success1);
1222 EXPECT_TRUE(success2);
1223 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1224 JSDeserializerTest jsDeserializerTest;
1225 std::thread t1(&JSDeserializerTest::NativeBindingObjectTest3, jsDeserializerTest, data);
1226 t1.join();
1227 delete serializer;
1228 }
1229
HWTEST_F_L0(JSSerializerTest,TestSerializeDescription)1230 HWTEST_F_L0(JSSerializerTest, TestSerializeDescription)
1231 {
1232 ObjectFactory *factory = ecmaVm->GetFactory();
1233 JSHandle<JSObject> obj = factory->NewEmptyJSObject();
1234
1235 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("x"));
1236 JSHandle<JSTaggedValue> key2(thread->GetEcmaVM()->GetFactory()->NewFromASCII("y"));
1237
1238 PropertyDescriptor desc1(thread);
1239 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(1));
1240 desc1.SetValue(value1);
1241 desc1.SetWritable(true);
1242 desc1.SetEnumerable(false);
1243 desc1.SetConfigurable(true);
1244 JSObject::DefineOwnProperty(thread, obj, key1, desc1);
1245
1246 PropertyDescriptor desc2(thread);
1247 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(2));
1248 desc2.SetValue(value2);
1249 desc2.SetWritable(false);
1250 desc2.SetEnumerable(true);
1251 desc2.SetConfigurable(false);
1252 JSObject::DefineOwnProperty(thread, obj, key2, desc2);
1253
1254 JSSerializer *serializer = new JSSerializer(thread);
1255 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj));
1256 EXPECT_TRUE(success);
1257 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1258 JSDeserializerTest jsDeserializerTest;
1259 std::thread t1(&JSDeserializerTest::DescriptionTest, jsDeserializerTest, data);
1260 t1.join();
1261 delete serializer;
1262 };
1263
HWTEST_F_L0(JSSerializerTest,TestSerializeJSSet)1264 HWTEST_F_L0(JSSerializerTest, TestSerializeJSSet)
1265 {
1266 ObjectFactory *factory = ecmaVm->GetFactory();
1267 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1268
1269 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsSetFunction();
1270 JSHandle<JSSet> set =
1271 JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1272 JSHandle<LinkedHashSet> linkedSet = LinkedHashSet::Create(thread);
1273 set->SetLinkedSet(thread, linkedSet);
1274 // set property to set
1275 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(7));
1276 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(9));
1277 JSHandle<JSTaggedValue> value3(factory->NewFromASCII("x"));
1278 JSHandle<JSTaggedValue> value4(factory->NewFromASCII("y"));
1279
1280 JSSet::Add(thread, set, value1);
1281 JSSet::Add(thread, set, value2);
1282 JSSet::Add(thread, set, value3);
1283 JSSet::Add(thread, set, value4);
1284
1285 // set property to object
1286 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("5"));
1287 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("6"));
1288
1289 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(set), key1, value1);
1290 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(set), key2, value2);
1291
1292 JSSerializer *serializer = new JSSerializer(thread);
1293 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(set));
1294 EXPECT_TRUE(success);
1295 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1296 JSDeserializerTest jsDeserializerTest;
1297 std::thread t1(&JSDeserializerTest::JSSetTest, jsDeserializerTest, data);
1298 t1.join();
1299 delete serializer;
1300 };
1301
HWTEST_F_L0(JSSerializerTest,TestSerializeJSArray)1302 HWTEST_F_L0(JSSerializerTest, TestSerializeJSArray)
1303 {
1304 ObjectFactory *factory = ecmaVm->GetFactory();
1305 JSHandle<JSArray> array = factory->NewJSArray();
1306
1307 // set property to object
1308 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("abasd"));
1309 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("qweqwedasd"));
1310
1311 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(7));
1312 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(9));
1313
1314 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(array), key1, value1);
1315 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(array), key2, value2);
1316
1317 // set value to array
1318 array->SetArrayLength(thread, 20);
1319 for (int i = 0; i < 20; i++) {
1320 JSHandle<JSTaggedValue> data(thread, JSTaggedValue(i));
1321 JSArray::FastSetPropertyByValue(thread, JSHandle<JSTaggedValue>::Cast(array), i, data);
1322 }
1323
1324 JSSerializer *serializer = new JSSerializer(thread);
1325 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(array));
1326 EXPECT_TRUE(success);
1327 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1328 JSDeserializerTest jsDeserializerTest;
1329 std::thread t1(&JSDeserializerTest::JSArrayTest, jsDeserializerTest, data);
1330 t1.join();
1331 delete serializer;
1332 };
1333
1334 // Test the situation that Objects' properties stores values that reference with each other
HWTEST_F_L0(JSSerializerTest,TestObjectsPropertyReference)1335 HWTEST_F_L0(JSSerializerTest, TestObjectsPropertyReference)
1336 {
1337 ObjectFactory *factory = ecmaVm->GetFactory();
1338 JSHandle<JSObject> obj1 = factory->NewEmptyJSObject();
1339 JSHandle<JSObject> obj2 = factory->NewEmptyJSObject();
1340 [[maybe_unused]] JSHandle<JSObject> obj3 = factory->NewEmptyJSObject();
1341
1342 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("abc"));
1343 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("def"));
1344 JSHandle<JSTaggedValue> key3(factory->NewFromASCII("dgsdgf"));
1345 JSHandle<JSTaggedValue> key4(factory->NewFromASCII("qwjhrf"));
1346
1347 JSHandle<JSTaggedValue> value3(thread, JSTaggedValue(10));
1348 JSHandle<JSTaggedValue> value4(thread, JSTaggedValue(5));
1349
1350 // set property to obj1
1351 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>::Cast(obj1), key1, JSHandle<JSTaggedValue>::Cast(obj2));
1352 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>::Cast(obj1), key3, value3);
1353
1354 // set property to obj2
1355 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>::Cast(obj2), key2, JSHandle<JSTaggedValue>::Cast(obj1));
1356 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>::Cast(obj2), key4, value4);
1357
1358 JSSerializer *serializer = new JSSerializer(thread);
1359 bool success1 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj1));
1360 bool success2 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(obj2));
1361 EXPECT_TRUE(success1) << "Serialize obj1 fail";
1362 EXPECT_TRUE(success2) << "Serialize obj2 fail";
1363 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1364 JSDeserializerTest jsDeserializerTest;
1365 std::thread t1(&JSDeserializerTest::ObjectsPropertyReferenceTest, jsDeserializerTest, data);
1366 t1.join();
1367 delete serializer;
1368 };
1369
HWTEST_F_L0(JSSerializerTest,SerializeEcmaString1)1370 HWTEST_F_L0(JSSerializerTest, SerializeEcmaString1)
1371 {
1372 const char *rawStr = "this is a test ecmaString";
1373 JSHandle<EcmaString> ecmaString = thread->GetEcmaVM()->GetFactory()->NewFromASCII(rawStr);
1374 JSSerializer *serializer = new JSSerializer(thread);
1375 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(ecmaString));
1376 EXPECT_TRUE(success) << "Serialize EcmaString fail";
1377 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1378 JSDeserializerTest jsDeserializerTest;
1379 std::thread t1(&JSDeserializerTest::EcmaStringTest1, jsDeserializerTest, data);
1380 t1.join();
1381 delete serializer;
1382 };
1383
HWTEST_F_L0(JSSerializerTest,SerializeEcmaString2)1384 HWTEST_F_L0(JSSerializerTest, SerializeEcmaString2)
1385 {
1386 const char *rawStr = "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"\
1387 "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"\
1388 "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"\
1389 "ssssss";
1390 JSHandle<EcmaString> ecmaString = thread->GetEcmaVM()->GetFactory()->NewFromASCII(rawStr);
1391 JSSerializer *serializer = new JSSerializer(thread);
1392 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(ecmaString));
1393 EXPECT_TRUE(success) << "Serialize EcmaString fail";
1394 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1395 JSDeserializerTest jsDeserializerTest;
1396 std::thread t1(&JSDeserializerTest::EcmaStringTest2, jsDeserializerTest, data);
1397 t1.join();
1398 delete serializer;
1399 };
1400
HWTEST_F_L0(JSSerializerTest,SerializeEcmaString3)1401 HWTEST_F_L0(JSSerializerTest, SerializeEcmaString3)
1402 {
1403 JSHandle<EcmaString> ecmaString = thread->GetEcmaVM()->GetFactory()->GetEmptyString();
1404 JSSerializer *serializer = new JSSerializer(thread);
1405 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(ecmaString));
1406 EXPECT_TRUE(success) << "Serialize EcmaString fail";
1407 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1408 JSDeserializerTest jsDeserializerTest;
1409 std::thread t1(&JSDeserializerTest::EcmaStringTest3, jsDeserializerTest, data);
1410 t1.join();
1411 delete serializer;
1412 };
1413
1414 // Test EcmaString contains Chinese Text
HWTEST_F_L0(JSSerializerTest,SerializeEcmaString4)1415 HWTEST_F_L0(JSSerializerTest, SerializeEcmaString4)
1416 {
1417 std::string rawStr = "你好,世界";
1418 JSHandle<EcmaString> ecmaString = thread->GetEcmaVM()->GetFactory()->NewFromStdString(rawStr);
1419 JSSerializer *serializer = new JSSerializer(thread);
1420 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(ecmaString));
1421 EXPECT_TRUE(success) << "Serialize EcmaString fail";
1422 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1423 JSDeserializerTest jsDeserializerTest;
1424 std::thread t1(&JSDeserializerTest::EcmaStringTest4, jsDeserializerTest, data);
1425 t1.join();
1426 delete serializer;
1427 };
1428
HWTEST_F_L0(JSSerializerTest,SerializeInt32_t)1429 HWTEST_F_L0(JSSerializerTest, SerializeInt32_t)
1430 {
1431 JSSerializer *serializer = new JSSerializer(thread);
1432 int32_t a = 64, min = -2147483648, b = -63;
1433 JSTaggedValue aTag(a), minTag(min), bTag(b);
1434 bool success1 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(thread, aTag));
1435 bool success2 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(thread, minTag));
1436 bool success3 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(thread, bTag));
1437 EXPECT_TRUE(success1 && success2 && success3) << "Serialize Int32 fail";
1438 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1439 JSDeserializerTest jsDeserializerTest;
1440 std::thread t1(&JSDeserializerTest::Int32Test, jsDeserializerTest, data);
1441 t1.join();
1442 delete serializer;
1443 };
1444
HWTEST_F_L0(JSSerializerTest,SerializeDouble)1445 HWTEST_F_L0(JSSerializerTest, SerializeDouble)
1446 {
1447 JSSerializer *serializer = new JSSerializer(thread);
1448 double a = 3.1415926535, b = -3.1415926535;
1449 JSTaggedValue aTag(a), bTag(b);
1450 bool success1 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(thread, aTag));
1451 bool success2 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(thread, bTag));
1452 EXPECT_TRUE(success1 && success2) << "Serialize Double fail";
1453 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1454 JSDeserializerTest jsDeserializerTest;
1455 std::thread t1(&JSDeserializerTest::DoubleTest, jsDeserializerTest, data);
1456 t1.join();
1457 delete serializer;
1458 };
1459
JSDateCreate(EcmaVM * ecmaVM)1460 JSDate *JSDateCreate(EcmaVM *ecmaVM)
1461 {
1462 ObjectFactory *factory = ecmaVM->GetFactory();
1463 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
1464 JSHandle<JSTaggedValue> dateFunction = globalEnv->GetDateFunction();
1465 JSHandle<JSDate> dateObject =
1466 JSHandle<JSDate>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(dateFunction), dateFunction));
1467 return *dateObject;
1468 }
1469
HWTEST_F_L0(JSSerializerTest,SerializeDate)1470 HWTEST_F_L0(JSSerializerTest, SerializeDate)
1471 {
1472 double tm = 28 * 60 * 60 * 1000;
1473 JSHandle<JSDate> jsDate(thread, JSDateCreate(ecmaVm));
1474 jsDate->SetTimeValue(thread, JSTaggedValue(tm));
1475 JSSerializer *serializer = new JSSerializer(thread);
1476 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(jsDate));
1477 EXPECT_TRUE(success) << "Serialize JSDate fail";
1478 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1479 JSDeserializerTest jsDeserializerTest;
1480 std::thread t1(&JSDeserializerTest::JSDateTest, jsDeserializerTest, data);
1481 t1.join();
1482 delete serializer;
1483 };
1484
CreateMap(JSThread * thread)1485 JSMap *CreateMap(JSThread *thread)
1486 {
1487 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1488 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1489 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
1490 JSHandle<JSMap> map =
1491 JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1492 JSHandle<LinkedHashMap> linkedMap = LinkedHashMap::Create(thread);
1493 map->SetLinkedMap(thread, linkedMap);
1494 return *map;
1495 }
1496
HWTEST_F_L0(JSSerializerTest,SerializeJSMap)1497 HWTEST_F_L0(JSSerializerTest, SerializeJSMap)
1498 {
1499 JSHandle<JSMap> map(thread, CreateMap(thread));
1500 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1501 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("3"));
1502 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(12345));
1503 JSMap::Set(thread, map, key1, value1);
1504 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("key1"));
1505 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(34567));
1506 JSMap::Set(thread, map, key2, value2);
1507
1508 JSSerializer *serializer = new JSSerializer(thread);
1509 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(map));
1510 EXPECT_TRUE(success) << "Serialize JSMap fail";
1511 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1512 JSDeserializerTest jsDeserializerTest;
1513 std::thread t1(&JSDeserializerTest::JSMapTest, jsDeserializerTest, data, map);
1514 t1.join();
1515 delete serializer;
1516 };
1517
CreateJSArrayBuffer(JSThread * thread)1518 JSArrayBuffer *CreateJSArrayBuffer(JSThread *thread)
1519 {
1520 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1521 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1522 JSHandle<JSTaggedValue> target = env->GetArrayBufferFunction();
1523 JSHandle<JSArrayBuffer> jsArrayBuffer =
1524 JSHandle<JSArrayBuffer>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(target), target));
1525 return *jsArrayBuffer;
1526 }
1527
HWTEST_F_L0(JSSerializerTest,SerializeJSArrayBuffer)1528 HWTEST_F_L0(JSSerializerTest, SerializeJSArrayBuffer)
1529 {
1530 JSHandle<JSArrayBuffer> jsArrayBuffer(thread, CreateJSArrayBuffer(thread));
1531 int32_t byteLength = 10;
1532 thread->GetEcmaVM()->GetFactory()->NewJSArrayBufferData(jsArrayBuffer, byteLength);
1533 jsArrayBuffer->SetArrayBufferByteLength(byteLength);
1534 JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(jsArrayBuffer);
1535 JSHandle<JSTaggedValue> number(thread, JSTaggedValue(7));
1536 BuiltinsArrayBuffer::SetValueInBuffer(thread, obj.GetTaggedValue(), 1, DataViewType::UINT8,
1537 number, true);
1538 number = JSHandle<JSTaggedValue>(thread, JSTaggedValue(17));
1539 BuiltinsArrayBuffer::SetValueInBuffer(thread, obj.GetTaggedValue(), 3, DataViewType::UINT8, // 3:index
1540 number, true);
1541
1542 JSSerializer *serializer = new JSSerializer(thread);
1543 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(jsArrayBuffer));
1544 EXPECT_TRUE(success) << "Serialize JSArrayBuffer fail";
1545 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1546 JSDeserializerTest jsDeserializerTest;
1547 std::thread t1(&JSDeserializerTest::JSArrayBufferTest, jsDeserializerTest, data, jsArrayBuffer, 10, nullptr);
1548 t1.join();
1549 delete serializer;
1550 };
1551
HWTEST_F_L0(JSSerializerTest,SerializeJSArrayBufferShared)1552 HWTEST_F_L0(JSSerializerTest, SerializeJSArrayBufferShared)
1553 {
1554 std::string msg = "hello world";
1555 uint32_t msgBufferLen = msg.length() + 1U;
1556 char* msgBuffer = new char[msgBufferLen] { 0 };
1557 if (memcpy_s(msgBuffer, msgBufferLen, msg.c_str(), msgBufferLen) != EOK) {
1558 delete[] msgBuffer;
1559 EXPECT_TRUE(false) << " memcpy error";
1560 }
1561
1562 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1563 JSHandle<JSArrayBuffer> jsArrayBuffer = factory->NewJSArrayBuffer(msgBuffer, msgBufferLen, nullptr, nullptr);
1564
1565 JSSerializer *serializer = new JSSerializer(thread);
1566 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(jsArrayBuffer));
1567 EXPECT_TRUE(success) << "Serialize JSArrayBuffer fail";
1568 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1569 JSDeserializerTest jsDeserializerTest;
1570 std::thread t1(&JSDeserializerTest::JSArrayBufferTest, jsDeserializerTest, data, jsArrayBuffer, 12, nullptr);
1571 t1.join();
1572 delete serializer;
1573 delete[] msgBuffer;
1574 };
1575
HWTEST_F_L0(JSSerializerTest,SerializeJSArrayBufferShared2)1576 HWTEST_F_L0(JSSerializerTest, SerializeJSArrayBufferShared2)
1577 {
1578 std::string msg = "hello world";
1579 int msgBufferLen = static_cast<int>(msg.length()) + 1;
1580 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1581 JSHandle<JSArrayBuffer> jsArrayBuffer = factory->NewJSSharedArrayBuffer(msgBufferLen);
1582 JSHandle<JSTaggedValue> BufferData(thread, jsArrayBuffer->GetArrayBufferData());
1583 JSHandle<JSNativePointer> resNp = JSHandle<JSNativePointer>::Cast(BufferData);
1584 void *Buffer = resNp->GetExternalPointer();
1585 if (memcpy_s(Buffer, msgBufferLen, msg.c_str(), msgBufferLen) != EOK) {
1586 EXPECT_TRUE(false) << " memcpy error";
1587 }
1588 JSSerializer *serializer = new JSSerializer(thread);
1589 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(jsArrayBuffer));
1590 EXPECT_TRUE(success) << "Serialize JSArrayBuffer fail";
1591 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1592 JSDeserializerTest jsDeserializerTest;
1593 std::string changeStr = "world hello";
1594 std::thread t1(&JSDeserializerTest::JSSharedArrayBufferTest,
1595 jsDeserializerTest, data, jsArrayBuffer, 12, changeStr.c_str());
1596 t1.join();
1597 EXPECT_TRUE(strcmp((char *)Buffer, "world hello") == 0) << "Serialize JSArrayBuffer fail";
1598 delete serializer;
1599 };
1600
HWTEST_F_L0(JSSerializerTest,SerializeJSArrayBufferShared3)1601 HWTEST_F_L0(JSSerializerTest, SerializeJSArrayBufferShared3)
1602 {
1603 std::string msg = "hello world";
1604 int msgBufferLen = static_cast<int>(msg.length()) + 1;
1605 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1606 JSHandle<JSArrayBuffer> jsArrayBuffer = factory->NewJSSharedArrayBuffer(msgBufferLen);
1607 JSHandle<JSTaggedValue> BufferData(thread, jsArrayBuffer->GetArrayBufferData());
1608 JSHandle<JSNativePointer> resNp = JSHandle<JSNativePointer>::Cast(BufferData);
1609 void *Buffer = resNp->GetExternalPointer();
1610 if (memcpy_s(Buffer, msgBufferLen, msg.c_str(), msgBufferLen) != EOK) {
1611 EXPECT_TRUE(false) << " memcpy error";
1612 }
1613 JSSerializer *serializer = new JSSerializer(thread);
1614 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(jsArrayBuffer));
1615 EXPECT_TRUE(success) << "Serialize JSArrayBuffer fail";
1616 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1617 JSDeserializerTest jsDeserializerTest;
1618 std::string changeStr = "aaaaaaaaaa";
1619 std::thread t1(&JSDeserializerTest::JSSharedArrayBufferTest1, jsDeserializerTest, data, 5, changeStr.c_str());
1620 t1.join();
1621 EXPECT_TRUE(strcmp((char *)Buffer, "aaaaa world") == 0) << "Serialize JSArrayBuffer fail";
1622 JSSerializer *serializer2 = new JSSerializer(thread);
1623 bool success2 = serializer2->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(jsArrayBuffer));
1624 EXPECT_TRUE(success2) << "Serialize JSArrayBuffer fail";
1625 std::pair<uint8_t *, size_t> data2 = serializer2->ReleaseBuffer();
1626 JSDeserializerTest jsDeserializerTest2;
1627 std::string changeStr2 = "bbbbbbbbbbb";
1628 std::thread t2(&JSDeserializerTest::JSSharedArrayBufferTest1, jsDeserializerTest2, data2, 6, changeStr2.c_str());
1629 t2.join();
1630 EXPECT_TRUE(strcmp((char *)Buffer, "bbbbbbworld") == 0) << "Serialize JSArrayBuffer fail";
1631 delete serializer;
1632 delete serializer2;
1633 };
1634
HWTEST_F_L0(JSSerializerTest,SerializeJSRegExp)1635 HWTEST_F_L0(JSSerializerTest, SerializeJSRegExp)
1636 {
1637 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1638 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1639 JSHandle<JSTaggedValue> target = env->GetRegExpFunction();
1640 JSHandle<JSRegExp> jsRegexp =
1641 JSHandle<JSRegExp>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(target), target));
1642 JSHandle<EcmaString> pattern = thread->GetEcmaVM()->GetFactory()->NewFromASCII("key2");
1643 JSHandle<EcmaString> flags = thread->GetEcmaVM()->GetFactory()->NewFromASCII("i");
1644 char buffer[] = "1234567"; // use char to simulate bytecode
1645 uint32_t bufferSize = 7;
1646 factory->NewJSRegExpByteCodeData(jsRegexp, static_cast<void *>(buffer), bufferSize);
1647 jsRegexp->SetOriginalSource(thread, JSHandle<JSTaggedValue>(pattern));
1648 jsRegexp->SetOriginalFlags(thread, JSHandle<JSTaggedValue>(flags));
1649
1650 JSSerializer *serializer = new JSSerializer(thread);
1651 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(jsRegexp));
1652 EXPECT_TRUE(success) << "Serialize JSRegExp fail";
1653 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1654 JSDeserializerTest jsDeserializerTest;
1655 std::thread t1(&JSDeserializerTest::JSRegexpTest, jsDeserializerTest, data);
1656 t1.join();
1657 delete serializer;
1658 };
1659
CreateTestJSArrayBuffer(JSThread * thread)1660 JSArrayBuffer *CreateTestJSArrayBuffer(JSThread *thread)
1661 {
1662 JSHandle<JSArrayBuffer> jsArrayBuffer(thread, CreateJSArrayBuffer(thread));
1663 int32_t byteLength = 10;
1664 thread->GetEcmaVM()->GetFactory()->NewJSArrayBufferData(jsArrayBuffer, byteLength);
1665 jsArrayBuffer->SetArrayBufferByteLength(byteLength);
1666 JSHandle<JSTaggedValue> obj = JSHandle<JSTaggedValue>(jsArrayBuffer);
1667 JSMutableHandle<JSTaggedValue> number(thread, JSTaggedValue::Undefined());
1668 for (int i = 0; i < 10; i++) { // 10: arrayLength
1669 number.Update(JSTaggedValue(i));
1670 BuiltinsArrayBuffer::SetValueInBuffer(thread, obj.GetTaggedValue(), i, DataViewType::UINT8,
1671 number, true);
1672 }
1673 return *jsArrayBuffer;
1674 }
1675
HWTEST_F_L0(JSSerializerTest,SerializeJSTypedArray1)1676 HWTEST_F_L0(JSSerializerTest, SerializeJSTypedArray1)
1677 {
1678 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1679 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1680 JSHandle<JSTaggedValue> target = env->GetInt8ArrayFunction();
1681 JSHandle<JSTypedArray> int8Array =
1682 JSHandle<JSTypedArray>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(target), target));
1683 JSHandle<JSTaggedValue> viewedArrayBuffer(thread, CreateTestJSArrayBuffer(thread));
1684 int8Array->SetViewedArrayBufferOrByteArray(thread, viewedArrayBuffer);
1685 int byteLength = 10;
1686 int byteOffset = 0;
1687 int arrayLength = (byteLength - byteOffset) / (sizeof(int8_t));
1688 int8Array->SetByteLength(byteLength);
1689 int8Array->SetByteOffset(byteOffset);
1690 int8Array->SetTypedArrayName(thread, thread->GlobalConstants()->GetInt8ArrayString());
1691 int8Array->SetArrayLength(arrayLength);
1692 int8Array->SetContentType(ContentType::Number);
1693 JSSerializer *serializer = new JSSerializer(thread);
1694 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(int8Array));
1695 EXPECT_TRUE(success);
1696 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1697 JSDeserializerTest jsDeserializerTest;
1698 std::thread t1(&JSDeserializerTest::TypedArrayTest1, jsDeserializerTest, data);
1699 t1.join();
1700 delete serializer;
1701 };
1702
HWTEST_F_L0(JSSerializerTest,SerializeJSTypedArray2)1703 HWTEST_F_L0(JSSerializerTest, SerializeJSTypedArray2)
1704 {
1705 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1706 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1707 JSHandle<JSTaggedValue> target = env->GetInt8ArrayFunction();
1708 JSHandle<JSTypedArray> int8Array =
1709 JSHandle<JSTypedArray>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(target), target));
1710 uint8_t value = 255; // 255 : test case
1711 JSTaggedType val = JSTaggedValue(value).GetRawData();
1712 int byteArrayLength = 10; // 10: arrayLength
1713 JSHandle<ByteArray> byteArray = factory->NewByteArray(byteArrayLength, sizeof(value));
1714 for (int i = 0; i < byteArrayLength; i++) {
1715 byteArray->Set(thread, i, DataViewType::UINT8, val);
1716 }
1717 int8Array->SetViewedArrayBufferOrByteArray(thread, byteArray);
1718 int byteLength = 10;
1719 int byteOffset = 0;
1720 int arrayLength = (byteLength - byteOffset) / (sizeof(int8_t));
1721 int8Array->SetByteLength(byteLength);
1722 int8Array->SetByteOffset(byteOffset);
1723 int8Array->SetTypedArrayName(thread, thread->GlobalConstants()->GetInt8ArrayString());
1724 int8Array->SetArrayLength(arrayLength);
1725 int8Array->SetContentType(ContentType::Number);
1726 JSSerializer *serializer = new JSSerializer(thread);
1727 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(int8Array));
1728 EXPECT_TRUE(success);
1729 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1730 JSDeserializerTest jsDeserializerTest;
1731 std::thread t1(&JSDeserializerTest::TypedArrayTest2, jsDeserializerTest, data);
1732 t1.join();
1733 delete serializer;
1734 };
1735
HWTEST_F_L0(JSSerializerTest,SerializeTaggedArray)1736 HWTEST_F_L0(JSSerializerTest, SerializeTaggedArray)
1737 {
1738 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1739 JSHandle<TaggedArray> taggedArray(factory->NewTaggedArray(4));
1740 JSHandle<EcmaString> str1 = factory->NewFromASCII("str11");
1741 JSHandle<EcmaString> str2 = factory->NewFromASCII("str22");
1742 // set value to the taggedarray
1743 taggedArray->Set(thread, 0, str1.GetTaggedValue());
1744 taggedArray->Set(thread, 1, str2.GetTaggedValue());
1745 taggedArray->Set(thread, 2, JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined()));
1746
1747 JSSerializer *serializer = new JSSerializer(thread);
1748 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(taggedArray));
1749 EXPECT_TRUE(success);
1750 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1751 JSDeserializerTest jsDeserializerTest;
1752 std::thread t1(&JSDeserializerTest::TaggedArrayTest, jsDeserializerTest, data);
1753 t1.join();
1754 delete serializer;
1755 };
1756
CreateJSPandaFile(const char * source,const CString filename,uint32_t * methodOffset)1757 std::shared_ptr<JSPandaFile> CreateJSPandaFile(const char *source, const CString filename, uint32_t *methodOffset)
1758 {
1759 pandasm::Parser parser;
1760 const std::string fn = "SRC.pa"; // test file name : "SRC.pa"
1761 auto res = parser.Parse(source, fn);
1762
1763 std::unique_ptr<const panda_file::File> pfPtr = pandasm::AsmEmitter::Emit(res.Value());
1764 JSPandaFileManager *pfManager = JSPandaFileManager::GetInstance();
1765 std::shared_ptr<JSPandaFile> pf = pfManager->NewJSPandaFile(pfPtr.release(), filename);
1766 const panda_file::File *file = pf->GetPandaFile();
1767 const uint8_t *typeDesc = utf::CStringAsMutf8("L_GLOBAL;");
1768 panda_file::File::EntityId classId = file->GetClassId(typeDesc);
1769 EXPECT_TRUE(classId.IsValid());
1770
1771 panda_file::ClassDataAccessor cda(*file, classId);
1772 cda.EnumerateMethods([&](panda_file::MethodDataAccessor &mda) {
1773 *methodOffset = mda.GetMethodId().GetOffset();
1774 });
1775 return pf;
1776 }
1777
TestFunc()1778 static void TestFunc()
1779 {
1780 return;
1781 }
1782
HWTEST_F_L0(JSSerializerTest,SerializeMethod1)1783 HWTEST_F_L0(JSSerializerTest, SerializeMethod1)
1784 {
1785 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1786 JSHandle<Method> method = factory->NewMethodForNativeFunction(reinterpret_cast<void*>(TestFunc));
1787 EXPECT_TRUE(method.GetTaggedValue().IsMethod());
1788
1789 JSSerializer *serializer = new JSSerializer(thread);
1790 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(method));
1791 EXPECT_TRUE(success);
1792 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1793 JSDeserializerTest jsDeserializerTest;
1794 std::thread t1(&JSDeserializerTest::MethodTest, jsDeserializerTest, data);
1795 t1.join();
1796 delete serializer;
1797 };
1798
HWTEST_F_L0(JSSerializerTest,SerializeMethod2)1799 HWTEST_F_L0(JSSerializerTest, SerializeMethod2)
1800 {
1801 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1802 const char *source = R"(
1803 .function void foo() {
1804 lda.str "helloworld"
1805 returnundefined
1806 }
1807 )";
1808 const CString fileName = "test.pa";
1809 uint32_t methodOffset = 0;
1810 std::shared_ptr<JSPandaFile> pf = CreateJSPandaFile(source, fileName, &methodOffset);
1811 MethodLiteral *methodLiteral = new MethodLiteral(EntityId(methodOffset));
1812 JSHandle<Method> method = factory->NewMethod(methodLiteral);
1813 EXPECT_TRUE(method.GetTaggedValue().IsMethod());
1814 JSPandaFileManager::GetInstance()->AddJSPandaFileVm(thread->GetEcmaVM(), pf);
1815 EXPECT_TRUE(pf != nullptr);
1816
1817 JSHandle<ConstantPool> constPool = factory->NewConstantPool(4);
1818 constPool->SetJSPandaFile(pf.get());
1819 EXPECT_TRUE(constPool.GetTaggedValue().IsConstantPool());
1820 method->SetConstantPool(thread, constPool.GetTaggedValue());
1821
1822 JSSerializer *serializer = new JSSerializer(thread);
1823 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(method));
1824 EXPECT_TRUE(success);
1825 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1826 JSDeserializerTest jsDeserializerTest;
1827 std::thread t1(&JSDeserializerTest::MethodTest1, jsDeserializerTest, data);
1828 t1.join();
1829 delete serializer;
1830 };
1831
1832 // support concurrent function
HWTEST_F_L0(JSSerializerTest,SerializeConcurrentFunction)1833 HWTEST_F_L0(JSSerializerTest, SerializeConcurrentFunction)
1834 {
1835 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1836 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1837 JSHandle<JSFunction> concurrentFunction = factory->NewJSFunction(env, nullptr, FunctionKind::CONCURRENT_FUNCTION);
1838 EXPECT_TRUE(concurrentFunction->IsJSFunction());
1839 EXPECT_TRUE(concurrentFunction->GetFunctionKind() == ecmascript::FunctionKind::CONCURRENT_FUNCTION);
1840
1841 JSSerializer *serializer = new JSSerializer(thread);
1842 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(concurrentFunction));
1843 EXPECT_TRUE(success);
1844 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1845 JSDeserializerTest jsDeserializerTest;
1846 std::thread t1(&JSDeserializerTest::ConcurrentFunctionTest, jsDeserializerTest, data);
1847 t1.join();
1848 delete serializer;
1849 };
1850
HWTEST_F_L0(JSSerializerTest,SerializeBigInt)1851 HWTEST_F_L0(JSSerializerTest, SerializeBigInt)
1852 {
1853 CString str1 = "100000000000000000000000000000000000000000000000000000"; // Binary: 2 ^ 53
1854 CString str2 = "111111111111111111111111111111111111111111111111111111"; // Binary: 2 ^ 54 - 1
1855 CString str3 = "-9007199254740991012345";
1856 CString str4 = "1234567890987654321"; // Decimal
1857 JSHandle<BigInt> bigInt1 = BigIntHelper::SetBigInt(thread, str1);
1858 JSHandle<BigInt> bigInt2 = BigIntHelper::SetBigInt(thread, str2);
1859 JSHandle<BigInt> bigInt3 = BigIntHelper::SetBigInt(thread, str3);
1860 JSHandle<BigInt> bigInt4 = BigInt::Uint32ToBigInt(thread, 0); // 0 : test case
1861 JSHandle<BigInt> bigInt5 = BigIntHelper::SetBigInt(thread, str4, BigInt::DECIMAL);
1862 JSHandle<BigInt> bigInt6 = BigInt::Uint64ToBigInt(thread, ULLONG_MAX);
1863 JSHandle<BigInt> bigInt7 = BigInt::Uint64ToBigInt(thread, UINT_MAX);
1864 JSHandle<BigInt> bigInt8 = BigInt::Uint32ToBigInt(thread, std::numeric_limits<uint32_t>::max());
1865 JSHandle<BigInt> bigInt9 = BigInt::Uint32ToBigInt(thread, std::numeric_limits<uint32_t>::min());
1866 JSHandle<BigInt> bigInt10 = BigInt::Int64ToBigInt(thread, LLONG_MAX);
1867 JSHandle<BigInt> bigInt11 = BigInt::Int64ToBigInt(thread, LLONG_MIN);
1868 JSHandle<BigInt> bigInt12 = BigInt::Int64ToBigInt(thread, INT_MAX);
1869 JSHandle<BigInt> bigInt13 = BigInt::Int64ToBigInt(thread, INT_MIN);
1870 JSHandle<BigInt> bigInt14 = BigInt::Int32ToBigInt(thread, -1); // -1 : test case
1871
1872 JSSerializer *serializer = new JSSerializer(thread);
1873 bool success1 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt1));
1874 bool success2 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt2));
1875 bool success3 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt3));
1876 bool success4 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt4));
1877 bool success5 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt5));
1878 bool success6 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt6));
1879 bool success7 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt7));
1880 bool success8 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt8));
1881 bool success9 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt9));
1882 bool success10 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt10));
1883 bool success11 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt11));
1884 bool success12 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt12));
1885 bool success13 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt13));
1886 bool success14 = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(bigInt14));
1887 EXPECT_TRUE(success1);
1888 EXPECT_TRUE(success2);
1889 EXPECT_TRUE(success3);
1890 EXPECT_TRUE(success4);
1891 EXPECT_TRUE(success5);
1892 EXPECT_TRUE(success6);
1893 EXPECT_TRUE(success7);
1894 EXPECT_TRUE(success8);
1895 EXPECT_TRUE(success9);
1896 EXPECT_TRUE(success10);
1897 EXPECT_TRUE(success11);
1898 EXPECT_TRUE(success12);
1899 EXPECT_TRUE(success13);
1900 EXPECT_TRUE(success14);
1901 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1902 JSDeserializerTest jsDeserializerTest;
1903 std::thread t1(&JSDeserializerTest::BigIntTest, jsDeserializerTest, data);
1904 t1.join();
1905 delete serializer;
1906 }
1907
HWTEST_F_L0(JSSerializerTest,SerializeObjectWithConcurrentFunction)1908 HWTEST_F_L0(JSSerializerTest, SerializeObjectWithConcurrentFunction)
1909 {
1910 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1911 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1912 JSHandle<JSFunction> concurrentFunction1 = factory->NewJSFunction(env, nullptr, FunctionKind::CONCURRENT_FUNCTION);
1913 EXPECT_TRUE(concurrentFunction1->IsJSFunction());
1914 EXPECT_TRUE(concurrentFunction1->GetFunctionKind() == ecmascript::FunctionKind::CONCURRENT_FUNCTION);
1915 JSHandle<JSFunction> concurrentFunction2 = factory->NewJSFunction(env, nullptr, FunctionKind::CONCURRENT_FUNCTION);
1916 EXPECT_TRUE(concurrentFunction2->IsJSFunction());
1917 EXPECT_TRUE(concurrentFunction2->GetFunctionKind() == ecmascript::FunctionKind::CONCURRENT_FUNCTION);
1918 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("1"));
1919 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("2"));
1920 JSHandle<JSTaggedValue> key3(factory->NewFromASCII("abc"));
1921 JSHandle<JSTaggedValue> key4(factory->NewFromASCII("4"));
1922 JSHandle<JSTaggedValue> key5(factory->NewFromASCII("key"));
1923 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(12345));
1924 JSHandle<JSTaggedValue> value2(factory->NewFromASCII("def"));
1925 JSHandle<JSTaggedValue> value3(factory->NewFromASCII("value"));
1926 JSHandle<JSObject> obj = factory->NewEmptyJSObject();
1927 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key1, value1);
1928 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key2, JSHandle<JSTaggedValue>(concurrentFunction1));
1929 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key3, value2);
1930 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key4, JSHandle<JSTaggedValue>(concurrentFunction2));
1931 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(obj), key5, value3);
1932
1933 JSSerializer *serializer = new JSSerializer(thread);
1934 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(obj));
1935 EXPECT_TRUE(success);
1936 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1937 JSDeserializerTest jsDeserializerTest;
1938 std::thread t1(&JSDeserializerTest::ObjectWithConcurrentFunctionTest, jsDeserializerTest, data);
1939 t1.join();
1940 delete serializer;
1941 };
1942
1943 // not support function
HWTEST_F_L0(JSSerializerTest,SerializeFunction)1944 HWTEST_F_L0(JSSerializerTest, SerializeFunction)
1945 {
1946 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1947 JSHandle<JSTaggedValue> function = env->GetFunctionFunction();
1948 EXPECT_TRUE(function->IsJSFunction());
1949
1950 JSSerializer *serializer = new JSSerializer(thread);
1951 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(function));
1952 EXPECT_FALSE(success);
1953 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1954 JSDeserializer deserializer(thread, data.first, data.second);
1955 JSHandle<JSTaggedValue> ret = deserializer.Deserialize();
1956 EXPECT_TRUE(ret.IsEmpty());
1957 delete serializer;
1958 }
1959
1960 // not support symbol
HWTEST_F_L0(JSSerializerTest,SerializeSymbolWithProperty)1961 HWTEST_F_L0(JSSerializerTest, SerializeSymbolWithProperty)
1962 {
1963 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1964 JSHandle<JSSymbol> jsSymbol = factory->NewJSSymbol();
1965 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("2"));
1966 JSHandle<JSTaggedValue> key2(factory->NewFromASCII("x"));
1967 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(1));
1968 JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(8));
1969 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsSymbol), key1, value1);
1970 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(jsSymbol), key2, value2);
1971
1972 JSSerializer *serializer = new JSSerializer(thread);
1973 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>(jsSymbol));
1974 EXPECT_FALSE(success);
1975 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
1976 JSDeserializer deserializer(thread, data.first, data.second);
1977 JSHandle<JSTaggedValue> ret = deserializer.Deserialize();
1978 EXPECT_TRUE(ret.IsEmpty());
1979 delete serializer;
1980 };
1981
1982 // Test transfer JSArrayBuffer
HWTEST_F_L0(JSSerializerTest,TransferJSArrayBuffer1)1983 HWTEST_F_L0(JSSerializerTest, TransferJSArrayBuffer1)
1984 {
1985 ObjectFactory *factory = ecmaVm->GetFactory();
1986
1987 // create a JSArrayBuffer
1988 size_t length = 5;
1989 uint8_t value = 100;
1990 void *buffer = ecmaVm->GetNativeAreaAllocator()->AllocateBuffer(length);
1991 if (memset_s(buffer, length, value, length) != EOK) {
1992 LOG_ECMA(FATAL) << "this branch is unreachable";
1993 UNREACHABLE();
1994 }
1995 JSHandle<JSArrayBuffer> arrBuf = factory->NewJSArrayBuffer(buffer,
1996 length, NativeAreaAllocator::FreeBufferFunc, ecmaVm->GetNativeAreaAllocator());
1997 JSHandle<JSTaggedValue> arrBufTag = JSHandle<JSTaggedValue>::Cast(arrBuf);
1998
1999 CUnorderedSet<uintptr_t> transferDataSet;
2000 uintptr_t addr = static_cast<uintptr_t>(arrBufTag.GetTaggedType());
2001 transferDataSet.insert(addr);
2002 JSSerializer *serializer = new JSSerializer(thread);
2003 serializer->InitTransferSet(transferDataSet);
2004 bool success = serializer->SerializeJSTaggedValue(arrBufTag);
2005 EXPECT_TRUE(success);
2006 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
2007 JSDeserializerTest jsDeserializerTest;
2008 std::thread t1(&JSDeserializerTest::TransferJSArrayBufferTest1,
2009 jsDeserializerTest,
2010 data,
2011 reinterpret_cast<uintptr_t>(buffer));
2012 t1.join();
2013 delete serializer;
2014 // test if detached
2015 EXPECT_TRUE(arrBuf->IsDetach());
2016 };
2017
2018 // Test serialize JSArrayBuffer that not transfer
HWTEST_F_L0(JSSerializerTest,TransferJSArrayBuffer2)2019 HWTEST_F_L0(JSSerializerTest, TransferJSArrayBuffer2)
2020 {
2021 ObjectFactory *factory = ecmaVm->GetFactory();
2022
2023 // create a JSArrayBuffer
2024 size_t length = 5;
2025 uint8_t value = 100;
2026 void *buffer = ecmaVm->GetNativeAreaAllocator()->AllocateBuffer(length);
2027 if (memset_s(buffer, length, value, length) != EOK) {
2028 LOG_ECMA(FATAL) << "this branch is unreachable";
2029 UNREACHABLE();
2030 }
2031 JSHandle<JSArrayBuffer> arrBuf = factory->NewJSArrayBuffer(buffer,
2032 length, NativeAreaAllocator::FreeBufferFunc, ecmaVm->GetNativeAreaAllocator());
2033 JSHandle<JSTaggedValue> arrBufTag = JSHandle<JSTaggedValue>::Cast(arrBuf);
2034
2035 JSSerializer *serializer = new JSSerializer(thread);
2036 bool success = serializer->SerializeJSTaggedValue(arrBufTag);
2037 EXPECT_TRUE(success);
2038 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
2039 JSDeserializerTest jsDeserializerTest;
2040 std::thread t1(&JSDeserializerTest::TransferJSArrayBufferTest2, jsDeserializerTest, data,
2041 reinterpret_cast<uintptr_t>(buffer));
2042 t1.join();
2043 delete serializer;
2044 // test if detached
2045 EXPECT_FALSE(arrBuf->IsDetach());
2046 };
2047
2048 // Test serialize an empty JSArrayBuffer
HWTEST_F_L0(JSSerializerTest,TransferJSArrayBuffer3)2049 HWTEST_F_L0(JSSerializerTest, TransferJSArrayBuffer3)
2050 {
2051 ObjectFactory *factory = ecmaVm->GetFactory();
2052
2053 // create a JSArrayBuffer
2054 JSHandle<JSArrayBuffer> arrBuf = factory->NewJSArrayBuffer(0);
2055 JSHandle<JSTaggedValue> arrBufTag = JSHandle<JSTaggedValue>::Cast(arrBuf);
2056
2057 JSSerializer *serializer = new JSSerializer(thread);
2058 bool success = serializer->SerializeJSTaggedValue(arrBufTag);
2059 EXPECT_TRUE(success);
2060 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
2061 JSDeserializerTest jsDeserializerTest;
2062 std::thread t1(&JSDeserializerTest::TransferJSArrayBufferTest3, jsDeserializerTest, data);
2063 t1.join();
2064 delete serializer;
2065 // test if detached
2066 EXPECT_FALSE(arrBuf->IsDetach());
2067 };
2068
2069 // Test JSArrayBuffer transfer mixed with other properties in a typedArray
HWTEST_F_L0(JSSerializerTest,TransferJSArrayBufferInTypedArray)2070 HWTEST_F_L0(JSSerializerTest, TransferJSArrayBufferInTypedArray)
2071 {
2072 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2073 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
2074 JSHandle<JSTaggedValue> target = env->GetInt8ArrayFunction();
2075 JSHandle<JSTypedArray> int8Array =
2076 JSHandle<JSTypedArray>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(target), target));
2077 JSHandle<JSArrayBuffer> arrBuffer(thread, CreateTestJSArrayBuffer(thread));
2078 JSHandle<JSNativePointer> np =
2079 JSHandle<JSNativePointer>::Cast(JSHandle<JSTaggedValue>(thread, arrBuffer->GetArrayBufferData()));
2080 // c buffer ptr
2081 uintptr_t buffer = reinterpret_cast<uintptr_t>(np->GetExternalPointer());
2082 int8Array->SetViewedArrayBufferOrByteArray(thread, JSHandle<JSTaggedValue>(arrBuffer));
2083 int byteLength = 10;
2084 int byteOffset = 0;
2085 int arrayLength = (byteLength - byteOffset) / (sizeof(int8_t));
2086 int8Array->SetByteLength(byteLength);
2087 int8Array->SetByteOffset(byteOffset);
2088 int8Array->SetTypedArrayName(thread, thread->GlobalConstants()->GetInt8ArrayString());
2089 int8Array->SetArrayLength(arrayLength);
2090 int8Array->SetContentType(ContentType::Number);
2091 CUnorderedSet<uintptr_t> transferDataSet;
2092 uintptr_t addr = static_cast<uintptr_t>(arrBuffer.GetTaggedType());
2093 transferDataSet.insert(addr);
2094 JSSerializer *serializer = new JSSerializer(thread);
2095 serializer->InitTransferSet(transferDataSet);
2096 bool success = serializer->SerializeJSTaggedValue(JSHandle<JSTaggedValue>::Cast(int8Array));
2097 EXPECT_TRUE(success);
2098 std::pair<uint8_t *, size_t> data = serializer->ReleaseBuffer();
2099 JSDeserializerTest jsDeserializerTest;
2100 std::thread t1(&JSDeserializerTest::TransferJSArrayBufferTest4, jsDeserializerTest, data, buffer);
2101 t1.join();
2102 delete serializer;
2103 // test if detached
2104 EXPECT_TRUE(arrBuffer->IsDetach());
2105 };
2106 } // namespace panda::test
2107