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 #define PROPERTY_COUNT_CASE1 10
66 #define PROPERTY_COUNT_CASE2 (PropertyAttributes::MAX_FAST_PROPS_CAPACITY + 1)
67
68 using namespace panda;
69 using namespace panda::ecmascript;
70 using namespace panda::ecmascript::kungfu;
71 static const char *TEST_KEY = "TestKey";
72 static const char *TEST_VALUE = "TestValue";
73 static const char *TEST_NUM1 = "-3.14";
74 static const char *TEST_NUM2 = "-123.3";
75
76 namespace panda::test {
77 using BuiltinsFunction = ecmascript::builtins::BuiltinsFunction;
78 using PGOProfilerManager = panda::ecmascript::pgo::PGOProfilerManager;
79 using FunctionForRef = Local<JSValueRef> (*)(JsiRuntimeCallInfo *);
80 class JSNApiTests : public testing::Test {
81 public:
SetUpTestCase()82 static void SetUpTestCase()
83 {
84 GTEST_LOG_(INFO) << "SetUpTestCase";
85 }
86
TearDownTestCase()87 static void TearDownTestCase()
88 {
89 GTEST_LOG_(INFO) << "TearDownCase";
90 }
91
SetUp()92 void SetUp() override
93 {
94 RuntimeOption option;
95 option.SetLogLevel(common::LOG_LEVEL::ERROR);
96 vm_ = JSNApi::CreateJSVM(option);
97 ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
98 thread_ = vm_->GetJSThread();
99 vm_->SetEnableForceGC(true);
100 thread_->ManagedCodeBegin();
101 }
102
TearDown()103 void TearDown() override
104 {
105 thread_->ManagedCodeEnd();
106 vm_->SetEnableForceGC(false);
107 JSNApi::DestroyJSVM(vm_);
108 }
109
TestNumberRef(T val,TaggedType expected)110 template <typename T> void TestNumberRef(T val, TaggedType expected)
111 {
112 LocalScope scope(vm_);
113 Local<NumberRef> obj = NumberRef::New(vm_, val);
114 ASSERT_TRUE(obj->IsNumber());
115 JSTaggedType res = JSNApiHelper::ToJSTaggedValue(*obj).GetRawData();
116 ASSERT_EQ(res, expected);
117 if constexpr (std::is_floating_point_v<T>) {
118 if (std::isnan(val)) {
119 ASSERT_TRUE(std::isnan(obj->Value()));
120 } else {
121 ASSERT_EQ(obj->Value(), val);
122 }
123 } else if constexpr (sizeof(T) >= sizeof(int32_t)) {
124 ASSERT_EQ(obj->IntegerValue(vm_), val);
125 } else if constexpr (std::is_signed_v<T>) {
126 ASSERT_EQ(obj->Int32Value(vm_), val);
127 } else {
128 ASSERT_EQ(obj->Uint32Value(vm_), val);
129 }
130 }
131
ConvertDouble(double val)132 TaggedType ConvertDouble(double val)
133 {
134 return base::bit_cast<JSTaggedType>(val) + JSTaggedValue::DOUBLE_ENCODE_OFFSET;
135 }
136
137 protected:
138 JSThread *thread_ = nullptr;
139 EcmaVM *vm_ = nullptr;
140 };
141
FunctionCallback(JsiRuntimeCallInfo * info)142 Local<JSValueRef> FunctionCallback(JsiRuntimeCallInfo *info)
143 {
144 EscapeLocalScope scope(info->GetVM());
145 return scope.Escape(ArrayRef::New(info->GetVM(), info->GetArgsNumber()));
146 }
147
InternalFunctionCallback(JsiRuntimeCallInfo * info)148 JSValueRef InternalFunctionCallback(JsiRuntimeCallInfo *info)
149 {
150 EscapeLocalScope scope(info->GetVM());
151 Local<JSValueRef> result = scope.Escape(ArrayRef::New(info->GetVM(), info->GetArgsNumber()));
152 return **result;
153 }
154
WeakRefCallback(EcmaVM * vm)155 void WeakRefCallback(EcmaVM *vm)
156 {
157 LocalScope scope(vm);
158 Local<ObjectRef> object = ObjectRef::New(vm);
159 Global<ObjectRef> globalObject(vm, object);
160 globalObject.SetWeak();
161 Local<ObjectRef> object1 = ObjectRef::New(vm);
162 Global<ObjectRef> globalObject1(vm, object1);
163 globalObject1.SetWeak();
164 vm->CollectGarbage(TriggerGCType::YOUNG_GC);
165 vm->CollectGarbage(TriggerGCType::OLD_GC);
166 globalObject.FreeGlobalHandleAddr();
167 }
168
ThreadCheck(const EcmaVM * vm)169 void ThreadCheck(const EcmaVM *vm)
170 {
171 EXPECT_TRUE(vm->GetJSThread()->GetThreadId() != JSThread::GetCurrentThreadId());
172 }
173
CheckReject(JsiRuntimeCallInfo * info)174 void CheckReject(JsiRuntimeCallInfo *info)
175 {
176 ASSERT_EQ(info->GetArgsNumber(), 1U);
177 Local<JSValueRef> reason = info->GetCallArgRef(0);
178 ASSERT_TRUE(reason->IsString(info->GetVM()));
179 ASSERT_EQ(Local<StringRef>(reason)->ToString(info->GetVM()), "Reject");
180 }
181
RejectCallback(JsiRuntimeCallInfo * info)182 Local<JSValueRef> RejectCallback(JsiRuntimeCallInfo *info)
183 {
184 LocalScope scope(info->GetVM());
185 CheckReject(info);
186 return JSValueRef::Undefined(info->GetVM());
187 }
188
189 /**
190 * @tc.number: ffi_interface_api_054
191 * @tc.name: NumberRef_uint32_int64
192 * @tc.desc:Define the variable input of type uint32_t and int64_t to determine whether the object is of numerical type
193 * @tc.type: FUNC
194 * @tc.require: parameter
195 */
HWTEST_F_L0(JSNApiTests,NumberRef_uint32_int64)196 HWTEST_F_L0(JSNApiTests, NumberRef_uint32_int64)
197 {
198 uint32_t input = 32;
199 int64_t input1 = 1;
200 Local<NumberRef> res = NumberRef::New(vm_, input);
201 Local<NumberRef> res1 = NumberRef::New(vm_, input1);
202 ASSERT_TRUE(res->IsNumber());
203 ASSERT_TRUE(res1->IsNumber());
204 }
205
206 /**
207 * @tc.number: ffi_interface_api_055
208 * @tc.name: NumberRef_int32_t_double
209 * @tc.desc:Define the variable input of type int32 and double to determine whether the object is of numerical type
210 * @tc.type: FUNC
211 * @tc.require: parameter
212 */
HWTEST_F_L0(JSNApiTests,NumberRef_int32_t_double)213 HWTEST_F_L0(JSNApiTests, NumberRef_int32_t_double)
214 {
215 int32_t input = -1;
216 double input1 = 1.1;
217 Local<NumberRef> res = NumberRef::New(vm_, input);
218 Local<NumberRef> res1 = NumberRef::New(vm_, input1);
219 ASSERT_TRUE(res->IsNumber());
220 ASSERT_TRUE(res1->IsNumber());
221 }
222
223 /**
224 * @tc.number: ffi_interface_api_056
225 * @tc.name: ObjectRef_Freeze
226 * @tc.desc:Execute a Freeze operation and call the JSObject:: SetIntegrity Level method to achieve immutability
227 * @tc.type: FUNC
228 * @tc.require: parameter
229 */
HWTEST_F_L0(JSNApiTests,ObjectRef_Freeze)230 HWTEST_F_L0(JSNApiTests, ObjectRef_Freeze)
231 {
232 LocalScope scope(vm_);
233 Local<ObjectRef> object = ObjectRef::New(vm_);
234 thread_ = vm_->GetJSThread();
235 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
236 JSHandle<JSTaggedValue> set = env->GetBuiltinsSetFunction();
237 object->Freeze(vm_);
238 ecmascript::ThreadManagedScope managedScope(thread_);
239 bool status = JSObject::SetIntegrityLevel(thread_, JSHandle<JSObject>::Cast(set), IntegrityLevel::FROZEN);
240 ASSERT_TRUE(status);
241 }
242
243 /**
244 * @tc.number: ffi_interface_api_057
245 * @tc.name: ObjectRef_Seal
246 * @tc.desc:The function is to perform a closed (Seal) operation by calling the JSObject::SetIntegrity Level method
247 * to ensure that its properties cannot be modified or deleted, and to set the integrity level of
248 * the object to SEALED
249 * @tc.type: FUNC
250 * @tc.require: parameter
251 */
HWTEST_F_L0(JSNApiTests,ObjectRef_Seal)252 HWTEST_F_L0(JSNApiTests, ObjectRef_Seal)
253 {
254 LocalScope scope(vm_);
255 Local<ObjectRef> object = ObjectRef::New(vm_); // 创建一个新的空对象
256 thread_ = vm_->GetJSThread();
257 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
258 JSHandle<JSTaggedValue> set = env->GetBuiltinsSetFunction();
259 object->Seal(vm_); // 尝试将对象封闭
260 ecmascript::ThreadManagedScope managedScope(thread_);
261 bool status = JSObject::SetIntegrityLevel(thread_, JSHandle<JSObject>::Cast(set), IntegrityLevel::SEALED);
262 ASSERT_TRUE(status);
263 }
264
265 /**
266 * @tc.number: ffi_interface_api_058
267 * @tc.name: ObjectRef_GetAllPropertyNames
268 * @tc.desc:Use the GetAllPropertyNames method to obtain all property names of the object and return an ArrayRef object.
269 * @tc.type: FUNC
270 * @tc.require: parameter
271 */
HWTEST_F_L0(JSNApiTests,ObjectRef_GetAllPropertyNames)272 HWTEST_F_L0(JSNApiTests, ObjectRef_GetAllPropertyNames)
273 {
274 LocalScope scope(vm_);
275 Local<ObjectRef> object = ObjectRef::New(vm_);
276 uint32_t filter = 3;
277 Local<ArrayRef> res = object->GetAllPropertyNames(vm_, filter);
278 ASSERT_FALSE(res->IsBigInt(vm_));
279 ASSERT_TRUE(res->IsArray(vm_));
280 }
281
282 /**
283 * @tc.number: ffi_interface_api_059
284 * @tc.name: GetIndex
285 * @tc.desc:Call the GetIndex() function to obtain the index value and use EXCECT_ EQ()
286 * assertion is used to verify whether the result is 0, which is the initial index value.
287 * @tc.type: FUNC
288 * @tc.require: parameter
289 */
HWTEST_F_L0(JSNApiTests,GetIndex)290 HWTEST_F_L0(JSNApiTests, GetIndex)
291 {
292 ecmascript::ThreadManagedScope managedScope(vm_->GetJSThread());
293 LocalScope scope(vm_);
294 JSThread *thread = vm_->GetJSThread();
295 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
296 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
297 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
298 JSHandle<JSMap> jsMap =
299 JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
300 JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
301 jsMap->SetLinkedMap(thread, hashMap);
302 JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
303 JSHandle<JSTaggedValue> jsMapIteratorTag4 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::VALUE);
304 JSHandle<JSMapIterator> jsMapIterator4(jsMapIteratorTag4);
305 Local<MapIteratorRef> mapIterator4 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag4);
306 int32_t res4 = mapIterator4->GetIndex();
307 EXPECT_EQ(0, res4);
308 }
309
310 /**
311 * @tc.number: ffi_interface_api_060
312 * @tc.name: GetKind_entries_values_keys
313 * @tc.desc:This test case is mainly used to verify whether the GetKind method of the JSMapIterator object can
314 * correctly return the expected type.
315 * @tc.type: FUNC
316 * @tc.require: parameter
317 */
HWTEST_F_L0(JSNApiTests,GetKind_entries_values_keys)318 HWTEST_F_L0(JSNApiTests, GetKind_entries_values_keys)
319 {
320 LocalScope scope(vm_);
321 JSThread *thread = vm_->GetJSThread();
322 ecmascript::ThreadManagedScope managedScope(thread);
323 std::string expectedResult = "entries";
324 std::string valuesResult = "values";
325 std::string keysResult = "keys";
326 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
327 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
328 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
329 JSHandle<JSMap> jsMap =
330 JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
331 JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
332 jsMap->SetLinkedMap(thread, hashMap);
333 JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
334 JSHandle<JSTaggedValue> jsMapIteratorTag =
335 JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY_AND_VALUE);
336 JSHandle<JSMapIterator> jsMapIterator(jsMapIteratorTag);
337 EXPECT_EQ(JSTaggedValue::SameValue(thread, jsMapIterator->GetIteratedMap(thread), jsMap->GetLinkedMap(thread)),
338 true);
339 Local<MapIteratorRef> mapIterator = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag);
340 Local<JSValueRef> res = mapIterator->GetKind(vm_);
341 EXPECT_EQ(expectedResult, res->ToString(vm_)->ToString(vm_));
342 EXPECT_TRUE(mapIterator->IsMapIterator(vm_));
343 JSHandle<JSTaggedValue> jsMapIteratorTag1 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY);
344 Local<MapIteratorRef> mapIterator1 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag1);
345 Local<JSValueRef> res1 = mapIterator1->GetKind(vm_);
346 EXPECT_EQ(keysResult, res1->ToString(vm_)->ToString(vm_));
347 JSHandle<JSTaggedValue> jsMapIteratorTag2 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::VALUE);
348 Local<MapIteratorRef> mapIterator2 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag2);
349 Local<JSValueRef> res2 = mapIterator2->GetKind(vm_);
350 EXPECT_EQ(valuesResult, res2->ToString(vm_)->ToString(vm_));
351 }
352
353 /**
354 * @tc.number: ffi_interface_api_061
355 * @tc.name: GetKind_001
356 * @tc.desc:This test case is mainly used to verify whether the GetKind method of the JSMapIterator object can
357 * correctly return the expected type (in this example, the expected type is "keys").
358 * @tc.type: FUNC
359 * @tc.require: parameter
360 */
HWTEST_F_L0(JSNApiTests,GetKind_001)361 HWTEST_F_L0(JSNApiTests, GetKind_001)
362 {
363 ecmascript::ThreadManagedScope managedScope(vm_->GetJSThread());
364 LocalScope scope(vm_);
365 JSThread *thread = vm_->GetJSThread();
366 std::string keysResult = "keys";
367 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
368 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
369 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
370 JSHandle<JSMap> jsMap =
371 JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
372 JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
373 jsMap->SetLinkedMap(thread, hashMap);
374 JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
375 JSHandle<JSTaggedValue> jsMapIteratorTag1 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY);
376 JSHandle<JSMapIterator> jsMapIterator1(jsMapIteratorTag1);
377 EXPECT_EQ(JSTaggedValue::SameValue(thread, jsMapIterator1->GetIteratedMap(thread), jsMap->GetLinkedMap(thread)),
378 true);
379 Local<MapIteratorRef> mapIterator1 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag1);
380 Local<JSValueRef> res1 = mapIterator1->GetKind(vm_);
381 EXPECT_EQ(keysResult, res1->ToString(vm_)->ToString(vm_));
382 }
383
384 /**
385 * @tc.number: ffi_interface_api_062
386 * @tc.name: GetKind_002
387 * @tc.desc:This test case is mainly used to verify whether the GetKind method of the JSMapIterator object can
388 * correctly return the expected type (in this example, the expected type is "values").
389 * @tc.type: FUNC
390 * @tc.require: parameter
391 */
HWTEST_F_L0(JSNApiTests,GetKind_002)392 HWTEST_F_L0(JSNApiTests, GetKind_002)
393 {
394 ecmascript::ThreadManagedScope managedScope(vm_->GetJSThread());
395 LocalScope scope(vm_);
396 JSThread *thread = vm_->GetJSThread();
397 std::string valuesResult = "values";
398 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
399 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
400 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
401 JSHandle<JSMap> jsMap =
402 JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
403 JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
404 jsMap->SetLinkedMap(thread, hashMap);
405 JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
406 JSHandle<JSTaggedValue> jsMapIteratorTag2 = JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::VALUE);
407 JSHandle<JSMapIterator> jsMapIterator2(jsMapIteratorTag2);
408 EXPECT_EQ(JSTaggedValue::SameValue(thread, jsMapIterator2->GetIteratedMap(thread), jsMap->GetLinkedMap(thread)),
409 true);
410 Local<MapIteratorRef> mapIterator2 = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag2);
411 Local<JSValueRef> res2 = mapIterator2->GetKind(vm_);
412 EXPECT_EQ(valuesResult, res2->ToString(vm_)->ToString(vm_));
413 }
414
415 /**
416 * @tc.number: ffi_interface_api_063
417 * @tc.name: GetKind_003
418 * @tc.desc:Calling the GetKind method to obtain the iteration type KEY_AND_VALUE Compare with the string
419 * variable 'entries' to assert whether the expected result is the same as the actual result
420 * @tc.type: FUNC
421 * @tc.require: parameter
422 */
HWTEST_F_L0(JSNApiTests,GetKind_003)423 HWTEST_F_L0(JSNApiTests, GetKind_003)
424 {
425 ecmascript::ThreadManagedScope managedScope(vm_->GetJSThread());
426 LocalScope scope(vm_);
427 JSThread *thread = vm_->GetJSThread();
428 std::string expectedResult = "entries";
429 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
430 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
431 // 从全局环境(GlobalEnv)中获取内置的Map构造函数
432 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
433 // 使用构造函数创建一个新的JSMap对象
434 JSHandle<JSMap> jsMap =
435 JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
436 JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
437 // 将创建的LinkedHashMap设置为JSMap的内部数据结构
438 jsMap->SetLinkedMap(thread, hashMap);
439 // 将jsMap转换为JSTaggedValue的句柄
440 JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
441 // 创建一个新的JSMapIterator对象,并将其迭代方式设置为KEY_AND_VALUE
442 JSHandle<JSTaggedValue> jsMapIteratorTag =
443 JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY_AND_VALUE);
444 JSHandle<JSMapIterator> jsMapIterator(jsMapIteratorTag);
445 EXPECT_EQ(JSTaggedValue::SameValue(thread, jsMapIterator->GetIteratedMap(thread), jsMap->GetLinkedMap(thread)),
446 true);
447 // 将jsMapIteratorTag转换为本地代码可以使用的MapIteratorRef类型
448 Local<MapIteratorRef> mapIterator = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag);
449 Local<JSValueRef> res = mapIterator->GetKind(vm_);
450 EXPECT_EQ(expectedResult, res->ToString(vm_)->ToString(vm_));
451 EXPECT_TRUE(mapIterator->IsMapIterator(vm_));
452 }
453
454 /*
455 * @tc.number: ffi_interface_api_064
456 * @tc.name: GetProperty_IsFunction
457 * @tc.desc: Verify if the GetProperty function of the JavaScript virtual machine correctly returns a
458 * function as the property of the 'Number' key.
459 * @tc.type: FUNC
460 * @tc.require: parameter
461 */
HWTEST_F_L0(JSNApiTests,GetProperty_IsFunction)462 HWTEST_F_L0(JSNApiTests, GetProperty_IsFunction)
463 {
464 LocalScope scope(vm_);
465 Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm_);
466 ASSERT_FALSE(globalObject.IsEmpty());
467 ASSERT_TRUE(globalObject->IsObject(vm_));
468
469 Local<ObjectRef> key = StringRef::NewFromUtf8(vm_, "Number");
470 Local<ObjectRef> property = globalObject->Get(vm_, key);
471 ASSERT_TRUE(property->IsFunction(vm_));
472 }
473
474 /**
475 * @tc.number: ffi_interface_api_065
476 * @tc.name: NewClassFunction
477 * @tc.desc:Check if the function created through the NewClassFunction method meets the specifications of the class
478 * constructor, and obtain and verify the properties of the function.
479 * @tc.type: FUNC
480 * @tc.require: parameter
481 */
HWTEST_F_L0(JSNApiTests,NewClassFunction)482 HWTEST_F_L0(JSNApiTests, NewClassFunction)
483 {
484 LocalScope scope(vm_);
485 Local<FunctionRef> cls = FunctionRef::NewClassFunction(vm_, FunctionCallback, nullptr, nullptr);
486
487 JSHandle<JSTaggedValue> obj = JSNApiHelper::ToJSHandle(Local<JSValueRef>(cls));
488 // 断言obj是一个类构造函数
489 ASSERT_TRUE(obj->IsClassConstructor());
490 // GetPropertyInlinedProps方法获取内联属性的方法,CLASS_PROTOTYPE_INLINE_PROPERTY_INDEX类原型的内联属性索引
491 JSTaggedValue res =
492 JSHandle<JSFunction>(obj)->GetPropertyInlinedProps(thread_, JSFunction::CLASS_PROTOTYPE_INLINE_PROPERTY_INDEX);
493 // 断言获取的属性是一个内部访问器
494 ASSERT_TRUE(res.IsInternalAccessor());
495 }
496
497 /**
498 * @tc.number: ffi_interface_api_066
499 * @tc.name: PromiseRef_Finally_IsPromise
500 * @tc.desc:FunctionRef:: New Create a reject callback function reject. The function of the code is to test the
501 * behavior of the Finally and Then methods of the Promise object
502 * in various situations, ensuring that they all return Promise objects and meet the expected behavior.
503 * @tc.type: FUNC
504 * @tc.require: parameter
505 */
HWTEST_F_L0(JSNApiTests,PromiseRef_Finally_IsPromise)506 HWTEST_F_L0(JSNApiTests, PromiseRef_Finally_IsPromise)
507 {
508 LocalScope scope(vm_);
509 Local<PromiseCapabilityRef> capability = PromiseCapabilityRef::New(vm_);
510
511 Local<PromiseRef> gitpromise = capability->GetPromise(vm_);
512 Local<FunctionRef> rejectcallback = FunctionRef::New(vm_, RejectCallback);
513 Local<PromiseRef> catchPromise = gitpromise->Finally(vm_, rejectcallback);
514 ASSERT_TRUE(gitpromise->IsPromise(vm_));
515 ASSERT_TRUE(catchPromise->IsPromise(vm_));
516 Local<PromiseRef> catchPromise2 = gitpromise->Then(vm_, rejectcallback, rejectcallback);
517 ASSERT_TRUE(catchPromise2->IsPromise(vm_));
518 Local<FunctionRef> functioncallback = FunctionRef::New(vm_, FunctionCallback);
519 ASSERT_TRUE(!functioncallback.IsEmpty());
520 Local<PromiseRef> catchPromise3 = gitpromise->Then(vm_, functioncallback);
521 ASSERT_TRUE(catchPromise3->IsPromise(vm_));
522 }
523
524 /**
525 * @tc.number: ffi_interface_api_067
526 * @tc.name: IsBuffer
527 * @tc.desc: Construct a BufferRef function to determine whether it is a Buffer
528 * @tc.type: FUNC
529 * @tc.require: parameter
530 */
HWTEST_F_L0(JSNApiTests,IsBuffer)531 HWTEST_F_L0(JSNApiTests, IsBuffer)
532 {
533 LocalScope scope(vm_);
534 const int32_t length = 15;
535 Local<BufferRef> buffer = BufferRef::New(vm_, length);
536 ASSERT_TRUE(buffer->IsBuffer(vm_));
537 }
538
539 /**
540 * @tc.number: ffi_interface_api_068
541 * @tc.name: IsDataView
542 * @tc.desc: Construct a BufferRef function to determine whether it is a dataView
543 * @tc.type: FUNC
544 * @tc.require: parameter
545 */
HWTEST_F_L0(JSNApiTests,IsDataView)546 HWTEST_F_L0(JSNApiTests, IsDataView)
547 {
548 LocalScope scope(vm_);
549 const int32_t length = 15;
550 Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
551 Local<DataViewRef> dataView = DataViewRef::New(vm_, arrayBuffer, 5, 7);
552 ASSERT_TRUE(dataView->IsDataView(vm_));
553 }
554
555 /**
556 * @tc.number: ffi_interface_api_071
557 * @tc.name: IsSharedArrayBuffer
558 * @tc.desc: Construct a BufferRef function to determine whether it is a SharedArrayBuffer
559 * @tc.type: FUNC
560 * @tc.require: parameter
561 */
HWTEST_F_L0(JSNApiTests,IsSharedArrayBuffer)562 HWTEST_F_L0(JSNApiTests, IsSharedArrayBuffer)
563 {
564 LocalScope scope(vm_);
565 const int32_t length = 15;
566 Local<JSValueRef> sharedArrayBuffer = ArrayBufferRef::New(vm_, length);
567 ASSERT_FALSE(sharedArrayBuffer->IsSharedArrayBuffer(vm_));
568 }
569
570
571 /**
572 * @tc.number: ffi_interface_api_072
573 * @tc.name: IsTrue
574 * @tc.desc: Construct a BufferRef function to determine whether it is a IsTrue
575 * @tc.type: FUNC
576 * @tc.require: parameter
577 */
HWTEST_F_L0(JSNApiTests,IsTrue)578 HWTEST_F_L0(JSNApiTests, IsTrue)
579 {
580 LocalScope scope(vm_);
581 Local<JSValueRef> b = JSValueRef::True(vm_);
582 ASSERT_TRUE(b->IsTrue());
583 }
584
585
586 /**
587 * @tc.number: ffi_interface_api_073
588 * @tc.name: IsFalse
589 * @tc.desc: Construct a BufferRef function to determine whether it is a IsFalse
590 * @tc.type: FUNC
591 * @tc.require: parameter
592 */
HWTEST_F_L0(JSNApiTests,IsFalse)593 HWTEST_F_L0(JSNApiTests, IsFalse)
594 {
595 LocalScope scope(vm_);
596 Local<JSValueRef> c = JSValueRef::False(vm_);
597 ASSERT_TRUE(c->IsFalse());
598 }
599
600 /**
601 * @tc.number: ffi_interface_api_074
602 * @tc.name: IsConstructor
603 * @tc.desc: Construct a BufferRef function to determine whether it is a Constructor
604 * @tc.require: parameter
605 */
HWTEST_F_L0(JSNApiTests,IsConstructor)606 HWTEST_F_L0(JSNApiTests, IsConstructor)
607 {
608 LocalScope scope(vm_);
609 Local<FunctionRef> target = FunctionRef::New(vm_, FunctionCallback);
610 ASSERT_FALSE(target->IsConstructor(vm_));
611 }
612
613
614 /**
615 * @tc.number: ffi_interface_api_076
616 * @tc.name: GetOwnProperty
617 * @tc.desc: Construct a BufferRef function to determine whether it is a GetOwnProperty not Construct function
618 * @tc.type: FUNC
619 * @tc.require: parameter
620 */
HWTEST_F_L0(JSNApiTests,GetOwnProperty)621 HWTEST_F_L0(JSNApiTests, GetOwnProperty)
622 {
623 LocalScope scope(vm_);
624 Local<ObjectRef> object = ObjectRef::New(vm_);
625 Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, TEST_KEY);
626 Local<JSValueRef> value = ObjectRef::New(vm_);
627 PropertyAttribute attribute(value, true, true, true);
628
629 ASSERT_TRUE(object->DefineProperty(vm_, key, attribute));
630 Local<JSValueRef> value1 = object->Get(vm_, key);
631 ASSERT_TRUE(value->IsStrictEquals(vm_, value1));
632 }
633
634 /**
635 * @tc.number: ffi_interface_api_078
636 * @tc.name: IsTreeMap
637 * @tc.desc: Used to verify whether the given object is a TreeMap.
638 * @tc.type: FUNC
639 * @tc.require: parameter
640 */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsTreeMap)641 HWTEST_F_L0(JSNApiTests, JSValueRef_IsTreeMap)
642 {
643 LocalScope scope(vm_);
644 JSThread *thread = vm_->GetJSThread();
645 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
646 auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
647 JSHandle<JSTaggedValue> proto = globalEnv->GetObjectFunctionPrototype();
648 JSHandle<JSHClass> mapClass = factory->NewEcmaHClass(JSAPITreeMap::SIZE, JSType::JS_API_TREE_MAP, proto);
649 JSHandle<JSAPITreeMap> jsTreeMap = JSHandle<JSAPITreeMap>::Cast(factory->NewJSObjectWithInit(mapClass));
650 JSHandle<TaggedTreeMap> treeMap(thread, TaggedTreeMap::Create(thread));
651 jsTreeMap->SetTreeMap(thread, treeMap);
652 JSHandle<JSTaggedValue> argumentTag = JSHandle<JSTaggedValue>::Cast(jsTreeMap);
653 Local<JSValueRef> TreeMap = JSNApiHelper::ToLocal<JSAPITreeSet>(argumentTag);
654 EXPECT_TRUE(TreeMap->IsTreeMap(vm_));
655 }
656
657 /**
658 * @tc.number: ffi_interface_api_079
659 * @tc.name: IsTreeSet
660 * @tc.desc: Used to verify whether the given object is a TreeSet.
661 * @tc.type: FUNC
662 * @tc.require: parameter
663 */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsTreeSet)664 HWTEST_F_L0(JSNApiTests, JSValueRef_IsTreeSet)
665 {
666 LocalScope scope(vm_);
667 JSThread *thread = vm_->GetJSThread();
668 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
669 auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
670 JSHandle<JSTaggedValue> proto = globalEnv->GetObjectFunctionPrototype();
671 JSHandle<JSHClass> setClass = factory->NewEcmaHClass(JSAPITreeSet::SIZE, JSType::JS_API_TREE_SET, proto);
672 JSHandle<JSAPITreeSet> jsTreeSet = JSHandle<JSAPITreeSet>::Cast(factory->NewJSObjectWithInit(setClass));
673 JSHandle<TaggedTreeSet> treeSet(thread, TaggedTreeSet::Create(thread));
674 jsTreeSet->SetTreeSet(thread, treeSet);
675 JSHandle<JSTaggedValue> argumentTag = JSHandle<JSTaggedValue>::Cast(jsTreeSet);
676 Local<JSValueRef> TreeSet = JSNApiHelper::ToLocal<JSAPITreeSet>(argumentTag);
677 EXPECT_TRUE(TreeSet->IsTreeSet(vm_));
678 }
679
680 /**
681 * @tc.number: ffi_interface_api_080
682 * @tc.name: IsVector
683 * @tc.desc: Used to verify whether the given object is a Vector.
684 * @tc.type: FUNC
685 * @tc.require: parameter
686 */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsVector)687 HWTEST_F_L0(JSNApiTests, JSValueRef_IsVector)
688 {
689 LocalScope scope(vm_);
690 JSThread *thread = vm_->GetJSThread();
691 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
692 auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
693 JSHandle<JSTaggedValue> proto = globalEnv->GetObjectFunctionPrototype();
694 JSHandle<JSHClass> vectorClass = factory->NewEcmaHClass(JSAPIVector::SIZE, JSType::JS_API_VECTOR, proto);
695 JSHandle<JSAPIVector> jsVector = JSHandle<JSAPIVector>::Cast(factory->NewJSObjectWithInit(vectorClass));
696 jsVector->SetLength(0);
697 JSHandle<JSTaggedValue> argumentTag = JSHandle<JSTaggedValue>::Cast(jsVector);
698 Local<JSValueRef> Vector = JSNApiHelper::ToLocal<JSAPIVector>(argumentTag);
699 EXPECT_TRUE(Vector->IsVector(vm_));
700 }
701
702 /**
703 * @tc.number: ffi_interface_api_081
704 * @tc.name: IsJSArray
705 * @tc.desc: Used to verify whether the given object is a JSArray.
706 * @tc.type: FUNC
707 * @tc.require: parameter
708 */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsJSArray)709 HWTEST_F_L0(JSNApiTests, JSValueRef_IsJSArray)
710 {
711 LocalScope scope(vm_);
712 JSHandle<JSTaggedValue> jsArrayTag = JSArray::ArrayCreate(vm_->GetJSThread(), JSTaggedNumber(0));
713 Local<JSValueRef> jsArray = JSNApiHelper::ToLocal<JSTypedArray>(jsArrayTag);
714 EXPECT_TRUE(jsArray->IsJSArray(vm_));
715 Local<JSValueRef> array = JSNApiHelper::ToLocal<ArrayRef>(jsArrayTag);
716 EXPECT_TRUE(array->IsArray(vm_));
717 }
718
719 /**
720 * @tc.number: ffi_interface_api_082
721 * @tc.name: IsMap
722 * @tc.desc: Used to verify whether the given object is a map container.
723 * @tc.type: FUNC
724 * @tc.require: parameter
725 */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsMap)726 HWTEST_F_L0(JSNApiTests, JSValueRef_IsMap)
727 {
728 LocalScope scope(vm_);
729 Local<MapRef> map = MapRef::New(vm_);
730 EXPECT_TRUE(map->IsMap(vm_));
731 }
732
733 /**
734 * @tc.number: ffi_interface_api_083
735 * @tc.name: IsSet
736 * @tc.desc: Used to verify whether the given object is a Set container.
737 * @tc.type: FUNC
738 * @tc.require: parameter
739 */
HWTEST_F_L0(JSNApiTests,SetRef_IsSet)740 HWTEST_F_L0(JSNApiTests, SetRef_IsSet)
741 {
742 LocalScope scope(vm_);
743 JSThread *thread = vm_->GetJSThread();
744 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
745 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
746 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsSetFunction();
747 JSHandle<JSSet> set =
748 JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
749 JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
750 set->SetLinkedSet(thread, hashSet);
751 JSHandle<JSTaggedValue> setTag = JSHandle<JSTaggedValue>::Cast(set);
752 Local<SetRef> Set = JSNApiHelper::ToLocal<SetRef>(setTag);
753 EXPECT_TRUE(Set->IsSet(vm_));
754 }
755
756 /**
757 * @tc.number: ffi_interface_api_084
758 * @tc.name: ObjectRef_NEW
759 * @tc.desc: Creating a new Object Ref object is not NULL if successfully created.
760 * @tc.type: FUNC
761 * @tc.require: parameter
762 */
HWTEST_F_L0(JSNApiTests,ObjectRef_NEW)763 HWTEST_F_L0(JSNApiTests, ObjectRef_NEW)
764 {
765 LocalScope scope(vm_);
766 Local<ObjectRef> object = ObjectRef::New(vm_);
767 ASSERT_FALSE(object.IsNull());
768 }
769
770 /**
771 * @tc.number: ffi_interface_api_085
772 * @tc.name: IsFalse
773 * @tc.desc: Used to verify whether the given object is false.
774 * @tc.type: FUNC
775 * @tc.require: parameter
776 */
HWTEST_F_L0(JSNApiTests,JSNAPI_IsFalse)777 HWTEST_F_L0(JSNApiTests, JSNAPI_IsFalse)
778 {
779 LocalScope scope(vm_);
780 Local<PrimitiveRef> res = JSValueRef::False(vm_);
781 EXPECT_TRUE(res->IsFalse());
782 }
783
784 /**
785 * @tc.number: ffi_interface_api_086
786 * @tc.name: GetDescription
787 * @tc.desc: Used to verify whether the object used to obtain descriptive information was successful.
788 * @tc.type: FUNC
789 * @tc.require: parameter
790 */
HWTEST_F_L0(JSNApiTests,SymbolRef_GetDescription)791 HWTEST_F_L0(JSNApiTests, SymbolRef_GetDescription)
792 {
793 LocalScope scope(vm_);
794 Local<StringRef> description = StringRef::NewFromUtf8(vm_, "test");
795 Local<SymbolRef> symbol = SymbolRef::New(vm_, description);
796 Local<StringRef> desc = symbol->GetDescription(vm_);
797 EXPECT_EQ(description->ToString(vm_), desc->ToString(vm_));
798 EXPECT_FALSE(symbol.IsNull());
799 EXPECT_FALSE(description.IsEmpty());
800 }
801
802 /**
803 * @tc.number: ffi_interface_api_087
804 * @tc.name: ArrayRefNew_uint32Length_SetValueAt_GetValueAt
805 * @tc.desc: Used to verify whether obtaining the length of the array, setting the value of the specified
806 * index position of the array, and obtaining the value of the specified index position of the
807 * array were successful
808 * @tc.type: FUNC
809 * @tc.require: parameter
810 */
HWTEST_F_L0(JSNApiTests,ArrayRefNew_uint32Length_SetValueAt_GetValueAt)811 HWTEST_F_L0(JSNApiTests, ArrayRefNew_uint32Length_SetValueAt_GetValueAt)
812 {
813 LocalScope scope(vm_);
814 Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm_);
815 ASSERT_FALSE(globalObject.IsEmpty());
816 ASSERT_TRUE(globalObject->IsObject(vm_));
817 Local<ArrayRef> property = ArrayRef::New(vm_, 3); // 3 : length
818 ASSERT_TRUE(property->IsArray(vm_));
819 ASSERT_EQ(property->Length(vm_), 3); // 3 : test case of input
820 uint32_t index = 1;
821 Local<JSValueRef> value = ObjectRef::New(vm_);
822 bool result = property->SetValueAt(vm_, globalObject, index, value);
823 ASSERT_TRUE(result);
824 Local<JSValueRef> value1 = property->GetValueAt(vm_, globalObject, index);
825 ASSERT_FALSE(value1.IsNull());
826 }
827
828 /**
829 * @tc.number: ffi_interface_api_088
830 * @tc.name: WeakSetRef_GetSize_GetTotalElements_GetValue
831 * @tc.desc: Used to verify whether the size of the weakset for obtaining settings, the total number of
832 * elements obtained, and the value of the specified index position obtained were successful
833 * @tc.type: FUNC
834 * @tc.require: parameter
835 */
HWTEST_F_L0(JSNApiTests,WeakSetRef_GetSize_GetTotalElements_GetValue)836 HWTEST_F_L0(JSNApiTests, WeakSetRef_GetSize_GetTotalElements_GetValue)
837 {
838 LocalScope scope(vm_);
839 JSThread *thread = vm_->GetJSThread();
840 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
841 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
842 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsWeakSetFunction();
843 JSHandle<JSWeakSet> weakSet =
844 JSHandle<JSWeakSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
845 JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
846 weakSet->SetLinkedSet(thread, hashSet);
847 JSHandle<JSTaggedValue> weakSetTag = JSHandle<JSTaggedValue>::Cast(weakSet);
848 Local<JSValueRef> set1 = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
849 EXPECT_TRUE(set1->IsWeakSet(vm_));
850 Local<WeakSetRef> set = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
851 JSHandle<JSTaggedValue> value(factory->NewFromASCII("value"));
852 JSWeakSet::Add(thread, weakSet, value);
853 int32_t num = set->GetSize(vm_);
854 int32_t num1 = set->GetTotalElements(vm_);
855 ASSERT_EQ(num, 1);
856 ASSERT_EQ(num1, 1);
857 Local<JSValueRef> res2 = set->GetValue(vm_, 0);
858 ASSERT_EQ(res2->ToString(vm_)->ToString(vm_), "value");
859 }
860
861 /**
862 * @tc.number: ffi_interface_api_089
863 * @tc.name: GetOwnPropertyNames
864 * @tc.desc: An array of self owned property names used to validate the acquisition of objects.
865 * @tc.type: FUNC
866 * @tc.require: parameter
867 */
HWTEST_F_L0(JSNApiTests,ObjectRef_GetOwnPropertyNames)868 HWTEST_F_L0(JSNApiTests, ObjectRef_GetOwnPropertyNames)
869 {
870 LocalScope scope(vm_);
871 Local<ObjectRef> object = ObjectRef::New(vm_);
872 Local<ArrayRef> res = object->GetOwnPropertyNames(vm_);
873 EXPECT_TRUE(res->IsArray(vm_));
874 }
875
876 /**
877 * @tc.number: ffi_interface_api_090
878 * @tc.name: SetBundle
879 * @tc.desc: Used to verify whether the initialization resource package was successfully set.
880 * @tc.type: FUNC
881 * @tc.require: parameter
882 */
HWTEST_F_L0(JSNApiTests,SetBundle)883 HWTEST_F_L0(JSNApiTests, SetBundle)
884 {
885 LocalScope scope(vm_);
886 bool value = true;
887 JSNApi::SetBundle(vm_, value);
888 bool res = JSNApi::IsBundle(vm_);
889 EXPECT_TRUE(res);
890 }
891
892 /**
893 * @tc.number: ffi_interface_api_091
894 * @tc.name: SetMockModuleList
895 * @tc.desc: Used to verify whether the function of setting the map container module was successful.
896 * @tc.type: FUNC
897 * @tc.require: parameter
898 */
HWTEST_F_L0(JSNApiTests,JSNApi_SetMockModuleList)899 HWTEST_F_L0(JSNApiTests, JSNApi_SetMockModuleList)
900 {
901 LocalScope scope(vm_);
902 std::map<std::string, std::string> str = { { TEST_NUM1, TEST_NUM2 } };
903 JSNApi::SetMockModuleList(vm_, str);
904 ASSERT_EQ(std::string(vm_->GetMockModule(TEST_NUM1)), TEST_NUM2);
905 }
906
907 /**
908 * @tc.number: ffi_interface_api_092
909 * @tc.name: SetSourceMapTranslateCallback
910 * @tc.desc: Whether the source mapping translation callback function used to verify the settings.
911 * was successfully set
912 * @tc.type: FUNC
913 * @tc.require: parameter
914 */
HWTEST_F_L0(JSNApiTests,JSNAPI_SetSourceMapTranslateCallback)915 HWTEST_F_L0(JSNApiTests, JSNAPI_SetSourceMapTranslateCallback)
916 {
917 LocalScope scope(vm_);
918 SourceMapTranslateCallback tag { nullptr };
919 JSNApi::SetSourceMapTranslateCallback(vm_, tag);
920 SourceMapTranslateCallback cur = vm_->GetSourceMapTranslateCallback();
921 ASSERT_EQ(nullptr, cur);
922 }
923
924 /**
925 * @tc.number: ffi_interface_api_093
926 * @tc.name: ExecutePendingJob
927 * @tc.desc: Used to verify whether the pending task has been successfully executed.
928 * @tc.type: FUNC
929 * @tc.require: parameter
930 */
HWTEST_F_L0(JSNApiTests,JSNApi_ExecutePendingJob)931 HWTEST_F_L0(JSNApiTests, JSNApi_ExecutePendingJob)
932 {
933 LocalScope scope(vm_);
934 JSNApi::ExecutePendingJob(vm_);
935 bool res = EcmaVM::ConstCast(vm_)->ExecutePromisePendingJob();
936 EXPECT_TRUE(res);
937 }
938
939 /**
940 * @tc.number: ffi_interface_api_096
941 * @tc.name: IsRegExp
942 * @tc.desc: Used to determine whether a given object is a regular expression.
943 * @tc.type: FUNC
944 * @tc.require: parameter
945 */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsRegExp)946 HWTEST_F_L0(JSNApiTests, JSValueRef_IsRegExp)
947 {
948 LocalScope scope(vm_);
949 JSThread *thread = vm_->GetJSThread();
950 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
951 auto globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
952 JSHandle<JSTaggedValue> proto = globalEnv->GetObjectFunctionPrototype();
953 JSHandle<JSHClass> jSRegExpClass = factory->NewEcmaHClass(JSRegExp::SIZE, JSType::JS_REG_EXP, proto);
954 JSHandle<JSRegExp> jSRegExp = JSHandle<JSRegExp>::Cast(factory->NewJSObject(jSRegExpClass));
955 jSRegExp->SetByteCodeBuffer(thread, JSTaggedValue::Undefined());
956 jSRegExp->SetOriginalSource(thread, JSTaggedValue::Undefined());
957 jSRegExp->SetGroupName(thread, JSTaggedValue::Undefined());
958 jSRegExp->SetOriginalFlags(thread, JSTaggedValue(0));
959 jSRegExp->SetLength(0);
960 JSHandle<JSTaggedValue> argumentTag = JSHandle<JSTaggedValue>::Cast(jSRegExp);
961 Local<JSValueRef> regexp = JSNApiHelper::ToLocal<JSRegExp>(argumentTag);
962 EXPECT_TRUE(regexp->IsRegExp(vm_));
963 }
964
965 /**
966 * @tc.number: ffi_interface_api_097
967 * @tc.name: GetAndClearUncaughtException
968 * @tc.desc: Used to verify the success of obtaining and clearing uncaught exceptions
969 * @tc.type: FUNC
970 * @tc.require: parameter
971 */
HWTEST_F_L0(JSNApiTests,GetAndClearUncaughtException)972 HWTEST_F_L0(JSNApiTests, GetAndClearUncaughtException)
973 {
974 LocalScope scope(vm_);
975 TryCatch tryCatch(vm_);
976 EXPECT_FALSE(tryCatch.HasCaught());
977 Local<StringRef> message = StringRef::NewFromUtf8(vm_, "ErrorTest");
978 Local<JSValueRef> error = Exception::Error(vm_, message);
979 EXPECT_TRUE(error->IsError(vm_));
980 JSNApi::ThrowException(vm_, error);
981 EXPECT_TRUE(vm_->GetJSThread()->HasPendingException());
982 JSNApi::GetAndClearUncaughtException(vm_);
983 EXPECT_FALSE(vm_->GetJSThread()->HasPendingException());
984 }
985
986 /**
987 * @tc.number: ffi_interface_api_098
988 * @tc.name: IsJSPrimitiveNumber
989 * @tc.desc: Verify if the given JSValueRef object is a primativenumber.
990 * @tc.type: FUNC
991 * @tc.require: parameter
992 */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsJSPrimitiveNumber)993 HWTEST_F_L0(JSNApiTests, JSValueRef_IsJSPrimitiveNumber)
994 {
995 LocalScope scope(vm_);
996 ObjectFactory *factory = vm_->GetFactory();
997 JSHandle<JSTaggedValue> jstagvalue;
998 JSHandle<JSPrimitiveRef> jsprimitive = factory->NewJSPrimitiveRef(PrimitiveType::PRIMITIVE_NUMBER, jstagvalue);
999 JSHandle<JSTaggedValue> jspri = JSHandle<JSTaggedValue>::Cast(jsprimitive);
1000 Local<JSValueRef> object = JSNApiHelper::ToLocal<JSValueRef>(jspri);
1001 EXPECT_FALSE(object->IsJSPrimitiveNumber(vm_));
1002 }
1003
1004
1005 /**
1006 * @tc.number: ffi_interface_api_099
1007 * @tc.name: StringUtf16_NewFromUtf16_Length_WriteUtf16_01
1008 * @tc.desc:
1009 * NewFromUtf16:Create StringRef in UTF8 format
1010 * WriteUtf16:Write the value of StringRef to char16_ T array buffer, testing Chinese
1011 * @tc.type: FUNC
1012 * @tc.require: parameter
1013 */
HWTEST_F_L0(JSNApiTests,StringUtf16_NewFromUtf16_Length_WriteUtf16_01)1014 HWTEST_F_L0(JSNApiTests, StringUtf16_NewFromUtf16_Length_WriteUtf16_01)
1015 {
1016 LocalScope scope(vm_);
1017 const char16_t *test = u"年度";
1018 Local<StringRef> testString = StringRef::NewFromUtf16(vm_, test);
1019 EXPECT_EQ(testString->Length(vm_), 2); // 2 : length of testString("年度")
1020 char16_t buffer[3]; // 3 : length of testString + 1
1021 EXPECT_EQ(testString->WriteUtf16(vm_, buffer, 2), 2); // 2 : length of testString("年度")
1022 GTEST_LOG_(WARNING) << "年度test =" << buffer;
1023 ASSERT_EQ(buffer[0], u'年');
1024 ASSERT_EQ(buffer[1], u'度');
1025 }
1026
1027 /**
1028 * @tc.number: ffi_interface_api_100
1029 * @tc.name: StringUtf16_NewFromUtf16_Length_WriteUtf16_01
1030 * @tc.desc: Write the value of StringRef to char16_ T array buffer, testing non Chinese
1031 * @tc.type: FUNC
1032 * @tc.require: parameter
1033 */
HWTEST_F_L0(JSNApiTests,StringUtf16_NewFromUtf16_Length_WriteUtf16_02)1034 HWTEST_F_L0(JSNApiTests, StringUtf16_NewFromUtf16_Length_WriteUtf16_02)
1035 {
1036 LocalScope scope(vm_);
1037 const char16_t *test = u"hello world!0?";
1038 Local<StringRef> testString = StringRef::NewFromUtf16(vm_, test);
1039
1040 EXPECT_EQ(testString->Length(vm_), 14);
1041 char16_t buffer[15]; // 15 : length of testString + 1
1042 EXPECT_EQ(testString->WriteUtf16(vm_, buffer, 14), 14); // 14 : length of testString("hello world!!!")
1043 ASSERT_EQ(buffer[0], u'h');
1044 ASSERT_EQ(buffer[13], u'?');
1045 }
1046
1047 /**
1048 * @tc.number: ffi_interface_api_101
1049 * @tc.name: SetRef_IsSet_GetSize_GetTotalElements_GetValue
1050 * @tc.desc:
1051 * IsSet:Determine if it is a set container object
1052 * GetSize:Get set length size
1053 * GetTotalElements:Get the total number of set elements
1054 * GetValue:Obtain the element values of the set container according to the following table
1055 * @tc.type: FUNC
1056 * @tc.require: parameter
1057 */
HWTEST_F_L0(JSNApiTests,SetRef_IsSet_GetSize_GetTotalElements_GetValue)1058 HWTEST_F_L0(JSNApiTests, SetRef_IsSet_GetSize_GetTotalElements_GetValue)
1059 {
1060 LocalScope scope(vm_);
1061 JSThread *thread = vm_->GetJSThread();
1062 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1063 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1064 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsSetFunction();
1065 JSHandle<JSSet> jsSet =
1066 JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1067 JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
1068 jsSet->SetLinkedSet(thread, hashSet);
1069 JSHandle<JSTaggedValue> setTag = JSHandle<JSTaggedValue>::Cast(jsSet);
1070 Local<SetRef> set = JSNApiHelper::ToLocal<SetRef>(setTag);
1071 EXPECT_TRUE(set->IsSet(vm_));
1072 JSHandle<JSTaggedValue> fristValue(factory->NewFromASCII("vlue1"));
1073 JSSet::Add(thread, jsSet, fristValue);
1074 JSSet::Add(thread, jsSet, fristValue);
1075 int32_t num = set->GetSize(vm_);
1076 int32_t num1 = set->GetTotalElements(vm_);
1077 ASSERT_EQ(num, 1);
1078 ASSERT_EQ(num1, 1);
1079 JSHandle<JSTaggedValue> secondValue(factory->NewFromASCII("vlue2"));
1080 JSSet::Add(thread, jsSet, secondValue);
1081 num = set->GetSize(vm_);
1082 num1 = set->GetTotalElements(vm_);
1083 ASSERT_EQ(num, 2);
1084 ASSERT_EQ(num1, 2);
1085 Local<JSValueRef> res1 = set->GetValue(vm_, 0);
1086 ASSERT_EQ(res1->ToString(vm_)->ToString(vm_), "vlue1");
1087 Local<JSValueRef> res2 = set->GetValue(vm_, 1);
1088 ASSERT_EQ(res2->ToString(vm_)->ToString(vm_), "vlue2");
1089 }
1090
CreateJSSet(JSThread * thread)1091 static JSSet *CreateJSSet(JSThread *thread)
1092 {
1093 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1094 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1095 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsSetFunction();
1096 JSHandle<JSSet> set =
1097 JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1098 JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
1099 set->SetLinkedSet(thread, hashSet);
1100 return JSSet::Cast(set.GetTaggedValue().GetTaggedObject());
1101 }
1102
1103 /**
1104 * @tc.number: ffi_interface_api_102
1105 * @tc.name: JSSetIterator_IsSetIterator_GetIndex_GetKind
1106 * @tc.desc:
1107 * IsSetIterator:Determine if it is a set iterator
1108 * GetIndex:Gets the index of the subscript position currently pointed to by the set iterator
1109 * GetKind:Obtain the index type 'key/value/key_and_value' for the set iterator
1110 * @tc.type: FUNC
1111 * @tc.require: parameter
1112 */
HWTEST_F_L0(JSNApiTests,JSSetIterator_IsSetIterator_GetIndex_GetKind)1113 HWTEST_F_L0(JSNApiTests, JSSetIterator_IsSetIterator_GetIndex_GetKind)
1114 {
1115 LocalScope scope(vm_);
1116 JSThread *thread = vm_->GetJSThread();
1117 JSHandle<JSSet> jsSet(thread, CreateJSSet(thread));
1118 EXPECT_TRUE(*jsSet != nullptr);
1119 JSHandle<JSTaggedValue> jsTagSetIterator =
1120 JSSetIterator::CreateSetIterator(thread, JSHandle<JSTaggedValue>(jsSet), IterationKind::KEY);
1121 JSHandle<JSSetIterator> jsSetIterator1(jsTagSetIterator);
1122 EXPECT_EQ(JSTaggedValue::SameValue(thread, jsSetIterator1->GetIteratedSet(thread), jsSet->GetLinkedSet(thread)),
1123 true);
1124 Local<SetIteratorRef> setIterator = JSNApiHelper::ToLocal<SetIteratorRef>(jsTagSetIterator);
1125 EXPECT_TRUE(setIterator->IsSetIterator(vm_));
1126 EXPECT_EQ(setIterator->GetIndex(), 0U);
1127 Local<JSValueRef> resultKey = StringRef::NewFromUtf8(vm_, "keys");
1128 EXPECT_EQ(setIterator->GetKind(vm_)->ToString(vm_)->ToString(vm_), resultKey->ToString(vm_)->ToString(vm_));
1129 }
1130
1131 /**
1132 * @tc.number: ffi_interface_api_103
1133 * @tc.name: JSValueRef_IsMapIterator
1134 * @tc.desc: Determine if it is a Map iterator
1135 * @tc.type: FUNC
1136 * @tc.require: parameter
1137 */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsMapIterator)1138 HWTEST_F_L0(JSNApiTests, JSValueRef_IsMapIterator)
1139 {
1140 LocalScope scope(vm_);
1141 JSThread *thread = vm_->GetJSThread();
1142 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1143 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1144 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
1145 JSHandle<JSMap> jsMap =
1146 JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1147 JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
1148 jsMap->SetLinkedMap(thread, hashMap);
1149 JSHandle<JSTaggedValue> mapTag = JSHandle<JSTaggedValue>::Cast(jsMap);
1150 JSHandle<JSTaggedValue> jsMapIteratorTag =
1151 JSMapIterator::CreateMapIterator(thread, mapTag, IterationKind::KEY_AND_VALUE);
1152 JSHandle<JSMapIterator> jsMapIterator(jsMapIteratorTag);
1153 EXPECT_EQ(JSTaggedValue::SameValue(thread, jsMapIterator->GetIteratedMap(thread), jsMap->GetLinkedMap(thread)),
1154 true);
1155 Local<JSValueRef> mapIterator = JSNApiHelper::ToLocal<MapIteratorRef>(jsMapIteratorTag);
1156 EXPECT_TRUE(mapIterator->IsMapIterator(vm_));
1157 }
1158
1159 /**
1160 * @tc.number: ffi_interface_api_104
1161 * @tc.name: JSValueRef_IsModuleNamespaceObject
1162 * @tc.desc: Determine if it is a module space object
1163 * @tc.type: FUNC
1164 * @tc.require: parameter
1165 */
HWTEST_F_L0(JSNApiTests,JSValueRef_IsModuleNamespaceObject)1166 HWTEST_F_L0(JSNApiTests, JSValueRef_IsModuleNamespaceObject)
1167 {
1168 LocalScope scope(vm_);
1169 ObjectFactory *objectFactory = thread_->GetEcmaVM()->GetFactory();
1170 JSHandle<SourceTextModule> module = objectFactory->NewSourceTextModule();
1171 JSHandle<LocalExportEntry> localExportEntry1 = objectFactory->NewLocalExportEntry();
1172 SourceTextModule::AddLocalExportEntry(thread_, module, localExportEntry1, 0, 2);
1173 JSHandle<LocalExportEntry> localExportEntry2 = objectFactory->NewLocalExportEntry();
1174 SourceTextModule::AddLocalExportEntry(thread_, module, localExportEntry2, 1, 2);
1175 JSHandle<TaggedArray> localExportEntries(thread_, module->GetLocalExportEntries(thread_));
1176 CString baseFileName = "a.abc";
1177 module->SetEcmaModuleFilenameString(baseFileName);
1178 ModuleManager *moduleManager = thread_->GetModuleManager();
1179 moduleManager->AddResolveImportedModule(baseFileName, module.GetTaggedValue());
1180 JSHandle<ModuleNamespace> np =
1181 ModuleNamespace::ModuleNamespaceCreate(thread_, JSHandle<JSTaggedValue>::Cast(module), localExportEntries);
1182 EXPECT_TRUE(ModuleNamespace::PreventExtensions());
1183 JSHandle<JSTaggedValue> moduleNamespaceTag = JSHandle<JSTaggedValue>::Cast(np);
1184 Local<JSValueRef> moduleNamespace = JSNApiHelper::ToLocal<ModuleNamespace>(moduleNamespaceTag);
1185 ASSERT_TRUE(moduleNamespace->IsModuleNamespaceObject(vm_));
1186 }
1187
1188
1189 /**
1190 * @tc.number: ffi_interface_api_105
1191 * @tc.name: IsHole
1192 * @tc.desc: Determine if it is a hole
1193 * @tc.type: FUNC
1194 * @tc.require: parameter
1195 */
HWTEST_F_L0(JSNApiTests,IsHole)1196 HWTEST_F_L0(JSNApiTests, IsHole)
1197 {
1198 LocalScope scope(vm_);
1199 Local<JSValueRef> b = JSValueRef::Hole(vm_);
1200 ASSERT_TRUE(b->IsHole());
1201 }
1202
1203
1204 /**
1205 * @tc.number: ffi_interface_api_106
1206 * @tc.name: GetValueDouble
1207 * @tc.desc: Get value double from isNumber
1208 * @tc.type: FUNC
1209 * @tc.require: parameter
1210 */
HWTEST_F_L0(JSNApiTests,GetValueDouble)1211 HWTEST_F_L0(JSNApiTests, GetValueDouble)
1212 {
1213 LocalScope scope(vm_);
1214 bool isNumber = true;
1215 panda::JSValueRef* nativeValue = new JSValueRef();
1216 ASSERT_FALSE(nativeValue->GetValueDouble(isNumber));
1217 isNumber = false;
1218 ASSERT_FALSE(nativeValue->GetValueDouble(isNumber));
1219
1220 delete nativeValue;
1221 }
1222
1223
1224 /**
1225 * @tc.number: ffi_interface_api_107
1226 * @tc.name: GetValueInt32
1227 * @tc.desc: Get value int32 from isNumber
1228 * @tc.type: FUNC
1229 * @tc.require: parameter
1230 */
HWTEST_F_L0(JSNApiTests,GetValueInt32)1231 HWTEST_F_L0(JSNApiTests, GetValueInt32)
1232 {
1233 LocalScope scope(vm_);
1234 bool isNumber = true;
1235 panda::JSValueRef* nativeValue = new JSValueRef();
1236 ASSERT_FALSE(nativeValue->GetValueInt32(isNumber));
1237 isNumber = false;
1238 ASSERT_FALSE(nativeValue->GetValueInt32(isNumber));
1239
1240 delete nativeValue;
1241 }
1242
1243
1244 /**
1245 * @tc.number: ffi_interface_api_108
1246 * @tc.name: GetValueUint32
1247 * @tc.desc: Get value uint32 from isNumber
1248 * @tc.type: FUNC
1249 * @tc.require: parameter
1250 */
HWTEST_F_L0(JSNApiTests,GetValueUint32)1251 HWTEST_F_L0(JSNApiTests, GetValueUint32)
1252 {
1253 LocalScope scope(vm_);
1254 bool isNumber = true;
1255 panda::JSValueRef* nativeValue = new JSValueRef();
1256 ASSERT_FALSE(nativeValue->GetValueUint32(isNumber));
1257 isNumber = false;
1258 ASSERT_FALSE(nativeValue->GetValueUint32(isNumber));
1259
1260 delete nativeValue;
1261 }
1262
1263
1264 /**
1265 * @tc.number: ffi_interface_api_109
1266 * @tc.name: GetValueInt64
1267 * @tc.desc: Get value int64 from isNumber
1268 * @tc.type: FUNC
1269 * @tc.require: parameter
1270 */
HWTEST_F_L0(JSNApiTests,GetValueInt64)1271 HWTEST_F_L0(JSNApiTests, GetValueInt64)
1272 {
1273 LocalScope scope(vm_);
1274 bool isNumber = true;
1275 panda::JSValueRef* nativeValue = new JSValueRef();
1276 ASSERT_FALSE(nativeValue->GetValueInt64(isNumber));
1277 isNumber = false;
1278 ASSERT_FALSE(nativeValue->GetValueInt64(isNumber));
1279
1280 delete nativeValue;
1281 }
1282
1283
1284 /**
1285 * @tc.number: ffi_interface_api_110
1286 * @tc.name: GetValueBool
1287 * @tc.desc: Get value bool from isNumber
1288 * @tc.type: FUNC
1289 * @tc.require: parameter
1290 */
HWTEST_F_L0(JSNApiTests,GetValueBool)1291 HWTEST_F_L0(JSNApiTests, GetValueBool)
1292 {
1293 LocalScope scope(vm_);
1294 bool isNumber = true;
1295 panda::JSValueRef* nativeValue = new JSValueRef();
1296 ASSERT_FALSE(nativeValue->GetValueBool(isNumber));
1297 isNumber = false;
1298 ASSERT_FALSE(nativeValue->GetValueBool(isNumber));
1299
1300 delete nativeValue;
1301 }
1302
1303
1304 /**
1305 * @tc.number: ffi_interface_api_111
1306 * @tc.name: ToBigInt
1307 * @tc.desc: String to big int
1308 * @tc.type: FUNC
1309 * @tc.require: parameter
1310 */
HWTEST_F_L0(JSNApiTests,ToBigInt)1311 HWTEST_F_L0(JSNApiTests, ToBigInt)
1312 {
1313 LocalScope scope(vm_);
1314 Local<StringRef> toString = StringRef::NewFromUtf8(vm_, TEST_NUM2);
1315 Local<JSValueRef> toValue(toString);
1316 EXPECT_FALSE(toValue->ToBigInt(vm_)->IsBigInt(vm_));
1317 }
1318
1319
1320 /**
1321 * @tc.number: ffi_interface_api_112
1322 * @tc.name: IsInt && WithinInt32
1323 * @tc.desc: Determine if it is int && Within Int32
1324 * @tc.type: FUNC
1325 * @tc.require: parameter
1326 */
HWTEST_F_L0(JSNApiTests,IsInt)1327 HWTEST_F_L0(JSNApiTests, IsInt)
1328 {
1329 LocalScope scope(vm_);
1330 bool input = true;
1331 Local<IntegerRef> res = IntegerRef::New(vm_, input);
1332 EXPECT_TRUE(res->IsInt());
1333 EXPECT_TRUE(res->WithinInt32());
1334 }
1335
1336
1337 /**
1338 * @tc.number: ffi_interface_api_113
1339 * @tc.name: IsJSFunction
1340 * @tc.desc: Determine if it is JSFunction
1341 * @tc.type: FUNC
1342 * @tc.require: parameter
1343 */
HWTEST_F_L0(JSNApiTests,IsJSFunction)1344 HWTEST_F_L0(JSNApiTests, IsJSFunction)
1345 {
1346 LocalScope scope(vm_);
1347 bool input = true;
1348 Local<FunctionRef> res = IntegerRef::New(vm_, input);
1349 EXPECT_FALSE(res->IsJSFunction(vm_));
1350 }
1351
1352
1353 /**
1354 * @tc.number: ffi_interface_api_114
1355 * @tc.name: IsWeakRef
1356 * @tc.desc: Determine if it is JSFunction
1357 * @tc.type: FUNC
1358 * @tc.require: parameter
1359 */
HWTEST_F_L0(JSNApiTests,IsWeakRef)1360 HWTEST_F_L0(JSNApiTests, IsWeakRef)
1361 {
1362 LocalScope scope(vm_);
1363 Local<JSValueRef> tag = BooleanRef::New(vm_, true);
1364 EXPECT_FALSE(tag->IsWeakRef(vm_));
1365 }
1366
1367
1368 /**
1369 * @tc.number: ffi_interface_api_115
1370 * @tc.name: IsArrayIterator
1371 * @tc.desc: Determine if it is arrayIterator
1372 * @tc.type: FUNC
1373 * @tc.require: parameter
1374 */
HWTEST_F_L0(JSNApiTests,IsArrayIterator)1375 HWTEST_F_L0(JSNApiTests, IsArrayIterator)
1376 {
1377 LocalScope scope(vm_);
1378 Local<JSValueRef> tag = ArrayRef::New(vm_, true);
1379 EXPECT_FALSE(tag->IsArrayIterator(vm_));
1380 }
1381
1382
1383 /**
1384 * @tc.number: ffi_interface_api_116
1385 * @tc.name: IsStringIterator
1386 * @tc.desc: Determine if it is string Iterator
1387 * @tc.type: FUNC
1388 * @tc.require: parameter
1389 */
HWTEST_F_L0(JSNApiTests,IsStringIterator)1390 HWTEST_F_L0(JSNApiTests, IsStringIterator)
1391 {
1392 LocalScope scope(vm_);
1393 Local<StringRef> toString = StringRef::NewFromUtf8(vm_, TEST_NUM2);
1394 EXPECT_FALSE(toString->IsStringIterator(vm_));
1395 std::string str = TEST_NUM2;
1396 EXPECT_EQ(toString->DebuggerToString(vm_), str);
1397 }
1398
1399
1400 /**
1401 * @tc.number: ffi_interface_api_116
1402 * @tc.name: IsJSSharedInt8Array
1403 * @tc.desc: Determine if it is JSSharedArray
1404 * @tc.type: FUNC
1405 * @tc.require: parameter
1406 */
HWTEST_F_L0(JSNApiTests,IsJSSharedInt8Array)1407 HWTEST_F_L0(JSNApiTests, IsJSSharedInt8Array)
1408 {
1409 LocalScope scope(vm_);
1410 Local<JSValueRef> tag = ArrayRef::New(vm_, true);
1411 EXPECT_FALSE(tag->IsJSSharedInt8Array(vm_));
1412 EXPECT_FALSE(tag->IsJSSharedUint8Array(vm_));
1413 EXPECT_FALSE(tag->IsJSSharedUint8ClampedArray(vm_));
1414 EXPECT_FALSE(tag->IsJSSharedInt16Array(vm_));
1415 EXPECT_FALSE(tag->IsJSSharedUint16Array(vm_));
1416 EXPECT_FALSE(tag->IsJSSharedInt32Array(vm_));
1417 EXPECT_FALSE(tag->IsJSSharedFloat32Array(vm_));
1418 EXPECT_FALSE(tag->IsJSSharedUint32Array(vm_));
1419 }
1420
1421
1422 /**
1423 * @tc.number: ffi_interface_api_117
1424 * @tc.name: IsNativeModuleFailureInfoObject
1425 * @tc.desc: Determine if it is nativeModuleFailureInfoObject
1426 * @tc.type: FUNC
1427 * @tc.require: parameter
1428 */
HWTEST_F_L0(JSNApiTests,IsNativeModuleFailureInfoObject)1429 HWTEST_F_L0(JSNApiTests, IsNativeModuleFailureInfoObject)
1430 {
1431 LocalScope scope(vm_);
1432 Local<JSValueRef> tag = ArrayRef::New(vm_, true);
1433 EXPECT_FALSE(tag->IsNativeModuleFailureInfoObject(vm_));
1434 EXPECT_FALSE(tag->IsSendableArrayBuffer(vm_));
1435 EXPECT_FALSE(tag->IsJSLocale(vm_));
1436 EXPECT_FALSE(tag->IsJSDateTimeFormat(vm_));
1437 EXPECT_FALSE(tag->IsJSRelativeTimeFormat(vm_));
1438 EXPECT_FALSE(tag->IsJSIntl(vm_));
1439 EXPECT_FALSE(tag->IsJSNumberFormat(vm_));
1440 EXPECT_FALSE(tag->IsJSCollator(vm_));
1441 EXPECT_FALSE(tag->IsJSPluralRules(vm_));
1442 EXPECT_FALSE(tag->IsJSListFormat(vm_));
1443 EXPECT_FALSE(tag->IsAsyncGeneratorObject(vm_));
1444 }
1445
1446
1447 /**
1448 * @tc.number: ffi_interface_api_118
1449 * @tc.name: IsAsyncGeneratorObject
1450 * @tc.desc: Determine if it is asyncGeneratorObject
1451 * @tc.type: FUNC
1452 * @tc.require: parameter
1453 */
HWTEST_F_L0(JSNApiTests,IsAsyncGeneratorObject)1454 HWTEST_F_L0(JSNApiTests, IsAsyncGeneratorObject)
1455 {
1456 LocalScope scope(vm_);
1457 Local<JSValueRef> tag = ArrayRef::New(vm_, true);
1458 EXPECT_FALSE(tag->IsAsyncGeneratorObject(vm_));
1459 EXPECT_FALSE(tag->IsArrayList(vm_));
1460 EXPECT_FALSE(tag->IsDeque(vm_));
1461 EXPECT_FALSE(tag->IsHashMap(vm_));
1462 EXPECT_FALSE(tag->IsHashSet(vm_));
1463 EXPECT_FALSE(tag->IsLightWeightMap(vm_));
1464 EXPECT_FALSE(tag->IsLightWeightSet(vm_));
1465 EXPECT_FALSE(tag->IsLinkedList(vm_));
1466 EXPECT_FALSE(tag->IsLinkedListIterator(vm_));
1467 EXPECT_FALSE(tag->IsList(vm_));
1468 EXPECT_FALSE(tag->IsPlainArray(vm_));
1469 EXPECT_FALSE(tag->IsQueue(vm_));
1470 EXPECT_FALSE(tag->IsStack(vm_));
1471 }
1472
1473
1474 /**
1475 * @tc.number: ffi_interface_api_119
1476 * @tc.name: IsSendableObject
1477 * @tc.desc: Determine if it is sendableObject
1478 * @tc.type: FUNC
1479 * @tc.require: parameter
1480 */
HWTEST_F_L0(JSNApiTests,IsSendableObject)1481 HWTEST_F_L0(JSNApiTests, IsSendableObject)
1482 {
1483 LocalScope scope(vm_);
1484 Local<JSValueRef> value = ObjectRef::New(vm_);
1485 EXPECT_FALSE(value->IsSendableObject(vm_));
1486 EXPECT_FALSE(value->IsJSShared(vm_));
1487 EXPECT_FALSE(value->IsSharedArray(vm_));
1488 EXPECT_FALSE(value->IsSharedTypedArray(vm_));
1489 EXPECT_FALSE(value->IsSharedSet(vm_));
1490 EXPECT_FALSE(value->IsSharedMap(vm_));
1491 EXPECT_FALSE(value->IsSharedMapIterator(vm_));
1492 EXPECT_TRUE(value->IsHeapObject());
1493 }
1494
1495
1496 /**
1497 * @tc.number: ffi_interface_api_120
1498 * @tc.name: GetNativePointerValue
1499 * @tc.desc: GetNativePointerValue
1500 * @tc.type: FUNC
1501 * @tc.require: parameter
1502 */
HWTEST_F_L0(JSNApiTests,GetNativePointerValue)1503 HWTEST_F_L0(JSNApiTests, GetNativePointerValue)
1504 {
1505 LocalScope scope(vm_);
1506 Local<JSValueRef> value = ObjectRef::New(vm_);
1507 bool isNativePointer = true;
1508 EXPECT_FALSE(value->GetNativePointerValue(vm_, isNativePointer));
1509 }
1510
1511
1512 /**
1513 * @tc.number: ffi_interface_api_121
1514 * @tc.name: IsDetachedArraybuffer
1515 * @tc.desc: IsDetachedArraybuffer
1516 * @tc.type: FUNC
1517 * @tc.require: parameter
1518 */
HWTEST_F_L0(JSNApiTests,IsDetachedArraybuffer)1519 HWTEST_F_L0(JSNApiTests, IsDetachedArraybuffer)
1520 {
1521 LocalScope scope(vm_);
1522 const int32_t length = 15;
1523 Local<JSValueRef> sharedArrayBuffer = ArrayBufferRef::New(vm_, length);
1524 bool isNativePointer = true;
1525 EXPECT_FALSE(sharedArrayBuffer->IsDetachedArraybuffer(vm_, isNativePointer));
1526 sharedArrayBuffer->DetachedArraybuffer(vm_, isNativePointer);
1527 EXPECT_TRUE(isNativePointer);
1528 }
1529
1530
1531 /**
1532 * @tc.number: ffi_interface_api_122
1533 * @tc.name: MapRefGet
1534 * @tc.desc: MapRef Get
1535 * @tc.type: FUNC
1536 * @tc.require: parameter
1537 */
HWTEST_F_L0(JSNApiTests,MapRefGet)1538 HWTEST_F_L0(JSNApiTests, MapRefGet)
1539 {
1540 LocalScope scope(vm_);
1541 Local<MapRef> object = MapRef::New(vm_);
1542 Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, TEST_KEY);
1543 Local<JSValueRef> value = StringRef::NewFromUtf8(vm_, TEST_VALUE);
1544 EXPECT_FALSE(object->Has(vm_, key));
1545 object->Set(vm_, key, value);
1546 EXPECT_TRUE(object->Has(vm_, key));
1547 Local<JSValueRef> mapRef = object->Get(vm_, TEST_KEY);
1548 EXPECT_FALSE(object->Has(vm_, mapRef));
1549 object->Delete(vm_, key);
1550 object->GetEntries(vm_);
1551 object->GetKeys(vm_);
1552 object->GetValues(vm_);
1553 EXPECT_FALSE(object->Has(vm_, TEST_KEY));
1554 object->Clear(vm_);
1555 }
1556
1557
1558 /**
1559 * @tc.number: ffi_interface_api_123
1560 * @tc.name: SendableMapRef
1561 * @tc.desc: SendableMapRef test
1562 * @tc.type: FUNC
1563 * @tc.require: parameter
1564 */
HWTEST_F_L0(JSNApiTests,SendableMapRef)1565 HWTEST_F_L0(JSNApiTests, SendableMapRef)
1566 {
1567 LocalScope scope(vm_);
1568 Local<SendableMapRef> object = SendableMapRef::New(vm_);
1569 Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, TEST_KEY);
1570 Local<JSValueRef> value = StringRef::NewFromUtf8(vm_, TEST_VALUE);
1571 EXPECT_FALSE(object->Has(vm_, key));
1572 object->Set(vm_, key, value);
1573 object->Set(vm_, TEST_KEY, value);
1574 EXPECT_TRUE(object->Has(vm_, key));
1575 Local<JSValueRef> sendableMapRef = object->Get(vm_, TEST_KEY);
1576 sendableMapRef = object->Get(vm_, key);
1577 EXPECT_FALSE(object->Has(vm_, sendableMapRef));
1578 Local<SendableMapIteratorRef> sendableMapIteratorRef = object->GetEntries(vm_);
1579 sendableMapIteratorRef->Next(vm_);
1580 object->GetKey(vm_, 0);
1581 object->GetKeys(vm_);
1582 object->GetValue(vm_, 0);
1583 object->GetValues(vm_);
1584 object->GetSize(vm_);
1585 object->GetTotalElements(vm_);
1586 EXPECT_TRUE(object->Has(vm_, TEST_KEY));
1587 object->Delete(vm_, key);
1588 object->Clear(vm_);
1589 }
1590
1591
1592 /**
1593 * @tc.number: ffi_interface_api_124
1594 * @tc.name: MapIteratorRef
1595 * @tc.desc: MapIteratorRef test
1596 * @tc.type: FUNC
1597 * @tc.require: parameter
1598 */
HWTEST_F_L0(JSNApiTests,MapIteratorRef)1599 HWTEST_F_L0(JSNApiTests, MapIteratorRef)
1600 {
1601 LocalScope scope(vm_);
1602 Local<MapRef> mapRef = MapRef::New(vm_);
1603 Local<MapIteratorRef> object = MapIteratorRef::New(vm_, mapRef);
1604 Local<JSValueRef> key = StringRef::NewFromUtf8(vm_, TEST_KEY);
1605 Local<JSValueRef> value = StringRef::NewFromUtf8(vm_, TEST_VALUE);
1606 EXPECT_FALSE(object->Has(vm_, key));
1607 ecmascript::EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = object->GetEcmaRuntimeCallInfo(vm_);
1608 object->Next(vm_, ecmaRuntimeCallInfo);
1609 object->Next(vm_);
1610 object->Set(vm_, key, value);
1611 object->Set(vm_, TEST_KEY, value);
1612 EXPECT_TRUE(object->Has(vm_, key));
1613 Local<JSValueRef> mapIteratorRef = object->Get(vm_, TEST_KEY);
1614 mapIteratorRef = object->Get(vm_, key);
1615 object->Delete(vm_, key);
1616 EXPECT_FALSE(object->Has(vm_, mapIteratorRef));
1617 }
1618
1619
1620 /**
1621 * @tc.number: ffi_interface_api_125
1622 * @tc.name: SetIteratorRef
1623 * @tc.desc: SetIteratorRef test
1624 * @tc.type: FUNC
1625 * @tc.require: parameter
1626 */
HWTEST_F_L0(JSNApiTests,SetIteratorRef)1627 HWTEST_F_L0(JSNApiTests, SetIteratorRef)
1628 {
1629 LocalScope scope(vm_);
1630 JSThread *thread = vm_->GetJSThread();
1631 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1632 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1633 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsSetFunction();
1634 JSHandle<JSSet> set =
1635 JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1636 JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
1637 set->SetLinkedSet(thread, hashSet);
1638 JSHandle<JSTaggedValue> setTag = JSHandle<JSTaggedValue>::Cast(set);
1639 Local<SetRef> setRef = JSNApiHelper::ToLocal<SetRef>(setTag);
1640 Local<MapIteratorRef> object = SetIteratorRef::New(vm_, setRef);
1641 EXPECT_TRUE(object->IsHeapObject());
1642 }
1643
1644
1645 /**
1646 * @tc.number: ffi_interface_api_126
1647 * @tc.name: PromiseRejectInfo
1648 * @tc.desc: PromiseRejectInfo test
1649 * @tc.type: FUNC
1650 * @tc.require: parameter
1651 */
HWTEST_F_L0(JSNApiTests,PromiseRejectInfo)1652 HWTEST_F_L0(JSNApiTests, PromiseRejectInfo)
1653 {
1654 LocalScope scope(vm_);
1655 Local<StringRef> toStringPromise = StringRef::NewFromUtf8(vm_, TEST_NUM1);
1656 Local<JSValueRef> promise(toStringPromise);
1657 Local<StringRef> toStringReason = StringRef::NewFromUtf8(vm_, TEST_NUM1);
1658 Local<JSValueRef> reason(toStringReason);
1659 std::string* str = new std::string(TEST_VALUE);
1660 void *data = static_cast<void *>(str);
1661 PromiseRejectInfo promisereject(promise, reason, PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT, data);
1662 EXPECT_EQ(promisereject.GetOperation(), PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT);
1663
1664 delete str;
1665 }
1666
1667 /**
1668 * @tc.number: ffi_interface_api_127
1669 * @tc.name: ObjectRef
1670 * @tc.desc: ObjectRef test
1671 * @tc.type: FUNC
1672 * @tc.require: parameter
1673 */
HWTEST_F_L0(JSNApiTests,ObjectRef)1674 HWTEST_F_L0(JSNApiTests, ObjectRef)
1675 {
1676 LocalScope scope(vm_);
1677 Local<ObjectRef> object = ObjectRef::New(vm_);
1678 object = ObjectRef::NewS(vm_);
1679 std::string str = TEST_VALUE;
1680 object->CreateNativeModuleFailureInfo(vm_, str);
1681 bool input = true;
1682 Local<FunctionRef> res = IntegerRef::New(vm_, input);
1683 object->CreateAccessorData(vm_, res, res);
1684 uintptr_t addr = ObjectRef::NewObject(vm_);
1685 Local<StringRef> key = StringRef::NewFromUtf8(vm_, TEST_VALUE);
1686 Local<JSValueRef> value = ObjectRef::New(vm_);
1687 PropertyAttribute property(value, true, true, true);
1688 object->SetConcurrentNativePointerField(vm_, 0);
1689 EXPECT_FALSE(object->SetPrototype(vm_, object));
1690 EXPECT_FALSE(object->HasOwnProperty(vm_, key));
1691 EXPECT_FALSE(object->GetOwnProperty(vm_, key, property));
1692 EXPECT_NE(addr, 1);
1693 }
1694
1695 /**
1696 * @tc.number: ffi_interface_api_128
1697 * @tc.name: SendableArrayBufferRef
1698 * @tc.desc: SendableArrayBufferRef test
1699 * @tc.type: FUNC
1700 * @tc.require: parameter
1701 */
HWTEST_F_L0(JSNApiTests,SendableArrayBufferRef)1702 HWTEST_F_L0(JSNApiTests, SendableArrayBufferRef)
1703 {
1704 LocalScope scope(vm_);
1705 Local<SendableArrayBufferRef> res = SendableArrayBufferRef::New(vm_, 0);
1706 const int32_t length = 33;
1707 res->GetBuffer(vm_);
1708 EXPECT_NE(res->ByteLength(vm_), length);
1709 res->Detach(vm_);
1710 EXPECT_NE(res->IsDetach(vm_), length);
1711 }
1712
1713 /**
1714 * @tc.number: ffi_interface_api_129
1715 * @tc.name: JSNApiSetPkgAliasList
1716 * @tc.desc: Used to verify whether the function of setting the map container module was successful.
1717 * @tc.type: FUNC
1718 * @tc.require: parameter
1719 */
HWTEST_F_L0(JSNApiTests,JSNApiSetPkgAliasList)1720 HWTEST_F_L0(JSNApiTests, JSNApiSetPkgAliasList)
1721 {
1722 LocalScope scope(vm_);
1723 std::map<std::string, std::string> str = { { TEST_NUM1, TEST_NUM2 } };
1724 JSNApi::SetPkgAliasList(vm_, str);
1725 std::string moduleName = TEST_NUM1;
1726 EXPECT_EQ(std::string(vm_->GetPkgNameWithAlias(TEST_NUM1)), TEST_NUM2);
1727 }
1728
1729 /**
1730 * @tc.number: ffi_interface_api_130
1731 * @tc.name: InitForConcurrentFunction
1732 * @tc.desc: Used to verify whether the function of setting the map container module was successful.
1733 * @tc.type: FUNC
1734 * @tc.require: parameter
1735 */
HWTEST_F_L0(JSNApiTests,JSNApiInitForConcurrentFunction)1736 HWTEST_F_L0(JSNApiTests, JSNApiInitForConcurrentFunction)
1737 {
1738 LocalScope scope(vm_);
1739 void *taskInfo = reinterpret_cast<void *>(BuiltinsFunction::FunctionPrototypeInvokeSelf);
1740 Local<JSValueRef> function = FunctionRef::New(vm_, FunctionCallback);
1741 EXPECT_FALSE(JSNApi::InitForConcurrentFunction(vm_, function, taskInfo));
1742 }
1743
1744 /**
1745 * @tc.number: ffi_interface_api_131
1746 * @tc.name: GetStackBeforeCallNapiSuccess
1747 * @tc.desc: Used to verify whether the function of setting the map container module was successful.
1748 * @tc.type: FUNC
1749 * @tc.require: parameter
1750 */
HWTEST_F_L0(JSNApiTests,GetStackBeforeCallNapiSuccess)1751 HWTEST_F_L0(JSNApiTests, GetStackBeforeCallNapiSuccess)
1752 {
1753 LocalScope scope(vm_);
1754 SourceMapCallback callback { nullptr };
1755 JSNApi::SetSourceMapCallback(vm_, callback);
1756 vm_->GetJSThread()->SetIsProfiling(false);
1757 bool getStackBeforeCallNapiSuccess = false;
1758 JSNApi::GetStackBeforeCallNapiSuccess(vm_, getStackBeforeCallNapiSuccess);
1759 JSNApi::GetStackAfterCallNapi(vm_);
1760 EXPECT_FALSE(getStackBeforeCallNapiSuccess);
1761 }
1762
1763 /**
1764 * @tc.number: ffi_interface_api_132
1765 * @tc.name: NewConcurrentWithName
1766 * @tc.desc: Check if the function created through the NewConcurrentWithName returns the same value to native callee
1767 * @tc.type: FUNC
1768 * @tc.require: parameter
1769 */
HWTEST_F_L0(JSNApiTests,NewConcurrentWithName)1770 HWTEST_F_L0(JSNApiTests, NewConcurrentWithName)
1771 {
1772 LocalScope scope(vm_);
1773 Local<JSValueRef> context = JSNApiHelper::ToLocal<JSValueRef>(JSHandle<JSTaggedValue>(thread_->GetGlobalEnv()));
1774 std::string name = "MyFunction";
1775 Local<FunctionRef> func = FunctionRef::NewConcurrentWithName(
1776 vm_, context,
1777 [](JsiRuntimeCallInfo *runtimeInfo) -> JSValueRef {
1778 EcmaVM *vm = runtimeInfo->GetVM();
1779 LocalScope scope(vm);
1780 return **StringRef::NewFromUtf8WithoutStringTable(vm, "funcResult");
1781 },
1782 nullptr, name.c_str());
1783 Local<JSValueRef> res = func->Call(vm_, JSValueRef::Undefined(vm_), nullptr, 0);
1784 ASSERT_EQ("funcResult", res->ToString(vm_)->ToString(vm_));
1785 Local<StringRef> funcName = func->GetName(vm_);
1786 std::string funcNameStr = funcName->ToString(vm_);
1787 ASSERT_EQ(name, funcNameStr);
1788 }
1789
1790 /**
1791 * @tc.number: ffi_interface_api_140
1792 * @tc.name: NewConcurrentClassFunctionWithNameCase0
1793 * @tc.desc: While property count is 0, check if the class created through the NewConcurrentClassFunctionWithName
1794 * method meets the specifications of the class constructor and the name property of class.
1795 * @tc.type: FUNC
1796 * @tc.require: parameter
1797 */
HWTEST_F_L0(JSNApiTests,NewConcurrentClassFunctionWithNameCase0)1798 HWTEST_F_L0(JSNApiTests, NewConcurrentClassFunctionWithNameCase0)
1799 {
1800 LocalScope scope(vm_);
1801 Local<JSValueRef> context = JSNApiHelper::ToLocal<JSValueRef>(JSHandle<JSTaggedValue>(thread_->GetGlobalEnv()));
1802 std::string name = "MyClassFunction";
1803 Local<FunctionRef> cls = FunctionRef::NewConcurrentClassFunctionWithName(vm_, context, InternalFunctionCallback,
1804 nullptr, name.c_str(), nullptr);
1805
1806 JSHandle<JSTaggedValue> obj = JSNApiHelper::ToJSHandle(Local<JSValueRef>(cls));
1807 // Asserting that obj is a class constructor
1808 ASSERT_TRUE(obj->IsClassConstructor());
1809 // The GetPropertyInlinedProps method gets the method of an inline property,
1810 // CLASS_PROTOTYPE_INLINE_PROPERTY_INDEX is the inline property index of the class prototype.
1811 JSTaggedValue res =
1812 JSHandle<JSFunction>(obj)->GetPropertyInlinedProps(thread_, JSFunction::CLASS_PROTOTYPE_INLINE_PROPERTY_INDEX);
1813 // The property that the assertion gets is an internal accessor
1814 ASSERT_TRUE(res.IsInternalAccessor());
1815 Local<StringRef> funcName = cls->GetName(vm_);
1816 std::string funcNameStr = funcName->ToString(vm_);
1817 ASSERT_EQ(name, funcNameStr);
1818 }
1819
1820 /**
1821 * @tc.number: ffi_interface_api_141
1822 * @tc.name: NewConcurrentClassFunctionWithNameCase1
1823 * @tc.desc: While property count is smaller than 1024, check if the keys and attrs were successfully set in the class
1824 * that created through the NewConcurrentClassFunctionWithName method.
1825 * @tc.type: FUNC
1826 * @tc.require: parameter
1827 */
HWTEST_F_L0(JSNApiTests,NewConcurrentClassFunctionWithNameCase1)1828 HWTEST_F_L0(JSNApiTests, NewConcurrentClassFunctionWithNameCase1)
1829 {
1830 LocalScope scope(vm_);
1831 JSHandle<GlobalEnv> env = thread_->GetGlobalEnv();
1832 Local<JSValueRef> context = JSNApiHelper::ToLocal<JSValueRef>(JSHandle<JSTaggedValue>(env));
1833 std::string name = "MyClassFunction";
1834 size_t staticPropCount = 5;
1835 size_t nonStaticPropCount = PROPERTY_COUNT_CASE1 - staticPropCount;
1836 Local<panda::JSValueRef> keys[PROPERTY_COUNT_CASE1];
1837 PropertyAttribute attrs[PROPERTY_COUNT_CASE1];
1838 // There are 10 properties totally, and the last two keys and first two keys are duplicate, so expectedVals[8] will
1839 // overwrite expectedVals[9], expectedVals[1] will overwrite expectedVals[0]
1840 const std::string keyNames[10] = {"key0", "key0", "key2", "key3", "key4", "key5", "key6", "key7", "key8", "key8"};
1841 const int expectedVals[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
1842 for (size_t i = 0; i < PROPERTY_COUNT_CASE1; ++i) {
1843 keys[i] = StringRef::NewFromUtf8(vm_, keyNames[i].c_str());
1844 Local<JSValueRef> curValue =
1845 JSNApiHelper::ToLocal<JSValueRef>(JSHandle<JSTaggedValue>(thread_, JSTaggedValue(expectedVals[i])));
1846 attrs[i].SetValue(curValue);
1847 attrs[i].SetConfigurable(true);
1848 attrs[i].SetWritable(true);
1849 }
1850
1851 Local<FunctionRef> cls = FunctionRef::NewConcurrentClassFunctionWithName(
1852 vm_, context, InternalFunctionCallback, nullptr, name.c_str(), nullptr, true, PROPERTY_COUNT_CASE1,
1853 staticPropCount, keys, attrs);
1854
1855 JSHandle<JSTaggedValue> obj = JSNApiHelper::ToJSHandle(Local<JSValueRef>(cls));
1856 JSHandle<JSFunction> clsFunc(obj);
1857 JSThread *thread = vm_->GetJSThread();
1858 JSHandle<JSObject> clsPrototype(thread_, clsFunc->GetProtoOrHClass(thread));
1859 JSHandle<JSHClass> clsFuncHClass(thread_, clsFunc->GetJSHClass());
1860 JSHandle<JSHClass> clsProtoHClass(thread_, clsPrototype->GetJSHClass());
1861 uint32_t clsInlPropNum = clsFuncHClass->GetInlinedProperties();
1862 uint32_t clsProtoInlPropNum = clsProtoHClass->GetInlinedProperties();
1863
1864 JSHandle<JSHClass> defaultClsFuncHClass(env->GetFunctionClassWithoutName());
1865 JSHandle<JSHClass> defaultClsProtoHClass(env->GetObjectFunctionClass());
1866 uint32_t defaultClsInlPropNum = defaultClsFuncHClass->GetInlinedProperties();
1867 uint32_t defaultProtoInlPropNum = defaultClsProtoHClass->GetInlinedProperties();
1868 // In current NewConcurrentClassFunctionWithName method, all properties were stored as inlined properties.
1869 ASSERT_EQ(clsInlPropNum, defaultClsInlPropNum + staticPropCount);
1870 ASSERT_EQ(clsProtoInlPropNum, defaultProtoInlPropNum + PROPERTY_COUNT_CASE1 - staticPropCount);
1871
1872 for (size_t i = 0; i < staticPropCount; ++i) {
1873 auto realProp = JSObject::GetProperty(thread_, obj, JSNApiHelper::ToJSHandle(keys[i])).GetValue();
1874 uint32_t realVal = JSTaggedValue::ToUint32(thread_, realProp);
1875 if (i == 0) {
1876 // expectedVals[0] was overwritten by expectedVals[1].
1877 ASSERT_EQ(realVal, expectedVals[1]);
1878 } else {
1879 ASSERT_EQ(realVal, expectedVals[i]);
1880 }
1881 }
1882 JSHandle<JSTaggedValue> clsProtoVal(thread_, clsFunc->GetProtoOrHClass(thread));
1883 for (size_t i = 0; i < nonStaticPropCount; ++i) {
1884 size_t trueIdx = PROPERTY_COUNT_CASE1 - 1 - i;
1885 auto realProp = JSObject::GetProperty(thread_, clsProtoVal, JSNApiHelper::ToJSHandle(keys[trueIdx])).GetValue();
1886 uint32_t realVal = JSTaggedValue::ToUint32(thread_, realProp);
1887 if (i == 0) {
1888 // expectedVals[9] was overwritten by expectedVals[8].
1889 ASSERT_EQ(realVal, expectedVals[trueIdx - 1]);
1890 } else {
1891 ASSERT_EQ(realVal, expectedVals[trueIdx]);
1892 }
1893 }
1894 }
1895 } // namespace panda::test
1896