• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/tests/test_helper.h"
17 
18 #include <cstddef>
19 
20 #include "ecmascript/builtins/builtins_function.h"
21 #include "ecmascript/ecma_global_storage.h"
22 #include "ecmascript/ecma_vm.h"
23 #include "ecmascript/global_env.h"
24 #include "ecmascript/js_thread.h"
25 #include "ecmascript/napi/include/jsnapi.h"
26 #include "ecmascript/napi/jsnapi_helper.h"
27 #include "ecmascript/object_factory.h"
28 
29 using namespace panda;
30 using namespace panda::ecmascript;
31 
32 namespace panda::test {
33 using BuiltinsFunction = ecmascript::builtins::BuiltinsFunction;
34 class JSNApiTests : public testing::Test {
35 public:
SetUpTestCase()36     static void SetUpTestCase()
37     {
38         GTEST_LOG_(INFO) << "SetUpTestCase";
39     }
40 
TearDownTestCase()41     static void TearDownTestCase()
42     {
43         GTEST_LOG_(INFO) << "TearDownCase";
44     }
45 
SetUp()46     void SetUp() override
47     {
48         RuntimeOption option;
49         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
50         vm_ = JSNApi::CreateJSVM(option);
51         ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
52         thread_ = vm_->GetJSThread();
53         vm_->SetEnableForceGC(true);
54     }
55 
TearDown()56     void TearDown() override
57     {
58         vm_->SetEnableForceGC(false);
59         JSNApi::DestroyJSVM(vm_);
60     }
61 
62 protected:
63     JSThread *thread_ = nullptr;
64     EcmaVM *vm_ = nullptr;
65 };
66 
FunctionCallback(JsiRuntimeCallInfo * info)67 Local<JSValueRef> FunctionCallback(JsiRuntimeCallInfo* info)
68 {
69     EscapeLocalScope scope(info->GetVM());
70     return scope.Escape(ArrayRef::New(info->GetVM(), info->GetArgsNumber()));
71 }
72 
WeakRefCallback(EcmaVM * vm)73 void WeakRefCallback(EcmaVM* vm)
74 {
75     LocalScope scope(vm);
76     Local<ObjectRef> object = ObjectRef::New(vm);
77     Global<ObjectRef> globalObject(vm, object);
78     globalObject.SetWeak();
79     Local<ObjectRef> object1 = ObjectRef::New(vm);
80     Global<ObjectRef> globalObject1(vm, object1);
81     globalObject1.SetWeak();
82     vm->CollectGarbage(TriggerGCType::YOUNG_GC);
83     vm->CollectGarbage(TriggerGCType::OLD_GC);
84     globalObject.FreeGlobalHandleAddr();
85 }
86 
ThreadCheck(const EcmaVM * vm)87 void ThreadCheck(const EcmaVM *vm)
88 {
89     EXPECT_TRUE(vm->GetJSThread()->GetThreadId() != JSThread::GetCurrentThreadId());
90 }
91 
HWTEST_F_L0(JSNApiTests,GetGlobalObject)92 HWTEST_F_L0(JSNApiTests, GetGlobalObject)
93 {
94     LocalScope scope(vm_);
95     Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm_);
96     ASSERT_FALSE(globalObject.IsEmpty());
97     ASSERT_TRUE(globalObject->IsObject());
98 }
99 
HWTEST_F_L0(JSNApiTests,ThreadIdCheck)100 HWTEST_F_L0(JSNApiTests, ThreadIdCheck)
101 {
102     EXPECT_TRUE(vm_->GetJSThread()->GetThreadId() == JSThread::GetCurrentThreadId());
103 }
104 
HWTEST_F_L0(JSNApiTests,RegisterFunction)105 HWTEST_F_L0(JSNApiTests, RegisterFunction)
106 {
107     LocalScope scope(vm_);
108     Local<FunctionRef> callback = FunctionRef::New(vm_, FunctionCallback);
109     ASSERT_TRUE(!callback.IsEmpty());
110     std::vector<Local<JSValueRef>> arguments;
111     arguments.emplace_back(JSValueRef::Undefined(vm_));
112     Local<JSValueRef> result =
113         callback->Call(vm_, JSValueRef::Undefined(vm_), arguments.data(), arguments.size());
114     ASSERT_TRUE(result->IsArray(vm_));
115     Local<ArrayRef> array(result);
116     ASSERT_EQ(static_cast<uint64_t>(array->Length(vm_)), arguments.size());
117 }
118 
HWTEST_F_L0(JSNApiTests,GetProperty)119 HWTEST_F_L0(JSNApiTests, GetProperty)
120 {
121     LocalScope scope(vm_);
122     Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm_);
123     ASSERT_FALSE(globalObject.IsEmpty());
124     ASSERT_TRUE(globalObject->IsObject());
125 
126     Local<ObjectRef> key = StringRef::NewFromUtf8(vm_, "Number");
127     Local<ObjectRef> property = globalObject->Get(vm_, key);
128     ASSERT_TRUE(property->IsFunction());
129 }
130 
HWTEST_F_L0(JSNApiTests,SetProperty)131 HWTEST_F_L0(JSNApiTests, SetProperty)
132 {
133     LocalScope scope(vm_);
134     Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm_);
135     ASSERT_FALSE(globalObject.IsEmpty());
136     ASSERT_TRUE(globalObject->IsObject());
137 
138     Local<ArrayRef> property = ArrayRef::New(vm_, 3); // 3 : length
139     ASSERT_TRUE(property->IsArray(vm_));
140     ASSERT_EQ(property->Length(vm_), 3); // 3 : test case of input
141 
142     Local<ObjectRef> key = StringRef::NewFromUtf8(vm_, "Test");
143     bool result = globalObject->Set(vm_, key, property);
144     ASSERT_TRUE(result);
145 
146     Local<ObjectRef> propertyGet = globalObject->Get(vm_, key);
147     ASSERT_TRUE(propertyGet->IsArray(vm_));
148     ASSERT_EQ(Local<ArrayRef>(propertyGet)->Length(vm_), 3); // 3 : test case of input
149 }
150 
HWTEST_F_L0(JSNApiTests,JsonParser)151 HWTEST_F_L0(JSNApiTests, JsonParser)
152 {
153     LocalScope scope(vm_);
154     Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm_);
155     ASSERT_FALSE(globalObject.IsEmpty());
156     ASSERT_TRUE(globalObject->IsObject());
157 
158     const char *const test{R"({"orientation": "portrait"})"};
159     Local<ObjectRef> jsonString = StringRef::NewFromUtf8(vm_, test);
160 
161     Local<JSValueRef> result = JSON::Parse(vm_, jsonString);
162     ASSERT_TRUE(result->IsObject());
163 
164     Local<ObjectRef> keyString = StringRef::NewFromUtf8(vm_, "orientation");
165     Local<JSValueRef> property = Local<ObjectRef>(result)->Get(vm_, keyString);
166     ASSERT_TRUE(property->IsString());
167 }
168 
HWTEST_F_L0(JSNApiTests,StrictEqual)169 HWTEST_F_L0(JSNApiTests, StrictEqual)
170 {
171     LocalScope scope(vm_);
172     Local<StringRef> origin = StringRef::NewFromUtf8(vm_, "1");
173     Local<StringRef> target1 = StringRef::NewFromUtf8(vm_, "1");
174     Local<NumberRef> target = NumberRef::New(vm_, 1);
175 
176     ASSERT_FALSE(origin->IsStrictEquals(vm_, target));
177     ASSERT_TRUE(origin->IsStrictEquals(vm_, target1));
178 }
179 
HWTEST_F_L0(JSNApiTests,InstanceOf)180 HWTEST_F_L0(JSNApiTests, InstanceOf)
181 {
182     LocalScope scope(vm_);
183     Local<FunctionRef> target = FunctionRef::New(vm_, nullptr);
184     Local<ArrayRef> origin = ArrayRef::New(vm_, 1);
185 
186     ASSERT_FALSE(origin->InstanceOf(vm_, target));
187 }
188 
HWTEST_F_L0(JSNApiTests,TypeOf)189 HWTEST_F_L0(JSNApiTests, TypeOf)
190 {
191     LocalScope scope(vm_);
192     Local<StringRef> origin = StringRef::NewFromUtf8(vm_, "1");
193     Local<StringRef> typeString = origin->Typeof(vm_);
194     ASSERT_EQ(typeString->ToString(), "string");
195 
196     Local<NumberRef> target = NumberRef::New(vm_, 1);
197     typeString = target->Typeof(vm_);
198     ASSERT_EQ(typeString->ToString(), "number");
199 }
200 
HWTEST_F_L0(JSNApiTests,Symbol)201 HWTEST_F_L0(JSNApiTests, Symbol)
202 {
203     LocalScope scope(vm_);
204     Local<StringRef> description = StringRef::NewFromUtf8(vm_, "test");
205     Local<SymbolRef> symbol = SymbolRef::New(vm_, description);
206 
207     ASSERT_FALSE(description->IsSymbol());
208     ASSERT_TRUE(symbol->IsSymbol());
209 }
210 
HWTEST_F_L0(JSNApiTests,StringUtf8_001)211 HWTEST_F_L0(JSNApiTests, StringUtf8_001)
212 {
213     LocalScope scope(vm_);
214     std::string test = "Hello world";
215     Local<StringRef> testString = StringRef::NewFromUtf8(vm_, test.c_str());
216 
217     EXPECT_EQ(testString->Utf8Length(), 12);          // 12 : length of testString("Hello World")
218     char buffer[12];                                      // 12 : length of testString
219     EXPECT_EQ(testString->WriteUtf8(buffer, 12), 12); // 12 : length of testString("Hello World")
220     std::string res(buffer);
221     ASSERT_EQ(res, test);
222 }
223 
HWTEST_F_L0(JSNApiTests,StringUtf8_002)224 HWTEST_F_L0(JSNApiTests, StringUtf8_002)
225 {
226     LocalScope scope(vm_);
227     std::string test = "年";
228     Local<StringRef> testString = StringRef::NewFromUtf8(vm_, test.c_str());
229 
230     EXPECT_EQ(testString->Utf8Length(), 4);          // 4 : length of testString("年")
231     char buffer[4];                                      // 4 : length of testString
232     EXPECT_EQ(testString->WriteUtf8(buffer, 4), 4); // 4 : length of testString("年")
233     std::string res(buffer);
234     ASSERT_EQ(res, test);
235 }
236 
HWTEST_F_L0(JSNApiTests,ToType)237 HWTEST_F_L0(JSNApiTests, ToType)
238 {
239     LocalScope scope(vm_);
240     Local<StringRef> toString = StringRef::NewFromUtf8(vm_, "-123.3");
241     Local<JSValueRef> toValue(toString);
242 
243     ASSERT_EQ(toString->ToNumber(vm_)->Value(), -123.3); // -123 : test case of input
244     ASSERT_EQ(toString->ToBoolean(vm_)->Value(), true);
245     ASSERT_EQ(toValue->ToString(vm_)->ToString(), "-123.3");
246     ASSERT_TRUE(toValue->ToObject(vm_)->IsObject());
247 }
248 
HWTEST_F_L0(JSNApiTests,TypeValue)249 HWTEST_F_L0(JSNApiTests, TypeValue)
250 {
251     LocalScope scope(vm_);
252     Local<StringRef> toString = StringRef::NewFromUtf8(vm_, "-123");
253     Local<JSValueRef> toValue(toString);
254 
255     ASSERT_EQ(toString->Int32Value(vm_), -123);        // -123 : test case of input
256     ASSERT_EQ(toString->BooleaValue(), true);
257     ASSERT_EQ(toString->Uint32Value(vm_), 4294967173U); // 4294967173 : test case of input
258     ASSERT_EQ(toString->IntegerValue(vm_), -123);      // -123 : test case of input
259 }
260 
detach()261 void* detach()
262 {
263     GTEST_LOG_(INFO) << "detach is running";
264     return nullptr;
265 }
266 
attach(void * buffer)267 void attach([[maybe_unused]] void* buffer)
268 {
269     GTEST_LOG_(INFO) << "attach is running";
270 }
271 
HWTEST_F_L0(JSNApiTests,CreateNativeObject)272 HWTEST_F_L0(JSNApiTests, CreateNativeObject)
273 {
274     LocalScope scope(vm_);
275     Local<ObjectRef> object = ObjectRef::New(vm_, reinterpret_cast<void*>(detach), reinterpret_cast<void*>(attach));
276     Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, "TestKey");
277     Local<JSValueRef> value = ObjectRef::New(vm_);
278     PropertyAttribute attribute(value, true, true, true);
279 
280     ASSERT_TRUE(object->DefineProperty(vm_, key, attribute));
281     Local<JSValueRef> value1 = object->Get(vm_, key);
282     ASSERT_TRUE(value->IsStrictEquals(vm_, value1));
283     ASSERT_TRUE(object->Has(vm_, key));
284     ASSERT_TRUE(object->Delete(vm_, key));
285     ASSERT_FALSE(object->Has(vm_, key));
286 }
287 
HWTEST_F_L0(JSNApiTests,DefineProperty)288 HWTEST_F_L0(JSNApiTests, DefineProperty)
289 {
290     LocalScope scope(vm_);
291     Local<ObjectRef> object = ObjectRef::New(vm_);
292     Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, "TestKey");
293     Local<JSValueRef> value = ObjectRef::New(vm_);
294     PropertyAttribute attribute(value, true, true, true);
295 
296     ASSERT_TRUE(object->DefineProperty(vm_, key, attribute));
297     Local<JSValueRef> value1 = object->Get(vm_, key);
298     ASSERT_TRUE(value->IsStrictEquals(vm_, value1));
299 }
300 
HWTEST_F_L0(JSNApiTests,HasProperty)301 HWTEST_F_L0(JSNApiTests, HasProperty)
302 {
303     LocalScope scope(vm_);
304     Local<ObjectRef> object = ObjectRef::New(vm_);
305     Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, "TestKey");
306     Local<JSValueRef> value = ObjectRef::New(vm_);
307     PropertyAttribute attribute(value, true, true, true);
308 
309     ASSERT_TRUE(object->DefineProperty(vm_, key, attribute));
310     ASSERT_TRUE(object->Has(vm_, key));
311 }
312 
HWTEST_F_L0(JSNApiTests,DeleteProperty)313 HWTEST_F_L0(JSNApiTests, DeleteProperty)
314 {
315     LocalScope scope(vm_);
316     Local<ObjectRef> object = ObjectRef::New(vm_);
317     Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, "TestKey");
318     Local<JSValueRef> value = ObjectRef::New(vm_);
319     PropertyAttribute attribute(value, true, true, true);
320 
321     ASSERT_TRUE(object->DefineProperty(vm_, key, attribute));
322     ASSERT_TRUE(object->Delete(vm_, key));
323     ASSERT_FALSE(object->Has(vm_, key));
324 }
325 
HWTEST_F_L0(JSNApiTests,GetProtoType)326 HWTEST_F_L0(JSNApiTests, GetProtoType)
327 {
328     LocalScope scope(vm_);
329     Local<FunctionRef> function = FunctionRef::New(vm_, nullptr);
330     Local<JSValueRef> protoType = function->GetPrototype(vm_);
331     ASSERT_TRUE(protoType->IsObject());
332 
333     Local<FunctionRef> object = ObjectRef::New(vm_);
334     protoType = object->GetPrototype(vm_);
335     ASSERT_TRUE(protoType->IsObject());
336 
337     Local<FunctionRef> native = ObjectRef::New(vm_, reinterpret_cast<void*>(detach), reinterpret_cast<void*>(attach));
338     protoType = native->GetPrototype(vm_);
339     ASSERT_TRUE(protoType->IsObject());
340 }
341 
CheckReject(JsiRuntimeCallInfo * info)342 void CheckReject(JsiRuntimeCallInfo* info)
343 {
344     ASSERT_EQ(info->GetArgsNumber(), 1U);
345     Local<JSValueRef> reason = info->GetCallArgRef(0);
346     ASSERT_TRUE(reason->IsString());
347     ASSERT_EQ(Local<StringRef>(reason)->ToString(), "Reject");
348 }
349 
RejectCallback(JsiRuntimeCallInfo * info)350 Local<JSValueRef> RejectCallback(JsiRuntimeCallInfo* info)
351 {
352     LocalScope scope(info->GetVM());
353     CheckReject(info);
354     return JSValueRef::Undefined(info->GetVM());
355 }
356 
HWTEST_F_L0(JSNApiTests,PromiseCatch)357 HWTEST_F_L0(JSNApiTests, PromiseCatch)
358 {
359     LocalScope scope(vm_);
360     Local<PromiseCapabilityRef> capability = PromiseCapabilityRef::New(vm_);
361 
362     Local<PromiseRef> promise = capability->GetPromise(vm_);
363     Local<FunctionRef> reject = FunctionRef::New(vm_, RejectCallback);
364     Local<PromiseRef> catchPromise = promise->Catch(vm_, reject);
365     ASSERT_TRUE(promise->IsPromise());
366     ASSERT_TRUE(catchPromise->IsPromise());
367 
368     Local<StringRef> reason = StringRef::NewFromUtf8(vm_, "Reject");
369     ASSERT_TRUE(capability->Reject(vm_, reason));
370 
371     vm_->ExecutePromisePendingJob();
372 }
373 
CheckResolve(JsiRuntimeCallInfo * info)374 void CheckResolve(JsiRuntimeCallInfo* info)
375 {
376     ASSERT_EQ(info->GetArgsNumber(), 1U);
377     Local<JSValueRef> value = info->GetCallArgRef(0);
378     ASSERT_TRUE(value->IsNumber());
379     ASSERT_EQ(Local<NumberRef>(value)->Value(), 300.3); // 300.3 : test case of input
380 }
381 
ResolvedCallback(JsiRuntimeCallInfo * info)382 Local<JSValueRef> ResolvedCallback(JsiRuntimeCallInfo* info)
383 {
384     LocalScope scope(info->GetVM());
385     CheckResolve(info);
386     return JSValueRef::Undefined(info->GetVM());
387 }
388 
HWTEST_F_L0(JSNApiTests,PromiseThen)389 HWTEST_F_L0(JSNApiTests, PromiseThen)
390 {
391     LocalScope scope(vm_);
392     Local<PromiseCapabilityRef> capability = PromiseCapabilityRef::New(vm_);
393 
394     Local<PromiseRef> promise = capability->GetPromise(vm_);
395     Local<FunctionRef> resolve = FunctionRef::New(vm_, ResolvedCallback);
396     Local<FunctionRef> reject = FunctionRef::New(vm_, RejectCallback);
397     Local<PromiseRef> thenPromise = promise->Then(vm_, resolve, reject);
398     ASSERT_TRUE(promise->IsPromise());
399     ASSERT_TRUE(thenPromise->IsPromise());
400 
401     Local<StringRef> value = NumberRef::New(vm_, 300.3); // 300.3 : test case of input
402     ASSERT_TRUE(capability->Resolve(vm_, value));
403     vm_->ExecutePromisePendingJob();
404 }
405 
HWTEST_F_L0(JSNApiTests,Constructor)406 HWTEST_F_L0(JSNApiTests, Constructor)
407 {
408     LocalScope scope(vm_);
409     Local<ObjectRef> object = JSNApi::GetGlobalObject(vm_);
410     Local<StringRef> key = StringRef::NewFromUtf8(vm_, "Number");
411     Local<FunctionRef> numberConstructor = object->Get(vm_, key);
412     Local<JSValueRef> argv[1];
413     argv[0] = NumberRef::New(vm_, 1.3); // 1.3 : test case of input
414     Local<JSValueRef> result = numberConstructor->Constructor(vm_, argv, 1);
415     ASSERT_TRUE(result->IsObject());
416     ASSERT_EQ(result->ToNumber(vm_)->Value(), 1.3); // 1.3 : size of arguments
417 }
418 
HWTEST_F_L0(JSNApiTests,ArrayBuffer)419 HWTEST_F_L0(JSNApiTests, ArrayBuffer)
420 {
421     LocalScope scope(vm_);
422     const int32_t length = 15;
423     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
424     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
425     ASSERT_EQ(arrayBuffer->ByteLength(vm_), length);
426     ASSERT_NE(arrayBuffer->GetBuffer(), nullptr);
427     JSNApi::TriggerGC(vm_);
428 }
429 
HWTEST_F_L0(JSNApiTests,ArrayBufferWithBuffer)430 HWTEST_F_L0(JSNApiTests, ArrayBufferWithBuffer)
431 {
432     static bool isFree = false;
433     struct Data {
434         int32_t length;
435     };
436     const int32_t length = 15;
437     Data *data = new Data();
438     data->length = length;
439     Deleter deleter = [](void *buffer, void *data) -> void {
440         delete[] reinterpret_cast<uint8_t *>(buffer);
441         Data *currentData = reinterpret_cast<Data *>(data);
442         ASSERT_EQ(currentData->length, 15); // 5 : size of arguments
443         delete currentData;
444         isFree = true;
445     };
446     {
447         LocalScope scope(vm_);
448         uint8_t *buffer = new uint8_t[length]();
449         Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, buffer, length, deleter, data);
450         ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
451         ASSERT_EQ(arrayBuffer->ByteLength(vm_), length);
452         ASSERT_EQ(arrayBuffer->GetBuffer(), buffer);
453     }
454     JSNApi::TriggerGC(vm_, JSNApi::TRIGGER_GC_TYPE::FULL_GC);
455     ASSERT_TRUE(isFree);
456 }
457 
HWTEST_F_L0(JSNApiTests,DataView)458 HWTEST_F_L0(JSNApiTests, DataView)
459 {
460     LocalScope scope(vm_);
461     const int32_t length = 15;
462     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
463     JSNApi::TriggerGC(vm_);
464     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
465 
466     // 5 : offset of byte, 7 : length
467     Local<DataViewRef> dataView = DataViewRef::New(vm_, arrayBuffer, 5, 7);
468     ASSERT_TRUE(dataView->IsDataView());
469     ASSERT_EQ(dataView->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
470     ASSERT_EQ(dataView->ByteLength(), 7U); // 7 : size of arguments
471     ASSERT_EQ(dataView->ByteOffset(), 5U); // 5 : size of arguments
472 
473     // 5 : offset of byte, 11 : length
474     dataView = DataViewRef::New(vm_, arrayBuffer, 5, 11);
475     ASSERT_TRUE(dataView->IsUndefined());
476 }
477 
HWTEST_F_L0(JSNApiTests,Int8Array)478 HWTEST_F_L0(JSNApiTests, Int8Array)
479 {
480     LocalScope scope(vm_);
481     const int32_t length = 15;
482     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
483     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
484 
485     // 5 : offset of byte, 6 : length
486     Local<Int8ArrayRef> typedArray = Int8ArrayRef::New(vm_, arrayBuffer, 5, 6);
487     ASSERT_TRUE(typedArray->IsInt8Array());
488     ASSERT_EQ(typedArray->ByteLength(vm_), 6U); // 6 : length of bytes
489     ASSERT_EQ(typedArray->ByteOffset(vm_), 5U); // 5 : offset of byte
490     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array
491     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
492 }
493 
HWTEST_F_L0(JSNApiTests,Uint8Array)494 HWTEST_F_L0(JSNApiTests, Uint8Array)
495 {
496     LocalScope scope(vm_);
497     const int32_t length = 15;
498     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
499     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
500 
501     // 5 : offset of byte, 6 : length
502     Local<Uint8ArrayRef> typedArray = Uint8ArrayRef::New(vm_, arrayBuffer, 5, 6);
503     ASSERT_TRUE(typedArray->IsUint8Array());
504     ASSERT_EQ(typedArray->ByteLength(vm_), 6U);  // 6 : length of bytes
505     ASSERT_EQ(typedArray->ByteOffset(vm_), 5U);  // 5 : offset of byte
506     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array
507     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
508 }
509 
HWTEST_F_L0(JSNApiTests,Uint8ClampedArray)510 HWTEST_F_L0(JSNApiTests, Uint8ClampedArray)
511 {
512     LocalScope scope(vm_);
513     const int32_t length = 15;
514     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
515     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
516 
517     // 5 : offset of byte, 6 : length
518     Local<Uint8ClampedArrayRef> typedArray = Uint8ClampedArrayRef::New(vm_, arrayBuffer, 5, 6);
519     ASSERT_TRUE(typedArray->IsUint8ClampedArray());
520     ASSERT_EQ(typedArray->ByteLength(vm_), 6U);  // 6 : length of bytes
521     ASSERT_EQ(typedArray->ByteOffset(vm_), 5U);  // 5 : offset of byte
522     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array
523     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
524 }
525 
HWTEST_F_L0(JSNApiTests,Int16Array)526 HWTEST_F_L0(JSNApiTests, Int16Array)
527 {
528     LocalScope scope(vm_);
529     const int32_t length = 30;
530     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
531     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
532 
533     // 4 : offset of byte, 6 : length
534     Local<Int16ArrayRef> typedArray = Int16ArrayRef::New(vm_, arrayBuffer, 4, 6);
535     ASSERT_TRUE(typedArray->IsInt16Array());
536     ASSERT_EQ(typedArray->ByteLength(vm_), 12U);  // 12 : length of bytes
537     ASSERT_EQ(typedArray->ByteOffset(vm_), 4U);   // 4 : offset of byte
538     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U);  // 6 : length of array
539     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
540 }
541 
HWTEST_F_L0(JSNApiTests,Uint16Array)542 HWTEST_F_L0(JSNApiTests, Uint16Array)
543 {
544     LocalScope scope(vm_);
545     const int32_t length = 30;
546     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
547     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
548 
549     // 4 : offset of byte, 6 : length
550     Local<Uint16ArrayRef> typedArray = Uint16ArrayRef::New(vm_, arrayBuffer, 4, 6);
551     ASSERT_TRUE(typedArray->IsUint16Array());
552     ASSERT_EQ(typedArray->ByteLength(vm_), 12U);  // 12 : length of bytes
553     ASSERT_EQ(typedArray->ByteOffset(vm_), 4U);   // 4 : offset of byte
554     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U);  // 6 : length of array
555     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
556 }
557 
HWTEST_F_L0(JSNApiTests,Uint32Array)558 HWTEST_F_L0(JSNApiTests, Uint32Array)
559 {
560     LocalScope scope(vm_);
561     const int32_t length = 30;
562     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
563     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
564 
565     // 4 : offset of byte, 6 : length
566     Local<Uint32ArrayRef> typedArray = Uint32ArrayRef::New(vm_, arrayBuffer, 4, 6);
567     ASSERT_TRUE(typedArray->IsUint32Array());
568     ASSERT_EQ(typedArray->ByteLength(vm_), 24U);  // 24 : length of bytes
569     ASSERT_EQ(typedArray->ByteOffset(vm_), 4U);   // 4 : offset of byte
570     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U);  // 6 : length of array
571     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
572 }
573 
HWTEST_F_L0(JSNApiTests,Int32Array)574 HWTEST_F_L0(JSNApiTests, Int32Array)
575 {
576     LocalScope scope(vm_);
577     const int32_t length = 30;
578     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
579     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
580 
581     // 4 : offset of byte, 6 : length
582     Local<Int32ArrayRef> typedArray = Int32ArrayRef::New(vm_, arrayBuffer, 4, 6);
583     ASSERT_TRUE(typedArray->IsInt32Array());
584     ASSERT_EQ(typedArray->ByteLength(vm_), 24U);  // 24 : length of bytes
585     ASSERT_EQ(typedArray->ByteOffset(vm_), 4U);   // 4 : offset of byte
586     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U);  // 6 : length of array
587     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
588 }
589 
HWTEST_F_L0(JSNApiTests,Float32Array)590 HWTEST_F_L0(JSNApiTests, Float32Array)
591 {
592     LocalScope scope(vm_);
593     const int32_t length = 30;
594     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
595     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
596 
597     // 4 : offset of byte, 6 : length
598     Local<Float32ArrayRef> typedArray = Float32ArrayRef::New(vm_, arrayBuffer, 4, 6);
599     ASSERT_TRUE(typedArray->IsFloat32Array());
600     ASSERT_EQ(typedArray->ByteLength(vm_), 24U);  // 24 : length of bytes
601     ASSERT_EQ(typedArray->ByteOffset(vm_), 4U);   // 4 : offset of byte
602     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U);  // 6 : length of array
603     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
604 }
605 
HWTEST_F_L0(JSNApiTests,Float64Array)606 HWTEST_F_L0(JSNApiTests, Float64Array)
607 {
608     LocalScope scope(vm_);
609     const int32_t length = 57;
610     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
611     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
612 
613     // 8 : offset of byte, 6 : length
614     Local<Float64ArrayRef> typedArray = Float64ArrayRef::New(vm_, arrayBuffer, 8, 6);
615     ASSERT_TRUE(typedArray->IsFloat64Array());
616     ASSERT_EQ(typedArray->ByteLength(vm_), 48U);  // 48 : length of bytes
617     ASSERT_EQ(typedArray->ByteOffset(vm_), 8U);   // 8 : offset of byte
618     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U);  // 6 : length of array
619     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
620 }
621 
HWTEST_F_L0(JSNApiTests,BigInt64Array)622 HWTEST_F_L0(JSNApiTests, BigInt64Array)
623 {
624     LocalScope scope(vm_);
625     const int32_t length = 57;
626     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
627     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
628 
629     // 8 : offset of byte, 6 : length
630     Local<BigInt64ArrayRef> typedArray = BigInt64ArrayRef::New(vm_, arrayBuffer, 8, 6);
631     ASSERT_TRUE(typedArray->IsBigInt64Array());
632     ASSERT_EQ(typedArray->ByteLength(vm_), 48U);  // 48 : length of bytes
633     ASSERT_EQ(typedArray->ByteOffset(vm_), 8U);   // 8 : offset of byte
634     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U);  // 6 : length of array
635     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
636 }
637 
HWTEST_F_L0(JSNApiTests,BigUint64Array)638 HWTEST_F_L0(JSNApiTests, BigUint64Array)
639 {
640     LocalScope scope(vm_);
641     const int32_t length = 57;
642     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
643     ASSERT_TRUE(arrayBuffer->IsArrayBuffer());
644 
645     // 8 : offset of byte, 6 : length
646     Local<BigUint64ArrayRef> typedArray = BigUint64ArrayRef::New(vm_, arrayBuffer, 8, 6);
647     ASSERT_TRUE(typedArray->IsBigUint64Array());
648     ASSERT_EQ(typedArray->ByteLength(vm_), 48U);  // 48 : length of bytes
649     ASSERT_EQ(typedArray->ByteOffset(vm_), 8U);   // 8 : offset of byte
650     ASSERT_EQ(typedArray->ArrayLength(vm_), 6U);  // 6 : length of array
651     ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer());
652 }
653 
HWTEST_F_L0(JSNApiTests,Error)654 HWTEST_F_L0(JSNApiTests, Error)
655 {
656     LocalScope scope(vm_);
657     Local<StringRef> message = StringRef::NewFromUtf8(vm_, "ErrorTest");
658     Local<JSValueRef> error = Exception::Error(vm_, message);
659     ASSERT_TRUE(error->IsError());
660 
661     JSNApi::ThrowException(vm_, error);
662     ASSERT_TRUE(thread_->HasPendingException());
663 }
664 
HWTEST_F_L0(JSNApiTests,RangeError)665 HWTEST_F_L0(JSNApiTests, RangeError)
666 {
667     LocalScope scope(vm_);
668     Local<StringRef> message = StringRef::NewFromUtf8(vm_, "ErrorTest");
669     Local<JSValueRef> error = Exception::RangeError(vm_, message);
670     ASSERT_TRUE(error->IsError());
671 
672     JSNApi::ThrowException(vm_, error);
673     ASSERT_TRUE(thread_->HasPendingException());
674 }
675 
HWTEST_F_L0(JSNApiTests,TypeError)676 HWTEST_F_L0(JSNApiTests, TypeError)
677 {
678     LocalScope scope(vm_);
679     Local<StringRef> message = StringRef::NewFromUtf8(vm_, "ErrorTest");
680     Local<JSValueRef> error = Exception::TypeError(vm_, message);
681     ASSERT_TRUE(error->IsError());
682 
683     JSNApi::ThrowException(vm_, error);
684     ASSERT_TRUE(thread_->HasPendingException());
685 }
686 
HWTEST_F_L0(JSNApiTests,ReferenceError)687 HWTEST_F_L0(JSNApiTests, ReferenceError)
688 {
689     LocalScope scope(vm_);
690     Local<StringRef> message = StringRef::NewFromUtf8(vm_, "ErrorTest");
691     Local<JSValueRef> error = Exception::ReferenceError(vm_, message);
692     ASSERT_TRUE(error->IsError());
693 
694     JSNApi::ThrowException(vm_, error);
695     ASSERT_TRUE(thread_->HasPendingException());
696 }
697 
HWTEST_F_L0(JSNApiTests,SyntaxError)698 HWTEST_F_L0(JSNApiTests, SyntaxError)
699 {
700     LocalScope scope(vm_);
701     Local<StringRef> message = StringRef::NewFromUtf8(vm_, "ErrorTest");
702     Local<JSValueRef> error = Exception::SyntaxError(vm_, message);
703     ASSERT_TRUE(error->IsError());
704 
705     JSNApi::ThrowException(vm_, error);
706     ASSERT_TRUE(thread_->HasPendingException());
707 }
708 
HWTEST_F_L0(JSNApiTests,OOMError)709 HWTEST_F_L0(JSNApiTests, OOMError)
710 {
711     LocalScope scope(vm_);
712     Local<StringRef> message = StringRef::NewFromUtf8(vm_, "ErrorTest");
713     Local<JSValueRef> error = Exception::OOMError(vm_, message);
714     ASSERT_TRUE(error->IsError());
715 
716     JSNApi::ThrowException(vm_, error);
717     ASSERT_TRUE(thread_->HasPendingException());
718 }
719 
HWTEST_F_L0(JSNApiTests,InheritPrototype_001)720 HWTEST_F_L0(JSNApiTests, InheritPrototype_001)
721 {
722     LocalScope scope(vm_);
723     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
724     // new with Builtins::Set Prototype
725     JSHandle<JSTaggedValue> set = env->GetBuiltinsSetFunction();
726     Local<FunctionRef> setLocal = JSNApiHelper::ToLocal<FunctionRef>(set);
727     // new with Builtins::Map Prototype
728     JSHandle<JSTaggedValue> map = env->GetBuiltinsMapFunction();
729     Local<FunctionRef> mapLocal = JSNApiHelper::ToLocal<FunctionRef>(map);
730     JSHandle<JSTaggedValue> setPrototype(thread_, JSHandle<JSFunction>::Cast(set)->GetFunctionPrototype());
731     JSHandle<JSTaggedValue> mapPrototype(thread_, JSHandle<JSFunction>::Cast(map)->GetFunctionPrototype());
732     JSHandle<JSTaggedValue> mapPrototypeProto(thread_, JSTaggedValue::GetPrototype(thread_, mapPrototype));
733     bool same = JSTaggedValue::SameValue(setPrototype, mapPrototypeProto);
734     // before inherit, map.Prototype.__proto__ should be different from set.Prototype
735     ASSERT_FALSE(same);
736     // before inherit, map.__proto__ should be different from set
737     JSHandle<JSTaggedValue> mapProto(thread_, JSTaggedValue::GetPrototype(thread_, map));
738     bool same1 = JSTaggedValue::SameValue(set, mapProto);
739     ASSERT_FALSE(same1);
740 
741     // Set property to Set Function
742     auto factory = vm_->GetFactory();
743     JSHandle<JSTaggedValue> defaultString = thread_->GlobalConstants()->GetHandledDefaultString();
744     PropertyDescriptor desc = PropertyDescriptor(thread_, defaultString);
745     bool success = JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>::Cast(set), defaultString, desc);
746     ASSERT_TRUE(success);
747     JSHandle<JSTaggedValue> property1String(thread_, factory->NewFromASCII("property1").GetTaggedValue());
748     JSHandle<JSTaggedValue> func = env->GetTypedArrayFunction();
749     PropertyDescriptor desc1 = PropertyDescriptor(thread_, func);
750     bool success1 = JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>::Cast(set), property1String, desc1);
751     ASSERT_TRUE(success1);
752 
753     mapLocal->Inherit(vm_, setLocal);
754     JSHandle<JSTaggedValue> sonHandle = JSNApiHelper::ToJSHandle(mapLocal);
755     JSHandle<JSTaggedValue> sonPrototype(thread_, JSHandle<JSFunction>::Cast(sonHandle)->GetFunctionPrototype());
756     JSHandle<JSTaggedValue> sonPrototypeProto(thread_, JSTaggedValue::GetPrototype(thread_, sonPrototype));
757     bool same2 = JSTaggedValue::SameValue(setPrototype, sonPrototypeProto);
758     ASSERT_TRUE(same2);
759     JSHandle<JSTaggedValue> sonProto(thread_, JSTaggedValue::GetPrototype(thread_, map));
760     bool same3 = JSTaggedValue::SameValue(set, sonProto);
761     ASSERT_TRUE(same3);
762 
763     // son = new Son(), Son() inherit from Parent(), Test whether son.InstanceOf(Parent) is true
764     JSHandle<JSObject> sonObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(sonHandle), sonHandle);
765     bool isInstance = JSObject::InstanceOf(thread_, JSHandle<JSTaggedValue>::Cast(sonObj), set);
766     ASSERT_TRUE(isInstance);
767 
768     // Test whether son Function can access to property of parent Function
769     OperationResult res = JSObject::GetProperty(thread_, JSHandle<JSObject>::Cast(sonHandle), defaultString);
770     bool same4 = JSTaggedValue::SameValue(defaultString, res.GetValue());
771     ASSERT_TRUE(same4);
772     OperationResult res1 = JSObject::GetProperty(thread_, JSHandle<JSObject>::Cast(sonHandle), property1String);
773     bool same5 = JSTaggedValue::SameValue(func, res1.GetValue());
774     ASSERT_TRUE(same5);
775 
776     // new with empty Function Constructor
777     Local<FunctionRef> son1 = FunctionRef::New(vm_, FunctionCallback, nullptr);
778     son1->Inherit(vm_, mapLocal);
779     JSHandle<JSFunction> son1Handle = JSHandle<JSFunction>::Cast(JSNApiHelper::ToJSHandle(son1));
780     ASSERT_TRUE(son1Handle->HasFunctionPrototype());
781 }
782 
HWTEST_F_L0(JSNApiTests,InheritPrototype_002)783 HWTEST_F_L0(JSNApiTests, InheritPrototype_002)
784 {
785     LocalScope scope(vm_);
786     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
787     // new with Builtins::weakSet Prototype
788     JSHandle<JSTaggedValue> weakSet = env->GetBuiltinsWeakSetFunction();
789     Local<FunctionRef> weakSetLocal = JSNApiHelper::ToLocal<FunctionRef>(weakSet);
790     // new with Builtins::weakMap Prototype
791     JSHandle<JSTaggedValue> weakMap = env->GetBuiltinsWeakMapFunction();
792     Local<FunctionRef> weakMapLocal = JSNApiHelper::ToLocal<FunctionRef>(weakMap);
793 
794     weakMapLocal->Inherit(vm_, weakSetLocal);
795 
796     auto factory = vm_->GetFactory();
797     JSHandle<JSTaggedValue> property1String(thread_, factory->NewFromASCII("property1").GetTaggedValue());
798     JSHandle<JSTaggedValue> func = env->GetArrayFunction();
799     PropertyDescriptor desc1 = PropertyDescriptor(thread_, func);
800     bool success1 = JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>::Cast(weakMap), property1String, desc1);
801     ASSERT_TRUE(success1);
802 
803     JSHandle<JSTaggedValue> sonHandle = JSNApiHelper::ToJSHandle(weakMapLocal);
804     JSHandle<JSObject> sonObj =
805         factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(sonHandle), sonHandle);
806 
807     JSHandle<JSTaggedValue> fatherHandle = JSNApiHelper::ToJSHandle(weakSetLocal);
808     JSHandle<JSObject> fatherObj =
809         factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(fatherHandle), fatherHandle);
810 
811     JSHandle<JSTaggedValue> sonMethod =
812         JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>(sonObj), property1String);
813     JSHandle<JSTaggedValue> fatherMethod =
814         JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>(fatherObj), property1String);
815     bool same = JSTaggedValue::SameValue(sonMethod, fatherMethod);
816     ASSERT_TRUE(same);
817 }
818 
HWTEST_F_L0(JSNApiTests,InheritPrototype_003)819 HWTEST_F_L0(JSNApiTests, InheritPrototype_003)
820 {
821     LocalScope scope(vm_);
822     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
823     auto factory = vm_->GetFactory();
824 
825     JSHandle<Method> invokeSelf =
826         factory->NewMethodForNativeFunction(reinterpret_cast<void *>(BuiltinsFunction::FunctionPrototypeInvokeSelf));
827     // father type
828     JSHandle<JSHClass> protoClass = JSHandle<JSHClass>::Cast(env->GetFunctionClassWithProto());
829     JSHandle<JSFunction> protoFunc = factory->NewJSFunctionByHClass(invokeSelf, protoClass);
830     Local<FunctionRef> protoLocal = JSNApiHelper::ToLocal<FunctionRef>(JSHandle<JSTaggedValue>(protoFunc));
831     // son type
832     JSHandle<JSHClass> noProtoClass = JSHandle<JSHClass>::Cast(env->GetFunctionClassWithoutProto());
833     JSHandle<JSFunction> noProtoFunc = factory->NewJSFunctionByHClass(invokeSelf, noProtoClass);
834     Local<FunctionRef> noProtoLocal = JSNApiHelper::ToLocal<FunctionRef>(JSHandle<JSTaggedValue>(noProtoFunc));
835 
836     JSHandle<JSFunction> sonHandle = JSHandle<JSFunction>::Cast(JSNApiHelper::ToJSHandle(noProtoLocal));
837     EXPECT_FALSE(sonHandle->HasFunctionPrototype());
838 
839     JSHandle<JSTaggedValue> defaultString = thread_->GlobalConstants()->GetHandledDefaultString();
840     PropertyDescriptor desc = PropertyDescriptor(thread_, defaultString);
841     JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>::Cast(protoFunc), defaultString, desc);
842 
843     noProtoLocal->Inherit(vm_, protoLocal);
844     JSHandle<JSFunction> son1Handle = JSHandle<JSFunction>::Cast(JSNApiHelper::ToJSHandle(noProtoLocal));
845     EXPECT_TRUE(son1Handle->HasFunctionPrototype());
846 
847     OperationResult res = JSObject::GetProperty(thread_, JSHandle<JSObject>::Cast(son1Handle), defaultString);
848     EXPECT_EQ(JSTaggedValue::SameValue(defaultString, res.GetValue()), true);
849 
850     JSHandle<JSTaggedValue> propertyString(thread_, factory->NewFromASCII("property").GetTaggedValue());
851     JSHandle<JSTaggedValue> func = env->GetArrayFunction();
852     PropertyDescriptor desc1 = PropertyDescriptor(thread_, func);
853     JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>::Cast(protoFunc), propertyString, desc1);
854     OperationResult res1 = JSObject::GetProperty(thread_, JSHandle<JSObject>::Cast(son1Handle), propertyString);
855     EXPECT_EQ(JSTaggedValue::SameValue(func, res1.GetValue()), true);
856 }
857 
HWTEST_F_L0(JSNApiTests,InheritPrototype_004)858 HWTEST_F_L0(JSNApiTests, InheritPrototype_004)
859 {
860     LocalScope scope(vm_);
861     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
862     auto factory = vm_->GetFactory();
863 
864     JSHandle<JSTaggedValue> weakSet = env->GetBuiltinsWeakSetFunction();
865     JSHandle<JSTaggedValue> deleteString(factory->NewFromASCII("delete"));
866     JSHandle<JSTaggedValue> addString(factory->NewFromASCII("add"));
867     JSHandle<JSTaggedValue> defaultString = thread_->GlobalConstants()->GetHandledDefaultString();
868     JSHandle<JSTaggedValue> deleteMethod = JSObject::GetMethod(thread_, weakSet, deleteString);
869     JSHandle<JSTaggedValue> addMethod = JSObject::GetMethod(thread_, weakSet, addString);
870 
871     JSHandle<Method> invokeSelf =
872         factory->NewMethodForNativeFunction(reinterpret_cast<void *>(BuiltinsFunction::FunctionPrototypeInvokeSelf));
873     JSHandle<Method> ctor =
874         factory->NewMethodForNativeFunction(reinterpret_cast<void *>(BuiltinsFunction::FunctionConstructor));
875 
876     JSHandle<JSHClass> protoClass = JSHandle<JSHClass>::Cast(env->GetFunctionClassWithProto());
877     JSHandle<JSFunction> funcFuncPrototype = factory->NewJSFunctionByHClass(invokeSelf, protoClass);
878     // add method in funcPrototype
879     PropertyDescriptor desc = PropertyDescriptor(thread_, deleteMethod);
880     JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>::Cast(funcFuncPrototype), deleteString, desc);
881     JSHandle<JSTaggedValue> funcFuncPrototypeValue(funcFuncPrototype);
882 
883     JSHandle<JSHClass> funcFuncProtoIntanceClass =
884        factory->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, funcFuncPrototypeValue);
885     // new with NewJSFunctionByHClass::function Class
886     JSHandle<JSFunction> protoFunc =
887        factory->NewJSFunctionByHClass(ctor, funcFuncProtoIntanceClass);
888     EXPECT_TRUE(*protoFunc != nullptr);
889     // add method in funcnction
890     PropertyDescriptor desc1 = PropertyDescriptor(thread_, addMethod);
891     JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>::Cast(protoFunc), addString, desc1);
892     JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>::Cast(protoFunc), deleteString, desc);
893     // father type
894     Local<FunctionRef> protoLocal = JSNApiHelper::ToLocal<FunctionRef>(JSHandle<JSTaggedValue>(protoFunc));
895 
896     JSHandle<JSHClass> noProtoClass = JSHandle<JSHClass>::Cast(env->GetFunctionClassWithoutProto());
897     JSHandle<JSFunction> funcFuncNoProtoPrototype = factory->NewJSFunctionByHClass(invokeSelf, noProtoClass);
898     JSHandle<JSTaggedValue> funcFuncNoProtoPrototypeValue(funcFuncNoProtoPrototype);
899 
900     JSHandle<JSHClass> funcFuncNoProtoProtoIntanceClass =
901        factory->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, funcFuncNoProtoPrototypeValue);
902     // new with NewJSFunctionByHClass::function Class
903     JSHandle<JSFunction> noProtoFunc = factory->NewJSFunctionByHClass(ctor,
904         funcFuncNoProtoProtoIntanceClass);
905     EXPECT_TRUE(*noProtoFunc != nullptr);
906     // set property that has same key with fater type
907     PropertyDescriptor desc2 = PropertyDescriptor(thread_, defaultString);
908     JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>::Cast(noProtoFunc), addString, desc2);
909     // son type
910     Local<FunctionRef> noProtoLocal = JSNApiHelper::ToLocal<FunctionRef>(JSHandle<JSTaggedValue>(noProtoFunc));
911 
912     noProtoLocal->Inherit(vm_, protoLocal);
913 
914     JSHandle<JSFunction> sonHandle = JSHandle<JSFunction>::Cast(JSNApiHelper::ToJSHandle(noProtoLocal));
915     OperationResult res = JSObject::GetProperty(thread_, JSHandle<JSObject>::Cast(sonHandle), deleteString);
916     EXPECT_EQ(JSTaggedValue::SameValue(deleteMethod, res.GetValue()), true);
917     // test if the property value changed after inherit
918     OperationResult res1 = JSObject::GetProperty(thread_, JSHandle<JSObject>::Cast(sonHandle), addString);
919     EXPECT_EQ(JSTaggedValue::SameValue(defaultString, res1.GetValue()), true);
920 }
921 
HWTEST_F_L0(JSNApiTests,ClassFunction)922 HWTEST_F_L0(JSNApiTests, ClassFunction)
923 {
924     LocalScope scope(vm_);
925     Local<FunctionRef> cls = FunctionRef::NewClassFunction(vm_, nullptr, nullptr, nullptr);
926 
927     JSHandle<JSTaggedValue> clsObj = JSNApiHelper::ToJSHandle(Local<JSValueRef>(cls));
928     ASSERT_TRUE(clsObj->IsClassConstructor());
929 
930     JSTaggedValue accessor = JSHandle<JSFunction>(clsObj)->GetPropertyInlinedProps(
931                                                            JSFunction::CLASS_PROTOTYPE_INLINE_PROPERTY_INDEX);
932     ASSERT_TRUE(accessor.IsInternalAccessor());
933 }
934 
HWTEST_F_L0(JSNApiTests,WeakRefSecondPassCallback)935 HWTEST_F_L0(JSNApiTests, WeakRefSecondPassCallback)
936 {
937     LocalScope scope(vm_);
938     Local<ObjectRef> object1 = ObjectRef::New(vm_);
939     Global<ObjectRef> globalObject1(vm_, object1);
940     globalObject1.SetWeak();
941     NativeReferenceHelper *temp = nullptr;
942     {
943         LocalScope scope1(vm_);
944         Local<ObjectRef> object2 = ObjectRef::New(vm_);
945         Global<ObjectRef> globalObject2(vm_, object2);
946         NativeReferenceHelper *ref1 = new NativeReferenceHelper(vm_, globalObject2, WeakRefCallback);
947         ref1->SetWeakCallback();
948         temp = ref1;
949     }
950     {
951         LocalScope scope1(vm_);
952         Local<ObjectRef> object3 = ObjectRef::New(vm_);
953         Global<ObjectRef> globalObject3(vm_, object3);
954         globalObject3.SetWeak();
955     }
956     Local<ObjectRef> object4 = ObjectRef::New(vm_);
957     Global<ObjectRef> globalObject4(vm_, object4);
958     NativeReferenceHelper *ref2 = new NativeReferenceHelper(vm_, globalObject4, WeakRefCallback);
959     ref2->SetWeakCallback();
960     vm_->CollectGarbage(TriggerGCType::OLD_GC);
961     delete temp;
962     delete ref2;
963 }
964 
HWTEST_F_L0(JSNApiTests,TriggerGC_OLD_GC)965 HWTEST_F_L0(JSNApiTests, TriggerGC_OLD_GC)
966 {
967     vm_->SetEnableForceGC(false);
968     auto globalEnv = vm_->GetGlobalEnv();
969     auto factory = vm_->GetFactory();
970     JSHandle<JSTaggedValue> jsFunc = globalEnv->GetArrayFunction();
971     JSHandle<JSObject> objVal1 =
972         factory->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc), jsFunc);
973     JSHandle<JSObject> objVal2 =
974         factory->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc), jsFunc);
975     JSObject *newObj2 = *objVal2;
976     JSTaggedValue canBeGcValue(newObj2);
977 
978     uint32_t arrayLength = 2;
979     JSHandle<TaggedArray> taggedArray = factory->NewTaggedArray(arrayLength);
980     taggedArray->Set(thread_, 0, objVal1);
981     taggedArray->Set(thread_, 1, canBeGcValue);
982     EXPECT_EQ(taggedArray->GetIdx(objVal1.GetTaggedValue()), 0U);
983     EXPECT_EQ(taggedArray->GetIdx(canBeGcValue), 1U);
984 
985     // trigger gc
986     JSNApi::TRIGGER_GC_TYPE gcType = JSNApi::TRIGGER_GC_TYPE::OLD_GC;
987     JSNApi::TriggerGC(vm_, gcType);
988     EXPECT_EQ(taggedArray->GetIdx(objVal1.GetTaggedValue()), 0U);
989     EXPECT_EQ(taggedArray->GetIdx(canBeGcValue), TaggedArray::MAX_ARRAY_INDEX);
990 
991     vm_->SetEnableForceGC(true);
992 }
993 
HWTEST_F_L0(JSNApiTests,ExecuteModuleBuffer)994 HWTEST_F_L0(JSNApiTests, ExecuteModuleBuffer)
995 {
996     const char *fileName = "__JSNApiTests_ExecuteModuleBuffer.abc";
997     const char *data = R"(
998         .language ECMAScript
999         .function any func_main_0(any a0, any a1, any a2) {
1000             ldai 1
1001             return
1002         }
1003     )";
1004     bool executeResult =
1005         JSNApi::ExecuteModuleBuffer(vm_, reinterpret_cast<const uint8_t *>(data), sizeof(data), fileName);
1006     EXPECT_FALSE(executeResult);
1007 }
1008 
HWTEST_F_L0(JSNApiTests,addWorker_DeleteWorker)1009 HWTEST_F_L0(JSNApiTests, addWorker_DeleteWorker)
1010 {
1011     JSRuntimeOptions option;
1012     EcmaVM *workerVm = JSNApi::CreateEcmaVM(option);
1013     JSNApi::addWorker(vm_, workerVm);
1014     bool hasDeleted = JSNApi::DeleteWorker(vm_, workerVm);
1015     EXPECT_TRUE(hasDeleted);
1016 
1017     hasDeleted = JSNApi::DeleteWorker(vm_, nullptr);
1018     EXPECT_FALSE(hasDeleted);
1019 }
1020 
HWTEST_F_L0(JSNApiTests,PrimitiveRef_GetValue)1021 HWTEST_F_L0(JSNApiTests, PrimitiveRef_GetValue)
1022 {
1023     auto factory = vm_->GetFactory();
1024     Local<IntegerRef> intValue = IntegerRef::New(vm_, 0);
1025     EXPECT_EQ(intValue->Value(), 0);
1026 
1027     Local<JSValueRef> jsValue = intValue->GetValue(vm_);
1028     EXPECT_TRUE(*jsValue == nullptr);
1029 
1030     JSHandle<JSTaggedValue> nullHandle(thread_, JSTaggedValue::Null());
1031     JSHandle<JSHClass> jsClassHandle =
1032         factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_PRIMITIVE_REF, nullHandle);
1033     TaggedObject *taggedObject = factory->NewObject(jsClassHandle);
1034     JSHandle<JSTaggedValue> jsTaggedValue(thread_, JSTaggedValue(taggedObject));
1035     Local<PrimitiveRef> jsValueRef = JSNApiHelper::ToLocal<JSPrimitiveRef>(jsTaggedValue);
1036     EXPECT_TRUE(*(jsValueRef->GetValue(vm_)) != nullptr);
1037 }
1038 
HWTEST_F_L0(JSNApiTests,BigIntRef_New_Uint64)1039 HWTEST_F_L0(JSNApiTests, BigIntRef_New_Uint64)
1040 {
1041     uint64_t maxUint64 = std::numeric_limits<uint64_t>::max();
1042     Local<BigIntRef> maxBigintUint64 = BigIntRef::New(vm_, maxUint64);
1043     EXPECT_TRUE(maxBigintUint64->IsBigInt());
1044 
1045     JSHandle<BigInt> maxBigintUint64Val(thread_, JSNApiHelper::ToJSTaggedValue(*maxBigintUint64));
1046     EXPECT_EQ(maxBigintUint64Val->GetDigit(0), static_cast<uint32_t>(maxUint64 & 0xffffffff));
1047     EXPECT_EQ(maxBigintUint64Val->GetDigit(1), static_cast<uint32_t>((maxUint64 >> BigInt::DATEBITS) & 0xffffffff));
1048 
1049     uint64_t minUint64 = std::numeric_limits<uint64_t>::min();
1050     Local<BigIntRef> minBigintUint64 = BigIntRef::New(vm_, minUint64);
1051     EXPECT_TRUE(minBigintUint64->IsBigInt());
1052 
1053     JSHandle<BigInt> minBigintUint64Val(thread_, JSNApiHelper::ToJSTaggedValue(*minBigintUint64));
1054     EXPECT_EQ(minBigintUint64Val->GetDigit(0), static_cast<uint32_t>(minUint64 & 0xffffffff));
1055     EXPECT_EQ(minBigintUint64Val->GetLength(), 1U);
1056 }
1057 
HWTEST_F_L0(JSNApiTests,BigIntRef_New_Int64)1058 HWTEST_F_L0(JSNApiTests, BigIntRef_New_Int64)
1059 {
1060     int64_t maxInt64 = std::numeric_limits<int64_t>::max();
1061     Local<BigIntRef> maxBigintInt64 = BigIntRef::New(vm_, maxInt64);
1062     EXPECT_TRUE(maxBigintInt64->IsBigInt());
1063 
1064     JSHandle<BigInt> maxBigintInt64Val(thread_, JSNApiHelper::ToJSTaggedValue(*maxBigintInt64));
1065     EXPECT_EQ(maxBigintInt64Val->GetDigit(0), static_cast<uint32_t>(maxInt64 & 0xffffffff));
1066     EXPECT_EQ(maxBigintInt64Val->GetDigit(1), static_cast<uint32_t>((maxInt64 >> BigInt::DATEBITS) & 0xffffffff));
1067 
1068     int64_t minInt64 = std::numeric_limits<int64_t>::min();
1069     Local<BigIntRef> minBigintInt64 = BigIntRef::New(vm_, minInt64);
1070     EXPECT_TRUE(minBigintInt64->IsBigInt());
1071 
1072     JSHandle<BigInt> minBigintInt64Val(thread_, JSNApiHelper::ToJSTaggedValue(*minBigintInt64));
1073     EXPECT_EQ(minBigintInt64Val->GetSign(), true);
1074     EXPECT_EQ(minBigintInt64Val->GetDigit(0), static_cast<uint32_t>((-minInt64) & 0xffffffff));
1075     EXPECT_EQ(minBigintInt64Val->GetDigit(1), static_cast<uint32_t>(((-minInt64) >> BigInt::DATEBITS) & 0xffffffff));
1076 }
1077 
HWTEST_F_L0(JSNApiTests,BigIntRef_CreateBigWords_GetWordsArray_GetWordsArraySize)1078 HWTEST_F_L0(JSNApiTests, BigIntRef_CreateBigWords_GetWordsArray_GetWordsArraySize)
1079 {
1080     bool sign = false;
1081     uint32_t size = 3;
1082     const uint32_t MULTIPLE = 2;
1083     const uint64_t words[3] = {
1084         std::numeric_limits<uint64_t>::min() - 1,
1085         std::numeric_limits<uint64_t>::min(),
1086         std::numeric_limits<uint64_t>::max(),
1087     };
1088     Local<JSValueRef> bigWords = BigIntRef::CreateBigWords(vm_, sign, size, words);
1089     EXPECT_TRUE(bigWords->IsBigInt());
1090 
1091     Local<BigIntRef> bigWordsRef(bigWords);
1092     EXPECT_EQ(bigWordsRef->GetWordsArraySize(), size);
1093 
1094     JSHandle<BigInt> bigintUint64Val(thread_, JSNApiHelper::ToJSTaggedValue(*bigWords));
1095     EXPECT_EQ(bigintUint64Val->GetSign(), false);
1096     EXPECT_EQ(bigintUint64Val->GetLength(), size * MULTIPLE);
1097 
1098     bool resultSignBit = true;
1099     uint64_t *resultWords = new uint64_t[3](); // 3 : length of words array
1100     bigWordsRef->GetWordsArray(&resultSignBit, size, resultWords);
1101     EXPECT_EQ(resultSignBit, false);
1102     EXPECT_EQ(resultWords[0], words[0]);
1103     EXPECT_EQ(resultWords[1], words[1]);
1104     EXPECT_EQ(resultWords[2], words[2]);
1105     delete[] resultWords;
1106 }
1107 
HWTEST_F_L0(JSNApiTests,BigIntToInt64)1108 HWTEST_F_L0(JSNApiTests, BigIntToInt64)
1109 {
1110     LocalScope scope(vm_);
1111     uint64_t maxUint64 = std::numeric_limits<uint64_t>::max();
1112     Local<BigIntRef> maxBigintUint64 = BigIntRef::New(vm_, maxUint64);
1113     EXPECT_TRUE(maxBigintUint64->IsBigInt());
1114     int64_t num = -11;
1115     int64_t num1 = num;
1116     bool lossless = true;
1117     maxBigintUint64->BigIntToInt64(vm_, &num, &lossless);
1118     EXPECT_TRUE(num != num1);
1119 }
1120 
HWTEST_F_L0(JSNApiTests,BigIntToUint64)1121 HWTEST_F_L0(JSNApiTests, BigIntToUint64)
1122 {
1123     LocalScope scope(vm_);
1124     uint64_t maxUint64 = std::numeric_limits<uint64_t>::max();
1125     Local<BigIntRef> maxBigintUint64 = BigIntRef::New(vm_, maxUint64);
1126     EXPECT_TRUE(maxBigintUint64->IsBigInt());
1127     uint64_t  num = -11;
1128     uint64_t  num1 = num;
1129     bool lossless = true;
1130     maxBigintUint64->BigIntToUint64(vm_, &num, &lossless);
1131     EXPECT_TRUE(num != num1);
1132 }
1133 
HWTEST_F_L0(JSNApiTests,BooleanRef_New)1134 HWTEST_F_L0(JSNApiTests, BooleanRef_New)
1135 {
1136     LocalScope scope(vm_);
1137     bool input = true;
1138     Local<BooleanRef> res = BooleanRef::New(vm_, input);
1139     EXPECT_TRUE(res->IsBoolean());
1140     EXPECT_TRUE(res->BooleaValue());
1141 }
1142 
HWTEST_F_L0(JSNApiTests,NewFromUnsigned)1143 HWTEST_F_L0(JSNApiTests, NewFromUnsigned)
1144 {
1145     LocalScope scope(vm_);
1146     unsigned int input = 1;
1147     [[maybe_unused]] Local<IntegerRef> res = IntegerRef::NewFromUnsigned(vm_, input);
1148     EXPECT_TRUE(res->IntegerValue(vm_) == 1);
1149     EXPECT_TRUE(res->Value() == 1);
1150 }
1151 
HWTEST_F_L0(JSNApiTests,SetBundleName_GetBundleName)1152 HWTEST_F_L0(JSNApiTests, SetBundleName_GetBundleName)
1153 {
1154     LocalScope scope(vm_);
1155     std::string str = "11";
1156     JSNApi::SetBundleName(vm_, str);
1157     std::string res = JSNApi::GetBundleName(vm_);
1158     ASSERT_EQ(str, res);
1159 }
1160 
HWTEST_F_L0(JSNApiTests,SetModuleName_GetModuleName)1161 HWTEST_F_L0(JSNApiTests, SetModuleName_GetModuleName)
1162 {
1163     LocalScope scope(vm_);
1164     std::string str = "11";
1165     JSNApi::SetModuleName(vm_, str);
1166     std::string res = JSNApi::GetModuleName(vm_);
1167     ASSERT_EQ(str, res);
1168 }
1169 
HWTEST_F_L0(JSNApiTests,IsBundle)1170 HWTEST_F_L0(JSNApiTests, IsBundle)
1171 {
1172     LocalScope scope(vm_);
1173     bool res = JSNApi::IsBundle(vm_);
1174     ASSERT_EQ(res, true);
1175 }
1176 
HWTEST_F_L0(JSNApiTests,MapRef_GetSize_GetTotalElements_Get_GetKey_GetValue_New_Set)1177 HWTEST_F_L0(JSNApiTests, MapRef_GetSize_GetTotalElements_Get_GetKey_GetValue_New_Set)
1178 {
1179     LocalScope scope(vm_);
1180     Local<MapRef> map = MapRef::New(vm_);
1181     Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, "TestKey");
1182     Local<JSValueRef> value = StringRef::NewFromUtf8(vm_, "TestValue");
1183     map->Set(vm_, key, value);
1184     Local<JSValueRef> res = map->Get(vm_, key);
1185     ASSERT_EQ(res->ToString(vm_)->ToString(), value->ToString(vm_)->ToString());
1186     int32_t num = map->GetSize();
1187     int32_t num1 = map->GetTotalElements();
1188     ASSERT_EQ(num, 1);
1189     ASSERT_EQ(num1, 1);
1190     Local<JSValueRef> res1 = map->GetKey(vm_, 0);
1191     ASSERT_EQ(res1->ToString(vm_)->ToString(), key->ToString(vm_)->ToString());
1192     Local<JSValueRef> res2 = map->GetValue(vm_, 0);
1193     ASSERT_EQ(res2->ToString(vm_)->ToString(), value->ToString(vm_)->ToString());
1194 }
1195 
HWTEST_F_L0(JSNApiTests,GetSourceCode)1196 HWTEST_F_L0(JSNApiTests, GetSourceCode)
1197 {
1198     LocalScope scope(vm_);
1199     Local<FunctionRef> callback = FunctionRef::New(vm_, FunctionCallback);
1200     bool res = callback->IsNative(vm_);
1201     EXPECT_TRUE(res);
1202 }
1203 
HWTEST_F_L0(JSNApiTests,ObjectRef_Delete)1204 HWTEST_F_L0(JSNApiTests, ObjectRef_Delete)
1205 {
1206     LocalScope scope(vm_);
1207     Local<ObjectRef> object = ObjectRef::New(vm_);
1208     Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, "TestKey");
1209     Local<JSValueRef> value = ObjectRef::New(vm_);
1210     PropertyAttribute attribute(value, true, true, true);
1211     ASSERT_TRUE(object->DefineProperty(vm_, key, attribute));
1212     ASSERT_TRUE(object->Delete(vm_, key));
1213     ASSERT_FALSE(object->Has(vm_, key));
1214 }
1215 
HWTEST_F_L0(JSNApiTests,ObjectRef_Set1)1216 HWTEST_F_L0(JSNApiTests, ObjectRef_Set1)
1217 {
1218     LocalScope scope(vm_);
1219     Local<ObjectRef> object = ObjectRef::New(vm_);
1220     Local<StringRef> toString = StringRef::NewFromUtf8(vm_, "-123.3");
1221     Local<JSValueRef> toValue(toString);
1222     bool res = object->Set(vm_, 12, toValue);
1223     ASSERT_TRUE(res);
1224     Local<JSValueRef> res1 = object->Get(vm_, 12);
1225     ASSERT_EQ(res1->ToString(vm_)->ToString(), toValue->ToString(vm_)->ToString());
1226 }
1227 
HWTEST_F_L0(JSNApiTests,NativePointerRef_New)1228 HWTEST_F_L0(JSNApiTests, NativePointerRef_New)
1229 {
1230     LocalScope scope(vm_);
1231     NativePointerCallback callBack = nullptr;
1232     void *vp1 = static_cast<void*>(new std::string("test"));
1233     void *vp2 = static_cast<void*>(new std::string("test"));
1234     Local<NativePointerRef> res =  NativePointerRef::New(vm_, vp1, callBack, vp2, 0);
1235     ASSERT_EQ(res->Value(), vp1);
1236 }
1237 
HWTEST_F_L0(JSNApiTests,ObjectRef_Has_Delete)1238 HWTEST_F_L0(JSNApiTests, ObjectRef_Has_Delete)
1239 {
1240     LocalScope scope(vm_);
1241     Local<ObjectRef> object = ObjectRef::New(vm_);
1242     uint32_t num = 10;
1243     Local<StringRef> toString = StringRef::NewFromUtf8(vm_, "-123.3");
1244     Local<JSValueRef> toValue(toString);
1245     bool res = object->Set(vm_, num, toValue);
1246     ASSERT_TRUE(res);
1247     bool res1 = object->Has(vm_, num);
1248     ASSERT_TRUE(res1);
1249     bool res2 = object->Delete(vm_, num);
1250     ASSERT_TRUE(res2);
1251     bool res3 = object->Has(vm_, num);
1252     ASSERT_FALSE(res3);
1253 }
1254 
HWTEST_F_L0(JSNApiTests,PromiseRejectInfo_GetData)1255 HWTEST_F_L0(JSNApiTests, PromiseRejectInfo_GetData)
1256 {
1257     LocalScope scope(vm_);
1258     Local<StringRef> toString = StringRef::NewFromUtf8(vm_, "-123.3");
1259     Local<JSValueRef> promise(toString);
1260     Local<StringRef> toString1 = StringRef::NewFromUtf8(vm_, "123.3");
1261     Local<JSValueRef> reason(toString1);
1262     void *data = static_cast<void*>(new std::string("test"));
1263     PromiseRejectInfo  promisereject(promise, reason, PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT, data);
1264     Local<JSValueRef> promise_res = promisereject.GetPromise();
1265     Local<JSValueRef> reason_res = promisereject.GetReason();
1266     ASSERT_EQ(promise_res->ToString(vm_)->ToString(), promise->ToString(vm_)->ToString());
1267     ASSERT_EQ(reason_res->ToString(vm_)->ToString(), reason->ToString(vm_)->ToString());
1268     void* dataRes = promisereject.GetData();
1269     ASSERT_EQ(dataRes, data);
1270 }
1271 }  // namespace panda::test
1272