• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2024 SwanLink (Jiangsu) Technology Development 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 #include "napi_object_test.h"
16 const size_t BUF_SIZE_10 = 10;
17 const JSVM_TypeTag tagsData[] = {
18     {0x9e4b2449547061b3, 0x33999f8a6516c499},
19     {0x1d55a794c53a726d, 0x43633f509f9c944e},
20     {0, 0}, // default tag
21     {0x6a971439f5b2e5d7, 0x531dc28a7e5317c0},
22 };
23 //OH_JSVM_CreateObject
24 //result is null
TestCreateObjectTest1(JSVM_Env env,JSVM_CallbackInfo info)25 [[maybe_unused]] JSVM_Value TestCreateObjectTest1(JSVM_Env env, JSVM_CallbackInfo info)
26 {
27     size_t argc = 1;
28     JSVM_Value argv[1] = {nullptr};
29     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
30 
31     JSVM_Status status = OH_JSVM_CreateObject(env, nullptr);
32     if (status == JSVM_OK) {
33         OH_JSVM_ThrowError(env, nullptr, "TestCreateObjectTest1: OH_JSVM_CreateObject Failed");
34         return nullptr;
35     }
36 
37     bool result = true;
38     JSVM_Value value = nullptr;
39     OH_JSVM_GetBoolean(env, result, &value);
40     return value;
41 }
42 //result is not nullptr
TestCreateObjectTest2(JSVM_Env env,JSVM_CallbackInfo info)43 [[maybe_unused]] JSVM_Value TestCreateObjectTest2(JSVM_Env env, JSVM_CallbackInfo info)
44 {
45     size_t argc = 1;
46     JSVM_Value argv[1] = {nullptr};
47     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
48 
49     JSVM_Value rstObject;
50     JSVM_Status status = OH_JSVM_CreateObject(env, &rstObject);
51     if (status != JSVM_OK) {
52         OH_JSVM_ThrowError(env, nullptr, "TestCreateObjectTest2: OH_JSVM_CreateObject Failed");
53         return nullptr;
54     }
55 
56     bool result = true;
57     JSVM_Value value = nullptr;
58     OH_JSVM_GetBoolean(env, result, &value);
59     return value;
60 }
61 //OH_JSVM_Typeof
62 //value is null
TestTypeofTest1(JSVM_Env env,JSVM_CallbackInfo info)63 [[maybe_unused]] JSVM_Value TestTypeofTest1(JSVM_Env env, JSVM_CallbackInfo info)
64 {
65     size_t argc = 1;
66     JSVM_Value argv[1] = {nullptr};
67     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
68 
69     JSVM_Value typeValue;
70     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
71     JSVM_Status status = OH_JSVM_Typeof(env, nullptr, &rstValueType);
72     if (status == JSVM_OK) {
73         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest1: OH_JSVM_Typeof Failed");
74         return nullptr;
75     }
76 
77     bool result = true;
78     JSVM_Value value = nullptr;
79     OH_JSVM_GetBoolean(env, result, &value);
80     return value;
81 }
82 //value is not null, type is JSVM_UNDEFINED
TestTypeofTest2(JSVM_Env env,JSVM_CallbackInfo info)83 [[maybe_unused]] JSVM_Value TestTypeofTest2(JSVM_Env env, JSVM_CallbackInfo info)
84 {
85     size_t argc = 1;
86     JSVM_Value argv[1] = {nullptr};
87     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
88 
89     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
90     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], &rstValueType);
91     if (status != JSVM_OK) {
92         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest2: OH_JSVM_Typeof Failed");
93         return nullptr;
94     }
95     if (rstValueType != JSVM_UNDEFINED) {
96         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest2: OH_JSVM_Typeof type is not JSVM_UNDEFINED");
97         return nullptr;
98     }
99 
100     bool result = true;
101     JSVM_Value value = nullptr;
102     OH_JSVM_GetBoolean(env, result, &value);
103     return value;
104 }
105 //value is not null, type is JSVM_NULL
TestTypeofTest3(JSVM_Env env,JSVM_CallbackInfo info)106 [[maybe_unused]] JSVM_Value TestTypeofTest3(JSVM_Env env, JSVM_CallbackInfo info)
107 {
108     size_t argc = 1;
109     JSVM_Value argv[1] = {nullptr};
110     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
111 
112     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
113     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], &rstValueType);
114     if (status != JSVM_OK) {
115         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest3: OH_JSVM_Typeof Failed");
116         return nullptr;
117     }
118     if (rstValueType != JSVM_NULL) {
119         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest3: OH_JSVM_Typeof type is not JSVM_NULL");
120         return nullptr;
121     }
122 
123     bool result = true;
124     JSVM_Value value = nullptr;
125     OH_JSVM_GetBoolean(env, result, &value);
126     return value;
127 }
128 //value is not null, type is JSVM_BOOLEAN
TestTypeofTest4(JSVM_Env env,JSVM_CallbackInfo info)129 [[maybe_unused]] JSVM_Value TestTypeofTest4(JSVM_Env env, JSVM_CallbackInfo info)
130 {
131     size_t argc = 1;
132     JSVM_Value argv[1] = {nullptr};
133     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
134 
135     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
136     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], &rstValueType);
137     if (status != JSVM_OK) {
138         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest4: OH_JSVM_Typeof Failed");
139         return nullptr;
140     }
141     if (rstValueType != JSVM_BOOLEAN) {
142         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest4: OH_JSVM_Typeof type is not JSVM_BOOLEAN");
143         return nullptr;
144     }
145 
146     bool result = true;
147     JSVM_Value value = nullptr;
148     OH_JSVM_GetBoolean(env, result, &value);
149     return value;
150 }
151 //value is not null, type is JSVM_NUMBER
TestTypeofTest5(JSVM_Env env,JSVM_CallbackInfo info)152 [[maybe_unused]] JSVM_Value TestTypeofTest5(JSVM_Env env, JSVM_CallbackInfo info)
153 {
154     size_t argc = 1;
155     JSVM_Value argv[1] = {nullptr};
156     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
157 
158     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
159     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], &rstValueType);
160     if (status != JSVM_OK) {
161         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest5: OH_JSVM_Typeof Failed");
162         return nullptr;
163     }
164     if (rstValueType != JSVM_NUMBER) {
165         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest5: OH_JSVM_Typeof type is not JSVM_NUMBER");
166         return nullptr;
167     }
168 
169     bool result = true;
170     JSVM_Value value = nullptr;
171     OH_JSVM_GetBoolean(env, result, &value);
172     return value;
173 }
174 //value is not null, type is JSVM_STRING
TestTypeofTest6(JSVM_Env env,JSVM_CallbackInfo info)175 [[maybe_unused]] JSVM_Value TestTypeofTest6(JSVM_Env env, JSVM_CallbackInfo info)
176 {
177     size_t argc = 1;
178     JSVM_Value argv[1] = {nullptr};
179     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
180 
181     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
182     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], &rstValueType);
183     if (status != JSVM_OK) {
184         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest6: OH_JSVM_Typeof Failed");
185         return nullptr;
186     }
187     if (rstValueType != JSVM_STRING) {
188         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest6: OH_JSVM_Typeof type is not JSVM_STRING");
189         return nullptr;
190     }
191 
192     bool result = true;
193     JSVM_Value value = nullptr;
194     OH_JSVM_GetBoolean(env, result, &value);
195     return value;
196 }
197 //value is not null, type is JSVM_SYMBOL
TestTypeofTest7(JSVM_Env env,JSVM_CallbackInfo info)198 [[maybe_unused]] JSVM_Value TestTypeofTest7(JSVM_Env env, JSVM_CallbackInfo info)
199 {
200     size_t argc = 1;
201     JSVM_Value argv[1] = {nullptr};
202     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
203 
204     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
205     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], &rstValueType);
206     if (status != JSVM_OK) {
207         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest7: OH_JSVM_Typeof Failed");
208         return nullptr;
209     }
210     if (rstValueType != JSVM_SYMBOL) {
211         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest7: OH_JSVM_Typeof type is not JSVM_SYMBOL");
212         return nullptr;
213     }
214 
215     bool result = true;
216     JSVM_Value value = nullptr;
217     OH_JSVM_GetBoolean(env, result, &value);
218     return value;
219 }
220 //value is not null, type is JSVM_OBJECT
TestTypeofTest8(JSVM_Env env,JSVM_CallbackInfo info)221 [[maybe_unused]] JSVM_Value TestTypeofTest8(JSVM_Env env, JSVM_CallbackInfo info)
222 {
223     size_t argc = 1;
224     JSVM_Value argv[1] = {nullptr};
225     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
226 
227     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
228     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], &rstValueType);
229     if (status != JSVM_OK) {
230         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest8: OH_JSVM_Typeof Failed");
231         return nullptr;
232     }
233     if (rstValueType != JSVM_OBJECT) {
234         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest8: OH_JSVM_Typeof type is not JSVM_OBJECT");
235         return nullptr;
236     }
237 
238     bool result = true;
239     JSVM_Value value = nullptr;
240     OH_JSVM_GetBoolean(env, result, &value);
241     return value;
242 }
243 //value is not null, type is JSVM_FUNCTION
TestTypeofTest9(JSVM_Env env,JSVM_CallbackInfo info)244 [[maybe_unused]] JSVM_Value TestTypeofTest9(JSVM_Env env, JSVM_CallbackInfo info)
245 {
246     size_t argc = 1;
247     JSVM_Value argv[1] = {nullptr};
248     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
249 
250     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
251     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], &rstValueType);
252     if (status != JSVM_OK) {
253         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest9: OH_JSVM_Typeof Failed");
254         return nullptr;
255     }
256     if (rstValueType != JSVM_FUNCTION) {
257         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest9: OH_JSVM_Typeof type is not JSVM_FUNCTION");
258         return nullptr;
259     }
260 
261     bool result = true;
262     JSVM_Value value = nullptr;
263     OH_JSVM_GetBoolean(env, result, &value);
264     return value;
265 }
266 //value is not null, type is JSVM_EXTERNAL
TestTypeofTest10(JSVM_Env env,JSVM_CallbackInfo info)267 [[maybe_unused]] JSVM_Value TestTypeofTest10(JSVM_Env env, JSVM_CallbackInfo info)
268 {
269     size_t argc = 1;
270     JSVM_Value argv[1] = {nullptr};
271     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
272 
273     JSVM_Value rstExternal = nullptr;
274     OH_JSVM_CreateExternal(env, nullptr, nullptr, nullptr, &rstExternal);
275     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
276     JSVM_Status status = OH_JSVM_Typeof(env, rstExternal, &rstValueType);
277     if (status != JSVM_OK) {
278         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest10: OH_JSVM_Typeof Failed");
279         return nullptr;
280     }
281     if (rstValueType != JSVM_EXTERNAL) {
282         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest10: OH_JSVM_Typeof type is not JSVM_EXTERNAL");
283         return nullptr;
284     }
285 
286     bool result = true;
287     JSVM_Value value = nullptr;
288     OH_JSVM_GetBoolean(env, result, &value);
289     return value;
290 }
291 //value is not null, type is JSVM_BIGINT
TestTypeofTest11(JSVM_Env env,JSVM_CallbackInfo info)292 [[maybe_unused]] JSVM_Value TestTypeofTest11(JSVM_Env env, JSVM_CallbackInfo info)
293 {
294     size_t argc = 1;
295     JSVM_Value argv[1] = {nullptr};
296     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
297 
298     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
299     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], &rstValueType);
300     if (status != JSVM_OK) {
301         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest11: OH_JSVM_Typeof Failed");
302         return nullptr;
303     }
304     if (rstValueType != JSVM_BIGINT) {
305         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest11: OH_JSVM_Typeof type is not JSVM_BIGINT");
306         return nullptr;
307     }
308 
309     bool result = true;
310     JSVM_Value value = nullptr;
311     OH_JSVM_GetBoolean(env, result, &value);
312     return value;
313 }
314 //value is not null, result is null
TestTypeofTest12(JSVM_Env env,JSVM_CallbackInfo info)315 [[maybe_unused]] JSVM_Value TestTypeofTest12(JSVM_Env env, JSVM_CallbackInfo info)
316 {
317     size_t argc = 1;
318     JSVM_Value argv[1] = {nullptr};
319     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
320 
321     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
322     JSVM_Status status = OH_JSVM_Typeof(env, argv[0], nullptr);
323     if (status == JSVM_OK) {
324         OH_JSVM_ThrowError(env, nullptr, "TestTypeofTest12: OH_JSVM_Typeof Failed");
325         return nullptr;
326     }
327 
328     bool result = true;
329     JSVM_Value value = nullptr;
330     OH_JSVM_GetBoolean(env, result, &value);
331     return value;
332 }
333 //OH_JSVM_TypeTagObject
334 //value is null
TestTypeTagObjectTest1(JSVM_Env env,JSVM_CallbackInfo info)335 [[maybe_unused]] JSVM_Value TestTypeTagObjectTest1(JSVM_Env env, JSVM_CallbackInfo info)
336 {
337     size_t argc = 1;
338     JSVM_Value argv[1] = {nullptr};
339     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
340 
341     JSVM_Status status = OH_JSVM_TypeTagObject(env, nullptr, &tagsData[0]);
342     if (status == JSVM_OK) {
343         OH_JSVM_ThrowError(env, nullptr, "TestTypeTagObjectTest1: OH_JSVM_TypeTagObject Failed");
344         return nullptr;
345     }
346 
347     bool result = true;
348     JSVM_Value value = nullptr;
349     OH_JSVM_GetBoolean(env, result, &value);
350     return value;
351 }
352 //value is not null, object is not connected with typeTag
TestTypeTagObjectTest2(JSVM_Env env,JSVM_CallbackInfo info)353 [[maybe_unused]] JSVM_Value TestTypeTagObjectTest2(JSVM_Env env, JSVM_CallbackInfo info)
354 {
355     size_t argc = 1;
356     JSVM_Value argv[1] = {nullptr};
357     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
358 
359     bool bRstTypeTag = false;
360     JSVM_Status status = OH_JSVM_CheckObjectTypeTag(env, argv[0], &tagsData[0], &bRstTypeTag);
361     if (status != JSVM_OK) {
362         OH_JSVM_ThrowError(env, nullptr, "TestTypeTagObjectTest2: OH_JSVM_CheckObjectTypeTag Failed");
363         return nullptr;
364     }
365     if (bRstTypeTag) {
366         OH_JSVM_ThrowError(env, nullptr, "TestTypeTagObjectTest2: bRstTypeTag is true");
367         return nullptr;
368     }
369     status = OH_JSVM_TypeTagObject(env, argv[0], &tagsData[0]);
370     if (status != JSVM_OK) {
371         OH_JSVM_ThrowError(env, nullptr, "TestTypeTagObjectTest2: OH_JSVM_TypeTagObject Failed");
372         return nullptr;
373     }
374 
375     bool result = true;
376     JSVM_Value value = nullptr;
377     OH_JSVM_GetBoolean(env, result, &value);
378     return value;
379 }
380 //value is not null, object is connected with typeTag
TestTypeTagObjectTest3(JSVM_Env env,JSVM_CallbackInfo info)381 [[maybe_unused]] JSVM_Value TestTypeTagObjectTest3(JSVM_Env env, JSVM_CallbackInfo info)
382 {
383     size_t argc = 1;
384     JSVM_Value argv[1] = {nullptr};
385     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
386 
387     // first OH_JSVM_TypeTagObject
388     JSVM_Status status = OH_JSVM_TypeTagObject(env, argv[0], &tagsData[0]);
389     if (status != JSVM_OK) {
390         OH_JSVM_ThrowError(env, nullptr, "TestTypeTagObjectTest3: OH_JSVM_TypeTagObject Failed");
391         return nullptr;
392     }
393     bool bRstTypeTag = false;
394     status = OH_JSVM_CheckObjectTypeTag(env, argv[0], &tagsData[0], &bRstTypeTag);
395     if (status != JSVM_OK) {
396         OH_JSVM_ThrowError(env, nullptr, "TestTypeTagObjectTest3: OH_JSVM_CheckObjectTypeTag Failed");
397         return nullptr;
398     }
399     if (!bRstTypeTag) {
400         OH_JSVM_ThrowError(env, nullptr, "TestTypeTagObjectTest3: bRstTypeTag is false");
401         return nullptr;
402     }
403     status = OH_JSVM_TypeTagObject(env, argv[0], &tagsData[0]);
404     if (status != JSVM_INVALID_ARG) {
405         OH_JSVM_ThrowError(env, nullptr, "TestTypeTagObjectTest3: OH_JSVM_TypeTagObject Failed");
406         return nullptr;
407     }
408 
409     bool result = true;
410     JSVM_Value value = nullptr;
411     OH_JSVM_GetBoolean(env, result, &value);
412     return value;
413 }
414 //value is not null, typeTag is null
TestTypeTagObjectTest4(JSVM_Env env,JSVM_CallbackInfo info)415 [[maybe_unused]] JSVM_Value TestTypeTagObjectTest4(JSVM_Env env, JSVM_CallbackInfo info)
416 {
417     size_t argc = 1;
418     JSVM_Value argv[1] = {nullptr};
419     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
420 
421     JSVM_Status status = OH_JSVM_TypeTagObject(env, argv[0], nullptr);
422     if (status == JSVM_OK) {
423         OH_JSVM_ThrowError(env, nullptr, "TestTypeTagObjectTest4: OH_JSVM_TypeTagObject Failed");
424         return nullptr;
425     }
426 
427     bool result = true;
428     JSVM_Value value = nullptr;
429     OH_JSVM_GetBoolean(env, result, &value);
430     return value;
431 }
432 //OH_JSVM_CheckObjectTypeTag
433 //value is null
TestCheckObjectTypeTagTest1(JSVM_Env env,JSVM_CallbackInfo info)434 [[maybe_unused]] JSVM_Value TestCheckObjectTypeTagTest1(JSVM_Env env, JSVM_CallbackInfo info)
435 {
436     size_t argc = 1;
437     JSVM_Value argv[1] = {nullptr};
438     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
439 
440     bool bRstTypeTag = false;
441     JSVM_Status status = OH_JSVM_CheckObjectTypeTag(env, nullptr, &tagsData[0], &bRstTypeTag);
442     if (status == JSVM_OK) {
443         OH_JSVM_ThrowError(env, nullptr, "TestCheckObjectTypeTagTest1: OH_JSVM_CheckObjectTypeTag Failed");
444         return nullptr;
445     }
446 
447     bool result = true;
448     JSVM_Value value = nullptr;
449     OH_JSVM_GetBoolean(env, result, &value);
450     return value;
451 }
452 //value is not null, object is not connected with typeTag
TestCheckObjectTypeTagTest2(JSVM_Env env,JSVM_CallbackInfo info)453 [[maybe_unused]] JSVM_Value TestCheckObjectTypeTagTest2(JSVM_Env env, JSVM_CallbackInfo info)
454 {
455     size_t argc = 1;
456     JSVM_Value argv[1] = {nullptr};
457     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
458 
459     bool bRstTypeTag = false;
460     JSVM_Status status = OH_JSVM_CheckObjectTypeTag(env, argv[0], &tagsData[0], &bRstTypeTag);
461     if (status != JSVM_OK) {
462         OH_JSVM_ThrowError(env, nullptr, "TestCheckObjectTypeTagTest2: OH_JSVM_CheckObjectTypeTag Failed");
463         return nullptr;
464     }
465     if (bRstTypeTag) {
466         OH_JSVM_ThrowError(env, nullptr, "TestCheckObjectTypeTagTest2: bRstTypeTag is true");
467         return nullptr;
468     }
469 
470     bool result = true;
471     JSVM_Value value = nullptr;
472     OH_JSVM_GetBoolean(env, result, &value);
473     return value;
474 }
475 //value is not null, object is connected with typeTag
TestCheckObjectTypeTagTest3(JSVM_Env env,JSVM_CallbackInfo info)476 [[maybe_unused]] JSVM_Value TestCheckObjectTypeTagTest3(JSVM_Env env, JSVM_CallbackInfo info)
477 {
478     size_t argc = 1;
479     JSVM_Value argv[1] = {nullptr};
480     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
481 
482     JSVM_Status status = OH_JSVM_TypeTagObject(env, argv[0], &tagsData[0]);
483     if (status != JSVM_OK) {
484         OH_JSVM_ThrowError(env, nullptr, "TestCheckObjectTypeTagTest3: OH_JSVM_TypeTagObject Failed");
485         return nullptr;
486     }
487     bool bRstTypeTag = false;
488     status = OH_JSVM_CheckObjectTypeTag(env, argv[0], &tagsData[0], &bRstTypeTag);
489     if (status != JSVM_OK) {
490         OH_JSVM_ThrowError(env, nullptr, "TestCheckObjectTypeTagTest3: OH_JSVM_CheckObjectTypeTag Failed");
491         return nullptr;
492     }
493     if (!bRstTypeTag) {
494         OH_JSVM_ThrowError(env, nullptr, "TestCheckObjectTypeTagTest3: bRstTypeTag is false");
495         return nullptr;
496     }
497 
498     bool result = true;
499     JSVM_Value value = nullptr;
500     OH_JSVM_GetBoolean(env, result, &value);
501     return value;
502 }
503 //value is not null, typeTag is null
TestCheckObjectTypeTagTest4(JSVM_Env env,JSVM_CallbackInfo info)504 [[maybe_unused]] JSVM_Value TestCheckObjectTypeTagTest4(JSVM_Env env, JSVM_CallbackInfo info)
505 {
506     size_t argc = 1;
507     JSVM_Value argv[1] = {nullptr};
508     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
509 
510     bool bRstTypeTag = false;
511     JSVM_Status status = OH_JSVM_CheckObjectTypeTag(env, argv[0], nullptr, &bRstTypeTag);
512     if (status == JSVM_OK) {
513         OH_JSVM_ThrowError(env, nullptr, "TestCheckObjectTypeTagTest4: OH_JSVM_CheckObjectTypeTag Failed");
514         return nullptr;
515     }
516 
517     bool result = true;
518     JSVM_Value value = nullptr;
519     OH_JSVM_GetBoolean(env, result, &value);
520     return value;
521 }
522 //value is not null, typeTag is not null, result is null
TestCheckObjectTypeTagTest5(JSVM_Env env,JSVM_CallbackInfo info)523 [[maybe_unused]] JSVM_Value TestCheckObjectTypeTagTest5(JSVM_Env env, JSVM_CallbackInfo info)
524 {
525     size_t argc = 1;
526     JSVM_Value argv[1] = {nullptr};
527     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
528 
529     bool bRstTypeTag = false;
530     JSVM_Status status = OH_JSVM_CheckObjectTypeTag(env, argv[0], &tagsData[0], nullptr);
531     if (status == JSVM_OK) {
532         OH_JSVM_ThrowError(env, nullptr, "TestCheckObjectTypeTagTest5: OH_JSVM_CheckObjectTypeTag Failed");
533         return nullptr;
534     }
535 
536     bool result = true;
537     JSVM_Value value = nullptr;
538     OH_JSVM_GetBoolean(env, result, &value);
539     return value;
540 }
541 //createObject -> Typeof -> GetPrototype-> TypeTagObject->CheckObjectTypeTag
542 //-> CheckObjectTypeTag -> Instanceof-> ObjectFreeze -> SetNamedProperty
TestObjectCombinationTest1(JSVM_Env env,JSVM_CallbackInfo info)543 [[maybe_unused]] JSVM_Value TestObjectCombinationTest1(JSVM_Env env, JSVM_CallbackInfo info)
544 {
545     size_t argc = 1;
546     JSVM_Value argv[1] = {nullptr};
547     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
548 
549     //createObject
550     JSVM_Value rstObject;
551     JSVM_Status status = OH_JSVM_CreateObject(env, &rstObject);
552     if (status != JSVM_OK) {
553         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_CreateObject Failed");
554         return nullptr;
555     }
556     //set property
557     const char* strKey = "key";
558     JSVM_Value propertyKey;
559     status = OH_JSVM_CreateStringUtf8(env, strKey, strlen(strKey), &propertyKey);
560     if (status != JSVM_OK) {
561         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_CreateStringUtf8 Failed");
562         return nullptr;
563     }
564     const char* strKeyValue = "key value";
565     JSVM_Value propertyKeyValue;
566     status = OH_JSVM_CreateStringUtf8(env, strKeyValue, strlen(strKeyValue), &propertyKeyValue);
567     if (status != JSVM_OK) {
568         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_CreateStringUtf8 Failed");
569         return nullptr;
570     }
571     status = OH_JSVM_SetProperty(env, rstObject, propertyKey, propertyKeyValue);
572     if (status != JSVM_OK) {
573         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_SetProperty Failed");
574         return nullptr;
575     }
576     // Typeof
577     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
578     status = OH_JSVM_Typeof(env, rstObject, &rstValueType);
579     if (status != JSVM_OK) {
580         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_Typeof Failed");
581         return nullptr;
582     }
583     if (rstValueType != JSVM_OBJECT) {
584         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: type is not JSVM_OBJECT");
585         return nullptr;
586     }
587     //GetPrototype
588     JSVM_Value rstGetPrototype;
589     status = OH_JSVM_GetPrototype(env, rstObject, &rstGetPrototype);
590     if (status != JSVM_OK) {
591         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_GetPrototype Failed");
592         return nullptr;
593     }
594     //TypeTagObject
595     status = OH_JSVM_TypeTagObject(env, rstObject, &tagsData[0]);
596     if (status != JSVM_OK) {
597         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_TypeTagObject Failed");
598         return nullptr;
599     }
600     //CheckObjectTypeTag true
601     bool bRstTypeTag1 = false;
602     status = OH_JSVM_CheckObjectTypeTag(env, rstObject, &tagsData[0], &bRstTypeTag1);
603     if (status != JSVM_OK) {
604         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_CheckObjectTypeTag Failed");
605         return nullptr;
606     }
607     if (!bRstTypeTag1) {
608         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_CheckObjectTypeTag bRstTypeTag = false");
609         return nullptr;
610     }
611     //CheckObjectTypeTag false
612     bool bRstTypeTag2 = false;
613     status = OH_JSVM_CheckObjectTypeTag(env, rstObject, &tagsData[1], &bRstTypeTag2);
614     if (status != JSVM_OK) {
615         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_CheckObjectTypeTag Failed");
616         return nullptr;
617     }
618     if (bRstTypeTag2) {
619         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_CheckObjectTypeTag bRstTypeTag = true");
620         return nullptr;
621     }
622     //OH_JSVM_Instanceof
623     bool rstInstanceof = false;
624     status = OH_JSVM_Instanceof(env, rstObject, argv[0], &rstInstanceof);
625     if (status != JSVM_OK) {
626         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_Instanceof Failed");
627         return nullptr;
628     }
629     if (rstInstanceof) {
630         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: rstInstanceof = true");
631         return nullptr;
632     }
633     //OH_JSVM_ObjectFreeze
634     status = OH_JSVM_ObjectFreeze(env, rstObject);
635     if (status != JSVM_OK) {
636         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_ObjectFreeze Failed");
637         return nullptr;
638     }
639     //OH_JSVM_SetNamedProperty
640     const char* strSetName = "add";
641     const char* strSetValue = "add property";
642     JSVM_Value setValue;
643     status = OH_JSVM_CreateStringUtf8(env, strSetValue, JSVM_AUTO_LENGTH, &setValue);
644     if (status != JSVM_OK) {
645         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_CreateStringUtf8 Failed");
646         return nullptr;
647     }
648     status = OH_JSVM_SetNamedProperty(env, rstObject, strSetName, setValue);
649     if (status != JSVM_OK) {
650         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_SetNamedProperty Failed");
651         return nullptr;
652     }
653     //OH_JSVM_HasNamedProperty
654     bool bRstHasNamed = false;
655     status = OH_JSVM_HasNamedProperty(env, rstObject, strSetName, &bRstHasNamed);
656     if (status != JSVM_OK) {
657         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: OH_JSVM_HasNamedProperty Failed");
658         return nullptr;
659     }
660     if (bRstHasNamed) {
661         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest1: bRstHasNamed is true");
662         return nullptr;
663     }
664 
665     bool result = true;
666     JSVM_Value value = nullptr;
667     OH_JSVM_GetBoolean(env, result, &value);
668     return value;
669 }
670 //object,set property,key is CreateSymbol create Symbol-> SymbolFor -> SymbolFor create new Symbol
671 //-> Typeof -> GetPrototype-> TypeTagObject-> CheckObjectTypeTag -> ObjectSeal-> SetNamedProperty
TestObjectCombinationTest2(JSVM_Env env,JSVM_CallbackInfo info)672 [[maybe_unused]] JSVM_Value TestObjectCombinationTest2(JSVM_Env env, JSVM_CallbackInfo info)
673 {
674     size_t argc = 1;
675     JSVM_Value argv[1] = {nullptr};
676     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
677 
678     //create object
679     JSVM_Value rstObject = nullptr;
680     JSVM_Status status = OH_JSVM_CreateObject(env, &rstObject);
681     if (status != JSVM_OK) {
682         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_CreateObject Failed");
683         return nullptr;
684     }
685     //OH_JSVM_CreateSymbol
686     JSVM_Value rstDes = nullptr;
687     const char *strDes = "only";
688     OH_JSVM_CreateStringUtf8(env, strDes, JSVM_AUTO_LENGTH, &rstDes);
689     JSVM_Value rstSymbol = nullptr;
690     status = OH_JSVM_CreateSymbol(env, rstDes, &rstSymbol);
691     if (status != JSVM_OK) {
692         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_CreateStringUtf8 Failed");
693         return nullptr;
694     }
695     //set property
696     const char *strKeyValue = "only the key value";
697     JSVM_Value keyValue = nullptr;
698     OH_JSVM_CreateStringUtf8(env, strKeyValue, JSVM_AUTO_LENGTH, &keyValue);
699     status = OH_JSVM_SetProperty(env, rstObject, rstSymbol, keyValue);
700     if (status != JSVM_OK) {
701         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_SetProperty Failed");
702         return nullptr;
703     }
704     //OH_JSVM_SymbolFor first
705     JSVM_Value rstSymbolFor = nullptr;
706     status = OH_JSVM_SymbolFor(env, strDes, JSVM_AUTO_LENGTH, &rstSymbolFor);
707     if (status != JSVM_OK) {
708         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_SymbolFor Failed");
709         return nullptr;
710     }
711     //OH_JSVM_SymbolFor second
712     const char* strNewDes = "new one";
713     JSVM_Value rstNewSymbolFor = nullptr;
714     status = OH_JSVM_SymbolFor(env, strNewDes, JSVM_AUTO_LENGTH, &rstNewSymbolFor);
715     if (status != JSVM_OK) {
716         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_SymbolFor Failed");
717         return nullptr;
718     }
719     // Typeof
720     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
721     status = OH_JSVM_Typeof(env, rstSymbolFor, &rstValueType);
722     if (status != JSVM_OK) {
723         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_Typeof Failed");
724         return nullptr;
725     }
726     if (rstValueType != JSVM_SYMBOL) {
727         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: type is not JSVM_UNDEFINED");
728         return nullptr;
729     }
730     status = OH_JSVM_Typeof(env, rstNewSymbolFor, &rstValueType);
731     if (status != JSVM_OK) {
732         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_Typeof Failed");
733         return nullptr;
734     }
735     if (rstValueType != JSVM_SYMBOL) {
736         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: type is not JSVM_UNDEFINED");
737         return nullptr;
738     }
739     //GetPrototype
740     JSVM_Value rstGetPrototype = nullptr;
741     status = OH_JSVM_GetPrototype(env, rstNewSymbolFor, &rstGetPrototype);
742     if (status != JSVM_OK) {
743         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_GetPrototype Failed");
744         return nullptr;
745     }
746     //TypeTagObject
747     status = OH_JSVM_TypeTagObject(env, rstObject, &tagsData[1]);
748     if (status != JSVM_OK) {
749         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_TypeTagObject Failed");
750         return nullptr;
751     }
752     //CheckObjectTypeTag true
753     bool bRstTypeTag1 = false;
754     status = OH_JSVM_CheckObjectTypeTag(env, rstObject, &tagsData[1], &bRstTypeTag1);
755     if (status != JSVM_OK) {
756         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_CheckObjectTypeTag Failed");
757         return nullptr;
758     }
759     if (!bRstTypeTag1) {
760         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_CheckObjectTypeTag bRstTypeTag = false");
761         return nullptr;
762     }
763     //OH_JSVM_ObjectSeal
764     status = OH_JSVM_ObjectSeal(env, rstNewSymbolFor);
765     if (status != JSVM_OK) {
766         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_ObjectSeal Failed");
767         return nullptr;
768     }
769     //OH_JSVM_SetNamedProperty
770     const char* strSetName = "add";
771     const char* strSetValue = "add property";
772     JSVM_Value setValue;
773     OH_JSVM_CreateStringUtf8(env, strSetValue, JSVM_AUTO_LENGTH, &setValue);
774     status = OH_JSVM_SetNamedProperty(env, rstNewSymbolFor, strSetName, setValue);
775     if (status != JSVM_OK) {
776         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_SetNamedProperty Failed");
777         return nullptr;
778     }
779     //OH_JSVM_HasNamedProperty
780     bool bRstHasNamed = false;
781     status = OH_JSVM_HasNamedProperty(env, rstNewSymbolFor, strSetName, &bRstHasNamed);
782     if (status != JSVM_OK) {
783         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: OH_JSVM_HasNamedProperty Failed");
784         return nullptr;
785     }
786     if (bRstHasNamed) {
787         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest2: bRstHasNamed is true");
788         return nullptr;
789     }
790 
791     bool result = true;
792     JSVM_Value value = nullptr;
793     OH_JSVM_GetBoolean(env, result, &value);
794     return value;
795 }
796 //CreateExternal create External -> GetValueExternal  -> Typeof-> GetPrototype -> TypeTagObject
797 //-> CheckObjectTypeTag
TestObjectCombinationTest3(JSVM_Env env,JSVM_CallbackInfo info)798 [[maybe_unused]] JSVM_Value TestObjectCombinationTest3(JSVM_Env env, JSVM_CallbackInfo info)
799 {
800     //CreateExternal
801     void *ptrData = malloc(BUF_SIZE_10);
802     memset_s(ptrData, BUF_SIZE_10, 0, BUF_SIZE_10);
803     const char* strExternal = "test";
804     JSVM_Value externalValue = nullptr;
805     JSVM_Status status = OH_JSVM_CreateExternal(
806         env, (void *)strExternal, [](JSVM_Env env, void *data, void *hint) {}, (void *)strExternal, &externalValue);
807     if (status != JSVM_OK) {
808         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest3: OH_JSVM_CreateExternal Failed");
809         return nullptr;
810     }
811     //GetValueExternal
812     void* ptrRstExternal;
813     status = OH_JSVM_GetValueExternal(env, externalValue, &ptrRstExternal);
814     if (status != JSVM_OK) {
815         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest3: OH_JSVM_GetValueExternal Failed");
816         return nullptr;
817     }
818     // Typeof
819     JSVM_ValueType rstValueType = JSVM_UNDEFINED;
820     status = OH_JSVM_Typeof(env, externalValue, &rstValueType);
821     if ((status != JSVM_OK) || (rstValueType != JSVM_UNDEFINED)) {
822         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest3: OH_JSVM_Typeof Failed");
823         return nullptr;
824     }
825     //GetPrototype
826     JSVM_Value rstGetPrototype = nullptr;
827     status = OH_JSVM_GetPrototype(env, externalValue, &rstGetPrototype);
828     if (status != JSVM_OK) {
829         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest3: OH_JSVM_GetPrototype Failed");
830         return nullptr;
831     }
832     //TypeTagObject
833     status = OH_JSVM_TypeTagObject(env, externalValue, &tagsData[0]);
834     if (status != JSVM_OK) {
835         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest3: OH_JSVM_TypeTagObject Failed");
836         return nullptr;
837     }
838     //CheckObjectTypeTag true
839     bool bRstTypeTag = false;
840     status = OH_JSVM_CheckObjectTypeTag(env, externalValue, &tagsData[0], &bRstTypeTag);
841     if ((status != JSVM_OK) || (!bRstTypeTag)) {
842         OH_JSVM_ThrowError(env, nullptr, "TestObjectCombinationTest3: OH_JSVM_CheckObjectTypeTag Failed");
843         return nullptr;
844     }
845     free(ptrData);
846     ptrData = nullptr;
847     bool result = true;
848     JSVM_Value value = nullptr;
849     OH_JSVM_GetBoolean(env, result, &value);
850     return value;
851 }