• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 <cstddef>
17 #include "ecmascript/builtins/builtins.h"
18 #include "ecmascript/builtins/builtins_function.h"
19 #include "ecmascript/builtins/builtins_object.h"
20 #include "ecmascript/checkpoint/thread_state_transition.h"
21 #include "ecmascript/compiler/aot_file/an_file_data_manager.h"
22 #include "ecmascript/compiler/aot_file/aot_file_manager.h"
23 #include "ecmascript/compiler/circuit_builder_helper.h"
24 #include "ecmascript/deoptimizer/deoptimizer.h"
25 #include "ecmascript/ecma_global_storage.h"
26 #include "ecmascript/ecma_vm.h"
27 #include "ecmascript/global_env.h"
28 #include "ecmascript/js_api/js_api_tree_map.h"
29 #include "ecmascript/js_api/js_api_tree_set.h"
30 #include "ecmascript/js_api/js_api_vector.h"
31 #include "ecmascript/js_array.h"
32 #include "ecmascript/js_bigint.h"
33 #include "ecmascript/js_date_time_format.h"
34 #include "ecmascript/js_generator_object.h"
35 #include "ecmascript/js_map.h"
36 #include "ecmascript/js_map_iterator.h"
37 #include "ecmascript/js_object-inl.h"
38 #include "ecmascript/js_primitive_ref.h"
39 #include "ecmascript/js_regexp.h"
40 #include "ecmascript/js_runtime_options.h"
41 #include "ecmascript/js_set.h"
42 #include "ecmascript/js_set_iterator.h"
43 #include "ecmascript/js_tagged_value.h"
44 #include "ecmascript/js_typed_array.h"
45 #include "ecmascript/js_thread.h"
46 #include "ecmascript/js_weak_container.h"
47 #include "ecmascript/linked_hash_table.h"
48 #include "ecmascript/mem/mem_map_allocator.h"
49 #include "ecmascript/module/js_module_manager.h"
50 #include "ecmascript/module/js_module_source_text.h"
51 #include "ecmascript/napi/include/jsnapi.h"
52 #include "ecmascript/napi/include/jsnapi_internals.h"
53 #include "ecmascript/napi/jsnapi_helper.h"
54 #include "ecmascript/object_factory.h"
55 #include "ecmascript/pgo_profiler/pgo_profiler.h"
56 #include "ecmascript/pgo_profiler/pgo_profiler_decoder.h"
57 #include "ecmascript/pgo_profiler/pgo_profiler_encoder.h"
58 #include "ecmascript/pgo_profiler/pgo_profiler_manager.h"
59 #include "ecmascript/tagged_array.h"
60 #include "ecmascript/tests/test_helper.h"
61 #include "ecmascript/tagged_tree.h"
62 #include "ecmascript/weak_vector.h"
63 #include "gtest/gtest.h"
64 
65 using namespace panda;
66 using namespace panda::ecmascript;
67 using namespace panda::ecmascript::kungfu;
68 
69 namespace panda::test {
70 using BuiltinsFunction = ecmascript::builtins::BuiltinsFunction;
71 using PGOProfilerManager = panda::ecmascript::pgo::PGOProfilerManager;
72 using FunctionForRef = Local<JSValueRef> (*)(JsiRuntimeCallInfo *);
73 class JSNApiTests : public testing::Test {
74 public:
SetUpTestCase()75     static void SetUpTestCase()
76     {
77         GTEST_LOG_(INFO) << "SetUpTestCase";
78     }
79 
TearDownTestCase()80     static void TearDownTestCase()
81     {
82         GTEST_LOG_(INFO) << "TearDownCase";
83     }
84 
SetUp()85     void SetUp() override
86     {
87         RuntimeOption option;
88         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
89         vm_ = JSNApi::CreateJSVM(option);
90         ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
91         thread_ = vm_->GetJSThread();
92         vm_->SetEnableForceGC(true);
93         thread_->ManagedCodeBegin();
94     }
95 
TearDown()96     void TearDown() override
97     {
98         thread_->ManagedCodeEnd();
99         vm_->SetEnableForceGC(false);
100         JSNApi::DestroyJSVM(vm_);
101     }
102 
TestNumberRef(T val,TaggedType expected)103     template <typename T> void TestNumberRef(T val, TaggedType expected)
104     {
105         LocalScope scope(vm_);
106         Local<NumberRef> obj = NumberRef::New(vm_, val);
107         ASSERT_TRUE(obj->IsNumber());
108         JSTaggedType res = JSNApiHelper::ToJSTaggedValue(*obj).GetRawData();
109         ASSERT_EQ(res, expected);
110         if constexpr (std::is_floating_point_v<T>) {
111             if (std::isnan(val)) {
112                 ASSERT_TRUE(std::isnan(obj->Value()));
113             } else {
114                 ASSERT_EQ(obj->Value(), val);
115             }
116         } else if constexpr (sizeof(T) >= sizeof(int32_t)) {
117             ASSERT_EQ(obj->IntegerValue(vm_), val);
118         } else if constexpr (std::is_signed_v<T>) {
119             ASSERT_EQ(obj->Int32Value(vm_), val);
120         } else {
121             ASSERT_EQ(obj->Uint32Value(vm_), val);
122         }
123     }
124 
ConvertDouble(double val)125     TaggedType ConvertDouble(double val)
126     {
127         return base::bit_cast<JSTaggedType>(val) + JSTaggedValue::DOUBLE_ENCODE_OFFSET;
128     }
129 
130 protected:
131     JSThread *thread_ = nullptr;
132     EcmaVM *vm_ = nullptr;
133 };
134 
FunctionCallback(JsiRuntimeCallInfo * info)135 Local<JSValueRef> FunctionCallback(JsiRuntimeCallInfo *info)
136 {
137     EscapeLocalScope scope(info->GetVM());
138     return scope.Escape(ArrayRef::New(info->GetVM(), info->GetArgsNumber()));
139 }
140 
WeakRefCallback(EcmaVM * vm)141 void WeakRefCallback(EcmaVM *vm)
142 {
143     LocalScope scope(vm);
144     Local<ObjectRef> object = ObjectRef::New(vm);
145     Global<ObjectRef> globalObject(vm, object);
146     globalObject.SetWeak();
147     Local<ObjectRef> object1 = ObjectRef::New(vm);
148     Global<ObjectRef> globalObject1(vm, object1);
149     globalObject1.SetWeak();
150     vm->CollectGarbage(TriggerGCType::YOUNG_GC);
151     vm->CollectGarbage(TriggerGCType::OLD_GC);
152     globalObject.FreeGlobalHandleAddr();
153 }
154 
ThreadCheck(const EcmaVM * vm)155 void ThreadCheck(const EcmaVM *vm)
156 {
157     EXPECT_TRUE(vm->GetJSThread()->GetThreadId() != JSThread::GetCurrentThreadId());
158 }
159 
CheckReject(JsiRuntimeCallInfo * info)160 void CheckReject(JsiRuntimeCallInfo *info)
161 {
162     ASSERT_EQ(info->GetArgsNumber(), 1U);
163     Local<JSValueRef> reason = info->GetCallArgRef(0);
164     ASSERT_TRUE(reason->IsString(info->GetVM()));
165     ASSERT_EQ(Local<StringRef>(reason)->ToString(info->GetVM()), "Reject");
166 }
167 
RejectCallback(JsiRuntimeCallInfo * info)168 Local<JSValueRef> RejectCallback(JsiRuntimeCallInfo *info)
169 {
170     LocalScope scope(info->GetVM());
171     CheckReject(info);
172     return JSValueRef::Undefined(info->GetVM());
173 }
174 
175 /**
176  * @tc.number: ffi_interface_api_054
177  * @tc.name: NumberRef_uint32_int64
178  * @tc.desc:Define the variable input of type uint32_t and int64_t to determine whether the object is of numerical type
179  * @tc.type: FUNC
180  * @tc.require:  parameter
181  */
HWTEST_F_L0(JSNApiTests,NumberRef_uint32_int64)182 HWTEST_F_L0(JSNApiTests, NumberRef_uint32_int64)
183 {
184     uint32_t input = 32;
185     int64_t input1 = 1;
186     Local<NumberRef> res = NumberRef::New(vm_, input);
187     Local<NumberRef> res1 = NumberRef::New(vm_, input1);
188     ASSERT_TRUE(res->IsNumber());
189     ASSERT_TRUE(res1->IsNumber());
190 }
191 
192 /**
193  * @tc.number: ffi_interface_api_055
194  * @tc.name: NumberRef_int32_t_double
195  * @tc.desc:Define the variable input of type int32 and double to determine whether the object is of numerical type
196  * @tc.type: FUNC
197  * @tc.require:  parameter
198  */
HWTEST_F_L0(JSNApiTests,NumberRef_int32_t_double)199 HWTEST_F_L0(JSNApiTests, NumberRef_int32_t_double)
200 {
201     int32_t input = -1;
202     double input1 = 1.1;
203     Local<NumberRef> res = NumberRef::New(vm_, input);
204     Local<NumberRef> res1 = NumberRef::New(vm_, input1);
205     ASSERT_TRUE(res->IsNumber());
206     ASSERT_TRUE(res1->IsNumber());
207 }
208 
209 /**
210  * @tc.number: ffi_interface_api_056
211  * @tc.name: ObjectRef_Freeze
212  * @tc.desc:Execute a Freeze operation and call the JSObject:: SetIntegrity Level method to achieve immutability
213  * @tc.type: FUNC
214  * @tc.require:  parameter
215  */
HWTEST_F_L0(JSNApiTests,ObjectRef_Freeze)216 HWTEST_F_L0(JSNApiTests, ObjectRef_Freeze)
217 {
218     LocalScope scope(vm_);
219     Local<ObjectRef> object = ObjectRef::New(vm_);
220     thread_ = vm_->GetJSThread();
221     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
222     JSHandle<JSTaggedValue> set = env->GetBuiltinsSetFunction();
223     object->Freeze(vm_);
224     ecmascript::ThreadManagedScope managedScope(thread_);
225     bool status = JSObject::SetIntegrityLevel(thread_, JSHandle<JSObject>::Cast(set), IntegrityLevel::FROZEN);
226     ASSERT_TRUE(status);
227 }
228 
229 /**
230  * @tc.number: ffi_interface_api_057
231  * @tc.name: ObjectRef_Seal
232  * @tc.desc:The function is to perform a closed (Seal) operation by calling the JSObject::SetIntegrity Level method
233  * to ensure that its properties cannot be modified or deleted, and to set the integrity level of
234  * the object to SEALED
235  * @tc.type: FUNC
236  * @tc.require:  parameter
237  */
HWTEST_F_L0(JSNApiTests,ObjectRef_Seal)238 HWTEST_F_L0(JSNApiTests, ObjectRef_Seal)
239 {
240     LocalScope scope(vm_);
241     Local<ObjectRef> object = ObjectRef::New(vm_); // 创建一个新的空对象
242     thread_ = vm_->GetJSThread();
243     JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
244     JSHandle<JSTaggedValue> set = env->GetBuiltinsSetFunction();
245     object->Seal(vm_); // 尝试将对象封闭
246     ecmascript::ThreadManagedScope managedScope(thread_);
247     bool status = JSObject::SetIntegrityLevel(thread_, JSHandle<JSObject>::Cast(set), IntegrityLevel::SEALED);
248     ASSERT_TRUE(status);
249 }
250 
251 /**
252  * @tc.number: ffi_interface_api_058
253  * @tc.name: ObjectRef_GetAllPropertyNames
254  * @tc.desc:Use the GetAllPropertyNames method to obtain all property names of the object and return an ArrayRef object.
255  * @tc.type: FUNC
256  * @tc.require:  parameter
257  */
HWTEST_F_L0(JSNApiTests,ObjectRef_GetAllPropertyNames)258 HWTEST_F_L0(JSNApiTests, ObjectRef_GetAllPropertyNames)
259 {
260     LocalScope scope(vm_);
261     Local<ObjectRef> object = ObjectRef::New(vm_);
262     uint32_t filter = 3;
263     Local<ArrayRef> res = object->GetAllPropertyNames(vm_, filter);
264     ASSERT_FALSE(res->IsBigInt(vm_));
265     ASSERT_TRUE(res->IsArray(vm_));
266 }
267 
268 /**
269  * @tc.number: ffi_interface_api_059
270  * @tc.name: GetIndex
271  * @tc.desc:Call the GetIndex() function to obtain the index value and use EXCECT_ EQ()
272  * assertion is used to verify whether the result is 0, which is the initial index value.
273  * @tc.type: FUNC
274  * @tc.require:  parameter
275  */
HWTEST_F_L0(JSNApiTests,GetIndex)276 HWTEST_F_L0(JSNApiTests, GetIndex)
277 {
278     ecmascript::ThreadManagedScope managedScope(vm_->GetJSThread());
279     LocalScope scope(vm_);
280     JSThread *thread = vm_->GetJSThread();
281     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
282     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
283     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
284     JSHandle<JSMap> jsMap =
285         JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
286     JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
287     jsMap->SetLinkedMap(thread, hashMap);
288     JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
289     JSHandle<JSTaggedValue> jsMapIteratorTag4 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::VALUE);
290     JSHandle<JSMapIterator> jsMapIterator4(jsMapIteratorTag4);
291     Local<MapIteratorRef> mapIterator4 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag4);
292     int32_t res4 = mapIterator4->GetIndex();
293     EXPECT_EQ(0, res4);
294 }
295 
296 /**
297  * @tc.number: ffi_interface_api_060
298  * @tc.name: GetKind_entries_values_keys
299  * @tc.desc:This test case is mainly used to verify whether the GetKind method of the JSMapIterator object can
300  * correctly return the expected type.
301  * @tc.type: FUNC
302  * @tc.require:  parameter
303  */
HWTEST_F_L0(JSNApiTests,GetKind_entries_values_keys)304 HWTEST_F_L0(JSNApiTests, GetKind_entries_values_keys)
305 {
306     LocalScope scope(vm_);
307     JSThread *thread = vm_->GetJSThread();
308     ecmascript::ThreadManagedScope managedScope(thread);
309     std::string expectedResult = "entries";
310     std::string valuesResult = "values";
311     std::string keysResult = "keys";
312     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
313     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
314     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
315     JSHandle<JSMap> jsMap =
316         JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
317     JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
318     jsMap->SetLinkedMap(thread, hashMap);
319     JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
320     JSHandle<JSTaggedValue> jsMapIteratorTag =
321         JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY_AND_VALUE);
322     JSHandle<JSMapIterator> jsMapIterator(jsMapIteratorTag);
323     EXPECT_EQ(JSTaggedValue::SameValue(jsMapIterator->GetIteratedMap(), jsMap->GetLinkedMap()), true);
324     Local<MapIteratorRef> mapIterator = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag);
325     Local<JSValueRef> res = mapIterator->GetKind(vm_);
326     EXPECT_EQ(expectedResult, res->ToString(vm_)->ToString(vm_));
327     EXPECT_TRUE(mapIterator->IsMapIterator(vm_));
328     JSHandle<JSTaggedValue> jsMapIteratorTag1 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY);
329     Local<MapIteratorRef> mapIterator1 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag1);
330     Local<JSValueRef> res1 = mapIterator1->GetKind(vm_);
331     EXPECT_EQ(keysResult, res1->ToString(vm_)->ToString(vm_));
332     JSHandle<JSTaggedValue> jsMapIteratorTag2 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::VALUE);
333     Local<MapIteratorRef> mapIterator2 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag2);
334     Local<JSValueRef> res2 = mapIterator2->GetKind(vm_);
335     EXPECT_EQ(valuesResult, res2->ToString(vm_)->ToString(vm_));
336 }
337 
338 /**
339  * @tc.number: ffi_interface_api_061
340  * @tc.name: GetKind_001
341  * @tc.desc:This test case is mainly used to verify whether the GetKind method of the JSMapIterator object can
342  * correctly return the expected type (in this example, the expected type is "keys").
343  * @tc.type: FUNC
344  * @tc.require:  parameter
345  */
HWTEST_F_L0(JSNApiTests,GetKind_001)346 HWTEST_F_L0(JSNApiTests, GetKind_001)
347 {
348     ecmascript::ThreadManagedScope managedScope(vm_->GetJSThread());
349     LocalScope scope(vm_);
350     JSThread *thread = vm_->GetJSThread();
351     std::string keysResult = "keys";
352     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
353     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
354     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
355     JSHandle<JSMap> jsMap =
356         JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
357     JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
358     jsMap->SetLinkedMap(thread, hashMap);
359     JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
360     JSHandle<JSTaggedValue> jsMapIteratorTag1 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY);
361     JSHandle<JSMapIterator> jsMapIterator1(jsMapIteratorTag1);
362     EXPECT_EQ(JSTaggedValue::SameValue(jsMapIterator1->GetIteratedMap(), jsMap->GetLinkedMap()), true);
363     Local<MapIteratorRef> mapIterator1 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag1);
364     Local<JSValueRef> res1 = mapIterator1->GetKind(vm_);
365     EXPECT_EQ(keysResult, res1->ToString(vm_)->ToString(vm_));
366 }
367 
368 /**
369  * @tc.number: ffi_interface_api_062
370  * @tc.name: GetKind_002
371  * @tc.desc:This test case is mainly used to verify whether the GetKind method of the JSMapIterator object can
372  * correctly return the expected type (in this example, the expected type is "values").
373  * @tc.type: FUNC
374  * @tc.require:  parameter
375  */
HWTEST_F_L0(JSNApiTests,GetKind_002)376 HWTEST_F_L0(JSNApiTests, GetKind_002)
377 {
378     ecmascript::ThreadManagedScope managedScope(vm_->GetJSThread());
379     LocalScope scope(vm_);
380     JSThread *thread = vm_->GetJSThread();
381     std::string valuesResult = "values";
382     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
383     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
384     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
385     JSHandle<JSMap> jsMap =
386         JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
387     JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
388     jsMap->SetLinkedMap(thread, hashMap);
389     JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
390     JSHandle<JSTaggedValue> jsMapIteratorTag2 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::VALUE);
391     JSHandle<JSMapIterator> jsMapIterator2(jsMapIteratorTag2);
392     EXPECT_EQ(JSTaggedValue::SameValue(jsMapIterator2->GetIteratedMap(), jsMap->GetLinkedMap()), true);
393     Local<MapIteratorRef> mapIterator2 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag2);
394     Local<JSValueRef> res2 = mapIterator2->GetKind(vm_);
395     EXPECT_EQ(valuesResult, res2->ToString(vm_)->ToString(vm_));
396 }
397 
398 /**
399  * @tc.number: ffi_interface_api_063
400  * @tc.name: GetKind_003
401  * @tc.desc:Calling the GetKind method to obtain the iteration type KEY_AND_VALUE Compare with the string
402  * variable 'entries' to assert whether the expected result is the same as the actual result
403  * @tc.type: FUNC
404  * @tc.require:  parameter
405  */
HWTEST_F_L0(JSNApiTests,GetKind_003)406 HWTEST_F_L0(JSNApiTests, GetKind_003)
407 {
408     ecmascript::ThreadManagedScope managedScope(vm_->GetJSThread());
409     LocalScope scope(vm_);
410     JSThread *thread = vm_->GetJSThread();
411     std::string expectedResult = "entries";
412     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
413     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
414     // 从全局环境(GlobalEnv)中获取内置的Map构造函数
415     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
416     // 使用构造函数创建一个新的JSMap对象
417     JSHandle<JSMap> jsMap =
418         JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
419     JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
420     // 将创建的LinkedHashMap设置为JSMap的内部数据结构
421     jsMap->SetLinkedMap(thread, hashMap);
422     // 将jsMap转换为JSTaggedValue的句柄
423     JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
424     // 创建一个新的JSMapIterator对象,并将其迭代方式设置为KEY_AND_VALUE
425     JSHandle<JSTaggedValue> jsMapIteratorTag =
426         JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY_AND_VALUE);
427     JSHandle<JSMapIterator> jsMapIterator(jsMapIteratorTag);
428     EXPECT_EQ(JSTaggedValue::SameValue(jsMapIterator->GetIteratedMap(), jsMap->GetLinkedMap()), true);
429     // 将jsMapIteratorTag转换为本地代码可以使用的MapIteratorRef类型
430     Local<MapIteratorRef> mapIterator = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag);
431     Local<JSValueRef> res = mapIterator->GetKind(vm_);
432     EXPECT_EQ(expectedResult, res->ToString(vm_)->ToString(vm_));
433     EXPECT_TRUE(mapIterator->IsMapIterator(vm_));
434 }
435 
436 /*
437  * @tc.number: ffi_interface_api_064
438  * @tc.name: GetProperty_IsFunction
439  * @tc.desc: Verify if the GetProperty function of the JavaScript virtual machine correctly returns a
440  * function as the property of the 'Number' key.
441  * @tc.type: FUNC
442  * @tc.require:  parameter
443  */
HWTEST_F_L0(JSNApiTests,GetProperty_IsFunction)444 HWTEST_F_L0(JSNApiTests, GetProperty_IsFunction)
445 {
446     LocalScope scope(vm_);
447     Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm_);
448     ASSERT_FALSE(globalObject.IsEmpty());
449     ASSERT_TRUE(globalObject->IsObject(vm_));
450 
451     Local<ObjectRef> key = StringRef::NewFromUtf8(vm_, "Number");
452     Local<ObjectRef> property = globalObject->Get(vm_, key);
453     ASSERT_TRUE(property->IsFunction(vm_));
454 }
455 
456 /**
457  * @tc.number: ffi_interface_api_065
458  * @tc.name: NewClassFunction
459  * @tc.desc:Check if the function created through the NewClassFunction method meets the specifications of the class
460  * constructor, and obtain and verify the properties of the function.
461  * @tc.type: FUNC
462  * @tc.require:  parameter
463  */
HWTEST_F_L0(JSNApiTests,NewClassFunction)464 HWTEST_F_L0(JSNApiTests, NewClassFunction)
465 {
466     LocalScope scope(vm_);
467     Local<FunctionRef> cls = FunctionRef::NewClassFunction(vm_, FunctionCallback, nullptr, nullptr);
468 
469     JSHandle<JSTaggedValue> obj = JSNApiHelper::ToJSHandle(Local<JSValueRef>(cls));
470     // 断言obj是一个类构造函数
471     ASSERT_TRUE(obj->IsClassConstructor());
472     // GetPropertyInlinedProps方法获取内联属性的方法,CLASS_PROTOTYPE_INLINE_PROPERTY_INDEX类原型的内联属性索引
473     JSTaggedValue res =
474         JSHandle<JSFunction>(obj)->GetPropertyInlinedProps(JSFunction::CLASS_PROTOTYPE_INLINE_PROPERTY_INDEX);
475     // 断言获取的属性是一个内部访问器
476     ASSERT_TRUE(res.IsInternalAccessor());
477 }
478 
479 /**
480  * @tc.number: ffi_interface_api_066
481  * @tc.name: PromiseRef_Finally_IsPromise
482  * @tc.desc:FunctionRef:: New Create a reject callback function reject. The function of the code is to test the
483  * behavior of the Finally and Then methods of the Promise object
484  * in various situations, ensuring that they all return Promise objects and meet the expected behavior.
485  * @tc.type: FUNC
486  * @tc.require:  parameter
487  */
HWTEST_F_L0(JSNApiTests,PromiseRef_Finally_IsPromise)488 HWTEST_F_L0(JSNApiTests, PromiseRef_Finally_IsPromise)
489 {
490     LocalScope scope(vm_);
491     Local<PromiseCapabilityRef> capability = PromiseCapabilityRef::New(vm_);
492 
493     Local<PromiseRef> gitpromise = capability->GetPromise(vm_);
494     Local<FunctionRef> rejectcallback = FunctionRef::New(vm_, RejectCallback);
495     Local<PromiseRef> catchPromise = gitpromise->Finally(vm_, rejectcallback);
496     ASSERT_TRUE(gitpromise->IsPromise(vm_));
497     ASSERT_TRUE(catchPromise->IsPromise(vm_));
498     Local<PromiseRef> catchPromise2 = gitpromise->Then(vm_, rejectcallback, rejectcallback);
499     ASSERT_TRUE(catchPromise2->IsPromise(vm_));
500     Local<FunctionRef> functioncallback = FunctionRef::New(vm_, FunctionCallback);
501     ASSERT_TRUE(!functioncallback.IsEmpty());
502     Local<PromiseRef> catchPromise3 = gitpromise->Then(vm_, functioncallback);
503     ASSERT_TRUE(catchPromise3->IsPromise(vm_));
504 }
505 
506 /**
507  * @tc.number: ffi_interface_api_067
508  * @tc.name: IsBuffer
509  * @tc.desc: Construct a BufferRef function to determine whether it is a Buffer
510  * @tc.type: FUNC
511  * @tc.require:  parameter
512  */
HWTEST_F_L0(JSNApiTests,IsBuffer)513 HWTEST_F_L0(JSNApiTests, IsBuffer)
514 {
515     LocalScope scope(vm_);
516     const int32_t length = 15;
517     Local<BufferRef> buffer = BufferRef::New(vm_, length);
518     ASSERT_TRUE(buffer->IsBuffer(vm_));
519 }
520 
521 /**
522  * @tc.number: ffi_interface_api_068
523  * @tc.name: IsDataView
524  * @tc.desc: Construct a BufferRef function to determine whether it is a dataView
525  * @tc.type: FUNC
526  * @tc.require:  parameter
527  */
HWTEST_F_L0(JSNApiTests,IsDataView)528 HWTEST_F_L0(JSNApiTests, IsDataView)
529 {
530     LocalScope scope(vm_);
531     const int32_t length = 15;
532     Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
533     Local<DataViewRef> dataView = DataViewRef::New(vm_, arrayBuffer, 5, 7);
534     ASSERT_TRUE(dataView->IsDataView(vm_));
535 }
536 
537 /**
538  * @tc.number: ffi_interface_api_071
539  * @tc.name: IsSharedArrayBuffer
540  * @tc.desc: Construct a BufferRef function to determine whether it is a SharedArrayBuffer
541  * @tc.type: FUNC
542  * @tc.require:  parameter
543  */
HWTEST_F_L0(JSNApiTests,IsSharedArrayBuffer)544 HWTEST_F_L0(JSNApiTests, IsSharedArrayBuffer)
545 {
546     LocalScope scope(vm_);
547     const int32_t length = 15;
548     Local<JSValueRef> sharedArrayBuffer = ArrayBufferRef::New(vm_, length);
549     ASSERT_FALSE(sharedArrayBuffer->IsSharedArrayBuffer(vm_));
550 }
551 
552 
553 /**
554  * @tc.number: ffi_interface_api_072
555  * @tc.name: IsTrue
556  * @tc.desc: Construct a BufferRef function to determine whether it is a IsTrue
557  * @tc.type: FUNC
558  * @tc.require:  parameter
559  */
HWTEST_F_L0(JSNApiTests,IsTrue)560 HWTEST_F_L0(JSNApiTests, IsTrue)
561 {
562     LocalScope scope(vm_);
563     Local<JSValueRef> b = JSValueRef::True(vm_);
564     ASSERT_TRUE(b->IsTrue());
565 }
566 
567 
568 /**
569  * @tc.number: ffi_interface_api_073
570  * @tc.name: IsFalse
571  * @tc.desc: Construct a BufferRef function to determine whether it is a IsFalse
572  * @tc.type: FUNC
573  * @tc.require:  parameter
574  */
HWTEST_F_L0(JSNApiTests,IsFalse)575 HWTEST_F_L0(JSNApiTests, IsFalse)
576 {
577     LocalScope scope(vm_);
578     Local<JSValueRef> c = JSValueRef::False(vm_);
579     ASSERT_TRUE(c->IsFalse());
580 }
581 
582 /**
583  * @tc.number: ffi_interface_api_074
584  * @tc.name: IsConstructor
585  * @tc.desc: Construct a BufferRef function to determine whether it is a Constructor
586  * @tc.require:  parameter
587  */
HWTEST_F_L0(JSNApiTests,IsConstructor)588 HWTEST_F_L0(JSNApiTests, IsConstructor)
589 {
590     LocalScope scope(vm_);
591     Local<FunctionRef> target = FunctionRef::New(vm_, FunctionCallback);
592     ASSERT_FALSE(target->IsConstructor(vm_));
593 }
594 
595 
596 /**
597  * @tc.number: ffi_interface_api_076
598  * @tc.name: GetOwnProperty
599  * @tc.desc: Construct a BufferRef function to determine whether it is a GetOwnProperty  not Construct function
600  * @tc.type: FUNC
601  * @tc.require:  parameter
602  */
HWTEST_F_L0(JSNApiTests,GetOwnProperty)603 HWTEST_F_L0(JSNApiTests, GetOwnProperty)
604 {
605     LocalScope scope(vm_);
606     Local<ObjectRef> object = ObjectRef::New(vm_);
607     Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, "TestKey");
608     Local<JSValueRef> value = ObjectRef::New(vm_);
609     PropertyAttribute attribute(value, true, true, true);
610 
611     ASSERT_TRUE(object->DefineProperty(vm_, key, attribute));
612     Local<JSValueRef> value1 = object->Get(vm_, key);
613     ASSERT_TRUE(value->IsStrictEquals(vm_, value1));
614 }
615 
616 /**
617  * @tc.number: ffi_interface_api_078
618  * @tc.name: IsTreeMap
619  * @tc.desc: Used to verify whether the given object is a TreeMap.
620  * @tc.type: FUNC
621  * @tc.require:  parameter
622  */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsTreeMap)623 HWTEST_F_L0(JSNApiTests, JSValueRef_IsTreeMap)
624 {
625     LocalScope scope(vm_);
626     JSThread *thread = vm_->GetJSThread();
627     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
628     auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
629     JSHandle<JSTaggedValue> proto = globalEnv->GetObjectFunctionPrototype();
630     JSHandle<JSHClass> mapClass = factory->NewEcmaHClass(JSAPITreeMap::SIZE, JSType::JS_API_TREE_MAP, proto);
631     JSHandle<JSAPITreeMap> jsTreeMap = JSHandle<JSAPITreeMap>::Cast(factory->NewJSObjectWithInit(mapClass));
632     JSHandle<TaggedTreeMap> treeMap(thread, TaggedTreeMap::Create(thread));
633     jsTreeMap->SetTreeMap(thread, treeMap);
634     JSHandle<JSTaggedValue> argumentTag = JSHandle<JSTaggedValue>::Cast(jsTreeMap);
635     Local<JSValueRef> TreeMap = JSNApiHelper::ToLocal<JSAPITreeSet>(argumentTag);
636     EXPECT_TRUE(TreeMap->IsTreeMap(vm_));
637 }
638 
639 /**
640  * @tc.number: ffi_interface_api_079
641  * @tc.name: IsTreeSet
642  * @tc.desc: Used to verify whether the given object is a TreeSet.
643  * @tc.type: FUNC
644  * @tc.require:  parameter
645  */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsTreeSet)646 HWTEST_F_L0(JSNApiTests, JSValueRef_IsTreeSet)
647 {
648     LocalScope scope(vm_);
649     JSThread *thread = vm_->GetJSThread();
650     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
651     auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
652     JSHandle<JSTaggedValue> proto = globalEnv->GetObjectFunctionPrototype();
653     JSHandle<JSHClass> setClass = factory->NewEcmaHClass(JSAPITreeSet::SIZE, JSType::JS_API_TREE_SET, proto);
654     JSHandle<JSAPITreeSet> jsTreeSet = JSHandle<JSAPITreeSet>::Cast(factory->NewJSObjectWithInit(setClass));
655     JSHandle<TaggedTreeSet> treeSet(thread, TaggedTreeSet::Create(thread));
656     jsTreeSet->SetTreeSet(thread, treeSet);
657     JSHandle<JSTaggedValue> argumentTag = JSHandle<JSTaggedValue>::Cast(jsTreeSet);
658     Local<JSValueRef> TreeSet = JSNApiHelper::ToLocal<JSAPITreeSet>(argumentTag);
659     EXPECT_TRUE(TreeSet->IsTreeSet(vm_));
660 }
661 
662 /**
663  * @tc.number: ffi_interface_api_080
664  * @tc.name: IsVector
665  * @tc.desc: Used to verify whether the given object is a Vector.
666  * @tc.type: FUNC
667  * @tc.require:  parameter
668  */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsVector)669 HWTEST_F_L0(JSNApiTests, JSValueRef_IsVector)
670 {
671     LocalScope scope(vm_);
672     JSThread *thread = vm_->GetJSThread();
673     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
674     auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
675     JSHandle<JSTaggedValue> proto = globalEnv->GetObjectFunctionPrototype();
676     JSHandle<JSHClass> vectorClass = factory->NewEcmaHClass(JSAPIVector::SIZE, JSType::JS_API_VECTOR, proto);
677     JSHandle<JSAPIVector> jsVector = JSHandle<JSAPIVector>::Cast(factory->NewJSObjectWithInit(vectorClass));
678     jsVector->SetLength(0);
679     JSHandle<JSTaggedValue> argumentTag = JSHandle<JSTaggedValue>::Cast(jsVector);
680     Local<JSValueRef> Vector = JSNApiHelper::ToLocal<JSAPIVector>(argumentTag);
681     EXPECT_TRUE(Vector->IsVector(vm_));
682 }
683 
684 /**
685  * @tc.number: ffi_interface_api_081
686  * @tc.name: IsJSArray
687  * @tc.desc: Used to verify whether the given object is a JSArray.
688  * @tc.type: FUNC
689  * @tc.require:  parameter
690  */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsJSArray)691 HWTEST_F_L0(JSNApiTests, JSValueRef_IsJSArray)
692 {
693     LocalScope scope(vm_);
694     JSHandle<JSTaggedValue> jsArrayTag = JSArray::ArrayCreate(vm_->GetJSThread(), JSTaggedNumber(0));
695     Local<JSValueRef> jsArray = JSNApiHelper::ToLocal<JSTypedArray>(jsArrayTag);
696     EXPECT_TRUE(jsArray->IsJSArray(vm_));
697     Local<JSValueRef> array = JSNApiHelper::ToLocal<ArrayRef>(jsArrayTag);
698     EXPECT_TRUE(array->IsArray(vm_));
699 }
700 
701 /**
702  * @tc.number: ffi_interface_api_082
703  * @tc.name: IsMap
704  * @tc.desc: Used to verify whether the given object is a map container.
705  * @tc.type: FUNC
706  * @tc.require:  parameter
707  */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsMap)708 HWTEST_F_L0(JSNApiTests, JSValueRef_IsMap)
709 {
710     LocalScope scope(vm_);
711     Local<MapRef> map = MapRef::New(vm_);
712     EXPECT_TRUE(map->IsMap(vm_));
713 }
714 
715 /**
716  * @tc.number: ffi_interface_api_083
717  * @tc.name: IsSet
718  * @tc.desc: Used to verify whether the given object is a Set container.
719  * @tc.type: FUNC
720  * @tc.require:  parameter
721  */
HWTEST_F_L0(JSNApiTests,SetRef_IsSet)722 HWTEST_F_L0(JSNApiTests, SetRef_IsSet)
723 {
724     LocalScope scope(vm_);
725     JSThread *thread = vm_->GetJSThread();
726     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
727     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
728     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsSetFunction();
729     JSHandle<JSSet> set =
730         JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
731     JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
732     set->SetLinkedSet(thread, hashSet);
733     JSHandle<JSTaggedValue> setTag = JSHandle<JSTaggedValue>::Cast(set);
734     Local<SetRef> Set = JSNApiHelper::ToLocal<SetRef>(setTag);
735     EXPECT_TRUE(Set->IsSet(vm_));
736 }
737 
738 /**
739  * @tc.number: ffi_interface_api_084
740  * @tc.name: ObjectRef_NEW
741  * @tc.desc: Creating a new Object Ref object is not NULL if successfully created.
742  * @tc.type: FUNC
743  * @tc.require:  parameter
744  */
HWTEST_F_L0(JSNApiTests,ObjectRef_NEW)745 HWTEST_F_L0(JSNApiTests, ObjectRef_NEW)
746 {
747     LocalScope scope(vm_);
748     Local<ObjectRef> object = ObjectRef::New(vm_);
749     ASSERT_FALSE(object.IsNull());
750 }
751 
752 /**
753  * @tc.number: ffi_interface_api_085
754  * @tc.name: IsFalse
755  * @tc.desc: Used to verify whether the given object is false.
756  * @tc.type: FUNC
757  * @tc.require:  parameter
758  */
HWTEST_F_L0(JSNApiTests,JSNAPI_IsFalse)759 HWTEST_F_L0(JSNApiTests, JSNAPI_IsFalse)
760 {
761     LocalScope scope(vm_);
762     Local<PrimitiveRef> res = JSValueRef::False(vm_);
763     EXPECT_TRUE(res->IsFalse());
764 }
765 
766 /**
767  * @tc.number: ffi_interface_api_086
768  * @tc.name: GetDescription
769  * @tc.desc: Used to verify whether the object used to obtain descriptive information was successful.
770  * @tc.type: FUNC
771  * @tc.require:  parameter
772  */
HWTEST_F_L0(JSNApiTests,SymbolRef_GetDescription)773 HWTEST_F_L0(JSNApiTests, SymbolRef_GetDescription)
774 {
775     LocalScope scope(vm_);
776     Local<StringRef> description = StringRef::NewFromUtf8(vm_, "test");
777     Local<SymbolRef> symbol = SymbolRef::New(vm_, description);
778     EXPECT_FALSE(symbol.IsNull());
779     EXPECT_FALSE(description.IsEmpty());
780 }
781 
782 /**
783  * @tc.number: ffi_interface_api_087
784  * @tc.name: ArrayRefNew_uint32Length_SetValueAt_GetValueAt
785  * @tc.desc: Used to verify whether obtaining the length of the array, setting the value of the specified
786  * index position of the array, and obtaining the value of the specified index position of the
787  * array were successful
788  * @tc.type: FUNC
789  * @tc.require:  parameter
790  */
HWTEST_F_L0(JSNApiTests,ArrayRefNew_uint32Length_SetValueAt_GetValueAt)791 HWTEST_F_L0(JSNApiTests, ArrayRefNew_uint32Length_SetValueAt_GetValueAt)
792 {
793     LocalScope scope(vm_);
794     Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm_);
795     ASSERT_FALSE(globalObject.IsEmpty());
796     ASSERT_TRUE(globalObject->IsObject(vm_));
797     Local<ArrayRef> property = ArrayRef::New(vm_, 3); // 3 : length
798     ASSERT_TRUE(property->IsArray(vm_));
799     ASSERT_EQ(property->Length(vm_), 3); // 3 : test case of input
800     uint32_t index = 1;
801     Local<JSValueRef> value = ObjectRef::New(vm_);
802     bool result = property->SetValueAt(vm_, globalObject, index, value);
803     ASSERT_TRUE(result);
804     Local<JSValueRef> value1 = property->GetValueAt(vm_, globalObject, index);
805     ASSERT_FALSE(value1.IsNull());
806 }
807 
808 /**
809  * @tc.number: ffi_interface_api_088
810  * @tc.name: WeakSetRef_GetSize_GetTotalElements_GetValue
811  * @tc.desc: Used to verify whether the size of the weakset for obtaining settings, the total number of
812  * elements obtained, and the value of the specified index position obtained were successful
813  * @tc.type: FUNC
814  * @tc.require:  parameter
815  */
HWTEST_F_L0(JSNApiTests,WeakSetRef_GetSize_GetTotalElements_GetValue)816 HWTEST_F_L0(JSNApiTests, WeakSetRef_GetSize_GetTotalElements_GetValue)
817 {
818     LocalScope scope(vm_);
819     JSThread *thread = vm_->GetJSThread();
820     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
821     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
822     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsWeakSetFunction();
823     JSHandle<JSWeakSet> weakSet =
824         JSHandle<JSWeakSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
825     JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
826     weakSet->SetLinkedSet(thread, hashSet);
827     JSHandle<JSTaggedValue> weakSetTag = JSHandle<JSTaggedValue>::Cast(weakSet);
828     Local<JSValueRef> set1 = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
829     EXPECT_TRUE(set1->IsWeakSet(vm_));
830     Local<WeakSetRef> set = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
831     JSHandle<JSTaggedValue> value(factory->NewFromASCII("value"));
832     JSWeakSet::Add(thread, weakSet, value);
833     int32_t num = set->GetSize(vm_);
834     int32_t num1 = set->GetTotalElements(vm_);
835     ASSERT_EQ(num, 1);
836     ASSERT_EQ(num1, 1);
837     Local<JSValueRef> res2 = set->GetValue(vm_, 0);
838     ASSERT_EQ(res2->ToString(vm_)->ToString(vm_), "value");
839 }
840 
841 /**
842  * @tc.number: ffi_interface_api_089
843  * @tc.name: GetOwnPropertyNames
844  * @tc.desc: An array of self owned property names used to validate the acquisition of objects.
845  * @tc.type: FUNC
846  * @tc.require:  parameter
847  */
HWTEST_F_L0(JSNApiTests,ObjectRef_GetOwnPropertyNames)848 HWTEST_F_L0(JSNApiTests, ObjectRef_GetOwnPropertyNames)
849 {
850     LocalScope scope(vm_);
851     Local<ObjectRef> object = ObjectRef::New(vm_);
852     Local<ArrayRef> res = object->GetOwnPropertyNames(vm_);
853     EXPECT_TRUE(res->IsArray(vm_));
854 }
855 
856 /**
857  * @tc.number: ffi_interface_api_090
858  * @tc.name: SetBundle
859  * @tc.desc: Used to verify whether the initialization resource package was successfully set.
860  * @tc.type: FUNC
861  * @tc.require:  parameter
862  */
HWTEST_F_L0(JSNApiTests,SetBundle)863 HWTEST_F_L0(JSNApiTests, SetBundle)
864 {
865     LocalScope scope(vm_);
866     bool value = true;
867     JSNApi::SetBundle(vm_, value);
868     bool res = JSNApi::IsBundle(vm_);
869     EXPECT_TRUE(res);
870 }
871 
872 /**
873  * @tc.number: ffi_interface_api_091
874  * @tc.name: SetMockModuleList
875  * @tc.desc: Used to verify whether the function of setting the map container module was successful.
876  * @tc.type: FUNC
877  * @tc.require:  parameter
878  */
HWTEST_F_L0(JSNApiTests,JSNApi_SetMockModuleList)879 HWTEST_F_L0(JSNApiTests, JSNApi_SetMockModuleList)
880 {
881     LocalScope scope(vm_);
882     std::map<std::string, std::string> str = { { "10", "20" } };
883     JSNApi::SetMockModuleList(vm_, str);
884     ASSERT_EQ(std::string(vm_->GetMockModule("10")), "20");
885 }
886 
887 /**
888  * @tc.number: ffi_interface_api_092
889  * @tc.name: SetSourceMapTranslateCallback
890  * @tc.desc: Whether the source mapping translation callback function used to verify the settings.
891  * was successfully set
892  * @tc.type: FUNC
893  * @tc.require:  parameter
894  */
HWTEST_F_L0(JSNApiTests,JSNAPI_SetSourceMapTranslateCallback)895 HWTEST_F_L0(JSNApiTests, JSNAPI_SetSourceMapTranslateCallback)
896 {
897     LocalScope scope(vm_);
898     SourceMapTranslateCallback tag { nullptr };
899     JSNApi::SetSourceMapTranslateCallback(vm_, tag);
900     SourceMapTranslateCallback cur = vm_->GetSourceMapTranslateCallback();
901     ASSERT_EQ(nullptr, cur);
902 }
903 
904 /**
905  * @tc.number: ffi_interface_api_093
906  * @tc.name: ExecutePendingJob
907  * @tc.desc: Used to verify whether the pending task has been successfully executed.
908  * @tc.type: FUNC
909  * @tc.require:  parameter
910  */
HWTEST_F_L0(JSNApiTests,JSNApi_ExecutePendingJob)911 HWTEST_F_L0(JSNApiTests, JSNApi_ExecutePendingJob)
912 {
913     LocalScope scope(vm_);
914     JSNApi::ExecutePendingJob(vm_);
915     bool res = EcmaVM::ConstCast(vm_)->GetJSThread()->GetCurrentEcmaContext()->ExecutePromisePendingJob();
916     EXPECT_TRUE(res);
917 }
918 
919 /**
920  * @tc.number: ffi_interface_api_096
921  * @tc.name: IsRegExp
922  * @tc.desc: Used to determine whether a given object is a regular expression.
923  * @tc.type: FUNC
924  * @tc.require:  parameter
925  */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsRegExp)926 HWTEST_F_L0(JSNApiTests, JSValueRef_IsRegExp)
927 {
928     LocalScope scope(vm_);
929     JSThread *thread = vm_->GetJSThread();
930     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
931     auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
932     JSHandle<JSTaggedValue> proto = globalEnv->GetObjectFunctionPrototype();
933     JSHandle<JSHClass> jSRegExpClass = factory->NewEcmaHClass(JSRegExp::SIZE, JSType::JS_REG_EXP, proto);
934     JSHandle<JSRegExp> jSRegExp = JSHandle<JSRegExp>::Cast(factory->NewJSObject(jSRegExpClass));
935     jSRegExp->SetByteCodeBuffer(thread, JSTaggedValue::Undefined());
936     jSRegExp->SetOriginalSource(thread, JSTaggedValue::Undefined());
937     jSRegExp->SetGroupName(thread, JSTaggedValue::Undefined());
938     jSRegExp->SetOriginalFlags(thread, JSTaggedValue(0));
939     jSRegExp->SetLength(0);
940     JSHandle<JSTaggedValue> argumentTag = JSHandle<JSTaggedValue>::Cast(jSRegExp);
941     Local<JSValueRef> regexp = JSNApiHelper::ToLocal<JSRegExp>(argumentTag);
942     EXPECT_TRUE(regexp->IsRegExp(vm_));
943 }
944 
945 /**
946  * @tc.number: ffi_interface_api_097
947  * @tc.name: GetAndClearUncaughtException
948  * @tc.desc: Used to verify the success of obtaining and clearing uncaught exceptions
949  * @tc.type: FUNC
950  * @tc.require:  parameter
951  */
HWTEST_F_L0(JSNApiTests,GetAndClearUncaughtException)952 HWTEST_F_L0(JSNApiTests, GetAndClearUncaughtException)
953 {
954     LocalScope scope(vm_);
955     TryCatch tryCatch(vm_);
956     EXPECT_FALSE(tryCatch.HasCaught());
957     Local<StringRef> message = StringRef::NewFromUtf8(vm_, "ErrorTest");
958     Local<JSValueRef> error = Exception::Error(vm_, message);
959     EXPECT_TRUE(error->IsError(vm_));
960     JSNApi::ThrowException(vm_, error);
961     EXPECT_TRUE(vm_->GetJSThread()->HasPendingException());
962     JSNApi::GetAndClearUncaughtException(vm_);
963     EXPECT_FALSE(vm_->GetJSThread()->HasPendingException());
964 }
965 
966 /**
967  * @tc.number: ffi_interface_api_098
968  * @tc.name: IsJSPrimitiveNumber
969  * @tc.desc: Verify if the given JSValueRef object is a primativenumber.
970  * @tc.type: FUNC
971  * @tc.require:  parameter
972  */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsJSPrimitiveNumber)973 HWTEST_F_L0(JSNApiTests, JSValueRef_IsJSPrimitiveNumber)
974 {
975     LocalScope scope(vm_);
976     ObjectFactory *factory = vm_->GetFactory();
977     JSHandle<JSTaggedValue> jstagvalue;
978     JSHandle<JSPrimitiveRef> jsprimitive = factory->NewJSPrimitiveRef(PrimitiveType::PRIMITIVE_NUMBER, jstagvalue);
979     JSHandle<JSTaggedValue> jspri = JSHandle<JSTaggedValue>::Cast(jsprimitive);
980     Local<JSValueRef> object = JSNApiHelper::ToLocal<JSValueRef>(jspri);
981     EXPECT_FALSE(object->IsJSPrimitiveNumber(vm_));
982 }
983 
984 
985 /**
986  * @tc.number: ffi_interface_api_099
987  * @tc.name: StringUtf16_NewFromUtf16_Length_WriteUtf16_01
988  * @tc.desc:
989  * NewFromUtf16:Create StringRef in UTF8 format
990  * WriteUtf16:Write the value of StringRef to char16_ T array buffer, testing Chinese
991  * @tc.type: FUNC
992  * @tc.require:  parameter
993  */
HWTEST_F_L0(JSNApiTests,StringUtf16_NewFromUtf16_Length_WriteUtf16_01)994 HWTEST_F_L0(JSNApiTests, StringUtf16_NewFromUtf16_Length_WriteUtf16_01)
995 {
996     LocalScope scope(vm_);
997     const char16_t *test = u"年度";
998     Local<StringRef> testString = StringRef::NewFromUtf16(vm_, test);
999     EXPECT_EQ(testString->Length(vm_), 2);              // 2 : length of testString("年度")
1000     char16_t buffer[3];                              // 3 : length of testString + 1
1001     EXPECT_EQ(testString->WriteUtf16(vm_, buffer, 2), 2); // 2 : length of testString("年度")
1002     GTEST_LOG_(WARNING) << "年度test =" << buffer;
1003     ASSERT_EQ(buffer[0], u'年');
1004     ASSERT_EQ(buffer[1], u'度');
1005 }
1006 
1007 /**
1008  * @tc.number: ffi_interface_api_100
1009  * @tc.name: StringUtf16_NewFromUtf16_Length_WriteUtf16_01
1010  * @tc.desc: Write the value of StringRef to char16_ T array buffer, testing non Chinese
1011  * @tc.type: FUNC
1012  * @tc.require:  parameter
1013  */
HWTEST_F_L0(JSNApiTests,StringUtf16_NewFromUtf16_Length_WriteUtf16_02)1014 HWTEST_F_L0(JSNApiTests, StringUtf16_NewFromUtf16_Length_WriteUtf16_02)
1015 {
1016     LocalScope scope(vm_);
1017     const char16_t *test = u"hello world!0?";
1018     Local<StringRef> testString = StringRef::NewFromUtf16(vm_, test);
1019 
1020     EXPECT_EQ(testString->Length(vm_), 14);
1021     char16_t buffer[15];                               // 15 : length of testString + 1
1022     EXPECT_EQ(testString->WriteUtf16(vm_, buffer, 14), 14); // 14 : length of testString("hello world!!!")
1023     ASSERT_EQ(buffer[0], u'h');
1024     ASSERT_EQ(buffer[13], u'?');
1025 }
1026 
1027 /**
1028  * @tc.number: ffi_interface_api_101
1029  * @tc.name: SetRef_IsSet_GetSize_GetTotalElements_GetValue
1030  * @tc.desc:
1031  * IsSet:Determine if it is a set container object
1032  * GetSize:Get set length size
1033  * GetTotalElements:Get the total number of set elements
1034  * GetValue:Obtain the element values of the set container according to the following table
1035  * @tc.type: FUNC
1036  * @tc.require:  parameter
1037  */
HWTEST_F_L0(JSNApiTests,SetRef_IsSet_GetSize_GetTotalElements_GetValue)1038 HWTEST_F_L0(JSNApiTests, SetRef_IsSet_GetSize_GetTotalElements_GetValue)
1039 {
1040     LocalScope scope(vm_);
1041     JSThread *thread = vm_->GetJSThread();
1042     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1043     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1044     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsSetFunction();
1045     JSHandle<JSSet> jsSet =
1046         JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1047     JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
1048     jsSet->SetLinkedSet(thread, hashSet);
1049     JSHandle<JSTaggedValue> setTag = JSHandle<JSTaggedValue>::Cast(jsSet);
1050     Local<SetRef> set = JSNApiHelper::ToLocal<SetRef>(setTag);
1051     EXPECT_TRUE(set->IsSet(vm_));
1052     JSHandle<JSTaggedValue> fristValue(factory->NewFromASCII("vlue1"));
1053     JSSet::Add(thread, jsSet, fristValue);
1054     JSSet::Add(thread, jsSet, fristValue);
1055     int32_t num = set->GetSize(vm_);
1056     int32_t num1 = set->GetTotalElements(vm_);
1057     ASSERT_EQ(num, 1);
1058     ASSERT_EQ(num1, 1);
1059     JSHandle<JSTaggedValue> secondValue(factory->NewFromASCII("vlue2"));
1060     JSSet::Add(thread, jsSet, secondValue);
1061     num = set->GetSize(vm_);
1062     num1 = set->GetTotalElements(vm_);
1063     ASSERT_EQ(num, 2);
1064     ASSERT_EQ(num1, 2);
1065     Local<JSValueRef> res1 = set->GetValue(vm_, 0);
1066     ASSERT_EQ(res1->ToString(vm_)->ToString(vm_), "vlue1");
1067     Local<JSValueRef> res2 = set->GetValue(vm_, 1);
1068     ASSERT_EQ(res2->ToString(vm_)->ToString(vm_), "vlue2");
1069 }
1070 
CreateJSSet(JSThread * thread)1071 static JSSet *CreateJSSet(JSThread *thread)
1072 {
1073     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1074     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1075     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsSetFunction();
1076     JSHandle<JSSet> set =
1077         JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1078     JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
1079     set->SetLinkedSet(thread, hashSet);
1080     return JSSet::Cast(set.GetTaggedValue().GetTaggedObject());
1081 }
1082 
1083 /**
1084  * @tc.number: ffi_interface_api_102
1085  * @tc.name: JSSetIterator_IsSetIterator_GetIndex_GetKind
1086  * @tc.desc:
1087  * IsSetIterator:Determine if it is a set iterator
1088  * GetIndex:Gets the index of the subscript position currently pointed to by the set iterator
1089  * GetKind:Obtain the index type 'key/value/key_and_value' for the set iterator
1090  * @tc.type: FUNC
1091  * @tc.require:  parameter
1092  */
HWTEST_F_L0(JSNApiTests,JSSetIterator_IsSetIterator_GetIndex_GetKind)1093 HWTEST_F_L0(JSNApiTests, JSSetIterator_IsSetIterator_GetIndex_GetKind)
1094 {
1095     LocalScope scope(vm_);
1096     JSThread *thread = vm_->GetJSThread();
1097     JSHandle<JSSet> jsSet(thread, CreateJSSet(thread));
1098     EXPECT_TRUE(*jsSet != nullptr);
1099     JSHandle<JSTaggedValue> jsTagSetIterator =
1100         JSSetIterator::CreateSetIterator(thread, JSHandle<JSTaggedValue>(jsSet), IterationKind::KEY);
1101     JSHandle<JSSetIterator> jsSetIterator1(jsTagSetIterator);
1102     EXPECT_EQ(JSTaggedValue::SameValue(jsSetIterator1->GetIteratedSet(), jsSet->GetLinkedSet()), true);
1103     Local<SetIteratorRef> setIterator = JSNApiHelper::ToLocal<SetIteratorRef>(jsTagSetIterator);
1104     EXPECT_TRUE(setIterator->IsSetIterator(vm_));
1105     EXPECT_EQ(setIterator->GetIndex(), 0U);
1106     Local<JSValueRef> resultKey = StringRef::NewFromUtf8(vm_, "keys");
1107     EXPECT_EQ(setIterator->GetKind(vm_)->ToString(vm_)->ToString(vm_), resultKey->ToString(vm_)->ToString(vm_));
1108 }
1109 
1110 /**
1111  * @tc.number: ffi_interface_api_103
1112  * @tc.name: JSValueRef_IsMapIterator
1113  * @tc.desc: Determine if it is a Map iterator
1114  * @tc.type: FUNC
1115  * @tc.require:  parameter
1116  */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsMapIterator)1117 HWTEST_F_L0(JSNApiTests, JSValueRef_IsMapIterator)
1118 {
1119     LocalScope scope(vm_);
1120     JSThread *thread = vm_->GetJSThread();
1121     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1122     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1123     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
1124     JSHandle<JSMap> jsMap =
1125         JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1126     JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
1127     jsMap->SetLinkedMap(thread, hashMap);
1128     JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
1129     JSHandle<JSTaggedValue> jsMapIteratorTag =
1130         JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY_AND_VALUE);
1131     JSHandle<JSMapIterator> jsMapIterator(jsMapIteratorTag);
1132     EXPECT_EQ(JSTaggedValue::SameValue(jsMapIterator->GetIteratedMap(), jsMap->GetLinkedMap()), true);
1133     Local<JSValueRef> mapIterator = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag);
1134     EXPECT_TRUE(mapIterator->IsMapIterator(vm_));
1135 }
1136 
1137 /**
1138  * @tc.number: ffi_interface_api_104
1139  * @tc.name: JSValueRef_IsModuleNamespaceObject
1140  * @tc.desc: Determine if it is a module space object
1141  * @tc.type: FUNC
1142  * @tc.require:  parameter
1143  */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsModuleNamespaceObject)1144 HWTEST_F_L0(JSNApiTests, JSValueRef_IsModuleNamespaceObject)
1145 {
1146     LocalScope scope(vm_);
1147     ObjectFactory *objectFactory = thread_->GetEcmaVM()->GetFactory();
1148     JSHandle<SourceTextModule> module = objectFactory->NewSourceTextModule();
1149     JSHandle<LocalExportEntry> localExportEntry1 = objectFactory->NewLocalExportEntry();
1150     SourceTextModule::AddLocalExportEntry(thread_, module, localExportEntry1, 0, 2);
1151     JSHandle<LocalExportEntry> localExportEntry2 = objectFactory->NewLocalExportEntry();
1152     SourceTextModule::AddLocalExportEntry(thread_, module, localExportEntry2, 1, 2);
1153     JSHandle<TaggedArray> localExportEntries(thread_, module->GetLocalExportEntries());
1154     CString baseFileName = "a.abc";
1155     module->SetEcmaModuleFilenameString(baseFileName);
1156     ModuleManager *moduleManager = thread_->GetCurrentEcmaContext()->GetModuleManager();
1157     moduleManager->AddResolveImportedModule(baseFileName, module.GetTaggedValue());
1158     JSHandle<ModuleNamespace> np =
1159         ModuleNamespace::ModuleNamespaceCreate(thread_, JSHandle<JSTaggedValue>::Cast(module), localExportEntries);
1160     EXPECT_TRUE(ModuleNamespace::PreventExtensions());
1161     JSHandle<JSTaggedValue> moduleNamespaceTag = JSHandle<JSTaggedValue>::Cast(np);
1162     Local<JSValueRef> moduleNamespace = JSNApiHelper::ToLocal<ModuleNamespace>(moduleNamespaceTag);
1163     ASSERT_TRUE(moduleNamespace->IsModuleNamespaceObject(vm_));
1164 }
1165 } // namespace panda::test