1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "napi/native_api.h"
16 #include "jsvm.h"
17 #include "jsvm_common.h"
18 #include "native_common.h"
19 #include "test_entry.h"
20 #include "securec.h"
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <malloc.h>
24 #include <ctime>
25 #include <thread>
26 #include <uv.h>
27 #include <bits/alltypes.h>
28 #include <unistd.h>
29 #include <cstring>
30 #include <fstream>
31 #include <string>
32 #include "napi_string_test.h"
33 #include "napi_arraytest.h"
34 #include "napi_error_test.h"
35 #include "napi_property_test.h"
36 #include "napi_strictqeuals_test.h"
37 #include "napi_executetasks_test.h"
38 #include "napi_object_test.h"
39 #include "napi_datatype_test.h"
40 #include "napi_date_test.h"
41 #include "napi_json_test.h"
42 #include "napi_arraybuffer_test.h"
43 #include "napi_basicdatatypes_test.h"
44 #include "napi_bigint_test.h"
45 #include "napi_class_test.h"
46 #include "napi_functioncall_test.h"
47 #include "napi_heapstatisticsdebugg_test.h"
48 #include "napi_lifecycle_test.h"
49 #include "napi_memorymanagementuse_test.h"
50 #include "napi_promise_test.h"
51 #include "napi_version_test.h"
52 #include <vector>
53 #define LOG_DEMAIN 0x0202
54 using namespace std;
55 static int aa = 0;
56 static bool g_temp = false;
57 const int DIFF_VALUE_ZERO = 0;
58 const int DIFF_VALUE_NOE = 1;
59 const int DIFF_VALUE_TWO = 2;
60 const int DIFF_VALUE_THREE = 3;
61 const int DIFF_VALUE_FOUR = 4;
62 const int DIFF_VALUE_FIVE = 5;
63 const int DIFF_VALUE_SIX = 6;
64 const int DIFF_VALUE_SEVEN = 7;
65 const int DIFF_VALUE_EIGHT = 8;
66 const int DIFF_VALUE_NINE = 9;
67 const int DIFF_VALUE_TEN = 10;
68 const int DIFF_VALUE_ELEVEN = 11;
69 const int DIFF_VALUE_TWELVE = 12;
70 const int DIFF_VALUE_THIRTEEN = 13;
71 const int DIFF_VALUE_FOURTEEN = 14;
72 const int DIFF_VALUE_FITEEN = 15;
73 const int DIFF_VALUE_SIXTEEN = 16;
74 const int DIFF_VALUE_SEVENTEEN = 17;
75 const int DIFF_VALUE_EIGHTEEN = 18;
76 const int DIFF_VALUE_NINETEEN = 19;
77 const int DIFF_VALUE_TWENTY = 20;
78 const int DIFF_VALUE_TWENTYONE = 21;
79 const int SCOPE_FOR_NUMBER = 100000;
80 const int RUN_JS_CODE_LENGTH = 1000;
81 const int DIFF_VALUE_INT32 = -20;
82 const int64_t DIFF_VALUE_INT = 2147483648;
83 const int64_t DIFF_VALUE_BIGINT = -5555555555555555555;
84 const uint64_t DIFF_VALUE_BIGUINT = 5555555555555555555;
85 const int32_t DIFF_VALUE_KEYLEN = 32;
86 const uint32_t DIFF_VALUE_UINT = 26;
87 const double DIFF_VALUE_DOUBLE = 1.234;
88 const double DIFF_VALUE_DATE = 1501924876711;
89 static char g_dataType[32] = "";
90 static bool g_threadFlag1 = false;
91 static bool g_threadFlag2 = false;
92 const int RUN_TEST_RETAIN_SCRIPT_VALUE = 121;
93 const int TWO = 2;
94 const int SIXTEEN = 16;
95
96 #define EOK 0
97 #define OPENCODE
98 #ifdef OPENCODE
addReturnedStatus(JSVM_Env env,const char * key,JSVM_Value object,const char * expected_message,JSVM_Status expected_status,JSVM_Status actual_status)99 static void addReturnedStatus(JSVM_Env env, const char *key, JSVM_Value object, const char *expected_message,
100 JSVM_Status expected_status, JSVM_Status actual_status) {
101 char napi_message_string[100] = "";
102 JSVM_Value prop_value;
103
104 if (actual_status != expected_status) {
105 printf("Invalid status [%d]", actual_status);
106 }
107
108 JSVM_CALL_RETURN_VOID(
109 env, OH_JSVM_CreateStringUtf8(env, (actual_status == expected_status ? expected_message : napi_message_string),
110 NAPI_AUTO_LENGTH, &prop_value));
111 JSVM_CALL_RETURN_VOID(env, OH_JSVM_SetNamedProperty(env, object, key, prop_value));
112 }
113
addLastStatus(JSVM_Env env,const char * key,JSVM_Value return_value)114 static void addLastStatus(JSVM_Env env, const char *key, JSVM_Value return_value) {
115 JSVM_Value prop_value;
116 const JSVM_ExtendedErrorInfo *p_last_error;
117 JSVM_CALL_RETURN_VOID(env, OH_JSVM_GetLastErrorInfo(env, &p_last_error));
118
119 JSVM_CALL_RETURN_VOID(
120 env, OH_JSVM_CreateStringUtf8(
121 env, (p_last_error->errorMessage == NULL ? "JSVM_Status::JSVM_OK" : p_last_error->errorMessage),
122 NAPI_AUTO_LENGTH, &prop_value));
123 JSVM_CALL_RETURN_VOID(env, OH_JSVM_SetNamedProperty(env, return_value, key, prop_value));
124 }
125 #endif
defineProperties(JSVM_Env env,JSVM_CallbackInfo info)126 static JSVM_Value defineProperties(JSVM_Env env, JSVM_CallbackInfo info) {
127 JSVM_Value object, return_value;
128 JSVM_CallbackStruct param;
129 param.data = nullptr;
130 param.callback = defineProperties;
131 JSVM_PropertyDescriptor desc = {"prop", NULL, ¶m, NULL, NULL, NULL, JSVM_PropertyAttributes::JSVM_ENUMERABLE};
132
133 JSVM_CALL(env, OH_JSVM_CreateObject(env, &object));
134 JSVM_CALL(env, OH_JSVM_CreateObject(env, &return_value));
135
136 addReturnedStatus(env, "envIsNull", return_value, "Invalid argument", JSVM_Status::JSVM_INVALID_ARG,
137 OH_JSVM_DefineProperties(NULL, object, 1, &desc));
138
139 OH_JSVM_DefineProperties(env, NULL, 1, &desc);
140 addLastStatus(env, "objectIsNull", return_value);
141
142 OH_JSVM_DefineProperties(env, object, 1, NULL);
143 addLastStatus(env, "descriptorListIsNull", return_value);
144
145 desc.utf8name = NULL;
146 OH_JSVM_DefineProperties(env, object, 1, NULL);
147 addLastStatus(env, "utf8nameIsNull", return_value);
148 desc.utf8name = "prop";
149
150 desc.method = NULL;
151 OH_JSVM_DefineProperties(env, object, 1, NULL);
152 addLastStatus(env, "methodIsNull", return_value);
153 desc.method = ¶m;
154
155 return return_value;
156 }
assertEqual(JSVM_Env env,JSVM_CallbackInfo info)157 static JSVM_Value assertEqual(JSVM_Env env, JSVM_CallbackInfo info) {
158 size_t argc = 2;
159 JSVM_Value args[2];
160 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL));
161
162 bool isStrictEquals = false;
163 OH_JSVM_StrictEquals(env, args[0], args[1], &isStrictEquals);
164 return nullptr;
165 }
166
assertTrue(JSVM_Env env,JSVM_CallbackInfo info)167 static JSVM_Value assertTrue(JSVM_Env env, JSVM_CallbackInfo info) {
168 size_t argc = 1;
169 JSVM_Value args[1];
170 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL));
171
172 bool value = false;
173 JSVM_CALL(env, OH_JSVM_GetValueBool(env, args[0], &value));
174 return nullptr;
175 }
176
assertFalse(JSVM_Env env,JSVM_CallbackInfo info)177 static JSVM_Value assertFalse(JSVM_Env env, JSVM_CallbackInfo info) {
178 size_t argc = 1;
179 JSVM_Value args[1];
180 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL));
181
182 bool value = false;
183 JSVM_CALL(env, OH_JSVM_GetValueBool(env, args[0], &value));
184 return nullptr;
185 }
186
consoleinfo(JSVM_Env env,JSVM_CallbackInfo info)187 static JSVM_Value consoleinfo(JSVM_Env env, JSVM_CallbackInfo info) {
188 size_t argc = 1;
189 JSVM_Value args[1];
190 char log[256] = "";
191 size_t log_length;
192 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL));
193
194 JSVM_CALL(env, OH_JSVM_GetValueStringUtf8(env, args[0], log, 255, &log_length));
195 return nullptr;
196 }
197
198
199 // OH_JSVM_IsArray
isArray(JSVM_Env env,JSVM_CallbackInfo info)200 static JSVM_Value isArray(JSVM_Env env, JSVM_CallbackInfo info) {
201 JSVM_Value array = nullptr;
202 OH_JSVM_CreateArray(env, &array);
203 JSVM_ASSERT(env, array != nullptr, "OH_JSVM_GetUndefined success");
204 bool isArray = false;
205 OH_JSVM_IsArray(env, array, &isArray);
206 JSVM_ASSERT(env, isArray, "OH_JSVM_IsArrayd success");
207
208 JSVM_Value _value;
209 JSVM_CALL(env, OH_JSVM_CreateInt32(env, 0, &_value));
210 return _value;
211 }
hello(JSVM_Env env,JSVM_CallbackInfo info)212 static JSVM_Value hello(JSVM_Env env, JSVM_CallbackInfo info) {
213 JSVM_Value output;
214 void *data = nullptr;
215 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, nullptr, &data);
216 OH_JSVM_CreateStringUtf8(env, (char *)data, strlen((char *)data), &output);
217 return output;
218 }
219 static JSVM_CallbackStruct hello_cb = {hello, (void *)"Hello"};
220 static intptr_t externals[] = {
221 (intptr_t)&hello_cb,
222 0,
223 };
224
225 static JSVM_PropertyHandlerConfigurationStruct propertyCfg{
226 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr
227 };
228
testEngineAndContext(napi_env env1,napi_callback_info info)229 static napi_value testEngineAndContext(napi_env env1, napi_callback_info info){
230 JSVM_InitOptions init_options;
231 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
232 printf("memset_s failed");
233 return nullptr;
234 }
235 init_options.externalReferences = externals;
236 if (aa == 0) {
237 OH_JSVM_Init(&init_options);
238 aa++;
239 }
240 JSVM_VM vm;
241 JSVM_CreateVMOptions options;
242 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
243 printf("memset_s failed");
244 return nullptr;
245 }
246 OH_JSVM_CreateVM(&options, &vm);
247 JSVM_VMScope vm_scope;
248 OH_JSVM_OpenVMScope(vm, &vm_scope);
249 JSVM_Env env;
250 JSVM_CallbackStruct param[1];
251 param[0].data = nullptr;
252 param[0].callback = assertEqual;
253
254
255 JSVM_PropertyDescriptor descriptor[] = {
256 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
257 };
258 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
259 JSVM_EnvScope envScope;
260 OH_JSVM_OpenEnvScope(env, &envScope);
261 JSVM_HandleScope handlescope;
262 OH_JSVM_OpenHandleScope(env, &handlescope);
263 const char *sourcecodestr = "\
264 {\
265 var length = 5; var arr = new Array(length); arr[4] = 123;\
266 }\
267 ";
268 JSVM_Value sourcecodevalue;
269 OH_JSVM_CreateStringUtf8(env, sourcecodestr, strlen(sourcecodestr), &sourcecodevalue);
270 JSVM_Script script;
271 OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script);
272 JSVM_Value result;
273 OH_JSVM_RunScript(env, script, &result);
274 OH_JSVM_CloseHandleScope(env, handlescope);
275 OH_JSVM_CloseEnvScope(env, envScope);
276 OH_JSVM_DestroyEnv(env);
277 OH_JSVM_CloseVMScope(vm, vm_scope);
278 OH_JSVM_DestroyVM(vm);
279 napi_value result11;
280 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
281 return result11;
282 }
testError(napi_env env1,napi_callback_info info)283 static napi_value testError(napi_env env1, napi_callback_info info){
284 JSVM_InitOptions init_options;
285 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
286 printf("memset_s failed");
287 return nullptr;
288 }
289 init_options.externalReferences = externals;
290 if (aa == 0) {
291 OH_JSVM_Init(&init_options);
292 aa++;
293 }
294 JSVM_VM vm;
295 JSVM_CreateVMOptions options;
296 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
297 printf("memset_s failed");
298 return nullptr;
299 }
300 OH_JSVM_CreateVM(&options, &vm);
301 JSVM_VMScope vm_scope;
302 OH_JSVM_OpenVMScope(vm, &vm_scope);
303 JSVM_Env env;
304 JSVM_CallbackStruct param[1];
305 param[0].data = nullptr;
306 param[0].callback = assertEqual;
307 JSVM_PropertyDescriptor descriptor[] = {
308 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
309 };
310 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
311 JSVM_EnvScope envScope;
312 OH_JSVM_OpenEnvScope(env, &envScope);
313 JSVM_HandleScope handlescope;
314 OH_JSVM_OpenHandleScope(env, &handlescope);
315 //error
316 JSVM_Value code = nullptr;
317 JSVM_Value message = nullptr;
318 OH_JSVM_CreateStringLatin1(env, "600", NAPI_AUTO_LENGTH, &code);
319 OH_JSVM_CreateStringLatin1(env, "test error", NAPI_AUTO_LENGTH, &message);
320 JSVM_Value error = nullptr;
321 OH_JSVM_CreateError(env, code, message, &error);
322 bool isError = false;
323 OH_JSVM_IsError(env, error, &isError);
324 OH_JSVM_ThrowError(env, "500", "Common error");
325 OH_JSVM_CloseHandleScope(env, handlescope);
326 OH_JSVM_CloseEnvScope(env, envScope);
327 OH_JSVM_DestroyEnv(env);
328 OH_JSVM_CloseVMScope(vm, vm_scope);
329 OH_JSVM_DestroyVM(vm);
330 napi_value result11;
331 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
332 return result11;
333 }
testTypeError(napi_env env1,napi_callback_info info)334 static napi_value testTypeError(napi_env env1, napi_callback_info info){
335 JSVM_InitOptions init_options;
336 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
337 printf("memset_s failed");
338 return nullptr;
339 }
340 init_options.externalReferences = externals;
341 if (aa == 0) {
342 OH_JSVM_Init(&init_options);
343 aa++;
344 }
345 JSVM_VM vm;
346 JSVM_CreateVMOptions options;
347 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
348 printf("memset_s failed");
349 return nullptr;
350 }
351 OH_JSVM_CreateVM(&options, &vm);
352 JSVM_VMScope vm_scope;
353 OH_JSVM_OpenVMScope(vm, &vm_scope);
354 JSVM_Env env;
355 JSVM_CallbackStruct param[1];
356 param[0].data = nullptr;
357 param[0].callback = assertEqual;
358
359 JSVM_PropertyDescriptor descriptor[] = {
360 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
361 };
362 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
363 JSVM_EnvScope envScope;
364 OH_JSVM_OpenEnvScope(env, &envScope);
365 JSVM_HandleScope handlescope;
366 OH_JSVM_OpenHandleScope(env, &handlescope);
367 // typeError
368 JSVM_Value code1 = nullptr;
369 JSVM_Value message1 = nullptr;
370 OH_JSVM_CreateStringLatin1(env, "500", NAPI_AUTO_LENGTH, &code1);
371 OH_JSVM_CreateStringLatin1(env, "type error 500", NAPI_AUTO_LENGTH, &message1);
372 JSVM_Value error1 = nullptr;
373 OH_JSVM_CreateTypeError(env, code1, message1, &error1);
374 bool isError1 = false;
375 OH_JSVM_IsError(env, error1, &isError1);
376 OH_JSVM_ThrowTypeError(env, NULL, "type error1");
377 OH_JSVM_CloseHandleScope(env, handlescope);
378 OH_JSVM_CloseEnvScope(env, envScope);
379 OH_JSVM_DestroyEnv(env);
380 OH_JSVM_CloseVMScope(vm, vm_scope);
381 OH_JSVM_DestroyVM(vm);
382 napi_value result11;
383 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
384 return result11;
385 }
testRangeError(napi_env env1,napi_callback_info info)386 static napi_value testRangeError(napi_env env1, napi_callback_info info){
387 JSVM_InitOptions init_options;
388 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
389 printf("memset_s failed");
390 return nullptr;
391 }
392 init_options.externalReferences = externals;
393 if (aa == 0) {
394 OH_JSVM_Init(&init_options);
395 aa++;
396 }
397 JSVM_VM vm;
398 JSVM_CreateVMOptions options;
399 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
400 printf("memset_s failed");
401 return nullptr;
402 }
403 OH_JSVM_CreateVM(&options, &vm);
404 JSVM_VMScope vm_scope;
405 OH_JSVM_OpenVMScope(vm, &vm_scope);
406 JSVM_Env env;
407 JSVM_CallbackStruct param[1];
408 param[0].data = nullptr;
409 param[0].callback = assertEqual;
410
411 JSVM_PropertyDescriptor descriptor[] = {
412 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
413 };
414 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
415 JSVM_EnvScope envScope;
416 OH_JSVM_OpenEnvScope(env, &envScope);
417 JSVM_HandleScope handlescope;
418 OH_JSVM_OpenHandleScope(env, &handlescope);
419 // rangeError
420 JSVM_Value code2 = nullptr;
421 JSVM_Value message2 = nullptr;
422 OH_JSVM_CreateStringLatin1(env, "500", NAPI_AUTO_LENGTH, &code2);
423 OH_JSVM_CreateStringLatin1(env, "range error 500", NAPI_AUTO_LENGTH, &message2);
424 JSVM_Value error2 = nullptr;
425 OH_JSVM_CreateRangeError(env, code2, message2, &error2);
426 bool isError2 = false;
427 OH_JSVM_IsError(env, error2, &isError2);
428 OH_JSVM_ThrowRangeError(env, NULL, "range error");
429 OH_JSVM_CloseHandleScope(env, handlescope);
430 OH_JSVM_CloseEnvScope(env, envScope);
431 OH_JSVM_DestroyEnv(env);
432 OH_JSVM_CloseVMScope(vm, vm_scope);
433 OH_JSVM_DestroyVM(vm);
434 napi_value result11;
435 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
436 return result11;
437 }
testSyntaxError(napi_env env1,napi_callback_info info)438 static napi_value testSyntaxError(napi_env env1, napi_callback_info info){
439 JSVM_InitOptions init_options;
440 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
441 printf("memset_s failed");
442 return nullptr;
443 }
444 init_options.externalReferences = externals;
445 if (aa == 0) {
446 OH_JSVM_Init(&init_options);
447 aa++;
448 }
449 JSVM_VM vm;
450 JSVM_CreateVMOptions options;
451 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
452 printf("memset_s failed");
453 return nullptr;
454 }
455 OH_JSVM_CreateVM(&options, &vm);
456 JSVM_VMScope vm_scope;
457 OH_JSVM_OpenVMScope(vm, &vm_scope);
458 JSVM_Env env;
459 JSVM_CallbackStruct param[1];
460 param[0].data = nullptr;
461 param[0].callback = assertEqual;
462
463 JSVM_PropertyDescriptor descriptor[] = {
464 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
465 };
466 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
467 JSVM_EnvScope envScope;
468 OH_JSVM_OpenEnvScope(env, &envScope);
469 JSVM_HandleScope handlescope;
470 OH_JSVM_OpenHandleScope(env, &handlescope);
471 // syntaxError
472 JSVM_Value code3 = nullptr;
473 JSVM_Value message3 = nullptr;
474 OH_JSVM_CreateStringLatin1(env, "500", NAPI_AUTO_LENGTH, &code3);
475 OH_JSVM_CreateStringLatin1(env, "syntax error 500", NAPI_AUTO_LENGTH, &message3);
476 JSVM_Value error3 = nullptr;
477 OH_JSVM_CreateSyntaxError(env, code3, message3, &error3);
478 bool isError3 = false;
479 OH_JSVM_IsError(env, error3, &isError3);
480 OH_JSVM_ThrowSyntaxError(env, NULL, "syntax error");
481 OH_JSVM_CloseHandleScope(env, handlescope);
482 OH_JSVM_CloseEnvScope(env, envScope);
483 OH_JSVM_DestroyEnv(env);
484 OH_JSVM_CloseVMScope(vm, vm_scope);
485 OH_JSVM_DestroyVM(vm);
486 napi_value result11;
487 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
488 return result11;
489 }
testClearError(napi_env env1,napi_callback_info info)490 static napi_value testClearError(napi_env env1, napi_callback_info info){
491 JSVM_InitOptions init_options;
492 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
493 printf("memset_s failed");
494 return nullptr;
495 }
496 init_options.externalReferences = externals;
497 if (aa == 0) {
498 OH_JSVM_Init(&init_options);
499 aa++;
500 }
501 JSVM_VM vm;
502 JSVM_CreateVMOptions options;
503 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
504 printf("memset_s failed");
505 return nullptr;
506 }
507 OH_JSVM_CreateVM(&options, &vm);
508 JSVM_VMScope vm_scope;
509 OH_JSVM_OpenVMScope(vm, &vm_scope);
510 JSVM_Env env;
511 JSVM_CallbackStruct param[1];
512 param[0].data = nullptr;
513 param[0].callback = assertEqual;
514
515 JSVM_PropertyDescriptor descriptor[] = {
516 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
517 };
518 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
519 JSVM_EnvScope envScope;
520 OH_JSVM_OpenEnvScope(env, &envScope);
521 JSVM_HandleScope handlescope;
522 OH_JSVM_OpenHandleScope(env, &handlescope);
523 // getLastErrorInfo
524 JSVM_Value value;
525 OH_JSVM_CreateStringUtf8(env, "xyz", 3, &value);
526 double double_value;
527 OH_JSVM_GetValueDouble(env, value, &double_value);
528 const JSVM_ExtendedErrorInfo *error_info = 0;
529 OH_JSVM_GetLastErrorInfo(env, &error_info);
530 // getAndClearLastException
531 JSVM_Value code4 = nullptr;
532 JSVM_Value message4 = nullptr;
533 OH_JSVM_CreateStringLatin1(env, "600", NAPI_AUTO_LENGTH, &code4);
534 OH_JSVM_CreateStringLatin1(env, "tag range error", NAPI_AUTO_LENGTH, &message4);
535 JSVM_Value error4 = nullptr;
536 OH_JSVM_CreateRangeError(env, code4, message4, &error4);
537 bool isError4 = false;
538 OH_JSVM_IsError(env, error4, &isError4);
539 JSVM_Value ex;
540 OH_JSVM_GetAndClearLastException(env, &ex);
541 bool exceptionWasPending = true;
542 OH_JSVM_IsExceptionPending(env, &exceptionWasPending);
543 OH_JSVM_Throw(env, error4);
544 OH_JSVM_IsExceptionPending(env, &exceptionWasPending);
545 JSVM_Value ex1;
546 OH_JSVM_GetAndClearLastException(env, &ex1);
547 isError4 = false;
548 OH_JSVM_IsError(env, ex1, &isError4);
549 OH_JSVM_IsExceptionPending(env, &exceptionWasPending);
550 OH_JSVM_CloseHandleScope(env, handlescope);
551 OH_JSVM_CloseEnvScope(env, envScope);
552 OH_JSVM_DestroyEnv(env);
553 // shaoshuai
554 OH_JSVM_CloseVMScope(vm, vm_scope);
555 OH_JSVM_DestroyVM(vm);
556 napi_value result11;
557 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
558 return result11;
559 }
testLifeCycle(napi_env env1,napi_callback_info info)560 static napi_value testLifeCycle(napi_env env1, napi_callback_info info){
561 JSVM_InitOptions init_options;
562 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
563 printf("memset_s failed");
564 return nullptr;
565 }
566 init_options.externalReferences = externals;
567 if (aa == 0) {
568 OH_JSVM_Init(&init_options);
569 aa++;
570 }
571 JSVM_VM vm;
572 JSVM_CreateVMOptions options;
573 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
574 printf("memset_s failed");
575 return nullptr;
576 }
577 OH_JSVM_CreateVM(&options, &vm);
578 JSVM_VMScope vm_scope;
579 OH_JSVM_OpenVMScope(vm, &vm_scope);
580 JSVM_Env env;
581 JSVM_CallbackStruct param[1];
582 param[0].data = nullptr;
583 param[0].callback = assertEqual;
584 JSVM_PropertyDescriptor descriptor[] = {
585 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
586 };
587 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
588 JSVM_EnvScope envScope;
589 OH_JSVM_OpenEnvScope(env, &envScope);
590 JSVM_HandleScope handlescope;
591 OH_JSVM_OpenHandleScope(env, &handlescope);
592 JSVM_EscapableHandleScope handlescope1;
593 OH_JSVM_OpenEscapableHandleScope(env,&handlescope1);
594 JSVM_Value output = NULL;
595 JSVM_Value escapee = NULL;
596 OH_JSVM_CreateObject(env, &output);
597 OH_JSVM_EscapeHandle(env, handlescope1, output, &escapee);
598 JSVM_Value result = nullptr;
599 JSVM_Ref resultRef = nullptr;
600 uint32_t resultRefCount = 0;
601 OH_JSVM_CreateObject(env, &result);
602 OH_JSVM_CreateReference(env, result, 1, &resultRef);
603 OH_JSVM_ReferenceRef(env, resultRef, &resultRefCount);
604 OH_JSVM_ReferenceUnref(env, resultRef, &resultRefCount);
605 JSVM_Value refValue = nullptr;
606 OH_JSVM_GetReferenceValue(env, resultRef, &refValue);
607 OH_JSVM_DeleteReference(env, resultRef);
608 OH_JSVM_CloseEscapableHandleScope(env,handlescope1);
609 OH_JSVM_CloseHandleScope(env, handlescope);
610 OH_JSVM_CloseEnvScope(env, envScope);
611 OH_JSVM_DestroyEnv(env);
612 OH_JSVM_CloseVMScope(vm, vm_scope);
613 OH_JSVM_DestroyVM(vm);
614 napi_value result11;
615 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
616 return result11;
617 }
CreateReference_test_1(JSVM_Env env)618 void CreateReference_test_1(JSVM_Env env)
619 {
620 JSVM_HandleScope handleScope;
621 OH_JSVM_OpenHandleScope(env, &handleScope);
622 JSVM_EscapableHandleScope handleScope1;
623 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
624 const char testStr[] = "foo";
625 JSVM_Value value = nullptr;
626 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
627 JSVM_Ref reference = nullptr;
628 OH_JSVM_CreateReference(env, value, 1, &reference);
629 JSVM_Value result = nullptr;
630 OH_JSVM_GetReferenceValue(env, reference, &result);
631 OH_JSVM_DeleteReference(env, reference);
632 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
633 OH_JSVM_CloseHandleScope(env, handleScope);
634 }
CreateReference_test_2(JSVM_Env env)635 void CreateReference_test_2(JSVM_Env env)
636 {
637 JSVM_HandleScope handleScope;
638 OH_JSVM_OpenHandleScope(env, &handleScope);
639 JSVM_EscapableHandleScope handleScope1;
640 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
641 JSVM_Value value = nullptr;
642 OH_JSVM_GetUndefined(env, &value);
643 JSVM_Ref reference = nullptr;
644 OH_JSVM_CreateReference(env, value, 1, &reference);
645 JSVM_Value result = nullptr;
646 OH_JSVM_GetReferenceValue(env, reference, &result);
647 OH_JSVM_DeleteReference(env, reference);
648 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
649 OH_JSVM_CloseHandleScope(env, handleScope);
650 }
CreateReference_test_3(JSVM_Env env)651 void CreateReference_test_3(JSVM_Env env)
652 {
653 JSVM_HandleScope handleScope;
654 OH_JSVM_OpenHandleScope(env, &handleScope);
655 JSVM_EscapableHandleScope handleScope1;
656 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
657 JSVM_Value value = nullptr;
658 OH_JSVM_GetNull(env, &value);
659 JSVM_Ref reference = nullptr;
660 OH_JSVM_CreateReference(env, value, 1, &reference);
661 JSVM_Value result = nullptr;
662 OH_JSVM_GetReferenceValue(env, reference, &result);
663 OH_JSVM_DeleteReference(env, reference);
664 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
665 OH_JSVM_CloseHandleScope(env, handleScope);
666 }
CreateReference_test_4(JSVM_Env env)667 void CreateReference_test_4(JSVM_Env env)
668 {
669 JSVM_HandleScope handleScope;
670 OH_JSVM_OpenHandleScope(env, &handleScope);
671 JSVM_EscapableHandleScope handleScope1;
672 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
673 JSVM_Value value = nullptr;
674 bool x = true;
675 OH_JSVM_GetBoolean(env, x, &value);
676 JSVM_Ref reference = nullptr;
677 OH_JSVM_CreateReference(env, value, 1, &reference);
678 JSVM_Value result = nullptr;
679 OH_JSVM_GetReferenceValue(env, reference, &result);
680 OH_JSVM_DeleteReference(env, reference);
681 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
682 OH_JSVM_CloseHandleScope(env, handleScope);
683 }
CreateReference_test_6(JSVM_Env env)684 void CreateReference_test_6(JSVM_Env env)
685 {
686 JSVM_HandleScope handleScope;
687 OH_JSVM_OpenHandleScope(env, &handleScope);
688 JSVM_EscapableHandleScope handleScope1;
689 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
690 JSVM_Value value = NULL;
691 OH_JSVM_CreateObject(env, &value);
692 const char *testNameStr = "bar";
693 JSVM_Value propValue = nullptr;
694 JSVM_Value key;
695 OH_JSVM_CreateStringUtf8(env, "foo", JSVM_AUTO_LENGTH, &key);
696 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue);
697 JSVM_Ref reference = nullptr;
698 OH_JSVM_CreateReference(env, value, 1, &reference);
699 JSVM_Value result = nullptr;
700 OH_JSVM_GetReferenceValue(env, reference, &result);
701 OH_JSVM_DeleteReference(env, reference);
702 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
703 OH_JSVM_CloseHandleScope(env, handleScope);
704 }
CreateReference_test_7(JSVM_Env env)705 void CreateReference_test_7(JSVM_Env env)
706 {
707 JSVM_HandleScope handleScope;
708 OH_JSVM_OpenHandleScope(env, &handleScope);
709 JSVM_EscapableHandleScope handleScope1;
710 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
711 JSVM_Value value;
712 OH_JSVM_CreateInt32(env, 0, &value);
713 JSVM_Ref reference = nullptr;
714 OH_JSVM_CreateReference(env, value, 1, &reference);
715 JSVM_Value result = nullptr;
716 OH_JSVM_GetReferenceValue(env, reference, &result);
717 OH_JSVM_DeleteReference(env, reference);
718 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
719 OH_JSVM_CloseHandleScope(env, handleScope);
720 }
CreateReference_test_8(JSVM_Env env)721 void CreateReference_test_8(JSVM_Env env)
722 {
723 JSVM_HandleScope handleScope;
724 OH_JSVM_OpenHandleScope(env, &handleScope);
725 JSVM_EscapableHandleScope handleScope1;
726 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
727 size_t arrayLength = 2;
728 JSVM_Value value;
729 OH_JSVM_CreateArrayWithLength(env, arrayLength, &value);
730 for (uint32_t i = 0; i < arrayLength; i++) {
731 JSVM_Value element;
732 OH_JSVM_CreateUint32(env, i + 1, &element);
733 OH_JSVM_SetElement(env, value, i, element);
734 }
735 JSVM_Ref reference = nullptr;
736 OH_JSVM_CreateReference(env, value, 1, &reference);
737 JSVM_Value result = nullptr;
738 OH_JSVM_GetReferenceValue(env, reference, &result);
739 OH_JSVM_DeleteReference(env, reference);
740 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
741 OH_JSVM_CloseHandleScope(env, handleScope);
742 }
CreateReference_test_9(JSVM_Env env)743 void CreateReference_test_9(JSVM_Env env)
744 {
745 JSVM_HandleScope handleScope;
746 OH_JSVM_OpenHandleScope(env, &handleScope);
747 JSVM_EscapableHandleScope handleScope1;
748 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
749 const char testStr[] = "foo";
750 JSVM_Value oldresult = nullptr;
751 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &oldresult);
752 JSVM_Value value;
753 OH_JSVM_CoerceToObject(env, oldresult, &value);
754 JSVM_Ref reference = nullptr;
755 OH_JSVM_CreateReference(env, value, 1, &reference);
756 JSVM_Value result = nullptr;
757 OH_JSVM_GetReferenceValue(env, reference, &result);
758 OH_JSVM_DeleteReference(env, reference);
759 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
760 OH_JSVM_CloseHandleScope(env, handleScope);
761 }
CreateReference_test_10(JSVM_Env env)762 void CreateReference_test_10(JSVM_Env env)
763 {
764 JSVM_HandleScope handleScope;
765 OH_JSVM_OpenHandleScope(env, &handleScope);
766 JSVM_EscapableHandleScope handleScope1;
767 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
768 JSVM_Value value;
769 OH_JSVM_CreateDouble(env, 0.0/0.0, &value);
770 JSVM_Ref reference = nullptr;
771 OH_JSVM_CreateReference(env, value, 1, &reference);
772 JSVM_Value result = nullptr;
773 OH_JSVM_GetReferenceValue(env, reference, &result);
774 OH_JSVM_DeleteReference(env, reference);
775 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
776 OH_JSVM_CloseHandleScope(env, handleScope);
777 }
testLifeCycle2(napi_env env1,napi_callback_info info)778 static napi_value testLifeCycle2(napi_env env1, napi_callback_info info)
779 {
780 JSVM_InitOptions init_options;
781 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
782 printf("memset_s failed");
783 return nullptr;
784 }
785 init_options.externalReferences = externals;
786 if (aa == 0) {
787 OH_JSVM_Init(&init_options);
788 aa++;
789 }
790 JSVM_VM vm;
791 JSVM_CreateVMOptions options;
792 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
793 printf("memset_s failed");
794 return nullptr;
795 }
796 OH_JSVM_CreateVM(&options, &vm);
797 JSVM_VMScope vm_scope;
798 OH_JSVM_OpenVMScope(vm, &vm_scope);
799 JSVM_Env env;
800 JSVM_CallbackStruct param[1];
801 param[0].data = nullptr;
802 param[0].callback = assertEqual;
803 JSVM_PropertyDescriptor descriptor[] = {
804 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
805 };
806 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
807 JSVM_EnvScope envScope;
808 OH_JSVM_OpenEnvScope(env, &envScope);
809
810 CreateReference_test_1(env);
811 CreateReference_test_2(env);
812 CreateReference_test_3(env);
813 CreateReference_test_4(env);
814 CreateReference_test_6(env);
815 CreateReference_test_7(env);
816 CreateReference_test_8(env);
817 CreateReference_test_9(env);
818 CreateReference_test_10(env);
819
820 OH_JSVM_CloseEnvScope(env, envScope);
821 OH_JSVM_DestroyEnv(env);
822 OH_JSVM_CloseVMScope(vm, vm_scope);
823 OH_JSVM_DestroyVM(vm);
824 napi_value result11;
825 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
826 return result11;
827 }
testCreateData1(napi_env env1,napi_callback_info info)828 static napi_value testCreateData1(napi_env env1, napi_callback_info info){
829 JSVM_InitOptions init_options;
830 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
831 printf("memset_s failed");
832 return nullptr;
833 }
834 init_options.externalReferences = externals;
835 if (aa == 0) {
836 OH_JSVM_Init(&init_options);
837 aa++;
838 }
839 JSVM_VM vm;
840 JSVM_CreateVMOptions options;
841 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
842 printf("memset_s failed");
843 return nullptr;
844 }
845 OH_JSVM_CreateVM(&options, &vm);
846 JSVM_VMScope vm_scope;
847 OH_JSVM_OpenVMScope(vm, &vm_scope);
848 JSVM_Env env;
849 JSVM_CallbackStruct param[1];
850 param[0].data = nullptr;
851 param[0].callback = assertEqual;
852 JSVM_PropertyDescriptor descriptor[] = {
853 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
854 };
855 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
856 JSVM_EnvScope envScope;
857 OH_JSVM_OpenEnvScope(env, &envScope);
858 JSVM_HandleScope handlescope;
859 OH_JSVM_OpenHandleScope(env, &handlescope);
860 JSVM_Value ret;
861 OH_JSVM_CreateArray(env, &ret);
862 bool is_array = false;
863 OH_JSVM_IsArray(env,ret,&is_array);
864 uint32_t array_length = 10;
865 JSVM_Value ret1;
866 OH_JSVM_CreateArrayWithLength(env, array_length, &ret1);
867 uint32_t length;
868 OH_JSVM_GetArrayLength(env, ret1, &length);
869 JSVM_Value arrayBuffer = nullptr;
870 void *arrayBufferPtr = nullptr;
871 size_t arrayBufferSize = 1024;
872 JSVM_Status status = OH_JSVM_CreateArraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
873 JSVM_ASSERT(env, status == JSVM_OK, "success to OH_JSVM_CreateArraybuffer");
874 JSVM_ASSERT(env, arrayBuffer != nullptr, "success create_arryBuffer");
875 JSVM_Value createResult = nullptr;
876 double time = 202110181203150;
877 OH_JSVM_CreateDate(env, time, &createResult);
878 double getTime;
879 OH_JSVM_GetDateValue(env, createResult, &getTime);
880 bool is_date = false;
881 OH_JSVM_IsDate(env,createResult,&is_date);
882 const char testStr[] = "test";
883 JSVM_Value external = nullptr;
884 OH_JSVM_CreateExternal(
885 env, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, (void *)testStr, &external);
886 void *tempExternal = nullptr;
887 OH_JSVM_GetValueExternal(env, external, &tempExternal);
888 OH_JSVM_DetachArraybuffer(env, arrayBuffer);
889 OH_JSVM_CloseHandleScope(env, handlescope);
890 OH_JSVM_CloseEnvScope(env, envScope);
891 OH_JSVM_DestroyEnv(env);
892 // shaoshuai
893 OH_JSVM_CloseVMScope(vm, vm_scope);
894 OH_JSVM_DestroyVM(vm);
895 napi_value result11;
896 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
897 return result11;
898 }
testCreateData2(napi_env env1,napi_callback_info info)899 static napi_value testCreateData2(napi_env env1, napi_callback_info info){
900 JSVM_InitOptions init_options;
901 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
902 printf("memset_s failed");
903 return nullptr;
904 }
905 init_options.externalReferences = externals;
906 if (aa == 0) {
907 OH_JSVM_Init(&init_options);
908 aa++;
909 }
910 JSVM_VM vm;
911 JSVM_CreateVMOptions options;
912 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
913 printf("memset_s failed");
914 return nullptr;
915 }
916 OH_JSVM_CreateVM(&options, &vm);
917 JSVM_VMScope vm_scope;
918 OH_JSVM_OpenVMScope(vm, &vm_scope);
919 JSVM_Env env;
920 JSVM_CallbackStruct param[1];
921 param[0].data = nullptr;
922 param[0].callback = assertEqual;
923 JSVM_PropertyDescriptor descriptor[] = {
924 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
925 };
926 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
927 JSVM_EnvScope envScope;
928 OH_JSVM_OpenEnvScope(env, &envScope);
929 JSVM_HandleScope handlescope;
930 OH_JSVM_OpenHandleScope(env, &handlescope);
931 JSVM_Value description;
932 OH_JSVM_CreateStringUtf8(env,"teststr",7,&description);
933 char buffer[128];
934 size_t bufferSize = 128;
935 size_t copied = 0;
936 OH_JSVM_GetValueStringUtf8(env,description, buffer, bufferSize, &copied);
937 JSVM_Value symbol;
938 OH_JSVM_CreateSymbol(env, description, &symbol);
939 JSVM_Value result_symbol;
940 OH_JSVM_SymbolFor(env, NULL, 0, &result_symbol);
941 JSVM_Value arrayBuffer = nullptr;
942 void *arrayBufferPtr = nullptr;
943 size_t arrayBufferSize = 16;
944 size_t typedArrayLength = 4;
945 OH_JSVM_CreateArraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
946 void *tmpArrayBufferPtr = nullptr;
947 size_t arrayBufferLength = 0;
948 OH_JSVM_GetArraybufferInfo(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLength);
949 JSVM_Value _value;
950 OH_JSVM_CreateTypedarray(env, JSVM_TypedarrayType::JSVM_INT32_ARRAY, typedArrayLength, arrayBuffer, 0, &_value);
951 bool isTypedArray = false;
952 OH_JSVM_IsTypedarray(env, _value, &isTypedArray);
953 JSVM_TypedarrayType type;
954 size_t length = 0;
955 void *data = nullptr;
956 JSVM_Value retArrayBuffer;
957 size_t byteOffset = -1;
958 OH_JSVM_GetTypedarrayInfo(env, _value, &type, &length, &data, &retArrayBuffer, &byteOffset);
959 OH_JSVM_DetachArraybuffer(env,retArrayBuffer);
960 bool isDetachedArrayBuffer = false;
961 OH_JSVM_IsDetachedArraybuffer(env, retArrayBuffer,&isDetachedArrayBuffer);
962 OH_JSVM_CloseHandleScope(env, handlescope);
963 OH_JSVM_CloseEnvScope(env, envScope);
964 OH_JSVM_DestroyEnv(env);
965 OH_JSVM_CloseVMScope(vm, vm_scope);
966 OH_JSVM_DestroyVM(vm);
967 napi_value result11;
968 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
969 return result11;
970 }
testCreateData3(napi_env env1,napi_callback_info info)971 static napi_value testCreateData3(napi_env env1, napi_callback_info info){
972 JSVM_InitOptions init_options;
973 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
974 printf("memset_s failed");
975 return nullptr;
976 }
977 init_options.externalReferences = externals;
978 if (aa == 0) {
979 OH_JSVM_Init(&init_options);
980 aa++;
981 }
982 JSVM_VM vm;
983 JSVM_CreateVMOptions options;
984 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
985 printf("memset_s failed");
986 return nullptr;
987 }
988 OH_JSVM_CreateVM(&options, &vm);
989 JSVM_VMScope vm_scope;
990 OH_JSVM_OpenVMScope(vm, &vm_scope);
991 JSVM_Env env;
992 JSVM_CallbackStruct param[1];
993 param[0].data = nullptr;
994 param[0].callback = assertEqual;
995 JSVM_PropertyDescriptor descriptor[] = {
996 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
997 };
998 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
999 JSVM_EnvScope envScope;
1000 OH_JSVM_OpenEnvScope(env, &envScope);
1001 JSVM_HandleScope handlescope;
1002 OH_JSVM_OpenHandleScope(env, &handlescope);
1003 JSVM_Value arrayBuffer = nullptr;
1004 void *arrayBufferPtr = nullptr;
1005 size_t arrayBufferSize = 16;
1006 JSVM_Status status = OH_JSVM_CreateArraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
1007 JSVM_ASSERT(env, status == JSVM_OK, "success to OH_JSVM_CreateArraybuffer");
1008 JSVM_ASSERT(env, arrayBuffer != nullptr, "success create_arryBuffer");
1009 bool isArrayBuffer = false;
1010 OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer);
1011 JSVM_Value result = nullptr;
1012 OH_JSVM_CreateDataview(env, arrayBufferSize, arrayBuffer, 0, &result);
1013 bool isDataView = false;
1014 OH_JSVM_IsDataview(env, result, &isDataView);
1015 JSVM_Value retArrayBuffer = nullptr;
1016 void *data = nullptr;
1017 size_t byteLength = 0;
1018 size_t byteOffset = -1;
1019 OH_JSVM_GetDataviewInfo(env, result, &byteLength, &data, &retArrayBuffer, &byteOffset);
1020 OH_JSVM_DetachArraybuffer(env, arrayBuffer);
1021 OH_JSVM_CloseHandleScope(env, handlescope);
1022 OH_JSVM_CloseEnvScope(env, envScope);
1023 OH_JSVM_DestroyEnv(env);
1024 OH_JSVM_CloseVMScope(vm, vm_scope);
1025 OH_JSVM_DestroyVM(vm);
1026 napi_value result11;
1027 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1028 return result11;
1029 }
testCreateData4(napi_env env1,napi_callback_info info)1030 static napi_value testCreateData4(napi_env env1, napi_callback_info info){
1031 JSVM_InitOptions init_options;
1032 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1033 printf("memset_s failed");
1034 return nullptr;
1035 }
1036 init_options.externalReferences = externals;
1037 if (aa == 0) {
1038 OH_JSVM_Init(&init_options);
1039 aa++;
1040 }
1041 JSVM_VM vm;
1042 JSVM_CreateVMOptions options;
1043 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1044 printf("memset_s failed");
1045 return nullptr;
1046 }
1047 OH_JSVM_CreateVM(&options, &vm);
1048 JSVM_VMScope vm_scope;
1049 OH_JSVM_OpenVMScope(vm, &vm_scope);
1050 JSVM_Env env;
1051 JSVM_CallbackStruct param[1];
1052 param[0].data = nullptr;
1053 param[0].callback = assertEqual;
1054 JSVM_PropertyDescriptor descriptor[] = {
1055 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1056 };
1057 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1058 JSVM_EnvScope envScope;
1059 OH_JSVM_OpenEnvScope(env, &envScope);
1060 JSVM_HandleScope handlescope;
1061 OH_JSVM_OpenHandleScope(env, &handlescope);
1062 JSVM_Value output1;
1063 OH_JSVM_CreateInt32(env, 0, &output1);
1064 int32_t value1;
1065 OH_JSVM_GetValueInt32(env, output1, &value1);
1066 JSVM_Value output2;
1067 OH_JSVM_CreateUint32(env,0, &output2);
1068 uint32_t value2;
1069 OH_JSVM_GetValueUint32(env,output2, &value2);
1070 JSVM_Value output3;
1071 OH_JSVM_CreateInt64(env, (double)0, &output3);
1072 int64_t value3;
1073 OH_JSVM_GetValueInt64(env,output3, &value3);
1074 int64_t resultValue = 0;
1075 bool flag = false;
1076 OH_JSVM_GetValueBigintInt64(env, output3, &resultValue, &flag);
1077 uint64_t testValue = UINT64_MAX;
1078 JSVM_Value result = nullptr;
1079 OH_JSVM_CreateBigintUint64(env, testValue, &result);
1080
1081 uint64_t resultValue1 = 0;
1082 bool flag1 = false;
1083 OH_JSVM_GetValueBigintUint64(env, result, &resultValue1, &flag1);
1084 double value = 1.0;
1085 JSVM_Value output4;
1086 OH_JSVM_CreateDouble(env, value, &output4);
1087 double resultDouble;
1088 OH_JSVM_GetValueDouble(env, output4, &resultDouble);
1089 OH_JSVM_CloseHandleScope(env, handlescope);
1090 OH_JSVM_CloseEnvScope(env, envScope);
1091 OH_JSVM_DestroyEnv(env);
1092 OH_JSVM_CloseVMScope(vm, vm_scope);
1093 OH_JSVM_DestroyVM(vm);
1094 napi_value result11;
1095 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1096 return result11;
1097 }
testCreateData5(napi_env env1,napi_callback_info info)1098 static napi_value testCreateData5(napi_env env1, napi_callback_info info) {
1099 JSVM_InitOptions init_options;
1100 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1101 printf("memset_s failed");
1102 return nullptr;
1103 }
1104 init_options.externalReferences = externals;
1105 if (aa == 0) {
1106 OH_JSVM_Init(&init_options);
1107 aa++;
1108 }
1109 JSVM_VM vm;
1110 JSVM_CreateVMOptions options;
1111 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1112 printf("memset_s failed");
1113 return nullptr;
1114 }
1115 OH_JSVM_CreateVM(&options, &vm);
1116 JSVM_VMScope vm_scope;
1117 OH_JSVM_OpenVMScope(vm, &vm_scope);
1118 JSVM_Env env;
1119 JSVM_CallbackStruct param[1];
1120 param[0].data = nullptr;
1121 param[0].callback = assertEqual;
1122 JSVM_PropertyDescriptor descriptor[] = {
1123 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1124 };
1125 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1126 JSVM_EnvScope envScope;
1127 OH_JSVM_OpenEnvScope(env, &envScope);
1128 JSVM_HandleScope handlescope;
1129 OH_JSVM_OpenHandleScope(env, &handlescope);
1130 int64_t testValue = INT64_MAX;
1131 JSVM_Value result = nullptr;
1132 OH_JSVM_CreateBigintInt64(env, testValue, &result);
1133 uint64_t testValue1 = UINT64_MAX;
1134 JSVM_Value result1 = nullptr;
1135 OH_JSVM_CreateBigintUint64(env, testValue1, &result1);
1136 int signBit = 0;
1137 size_t wordCount = 4;
1138 uint64_t words[] = {0ULL, 34ULL, 56ULL, 2ULL};
1139 uint64_t wordsOut[] = {0ULL, 0ULL, 0ULL, 0ULL};
1140 JSVM_Value result2 = nullptr;
1141 OH_JSVM_CreateBigintWords(env, signBit, wordCount, words, &result2);
1142 int retSignBit = -1;
1143 size_t retWordCount = 4;
1144 OH_JSVM_GetValueBigintWords(env, result2, &retSignBit, &retWordCount, wordsOut);
1145 JSVM_Value description;
1146 OH_JSVM_CreateStringLatin1(env, "teststr", 7, &description);
1147 char buffer[128];
1148 size_t bufferSize = 128;
1149 size_t copied = 0;
1150 OH_JSVM_GetValueStringLatin1(env,description, buffer, bufferSize, &copied);
1151 const char16_t *str = u"he";
1152 JSVM_Value result4;
1153 OH_JSVM_CreateStringUtf16(env, str, 2, &result4);
1154 char16_t buffer1[128]; // 128: char16_t type of element size
1155 size_t bufferSize1 = 128; // 128: char16_t type of element size
1156 size_t copied1 = 0;
1157 OH_JSVM_GetValueStringUtf16(env,result4, buffer1, bufferSize1, &copied1);
1158 OH_JSVM_CloseHandleScope(env, handlescope);
1159 OH_JSVM_CloseEnvScope(env, envScope);
1160 OH_JSVM_DestroyEnv(env);
1161 OH_JSVM_CloseVMScope(vm, vm_scope);
1162 OH_JSVM_DestroyVM(vm);
1163 napi_value result11;
1164 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1165 return result11;
1166 }
testGetData1(napi_env env1,napi_callback_info info)1167 static napi_value testGetData1(napi_env env1, napi_callback_info info){
1168 JSVM_InitOptions init_options;
1169 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1170 printf("memset_s failed");
1171 return nullptr;
1172 }
1173 init_options.externalReferences = externals;
1174 if (aa == 0) {
1175 OH_JSVM_Init(&init_options);
1176 aa++;
1177 }
1178 JSVM_VM vm;
1179 JSVM_CreateVMOptions options;
1180 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1181 printf("memset_s failed");
1182 return nullptr;
1183 }
1184 OH_JSVM_CreateVM(&options, &vm);
1185 JSVM_VMScope vm_scope;
1186 OH_JSVM_OpenVMScope(vm, &vm_scope);
1187 JSVM_Env env;
1188 JSVM_CallbackStruct param[1];
1189 param[0].data = nullptr;
1190 param[0].callback = assertEqual;
1191 JSVM_PropertyDescriptor descriptor[] = {
1192 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1193 };
1194 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1195 JSVM_EnvScope envScope;
1196 OH_JSVM_OpenEnvScope(env, &envScope);
1197 JSVM_HandleScope handlescope;
1198 OH_JSVM_OpenHandleScope(env, &handlescope);
1199 JSVM_Value testWrapClass = nullptr;
1200 JSVM_CallbackStruct param1;
1201 param1.data = nullptr;
1202 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
1203 JSVM_Value thisVar = nullptr;
1204 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
1205 return thisVar;
1206 };
1207 OH_JSVM_DefineClass(env, "TestWrapClass", NAPI_AUTO_LENGTH, ¶m1, 0, nullptr, &testWrapClass);
1208 JSVM_Value customClassPrototype = nullptr;
1209 OH_JSVM_GetPrototype(env, testWrapClass, &customClassPrototype);
1210 bool value1 = true;
1211 JSVM_Value boolValue;
1212 OH_JSVM_GetBoolean(env,value1,&boolValue);
1213 bool value2;
1214 OH_JSVM_GetValueBool(env,boolValue, &value2);
1215 OH_JSVM_CloseHandleScope(env, handlescope);
1216 OH_JSVM_CloseEnvScope(env, envScope);
1217 OH_JSVM_DestroyEnv(env);
1218 OH_JSVM_CloseVMScope(vm, vm_scope);
1219 OH_JSVM_DestroyVM(vm);
1220 napi_value result11;
1221 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1222 return result11;
1223 }
testGetData2(napi_env env1,napi_callback_info info)1224 static napi_value testGetData2(napi_env env1, napi_callback_info info) {
1225 JSVM_InitOptions init_options;
1226 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1227 printf("memset_s failed");
1228 return nullptr;
1229 }
1230 init_options.externalReferences = externals;
1231 if (aa == 0) {
1232 OH_JSVM_Init(&init_options);
1233 aa++;
1234 }
1235 JSVM_VM vm;
1236 JSVM_CreateVMOptions options;
1237 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1238 printf("memset_s failed");
1239 return nullptr;
1240 }
1241 OH_JSVM_CreateVM(&options, &vm);
1242 JSVM_VMScope vm_scope;
1243 OH_JSVM_OpenVMScope(vm, &vm_scope);
1244 JSVM_Env env;
1245 JSVM_CallbackStruct param[1];
1246 param[0].data = nullptr;
1247 param[0].callback = assertEqual;
1248 JSVM_PropertyDescriptor descriptor[] = {
1249 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1250 };
1251 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1252 JSVM_EnvScope envScope;
1253 OH_JSVM_OpenEnvScope(env, &envScope);
1254 JSVM_HandleScope handlescope;
1255 OH_JSVM_OpenHandleScope(env, &handlescope);
1256 JSVM_Value result = nullptr;
1257 OH_JSVM_GetGlobal(env, &result);
1258 JSVM_Value result1 = nullptr;
1259 OH_JSVM_GetNull(env, &result1);
1260 JSVM_ValueType type;
1261 OH_JSVM_Typeof(env, result1, &type);
1262 JSVM_Value result2 = nullptr;
1263 OH_JSVM_GetUndefined(env, &result2);
1264 OH_JSVM_CloseHandleScope(env, handlescope);
1265 OH_JSVM_CloseEnvScope(env, envScope);
1266 OH_JSVM_DestroyEnv(env);
1267 OH_JSVM_CloseVMScope(vm, vm_scope);
1268 OH_JSVM_DestroyVM(vm);
1269 napi_value result11;
1270 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1271 return result11;
1272 }
testValueOperation(napi_env env1,napi_callback_info info)1273 static napi_value testValueOperation(napi_env env1, napi_callback_info info){
1274 JSVM_InitOptions init_options;
1275 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1276 printf("memset_s failed");
1277 return nullptr;
1278 }
1279 init_options.externalReferences = externals;
1280 if (aa == 0) {
1281 OH_JSVM_Init(&init_options);
1282 aa++;
1283 }
1284 JSVM_VM vm;
1285 JSVM_CreateVMOptions options;
1286 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1287 printf("memset_s failed");
1288 return nullptr;
1289 }
1290 OH_JSVM_CreateVM(&options, &vm);
1291 JSVM_VMScope vm_scope;
1292 OH_JSVM_OpenVMScope(vm, &vm_scope);
1293 JSVM_Env env;
1294 JSVM_CallbackStruct param[1];
1295 param[0].data = nullptr;
1296 param[0].callback = assertEqual;
1297 JSVM_PropertyDescriptor descriptor[] = {
1298 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1299 };
1300 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1301 JSVM_EnvScope envScope;
1302 OH_JSVM_OpenEnvScope(env, &envScope);
1303 JSVM_HandleScope handlescope;
1304 OH_JSVM_OpenHandleScope(env, &handlescope);
1305 JSVM_Value value1;
1306 OH_JSVM_CreateInt32(env,0,&value1);
1307 JSVM_Value value2;
1308 OH_JSVM_CoerceToBool(env,value1,&value2);
1309 JSVM_Value value3;
1310 OH_JSVM_CreateStringUtf8(env, "12", 2, &value3);
1311 JSVM_Value value4;
1312 OH_JSVM_CoerceToNumber(env,value3,&value4);
1313 JSVM_Value ret;
1314 OH_JSVM_CreateArray(env, &ret);
1315 JSVM_Value value5;
1316 OH_JSVM_CoerceToObject(env, ret,&value5);
1317 JSVM_Value value6;
1318 OH_JSVM_CoerceToString(env,value1,&value6);
1319 OH_JSVM_CloseHandleScope(env, handlescope);
1320 OH_JSVM_CloseEnvScope(env, envScope);
1321 OH_JSVM_DestroyEnv(env);
1322 OH_JSVM_CloseVMScope(vm, vm_scope);
1323 OH_JSVM_DestroyVM(vm);
1324 napi_value result11;
1325 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1326 return result11;
1327 }
testValueOperation2(napi_env env1,napi_callback_info info)1328 static napi_value testValueOperation2(napi_env env1, napi_callback_info info) {
1329 JSVM_InitOptions init_options;
1330 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1331 printf("memset_s failed");
1332 return nullptr;
1333 }
1334 init_options.externalReferences = externals;
1335 if (aa == 0) {
1336 OH_JSVM_Init(&init_options);
1337 aa++;
1338 }
1339 JSVM_VM vm;
1340 JSVM_CreateVMOptions options;
1341 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1342 printf("memset_s failed");
1343 return nullptr;
1344 }
1345 OH_JSVM_CreateVM(&options, &vm);
1346 JSVM_VMScope vm_scope;
1347 OH_JSVM_OpenVMScope(vm, &vm_scope);
1348 JSVM_Env env;
1349 JSVM_CallbackStruct param[1];
1350 param[0].data = nullptr;
1351 param[0].callback = assertEqual;
1352 JSVM_PropertyDescriptor descriptor[] = {
1353 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1354 };
1355 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1356 JSVM_EnvScope envScope;
1357 OH_JSVM_OpenEnvScope(env, &envScope);
1358 JSVM_HandleScope handlescope;
1359 OH_JSVM_OpenHandleScope(env, &handlescope);
1360 JSVM_Value customClass = nullptr;
1361 JSVM_CallbackStruct param1;
1362 param1.data = nullptr;
1363 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
1364 JSVM_Value thisVar = nullptr;
1365 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
1366 return thisVar;
1367 };
1368 OH_JSVM_DefineClass(env, "TestWrapClass", NAPI_AUTO_LENGTH, ¶m1, 0, nullptr, &customClass);
1369 JSVM_Value customClassPrototype = nullptr;
1370 OH_JSVM_GetPrototype(env, customClass, &customClassPrototype);
1371 JSVM_Value customInstance = nullptr;
1372 OH_JSVM_NewInstance(env, customClass, 0, nullptr, &customInstance);
1373 bool isInstanceOf = false;
1374 OH_JSVM_Instanceof(env, customInstance, customClass, &isInstanceOf);
1375 const char *testStringStr = "test";
1376 JSVM_Value testString = nullptr;
1377 OH_JSVM_CreateStringUtf8(env, testStringStr, strlen(testStringStr), &testString);
1378 bool isStrictEquals = false;
1379 OH_JSVM_StrictEquals(env, testString, testString, &isStrictEquals);
1380 JSVM_Value testObject = nullptr;
1381 OH_JSVM_CreateObject(env, &testObject);
1382 isStrictEquals = false;
1383 OH_JSVM_StrictEquals(env, testObject, testObject, &isStrictEquals);
1384 OH_JSVM_CloseHandleScope(env, handlescope);
1385 OH_JSVM_CloseEnvScope(env, envScope);
1386 OH_JSVM_DestroyEnv(env);
1387 OH_JSVM_CloseVMScope(vm, vm_scope);
1388 OH_JSVM_DestroyVM(vm);
1389 napi_value result11;
1390 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1391 return result11;
1392 }
equals_test_1(JSVM_Env env)1393 void equals_test_1(JSVM_Env env)
1394 {
1395 JSVM_HandleScope handlescope;
1396 OH_JSVM_OpenHandleScope(env, &handlescope);
1397 JSVM_Value lhs = nullptr;
1398 OH_JSVM_GetUndefined(env, &lhs);
1399 JSVM_Value rhs = nullptr;
1400 OH_JSVM_GetUndefined(env, &rhs);
1401 bool isEquals = false;
1402 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1403 OH_JSVM_CloseHandleScope(env, handlescope);
1404 }
equals_test_2(JSVM_Env env)1405 void equals_test_2(JSVM_Env env)
1406 {
1407 JSVM_HandleScope handlescope;
1408 OH_JSVM_OpenHandleScope(env, &handlescope);
1409 JSVM_Value lhs = nullptr;
1410 OH_JSVM_GetNull(env, &lhs);
1411 JSVM_Value rhs = nullptr;
1412 OH_JSVM_GetNull(env, &rhs);
1413 bool isEquals = false;
1414 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1415 OH_JSVM_CloseHandleScope(env, handlescope);
1416 }
equals_test_3(JSVM_Env env)1417 void equals_test_3(JSVM_Env env)
1418 {
1419 JSVM_HandleScope handlescope;
1420 OH_JSVM_OpenHandleScope(env, &handlescope);
1421 JSVM_Value lhs = nullptr;
1422 bool x = true;
1423 OH_JSVM_GetBoolean(env, x, &lhs);
1424 JSVM_Value rhs = nullptr;
1425 bool y = true;
1426 OH_JSVM_GetBoolean(env, y, &rhs);
1427 bool isEquals = false;
1428 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1429 OH_JSVM_CloseHandleScope(env, handlescope);
1430 }
equals_test_4(JSVM_Env env)1431 void equals_test_4(JSVM_Env env)
1432 {
1433 JSVM_HandleScope handlescope;
1434 OH_JSVM_OpenHandleScope(env, &handlescope);
1435 JSVM_Value lhs = nullptr;
1436 bool x = false;
1437 OH_JSVM_GetBoolean(env, x, &lhs);
1438 JSVM_Value rhs = nullptr;
1439 bool y = false;
1440 OH_JSVM_GetBoolean(env, y, &rhs);
1441 bool isEquals = false;
1442 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1443 OH_JSVM_CloseHandleScope(env, handlescope);
1444 }
equals_test_5(JSVM_Env env)1445 void equals_test_5(JSVM_Env env)
1446 {
1447 JSVM_HandleScope handlescope;
1448 OH_JSVM_OpenHandleScope(env, &handlescope);
1449 const char testStr1[] = "foo";
1450 JSVM_Value lhs = nullptr;
1451 OH_JSVM_CreateStringUtf8(env, testStr1, strlen(testStr1), &lhs);
1452 const char testStr2[] = "foo";
1453 JSVM_Value rhs = nullptr;
1454 OH_JSVM_CreateStringUtf8(env, testStr2, strlen(testStr2), &rhs);
1455 bool isEquals = false;
1456 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1457 OH_JSVM_CloseHandleScope(env, handlescope);
1458 }
equals_test_6(JSVM_Env env)1459 void equals_test_6(JSVM_Env env)
1460 {
1461 JSVM_HandleScope handlescope;
1462 OH_JSVM_OpenHandleScope(env, &handlescope);
1463 JSVM_Value result = NULL;
1464 OH_JSVM_CreateObject(env, &result);
1465 const char *testNameStr = "bar";
1466 JSVM_Value propValue = nullptr;
1467 JSVM_Value key;
1468 OH_JSVM_CreateStringUtf8(env, "foo", JSVM_AUTO_LENGTH, &key);
1469 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue);
1470 OH_JSVM_SetProperty(env, result, key, propValue);
1471 bool isEquals = false;
1472 OH_JSVM_Equals(env, result, result, &isEquals);
1473 OH_JSVM_CloseHandleScope(env, handlescope);
1474 }
equals_test_7(JSVM_Env env)1475 void equals_test_7(JSVM_Env env)
1476 {
1477 JSVM_HandleScope handlescope;
1478 OH_JSVM_OpenHandleScope(env, &handlescope);
1479 JSVM_Value lhs;
1480 OH_JSVM_CreateInt32(env, 0, &lhs);
1481 JSVM_Value rhs;
1482 OH_JSVM_CreateInt32(env, 0, &rhs);
1483 bool isEquals = false;
1484 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1485 OH_JSVM_CloseHandleScope(env, handlescope);
1486 }
equals_test_8(JSVM_Env env)1487 void equals_test_8(JSVM_Env env)
1488 {
1489 JSVM_HandleScope handlescope;
1490 OH_JSVM_OpenHandleScope(env, &handlescope);
1491 JSVM_Value lhs;
1492 OH_JSVM_CreateInt32(env, +0, &lhs);
1493 JSVM_Value rhs;
1494 OH_JSVM_CreateInt32(env, -0, &rhs);
1495 bool isEquals = false;
1496 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1497 OH_JSVM_CloseHandleScope(env, handlescope);
1498 }
equals_test_9(JSVM_Env env)1499 void equals_test_9(JSVM_Env env)
1500 {
1501 JSVM_HandleScope handlescope;
1502 OH_JSVM_OpenHandleScope(env, &handlescope);
1503 JSVM_Value lhs;
1504 OH_JSVM_CreateInt32(env, 0, &lhs);
1505 JSVM_Value rhs = nullptr;
1506 bool y = false;
1507 OH_JSVM_GetBoolean(env, y, &rhs);
1508 bool isEquals = false;
1509 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1510 OH_JSVM_CloseHandleScope(env, handlescope);
1511 }
equals_test_10(JSVM_Env env)1512 void equals_test_10(JSVM_Env env)
1513 {
1514 JSVM_HandleScope handlescope;
1515 OH_JSVM_OpenHandleScope(env, &handlescope);
1516 const char testStr[] = "";
1517 JSVM_Value lhs = nullptr;
1518 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &lhs);
1519 JSVM_Value rhs = nullptr;
1520 bool y = false;
1521 OH_JSVM_GetBoolean(env, y, &rhs);
1522 bool isEquals = false;
1523 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1524 OH_JSVM_CloseHandleScope(env, handlescope);
1525 }
equals_test_11(JSVM_Env env)1526 void equals_test_11(JSVM_Env env)
1527 {
1528 JSVM_HandleScope handlescope;
1529 OH_JSVM_OpenHandleScope(env, &handlescope);
1530 const char testStr[] = "";
1531 JSVM_Value lhs = nullptr;
1532 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &lhs);
1533 JSVM_Value rhs;
1534 OH_JSVM_CreateInt32(env, 0, &rhs);
1535 bool isEquals = false;
1536 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1537 OH_JSVM_CloseHandleScope(env, handlescope);
1538 }
equals_test_12(JSVM_Env env)1539 void equals_test_12(JSVM_Env env)
1540 {
1541 JSVM_HandleScope handlescope;
1542 OH_JSVM_OpenHandleScope(env, &handlescope);
1543 const char testStr[] = "0";
1544 JSVM_Value lhs = nullptr;
1545 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &lhs);
1546 JSVM_Value rhs;
1547 OH_JSVM_CreateInt32(env, 0, &rhs);
1548 bool isEquals = false;
1549 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1550 OH_JSVM_CloseHandleScope(env, handlescope);
1551 }
equals_test_13(JSVM_Env env)1552 void equals_test_13(JSVM_Env env)
1553 {
1554 JSVM_HandleScope handlescope;
1555 OH_JSVM_OpenHandleScope(env, &handlescope);
1556 const char testStr[] = "1";
1557 JSVM_Value lhs = nullptr;
1558 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &lhs);
1559 JSVM_Value rhs;
1560 OH_JSVM_CreateInt32(env, 1, &rhs);
1561 bool isEquals = false;
1562 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1563 OH_JSVM_CloseHandleScope(env, handlescope);
1564 }
equals_test_14(JSVM_Env env)1565 void equals_test_14(JSVM_Env env)
1566 {
1567 JSVM_HandleScope handlescope;
1568 OH_JSVM_OpenHandleScope(env, &handlescope);
1569 size_t arrayLength = 2;
1570 JSVM_Value lhs;
1571 OH_JSVM_CreateArrayWithLength(env, arrayLength, &lhs);
1572 for (uint32_t i = 0; i < arrayLength; i++) {
1573 JSVM_Value element;
1574 OH_JSVM_CreateUint32(env, i + 1, &element);
1575 OH_JSVM_SetElement(env, lhs, i, element);
1576 }
1577 const char testStr[] = "1,2";
1578 JSVM_Value rhs = nullptr;
1579 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &rhs);
1580 bool isEquals = false;
1581 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1582 OH_JSVM_CloseHandleScope(env, handlescope);
1583 }
equals_test_15(JSVM_Env env)1584 void equals_test_15(JSVM_Env env)
1585 {
1586 JSVM_HandleScope handlescope;
1587 OH_JSVM_OpenHandleScope(env, &handlescope);
1588 const char testStr1[] = "foo";
1589 JSVM_Value oldresult = nullptr;
1590 OH_JSVM_CreateStringUtf8(env, testStr1, strlen(testStr1), &oldresult);
1591 JSVM_Value lhs;
1592 OH_JSVM_CoerceToObject(env, oldresult, &lhs);
1593 const char testStr2[] = "foo";
1594 JSVM_Value rhs = nullptr;
1595 OH_JSVM_CreateStringUtf8(env, testStr2, strlen(testStr2), &rhs);
1596 bool isEquals = false;
1597 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1598 OH_JSVM_CloseHandleScope(env, handlescope);
1599 }
equals_test_16(JSVM_Env env)1600 void equals_test_16(JSVM_Env env)
1601 {
1602 JSVM_HandleScope handlescope;
1603 OH_JSVM_OpenHandleScope(env, &handlescope);
1604 JSVM_Value lhs = nullptr;
1605 OH_JSVM_GetNull(env, &lhs);
1606 JSVM_Value rhs = nullptr;
1607 OH_JSVM_GetUndefined(env, &rhs);
1608 bool isEquals = false;
1609 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1610 OH_JSVM_CloseHandleScope(env, handlescope);
1611 }
equals_test_17(JSVM_Env env)1612 void equals_test_17(JSVM_Env env)
1613 {
1614 JSVM_HandleScope handlescope;
1615 OH_JSVM_OpenHandleScope(env, &handlescope);
1616 JSVM_Value lhs = nullptr;
1617 OH_JSVM_GetNull(env, &lhs);
1618 JSVM_Value rhs = nullptr;
1619 bool y = false;
1620 OH_JSVM_GetBoolean(env, y, &rhs);
1621 bool isEquals = false;
1622 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1623 OH_JSVM_CloseHandleScope(env, handlescope);
1624 }
equals_test_18(JSVM_Env env)1625 void equals_test_18(JSVM_Env env)
1626 {
1627 JSVM_HandleScope handlescope;
1628 OH_JSVM_OpenHandleScope(env, &handlescope);
1629 JSVM_Value lhs = nullptr;
1630 OH_JSVM_GetUndefined(env, &lhs);
1631 JSVM_Value rhs = nullptr;
1632 bool y = false;
1633 OH_JSVM_GetBoolean(env, y, &rhs);
1634 bool isEquals = false;
1635 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1636 OH_JSVM_CloseHandleScope(env, handlescope);
1637 }
equals_test_19(JSVM_Env env)1638 void equals_test_19(JSVM_Env env)
1639 {
1640 JSVM_HandleScope handlescope;
1641 OH_JSVM_OpenHandleScope(env, &handlescope);
1642 JSVM_Value lhs = NULL;
1643 OH_JSVM_CreateObject(env, &lhs);
1644 const char *testNameStr = "bar";
1645 JSVM_Value propValue1 = nullptr;
1646 JSVM_Value key1;
1647 OH_JSVM_CreateStringUtf8(env, "foo", JSVM_AUTO_LENGTH, &key1);
1648 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue1);
1649 OH_JSVM_SetProperty(env, lhs, key1, propValue1);
1650 JSVM_Value rhs = NULL;
1651 OH_JSVM_CreateObject(env, &rhs);
1652 JSVM_Value propValue2 = nullptr;
1653 JSVM_Value key2;
1654 OH_JSVM_CreateStringUtf8(env, "foo", JSVM_AUTO_LENGTH, &key2);
1655 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue2);
1656 OH_JSVM_SetProperty(env, rhs, key2, propValue2);
1657 bool isEquals = false;
1658 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1659 OH_JSVM_CloseHandleScope(env, handlescope);
1660 }
equals_test_20(JSVM_Env env)1661 void equals_test_20(JSVM_Env env)
1662 {
1663 JSVM_HandleScope handlescope;
1664 OH_JSVM_OpenHandleScope(env, &handlescope);
1665 const char testStr1[] = "foo";
1666 JSVM_Value oldresult1 = nullptr;
1667 OH_JSVM_CreateStringUtf8(env, testStr1, strlen(testStr1), &oldresult1);
1668 JSVM_Value lhs;
1669 OH_JSVM_CoerceToObject(env, oldresult1, &lhs);
1670 const char testStr2[] = "foo";
1671 JSVM_Value oldresult2 = nullptr;
1672 OH_JSVM_CreateStringUtf8(env, testStr2, strlen(testStr2), &oldresult2);
1673 JSVM_Value rhs;
1674 OH_JSVM_CoerceToObject(env, oldresult2, &rhs);
1675 bool isEquals = false;
1676 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1677 OH_JSVM_CloseHandleScope(env, handlescope);
1678 }
equals_test_21(JSVM_Env env)1679 void equals_test_21(JSVM_Env env)
1680 {
1681 JSVM_HandleScope handlescope;
1682 OH_JSVM_OpenHandleScope(env, &handlescope);
1683 JSVM_Value lhs;
1684 OH_JSVM_CreateInt32(env, 0, &lhs);
1685 JSVM_Value rhs = nullptr;
1686 OH_JSVM_GetNull(env, &rhs);
1687 bool isEquals = false;
1688 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1689 OH_JSVM_CloseHandleScope(env, handlescope);
1690 }
equals_test_22(JSVM_Env env)1691 void equals_test_22(JSVM_Env env)
1692 {
1693 JSVM_HandleScope handlescope;
1694 OH_JSVM_OpenHandleScope(env, &handlescope);
1695 JSVM_Value lhs;
1696 OH_JSVM_CreateInt32(env, 0, &lhs);
1697 JSVM_Value rhs;
1698 OH_JSVM_CreateDouble(env, 0.0/0.0, &rhs);
1699 bool isEquals = false;
1700 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1701 OH_JSVM_CloseHandleScope(env, handlescope);
1702 }
equals_test_23(JSVM_Env env)1703 void equals_test_23(JSVM_Env env)
1704 {
1705 JSVM_HandleScope handlescope;
1706 OH_JSVM_OpenHandleScope(env, &handlescope);
1707 const char testStr1[] = "foo";
1708 JSVM_Value lhs = nullptr;
1709 OH_JSVM_CreateStringUtf8(env, testStr1, strlen(testStr1), &lhs);
1710 JSVM_Value rhs;
1711 OH_JSVM_CreateDouble(env, 0.0/0.0, &rhs);
1712 bool isEquals = false;
1713 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1714 OH_JSVM_CloseHandleScope(env, handlescope);
1715 }
equals_test_24(JSVM_Env env)1716 void equals_test_24(JSVM_Env env)
1717 {
1718 JSVM_HandleScope handlescope;
1719 OH_JSVM_OpenHandleScope(env, &handlescope);
1720 JSVM_Value lhs;
1721 OH_JSVM_CreateDouble(env, 0.0/0.0, &lhs);
1722 JSVM_Value rhs;
1723 OH_JSVM_CreateDouble(env, 0.0/0.0, &rhs);
1724 bool isEquals = false;
1725 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1726 OH_JSVM_CloseHandleScope(env, handlescope);
1727 }
testValueOperation3(napi_env env1,napi_callback_info info)1728 static napi_value testValueOperation3(napi_env env1, napi_callback_info info)
1729 {
1730 JSVM_InitOptions init_options;
1731 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1732 printf("memset_s failed");
1733 return nullptr;
1734 }
1735 init_options.externalReferences = externals;
1736 if (aa == 0) {
1737 OH_JSVM_Init(&init_options);
1738 aa++;
1739 }
1740 JSVM_VM vm;
1741 JSVM_CreateVMOptions options;
1742 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1743 printf("memset_s failed");
1744 return nullptr;
1745 }
1746 OH_JSVM_CreateVM(&options, &vm);
1747 JSVM_VMScope vm_scope;
1748 OH_JSVM_OpenVMScope(vm, &vm_scope);
1749 JSVM_Env env;
1750 JSVM_CallbackStruct param[1];
1751 param[0].data = nullptr;
1752 param[0].callback = assertEqual;
1753 JSVM_PropertyDescriptor descriptor[] = {
1754 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1755 };
1756 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1757 JSVM_EnvScope envScope;
1758 OH_JSVM_OpenEnvScope(env, &envScope);
1759
1760 equals_test_1(env);
1761 equals_test_2(env);
1762 equals_test_3(env);
1763 equals_test_4(env);
1764 equals_test_5(env);
1765 equals_test_6(env);
1766 equals_test_7(env);
1767 equals_test_8(env);
1768 equals_test_9(env);
1769 equals_test_10(env);
1770 equals_test_11(env);
1771 equals_test_12(env);
1772
1773 OH_JSVM_CloseEnvScope(env, envScope);
1774 OH_JSVM_DestroyEnv(env);
1775 OH_JSVM_CloseVMScope(vm, vm_scope);
1776 OH_JSVM_DestroyVM(vm);
1777 napi_value result11;
1778 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1779 return result11;
1780 }
testValueOperation4(napi_env env1,napi_callback_info info)1781 static napi_value testValueOperation4(napi_env env1, napi_callback_info info)
1782 {
1783 JSVM_InitOptions init_options;
1784 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1785 printf("memset_s failed");
1786 return nullptr;
1787 }
1788 init_options.externalReferences = externals;
1789 if (aa == 0) {
1790 OH_JSVM_Init(&init_options);
1791 aa++;
1792 }
1793 JSVM_VM vm;
1794 JSVM_CreateVMOptions options;
1795 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1796 printf("memset_s failed");
1797 return nullptr;
1798 }
1799 OH_JSVM_CreateVM(&options, &vm);
1800 JSVM_VMScope vm_scope;
1801 OH_JSVM_OpenVMScope(vm, &vm_scope);
1802 JSVM_Env env;
1803 JSVM_CallbackStruct param[1];
1804 param[0].data = nullptr;
1805 param[0].callback = assertEqual;
1806 JSVM_PropertyDescriptor descriptor[] = {
1807 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1808 };
1809 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1810 JSVM_EnvScope envScope;
1811 OH_JSVM_OpenEnvScope(env, &envScope);
1812
1813 equals_test_13(env);
1814 equals_test_14(env);
1815 equals_test_15(env);
1816 equals_test_16(env);
1817 equals_test_17(env);
1818 equals_test_18(env);
1819 equals_test_19(env);
1820 equals_test_20(env);
1821 equals_test_21(env);
1822 equals_test_22(env);
1823 equals_test_23(env);
1824 equals_test_24(env);
1825
1826 OH_JSVM_CloseEnvScope(env, envScope);
1827 OH_JSVM_DestroyEnv(env);
1828 OH_JSVM_CloseVMScope(vm, vm_scope);
1829 OH_JSVM_DestroyVM(vm);
1830 napi_value result11;
1831 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1832 return result11;
1833 }
type_test(JSVM_Env env,JSVM_Value value)1834 void type_test(JSVM_Env env, JSVM_Value value)
1835 {
1836 bool isValue = false;
1837 OH_JSVM_IsUndefined(env, value, &isValue);
1838 OH_JSVM_IsNull(env, value, &isValue);
1839 OH_JSVM_IsNullOrUndefined(env, value, &isValue);
1840 OH_JSVM_IsBoolean(env, value, &isValue);
1841 OH_JSVM_IsNumber(env, value, &isValue);
1842 OH_JSVM_IsString(env, value, &isValue);
1843 OH_JSVM_IsSymbol(env, value, &isValue);
1844 OH_JSVM_IsFunction(env, value, &isValue);
1845 OH_JSVM_IsObject(env, value, &isValue);
1846 OH_JSVM_IsBigInt(env, value, &isValue);
1847 OH_JSVM_IsConstructor(env, value, &isValue);
1848 OH_JSVM_IsMap(env, value, &isValue);
1849 }
type_test_Undefined(JSVM_Env env)1850 void type_test_Undefined(JSVM_Env env)
1851 {
1852 JSVM_HandleScope handleScope;
1853 OH_JSVM_OpenHandleScope(env, &handleScope);
1854
1855 JSVM_Value value = nullptr;
1856 OH_JSVM_GetUndefined(env, &value);
1857 type_test(env, value);
1858
1859 OH_JSVM_CloseHandleScope(env, handleScope);
1860 }
type_test_Null(JSVM_Env env)1861 void type_test_Null(JSVM_Env env)
1862 {
1863 JSVM_HandleScope handleScope;
1864 OH_JSVM_OpenHandleScope(env, &handleScope);
1865
1866 JSVM_Value value = nullptr;
1867 OH_JSVM_GetNull(env, &value);
1868 type_test(env, value);
1869
1870 OH_JSVM_CloseHandleScope(env, handleScope);
1871 }
type_test_Boolean(JSVM_Env env)1872 void type_test_Boolean(JSVM_Env env)
1873 {
1874 JSVM_HandleScope handleScope;
1875 OH_JSVM_OpenHandleScope(env, &handleScope);
1876
1877 JSVM_Value value = nullptr;
1878 bool x = true;
1879 OH_JSVM_GetBoolean(env, x, &value);
1880 type_test(env, value);
1881
1882 OH_JSVM_CloseHandleScope(env, handleScope);
1883 }
type_test_Int32(JSVM_Env env)1884 void type_test_Int32(JSVM_Env env)
1885 {
1886 JSVM_HandleScope handleScope;
1887 OH_JSVM_OpenHandleScope(env, &handleScope);
1888
1889 JSVM_Value value;
1890 OH_JSVM_CreateInt32(env, 1, &value);
1891 type_test(env, value);
1892
1893 OH_JSVM_CloseHandleScope(env, handleScope);
1894 }
type_test_String(JSVM_Env env)1895 void type_test_String(JSVM_Env env)
1896 {
1897 JSVM_HandleScope handleScope;
1898 OH_JSVM_OpenHandleScope(env, &handleScope);
1899
1900 const char testStr[] = "foo";
1901 JSVM_Value value = nullptr;
1902 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
1903 type_test(env, value);
1904
1905 OH_JSVM_CloseHandleScope(env, handleScope);
1906 }
type_test_Symbol(JSVM_Env env)1907 void type_test_Symbol(JSVM_Env env)
1908 {
1909 JSVM_HandleScope handleScope;
1910 OH_JSVM_OpenHandleScope(env, &handleScope);
1911
1912 JSVM_Value value = nullptr;
1913 JSVM_Value description;
1914 const char testStr[] = "1234567";
1915 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &description);
1916 OH_JSVM_CreateSymbol(env, description, &value);
1917 type_test(env, value);
1918
1919 OH_JSVM_CloseHandleScope(env, handleScope);
1920 }
type_test_Function(JSVM_Env env)1921 void type_test_Function(JSVM_Env env)
1922 {
1923 JSVM_HandleScope handleScope;
1924 OH_JSVM_OpenHandleScope(env, &handleScope);
1925
1926 JSVM_Value value = nullptr;
1927 JSVM_CallbackStruct param;
1928 OH_JSVM_CreateFunction(env, "func", JSVM_AUTO_LENGTH, ¶m, &value);
1929 type_test(env, value);
1930
1931 OH_JSVM_CloseHandleScope(env, handleScope);
1932 }
type_test_Object(JSVM_Env env)1933 void type_test_Object(JSVM_Env env)
1934 {
1935 JSVM_HandleScope handleScope;
1936 OH_JSVM_OpenHandleScope(env, &handleScope);
1937
1938 JSVM_Value value = nullptr;
1939 OH_JSVM_CreateObject(env, &value);
1940 type_test(env, value);
1941
1942 OH_JSVM_CloseHandleScope(env, handleScope);
1943 }
type_test_Bigint(JSVM_Env env)1944 void type_test_Bigint(JSVM_Env env)
1945 {
1946 JSVM_HandleScope handleScope;
1947 OH_JSVM_OpenHandleScope(env, &handleScope);
1948
1949 JSVM_Value value = nullptr;
1950 int64_t testValue = INT64_MAX;
1951 OH_JSVM_CreateBigintInt64(env, testValue, &value);
1952 type_test(env, value);
1953
1954 OH_JSVM_CloseHandleScope(env, handleScope);
1955 }
type_test_Map(JSVM_Env env)1956 void type_test_Map(JSVM_Env env)
1957 {
1958 JSVM_HandleScope handleScope;
1959 OH_JSVM_OpenHandleScope(env, &handleScope);
1960
1961 JSVM_Value value = nullptr;
1962 OH_JSVM_CreateMap(env, &value);
1963 type_test(env, value);
1964
1965 OH_JSVM_CloseHandleScope(env, handleScope);
1966 }
testValueOperation5(napi_env env1,napi_callback_info info)1967 static napi_value testValueOperation5(napi_env env1, napi_callback_info info)
1968 {
1969 JSVM_InitOptions init_options;
1970 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1971 printf("memset_s failed");
1972 return nullptr;
1973 }
1974 init_options.externalReferences = externals;
1975 if (aa == 0) {
1976 OH_JSVM_Init(&init_options);
1977 aa++;
1978 }
1979 JSVM_VM vm;
1980 JSVM_CreateVMOptions options;
1981 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1982 printf("memset_s failed");
1983 return nullptr;
1984 }
1985 OH_JSVM_CreateVM(&options, &vm);
1986 JSVM_VMScope vm_scope;
1987 OH_JSVM_OpenVMScope(vm, &vm_scope);
1988 JSVM_Env env;
1989 JSVM_CallbackStruct param[1];
1990 param[0].data = nullptr;
1991 param[0].callback = assertEqual;
1992 JSVM_PropertyDescriptor descriptor[] = {
1993 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1994 };
1995 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1996 JSVM_EnvScope envScope;
1997 OH_JSVM_OpenEnvScope(env, &envScope);
1998
1999 type_test_Undefined(env);
2000 type_test_Null(env);
2001 type_test_Boolean(env);
2002 type_test_Int32(env);
2003 type_test_String(env);
2004 type_test_Symbol(env);
2005 type_test_Function(env);
2006 type_test_Object(env);
2007 type_test_Bigint(env);
2008 type_test_Map(env);
2009
2010 OH_JSVM_CloseEnvScope(env, envScope);
2011 OH_JSVM_DestroyEnv(env);
2012 OH_JSVM_CloseVMScope(vm, vm_scope);
2013 OH_JSVM_DestroyVM(vm);
2014 napi_value result11;
2015 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2016 return result11;
2017 }
type_test_RegExp_UNICODE(JSVM_Env env)2018 void type_test_RegExp_UNICODE(JSVM_Env env)
2019 {
2020 JSVM_HandleScope handleScope;
2021 OH_JSVM_OpenHandleScope(env, &handleScope);
2022
2023 JSVM_Value value = nullptr;
2024 const char testStr[] = "ab+c";
2025 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2026 JSVM_Value result = nullptr;
2027 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2028 type_test(env, result);
2029
2030 OH_JSVM_CloseHandleScope(env, handleScope);
2031 }
type_test_RegExp_GLOBAL(JSVM_Env env)2032 void type_test_RegExp_GLOBAL(JSVM_Env env)
2033 {
2034 JSVM_HandleScope handleScope;
2035 OH_JSVM_OpenHandleScope(env, &handleScope);
2036
2037 JSVM_Value value = nullptr;
2038 const char testStr[] = "ab+c";
2039 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2040 JSVM_Value result = nullptr;
2041 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_GLOBAL, &result);
2042 type_test(env, result);
2043
2044 OH_JSVM_CloseHandleScope(env, handleScope);
2045 }
type_test_RegExp_IGNORE_CASE(JSVM_Env env)2046 void type_test_RegExp_IGNORE_CASE(JSVM_Env env)
2047 {
2048 JSVM_HandleScope handleScope;
2049 OH_JSVM_OpenHandleScope(env, &handleScope);
2050
2051 JSVM_Value value = nullptr;
2052 const char testStr[] = "ab+c";
2053 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2054 JSVM_Value result = nullptr;
2055 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_IGNORE_CASE, &result);
2056 type_test(env, result);
2057
2058 OH_JSVM_CloseHandleScope(env, handleScope);
2059 }
type_test_RegExp_MULTILINE(JSVM_Env env)2060 void type_test_RegExp_MULTILINE(JSVM_Env env)
2061 {
2062 JSVM_HandleScope handleScope;
2063 OH_JSVM_OpenHandleScope(env, &handleScope);
2064
2065 JSVM_Value value = nullptr;
2066 const char testStr[] = "ab+c";
2067 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2068 JSVM_Value result = nullptr;
2069 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_MULTILINE, &result);
2070 type_test(env, result);
2071
2072 OH_JSVM_CloseHandleScope(env, handleScope);
2073 }
type_test_RegExp_STICKY(JSVM_Env env)2074 void type_test_RegExp_STICKY(JSVM_Env env)
2075 {
2076 JSVM_HandleScope handleScope;
2077 OH_JSVM_OpenHandleScope(env, &handleScope);
2078
2079 JSVM_Value value = nullptr;
2080 const char testStr[] = "ab+c";
2081 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2082 JSVM_Value result = nullptr;
2083 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_STICKY, &result);
2084 type_test(env, result);
2085
2086 OH_JSVM_CloseHandleScope(env, handleScope);
2087 }
type_test_RegExp_DOT_ALL(JSVM_Env env)2088 void type_test_RegExp_DOT_ALL(JSVM_Env env)
2089 {
2090 JSVM_HandleScope handleScope;
2091 OH_JSVM_OpenHandleScope(env, &handleScope);
2092
2093 JSVM_Value value = nullptr;
2094 const char testStr[] = "ab+c";
2095 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2096 JSVM_Value result = nullptr;
2097 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_DOT_ALL, &result);
2098 type_test(env, result);
2099
2100 OH_JSVM_CloseHandleScope(env, handleScope);
2101 }
type_test_RegExp_LINEAR(JSVM_Env env)2102 void type_test_RegExp_LINEAR(JSVM_Env env)
2103 {
2104 JSVM_HandleScope handleScope;
2105 OH_JSVM_OpenHandleScope(env, &handleScope);
2106
2107 JSVM_Value value = nullptr;
2108 const char testStr[] = "ab+c";
2109 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2110 JSVM_Value result = nullptr;
2111 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_LINEAR, &result);
2112 type_test(env, result);
2113
2114 OH_JSVM_CloseHandleScope(env, handleScope);
2115 }
type_test_RegExp_HAS_INDICES(JSVM_Env env)2116 void type_test_RegExp_HAS_INDICES(JSVM_Env env)
2117 {
2118 JSVM_HandleScope handleScope;
2119 OH_JSVM_OpenHandleScope(env, &handleScope);
2120
2121 JSVM_Value value = nullptr;
2122 const char testStr[] = "ab+c";
2123 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2124 JSVM_Value result = nullptr;
2125 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_HAS_INDICES, &result);
2126 type_test(env, result);
2127
2128 OH_JSVM_CloseHandleScope(env, handleScope);
2129 }
type_test_RegExp_UNICODE_SETS(JSVM_Env env)2130 void type_test_RegExp_UNICODE_SETS(JSVM_Env env)
2131 {
2132 JSVM_HandleScope handleScope;
2133 OH_JSVM_OpenHandleScope(env, &handleScope);
2134
2135 JSVM_Value value = nullptr;
2136 const char testStr[] = "ab+c";
2137 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2138 JSVM_Value result = nullptr;
2139 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE_SETS, &result);
2140 type_test(env, result);
2141
2142 OH_JSVM_CloseHandleScope(env, handleScope);
2143 }
testValueOperation6(napi_env env1,napi_callback_info info)2144 static napi_value testValueOperation6(napi_env env1, napi_callback_info info)
2145 {
2146 JSVM_InitOptions init_options;
2147 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2148 printf("memset_s failed");
2149 return nullptr;
2150 }
2151 init_options.externalReferences = externals;
2152 if (aa == 0) {
2153 OH_JSVM_Init(&init_options);
2154 aa++;
2155 }
2156 JSVM_VM vm;
2157 JSVM_CreateVMOptions options;
2158 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2159 printf("memset_s failed");
2160 return nullptr;
2161 }
2162 OH_JSVM_CreateVM(&options, &vm);
2163 JSVM_VMScope vm_scope;
2164 OH_JSVM_OpenVMScope(vm, &vm_scope);
2165 JSVM_Env env;
2166 JSVM_CallbackStruct param[1];
2167 param[0].data = nullptr;
2168 param[0].callback = assertEqual;
2169 JSVM_PropertyDescriptor descriptor[] = {
2170 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2171 };
2172 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2173 JSVM_EnvScope envScope;
2174 OH_JSVM_OpenEnvScope(env, &envScope);
2175
2176 type_test_RegExp_UNICODE(env);
2177 type_test_RegExp_GLOBAL(env);
2178 type_test_RegExp_IGNORE_CASE(env);
2179 type_test_RegExp_MULTILINE(env);
2180 type_test_RegExp_STICKY(env);
2181 type_test_RegExp_DOT_ALL(env);
2182 type_test_RegExp_LINEAR(env);
2183 type_test_RegExp_HAS_INDICES(env);
2184 type_test_RegExp_UNICODE_SETS(env);
2185
2186 OH_JSVM_CloseEnvScope(env, envScope);
2187 OH_JSVM_DestroyEnv(env);
2188 OH_JSVM_CloseVMScope(vm, vm_scope);
2189 OH_JSVM_DestroyVM(vm);
2190 napi_value result11;
2191 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2192 return result11;
2193 }
type_test_RegExp_Undefined(JSVM_Env env)2194 void type_test_RegExp_Undefined(JSVM_Env env)
2195 {
2196 JSVM_HandleScope handleScope;
2197 OH_JSVM_OpenHandleScope(env, &handleScope);
2198
2199 JSVM_Value value = nullptr;
2200 OH_JSVM_GetUndefined(env, &value);
2201 JSVM_Value result = nullptr;
2202 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2203 type_test(env, result);
2204
2205 OH_JSVM_CloseHandleScope(env, handleScope);
2206 }
type_test_RegExp_Null(JSVM_Env env)2207 void type_test_RegExp_Null(JSVM_Env env)
2208 {
2209 JSVM_HandleScope handleScope;
2210 OH_JSVM_OpenHandleScope(env, &handleScope);
2211
2212 JSVM_Value value = nullptr;
2213 OH_JSVM_GetNull(env, &value);
2214 JSVM_Value result = nullptr;
2215 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2216 type_test(env, result);
2217
2218 OH_JSVM_CloseHandleScope(env, handleScope);
2219 }
type_test_RegExp_Boolean(JSVM_Env env)2220 void type_test_RegExp_Boolean(JSVM_Env env)
2221 {
2222 JSVM_HandleScope handleScope;
2223 OH_JSVM_OpenHandleScope(env, &handleScope);
2224
2225 JSVM_Value value = nullptr;
2226 bool x = true;
2227 OH_JSVM_GetBoolean(env, x, &value);
2228 JSVM_Value result = nullptr;
2229 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2230 type_test(env, result);
2231
2232 OH_JSVM_CloseHandleScope(env, handleScope);
2233 }
type_test_RegExp_Int32(JSVM_Env env)2234 void type_test_RegExp_Int32(JSVM_Env env)
2235 {
2236 JSVM_HandleScope handleScope;
2237 OH_JSVM_OpenHandleScope(env, &handleScope);
2238
2239 JSVM_Value value;
2240 OH_JSVM_CreateInt32(env, 1, &value);
2241 JSVM_Value result = nullptr;
2242 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2243 type_test(env, result);
2244
2245 OH_JSVM_CloseHandleScope(env, handleScope);
2246 }
type_test_RegExp_Symbol(JSVM_Env env)2247 void type_test_RegExp_Symbol(JSVM_Env env)
2248 {
2249 JSVM_HandleScope handleScope;
2250 OH_JSVM_OpenHandleScope(env, &handleScope);
2251
2252 JSVM_Value value = nullptr;
2253 JSVM_Value description;
2254 const char testStr[] = "1234567";
2255 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &description);
2256 OH_JSVM_CreateSymbol(env, description, &value);
2257 JSVM_Value result = nullptr;
2258 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2259 type_test(env, result);
2260
2261 OH_JSVM_CloseHandleScope(env, handleScope);
2262 }
type_test_RegExp_Function(JSVM_Env env)2263 void type_test_RegExp_Function(JSVM_Env env)
2264 {
2265 JSVM_HandleScope handleScope;
2266 OH_JSVM_OpenHandleScope(env, &handleScope);
2267
2268 JSVM_Value value = nullptr;
2269 JSVM_CallbackStruct param;
2270 OH_JSVM_CreateFunction(env, "func", JSVM_AUTO_LENGTH, ¶m, &value);
2271 JSVM_Value result = nullptr;
2272 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2273 type_test(env, result);
2274
2275 OH_JSVM_CloseHandleScope(env, handleScope);
2276 }
type_test_RegExp_Object(JSVM_Env env)2277 void type_test_RegExp_Object(JSVM_Env env)
2278 {
2279 JSVM_HandleScope handleScope;
2280 OH_JSVM_OpenHandleScope(env, &handleScope);
2281
2282 JSVM_Value value = nullptr;
2283 OH_JSVM_CreateObject(env, &value);
2284 JSVM_Value result = nullptr;
2285 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2286 type_test(env, result);
2287
2288 OH_JSVM_CloseHandleScope(env, handleScope);
2289 }
type_test_RegExp_BigintInt64(JSVM_Env env)2290 void type_test_RegExp_BigintInt64(JSVM_Env env)
2291 {
2292 JSVM_HandleScope handleScope;
2293 OH_JSVM_OpenHandleScope(env, &handleScope);
2294
2295 JSVM_Value value = nullptr;
2296 int64_t testValue = INT64_MAX;
2297 OH_JSVM_CreateBigintInt64(env, testValue, &value);
2298 JSVM_Value result = nullptr;
2299 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2300 type_test(env, result);
2301
2302 OH_JSVM_CloseHandleScope(env, handleScope);
2303 }
type_test_RegExp_Map(JSVM_Env env)2304 void type_test_RegExp_Map(JSVM_Env env)
2305 {
2306 JSVM_HandleScope handleScope;
2307 OH_JSVM_OpenHandleScope(env, &handleScope);
2308
2309 JSVM_Value value = nullptr;
2310 OH_JSVM_CreateMap(env, &value);
2311 JSVM_Value result = nullptr;
2312 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2313 type_test(env, result);
2314
2315 OH_JSVM_CloseHandleScope(env, handleScope);
2316 }
testValueOperation7(napi_env env1,napi_callback_info info)2317 static napi_value testValueOperation7(napi_env env1, napi_callback_info info)
2318 {
2319 JSVM_InitOptions init_options;
2320 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2321 printf("memset_s failed");
2322 return nullptr;
2323 }
2324 init_options.externalReferences = externals;
2325 if (aa == 0) {
2326 OH_JSVM_Init(&init_options);
2327 aa++;
2328 }
2329 JSVM_VM vm;
2330 JSVM_CreateVMOptions options;
2331 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2332 printf("memset_s failed");
2333 return nullptr;
2334 }
2335 OH_JSVM_CreateVM(&options, &vm);
2336 JSVM_VMScope vm_scope;
2337 OH_JSVM_OpenVMScope(vm, &vm_scope);
2338 JSVM_Env env;
2339 JSVM_CallbackStruct param[1];
2340 param[0].data = nullptr;
2341 param[0].callback = assertEqual;
2342 JSVM_PropertyDescriptor descriptor[] = {
2343 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2344 };
2345 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2346 JSVM_EnvScope envScope;
2347 OH_JSVM_OpenEnvScope(env, &envScope);
2348
2349 type_test_RegExp_Undefined(env);
2350 type_test_RegExp_Null(env);
2351 type_test_RegExp_Boolean(env);
2352 type_test_RegExp_Int32(env);
2353 type_test_RegExp_Symbol(env);
2354 type_test_RegExp_Function(env);
2355 type_test_RegExp_Object(env);
2356 type_test_RegExp_BigintInt64(env);
2357 type_test_RegExp_Map(env);
2358
2359 OH_JSVM_CloseEnvScope(env, envScope);
2360 OH_JSVM_DestroyEnv(env);
2361 OH_JSVM_CloseVMScope(vm, vm_scope);
2362 OH_JSVM_DestroyVM(vm);
2363 napi_value result11;
2364 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2365 return result11;
2366 }
2367
prototypeof_test_1(JSVM_Env env,napi_env env1)2368 void prototypeof_test_1(JSVM_Env env, napi_env env1)
2369 {
2370 JSVM_HandleScope handleScope;
2371 OH_JSVM_OpenHandleScope(env, &handleScope);
2372
2373 JSVM_Value obj;
2374 OH_JSVM_CreateObject(env, &obj);
2375 JSVM_Value result;
2376 OH_JSVM_ObjectGetPrototypeOf(env, obj, &result);
2377 NAPI_ASSERT_RETURN_VOID(env1, result != nullptr, "check OH_JSVM_ObjectGetPrototype 1");
2378
2379 OH_JSVM_CloseHandleScope(env, handleScope);
2380 }
2381
prototypeof_test_2(JSVM_Env env,napi_env env1)2382 void prototypeof_test_2(JSVM_Env env, napi_env env1)
2383 {
2384 JSVM_HandleScope handleScope;
2385 OH_JSVM_OpenHandleScope(env, &handleScope);
2386
2387 JSVM_Value obj;
2388 OH_JSVM_CreateObject(env, &obj);
2389 JSVM_Value mySet;
2390 OH_JSVM_CreateSet(env, &mySet);
2391 JSVM_Status status = OH_JSVM_ObjectSetPrototypeOf(env, obj, mySet);
2392 NAPI_ASSERT_RETURN_VOID(env1, status == JSVM_OK, "OH_JSVM_ObjectSetPrototypeOf check status");
2393 JSVM_Value proto;
2394 OH_JSVM_ObjectGetPrototypeOf(env, obj, &proto);
2395 bool result = false;
2396 OH_JSVM_StrictEquals(env, proto, mySet, &result);
2397 NAPI_ASSERT_RETURN_VOID(env1, result, "check OH_JSVM_ObjectGetPrototype 2");
2398 OH_JSVM_CloseHandleScope(env, handleScope);
2399 }
2400
prototypeof_test_3(JSVM_Env env,napi_env env1)2401 void prototypeof_test_3(JSVM_Env env, napi_env env1)
2402 {
2403 JSVM_HandleScope handleScope;
2404 OH_JSVM_OpenHandleScope(env, &handleScope);
2405
2406 JSVM_Value obj;
2407 OH_JSVM_GetUndefined(env, &obj);
2408 JSVM_Value proto;
2409 JSVM_Status status = OH_JSVM_ObjectGetPrototypeOf(env, obj, &proto);
2410 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "check OH_JSVM_ObjectGetPrototype 3");
2411 OH_JSVM_CloseHandleScope(env, handleScope);
2412 }
2413
prototypeof_test_4(JSVM_Env env,napi_env env1)2414 void prototypeof_test_4(JSVM_Env env, napi_env env1)
2415 {
2416 JSVM_HandleScope handleScope;
2417 OH_JSVM_OpenHandleScope(env, &handleScope);
2418
2419 JSVM_Value obj;
2420 OH_JSVM_GetUndefined(env, &obj);
2421 JSVM_Value proto = nullptr;
2422 JSVM_Status status = OH_JSVM_ObjectSetPrototypeOf(env, obj, proto);
2423 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype obj not an object");
2424 status = OH_JSVM_ObjectSetPrototypeOf(env, nullptr, proto);
2425 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype obj is null");
2426 OH_JSVM_CloseHandleScope(env, handleScope);
2427 }
2428
prototypeof_test_5(JSVM_Env env,napi_env env1)2429 void prototypeof_test_5(JSVM_Env env, napi_env env1)
2430 {
2431 JSVM_HandleScope handleScope;
2432 OH_JSVM_OpenHandleScope(env, &handleScope);
2433
2434 JSVM_Value obj;
2435 OH_JSVM_CreateObject(env, &obj);
2436 JSVM_Status status = OH_JSVM_ObjectSetPrototypeOf(env, obj, nullptr);
2437 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype prototype is null");
2438 JSVM_Value proto;
2439 OH_JSVM_GetUndefined(env, &proto);
2440 status = OH_JSVM_ObjectSetPrototypeOf(env, obj, proto);
2441 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype prototype is not an object 1");
2442 OH_JSVM_CloseHandleScope(env, handleScope);
2443 }
2444
prototypeof_test_6(JSVM_Env env,napi_env env1)2445 void prototypeof_test_6(JSVM_Env env, napi_env env1)
2446 {
2447 JSVM_HandleScope handleScope;
2448 OH_JSVM_OpenHandleScope(env, &handleScope);
2449
2450 JSVM_Value obj;
2451 OH_JSVM_CreateObject(env, &obj);
2452 JSVM_Status status = OH_JSVM_ObjectSetPrototypeOf(nullptr, obj, nullptr);
2453 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype prototype is null");
2454 JSVM_Value proto;
2455 OH_JSVM_GetUndefined(env, &proto);
2456 status = OH_JSVM_ObjectSetPrototypeOf(nullptr, obj, proto);
2457 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype prototype is not an object 2");
2458 OH_JSVM_CloseHandleScope(env, handleScope);
2459 }
2460
set_test_1(JSVM_Env env,napi_env env1)2461 void set_test_1(JSVM_Env env, napi_env env1)
2462 {
2463 JSVM_HandleScope handleScope;
2464 OH_JSVM_OpenHandleScope(env, &handleScope);
2465 JSVM_Status status = OH_JSVM_CreateSet(env, nullptr);
2466 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "OH_JSVM_CreateSet check status 1");
2467
2468 OH_JSVM_CloseHandleScope(env, handleScope);
2469 }
2470
set_test_2(JSVM_Env env,napi_env env1)2471 void set_test_2(JSVM_Env env, napi_env env1)
2472 {
2473 JSVM_HandleScope handleScope;
2474 OH_JSVM_OpenHandleScope(env, &handleScope);
2475
2476 JSVM_Value mySet;
2477 JSVM_Status status = OH_JSVM_CreateSet(env, &mySet);
2478 NAPI_ASSERT_RETURN_VOID(env1, status == JSVM_OK, "OH_JSVM_CreateSet check status 2");
2479 bool isSet = false;
2480 OH_JSVM_IsSet(env, mySet, &isSet);
2481 NAPI_ASSERT_RETURN_VOID(env1, mySet != nullptr, "OH_JSVM_CreateSet check status 3");
2482 OH_JSVM_CloseHandleScope(env, handleScope);
2483 }
2484
set_test_3(JSVM_Env env,napi_env env1)2485 void set_test_3(JSVM_Env env, napi_env env1)
2486 {
2487 JSVM_HandleScope handleScope;
2488 OH_JSVM_OpenHandleScope(env, &handleScope);
2489
2490 JSVM_Value mySet = nullptr;
2491 bool isSet = false;
2492 OH_JSVM_IsSet(env, mySet, &isSet);
2493 NAPI_ASSERT_RETURN_VOID(env1, !isSet, "check OH_JSVM_IsSet when not a set");
2494
2495 OH_JSVM_CloseHandleScope(env, handleScope);
2496 }
2497
set_test_4(JSVM_Env env,napi_env env1)2498 void set_test_4(JSVM_Env env, napi_env env1)
2499 {
2500 JSVM_HandleScope handleScope;
2501 OH_JSVM_OpenHandleScope(env, &handleScope);
2502 JSVM_Status status = OH_JSVM_CreateSet(nullptr, nullptr);
2503 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "OH_JSVM_CreateSet check status 4");
2504 bool isSet = false;
2505 JSVM_Status status2 = OH_JSVM_IsSet(nullptr, nullptr, &isSet);
2506 NAPI_ASSERT_RETURN_VOID(env1, status2 != JSVM_OK, "OH_JSVM_IsSet check status");
2507 OH_JSVM_CloseHandleScope(env, handleScope);
2508 }
2509
testValueOperation8(napi_env env1,napi_callback_info info)2510 static napi_value testValueOperation8(napi_env env1, napi_callback_info info)
2511 {
2512 JSVM_InitOptions init_options;
2513 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2514 printf("memset_s failed");
2515 return nullptr;
2516 }
2517 init_options.externalReferences = externals;
2518 if (aa == 0) {
2519 OH_JSVM_Init(&init_options);
2520 aa++;
2521 }
2522 JSVM_VM vm;
2523 JSVM_CreateVMOptions options;
2524 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2525 printf("memset_s failed");
2526 return nullptr;
2527 }
2528 OH_JSVM_CreateVM(&options, &vm);
2529 JSVM_VMScope vm_scope;
2530 OH_JSVM_OpenVMScope(vm, &vm_scope);
2531 JSVM_Env env;
2532 JSVM_CallbackStruct param[1];
2533 param[0].data = nullptr;
2534 param[0].callback = assertEqual;
2535 JSVM_PropertyDescriptor descriptor[] = {
2536 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2537 };
2538 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2539 JSVM_EnvScope envScope;
2540 OH_JSVM_OpenEnvScope(env, &envScope);
2541
2542 prototypeof_test_1(env, env1);
2543 prototypeof_test_2(env, env1);
2544 prototypeof_test_3(env, env1);
2545 prototypeof_test_4(env, env1);
2546 prototypeof_test_5(env, env1);
2547 prototypeof_test_6(env, env1);
2548
2549 set_test_1(env, env1);
2550 set_test_2(env, env1);
2551 set_test_3(env, env1);
2552 set_test_4(env, env1);
2553
2554 OH_JSVM_CloseEnvScope(env, envScope);
2555 OH_JSVM_DestroyEnv(env);
2556 OH_JSVM_CloseVMScope(vm, vm_scope);
2557 OH_JSVM_DestroyVM(vm);
2558 napi_value result11;
2559 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2560 return result11;
2561 }
2562
testGetPropertyNames(napi_env env1,napi_callback_info info)2563 static napi_value testGetPropertyNames(napi_env env1, napi_callback_info info) {
2564 JSVM_InitOptions init_options;
2565 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2566 printf("memset_s failed");
2567 return nullptr;
2568 }
2569 init_options.externalReferences = externals;
2570 if (aa == 0) {
2571 OH_JSVM_Init(&init_options);
2572 aa++;
2573 }
2574 JSVM_VM vm;
2575 JSVM_CreateVMOptions options;
2576 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2577 printf("memset_s failed");
2578 return nullptr;
2579 }
2580 OH_JSVM_CreateVM(&options, &vm);
2581 JSVM_VMScope vm_scope;
2582 OH_JSVM_OpenVMScope(vm, &vm_scope);
2583 JSVM_Env env;
2584 JSVM_CallbackStruct param[1];
2585 param[0].data = nullptr;
2586 param[0].callback = assertEqual;
2587 JSVM_PropertyDescriptor descriptor[] = {
2588 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2589 };
2590 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2591 JSVM_EnvScope envScope;
2592 OH_JSVM_OpenEnvScope(env, &envScope);
2593 JSVM_HandleScope handlescope;
2594 OH_JSVM_OpenHandleScope(env, &handlescope);
2595 JSVM_Value result = nullptr;
2596 OH_JSVM_CreateObject(env, &result);
2597 const char testStr[] = "1234567";
2598 JSVM_Value strAttribute = nullptr;
2599 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &strAttribute);
2600 OH_JSVM_SetNamedProperty(env, result, "strAttribute", strAttribute);
2601 bool hasNamedProperty = false;
2602 OH_JSVM_HasNamedProperty(env, result, "strAttribute", &hasNamedProperty);
2603 JSVM_Value retStrAttribute = nullptr;
2604 OH_JSVM_GetNamedProperty(env, result, "strAttribute", &retStrAttribute);
2605 OH_JSVM_CloseHandleScope(env, handlescope);
2606 OH_JSVM_CloseEnvScope(env, envScope);
2607 OH_JSVM_DestroyEnv(env);
2608 OH_JSVM_CloseVMScope(vm, vm_scope);
2609 OH_JSVM_DestroyVM(vm);
2610 napi_value result11;
2611 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2612 return result11;
2613 }
testGetAllPropertyNames(napi_env env1,napi_callback_info info)2614 static napi_value testGetAllPropertyNames(napi_env env1, napi_callback_info info){
2615 JSVM_InitOptions init_options;
2616 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2617 printf("memset_s failed");
2618 return nullptr;
2619 }
2620 init_options.externalReferences = externals;
2621 if (aa == 0) {
2622 OH_JSVM_Init(&init_options);
2623 aa++;
2624 }
2625 JSVM_VM vm;
2626 JSVM_CreateVMOptions options;
2627 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2628 printf("memset_s failed");
2629 return nullptr;
2630 }
2631 OH_JSVM_CreateVM(&options, &vm);
2632 JSVM_VMScope vm_scope;
2633 OH_JSVM_OpenVMScope(vm, &vm_scope);
2634 JSVM_Env env;
2635 JSVM_CallbackStruct param[1];
2636 param[0].data = nullptr;
2637 param[0].callback = assertEqual;
2638 JSVM_PropertyDescriptor descriptor[] = {
2639 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2640 };
2641 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2642 JSVM_EnvScope envScope;
2643 OH_JSVM_OpenEnvScope(env, &envScope);
2644 JSVM_HandleScope handlescope;
2645 OH_JSVM_OpenHandleScope(env, &handlescope);
2646 JSVM_Value returnValue, props;
2647 JSVM_CALL(env, OH_JSVM_CreateObject(env, &returnValue));
2648 addReturnedStatus(env, "envIsNull", returnValue, "Invalid argument", JSVM_Status::JSVM_INVALID_ARG,
2649 OH_JSVM_GetAllPropertyNames(nullptr, returnValue, JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
2650 JSVM_KeyFilter::JSVM_KEY_WRITABLE,
2651 JSVM_KeyConversion::JSVM_KEY_KEEP_NUMBERS, &props));
2652 OH_JSVM_GetAllPropertyNames(env, nullptr, JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
2653 JSVM_KeyFilter::JSVM_KEY_WRITABLE, JSVM_KeyConversion::JSVM_KEY_KEEP_NUMBERS, &props);
2654 addLastStatus(env, "objectIsNull", returnValue);
2655 OH_JSVM_GetAllPropertyNames(env, returnValue, JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
2656 JSVM_KeyFilter::JSVM_KEY_WRITABLE, JSVM_KeyConversion::JSVM_KEY_KEEP_NUMBERS, nullptr);
2657 addLastStatus(env, "valueIsNull", returnValue);
2658 OH_JSVM_CloseHandleScope(env, handlescope);
2659 OH_JSVM_CloseEnvScope(env, envScope);
2660 OH_JSVM_DestroyEnv(env);
2661 OH_JSVM_CloseVMScope(vm, vm_scope);
2662 OH_JSVM_DestroyVM(vm);
2663 napi_value result11;
2664 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2665 return result11;
2666 }
testProperty(napi_env env1,napi_callback_info info)2667 static napi_value testProperty(napi_env env1, napi_callback_info info) {
2668 JSVM_InitOptions init_options;
2669 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2670 printf("memset_s failed");
2671 return nullptr;
2672 }
2673 init_options.externalReferences = externals;
2674 if (aa == 0) {
2675 OH_JSVM_Init(&init_options);
2676 aa++;
2677 }
2678 JSVM_VM vm;
2679 JSVM_CreateVMOptions options;
2680 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2681 printf("memset_s failed");
2682 return nullptr;
2683 }
2684 OH_JSVM_CreateVM(&options, &vm);
2685 JSVM_VMScope vm_scope;
2686 OH_JSVM_OpenVMScope(vm, &vm_scope);
2687 JSVM_Env env;
2688 JSVM_CallbackStruct param[1];
2689 param[0].data = nullptr;
2690 param[0].callback = assertEqual;
2691 JSVM_PropertyDescriptor descriptor[] = {
2692 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2693 };
2694 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2695 JSVM_EnvScope envScope;
2696 OH_JSVM_OpenEnvScope(env, &envScope);
2697 JSVM_HandleScope handlescope;
2698 OH_JSVM_OpenHandleScope(env, &handlescope);
2699 JSVM_Value result = nullptr;
2700 OH_JSVM_CreateObject(env, &result);
2701 const char testStr[] = "1234567";
2702 JSVM_Value strAttribute = nullptr;
2703 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &strAttribute);
2704 OH_JSVM_SetNamedProperty(env, result, "strAttribute", strAttribute);
2705
2706 JSVM_Value retStrAttribute = nullptr;
2707 OH_JSVM_GetNamedProperty(env, result, "strAttribute", &retStrAttribute);
2708
2709 int32_t testNumber = 12345;
2710 JSVM_Value numberAttribute = nullptr;
2711 OH_JSVM_CreateInt32(env, testNumber, &numberAttribute);
2712 OH_JSVM_SetNamedProperty(env, result, "numberAttribute", numberAttribute);
2713 JSVM_Value propNames = nullptr;
2714 OH_JSVM_GetPropertyNames(env, result, &propNames);
2715
2716 bool isArray = false;
2717 OH_JSVM_IsArray(env, propNames, &isArray);
2718 uint32_t arrayLength = 0;
2719 OH_JSVM_GetArrayLength(env, propNames, &arrayLength);
2720
2721 for (uint32_t i = 0; i < arrayLength; i++) {
2722 bool hasElement = false;
2723 OH_JSVM_HasElement(env, propNames, i, &hasElement);
2724 JSVM_Value propName = nullptr;
2725 OH_JSVM_GetElement(env, propNames, i, &propName);
2726 bool hasProp = false;
2727 OH_JSVM_HasProperty(env, result, propName, &hasProp);
2728 JSVM_Value propValue = nullptr;
2729 OH_JSVM_GetProperty(env, result, propName, &propValue);
2730 }
2731 bool deletion = false;
2732 OH_JSVM_DeleteElement(env, propNames, 1, &deletion);
2733 bool hasElement = true;
2734 OH_JSVM_HasElement(env, propNames, 1, &hasElement);
2735 OH_JSVM_CloseHandleScope(env, handlescope);
2736 OH_JSVM_CloseEnvScope(env, envScope);
2737 OH_JSVM_DestroyEnv(env);
2738 OH_JSVM_CloseVMScope(vm, vm_scope);
2739 OH_JSVM_DestroyVM(vm);
2740 napi_value result11;
2741 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2742 return result11;
2743 }
testDefineProperties(napi_env env1,napi_callback_info info)2744 static napi_value testDefineProperties(napi_env env1, napi_callback_info info) {
2745 JSVM_InitOptions init_options;
2746 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2747 printf("memset_s failed");
2748 return nullptr;
2749 }
2750 init_options.externalReferences = externals;
2751 if (aa == 0) {
2752 OH_JSVM_Init(&init_options);
2753 aa++;
2754 }
2755 JSVM_VM vm;
2756 JSVM_CreateVMOptions options;
2757 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2758 printf("memset_s failed");
2759 return nullptr;
2760 }
2761 OH_JSVM_CreateVM(&options, &vm);
2762 JSVM_VMScope vm_scope;
2763 OH_JSVM_OpenVMScope(vm, &vm_scope);
2764 JSVM_Env env;
2765 JSVM_CallbackStruct param[1];
2766 param[0].data = nullptr;
2767 param[0].callback = assertEqual;
2768 JSVM_PropertyDescriptor descriptor[] = {
2769 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2770 };
2771 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2772 JSVM_EnvScope envScope;
2773 OH_JSVM_OpenEnvScope(env, &envScope);
2774 JSVM_HandleScope handlescope;
2775 OH_JSVM_OpenHandleScope(env, &handlescope);
2776 JSVM_Value object, return_value;
2777 JSVM_CallbackStruct param1;
2778 param1.data = nullptr;
2779 param1.callback = defineProperties;
2780 JSVM_PropertyDescriptor desc = {"prop", NULL, ¶m1, NULL, NULL, NULL, JSVM_PropertyAttributes::JSVM_ENUMERABLE};
2781
2782 JSVM_CALL(env, OH_JSVM_CreateObject(env, &object));
2783 JSVM_CALL(env, OH_JSVM_CreateObject(env, &return_value));
2784
2785 addReturnedStatus(env, "envIsNull", return_value, "Invalid argument", JSVM_Status::JSVM_INVALID_ARG,
2786 OH_JSVM_DefineProperties(NULL, object, 1, &desc));
2787 OH_JSVM_DefineProperties(env, NULL, 1, &desc);
2788 OH_JSVM_ObjectFreeze(env, object);
2789 OH_JSVM_ObjectSeal(env, object);
2790 OH_JSVM_CloseHandleScope(env, handlescope);
2791 OH_JSVM_CloseEnvScope(env, envScope);
2792 OH_JSVM_DestroyEnv(env);
2793 OH_JSVM_CloseVMScope(vm, vm_scope);
2794 OH_JSVM_DestroyVM(vm);
2795 napi_value result11;
2796 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2797 return result11;
2798 }
testWrap(napi_env env1,napi_callback_info info)2799 static napi_value testWrap(napi_env env1, napi_callback_info info){
2800 JSVM_InitOptions init_options;
2801 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2802 printf("memset_s failed");
2803 return nullptr;
2804 }
2805 init_options.externalReferences = externals;
2806 if (aa == 0) {
2807 OH_JSVM_Init(&init_options);
2808 aa++;
2809 }
2810 JSVM_VM vm;
2811 JSVM_CreateVMOptions options;
2812 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2813 printf("memset_s failed");
2814 return nullptr;
2815 }
2816 OH_JSVM_CreateVM(&options, &vm);
2817 JSVM_VMScope vm_scope;
2818 OH_JSVM_OpenVMScope(vm, &vm_scope);
2819 JSVM_Env env;
2820 JSVM_CallbackStruct param[1];
2821 param[0].data = nullptr;
2822 param[0].callback = assertEqual;
2823 JSVM_PropertyDescriptor descriptor[] = {
2824 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2825 };
2826 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2827 JSVM_EnvScope envScope;
2828 OH_JSVM_OpenEnvScope(env, &envScope);
2829 JSVM_HandleScope handlescope;
2830 OH_JSVM_OpenHandleScope(env, &handlescope);
2831 JSVM_Value testClass = nullptr;
2832 JSVM_CallbackStruct param1;
2833 param1.data = nullptr;
2834 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
2835 JSVM_Value thisVar = nullptr;
2836 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
2837
2838 return thisVar;
2839 };
2840 OH_JSVM_DefineClass(env, "TestClass", NAPI_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
2841
2842 JSVM_Value instanceValue = nullptr;
2843 OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
2844
2845 const char *testStr = "test";
2846 OH_JSVM_Wrap(
2847 env, instanceValue, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, nullptr, nullptr);
2848 const char *tmpTestStr = nullptr;
2849 OH_JSVM_Unwrap(env, instanceValue, (void **)&tmpTestStr);
2850 const char *tmpTestStr1 = nullptr;
2851 OH_JSVM_RemoveWrap(env, instanceValue, (void **)&tmpTestStr1);
2852 OH_JSVM_Unwrap(env, instanceValue, (void **)&tmpTestStr1);
2853 OH_JSVM_CloseHandleScope(env, handlescope);
2854 OH_JSVM_CloseEnvScope(env, envScope);
2855 OH_JSVM_DestroyEnv(env);
2856 OH_JSVM_CloseVMScope(vm, vm_scope);
2857 OH_JSVM_DestroyVM(vm);
2858 napi_value result11;
2859 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2860 return result11;
2861 }
2862 static JSVM_Deferred deferred = nullptr;
testOthers(napi_env env1,napi_callback_info info)2863 static napi_value testOthers(napi_env env1, napi_callback_info info){
2864 JSVM_InitOptions init_options;
2865 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2866 printf("memset_s failed");
2867 return nullptr;
2868 }
2869 init_options.externalReferences = externals;
2870 if (aa == 0) {
2871 OH_JSVM_Init(&init_options);
2872 aa++;
2873 }
2874 JSVM_VM vm;
2875 JSVM_CreateVMOptions options;
2876 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2877 printf("memset_s failed");
2878 return nullptr;
2879 }
2880 OH_JSVM_CreateVM(&options, &vm);
2881 JSVM_VMScope vm_scope;
2882 OH_JSVM_OpenVMScope(vm, &vm_scope);
2883 JSVM_Env env;
2884 JSVM_CallbackStruct param[1];
2885 param[0].data = nullptr;
2886 param[0].callback = assertEqual;
2887 JSVM_PropertyDescriptor descriptor[] = {
2888 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2889 };
2890 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2891 JSVM_EnvScope envScope;
2892 OH_JSVM_OpenEnvScope(env, &envScope);
2893 JSVM_HandleScope handlescope;
2894 OH_JSVM_OpenHandleScope(env, &handlescope);
2895 uint32_t result;
2896 OH_JSVM_GetVersion(env, &result);
2897 JSVM_VMInfo result1;
2898 OH_JSVM_GetVMInfo(&result1);
2899 int64_t adjustedValue;
2900 OH_JSVM_AdjustExternalMemory(env, 1, &adjustedValue);
2901 JSVM_Value promise;
2902 OH_JSVM_CreatePromise(env, &deferred, &promise);
2903 bool is_promise;
2904 OH_JSVM_IsPromise(env, promise, &is_promise);
2905 const char *blob_data = nullptr;
2906 size_t blob_size = 0;
2907 JSVM_Env envs[1] = {env};
2908 OH_JSVM_CreateSnapshot(vm, 1, envs, &blob_data, &blob_size);
2909 OH_JSVM_CloseHandleScope(env, handlescope);
2910 OH_JSVM_CloseEnvScope(env, envScope);
2911 OH_JSVM_DestroyEnv(env);
2912 OH_JSVM_CloseVMScope(vm, vm_scope);
2913 OH_JSVM_DestroyVM(vm);
2914 napi_value result11;
2915 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2916 return result11;
2917 }
2918
2919
Add(napi_env env1,napi_callback_info info)2920 static napi_value Add(napi_env env1, napi_callback_info info) {
2921 JSVM_InitOptions init_options;
2922 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2923 printf("memset_s failed");
2924 return nullptr;
2925 }
2926 init_options.externalReferences = externals;
2927 if (aa == 0) {
2928 OH_JSVM_Init(&init_options);
2929 aa++;
2930 }
2931 JSVM_VM vm;
2932 JSVM_CreateVMOptions options;
2933 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2934 printf("memset_s failed");
2935 return nullptr;
2936 }
2937 OH_JSVM_CreateVM(&options, &vm);
2938 JSVM_VMScope vm_scope;
2939 OH_JSVM_OpenVMScope(vm, &vm_scope);
2940 JSVM_Env env;
2941
2942 JSVM_CallbackStruct param[5];
2943 for (int i = 0; i < 5; i++) {
2944 param[i].data = nullptr;
2945 }
2946 param[0].callback = assertEqual;
2947 param[1].callback = assertTrue;
2948 param[2].callback = assertFalse;
2949 param[3].callback = consoleinfo;
2950 param[4].callback = isArray;
2951
2952 JSVM_PropertyDescriptor descriptor[] = {
2953 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2954 {"assertTrue", NULL, ¶m[1], NULL, NULL, NULL, JSVM_DEFAULT},
2955 {"assertFalse", NULL, ¶m[2], NULL, NULL, NULL, JSVM_DEFAULT},
2956 {"consoleinfo", NULL, ¶m[3], NULL, NULL, NULL, JSVM_DEFAULT},
2957 {"isArray", NULL, ¶m[4], NULL, NULL, NULL, JSVM_DEFAULT},
2958 };
2959
2960 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2961 JSVM_EnvScope envScope;
2962 OH_JSVM_OpenEnvScope(env, &envScope);
2963 JSVM_HandleScope handlescope;
2964 OH_JSVM_OpenHandleScope(env, &handlescope);
2965 const char* sourcecodestr = "\
2966 {\
2967 var length = 5; var arr = new Array(length); arr[4] = 123;\
2968 }\
2969 ";
2970 JSVM_Value sourcecodevalue;
2971 OH_JSVM_CreateStringUtf8(env, sourcecodestr, strlen(sourcecodestr), &sourcecodevalue);
2972 JSVM_Script script;
2973 OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script);
2974 JSVM_Value result;
2975 OH_JSVM_RunScript(env, script, &result);
2976 int32_t value2;
2977 JSVM_CALL(env, OH_JSVM_GetValueInt32(env, result, &value2));
2978 OH_JSVM_CloseHandleScope(env, handlescope);
2979 OH_JSVM_CloseEnvScope(env, envScope);
2980 OH_JSVM_DestroyEnv(env);
2981 // shaoshuai
2982 OH_JSVM_CloseVMScope(vm, vm_scope);
2983 OH_JSVM_DestroyVM(vm);
2984 return nullptr;
2985 }
test(JSVM_Env env,JSVM_CallbackInfo info)2986 static JSVM_Value test(JSVM_Env env, JSVM_CallbackInfo info) {
2987 JSVM_VM vm1 = nullptr;
2988 JSVM_Env env2;
2989 JSVM_Script script = nullptr;
2990 OH_JSVM_CreateEnvFromSnapshot(vm1, 0, &env2);
2991 const uint8_t **data_ptr = nullptr;
2992 size_t *length_ptr =nullptr;
2993 OH_JSVM_CreateCodeCache(env, script, data_ptr, length_ptr);
2994 OH_JSVM_SetInstanceData(
2995 env2, 0, [](JSVM_Env env, void *data, void *hint) { ++*static_cast<int *>(data); }, nullptr);
2996 JSVM_Ref *refValues;
2997 OH_JSVM_GetInstanceData(env, (void **)&refValues);
2998 JSVM_Value object = nullptr;
2999 OH_JSVM_SetProperty(env, object, 0, NULL);
3000 JSVM_Value key = nullptr;
3001 bool result;
3002 OH_JSVM_DeleteProperty(env, object, key, &result);
3003 OH_JSVM_HasOwnProperty(env, object, key, &result);
3004 OH_JSVM_SetElement(NULL, object, 0, object);
3005 JSVM_Value ret;
3006 JSVM_Value funcValue = nullptr;
3007 JSVM_CallbackStruct param;
3008 OH_JSVM_CreateFunction(env, "func", NAPI_AUTO_LENGTH, ¶m, &funcValue);
3009 OH_JSVM_CallFunction(env, nullptr, funcValue, 0, nullptr, &ret);
3010 JSVM_Value constructor = nullptr;
3011 JSVM_CallbackInfo info1 = nullptr;
3012 OH_JSVM_GetNewTarget(env, info1, &constructor);
3013 JSVM_Value value = nullptr;
3014 OH_JSVM_TypeTagObject(env, value,nullptr);
3015 OH_JSVM_CheckObjectTypeTag(env, object, nullptr, &result);
3016 JSVM_Finalize finalizeCb = nullptr;
3017 JSVM_Ref result1;
3018 OH_JSVM_AddFinalizer(env, object, NULL, finalizeCb, NULL, &result1);
3019 OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_NONE);
3020 JSVM_Deferred deferred = nullptr;
3021 OH_JSVM_ResolveDeferred(env, deferred,value);
3022 OH_JSVM_RejectDeferred(env, deferred, value);
3023 JSVM_Value value1;
3024 OH_JSVM_JsonParse(env, value, &value1);
3025 OH_JSVM_JsonStringify(env, object, &value1);
3026 return nullptr;
3027 }
Add1(napi_env env1,napi_callback_info info)3028 static napi_value Add1(napi_env env1, napi_callback_info info) {
3029 JSVM_InitOptions init_options;
3030 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3031 printf("memset_s failed");
3032 return nullptr;
3033 }
3034 init_options.externalReferences = externals;
3035 if (aa == 0) {
3036 OH_JSVM_Init(&init_options);
3037 aa++;
3038 }
3039 JSVM_VM vm;
3040 JSVM_CreateVMOptions options;
3041 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3042 printf("memset_s failed");
3043 return nullptr;
3044 }
3045 OH_JSVM_CreateVM(&options, &vm);
3046 JSVM_VMScope vm_scope;
3047 OH_JSVM_OpenVMScope(vm, &vm_scope);
3048 JSVM_Env env;
3049 JSVM_CallbackStruct param[1];
3050 param[0].data = nullptr;
3051 param[0].callback = test;
3052 JSVM_PropertyDescriptor descriptor[] = {
3053 {"test", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3054 };
3055 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3056 JSVM_EnvScope envScope;
3057 OH_JSVM_OpenEnvScope(env, &envScope);
3058 JSVM_HandleScope handlescope;
3059 OH_JSVM_OpenHandleScope(env, &handlescope);
3060 OH_JSVM_CloseHandleScope(env, handlescope);
3061 OH_JSVM_CloseEnvScope(env, envScope);
3062 OH_JSVM_DestroyEnv(env);
3063 OH_JSVM_CloseVMScope(vm, vm_scope);
3064 OH_JSVM_DestroyVM(vm);
3065 napi_value result11;
3066 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3067 return result11;
3068 }
output_stream(const char * data,int size,void * stream_data)3069 static bool output_stream(const char *data, int size, void *stream_data) {
3070 return true;
3071 }
theSecondOperations(JSVM_Env env,JSVM_CallbackInfo info)3072 static JSVM_Value theSecondOperations(JSVM_Env env, JSVM_CallbackInfo info) {
3073 JSVM_VM vm;
3074 OH_JSVM_CreateVM(nullptr, &vm);
3075 void *data = nullptr;
3076 JSVM_HeapStatistics stats;
3077 OH_JSVM_GetHeapStatistics(vm, &stats);
3078 OH_JSVM_TakeHeapSnapshot(vm,output_stream,data);
3079 JSVM_CpuProfiler cpu_profiler;
3080 OH_JSVM_StartCpuProfiler(vm, &cpu_profiler);
3081 OH_JSVM_StopCpuProfiler( vm,cpu_profiler,output_stream,data);
3082 return nullptr;
3083 }
testSecondOperations(napi_env env1,napi_callback_info info)3084 static napi_value testSecondOperations(napi_env env1, napi_callback_info info) {
3085 JSVM_InitOptions init_options;
3086 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3087 printf("memset_s failed");
3088 return nullptr;
3089 }
3090 init_options.externalReferences = externals;
3091 if (aa == 0) {
3092 OH_JSVM_Init(&init_options);
3093 aa++;
3094 }
3095 JSVM_VM vm;
3096 JSVM_CreateVMOptions options;
3097 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3098 printf("memset_s failed");
3099 return nullptr;
3100 }
3101 OH_JSVM_CreateVM(&options, &vm);
3102 JSVM_VMScope vm_scope;
3103 OH_JSVM_OpenVMScope(vm, &vm_scope);
3104 JSVM_Env env;
3105 JSVM_CallbackStruct param[1];
3106 param[0].data = nullptr;
3107 param[0].callback = theSecondOperations;
3108 JSVM_PropertyDescriptor descriptor[] = {
3109 {"theSecondOperations", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3110 };
3111 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3112 uint16_t num = 9229;
3113 OH_JSVM_OpenInspector(env, "localhost", num);
3114 JSVM_EnvScope envScope;
3115 OH_JSVM_OpenEnvScope(env, &envScope);
3116 JSVM_HandleScope handlescope;
3117 OH_JSVM_OpenHandleScope(env, &handlescope);
3118 OH_JSVM_CloseHandleScope(env, handlescope);
3119 OH_JSVM_CloseInspector(env);
3120 OH_JSVM_CloseEnvScope(env, envScope);
3121 OH_JSVM_DestroyEnv(env);
3122 OH_JSVM_CloseVMScope(vm, vm_scope);
3123 OH_JSVM_DestroyVM(vm);
3124 napi_value result11;
3125 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3126 return result11;
3127 }
3128
3129 // ========= arguments check =========
checkArgs(JSVM_Env env,JSVM_CallbackInfo info)3130 static JSVM_Value checkArgs(JSVM_Env env, JSVM_CallbackInfo info)
3131 {
3132 JSVM_CallbackStruct param;
3133 auto func = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3134 JSVM_Value thisVar = nullptr;
3135 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3136 return thisVar;
3137 };
3138 param.callback = func;
3139 param.data = nullptr;
3140 JSVM_Value testWrapClass = nullptr;
3141
3142 // case 1. If env is null ptr, return JSVM_INVALID_ARG.
3143 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(nullptr, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3144 &propertyCfg, ¶m, &testWrapClass);
3145 if (res == JSVM_INVALID_ARG) {
3146 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr env");
3147 }
3148
3149 // case 1. If utf8name is nullptr, return JSVM_INVALID_ARG.
3150 res = OH_JSVM_DefineClassWithPropertyHandler(env, nullptr, NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3151 &propertyCfg, ¶m, &testWrapClass);
3152 if (res == JSVM_INVALID_ARG) {
3153 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr utf8name");
3154 }
3155
3156 // case 3. if constructor is nullptr, return JSVM_INVALID_ARG.
3157 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, nullptr, 0, nullptr, &propertyCfg,
3158 ¶m, &testWrapClass);
3159 if (res == JSVM_INVALID_ARG) {
3160 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr constructor");
3161 }
3162
3163 // case 4. If the callback of the constructor is nullptr, return JSVM_INVALID_ARG.
3164 param.callback = nullptr;
3165 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3166 &propertyCfg, ¶m, &testWrapClass);
3167 if (res == JSVM_INVALID_ARG) {
3168 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr constructor callback");
3169 }
3170 param.callback = func;
3171
3172 // case 5. If propertyCount is greater than 0, properties are nullptr
3173 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 1, nullptr, &propertyCfg,
3174 ¶m, &testWrapClass);
3175 if (res == JSVM_INVALID_ARG) {
3176 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr properties");
3177 }
3178
3179 // case 6. If propertyHandlerCfg is nullptr, return JSVM_INVALID_ARG.
3180 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, nullptr,
3181 ¶m, &testWrapClass);
3182 if (res == JSVM_INVALID_ARG) {
3183 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr propertyHandlerCfg");
3184 }
3185
3186 // case 7. If result is nullptr, return JSVM_INVALID_ARG.
3187 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg,
3188 ¶m, nullptr);
3189 if (res == JSVM_INVALID_ARG) {
3190 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr result");
3191 }
3192
3193 // case 8. Valid input parameter, return JSVM_OK.
3194 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg,
3195 nullptr, &testWrapClass);
3196 if (res == JSVM_OK) {
3197 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler successfully");
3198 }
3199 return nullptr;
3200 }
3201
testCheckArgs(napi_env env1,napi_callback_info info)3202 static napi_value testCheckArgs(napi_env env1, napi_callback_info info)
3203 {
3204 JSVM_InitOptions init_options;
3205 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3206 printf("memset_s failed");
3207 return nullptr;
3208 }
3209 init_options.externalReferences = externals;
3210 if (aa == 0) {
3211 OH_JSVM_Init(&init_options);
3212 aa++;
3213 }
3214 JSVM_VM vm;
3215 JSVM_CreateVMOptions options;
3216 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3217 printf("memset_s failed");
3218 return nullptr;
3219 }
3220 OH_JSVM_CreateVM(&options, &vm);
3221 JSVM_VMScope vm_scope;
3222 OH_JSVM_OpenVMScope(vm, &vm_scope);
3223 JSVM_Env env;
3224 JSVM_CallbackStruct param[1];
3225 param[0].callback = nullptr;
3226 param[0].callback = checkArgs;
3227
3228 JSVM_PropertyDescriptor descriptor[] = {
3229 {"checkArgs", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3230 };
3231 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3232 JSVM_EnvScope envScope;
3233 OH_JSVM_OpenEnvScope(env, &envScope);
3234 JSVM_HandleScope handlescope;
3235 OH_JSVM_OpenHandleScope(env, &handlescope);
3236 OH_JSVM_CloseHandleScope(env, handlescope);
3237 OH_JSVM_CloseEnvScope(env, envScope);
3238 OH_JSVM_DestroyEnv(env);
3239 OH_JSVM_CloseVMScope(vm, vm_scope);
3240 OH_JSVM_DestroyVM(vm);
3241 napi_value result11;
3242 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3243 return result11;
3244 }
3245
3246 // ============ empty propertyHandlerCfg ============================
3247 // case 9. setProperty
setProperty(JSVM_Env env,JSVM_CallbackInfo info)3248 static JSVM_Value setProperty(JSVM_Env env, JSVM_CallbackInfo info)
3249 {
3250 JSVM_CallbackStruct param;
3251 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3252 JSVM_Value thisVar = nullptr;
3253 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3254 return thisVar;
3255 };
3256 param.data = nullptr;
3257 JSVM_Value testWrapClass = nullptr;
3258
3259 OH_JSVM_DefineClassWithPropertyHandler(env, "Test2", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg, nullptr,
3260 &testWrapClass);
3261 JSVM_Value instanceValue = nullptr;
3262 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3263
3264 const char testStr[] = "hello world";
3265 JSVM_Value setvalueName = nullptr;
3266 JSVM_Value result = nullptr;
3267 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3268 JSVM_Status res = OH_JSVM_SetNamedProperty(env, instanceValue, "mySettedProperty", setvalueName);
3269 if (res == JSVM_OK) {
3270 char resultStr[] = "set property successfully";
3271 OH_JSVM_CreateStringUtf8(env, resultStr, strlen(resultStr), &result);
3272 }
3273 return result;
3274 }
3275
testSetProperty(napi_env env1,napi_callback_info info)3276 static napi_value testSetProperty(napi_env env1, napi_callback_info info)
3277 {
3278 JSVM_InitOptions init_options;
3279 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3280 printf("memset_s failed");
3281 return nullptr;
3282 }
3283 init_options.externalReferences = externals;
3284 if (aa == 0) {
3285 OH_JSVM_Init(&init_options);
3286 aa++;
3287 }
3288 JSVM_VM vm;
3289 JSVM_CreateVMOptions options;
3290 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3291 printf("memset_s failed");
3292 return nullptr;
3293 }
3294 OH_JSVM_CreateVM(&options, &vm);
3295 JSVM_VMScope vm_scope;
3296 OH_JSVM_OpenVMScope(vm, &vm_scope);
3297 JSVM_Env env;
3298
3299 JSVM_CallbackStruct param[1];
3300 param[0].callback = nullptr;
3301 param[0].callback = setProperty;
3302
3303 JSVM_PropertyDescriptor descriptor[] = {
3304 {"setProperty", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3305 };
3306 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3307 JSVM_EnvScope envScope;
3308 OH_JSVM_OpenEnvScope(env, &envScope);
3309 JSVM_HandleScope handlescope;
3310 OH_JSVM_OpenHandleScope(env, &handlescope);
3311 OH_JSVM_CloseHandleScope(env, handlescope);
3312 OH_JSVM_CloseEnvScope(env, envScope);
3313 OH_JSVM_DestroyEnv(env);
3314 OH_JSVM_CloseVMScope(vm, vm_scope);
3315 OH_JSVM_DestroyVM(vm);
3316 napi_value result11;
3317 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3318 return result11;
3319 }
3320
3321 // case 10. getProperty
getProperty(JSVM_Env env,JSVM_CallbackInfo info)3322 static JSVM_Value getProperty(JSVM_Env env, JSVM_CallbackInfo info)
3323 {
3324 JSVM_CallbackStruct param;
3325 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3326 JSVM_Value thisVar = nullptr;
3327 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3328 return thisVar;
3329 };
3330 param.data = nullptr;
3331 JSVM_Value testWrapClass = nullptr;
3332 OH_JSVM_DefineClassWithPropertyHandler(env, "Test3", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg, nullptr,
3333 &testWrapClass);
3334 JSVM_Value instanceValue = nullptr;
3335 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3336
3337 const char testStr[] = "hello world";
3338 JSVM_Value setvalueName = nullptr;
3339 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3340 OH_JSVM_SetNamedProperty(env, instanceValue, "mySettedProperty", setvalueName);
3341
3342 JSVM_Value valueName = nullptr;
3343 JSVM_Status res = OH_JSVM_GetNamedProperty(env, instanceValue, "mySettedProperty", &valueName);
3344 if (res == JSVM_OK) {
3345 printf("JSVM getProperty successfully");
3346 }
3347 return valueName;
3348 }
3349
testGetProperty(napi_env env1,napi_callback_info info)3350 static napi_value testGetProperty(napi_env env1, napi_callback_info info)
3351 {
3352 JSVM_InitOptions init_options;
3353 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3354 printf("memset_s failed");
3355 return nullptr;
3356 }
3357 init_options.externalReferences = externals;
3358 if (aa == 0) {
3359 OH_JSVM_Init(&init_options);
3360 aa++;
3361 }
3362 JSVM_VM vm;
3363 JSVM_CreateVMOptions options;
3364 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3365 printf("memset_s failed");
3366 return nullptr;
3367 }
3368 OH_JSVM_CreateVM(&options, &vm);
3369 JSVM_VMScope vm_scope;
3370 OH_JSVM_OpenVMScope(vm, &vm_scope);
3371 JSVM_Env env;
3372 JSVM_CallbackStruct param[1];
3373 param[0].callback = nullptr;
3374 param[0].callback = getProperty;
3375 JSVM_PropertyDescriptor descriptor[] = {
3376 {"getProperty", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3377 };
3378 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3379 JSVM_EnvScope envScope;
3380 OH_JSVM_OpenEnvScope(env, &envScope);
3381 JSVM_HandleScope handlescope;
3382 OH_JSVM_OpenHandleScope(env, &handlescope);
3383 OH_JSVM_CloseHandleScope(env, handlescope);
3384 OH_JSVM_CloseEnvScope(env, envScope);
3385 OH_JSVM_DestroyEnv(env);
3386 OH_JSVM_CloseVMScope(vm, vm_scope);
3387 OH_JSVM_DestroyVM(vm);
3388 napi_value result11;
3389 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3390 return result11;
3391 }
3392
3393 // case 11 and case 12
CallObjectAsFunction(JSVM_Env env,JSVM_CallbackInfo info)3394 static JSVM_Value CallObjectAsFunction(JSVM_Env env, JSVM_CallbackInfo info)
3395 {
3396 JSVM_CallbackStruct param;
3397 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3398 JSVM_Value thisVar = nullptr;
3399 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3400 return thisVar;
3401 };
3402 param.data = nullptr;
3403
3404 JSVM_CallbackStruct callbackStruct;
3405 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3406 JSVM_Value thisVar = nullptr;
3407 void *innerData;
3408 size_t argc = 1;
3409 JSVM_Value args[1];
3410 OH_JSVM_GetCbInfo(env, info, &argc, args, &thisVar, &innerData);
3411 uint32_t ret = 0;
3412 OH_JSVM_GetValueUint32(env, args[0], &ret);
3413 const char testStr[] = "hello world 111111";
3414 JSVM_Value setvalueName = nullptr;
3415 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3416 return setvalueName;
3417 };
3418 char data[100] = "hello world";
3419 callbackStruct.data = data;
3420 JSVM_Value testWrapClass = nullptr;
3421 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test12", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3422 &propertyCfg, &callbackStruct, &testWrapClass);
3423 if (res == JSVM_OK) {
3424 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3425 }
3426 JSVM_Value instanceValue = nullptr;
3427 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3428
3429 JSVM_Value gloablObj = nullptr;
3430 OH_JSVM_GetGlobal(env, &gloablObj);
3431 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance", instanceValue);
3432 return nullptr;
3433 }
3434
testCallFunction(napi_env env1,napi_callback_info info)3435 static napi_value testCallFunction(napi_env env1, napi_callback_info info)
3436 {
3437 JSVM_InitOptions init_options;
3438 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3439 printf("memset_s failed");
3440 return nullptr;
3441 }
3442 init_options.externalReferences = externals;
3443 if (aa == 0) {
3444 OH_JSVM_Init(&init_options);
3445 aa++;
3446 }
3447 JSVM_VM vm;
3448 JSVM_CreateVMOptions options;
3449 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3450 printf("memset_s failed");
3451 return nullptr;
3452 }
3453 OH_JSVM_CreateVM(&options, &vm);
3454 JSVM_VMScope vm_scope;
3455 OH_JSVM_OpenVMScope(vm, &vm_scope);
3456 JSVM_Env env;
3457
3458 JSVM_CallbackStruct param[1];
3459 param[0].callback = nullptr;
3460 param[0].callback = CallObjectAsFunction;
3461
3462 JSVM_PropertyDescriptor descriptor[] = {
3463 {"CallObjectAsFunction", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3464 };
3465 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3466 JSVM_EnvScope envScope;
3467 OH_JSVM_OpenEnvScope(env, &envScope);
3468 JSVM_HandleScope handlescope;
3469 OH_JSVM_OpenHandleScope(env, &handlescope);
3470 OH_JSVM_CloseHandleScope(env, handlescope);
3471 OH_JSVM_CloseEnvScope(env, envScope);
3472 OH_JSVM_DestroyEnv(env);
3473 OH_JSVM_CloseVMScope(vm, vm_scope);
3474 OH_JSVM_DestroyVM(vm);
3475 napi_value result11;
3476 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3477 return result11;
3478 }
3479
3480 // ============================= test nullptr return for namedProperty handle ====================================
3481 // cases 13-16, return nullptr, only listen without interception, successfully setProperty.
SetNamedPropertyCbInfo1(JSVM_Env env,JSVM_Value name,JSVM_Value property,JSVM_Value thisArg,JSVM_Value data)3482 static JSVM_Value SetNamedPropertyCbInfo1(JSVM_Env env, JSVM_Value name, JSVM_Value property, JSVM_Value thisArg,
3483 JSVM_Value data)
3484 {
3485 char strValue[100];
3486 size_t size;
3487 size_t bufferSize = 300;
3488 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
3489 char str[100];
3490 size_t size1;
3491 OH_JSVM_GetValueStringUtf8(env, property, str, strlen(str), &size1);
3492 JSVM_Value gloablObj = nullptr;
3493 OH_JSVM_GetGlobal(env, &gloablObj);
3494 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
3495 return nullptr;
3496 }
3497
NameHandler(JSVM_Env env,JSVM_CallbackInfo info)3498 static JSVM_Value NameHandler(JSVM_Env env, JSVM_CallbackInfo info)
3499 {
3500 JSVM_CallbackStruct param;
3501 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3502 JSVM_Value thisVar = nullptr;
3503 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3504 return thisVar;
3505 };
3506 param.data = nullptr;
3507 propertyCfg.genericNamedPropertySetterCallback = SetNamedPropertyCbInfo1;
3508 JSVM_CallbackStruct callbackStruct;
3509 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3510 printf("call as a function called");
3511 return nullptr;
3512 };
3513 callbackStruct.data = nullptr;
3514 JSVM_Value testWrapClass = nullptr;
3515 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test4", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg, &callbackStruct, &testWrapClass);
3516 if (res == JSVM_OK) {
3517 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3518 } else {
3519 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3520 return nullptr;
3521 }
3522 JSVM_Value instanceValue = nullptr;
3523 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3524
3525 const char testStr[] = "hello world1";
3526 JSVM_Value setvalueName = nullptr;
3527 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3528 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty1", setvalueName);
3529 JSVM_Value valueName = nullptr;
3530 OH_JSVM_GetNamedProperty(env, instanceValue, "nameProperty1", &valueName);
3531 propertyCfg.genericNamedPropertySetterCallback = nullptr;
3532 return valueName;
3533 }
3534
testSetNamedProperty01(napi_env env1,napi_callback_info info)3535 static napi_value testSetNamedProperty01(napi_env env1, napi_callback_info info)
3536 {
3537 JSVM_InitOptions init_options;
3538 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3539 printf("memset_s failed");
3540 return nullptr;
3541 }
3542 init_options.externalReferences = externals;
3543 if (aa == 0) {
3544 OH_JSVM_Init(&init_options);
3545 aa++;
3546 }
3547 JSVM_VM vm;
3548 JSVM_CreateVMOptions options;
3549 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3550 printf("memset_s failed");
3551 return nullptr;
3552 }
3553 OH_JSVM_CreateVM(&options, &vm);
3554 JSVM_VMScope vm_scope;
3555 OH_JSVM_OpenVMScope(vm, &vm_scope);
3556 JSVM_Env env;
3557
3558 JSVM_CallbackStruct param[1];
3559 param[0].callback = nullptr;
3560 param[0].callback = NameHandler;
3561
3562 JSVM_PropertyDescriptor descriptor[] = {
3563 {"NameHandler", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3564 };
3565 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3566 JSVM_EnvScope envScope;
3567 OH_JSVM_OpenEnvScope(env, &envScope);
3568 JSVM_HandleScope handlescope;
3569 OH_JSVM_OpenHandleScope(env, &handlescope);
3570 OH_JSVM_CloseHandleScope(env, handlescope);
3571 OH_JSVM_CloseEnvScope(env, envScope);
3572 OH_JSVM_DestroyEnv(env);
3573 OH_JSVM_CloseVMScope(vm, vm_scope);
3574 OH_JSVM_DestroyVM(vm);
3575 napi_value result11;
3576 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3577 return result11;
3578 }
3579
3580 // ============================= test non-nullptr return for namedProperty handle ================================
3581 // case 17 Return non null ptr, listen and intercept, failed to setProperty.
SetNamedPropertyCbInfo2(JSVM_Env env,JSVM_Value name,JSVM_Value property,JSVM_Value thisArg,JSVM_Value data)3582 static JSVM_Value SetNamedPropertyCbInfo2(JSVM_Env env, JSVM_Value name, JSVM_Value property, JSVM_Value thisArg,
3583 JSVM_Value data)
3584 {
3585 return property;
3586 }
3587
NameHandler1(JSVM_Env env,JSVM_CallbackInfo info)3588 static JSVM_Value NameHandler1(JSVM_Env env, JSVM_CallbackInfo info)
3589 {
3590 JSVM_CallbackStruct param;
3591 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3592 JSVM_Value thisVar = nullptr;
3593 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3594 return thisVar;
3595 };
3596 param.data = nullptr;
3597 propertyCfg.genericNamedPropertySetterCallback = SetNamedPropertyCbInfo2;
3598 JSVM_Value testWrapClass = nullptr;
3599 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test5", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3600 &propertyCfg, nullptr, &testWrapClass);
3601 if (res == JSVM_OK) {
3602 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3603 } else {
3604 return nullptr;
3605 }
3606 JSVM_Value instanceValue = nullptr;
3607 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3608
3609 const char testStr[] = "hello world2";
3610 JSVM_Value setvalueName = nullptr;
3611 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3612 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty2", setvalueName);
3613
3614 const char testStr2[] = "nameProperty2";
3615 JSVM_Value setvalueName2 = nullptr;
3616 OH_JSVM_CreateStringUtf8(env, testStr2, strlen(testStr2), &setvalueName2);
3617 bool isExisted = false;
3618 OH_JSVM_HasProperty(env, instanceValue, setvalueName2, &isExisted);
3619 propertyCfg.genericNamedPropertySetterCallback = nullptr;
3620 return nullptr;
3621 }
3622
testSetNamedProperty02(napi_env env1,napi_callback_info info)3623 static napi_value testSetNamedProperty02(napi_env env1, napi_callback_info info)
3624 {
3625 JSVM_InitOptions init_options;
3626 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3627 printf("memset_s failed");
3628 return nullptr;
3629 }
3630 init_options.externalReferences = externals;
3631 if (aa == 0) {
3632 OH_JSVM_Init(&init_options);
3633 aa++;
3634 }
3635 JSVM_VM vm;
3636 JSVM_CreateVMOptions options;
3637 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3638 printf("memset_s failed");
3639 return nullptr;
3640 }
3641 OH_JSVM_CreateVM(&options, &vm);
3642 JSVM_VMScope vm_scope;
3643 OH_JSVM_OpenVMScope(vm, &vm_scope);
3644 JSVM_Env env;
3645
3646 JSVM_CallbackStruct param[1];
3647 param[0].callback = nullptr;
3648 param[0].callback = NameHandler1;
3649
3650 JSVM_PropertyDescriptor descriptor[] = {
3651 {"NameHandler1", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3652 };
3653 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3654 JSVM_EnvScope envScope;
3655 OH_JSVM_OpenEnvScope(env, &envScope);
3656 JSVM_HandleScope handlescope;
3657 OH_JSVM_OpenHandleScope(env, &handlescope);
3658 OH_JSVM_CloseHandleScope(env, handlescope);
3659 OH_JSVM_CloseEnvScope(env, envScope);
3660 OH_JSVM_DestroyEnv(env);
3661 OH_JSVM_CloseVMScope(vm, vm_scope);
3662 OH_JSVM_DestroyVM(vm);
3663 napi_value result11;
3664 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3665 return result11;
3666 }
3667
3668 // ============================= test nullptr return for indexedProperty handle ===================================
3669 // cases 18-21 return nullptr, only listen without interception, successfully setProperty.
SetIndexPropertyCbInfo1(JSVM_Env env,JSVM_Value index,JSVM_Value property,JSVM_Value thisArg,JSVM_Value data)3670 static JSVM_Value SetIndexPropertyCbInfo1(JSVM_Env env, JSVM_Value index, JSVM_Value property, JSVM_Value thisArg,
3671 JSVM_Value data)
3672 {
3673 uint32_t value;
3674 OH_JSVM_GetValueUint32(env, index, &value);
3675 char str[100];
3676 size_t size;
3677 OH_JSVM_GetValueStringUtf8(env, property, str, strlen(str), &size);
3678 JSVM_Value gloablObj = nullptr;
3679 OH_JSVM_GetGlobal(env, &gloablObj);
3680 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
3681 return nullptr;
3682 }
3683
IndexHandler1(JSVM_Env env,JSVM_CallbackInfo info)3684 static JSVM_Value IndexHandler1(JSVM_Env env, JSVM_CallbackInfo info)
3685 {
3686 JSVM_CallbackStruct param;
3687 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3688 JSVM_Value thisVar = nullptr;
3689 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3690 return thisVar;
3691 };
3692 param.data = nullptr;
3693
3694 propertyCfg.genericIndexedPropertySetterCallback = SetIndexPropertyCbInfo1;
3695 JSVM_CallbackStruct callbackStruct;
3696 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3697 printf("call as a function called");
3698 return nullptr;
3699 };
3700 callbackStruct.data = nullptr;
3701 JSVM_Value testWrapClass = nullptr;
3702 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test6", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3703 &propertyCfg, &callbackStruct, &testWrapClass);
3704 if (res == JSVM_OK) {
3705 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3706 } else {
3707 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3708 return nullptr;
3709 }
3710 JSVM_Value instanceValue = nullptr;
3711 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3712
3713 JSVM_Value jsIndex = nullptr;
3714 uint32_t index = 0;
3715 OH_JSVM_CreateUint32(env, index, &jsIndex);
3716 const char testStr[] = "hello world1";
3717 JSVM_Value setvalueName = nullptr;
3718 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3719 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
3720 propertyCfg.genericIndexedPropertySetterCallback = nullptr;
3721 return setvalueName;
3722 }
3723
testSetIndexProperty01(napi_env env1,napi_callback_info info)3724 static napi_value testSetIndexProperty01(napi_env env1, napi_callback_info info)
3725 {
3726 JSVM_InitOptions init_options;
3727 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3728 printf("memset_s failed");
3729 return nullptr;
3730 }
3731 init_options.externalReferences = externals;
3732 if (aa == 0) {
3733 OH_JSVM_Init(&init_options);
3734 aa++;
3735 }
3736 JSVM_VM vm;
3737 JSVM_CreateVMOptions options;
3738 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3739 printf("memset_s failed");
3740 return nullptr;
3741 }
3742 OH_JSVM_CreateVM(&options, &vm);
3743 JSVM_VMScope vm_scope;
3744 OH_JSVM_OpenVMScope(vm, &vm_scope);
3745 JSVM_Env env;
3746
3747 JSVM_CallbackStruct param[1];
3748 param[0].callback = nullptr;
3749 param[0].callback = IndexHandler1;
3750
3751 JSVM_PropertyDescriptor descriptor[] = {
3752 {"IndexHandler1", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3753 };
3754 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3755 JSVM_EnvScope envScope;
3756 OH_JSVM_OpenEnvScope(env, &envScope);
3757 JSVM_HandleScope handlescope;
3758 OH_JSVM_OpenHandleScope(env, &handlescope);
3759 OH_JSVM_CloseHandleScope(env, handlescope);
3760 OH_JSVM_CloseEnvScope(env, envScope);
3761 OH_JSVM_DestroyEnv(env);
3762 OH_JSVM_CloseVMScope(vm, vm_scope);
3763 OH_JSVM_DestroyVM(vm);
3764 napi_value result11;
3765 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3766 return result11;
3767 }
3768
3769 // ============================= test non-nullptr return for indexedProperty handle ==================================
3770 // //case 22 returns non nullptr, only listens and intercepts, property setting failed.
SetIndexPropertyCbInfo2(JSVM_Env env,JSVM_Value index,JSVM_Value property,JSVM_Value thisArg,JSVM_Value data)3771 static JSVM_Value SetIndexPropertyCbInfo2(JSVM_Env env, JSVM_Value index, JSVM_Value property, JSVM_Value thisArg,
3772 JSVM_Value data)
3773 {
3774 return property;
3775 }
3776
IndexHandler2(JSVM_Env env,JSVM_CallbackInfo info)3777 static JSVM_Value IndexHandler2(JSVM_Env env, JSVM_CallbackInfo info)
3778 {
3779 JSVM_CallbackStruct param;
3780 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3781 JSVM_Value thisVar = nullptr;
3782 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3783 return thisVar;
3784 };
3785 param.data = nullptr;
3786 propertyCfg.genericIndexedPropertySetterCallback = SetIndexPropertyCbInfo2;
3787 JSVM_Value testWrapClass = nullptr;
3788 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test7", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3789 &propertyCfg, nullptr, &testWrapClass);
3790 if (res == JSVM_OK) {
3791 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3792 } else {
3793 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3794 return nullptr;
3795 }
3796 JSVM_Value instanceValue = nullptr;
3797 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3798
3799 JSVM_Value jsIndex = nullptr;
3800 uint32_t index = 0;
3801 OH_JSVM_CreateUint32(env, index, &jsIndex);
3802 const char testStr[] = "hello world1";
3803 JSVM_Value setvalueName = nullptr;
3804 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3805 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
3806 bool isExisted = false;
3807 OH_JSVM_HasProperty(env, instanceValue, jsIndex, &isExisted);
3808 propertyCfg.genericIndexedPropertySetterCallback = nullptr;
3809 return nullptr;
3810 }
3811
testSetIndexProperty02(napi_env env1,napi_callback_info info)3812 static napi_value testSetIndexProperty02(napi_env env1, napi_callback_info info)
3813 {
3814 JSVM_InitOptions init_options;
3815 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3816 printf("memset_s failed");
3817 return nullptr;
3818 }
3819 init_options.externalReferences = externals;
3820 if (aa == 0) {
3821 OH_JSVM_Init(&init_options);
3822 aa++;
3823 }
3824 JSVM_VM vm;
3825 JSVM_CreateVMOptions options;
3826 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3827 printf("memset_s failed");
3828 return nullptr;
3829 }
3830 OH_JSVM_CreateVM(&options, &vm);
3831 JSVM_VMScope vm_scope;
3832 OH_JSVM_OpenVMScope(vm, &vm_scope);
3833 JSVM_Env env;
3834
3835 JSVM_CallbackStruct param[1];
3836 param[0].callback = nullptr;
3837 param[0].callback = IndexHandler2;
3838
3839 JSVM_PropertyDescriptor descriptor[] = {
3840 {"IndexHandler2", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3841 };
3842 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3843 JSVM_EnvScope envScope;
3844 OH_JSVM_OpenEnvScope(env, &envScope);
3845 JSVM_HandleScope handlescope;
3846 OH_JSVM_OpenHandleScope(env, &handlescope);
3847 OH_JSVM_CloseHandleScope(env, handlescope);
3848 OH_JSVM_CloseEnvScope(env, envScope);
3849 OH_JSVM_DestroyEnv(env);
3850 OH_JSVM_CloseVMScope(vm, vm_scope);
3851 OH_JSVM_DestroyVM(vm);
3852 napi_value result11;
3853 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3854 return result11;
3855 }
3856
3857 // ============================= test nullptr return for namedProperty handle ===============
3858 // case 23~25
GetNamedPropertyCbInfo1(JSVM_Env env,JSVM_Value name,JSVM_Value thisArg,JSVM_Value data)3859 static JSVM_Value GetNamedPropertyCbInfo1(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
3860 {
3861 char strValue[100];
3862 size_t size;
3863 size_t bufferSize = 300;
3864 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
3865 JSVM_Value gloablObj = nullptr;
3866 OH_JSVM_GetGlobal(env, &gloablObj);
3867 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
3868 return nullptr;
3869 }
3870
NameHandler2(JSVM_Env env,JSVM_CallbackInfo info)3871 static JSVM_Value NameHandler2(JSVM_Env env, JSVM_CallbackInfo info)
3872 {
3873 JSVM_CallbackStruct param;
3874 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3875 JSVM_Value thisVar = nullptr;
3876 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3877 return thisVar;
3878 };
3879 param.data = nullptr;
3880 propertyCfg.genericNamedPropertyGetterCallback = GetNamedPropertyCbInfo1;
3881 JSVM_CallbackStruct callbackStruct;
3882 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3883 printf("call as a function called");
3884 return nullptr;
3885 };
3886 callbackStruct.data = nullptr;
3887 JSVM_Value testWrapClass = nullptr;
3888 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test8", NAPI_AUTO_LENGTH, ¶m, 0,
3889 nullptr, &propertyCfg, &callbackStruct, &testWrapClass);
3890 if (res == JSVM_OK) {
3891 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3892 } else {
3893 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3894 return nullptr;
3895 }
3896 JSVM_Value instanceValue = nullptr;
3897 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3898
3899 const char testStr[] = "hello world1";
3900 JSVM_Value setvalueName = nullptr;
3901 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3902 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty1", setvalueName);
3903 JSVM_Value valueName = nullptr;
3904 OH_JSVM_GetNamedProperty(env, instanceValue, "nameProperty1", &valueName);
3905 propertyCfg.genericNamedPropertyGetterCallback = nullptr;
3906 return valueName;
3907 }
3908
testGetNamedProperty01(napi_env env1,napi_callback_info info)3909 static napi_value testGetNamedProperty01(napi_env env1, napi_callback_info info)
3910 {
3911 JSVM_InitOptions init_options;
3912 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3913 printf("memset_s failed");
3914 return nullptr;
3915 }
3916 init_options.externalReferences = externals;
3917 if (aa == 0) {
3918 OH_JSVM_Init(&init_options);
3919 aa++;
3920 }
3921 JSVM_VM vm;
3922 JSVM_CreateVMOptions options;
3923 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3924 printf("memset_s failed");
3925 return nullptr;
3926 }
3927 OH_JSVM_CreateVM(&options, &vm);
3928 JSVM_VMScope vm_scope;
3929 OH_JSVM_OpenVMScope(vm, &vm_scope);
3930 JSVM_Env env;
3931
3932 JSVM_CallbackStruct param[1];
3933 param[0].callback = nullptr;
3934 param[0].callback = NameHandler2;
3935
3936 JSVM_PropertyDescriptor descriptor[] = {
3937 {"NameHandler2", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3938 };
3939 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3940 JSVM_EnvScope envScope;
3941 OH_JSVM_OpenEnvScope(env, &envScope);
3942 JSVM_HandleScope handlescope;
3943 OH_JSVM_OpenHandleScope(env, &handlescope);
3944 OH_JSVM_CloseHandleScope(env, handlescope);
3945 OH_JSVM_CloseEnvScope(env, envScope);
3946 OH_JSVM_DestroyEnv(env);
3947 OH_JSVM_CloseVMScope(vm, vm_scope);
3948 OH_JSVM_DestroyVM(vm);
3949 napi_value result11;
3950 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3951 return result11;
3952 }
3953
3954 // ========================= test non-nullptr return for namedProperty handle ============
3955 // case 26
GetNamedPropertyCbInfo2(JSVM_Env env,JSVM_Value name,JSVM_Value thisArg,JSVM_Value data)3956 static JSVM_Value GetNamedPropertyCbInfo2(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
3957 {
3958 JSVM_Value newResult = nullptr;
3959 if (g_temp) {
3960 char newStr[] = "hi from name handler";
3961 OH_JSVM_CreateStringUtf8(env, newStr, strlen(newStr), &newResult);
3962 }
3963 return newResult;
3964 }
3965
NameHandler3(JSVM_Env env,JSVM_CallbackInfo info)3966 static JSVM_Value NameHandler3(JSVM_Env env, JSVM_CallbackInfo info)
3967 {
3968 JSVM_CallbackStruct param;
3969 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3970 JSVM_Value thisVar = nullptr;
3971 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3972 return thisVar;
3973 };
3974 param.data = nullptr;
3975
3976 propertyCfg.genericNamedPropertyGetterCallback = GetNamedPropertyCbInfo2;
3977 JSVM_Value testWrapClass = nullptr;
3978 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test9", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3979 &propertyCfg, nullptr, &testWrapClass);
3980 if (res == JSVM_OK) {
3981 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3982 } else {
3983 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3984 return nullptr;
3985 }
3986 JSVM_Value instanceValue = nullptr;
3987 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3988
3989 const char testStr[] = "hello world1";
3990 JSVM_Value setvalueName = nullptr;
3991 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3992 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty2", setvalueName);
3993
3994 g_temp = true;
3995 JSVM_Value valueName = nullptr;
3996 OH_JSVM_GetNamedProperty(env, instanceValue, "nameProperty2", &valueName);
3997 char str[100];
3998 size_t size;
3999 OH_JSVM_GetValueStringUtf8(env, valueName, str, strlen(str), &size);
4000 g_temp = false;
4001 OH_JSVM_GetNamedProperty(env, instanceValue, "nameProperty2", &valueName);
4002 char str2[100];
4003 size_t size2;
4004 OH_JSVM_GetValueStringUtf8(env, valueName, str2, strlen(str2), &size2);
4005 propertyCfg.genericNamedPropertyGetterCallback = nullptr;
4006 return valueName;
4007 }
4008
testGetNamedProperty02(napi_env env1,napi_callback_info info)4009 static napi_value testGetNamedProperty02(napi_env env1, napi_callback_info info)
4010 {
4011 JSVM_InitOptions init_options;
4012 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4013 printf("memset_s failed");
4014 return nullptr;
4015 }
4016 init_options.externalReferences = externals;
4017 if (aa == 0) {
4018 OH_JSVM_Init(&init_options);
4019 aa++;
4020 }
4021 JSVM_VM vm;
4022 JSVM_CreateVMOptions options;
4023 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4024 printf("memset_s failed");
4025 return nullptr;
4026 }
4027 OH_JSVM_CreateVM(&options, &vm);
4028 JSVM_VMScope vm_scope;
4029 OH_JSVM_OpenVMScope(vm, &vm_scope);
4030 JSVM_Env env;
4031
4032 JSVM_CallbackStruct param[1];
4033 param[0].callback = nullptr;
4034 param[0].callback = NameHandler3;
4035
4036 JSVM_PropertyDescriptor descriptor[] = {
4037 {"NameHandler3", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4038 };
4039 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4040 JSVM_EnvScope envScope;
4041 OH_JSVM_OpenEnvScope(env, &envScope);
4042 JSVM_HandleScope handlescope;
4043 OH_JSVM_OpenHandleScope(env, &handlescope);
4044 OH_JSVM_CloseHandleScope(env, handlescope);
4045 OH_JSVM_CloseEnvScope(env, envScope);
4046 OH_JSVM_DestroyEnv(env);
4047 OH_JSVM_CloseVMScope(vm, vm_scope);
4048 OH_JSVM_DestroyVM(vm);
4049 napi_value result11;
4050 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4051 return result11;
4052 }
4053
4054 // ========================== test nullptr return for indexedProperty handle ================
4055 // case 27~29
GetIndexPropertyCbInfo1(JSVM_Env env,JSVM_Value index,JSVM_Value thisArg,JSVM_Value data)4056 static JSVM_Value GetIndexPropertyCbInfo1(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4057 {
4058 uint32_t value;
4059 OH_JSVM_GetValueUint32(env, index, &value);
4060 JSVM_Value gloablObj = nullptr;
4061 OH_JSVM_GetGlobal(env, &gloablObj);
4062 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance2", thisArg);
4063 return nullptr;
4064 }
4065
IndexHandler3(JSVM_Env env,JSVM_CallbackInfo info)4066 static JSVM_Value IndexHandler3(JSVM_Env env, JSVM_CallbackInfo info)
4067 {
4068 JSVM_CallbackStruct param;
4069 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4070 JSVM_Value thisVar = nullptr;
4071 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4072 return thisVar;
4073 };
4074 param.data = nullptr;
4075 propertyCfg.genericIndexedPropertyGetterCallback = GetIndexPropertyCbInfo1;
4076 JSVM_CallbackStruct callbackStruct;
4077 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4078 printf("call as a function called");
4079 return nullptr;
4080 };
4081 callbackStruct.data = nullptr;
4082 JSVM_Value testWrapClass = nullptr;
4083 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test10", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4084 &propertyCfg, &callbackStruct, &testWrapClass);
4085 if (res == JSVM_OK) {
4086 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4087 } else {
4088 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4089 return nullptr;
4090 }
4091 JSVM_Value instanceValue = nullptr;
4092 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4093
4094 JSVM_Value jsIndex = nullptr;
4095 uint32_t index = 0;
4096 OH_JSVM_CreateUint32(env, index, &jsIndex);
4097 const char testStr[] = "hello world1";
4098 JSVM_Value setvalueName = nullptr;
4099 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4100 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4101 JSVM_Value valueName = nullptr;
4102 OH_JSVM_GetProperty(env, instanceValue, jsIndex, &valueName);
4103 propertyCfg.genericIndexedPropertyGetterCallback = nullptr;
4104 return valueName;
4105 }
4106
testGetIndexProperty01(napi_env env1,napi_callback_info info)4107 static napi_value testGetIndexProperty01(napi_env env1, napi_callback_info info)
4108 {
4109 JSVM_InitOptions init_options;
4110 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4111 printf("memset_s failed");
4112 return nullptr;
4113 }
4114 init_options.externalReferences = externals;
4115 if (aa == 0) {
4116 OH_JSVM_Init(&init_options);
4117 aa++;
4118 }
4119 JSVM_VM vm;
4120 JSVM_CreateVMOptions options;
4121 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4122 printf("memset_s failed");
4123 return nullptr;
4124 }
4125 OH_JSVM_CreateVM(&options, &vm);
4126 JSVM_VMScope vm_scope;
4127 OH_JSVM_OpenVMScope(vm, &vm_scope);
4128 JSVM_Env env;
4129 JSVM_CallbackStruct param[1];
4130 param[0].callback = nullptr;
4131 param[0].callback = IndexHandler3;
4132
4133 JSVM_PropertyDescriptor descriptor[] = {
4134 {"IndexHandler3", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4135 };
4136 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4137 JSVM_EnvScope envScope;
4138 OH_JSVM_OpenEnvScope(env, &envScope);
4139 JSVM_HandleScope handlescope;
4140 OH_JSVM_OpenHandleScope(env, &handlescope);
4141 OH_JSVM_CloseHandleScope(env, handlescope);
4142 OH_JSVM_CloseEnvScope(env, envScope);
4143 OH_JSVM_DestroyEnv(env);
4144 OH_JSVM_CloseVMScope(vm, vm_scope);
4145 OH_JSVM_DestroyVM(vm);
4146 napi_value result11;
4147 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4148 return result11;
4149 }
4150
4151 // =====================test non-nullptr return for indexedProperty handle =========
4152 // case 30
GetIndexPropertyCbInfo2(JSVM_Env env,JSVM_Value index,JSVM_Value thisArg,JSVM_Value data)4153 static JSVM_Value GetIndexPropertyCbInfo2(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4154 {
4155 JSVM_Value newResult = nullptr;
4156 if (g_temp) {
4157 char newStr[] = "hi from index handler";
4158 OH_JSVM_CreateStringUtf8(env, newStr, strlen(newStr), &newResult);
4159 }
4160 return newResult;
4161 }
4162
IndexHandler4(JSVM_Env env,JSVM_CallbackInfo info)4163 static JSVM_Value IndexHandler4(JSVM_Env env, JSVM_CallbackInfo info)
4164 {
4165 JSVM_CallbackStruct param;
4166 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4167 JSVM_Value thisVar = nullptr;
4168 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4169 return thisVar;
4170 };
4171 param.data = nullptr;
4172 propertyCfg.genericIndexedPropertyGetterCallback = GetIndexPropertyCbInfo2;
4173 JSVM_Value testWrapClass = nullptr;
4174 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test11", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4175 &propertyCfg, nullptr, &testWrapClass);
4176 if (res == JSVM_OK) {
4177 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4178 } else {
4179 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4180 return nullptr;
4181 }
4182 JSVM_Value instanceValue = nullptr;
4183 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4184
4185 JSVM_Value jsIndex = nullptr;
4186 uint32_t index = 0;
4187 OH_JSVM_CreateUint32(env, index, &jsIndex);
4188 const char testStr[] = "hello world1";
4189 JSVM_Value setvalueName = nullptr;
4190 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4191 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4192 g_temp = true;
4193 JSVM_Value valueName = nullptr;
4194 OH_JSVM_GetProperty(env, instanceValue, jsIndex, &valueName);
4195 char str[100];
4196 size_t size;
4197 OH_JSVM_GetValueStringUtf8(env, valueName, str, strlen(str), &size);
4198 g_temp = false;
4199 OH_JSVM_GetProperty(env, instanceValue, jsIndex, &valueName);
4200 char str2[100];
4201 size_t size2;
4202 OH_JSVM_GetValueStringUtf8(env, valueName, str2, strlen(str2), &size2);
4203 propertyCfg.genericIndexedPropertyGetterCallback = nullptr;
4204 return valueName;
4205 }
4206
testGetIndexProperty02(napi_env env1,napi_callback_info info)4207 static napi_value testGetIndexProperty02(napi_env env1, napi_callback_info info)
4208 {
4209 JSVM_InitOptions init_options;
4210 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4211 printf("memset_s failed");
4212 return nullptr;
4213 }
4214 init_options.externalReferences = externals;
4215 if (aa == 0) {
4216 OH_JSVM_Init(&init_options);
4217 aa++;
4218 }
4219 JSVM_VM vm;
4220 JSVM_CreateVMOptions options;
4221 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4222 printf("memset_s failed");
4223 return nullptr;
4224 }
4225 OH_JSVM_CreateVM(&options, &vm);
4226 JSVM_VMScope vm_scope;
4227 OH_JSVM_OpenVMScope(vm, &vm_scope);
4228 JSVM_Env env;
4229
4230 JSVM_CallbackStruct param[1];
4231 param[0].callback = nullptr;
4232 param[0].callback = IndexHandler4;
4233
4234 JSVM_PropertyDescriptor descriptor[] = {
4235 {"IndexHandler4", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4236 };
4237 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4238 JSVM_EnvScope envScope;
4239 OH_JSVM_OpenEnvScope(env, &envScope);
4240 JSVM_HandleScope handlescope;
4241 OH_JSVM_OpenHandleScope(env, &handlescope);
4242 OH_JSVM_CloseHandleScope(env, handlescope);
4243 OH_JSVM_CloseEnvScope(env, envScope);
4244 OH_JSVM_DestroyEnv(env);
4245 OH_JSVM_CloseVMScope(vm, vm_scope);
4246 OH_JSVM_DestroyVM(vm);
4247 napi_value result11;
4248 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4249 return result11;
4250 }
4251
4252 // ======================== test nullptr return for namedProperty handle ==================
4253 // cases 31-33 when returning nullptr, delete listening trigger, only listen without intercepting.
4254 // The attribute deletion is successful, and the OH_JSVM_DeleteProperty output parameter is true.
DeleterNamedPropertyCbInfo1(JSVM_Env env,JSVM_Value name,JSVM_Value thisArg,JSVM_Value data)4255 static JSVM_Value DeleterNamedPropertyCbInfo1(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
4256 {
4257 char strValue[100];
4258 size_t size;
4259 size_t bufferSize = 300;
4260 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
4261 JSVM_Value gloablObj = nullptr;
4262 OH_JSVM_GetGlobal(env, &gloablObj);
4263 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
4264 return nullptr;
4265 }
4266
NameHandler4(JSVM_Env env,JSVM_CallbackInfo info)4267 static JSVM_Value NameHandler4(JSVM_Env env, JSVM_CallbackInfo info)
4268 {
4269 JSVM_CallbackStruct param;
4270 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4271 JSVM_Value thisVar = nullptr;
4272 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4273 return thisVar;
4274 };
4275 param.data = nullptr;
4276 propertyCfg.genericNamedPropertyDeleterCallback = DeleterNamedPropertyCbInfo1;
4277 JSVM_CallbackStruct callbackStruct;
4278 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4279 printf("call as a function called");
4280 return nullptr;
4281 };
4282 callbackStruct.data = nullptr;
4283 JSVM_Value testWrapClass = nullptr;
4284 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test13", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4285 &propertyCfg, &callbackStruct, &testWrapClass);
4286 if (res == JSVM_OK) {
4287 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4288 } else {
4289 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4290 return nullptr;
4291 }
4292 JSVM_Value instanceValue = nullptr;
4293 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4294 const char testStr[] = "hello world1";
4295 JSVM_Value setvalueName = nullptr;
4296 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4297 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty1", setvalueName);
4298 bool result = false;
4299 JSVM_Value propertyName = nullptr;
4300 char propertyChar[] = "nameProperty1";
4301 OH_JSVM_CreateStringUtf8(env, propertyChar, strlen(propertyChar), &propertyName);
4302 OH_JSVM_DeleteProperty(env, instanceValue, propertyName, &result);
4303 bool isExisted = false;
4304 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
4305 propertyCfg.genericNamedPropertyDeleterCallback = nullptr;
4306 return nullptr;
4307 }
4308
testDeleterNamedProperty01(napi_env env1,napi_callback_info info)4309 static napi_value testDeleterNamedProperty01(napi_env env1, napi_callback_info info)
4310 {
4311 JSVM_InitOptions init_options;
4312 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4313 printf("memset_s failed");
4314 return nullptr;
4315 }
4316 init_options.externalReferences = externals;
4317 if (aa == 0) {
4318 OH_JSVM_Init(&init_options);
4319 aa++;
4320 }
4321 JSVM_VM vm;
4322 JSVM_CreateVMOptions options;
4323 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4324 printf("memset_s failed");
4325 return nullptr;
4326 }
4327 OH_JSVM_CreateVM(&options, &vm);
4328 JSVM_VMScope vm_scope;
4329 OH_JSVM_OpenVMScope(vm, &vm_scope);
4330 JSVM_Env env;
4331
4332 JSVM_CallbackStruct param[1];
4333 param[0].callback = nullptr;
4334 param[0].callback = NameHandler4;
4335 JSVM_PropertyDescriptor descriptor[] = {
4336 {"NameHandler4", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4337 };
4338 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4339 JSVM_EnvScope envScope;
4340 OH_JSVM_OpenEnvScope(env, &envScope);
4341 JSVM_HandleScope handlescope;
4342 OH_JSVM_OpenHandleScope(env, &handlescope);
4343 OH_JSVM_CloseHandleScope(env, handlescope);
4344 OH_JSVM_CloseEnvScope(env, envScope);
4345 OH_JSVM_DestroyEnv(env);
4346 OH_JSVM_CloseVMScope(vm, vm_scope);
4347 OH_JSVM_DestroyVM(vm);
4348 napi_value result11;
4349 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4350 return result11;
4351 }
4352
4353 // ===================== test return true for namedProperty handle =======================
4354 // case 34 when returning true, the deletion of the listening trigger is triggered, and the interception
4355 // is successful. The deletion of the attribute fails, and the output parameter of OH_JSVM_DeleteProperty is true.
DeleterNamedPropertyCbInfo2(JSVM_Env env,JSVM_Value name,JSVM_Value thisArg,JSVM_Value data)4356 static JSVM_Value DeleterNamedPropertyCbInfo2(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
4357 {
4358 char strValue[100];
4359 size_t size;
4360 size_t bufferSize = 300;
4361 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
4362 JSVM_Value newResult = nullptr;
4363 bool returnValue = true;
4364 OH_JSVM_GetBoolean(env, returnValue, &newResult);
4365 return newResult;
4366 }
4367
NameHandler5(JSVM_Env env,JSVM_CallbackInfo info)4368 static JSVM_Value NameHandler5(JSVM_Env env, JSVM_CallbackInfo info)
4369 {
4370 JSVM_CallbackStruct param;
4371 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4372 JSVM_Value thisVar = nullptr;
4373 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4374 return thisVar;
4375 };
4376 param.data = nullptr;
4377
4378 propertyCfg.genericIndexedPropertyEnumeratorCallback = nullptr;
4379 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4380 propertyCfg.genericIndexedPropertySetterCallback = nullptr;
4381 propertyCfg.genericIndexedPropertyGetterCallback = nullptr;
4382 propertyCfg.genericNamedPropertyEnumeratorCallback = nullptr;
4383 propertyCfg.genericNamedPropertyDeleterCallback = DeleterNamedPropertyCbInfo2;
4384 propertyCfg.genericNamedPropertySetterCallback = nullptr;
4385 propertyCfg.genericNamedPropertyGetterCallback = nullptr;
4386 JSVM_Value testWrapClass = nullptr;
4387 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test14", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4388 &propertyCfg, nullptr, &testWrapClass);
4389 if (res == JSVM_OK) {
4390 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4391 } else {
4392 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4393 return nullptr;
4394 }
4395 JSVM_Value instanceValue = nullptr;
4396 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4397
4398 const char testStr[] = "hello world2";
4399 JSVM_Value setvalueName = nullptr;
4400 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4401 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty2", setvalueName);
4402
4403 bool result = false;
4404 JSVM_Value propertyName = nullptr;
4405 char propertyChar[] = "nameProperty2";
4406 OH_JSVM_CreateStringUtf8(env, propertyChar, strlen(propertyChar), &propertyName);
4407 OH_JSVM_DeleteProperty(env, instanceValue, propertyName, &result);
4408 bool isExisted = false;
4409 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
4410 return nullptr;
4411 }
4412
testDeleterNamedProperty02(napi_env env1,napi_callback_info info)4413 static napi_value testDeleterNamedProperty02(napi_env env1, napi_callback_info info)
4414 {
4415 JSVM_InitOptions init_options;
4416 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4417 printf("memset_s failed");
4418 return nullptr;
4419 }
4420 init_options.externalReferences = externals;
4421 if (aa == 0) {
4422 OH_JSVM_Init(&init_options);
4423 aa++;
4424 }
4425 JSVM_VM vm;
4426 JSVM_CreateVMOptions options;
4427 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4428 printf("memset_s failed");
4429 return nullptr;
4430 }
4431 OH_JSVM_CreateVM(&options, &vm);
4432 JSVM_VMScope vm_scope;
4433 OH_JSVM_OpenVMScope(vm, &vm_scope);
4434 JSVM_Env env;
4435 JSVM_CallbackStruct param[1];
4436 param[0].callback = nullptr;
4437 param[0].callback = NameHandler5;
4438 JSVM_PropertyDescriptor descriptor[] = {
4439 {"NameHandler5", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4440 };
4441 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4442 JSVM_EnvScope envScope;
4443 OH_JSVM_OpenEnvScope(env, &envScope);
4444 JSVM_HandleScope handlescope;
4445 OH_JSVM_OpenHandleScope(env, &handlescope);
4446 OH_JSVM_CloseHandleScope(env, handlescope);
4447 OH_JSVM_CloseEnvScope(env, envScope);
4448 OH_JSVM_DestroyEnv(env);
4449 OH_JSVM_CloseVMScope(vm, vm_scope);
4450 OH_JSVM_DestroyVM(vm);
4451 napi_value result11;
4452 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4453 return result11;
4454 }
4455
4456 // ======================= test return false for namedProperty handle ========================
4457 // case 35 when false is returned, the deletion of the listening trigger is triggered, and the
4458 // interception is successful. The attribute deletion fails, and the OH_JSVM_DeleteProperty output parameter is false.
DeleterNamedPropertyCbInfo3(JSVM_Env env,JSVM_Value name,JSVM_Value thisArg,JSVM_Value data)4459 static JSVM_Value DeleterNamedPropertyCbInfo3(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
4460 {
4461 char strValue[100];
4462 size_t size;
4463 size_t bufferSize = 300;
4464 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
4465 JSVM_Value newResult = nullptr;
4466 bool returnValue = false;
4467 OH_JSVM_GetBoolean(env, returnValue, &newResult);
4468 return newResult;
4469 }
4470
NameHandler6(JSVM_Env env,JSVM_CallbackInfo info)4471 static JSVM_Value NameHandler6(JSVM_Env env, JSVM_CallbackInfo info)
4472 {
4473 JSVM_CallbackStruct param;
4474 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4475 JSVM_Value thisVar = nullptr;
4476 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4477 return thisVar;
4478 };
4479 param.data = nullptr;
4480
4481 propertyCfg.genericIndexedPropertyEnumeratorCallback = nullptr;
4482 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4483 propertyCfg.genericIndexedPropertySetterCallback = nullptr;
4484 propertyCfg.genericIndexedPropertyGetterCallback = nullptr;
4485 propertyCfg.genericNamedPropertyEnumeratorCallback = nullptr;
4486 propertyCfg.genericNamedPropertyDeleterCallback = DeleterNamedPropertyCbInfo3;
4487 propertyCfg.genericNamedPropertySetterCallback = nullptr;
4488 propertyCfg.genericNamedPropertyGetterCallback = nullptr;
4489 JSVM_Value testWrapClass = nullptr;
4490 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test15", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4491 &propertyCfg, nullptr, &testWrapClass);
4492 if (res == JSVM_OK) {
4493 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4494 } else {
4495 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4496 return nullptr;
4497 }
4498 JSVM_Value instanceValue = nullptr;
4499 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4500 const char testStr[] = "hello world3";
4501 JSVM_Value setvalueName = nullptr;
4502 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4503 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty3", setvalueName);
4504 bool result = false;
4505 JSVM_Value propertyName = nullptr;
4506 char propertyChar[] = "nameProperty3";
4507 OH_JSVM_CreateStringUtf8(env, propertyChar, strlen(propertyChar), &propertyName);
4508 OH_JSVM_DeleteProperty(env, instanceValue, propertyName, &result);
4509 bool isExisted = false;
4510 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
4511 return nullptr;
4512 }
4513
testDeleterNamedProperty03(napi_env env1,napi_callback_info info)4514 static napi_value testDeleterNamedProperty03(napi_env env1, napi_callback_info info)
4515 {
4516 JSVM_InitOptions init_options;
4517 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4518 printf("memset_s failed");
4519 return nullptr;
4520 }
4521 init_options.externalReferences = externals;
4522 if (aa == 0) {
4523 OH_JSVM_Init(&init_options);
4524 aa++;
4525 }
4526 JSVM_VM vm;
4527 JSVM_CreateVMOptions options;
4528 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4529 printf("memset_s failed");
4530 return nullptr;
4531 }
4532 OH_JSVM_CreateVM(&options, &vm);
4533 JSVM_VMScope vm_scope;
4534 OH_JSVM_OpenVMScope(vm, &vm_scope);
4535 JSVM_Env env;
4536 JSVM_CallbackStruct param[1];
4537 param[0].callback = nullptr;
4538 param[0].callback = NameHandler6;
4539 JSVM_PropertyDescriptor descriptor[] = {
4540 {"NameHandler6", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4541 };
4542 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4543 JSVM_EnvScope envScope;
4544 OH_JSVM_OpenEnvScope(env, &envScope);
4545 JSVM_HandleScope handlescope;
4546 OH_JSVM_OpenHandleScope(env, &handlescope);
4547 OH_JSVM_CloseHandleScope(env, handlescope);
4548 OH_JSVM_CloseEnvScope(env, envScope);
4549 OH_JSVM_DestroyEnv(env);
4550 OH_JSVM_CloseVMScope(vm, vm_scope);
4551 OH_JSVM_DestroyVM(vm);
4552 napi_value result11;
4553 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4554 return result11;
4555 }
4556
4557 // ==================test nullptr return for indexedProperty handle =====================
4558 // cases 36-38, when returning nullptr, delete listening trigger, only listen without intercepting.
4559 // The attribute deletion is successful, and the OH_JSVM_DeleteProperty output parameter is true.
DeleterIndexedPropertyCbInfo1(JSVM_Env env,JSVM_Value index,JSVM_Value thisArg,JSVM_Value data)4560 static JSVM_Value DeleterIndexedPropertyCbInfo1(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4561 {
4562 uint32_t value;
4563 OH_JSVM_GetValueUint32(env, index, &value);
4564 JSVM_Value gloablObj = nullptr;
4565 OH_JSVM_GetGlobal(env, &gloablObj);
4566 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
4567 return nullptr;
4568 }
4569
IndexHandler5(JSVM_Env env,JSVM_CallbackInfo info)4570 static JSVM_Value IndexHandler5(JSVM_Env env, JSVM_CallbackInfo info)
4571 {
4572 JSVM_CallbackStruct param;
4573 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4574 JSVM_Value thisVar = nullptr;
4575 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4576 return thisVar;
4577 };
4578 param.data = nullptr;
4579 propertyCfg.genericIndexedPropertyDeleterCallback = DeleterIndexedPropertyCbInfo1;
4580 JSVM_CallbackStruct callbackStruct;
4581 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4582 printf("call as a function called");
4583 return nullptr;
4584 };
4585 callbackStruct.data = nullptr;
4586 JSVM_Value testWrapClass = nullptr;
4587 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test16", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4588 &propertyCfg, &callbackStruct, &testWrapClass);
4589 if (res == JSVM_OK) {
4590 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4591 } else {
4592 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4593 return nullptr;
4594 }
4595 JSVM_Value instanceValue = nullptr;
4596 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4597
4598 const char testStr[] = "hello world1";
4599 JSVM_Value setvalueName = nullptr;
4600 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4601 JSVM_Value jsIndex = nullptr;
4602 uint32_t index = 0;
4603 OH_JSVM_CreateUint32(env, index, &jsIndex);
4604 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4605 bool result = false;
4606 OH_JSVM_DeleteProperty(env, instanceValue, jsIndex, &result);
4607 bool isExisted = false;
4608 OH_JSVM_HasProperty(env, instanceValue, jsIndex, &isExisted);
4609 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4610 return nullptr;
4611 }
4612
testDeleterIndexedProperty01(napi_env env1,napi_callback_info info)4613 static napi_value testDeleterIndexedProperty01(napi_env env1, napi_callback_info info)
4614 {
4615 JSVM_InitOptions init_options;
4616 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4617 printf("memset_s failed");
4618 return nullptr;
4619 }
4620 init_options.externalReferences = externals;
4621 if (aa == 0) {
4622 OH_JSVM_Init(&init_options);
4623 aa++;
4624 }
4625 JSVM_VM vm;
4626 JSVM_CreateVMOptions options;
4627 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4628 printf("memset_s failed");
4629 return nullptr;
4630 }
4631 OH_JSVM_CreateVM(&options, &vm);
4632 JSVM_VMScope vm_scope;
4633 OH_JSVM_OpenVMScope(vm, &vm_scope);
4634 JSVM_Env env;
4635 JSVM_CallbackStruct param[1];
4636 param[0].callback = nullptr;
4637 param[0].callback = IndexHandler5;
4638 JSVM_PropertyDescriptor descriptor[] = {
4639 {"IndexHandler5", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4640 };
4641 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4642 JSVM_EnvScope envScope;
4643 OH_JSVM_OpenEnvScope(env, &envScope);
4644 JSVM_HandleScope handlescope;
4645 OH_JSVM_OpenHandleScope(env, &handlescope);
4646 OH_JSVM_CloseHandleScope(env, handlescope);
4647 OH_JSVM_CloseEnvScope(env, envScope);
4648 OH_JSVM_DestroyEnv(env);
4649 OH_JSVM_CloseVMScope(vm, vm_scope);
4650 OH_JSVM_DestroyVM(vm);
4651 napi_value result11;
4652 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4653 return result11;
4654 }
4655
4656 // =================== test return true for indexedProperty handle ========================
4657 // case 39 When returning true, the deletion of the listening trigger is triggered, and the interception
4658 // is successful. The attribute deletion fails, and the OH_JSVM_DeleteProperty output parameter is true.
DeleterIndexedPropertyCbInfo2(JSVM_Env env,JSVM_Value index,JSVM_Value thisArg,JSVM_Value data)4659 static JSVM_Value DeleterIndexedPropertyCbInfo2(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4660 {
4661 uint32_t value;
4662 OH_JSVM_GetValueUint32(env, index, &value);
4663 JSVM_Value newResult = nullptr;
4664 bool returnValue = true;
4665 OH_JSVM_GetBoolean(env, returnValue, &newResult);
4666 return newResult;
4667 }
4668
IndexHandler6(JSVM_Env env,JSVM_CallbackInfo info)4669 static JSVM_Value IndexHandler6(JSVM_Env env, JSVM_CallbackInfo info)
4670 {
4671 JSVM_CallbackStruct param;
4672 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4673 JSVM_Value thisVar = nullptr;
4674 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4675 return thisVar;
4676 };
4677 param.data = nullptr;
4678 propertyCfg.genericIndexedPropertyDeleterCallback = DeleterIndexedPropertyCbInfo2;
4679 JSVM_Value testWrapClass = nullptr;
4680 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test17", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4681 &propertyCfg, nullptr, &testWrapClass);
4682 if (res == JSVM_OK) {
4683 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4684 } else {
4685 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4686 return nullptr;
4687 }
4688 JSVM_Value instanceValue = nullptr;
4689 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4690
4691 JSVM_Value jsIndex = nullptr;
4692 uint32_t index = 0;
4693 OH_JSVM_CreateUint32(env, index, &jsIndex);
4694 const char testStr[] = "hello world2";
4695 JSVM_Value setvalueName = nullptr;
4696 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4697 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4698 bool result = false;
4699 OH_JSVM_DeleteProperty(env, instanceValue, jsIndex, &result);
4700 bool isExisted = false;
4701 OH_JSVM_HasProperty(env, instanceValue, jsIndex, &isExisted);
4702 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4703 return nullptr;
4704 }
4705
testDeleterIndexedProperty02(napi_env env1,napi_callback_info info)4706 static napi_value testDeleterIndexedProperty02(napi_env env1, napi_callback_info info)
4707 {
4708 JSVM_InitOptions init_options;
4709 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4710 printf("memset_s failed");
4711 return nullptr;
4712 }
4713 init_options.externalReferences = externals;
4714 if (aa == 0) {
4715 OH_JSVM_Init(&init_options);
4716 aa++;
4717 }
4718 JSVM_VM vm;
4719 JSVM_CreateVMOptions options;
4720 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4721 printf("memset_s failed");
4722 return nullptr;
4723 }
4724 OH_JSVM_CreateVM(&options, &vm);
4725 JSVM_VMScope vm_scope;
4726 OH_JSVM_OpenVMScope(vm, &vm_scope);
4727 JSVM_Env env;
4728 JSVM_CallbackStruct param[1];
4729 param[0].callback = nullptr;
4730 param[0].callback = IndexHandler6;
4731 JSVM_PropertyDescriptor descriptor[] = {
4732 {"IndexHandler6", NULL, ¶m[1], NULL, NULL, NULL, JSVM_DEFAULT},
4733 };
4734 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4735 JSVM_EnvScope envScope;
4736 OH_JSVM_OpenEnvScope(env, &envScope);
4737 JSVM_HandleScope handlescope;
4738 OH_JSVM_OpenHandleScope(env, &handlescope);
4739 OH_JSVM_CloseHandleScope(env, handlescope);
4740 OH_JSVM_CloseEnvScope(env, envScope);
4741 OH_JSVM_DestroyEnv(env);
4742 OH_JSVM_CloseVMScope(vm, vm_scope);
4743 OH_JSVM_DestroyVM(vm);
4744 napi_value result11;
4745 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4746 return result11;
4747 }
4748
4749 // ============================= test return false for indexedProperty handle =======================
4750 // case 40 When returning false, the deletion of the listening trigger is triggered, and the interception
4751 // is successful.The attribute deletion fails, and the OH_JSVM_DeleteProperty output parameter is false.
DeleterIndexedPropertyCbInfo3(JSVM_Env env,JSVM_Value index,JSVM_Value thisArg,JSVM_Value data)4752 static JSVM_Value DeleterIndexedPropertyCbInfo3(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4753 {
4754 uint32_t value;
4755 OH_JSVM_GetValueUint32(env, index, &value);
4756 JSVM_Value newResult = nullptr;
4757 bool returnValue = false;
4758 OH_JSVM_GetBoolean(env, returnValue, &newResult);
4759 return newResult;
4760 }
4761
IndexHandler7(JSVM_Env env,JSVM_CallbackInfo info)4762 static JSVM_Value IndexHandler7(JSVM_Env env, JSVM_CallbackInfo info)
4763 {
4764 JSVM_CallbackStruct param;
4765 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4766 JSVM_Value thisVar = nullptr;
4767 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4768 return thisVar;
4769 };
4770 param.data = nullptr;
4771 propertyCfg.genericIndexedPropertyDeleterCallback = DeleterIndexedPropertyCbInfo3;
4772 JSVM_Value testWrapClass = nullptr;
4773 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test18", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4774 &propertyCfg, nullptr, &testWrapClass);
4775 if (res == JSVM_OK) {
4776 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4777 } else {
4778 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4779 return nullptr;
4780 }
4781 JSVM_Value instanceValue = nullptr;
4782 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4783
4784 JSVM_Value jsIndex = nullptr;
4785 uint32_t index = 0;
4786 OH_JSVM_CreateUint32(env, index, &jsIndex);
4787 const char testStr[] = "hello world2";
4788 JSVM_Value setvalueName = nullptr;
4789 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4790 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4791 bool result = false;
4792 OH_JSVM_DeleteProperty(env, instanceValue, jsIndex, &result);
4793 bool isExisted = false;
4794 OH_JSVM_HasProperty(env, instanceValue, jsIndex, &isExisted);
4795 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4796 return nullptr;
4797 }
4798
testDeleterIndexedProperty03(napi_env env1,napi_callback_info info)4799 static napi_value testDeleterIndexedProperty03(napi_env env1, napi_callback_info info)
4800 {
4801 JSVM_InitOptions init_options;
4802 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4803 printf("memset_s failed");
4804 return nullptr;
4805 }
4806 init_options.externalReferences = externals;
4807 if (aa == 0) {
4808 OH_JSVM_Init(&init_options);
4809 aa++;
4810 }
4811 JSVM_VM vm;
4812 JSVM_CreateVMOptions options;
4813 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4814 printf("memset_s failed");
4815 return nullptr;
4816 }
4817 OH_JSVM_CreateVM(&options, &vm);
4818 JSVM_VMScope vm_scope;
4819 OH_JSVM_OpenVMScope(vm, &vm_scope);
4820 JSVM_Env env;
4821 JSVM_CallbackStruct param[1];
4822 param[0].callback = nullptr;
4823 param[0].callback = IndexHandler7;
4824 JSVM_PropertyDescriptor descriptor[] = {
4825 {"IndexHandler7", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4826 };
4827 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4828 JSVM_EnvScope envScope;
4829 OH_JSVM_OpenEnvScope(env, &envScope);
4830 JSVM_HandleScope handlescope;
4831 OH_JSVM_OpenHandleScope(env, &handlescope);
4832 OH_JSVM_CloseHandleScope(env, handlescope);
4833 OH_JSVM_CloseEnvScope(env, envScope);
4834 OH_JSVM_DestroyEnv(env);
4835 OH_JSVM_CloseVMScope(vm, vm_scope);
4836 OH_JSVM_DestroyVM(vm);
4837 napi_value result11;
4838 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4839 return result11;
4840 }
4841
4842 // =================test nullptr return for namedProperty handle =======
4843 // case 41 and case 42
4844 // When returning nullptr, the enumerator listens and triggers, only listens but not
4845 // intercepts.OH_JSVM_GetAllPropertyNames can retrieve the property names that have already been set.
EnumeratorNamedPropertyCbInfo1(JSVM_Env env,JSVM_Value thisArg,JSVM_Value data)4846 static JSVM_Value EnumeratorNamedPropertyCbInfo1(JSVM_Env env, JSVM_Value thisArg, JSVM_Value data)
4847 {
4848 JSVM_Value gloablObj = nullptr;
4849 OH_JSVM_GetGlobal(env, &gloablObj);
4850 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
4851 return nullptr;
4852 }
4853
NameHandler7(JSVM_Env env,JSVM_CallbackInfo info)4854 static JSVM_Value NameHandler7(JSVM_Env env, JSVM_CallbackInfo info)
4855 {
4856 JSVM_CallbackStruct param;
4857 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4858 JSVM_Value thisVar = nullptr;
4859 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4860 return thisVar;
4861 };
4862 param.data = nullptr;
4863 propertyCfg.genericNamedPropertyEnumeratorCallback = EnumeratorNamedPropertyCbInfo1;
4864 JSVM_CallbackStruct callbackStruct;
4865 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4866 printf("call as a function called");
4867 return nullptr;
4868 };
4869 callbackStruct.data = nullptr;
4870 JSVM_Value testWrapClass = nullptr;
4871 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test19", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4872 &propertyCfg, &callbackStruct, &testWrapClass);
4873 if (res == JSVM_OK) {
4874 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4875 } else {
4876 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4877 return nullptr;
4878 }
4879
4880 JSVM_Value instanceValue = nullptr;
4881 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4882
4883 const char testStr[] = "hello world1";
4884 JSVM_Value setvalueName = nullptr;
4885 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4886 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty1", setvalueName);
4887 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty2", setvalueName);
4888
4889 JSVM_Value allPropertyNames = nullptr;
4890 OH_JSVM_GetAllPropertyNames(
4891 env,
4892 instanceValue,
4893 JSVM_KEY_OWN_ONLY,
4894 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_SKIP_SYMBOLS),
4895 JSVM_KEY_NUMBERS_TO_STRINGS,
4896 &allPropertyNames);
4897 uint32_t nameSize = 0;
4898 OH_JSVM_GetArrayLength(env, allPropertyNames, &nameSize);
4899 JSVM_Value propertyName = nullptr;
4900 for (uint32_t i = 0; i < nameSize; ++i) {
4901 OH_JSVM_GetElement(env, allPropertyNames, i, &propertyName);
4902 char str[100];
4903 size_t size;
4904 OH_JSVM_GetValueStringUtf8(env, propertyName, str, strlen(str), &size);
4905 bool isExisted = false;
4906 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
4907 }
4908 propertyCfg.genericNamedPropertyEnumeratorCallback = nullptr;
4909 return nullptr;
4910 }
4911
testEnumeratorNamedProperty01(napi_env env1,napi_callback_info info)4912 static napi_value testEnumeratorNamedProperty01(napi_env env1, napi_callback_info info)
4913 {
4914 JSVM_InitOptions init_options;
4915 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4916 printf("memset_s failed");
4917 return nullptr;
4918 }
4919 init_options.externalReferences = externals;
4920 if (aa == 0) {
4921 OH_JSVM_Init(&init_options);
4922 aa++;
4923 }
4924 JSVM_VM vm;
4925 JSVM_CreateVMOptions options;
4926 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4927 printf("memset_s failed");
4928 return nullptr;
4929 }
4930 OH_JSVM_CreateVM(&options, &vm);
4931 JSVM_VMScope vm_scope;
4932 OH_JSVM_OpenVMScope(vm, &vm_scope);
4933 JSVM_Env env;
4934 JSVM_CallbackStruct param[1];
4935 param[0].callback = nullptr;
4936 param[0].callback = NameHandler7;
4937 JSVM_PropertyDescriptor descriptor[] = {
4938 {"NameHandler7", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4939 };
4940 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4941 JSVM_EnvScope envScope;
4942 OH_JSVM_OpenEnvScope(env, &envScope);
4943 JSVM_HandleScope handlescope;
4944 OH_JSVM_OpenHandleScope(env, &handlescope);
4945 OH_JSVM_CloseHandleScope(env, handlescope);
4946 OH_JSVM_CloseEnvScope(env, envScope);
4947 OH_JSVM_DestroyEnv(env);
4948 OH_JSVM_CloseVMScope(vm, vm_scope);
4949 OH_JSVM_DestroyVM(vm);
4950 napi_value result11;
4951 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4952 return result11;
4953 }
4954
4955 // ================== test return true for namedProperty handle =========
4956 // case 43
4957 // When returning nullptr, the enumerator listens and triggers, only listens but not
4958 // intercepts. OH_JSVM_GetAllPropertyNames can retrieve the property names that have already been set.
EnumeratorNamedPropertyCbInfo2(JSVM_Env env,JSVM_Value thisArg,JSVM_Value data)4959 static JSVM_Value EnumeratorNamedPropertyCbInfo2(JSVM_Env env, JSVM_Value thisArg, JSVM_Value data)
4960 {
4961 uint32_t arrayLength = 2;
4962 JSVM_Value testArray = nullptr;
4963 OH_JSVM_CreateArrayWithLength(env, arrayLength, &testArray);
4964 JSVM_Value name1 = nullptr;
4965 char newStr1[] = "hahaha";
4966 OH_JSVM_CreateStringUtf8(env, newStr1, strlen(newStr1), &name1);
4967 JSVM_Value name2 = nullptr;
4968 char newStr2[] = "heheheh";
4969 OH_JSVM_CreateStringUtf8(env, newStr2, strlen(newStr2), &name2);
4970 OH_JSVM_SetElement(env, testArray, 0, name1);
4971 OH_JSVM_SetElement(env, testArray, 1, name2);
4972 return testArray;
4973 }
4974
NameHandler8(JSVM_Env env,JSVM_CallbackInfo info)4975 static JSVM_Value NameHandler8(JSVM_Env env, JSVM_CallbackInfo info)
4976 {
4977 JSVM_CallbackStruct param;
4978 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4979 JSVM_Value thisVar = nullptr;
4980 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4981 return thisVar;
4982 };
4983 param.data = nullptr;
4984 propertyCfg.genericNamedPropertyEnumeratorCallback = EnumeratorNamedPropertyCbInfo2;
4985 JSVM_Value testWrapClass = nullptr;
4986 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test20", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4987 &propertyCfg, nullptr, &testWrapClass);
4988 if (res == JSVM_OK) {
4989 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4990 } else {
4991 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4992 return nullptr;
4993 }
4994 JSVM_Value instanceValue = nullptr;
4995 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4996 const char testStr[] = "hello world2";
4997 JSVM_Value setvalueName = nullptr;
4998 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4999 OH_JSVM_SetNamedProperty(env, instanceValue, "str11", setvalueName);
5000 OH_JSVM_SetNamedProperty(env, instanceValue, "str123", setvalueName);
5001 JSVM_Value allPropertyNames = nullptr;
5002 OH_JSVM_GetAllPropertyNames(
5003 env,
5004 instanceValue,
5005 JSVM_KEY_OWN_ONLY,
5006 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_SKIP_SYMBOLS),
5007 JSVM_KEY_NUMBERS_TO_STRINGS,
5008 &allPropertyNames);
5009 uint32_t nameSize = 0;
5010 OH_JSVM_GetArrayLength(env, allPropertyNames, &nameSize);
5011 JSVM_Value propertyName = nullptr;
5012 for (uint32_t i = 0; i < nameSize; ++i) {
5013 OH_JSVM_GetElement(env, allPropertyNames, i, &propertyName);
5014 char str[100];
5015 size_t size;
5016 OH_JSVM_GetValueStringUtf8(env, propertyName, str, strlen(str), &size);
5017 bool isExisted = false;
5018 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
5019 }
5020 propertyCfg.genericNamedPropertyEnumeratorCallback = nullptr;
5021 return nullptr;
5022 }
5023
testEnumeratorNamedProperty02(napi_env env1,napi_callback_info info)5024 static napi_value testEnumeratorNamedProperty02(napi_env env1, napi_callback_info info)
5025 {
5026 JSVM_InitOptions init_options;
5027 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
5028 printf("memset_s failed");
5029 return nullptr;
5030 }
5031 init_options.externalReferences = externals;
5032 if (aa == 0) {
5033 OH_JSVM_Init(&init_options);
5034 aa++;
5035 }
5036 JSVM_VM vm;
5037 JSVM_CreateVMOptions options;
5038 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
5039 printf("memset_s failed");
5040 return nullptr;
5041 }
5042 OH_JSVM_CreateVM(&options, &vm);
5043 JSVM_VMScope vm_scope;
5044 OH_JSVM_OpenVMScope(vm, &vm_scope);
5045 JSVM_Env env;
5046 JSVM_CallbackStruct param[1];
5047 param[0].callback = nullptr;
5048 param[0].callback = NameHandler8;
5049 JSVM_PropertyDescriptor descriptor[] = {
5050 {"NameHandler8", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
5051 };
5052 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
5053 JSVM_EnvScope envScope;
5054 OH_JSVM_OpenEnvScope(env, &envScope);
5055 JSVM_HandleScope handlescope;
5056 OH_JSVM_OpenHandleScope(env, &handlescope);
5057 OH_JSVM_CloseHandleScope(env, handlescope);
5058 OH_JSVM_CloseEnvScope(env, envScope);
5059 OH_JSVM_DestroyEnv(env);
5060 OH_JSVM_CloseVMScope(vm, vm_scope);
5061 OH_JSVM_DestroyVM(vm);
5062 napi_value result11;
5063 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
5064 return result11;
5065 }
5066
5067 // ================== test nullptr return for indexedProperty handle =========
5068 // case 44 and case 45
5069 // When returning nullptr, the enumerator listens and triggers, only listens but not intercepts.
5070 // OH_JSVM_GetAllPropertyNames can retrieve the property names that have already been set
EnumeratorIndexedPropertyCbInfo1(JSVM_Env env,JSVM_Value thisArg,JSVM_Value data)5071 static JSVM_Value EnumeratorIndexedPropertyCbInfo1(JSVM_Env env, JSVM_Value thisArg, JSVM_Value data)
5072 {
5073 JSVM_Value gloablObj = nullptr;
5074 OH_JSVM_GetGlobal(env, &gloablObj);
5075 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
5076 return nullptr;
5077 }
5078
IndexHandler8(JSVM_Env env,JSVM_CallbackInfo info)5079 static JSVM_Value IndexHandler8(JSVM_Env env, JSVM_CallbackInfo info)
5080 {
5081 JSVM_CallbackStruct param;
5082 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
5083 JSVM_Value thisVar = nullptr;
5084 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
5085 return thisVar;
5086 };
5087 param.data = nullptr;
5088 propertyCfg.genericIndexedPropertyEnumeratorCallback = EnumeratorIndexedPropertyCbInfo1;
5089 JSVM_CallbackStruct callbackStruct;
5090 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
5091 printf("call as a function called");
5092 return nullptr;
5093 };
5094 callbackStruct.data = nullptr;
5095 JSVM_Value testWrapClass = nullptr;
5096 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test21", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
5097 &propertyCfg, &callbackStruct, &testWrapClass);
5098 if (res == JSVM_OK) {
5099 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
5100 } else {
5101 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
5102 return nullptr;
5103 }
5104 JSVM_Value instanceValue = nullptr;
5105 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
5106
5107 const char testStr[] = "hello world1";
5108 JSVM_Value setvalueName = nullptr;
5109 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
5110 JSVM_Value jsIndex = nullptr;
5111 uint32_t index = 0;
5112 OH_JSVM_CreateUint32(env, index, &jsIndex);
5113 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
5114 JSVM_Value jsIndex1 = nullptr;
5115 uint32_t index1 = 1;
5116 OH_JSVM_CreateUint32(env, index1, &jsIndex1);
5117 OH_JSVM_SetProperty(env, instanceValue, jsIndex1, setvalueName);
5118 JSVM_Value allPropertyNames = nullptr;
5119 OH_JSVM_GetAllPropertyNames(
5120 env,
5121 instanceValue,
5122 JSVM_KEY_OWN_ONLY,
5123 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_SKIP_SYMBOLS),
5124 JSVM_KEY_NUMBERS_TO_STRINGS,
5125 &allPropertyNames);
5126 uint32_t nameSize = 0;
5127 OH_JSVM_GetArrayLength(env, allPropertyNames, &nameSize);
5128 JSVM_Value propertyName = nullptr;
5129 for (uint32_t i = 0; i < nameSize; ++i) {
5130 OH_JSVM_GetElement(env, allPropertyNames, i, &propertyName);
5131 char str[100];
5132 size_t size;
5133 OH_JSVM_GetValueStringUtf8(env, propertyName, str, strlen(str), &size);
5134 bool isExisted = false;
5135 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
5136 }
5137 propertyCfg.genericIndexedPropertyEnumeratorCallback = nullptr;
5138 return nullptr;
5139 }
5140
testEnumeratorIndexedProperty01(napi_env env1,napi_callback_info info)5141 static napi_value testEnumeratorIndexedProperty01(napi_env env1, napi_callback_info info)
5142 {
5143 JSVM_InitOptions init_options;
5144 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
5145 printf("memset_s failed");
5146 return nullptr;
5147 }
5148 init_options.externalReferences = externals;
5149 if (aa == 0) {
5150 OH_JSVM_Init(&init_options);
5151 aa++;
5152 }
5153 JSVM_VM vm;
5154 JSVM_CreateVMOptions options;
5155 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
5156 printf("memset_s failed");
5157 return nullptr;
5158 }
5159 OH_JSVM_CreateVM(&options, &vm);
5160 JSVM_VMScope vm_scope;
5161 OH_JSVM_OpenVMScope(vm, &vm_scope);
5162 JSVM_Env env;
5163 JSVM_CallbackStruct param[1];
5164 param[0].callback = nullptr;
5165 param[0].callback = IndexHandler8;
5166 JSVM_PropertyDescriptor descriptor[] = {
5167 {"indexHandler8", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
5168 };
5169 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
5170 JSVM_EnvScope envScope;
5171 OH_JSVM_OpenEnvScope(env, &envScope);
5172 JSVM_HandleScope handlescope;
5173 OH_JSVM_OpenHandleScope(env, &handlescope);
5174 OH_JSVM_CloseHandleScope(env, handlescope);
5175 OH_JSVM_CloseEnvScope(env, envScope);
5176 OH_JSVM_DestroyEnv(env);
5177 OH_JSVM_CloseVMScope(vm, vm_scope);
5178 OH_JSVM_DestroyVM(vm);
5179 napi_value result11;
5180 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
5181 return result11;
5182 }
5183
5184 // =================== test return true for indexedProperty handle ============
5185 // case 46
5186 // When returning the array, the enumerator listens and triggers, listens and intercepts, and
5187 // OH_JSVM_GetAllPropertyNames can retrieve the already set property names You can obtain the property
5188 // names set in the listening callback and in the array, but the property names set in the callback are not actual properties
EnumeratorIndexedPropertyCbInfo2(JSVM_Env env,JSVM_Value thisArg,JSVM_Value data)5189 static JSVM_Value EnumeratorIndexedPropertyCbInfo2(JSVM_Env env, JSVM_Value thisArg, JSVM_Value data)
5190 {
5191 JSVM_Value testArray = nullptr;
5192 uint32_t arrayLength = 2;
5193 OH_JSVM_CreateArrayWithLength(env, arrayLength, &testArray);
5194 JSVM_Value index1 = nullptr;
5195 uint32_t num1 = 2;
5196 OH_JSVM_CreateUint32(env, num1, &index1);
5197 JSVM_Value index2 = nullptr;
5198 uint32_t num2 = 3;
5199 OH_JSVM_CreateUint32(env, num2, &index2);
5200 OH_JSVM_SetElement(env, testArray, 0, index1);
5201 OH_JSVM_SetElement(env, testArray, 1, index2);
5202 return testArray;
5203 }
5204
IndexHandler9(JSVM_Env env,JSVM_CallbackInfo info)5205 static JSVM_Value IndexHandler9(JSVM_Env env, JSVM_CallbackInfo info)
5206 {
5207 JSVM_CallbackStruct param;
5208 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
5209 JSVM_Value thisVar = nullptr;
5210 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
5211 return thisVar;
5212 };
5213 param.data = nullptr;
5214 propertyCfg.genericIndexedPropertyEnumeratorCallback = EnumeratorIndexedPropertyCbInfo2;
5215 JSVM_Value testWrapClass = nullptr;
5216 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test22", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
5217 &propertyCfg, nullptr, &testWrapClass);
5218 if (res == JSVM_OK) {
5219 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
5220 } else {
5221 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
5222 return nullptr;
5223 }
5224 JSVM_Value instanceValue = nullptr;
5225 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
5226
5227 const char testStr[] = "hello world2";
5228 JSVM_Value setvalueName = nullptr;
5229 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
5230 JSVM_Value jsIndex = nullptr;
5231 uint32_t index = 0;
5232 OH_JSVM_CreateUint32(env, index, &jsIndex);
5233 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
5234 JSVM_Value jsIndex1 = nullptr;
5235 uint32_t index1 = 1;
5236 OH_JSVM_CreateUint32(env, index1, &jsIndex1);
5237 OH_JSVM_SetProperty(env, instanceValue, jsIndex1, setvalueName);
5238 JSVM_Value allPropertyNames = nullptr;
5239 OH_JSVM_GetAllPropertyNames(
5240 env,
5241 instanceValue,
5242 JSVM_KEY_OWN_ONLY,
5243 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_SKIP_SYMBOLS),
5244 JSVM_KEY_NUMBERS_TO_STRINGS,
5245 &allPropertyNames);
5246 uint32_t nameSize = 0;
5247 OH_JSVM_GetArrayLength(env, allPropertyNames, &nameSize);
5248 JSVM_Value propertyName = nullptr;
5249 for (uint32_t i = 0; i < nameSize; ++i) {
5250 OH_JSVM_GetElement(env, allPropertyNames, i, &propertyName);
5251 char str[100];
5252 size_t size;
5253 OH_JSVM_GetValueStringUtf8(env, propertyName, str, strlen(str), &size);
5254 bool isExisted = false;
5255 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
5256 }
5257 propertyCfg.genericIndexedPropertyEnumeratorCallback = nullptr;
5258 return nullptr;
5259 }
5260
testEnumeratorIndexedProperty02(napi_env env1,napi_callback_info info)5261 static napi_value testEnumeratorIndexedProperty02(napi_env env1, napi_callback_info info)
5262 {
5263 JSVM_InitOptions init_options;
5264 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
5265 printf("memset_s failed");
5266 return nullptr;
5267 }
5268 init_options.externalReferences = externals;
5269 if (aa == 0) {
5270 OH_JSVM_Init(&init_options);
5271 aa++;
5272 }
5273 JSVM_VM vm;
5274 JSVM_CreateVMOptions options;
5275 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
5276 printf("memset_s failed");
5277 return nullptr;
5278 }
5279 OH_JSVM_CreateVM(&options, &vm);
5280 JSVM_VMScope vm_scope;
5281 OH_JSVM_OpenVMScope(vm, &vm_scope);
5282 JSVM_Env env;
5283 JSVM_CallbackStruct param[1];
5284 param[0].callback = nullptr;
5285 param[0].callback = IndexHandler9;
5286 JSVM_PropertyDescriptor descriptor[] = {
5287 {"indexHandler9", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
5288 };
5289 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
5290 JSVM_EnvScope envScope;
5291 OH_JSVM_OpenEnvScope(env, &envScope);
5292 JSVM_HandleScope handlescope;
5293 OH_JSVM_OpenHandleScope(env, &handlescope);
5294 OH_JSVM_CloseHandleScope(env, handlescope);
5295 OH_JSVM_CloseEnvScope(env, envScope);
5296 OH_JSVM_DestroyEnv(env);
5297 OH_JSVM_CloseVMScope(vm, vm_scope);
5298 OH_JSVM_DestroyVM(vm);
5299 napi_value result11;
5300 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
5301 return result11;
5302 }
5303
testDefinePropertyHandle(napi_env env,napi_callback_info info)5304 static napi_value testDefinePropertyHandle(napi_env env, napi_callback_info info)
5305 {
5306 size_t argc = 1;
5307 napi_value args[1];
5308 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
5309 NAPI_ASSERT(env, argc > 0, "Wrong number of arguments");
5310
5311 int32_t value;
5312 napi_value result = 0;
5313 NAPI_CALL(env, napi_get_value_int32(env, args[0], &value));
5314 if (value == DIFF_VALUE_ZERO) {
5315 result = testCheckArgs(env, info);
5316 } else if (value == DIFF_VALUE_NOE) {
5317 result = testSetProperty(env, info);
5318 } else if (value == DIFF_VALUE_TWO) {
5319 result = testGetProperty(env, info);
5320 } else if (value == DIFF_VALUE_THREE) {
5321 result = testCallFunction(env, info);
5322 } else if (value == DIFF_VALUE_FOUR) {
5323 result = testSetNamedProperty01(env, info);
5324 } else if (value == DIFF_VALUE_FIVE) {
5325 result = testSetNamedProperty02(env, info);
5326 } else if (value == DIFF_VALUE_SIX) {
5327 result = testSetIndexProperty01(env, info);
5328 } else if (value == DIFF_VALUE_SEVEN) {
5329 result = testSetIndexProperty02(env, info);
5330 } else if (value == DIFF_VALUE_EIGHT) {
5331 result = testGetNamedProperty01(env, info);
5332 } else if (value == DIFF_VALUE_NINE) {
5333 result = testGetNamedProperty02(env, info);
5334 } else if (value == DIFF_VALUE_TEN) {
5335 result = testGetIndexProperty01(env, info);
5336 } else if (value == DIFF_VALUE_ELEVEN) {
5337 result = testGetIndexProperty02(env, info);
5338 } else if (value == DIFF_VALUE_TWELVE) {
5339 result = testDeleterNamedProperty01(env, info);
5340 } else if (value == DIFF_VALUE_THIRTEEN) {
5341 result = testDeleterNamedProperty02(env, info);
5342 } else if (value == DIFF_VALUE_FOURTEEN) {
5343 result = testDeleterNamedProperty03(env, info);
5344 } else if (value == DIFF_VALUE_FITEEN) {
5345 result = testDeleterIndexedProperty01(env, info);
5346 } else if (value == DIFF_VALUE_SIXTEEN) {
5347 result = testDeleterIndexedProperty02(env, info);
5348 } else if (value == DIFF_VALUE_SEVENTEEN) {
5349 result = testDeleterIndexedProperty03(env, info);
5350 } else if (value == DIFF_VALUE_EIGHTEEN) {
5351 result = testEnumeratorNamedProperty01(env, info);
5352 } else if (value == DIFF_VALUE_NINETEEN) {
5353 result = testEnumeratorNamedProperty02(env, info);
5354 } else if (value == DIFF_VALUE_TWENTY) {
5355 result = testEnumeratorIndexedProperty01(env, info);
5356 } else if (value == DIFF_VALUE_TWENTYONE) {
5357 result = testEnumeratorIndexedProperty02(env, info);
5358 }
5359 return result;
5360 }
5361
CreateStringUtf8(JSVM_Env env,JSVM_CallbackInfo info)5362 static JSVM_Value CreateStringUtf8(JSVM_Env env, JSVM_CallbackInfo info)
5363 {
5364 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
5365 const char *str = u8"你好, World!, successes to create UTF-8 string!";
5366 JSVM_Value result;
5367 JSVM_Status status = OH_JSVM_CreateStringUtf8(env, str, strlen(str), &result);
5368 if (status != JSVM_OK) {
5369 OH_JSVM_ThrowError(env, nullptr, "Failed to create UTF-8 string");
5370 }
5371 return result;
5372 }
5373
GetValueStringUtf8(JSVM_Env env,JSVM_CallbackInfo info)5374 static JSVM_Value GetValueStringUtf8(JSVM_Env env, JSVM_CallbackInfo info)
5375 {
5376 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
5377 size_t argc = 1;
5378 JSVM_Value args[1] = {nullptr};
5379 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5380 size_t length = 0;
5381 OH_JSVM_GetValueStringUtf8(env, args[0], nullptr, 0, &length);
5382 if (length <= 0) {
5383 return nullptr;
5384 }
5385 char *buf = (char *)malloc(length + 1);
5386 if (buf == nullptr) {
5387 printf("Memory allocation failed!\n");
5388 return nullptr;
5389 }
5390 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, args[0], buf, length + 1, &length);
5391 if (status != JSVM_OK) {
5392 return nullptr;
5393 }
5394 JSVM_Value result;
5395 OH_JSVM_CreateStringUtf8(env, buf, length, &result);
5396 return result;
5397 }
5398
CreateStringUtf16(JSVM_Env env,JSVM_CallbackInfo info)5399 static JSVM_Value CreateStringUtf16(JSVM_Env env, JSVM_CallbackInfo info)
5400 {
5401 strcpy_s(g_dataType, sizeof(g_dataType), "utf16");
5402 const char16_t *str = u"你好, World!, successes to create UTF-16 string!";
5403 size_t length = 0;
5404 while (str[length] != u'\0') {
5405 ++length;
5406 }
5407 JSVM_Value result;
5408 JSVM_Status status = OH_JSVM_CreateStringUtf16(env, str, length, &result);
5409 if (status != JSVM_OK) {
5410 OH_JSVM_ThrowError(env, nullptr, "Failed to create UTF-16 string");
5411 }
5412 return result;
5413 }
5414
5415 static const int MAX_BUFFER_SIZE = 128;
GetValueStringUtf16(JSVM_Env env,JSVM_CallbackInfo info)5416 static JSVM_Value GetValueStringUtf16(JSVM_Env env, JSVM_CallbackInfo info)
5417 {
5418 strcpy_s(g_dataType, sizeof(g_dataType), "utf16");
5419 size_t argc = 1;
5420 JSVM_Value args[1] = {nullptr};
5421 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5422 JSVM_Value result;
5423 size_t length;
5424 char16_t buffer[MAX_BUFFER_SIZE];
5425
5426 size_t bufferSize = MAX_BUFFER_SIZE;
5427 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, args[0], buffer, bufferSize, &length);
5428 if (status != JSVM_OK) {
5429 return nullptr;
5430 }
5431
5432 OH_JSVM_CreateStringUtf16(env, buffer, length, &result);
5433 return result;
5434 }
5435
CreateStringLatin1(JSVM_Env env,JSVM_CallbackInfo info)5436 static JSVM_Value CreateStringLatin1(JSVM_Env env, JSVM_CallbackInfo info)
5437 {
5438 strcpy_s(g_dataType, sizeof(g_dataType), "latin");
5439 const char *str = "Hello, World! éçñ, successes to create Latin1 string! 111";
5440 JSVM_Value result = nullptr;
5441 JSVM_Status status = OH_JSVM_CreateStringLatin1(env, str, JSVM_AUTO_LENGTH, &result);
5442 if (status != JSVM_OK) {
5443 OH_JSVM_ThrowError(env, nullptr, "Failed to create Latin1 string");
5444 }
5445 return result;
5446 }
5447
GetValueStringLatin1(JSVM_Env env,JSVM_CallbackInfo info)5448 static JSVM_Value GetValueStringLatin1(JSVM_Env env, JSVM_CallbackInfo info)
5449 {
5450 strcpy_s(g_dataType, sizeof(g_dataType), "latin");
5451 size_t argc = 1;
5452 JSVM_Value args[1] = {nullptr};
5453 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5454 char buf[MAX_BUFFER_SIZE];
5455 size_t length = 0;
5456 JSVM_Value jsvmRes;
5457 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, args[0], buf, MAX_BUFFER_SIZE, &length);
5458 if (status != JSVM_OK) {
5459 return nullptr;
5460 }
5461 OH_JSVM_CreateStringLatin1(env, buf, length, &jsvmRes);
5462 return jsvmRes;
5463 }
5464
GetResultType(JSVM_Env env,JSVM_Value result)5465 void GetResultType(JSVM_Env env, JSVM_Value result)
5466 {
5467 JSVM_ValueType valueType;
5468 OH_JSVM_Typeof(env, result, &valueType);
5469 switch (valueType) {
5470 case JSVM_NUMBER: {
5471 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5472 break;
5473 }
5474 case JSVM_STRING: {
5475 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
5476 break;
5477 }
5478 case JSVM_OBJECT: {
5479 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
5480 break;
5481 }
5482 case JSVM_BIGINT: {
5483 strcpy_s(g_dataType, sizeof(g_dataType), "BigintUint64");
5484 break;
5485 }
5486 default: {
5487 break;
5488 }
5489 }
5490 }
5491
CreateDate(JSVM_Env env,JSVM_CallbackInfo info)5492 static JSVM_Value CreateDate(JSVM_Env env, JSVM_CallbackInfo info)
5493 {
5494 strcpy_s(g_dataType, sizeof(g_dataType), "date");
5495 JSVM_Value returnValue = nullptr;
5496 OH_JSVM_CreateDate(env, DIFF_VALUE_DATE, &returnValue);
5497 return returnValue;
5498 }
5499
CreateObject(JSVM_Env env,JSVM_CallbackInfo info)5500 static JSVM_Value CreateObject(JSVM_Env env, JSVM_CallbackInfo info)
5501 {
5502 strcpy_s(g_dataType, sizeof(g_dataType), "object");
5503 JSVM_Value object;
5504 OH_JSVM_CreateObject(env, &object);
5505 JSVM_Value name;
5506 OH_JSVM_CreateStringUtf8(env, "name", JSVM_AUTO_LENGTH, &name);
5507 JSVM_Value value;
5508 OH_JSVM_CreateStringUtf8(env, "Hello OH_JSVM_CreateObject!", NAPI_AUTO_LENGTH, &value);
5509 OH_JSVM_SetProperty(env, object, name, value);
5510 return object;
5511 }
5512
CreateExternal(JSVM_Env env,JSVM_CallbackInfo info)5513 static JSVM_Value CreateExternal(JSVM_Env env, JSVM_CallbackInfo info)
5514 {
5515 void *data = malloc(DIFF_VALUE_TEN);
5516 if (data == nullptr) {
5517 printf("Memory allocation failed!\n");
5518 return nullptr;
5519 }
5520 if (memset_s(data, DIFF_VALUE_TEN, 0, DIFF_VALUE_TEN) != EOK) {
5521 printf("memset_s failed");
5522 return nullptr;
5523 }
5524 const char testStr[] = "test";
5525 JSVM_Value external = nullptr;
5526 JSVM_Status status = OH_JSVM_CreateExternal(
5527 env, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, (void *)testStr, &external);
5528 JSVM_Value returnValue;
5529 bool type = false;
5530 if (status != JSVM_OK) {
5531 return nullptr;
5532 } else {
5533 type = true;
5534 }
5535 OH_JSVM_GetBoolean(env, type, &returnValue);
5536 return returnValue;
5537 }
5538
GetValueExternal(JSVM_Env env,JSVM_CallbackInfo info)5539 static JSVM_Value GetValueExternal(JSVM_Env env, JSVM_CallbackInfo info)
5540 {
5541 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5542 void *data = reinterpret_cast<void *>(0x12345);
5543 JSVM_Value externalValue;
5544 JSVM_Status status = OH_JSVM_CreateExternal(env, data, nullptr, nullptr, &externalValue);
5545 if (status != JSVM_OK) {
5546 return nullptr;
5547 }
5548 void *dataValue;
5549 status = OH_JSVM_GetValueExternal(env, externalValue, &dataValue);
5550 if (status != JSVM_OK) {
5551 return nullptr;
5552 }
5553 JSVM_Value returnValue;
5554 OH_JSVM_CreateInt32(env, 0, &returnValue);
5555 return returnValue;
5556 }
5557
CreateArray(JSVM_Env env,JSVM_CallbackInfo info)5558 static JSVM_Value CreateArray(JSVM_Env env, JSVM_CallbackInfo info)
5559 {
5560 strcpy_s(g_dataType, sizeof(g_dataType), "array");
5561 JSVM_Value array = nullptr;
5562 OH_JSVM_CreateArray(env, &array);
5563 for (int i = 0; i < DIFF_VALUE_FIVE; i++) {
5564 JSVM_Value element;
5565 OH_JSVM_CreateInt32(env, i, &element);
5566 OH_JSVM_SetElement(env, array, i, element);
5567 }
5568 return array;
5569 }
5570
CreateArrayWithLength(JSVM_Env env,JSVM_CallbackInfo info)5571 static JSVM_Value CreateArrayWithLength(JSVM_Env env, JSVM_CallbackInfo info)
5572 {
5573 strcpy_s(g_dataType, sizeof(g_dataType), "array");
5574 size_t argc = 1;
5575 JSVM_Value argv[1] = {nullptr};
5576 JSVM_Value result = nullptr;
5577 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
5578 int32_t length = 0;
5579 OH_JSVM_GetValueInt32(env, argv[0], &length);
5580 JSVM_Status status = OH_JSVM_CreateArrayWithLength(env, length, &result);
5581 if (status == JSVM_OK) {
5582 for (int32_t i = 0; i < length; i++) {
5583 JSVM_Value value;
5584 OH_JSVM_CreateInt32(env, i, &value);
5585 OH_JSVM_SetElement(env, result, i, value);
5586 }
5587 }
5588 return result;
5589 }
5590
CreateArraybuffer(JSVM_Env env,JSVM_CallbackInfo info)5591 static JSVM_Value CreateArraybuffer(JSVM_Env env, JSVM_CallbackInfo info)
5592 {
5593 strcpy_s(g_dataType, sizeof(g_dataType), "arraybuffer");
5594 size_t argc = 1;
5595 JSVM_Value argv[1] = {nullptr};
5596 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
5597 int32_t value;
5598 size_t length;
5599 OH_JSVM_GetValueInt32(env, argv[0], &value);
5600 length = size_t(value);
5601 void *data;
5602 JSVM_Value result = nullptr;
5603 OH_JSVM_CreateArraybuffer(env, length, &data, &result);
5604 return result;
5605 }
5606
GetArrayLength(JSVM_Env env,JSVM_CallbackInfo info)5607 static JSVM_Value GetArrayLength(JSVM_Env env, JSVM_CallbackInfo info)
5608 {
5609 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5610 size_t argc = 1;
5611 JSVM_Value args[1] = {nullptr};
5612 uint32_t length;
5613 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5614
5615 bool isArray = false;
5616 OH_JSVM_IsArray(env, args[0], &isArray);
5617 if (!isArray) {
5618 return nullptr;
5619 }
5620 OH_JSVM_GetArrayLength(env, args[0], &length);
5621 JSVM_Value result = nullptr;
5622 OH_JSVM_CreateInt32(env, length, &result);
5623 return result;
5624 }
5625
CreateInt32(JSVM_Env env,JSVM_CallbackInfo info)5626 static JSVM_Value CreateInt32(JSVM_Env env, JSVM_CallbackInfo info)
5627 {
5628 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5629 JSVM_Value result;
5630 JSVM_Status status = OH_JSVM_CreateInt32(env, DIFF_VALUE_INT32, &result);
5631 if (status != JSVM_OK) {
5632 return nullptr;
5633 }
5634 return result;
5635 }
5636
GetValueInt32(JSVM_Env env,JSVM_CallbackInfo info)5637 static JSVM_Value GetValueInt32(JSVM_Env env, JSVM_CallbackInfo info)
5638 {
5639 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5640 size_t argc = 1;
5641 JSVM_Value args[1] = {nullptr};
5642 int32_t result;
5643 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5644 JSVM_Status status = OH_JSVM_GetValueInt32(env, args[0], &result);
5645 if (status != JSVM_OK) {
5646 return nullptr;
5647 }
5648 JSVM_Value jsvmResult;
5649 status = OH_JSVM_CreateInt32(env, result, &jsvmResult);
5650 if (status != JSVM_OK) {
5651 return nullptr;
5652 }
5653 return jsvmResult;
5654 }
5655
CreateUInt32(JSVM_Env env,JSVM_CallbackInfo info)5656 static JSVM_Value CreateUInt32(JSVM_Env env, JSVM_CallbackInfo info)
5657 {
5658 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5659 JSVM_Value result;
5660 JSVM_Status status = OH_JSVM_CreateUint32(env, DIFF_VALUE_UINT, &result);
5661 if (status != JSVM_OK) {
5662 return nullptr;
5663 }
5664 return result;
5665 }
5666
GetValueUint32(JSVM_Env env,JSVM_CallbackInfo info)5667 static JSVM_Value GetValueUint32(JSVM_Env env, JSVM_CallbackInfo info)
5668 {
5669 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5670 size_t argc = 1;
5671 JSVM_Value argv[1] = {nullptr};
5672 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
5673
5674 uint32_t number;
5675 JSVM_Status status = OH_JSVM_GetValueUint32(env, argv[0], &number);
5676 JSVM_Value result = nullptr;
5677 OH_JSVM_CreateUint32(env, number, &result);
5678 if (status != JSVM_OK) {
5679 return nullptr;
5680 }
5681 return result;
5682 }
5683
CreateInt64(JSVM_Env env,JSVM_CallbackInfo info)5684 static JSVM_Value CreateInt64(JSVM_Env env, JSVM_CallbackInfo info)
5685 {
5686 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5687 JSVM_Value result;
5688 JSVM_Status status = OH_JSVM_CreateInt64(env, DIFF_VALUE_INT, &result);
5689 if (status != JSVM_OK) {
5690 return nullptr;
5691 }
5692 return result;
5693 }
5694
GetValueInt64(JSVM_Env env,JSVM_CallbackInfo info)5695 static JSVM_Value GetValueInt64(JSVM_Env env, JSVM_CallbackInfo info)
5696 {
5697 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5698 size_t argc = 1;
5699 JSVM_Value args[1] = {nullptr};
5700 int64_t result;
5701 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5702 JSVM_Status status = OH_JSVM_GetValueInt64(env, args[0], &result);
5703 JSVM_Value jsvmResult;
5704 OH_JSVM_CreateInt64(env, result, &jsvmResult);
5705 if (status != JSVM_OK) {
5706 return nullptr;
5707 }
5708 return jsvmResult;
5709 }
5710
CreateDouble(JSVM_Env env,JSVM_CallbackInfo info)5711 static JSVM_Value CreateDouble(JSVM_Env env, JSVM_CallbackInfo info)
5712 {
5713 strcpy_s(g_dataType, sizeof(g_dataType), "double");
5714 JSVM_Value result;
5715 JSVM_Status status = OH_JSVM_CreateDouble(env, DIFF_VALUE_DOUBLE, &result);
5716 if (status != JSVM_OK) {
5717 return nullptr;
5718 }
5719 return result;
5720 }
5721
GetDouble(JSVM_Env env,JSVM_CallbackInfo info)5722 static JSVM_Value GetDouble(JSVM_Env env, JSVM_CallbackInfo info)
5723 {
5724 strcpy_s(g_dataType, sizeof(g_dataType), "double");
5725 size_t argc = 1;
5726 JSVM_Value args[1] = {nullptr};
5727 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5728
5729 double value;
5730 JSVM_Status status = OH_JSVM_GetValueDouble(env, args[0], &value);
5731 if (status != JSVM_OK) {
5732 return nullptr;
5733 }
5734 JSVM_Value result;
5735 OH_JSVM_CreateDouble(env, value, &result);
5736 return result;
5737 }
5738
GetBoolean(JSVM_Env env,JSVM_CallbackInfo info)5739 static JSVM_Value GetBoolean(JSVM_Env env, JSVM_CallbackInfo info)
5740 {
5741 size_t argc = 2;
5742 JSVM_Value argv[2] = {nullptr};
5743 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
5744 int32_t paramData = 0;
5745 OH_JSVM_GetValueInt32(env, argv[0], ¶mData);
5746 int32_t paramValue = 0;
5747 OH_JSVM_GetValueInt32(env, argv[1], ¶mValue);
5748 JSVM_Value returnValue;
5749 bool type = false;
5750 if (paramData == paramValue) {
5751 type = true;
5752 }
5753 OH_JSVM_GetBoolean(env, type, &returnValue);
5754 return returnValue;
5755 }
5756
GetValueBool(JSVM_Env env,JSVM_CallbackInfo info)5757 static JSVM_Value GetValueBool(JSVM_Env env, JSVM_CallbackInfo info)
5758 {
5759 size_t argc = 1;
5760 JSVM_Value args[1] = {nullptr};
5761 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5762 bool boolC;
5763 JSVM_Status status = OH_JSVM_GetValueBool(env, args[0], &boolC);
5764 if (status == JSVM_BOOLEAN_EXPECTED) {
5765 return nullptr;
5766 }
5767 JSVM_Value bool_napi;
5768 OH_JSVM_GetBoolean(env, boolC, &bool_napi);
5769 return bool_napi;
5770 }
5771
CreateBigintUint64(JSVM_Env env,JSVM_CallbackInfo info)5772 static JSVM_Value CreateBigintUint64(JSVM_Env env, JSVM_CallbackInfo info)
5773 {
5774 strcpy_s(g_dataType, sizeof(g_dataType), "BigintUint64");
5775 JSVM_Value returnValue;
5776 OH_JSVM_CreateBigintUint64(env, DIFF_VALUE_BIGUINT, &returnValue);
5777 return returnValue;
5778 }
5779
GetValueBigintUint64(JSVM_Env env,JSVM_CallbackInfo info)5780 static JSVM_Value GetValueBigintUint64(JSVM_Env env, JSVM_CallbackInfo info)
5781 {
5782 strcpy_s(g_dataType, sizeof(g_dataType), "BigintUint64");
5783 size_t argc = 1;
5784 JSVM_Value args[1] = {nullptr};
5785 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5786 uint64_t value = 0;
5787 bool lossLess = false;
5788 OH_JSVM_GetValueBigintUint64(env, args[0], &value, &lossLess);
5789 if (!lossLess) {
5790 OH_JSVM_ThrowError(env, nullptr, "BigInt values have no lossless converted");
5791 }
5792 JSVM_Value returnValue = nullptr;
5793 OH_JSVM_CreateBigintUint64(env, value, &returnValue);
5794 return returnValue;
5795 }
5796
CreateBigintInt64(JSVM_Env env,JSVM_CallbackInfo info)5797 static JSVM_Value CreateBigintInt64(JSVM_Env env, JSVM_CallbackInfo info)
5798 {
5799 strcpy_s(g_dataType, sizeof(g_dataType), "BigintInt64");
5800 JSVM_Value returnValue = nullptr;
5801 OH_JSVM_CreateBigintInt64(env, DIFF_VALUE_BIGINT, &returnValue);
5802 return returnValue;
5803 }
5804
GetBigintInt64(JSVM_Env env,JSVM_CallbackInfo info)5805 static JSVM_Value GetBigintInt64(JSVM_Env env, JSVM_CallbackInfo info)
5806 {
5807 strcpy_s(g_dataType, sizeof(g_dataType), "BigintInt64");
5808 size_t argc = 1;
5809 JSVM_Value args[1] = {nullptr};
5810 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5811 int64_t value = 0;
5812 bool lossLess = false;
5813 OH_JSVM_GetValueBigintInt64(env, args[0], &value, &lossLess);
5814 if (!lossLess) {
5815 OH_JSVM_ThrowError(env, nullptr, "BigInt values have not been lossless converted");
5816 }
5817 JSVM_Value returnValue = nullptr;
5818 OH_JSVM_CreateBigintInt64(env, value, &returnValue);
5819 return returnValue;
5820 }
5821
CreateBigintWords(JSVM_Env env,JSVM_CallbackInfo info)5822 static JSVM_Value CreateBigintWords(JSVM_Env env, JSVM_CallbackInfo info)
5823 {
5824 strcpy_s(g_dataType, sizeof(g_dataType), "BigintWords");
5825 int signBit = 0;
5826 size_t wordCount = DIFF_VALUE_THREE;
5827 uint64_t words[] = {12ULL, 34ULL, 56ULL};
5828 JSVM_Value returnValue = nullptr;
5829 JSVM_Status status = OH_JSVM_CreateBigintWords(env, signBit, wordCount, words, &returnValue);
5830 if (status != JSVM_OK) {
5831 OH_JSVM_ThrowError(env, nullptr, "OH_JSVM_CreateBigintWords fail");
5832 }
5833 return returnValue;
5834 }
5835
GetValueBigintWords(JSVM_Env env,JSVM_CallbackInfo info)5836 static JSVM_Value GetValueBigintWords(JSVM_Env env, JSVM_CallbackInfo info)
5837 {
5838 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5839 size_t argc = 1;
5840 JSVM_Value args[1] = {nullptr};
5841 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5842
5843 int signBit = 0;
5844 size_t wordCount = 0;
5845 uint64_t words;
5846
5847 OH_JSVM_GetValueBigintWords(env, args[0], nullptr, &wordCount, nullptr);
5848 JSVM_Status status = OH_JSVM_GetValueBigintWords(env, args[0], &signBit, &wordCount, &words);
5849 if (status != JSVM_OK) {
5850 return nullptr;
5851 }
5852
5853 JSVM_Value returnValue = nullptr;
5854 OH_JSVM_CreateInt32(env, signBit, &returnValue);
5855 return returnValue;
5856 }
5857
GetNull(JSVM_Env env,JSVM_CallbackInfo info)5858 static JSVM_Value GetNull(JSVM_Env env, JSVM_CallbackInfo info)
5859 {
5860 JSVM_Value nullValue;
5861 OH_JSVM_GetNull(env, &nullValue);
5862 return nullValue;
5863 }
5864
GetUndefined(JSVM_Env env,JSVM_CallbackInfo info)5865 static JSVM_Value GetUndefined(JSVM_Env env, JSVM_CallbackInfo info)
5866 {
5867 size_t argc = 1;
5868 JSVM_Value args[1] = {nullptr};
5869 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5870 JSVM_Value value = nullptr;
5871 OH_JSVM_GetUndefined(env, &value);
5872 return value;
5873 }
5874
GetGlobal(JSVM_Env env,JSVM_CallbackInfo info)5875 static JSVM_Value GetGlobal(JSVM_Env env, JSVM_CallbackInfo info)
5876 {
5877 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
5878 JSVM_Value value = nullptr;
5879 JSVM_Value global = nullptr;
5880 OH_JSVM_CreateInt32(env, DIFF_VALUE_NOE, &value);
5881 OH_JSVM_GetGlobal(env, &global);
5882 OH_JSVM_SetNamedProperty(env, global, "Row", value);
5883 return global;
5884 }
5885
CreateSymbol(JSVM_Env env,JSVM_CallbackInfo info)5886 static JSVM_Value CreateSymbol(JSVM_Env env, JSVM_CallbackInfo info)
5887 {
5888 JSVM_Value result;
5889 const char *des = "only";
5890 OH_JSVM_CreateStringUtf8(env, des, JSVM_AUTO_LENGTH, &result);
5891 JSVM_Value returnSymbol;
5892 OH_JSVM_CreateSymbol(env, result, &returnSymbol);
5893 JSVM_Value returnValue;
5894 bool type = false;
5895 JSVM_ValueType valuetypeSymbol;
5896 OH_JSVM_Typeof(env, returnSymbol, &valuetypeSymbol);
5897 if (valuetypeSymbol == JSVM_SYMBOL) {
5898 type = true;
5899 }
5900 OH_JSVM_GetBoolean(env, type, &returnValue);
5901 return returnValue;
5902 }
5903
SymbolFor(JSVM_Env env,JSVM_CallbackInfo info)5904 static JSVM_Value SymbolFor(JSVM_Env env, JSVM_CallbackInfo info)
5905 {
5906 JSVM_Value description;
5907 OH_JSVM_CreateStringUtf8(env, "test_demo", DIFF_VALUE_NINE, &description);
5908 char buffer[MAX_BUFFER_SIZE];
5909 size_t copied = 0;
5910 OH_JSVM_GetValueStringUtf8(env, description, buffer, MAX_BUFFER_SIZE, &copied);
5911 JSVM_Value symbol;
5912 OH_JSVM_CreateSymbol(env, description, &symbol);
5913 JSVM_Value result_symbol;
5914 JSVM_Status status = OH_JSVM_SymbolFor(env, nullptr, 0, &result_symbol);
5915 JSVM_Value returnValue;
5916 bool type = false;
5917 JSVM_ValueType valuetypeSymbol;
5918 OH_JSVM_Typeof(env, result_symbol, &valuetypeSymbol);
5919 if (valuetypeSymbol == JSVM_SYMBOL && status == JSVM_OK) {
5920 type = true;
5921 }
5922 OH_JSVM_GetBoolean(env, type, &returnValue);
5923 return returnValue;
5924 }
5925
GetDateValue(JSVM_Env env,JSVM_CallbackInfo info)5926 static JSVM_Value GetDateValue(JSVM_Env env, JSVM_CallbackInfo info)
5927 {
5928 strcpy_s(g_dataType, sizeof(g_dataType), "double");
5929 size_t argc = 1;
5930 JSVM_Value args[1] = {nullptr};
5931 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5932 double value;
5933 JSVM_Status status = OH_JSVM_GetDateValue(env, args[0], &value);
5934 if (status != JSVM_OK) {
5935 OH_JSVM_ThrowError(env, nullptr, "napi_get_date_value fail");
5936 }
5937 JSVM_Value returnValue = nullptr;
5938 OH_JSVM_CreateDouble(env, value, &returnValue);
5939 return returnValue;
5940 }
5941
IsDate(JSVM_Env env,JSVM_CallbackInfo info)5942 static JSVM_Value IsDate(JSVM_Env env, JSVM_CallbackInfo info)
5943 {
5944 size_t argc = 1;
5945 JSVM_Value args[1] = {nullptr};
5946 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5947
5948 bool result = false;
5949 OH_JSVM_IsDate(env, args[0], &result);
5950
5951 JSVM_Value isDate = nullptr;
5952 OH_JSVM_GetBoolean(env, result, &isDate);
5953 return isDate;
5954 }
5955
5956
GetArraybufferInfo(JSVM_Env env,JSVM_CallbackInfo info)5957 static JSVM_Value GetArraybufferInfo(JSVM_Env env, JSVM_CallbackInfo info)
5958 {
5959 size_t argc = 2;
5960 JSVM_Value args[2] = {nullptr};
5961 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5962 int32_t infoTypeParam;
5963 OH_JSVM_GetValueInt32(env, args[1], &infoTypeParam);
5964 bool isArrayBuffer = false;
5965 OH_JSVM_IsArraybuffer(env, args[0], &isArrayBuffer);
5966 if (!isArrayBuffer) {
5967 return nullptr;
5968 }
5969 enum InfoType {
5970 ARRAY_LENGTH,
5971 ARRAY_BUFFER
5972 };
5973 void *data;
5974 size_t byteLength;
5975 JSVM_Status status = OH_JSVM_GetArraybufferInfo(env, args[0], &data, &byteLength);
5976 if (status != JSVM_OK) {
5977 return nullptr;
5978 }
5979 JSVM_Value result = nullptr;
5980 switch (infoTypeParam) {
5981 case ARRAY_LENGTH:
5982 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5983 JSVM_Value jsvmLength;
5984 OH_JSVM_CreateInt32(env, byteLength, &jsvmLength);
5985 result = jsvmLength;
5986 break;
5987 case ARRAY_BUFFER:
5988 strcpy_s(g_dataType, sizeof(g_dataType), "arraybuffer");
5989 JSVM_Value bufferData;
5990 OH_JSVM_CreateArraybuffer(env, byteLength, &data, &bufferData);
5991 result = bufferData;
5992 break;
5993 default:
5994 break;
5995 }
5996 return result;
5997 }
5998
CreateTypedArray(JSVM_Env env,JSVM_CallbackInfo info)5999 static JSVM_Value CreateTypedArray(JSVM_Env env, JSVM_CallbackInfo info)
6000 {
6001 strcpy_s(g_dataType, sizeof(g_dataType), "typearray");
6002 size_t argc = 1;
6003 JSVM_Value args[1] = {nullptr};
6004 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6005 int32_t typeNum;
6006 OH_JSVM_GetValueInt32(env, args[0], &typeNum);
6007 JSVM_TypedarrayType arrayType;
6008 size_t elementSize = 0;
6009 arrayType = static_cast<JSVM_TypedarrayType>(typeNum);
6010 switch (typeNum) {
6011 case JSVM_INT8_ARRAY:
6012 case JSVM_UINT8_ARRAY:
6013 case JSVM_UINT8_CLAMPED_ARRAY:
6014 elementSize = sizeof(int8_t);
6015 break;
6016 case JSVM_INT16_ARRAY:
6017 case JSVM_UINT16_ARRAY:
6018 elementSize = sizeof(int16_t);
6019 break;
6020 case JSVM_INT32_ARRAY:
6021 case JSVM_UINT32_ARRAY:
6022 elementSize = sizeof(int32_t);
6023 break;
6024 case JSVM_FLOAT32_ARRAY:
6025 elementSize = sizeof(float);
6026 break;
6027 case JSVM_FLOAT64_ARRAY:
6028 elementSize = sizeof(double);
6029 break;
6030 case JSVM_BIGINT64_ARRAY:
6031 case JSVM_BIGUINT64_ARRAY:
6032 elementSize = sizeof(int64_t);
6033 break;
6034 default:
6035 arrayType = JSVM_INT8_ARRAY;
6036 elementSize = sizeof(int8_t);
6037 break;
6038 }
6039 size_t length = DIFF_VALUE_THREE;
6040 JSVM_Value arrayBuffer = nullptr;
6041 JSVM_Value typedArray = nullptr;
6042 void *data;
6043 OH_JSVM_CreateArraybuffer(env, length * elementSize, (void **)&data, &arrayBuffer);
6044 OH_JSVM_CreateTypedarray(env, arrayType, length, arrayBuffer, DIFF_VALUE_ZERO, &typedArray);
6045 return typedArray;
6046 }
6047
6048
GetTypedArrayInfo(JSVM_Env env,JSVM_CallbackInfo info)6049 static JSVM_Value GetTypedArrayInfo(JSVM_Env env, JSVM_CallbackInfo info)
6050 {
6051 size_t argc = 2;
6052 JSVM_Value args[2] = {nullptr};
6053 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6054 int32_t infoTypeParam;
6055 OH_JSVM_GetValueInt32(env, args[1], &infoTypeParam);
6056 enum InfoType {
6057 INFO_TYPE,
6058 INFO_LENGTH,
6059 INFO_ARRAY_BUFFER,
6060 INFO_BYTE_OFFSET
6061 };
6062 void *data;
6063 JSVM_TypedarrayType type;
6064 size_t byteOffset;
6065 size_t length;
6066 JSVM_Value arrayBuffer = nullptr;
6067 JSVM_Status status = OH_JSVM_GetTypedarrayInfo(env, args[0], &type, &length, &data, &arrayBuffer, &byteOffset);
6068
6069 JSVM_Value result = nullptr;
6070 switch (infoTypeParam) {
6071 case INFO_TYPE:
6072 JSVM_Value int8_type;
6073 OH_JSVM_GetBoolean(env, status == JSVM_OK ? type == JSVM_INT8_ARRAY : false, &int8_type);
6074 result = int8_type;
6075 break;
6076 case INFO_LENGTH:
6077 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6078 JSVM_Value jsvmLength;
6079 OH_JSVM_CreateInt32(env, status == JSVM_OK ? length : 0, &jsvmLength);
6080 result = jsvmLength;
6081 break;
6082 case INFO_BYTE_OFFSET:
6083 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6084 JSVM_Value jsvmOffset;
6085 OH_JSVM_CreateInt32(env, status == JSVM_OK ? byteOffset : 0, &jsvmOffset);
6086 result = jsvmOffset;
6087 break;
6088 case INFO_ARRAY_BUFFER:
6089 bool isArrayBuffer;
6090 OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer);
6091 JSVM_Value isArray;
6092 OH_JSVM_GetBoolean(env, status == JSVM_OK ? isArrayBuffer : false, &isArray);
6093 result = isArray;
6094 break;
6095 default:
6096 break;
6097 }
6098 return result;
6099 }
6100
6101
CreateDataView(JSVM_Env env,JSVM_CallbackInfo info)6102 static JSVM_Value CreateDataView(JSVM_Env env, JSVM_CallbackInfo info)
6103 {
6104 size_t argc = 2;
6105 JSVM_Value args[2] = {nullptr};
6106 JSVM_Value arraybuffer = nullptr;
6107 JSVM_Value result = nullptr;
6108 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6109 OH_JSVM_CoerceToObject(env, args[0], &arraybuffer);
6110 OH_JSVM_CreateDataview(env, DIFF_VALUE_TWELVE, arraybuffer, DIFF_VALUE_FOUR, &result);
6111 uint8_t *data = nullptr;
6112 size_t length = 0;
6113 for (size_t i = 0; i < length; i++) {
6114 data[i] = static_cast<uint8_t>(i + 1);
6115 }
6116 int32_t infoType;
6117 OH_JSVM_GetValueInt32(env, args[1], &infoType);
6118 size_t returnLength;
6119 JSVM_Value returnArrayBuffer = nullptr;
6120 size_t returnOffset;
6121 enum InfoType {
6122 BYTE_LENGTHE,
6123 ARRAY_BUFFERE,
6124 BYTE_OFFSET
6125 };
6126 OH_JSVM_GetDataviewInfo(env, result, &returnLength, (void **)&data, &returnArrayBuffer, &returnOffset);
6127 JSVM_Value returnResult = nullptr;
6128 switch (infoType) {
6129 case BYTE_LENGTHE:
6130 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6131 OH_JSVM_CreateInt32(env, returnLength, &returnResult);
6132 break;
6133 case ARRAY_BUFFERE:
6134 bool isArrayBuffer;
6135 OH_JSVM_IsArraybuffer(env, returnArrayBuffer, &isArrayBuffer);
6136 OH_JSVM_GetBoolean(env, isArrayBuffer, &returnResult);
6137 break;
6138 case BYTE_OFFSET:
6139 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6140 OH_JSVM_CreateInt32(env, returnOffset, &returnResult);
6141 break;
6142 default:
6143 break;
6144 }
6145 return returnResult;
6146 }
6147
GetDataViewInfo(JSVM_Env env,JSVM_CallbackInfo info)6148 static JSVM_Value GetDataViewInfo(JSVM_Env env, JSVM_CallbackInfo info)
6149 {
6150 size_t argc = 2;
6151 JSVM_Value args[2] = {nullptr};
6152
6153 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6154 int32_t infoType;
6155 OH_JSVM_GetValueInt32(env, args[1], &infoType);
6156
6157 size_t byteLength;
6158 void *data;
6159 JSVM_Value arrayBuffer = nullptr;
6160 size_t byteOffset;
6161 enum InfoType {
6162 BYTE_LENGTHE,
6163 ARRAY_BUFFERE,
6164 BYTE_OFFSET
6165 };
6166 OH_JSVM_GetDataviewInfo(env, args[0], &byteLength, &data, &arrayBuffer, &byteOffset);
6167 JSVM_Value result = nullptr;
6168 switch (infoType) {
6169 case BYTE_LENGTHE: {
6170 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6171 JSVM_Value len;
6172 OH_JSVM_CreateInt32(env, byteLength, &len);
6173 result = len;
6174 break;
6175 }
6176 case ARRAY_BUFFERE: {
6177 bool isArrayBuffer = false;
6178 OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer);
6179 JSVM_Value isArray;
6180 OH_JSVM_GetBoolean(env, isArrayBuffer, &isArray);
6181 result = isArray;
6182 break;
6183 }
6184 case BYTE_OFFSET: {
6185 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6186 JSVM_Value offset;
6187 OH_JSVM_CreateInt32(env, byteOffset, &offset);
6188 result = offset;
6189 break;
6190 }
6191 default:
6192 break;
6193 }
6194 return result;
6195 }
6196
GetPrototype(JSVM_Env env,JSVM_CallbackInfo info)6197 static JSVM_Value GetPrototype(JSVM_Env env, JSVM_CallbackInfo info)
6198 {
6199 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6200 JSVM_Value obj = nullptr;
6201 OH_JSVM_CreateObject(env, &obj);
6202 const char *testNameStr = "set and get proto";
6203 JSVM_Value propValue = nullptr;
6204 JSVM_Value key;
6205 OH_JSVM_CreateStringUtf8(env, "name", JSVM_AUTO_LENGTH, &key);
6206 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue);
6207 OH_JSVM_SetProperty(env, obj, key, propValue);
6208
6209 JSVM_Value propResult = nullptr;
6210 JSVM_Status status = OH_JSVM_GetProperty(env, obj, key, &propResult);
6211 if (status != JSVM_OK) {
6212 return nullptr;
6213 }
6214 return propResult;
6215 }
6216
CreateReference(JSVM_Env env,JSVM_CallbackInfo info)6217 static JSVM_Value CreateReference(JSVM_Env env, JSVM_CallbackInfo info)
6218 {
6219 JSVM_Ref gRef = nullptr;
6220 strcpy_s(g_dataType, sizeof(g_dataType), "object");
6221 JSVM_Value obj = nullptr;
6222 OH_JSVM_CreateObject(env, &obj);
6223 JSVM_Value value = nullptr;
6224 OH_JSVM_CreateStringUtf8(env, "CreateReference", JSVM_AUTO_LENGTH, &value);
6225 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6226 JSVM_Status status = OH_JSVM_CreateReference(env, obj, 1, &gRef);
6227 if (status != JSVM_OK) {
6228 return nullptr;
6229 }
6230 JSVM_Finalize jSVM_Finalize = nullptr;
6231 OH_JSVM_AddFinalizer(env, obj, nullptr, jSVM_Finalize, nullptr, &gRef);
6232 uint32_t result;
6233 OH_JSVM_ReferenceRef(env, gRef, &result);
6234 if (result != DIFF_VALUE_TWO) {
6235 return nullptr;
6236 }
6237 return obj;
6238 }
6239
UseReference(JSVM_Env env,JSVM_CallbackInfo info)6240 static JSVM_Value UseReference(JSVM_Env env, JSVM_CallbackInfo info)
6241 {
6242 JSVM_Ref gRef = nullptr;
6243 strcpy_s(g_dataType, sizeof(g_dataType), "object");
6244 JSVM_Value obj = nullptr;
6245 OH_JSVM_CreateObject(env, &obj);
6246 JSVM_Value value = nullptr;
6247 OH_JSVM_CreateStringUtf8(env, "UseReference", JSVM_AUTO_LENGTH, &value);
6248 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6249 JSVM_Status status = OH_JSVM_CreateReference(env, obj, 1, &gRef);
6250 if (status != JSVM_OK) {
6251 return nullptr;
6252 }
6253 JSVM_Finalize jSVM_Finalize = nullptr;
6254 OH_JSVM_AddFinalizer(env, obj, nullptr, jSVM_Finalize, nullptr, &gRef);
6255 uint32_t result;
6256 OH_JSVM_ReferenceRef(env, gRef, &result);
6257 if (result != DIFF_VALUE_TWO) {
6258 return nullptr;
6259 }
6260 JSVM_Value object = nullptr;
6261 status = OH_JSVM_GetReferenceValue(env, gRef, &object);
6262 if (status != JSVM_OK) {
6263 return nullptr;
6264 }
6265 return object;
6266 }
6267
DeleteReference(JSVM_Env env,JSVM_CallbackInfo info)6268 static JSVM_Value DeleteReference(JSVM_Env env, JSVM_CallbackInfo info)
6269 {
6270 JSVM_Ref gRef = nullptr;
6271 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6272 JSVM_Value obj = nullptr;
6273 OH_JSVM_CreateObject(env, &obj);
6274 JSVM_Value value = nullptr;
6275 OH_JSVM_CreateStringUtf8(env, "DeleteReference", JSVM_AUTO_LENGTH, &value);
6276 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6277 JSVM_Status status = OH_JSVM_CreateReference(env, obj, 1, &gRef);
6278 if (status != JSVM_OK) {
6279 return nullptr;
6280 }
6281 JSVM_Finalize jSVM_Finalize = nullptr;
6282 OH_JSVM_AddFinalizer(env, obj, nullptr, jSVM_Finalize, nullptr, &gRef);
6283 uint32_t result;
6284 OH_JSVM_ReferenceRef(env, gRef, &result);
6285 if (result != DIFF_VALUE_TWO) {
6286 return nullptr;
6287 }
6288 uint32_t num;
6289 OH_JSVM_ReferenceUnref(env, gRef, &num);
6290 if (num != 1) {
6291 return nullptr;
6292 }
6293 status = OH_JSVM_DeleteReference(env, gRef);
6294 if (status != JSVM_OK) {
6295 return nullptr;
6296 }
6297 JSVM_Value returnResult = nullptr;
6298 OH_JSVM_CreateStringUtf8(env, "OH_JSVM_DeleteReference success", JSVM_AUTO_LENGTH, &returnResult);
6299 return returnResult;
6300 }
6301
EscapableHandleScopeTest(JSVM_Env env,JSVM_CallbackInfo info)6302 static JSVM_Value EscapableHandleScopeTest(JSVM_Env env, JSVM_CallbackInfo info)
6303 {
6304 strcpy_s(g_dataType, sizeof(g_dataType), "object");
6305 JSVM_EscapableHandleScope scope = nullptr;
6306 OH_JSVM_OpenEscapableHandleScope(env, &scope);
6307 JSVM_Value obj = nullptr;
6308 OH_JSVM_CreateObject(env, &obj);
6309 JSVM_Value value = nullptr;
6310 OH_JSVM_CreateStringUtf8(env, "Test jsvm_escapable_handle_scope", JSVM_AUTO_LENGTH, &value);
6311 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6312 JSVM_Value escapedObj = nullptr;
6313 OH_JSVM_EscapeHandle(env, scope, obj, &escapedObj);
6314 OH_JSVM_CloseEscapableHandleScope(env, scope);
6315 OH_JSVM_CreateStringUtf8(env, "001", JSVM_AUTO_LENGTH, &value);
6316 OH_JSVM_SetNamedProperty(env, escapedObj, "id", value);
6317 return escapedObj;
6318 }
6319
HandleScopeTest(JSVM_Env env,JSVM_CallbackInfo info)6320 static JSVM_Value HandleScopeTest(JSVM_Env env, JSVM_CallbackInfo info)
6321 {
6322 strcpy_s(g_dataType, sizeof(g_dataType), "object");
6323 JSVM_HandleScope scope = nullptr;
6324 JSVM_Status status = OH_JSVM_OpenHandleScope(env, &scope);
6325 if (status != JSVM_OK) {
6326 return nullptr;
6327 }
6328 JSVM_Value obj = nullptr;
6329 OH_JSVM_CreateObject(env, &obj);
6330 JSVM_Value value = nullptr;
6331 OH_JSVM_CreateStringUtf8(env, "test handleScope", JSVM_AUTO_LENGTH, &value);
6332 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6333 status = OH_JSVM_CloseHandleScope(env, scope);
6334 if (status != JSVM_OK) {
6335 return nullptr;
6336 }
6337 return obj;
6338 }
6339
HandleScope(JSVM_Env env,JSVM_CallbackInfo info)6340 static JSVM_Value HandleScope(JSVM_Env env, JSVM_CallbackInfo info)
6341 {
6342 strcpy_s(g_dataType, sizeof(g_dataType), "object");
6343 JSVM_HandleScope scope = nullptr;
6344 JSVM_Status status = OH_JSVM_OpenHandleScope(env, &scope);
6345 if (status != JSVM_OK) {
6346 return nullptr;
6347 }
6348 JSVM_Value obj = nullptr;
6349 OH_JSVM_CreateObject(env, &obj);
6350 JSVM_Value value = nullptr;
6351 OH_JSVM_CreateStringUtf8(env, "handleScope", JSVM_AUTO_LENGTH, &value);
6352 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6353 status = OH_JSVM_CloseHandleScope(env, scope);
6354 if (status != JSVM_OK) {
6355 return nullptr;
6356 }
6357 return obj;
6358 }
6359
6360
GetPropertyNames(JSVM_Env env,JSVM_CallbackInfo info)6361 static JSVM_Value GetPropertyNames(JSVM_Env env, JSVM_CallbackInfo info)
6362 {
6363 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6364 size_t argc = 1;
6365 JSVM_Value args[1] = {nullptr};
6366 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6367
6368 JSVM_Value result = nullptr;
6369 JSVM_Status status = OH_JSVM_GetPropertyNames(env, args[0], &result);
6370 if (status != JSVM_OK) {
6371 OH_JSVM_ThrowError(env, nullptr, "Failed to get propertynames");
6372 }
6373 return result;
6374 }
6375
GetAllPropertyNames(JSVM_Env env,JSVM_CallbackInfo info)6376 static JSVM_Value GetAllPropertyNames(JSVM_Env env, JSVM_CallbackInfo info)
6377 {
6378 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6379 size_t argc = 1;
6380 JSVM_Value args[1] = {nullptr};
6381 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6382 JSVM_Value result = nullptr;
6383 JSVM_Status status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
6384 JSVM_KeyFilter::JSVM_KEY_WRITABLE, JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6385 if (status != JSVM_OK) {
6386 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6387 }
6388 return result;
6389 }
6390
GetEnumerableNames(JSVM_Env env,JSVM_CallbackInfo info)6391 static JSVM_Value GetEnumerableNames(JSVM_Env env, JSVM_CallbackInfo info)
6392 {
6393 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6394 JSVM_Status status;
6395 size_t argc = 1;
6396 JSVM_Value args[1] = {nullptr};
6397 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6398 JSVM_Value result;
6399 status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_INCLUDE_PROTOTYPES,
6400 JSVM_KeyFilter::JSVM_KEY_ENUMERABLE, JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6401 if (status != JSVM_OK) {
6402 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6403 }
6404 return result;
6405 }
6406
GetOwnWritableNames(JSVM_Env env,JSVM_CallbackInfo info)6407 static JSVM_Value GetOwnWritableNames(JSVM_Env env, JSVM_CallbackInfo info)
6408 {
6409 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6410 JSVM_Status status;
6411 size_t argc = 1;
6412 JSVM_Value args[1] = {nullptr};
6413 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6414 JSVM_Value result;
6415 status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
6416 JSVM_KeyFilter::JSVM_KEY_WRITABLE, JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6417 if (status != JSVM_OK) {
6418 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6419 }
6420 return result;
6421 }
6422
GetEnumerableConfigurableNames(JSVM_Env env,JSVM_CallbackInfo info)6423 static JSVM_Value GetEnumerableConfigurableNames(JSVM_Env env, JSVM_CallbackInfo info)
6424 {
6425 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6426 JSVM_Status status;
6427 size_t argc = 1;
6428 JSVM_Value args[1] = {nullptr};
6429 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6430 JSVM_Value result;
6431 status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_INCLUDE_PROTOTYPES,
6432 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_CONFIGURABLE),
6433 JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6434 if (status != JSVM_OK) {
6435 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6436 }
6437 return result;
6438 }
6439
GetOwnConfigurableNames(JSVM_Env env,JSVM_CallbackInfo info)6440 static JSVM_Value GetOwnConfigurableNames(JSVM_Env env, JSVM_CallbackInfo info)
6441 {
6442 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6443 JSVM_Status status;
6444 size_t argc = 1;
6445 JSVM_Value args[1] = {nullptr};
6446 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6447 JSVM_Value result;
6448 status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
6449 JSVM_KeyFilter::JSVM_KEY_CONFIGURABLE, JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6450 if (status != JSVM_OK) {
6451 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6452 }
6453 return result;
6454 }
6455
AddReturnedStatus(JSVM_Env env,const char * key,JSVM_Value object,JSVM_Status expectedStatus,JSVM_Status actualStatus)6456 static void AddReturnedStatus(JSVM_Env env, const char *key, JSVM_Value object, JSVM_Status expectedStatus,
6457 JSVM_Status actualStatus)
6458 {
6459 char messageString[100] = "";
6460 JSVM_Value propValue;
6461 OH_JSVM_CreateStringUtf8(env, (actualStatus == expectedStatus ? "Invalid argument" : messageString),
6462 JSVM_AUTO_LENGTH, &propValue);
6463 OH_JSVM_SetNamedProperty(env, object, key, propValue);
6464 }
6465
AddLastStatus(JSVM_Env env,const char * key,JSVM_Value returnValue)6466 static void AddLastStatus(JSVM_Env env, const char *key, JSVM_Value returnValue)
6467 {
6468 JSVM_Value propValue;
6469 const JSVM_ExtendedErrorInfo *pLastError;
6470 OH_JSVM_GetLastErrorInfo(env, &pLastError);
6471 OH_JSVM_CreateStringUtf8(env,
6472 (pLastError->errorMessage == nullptr ? "JSVM_status::JSVM_OK" : pLastError->errorMessage), JSVM_AUTO_LENGTH,
6473 &propValue);
6474 OH_JSVM_SetNamedProperty(env, returnValue, key, propValue);
6475 }
6476
SetProperty(JSVM_Env env,JSVM_CallbackInfo info)6477 static JSVM_Value SetProperty(JSVM_Env env, JSVM_CallbackInfo info)
6478 {
6479 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6480 size_t argc = 3;
6481 JSVM_Value args[3] = {nullptr};
6482 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6483
6484 JSVM_Status status = OH_JSVM_SetProperty(env, args[0], args[1], args[2]);
6485 if (status != JSVM_OK) {
6486 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetProperty fail");
6487 }
6488 return args[0];
6489 }
6490
SetPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6491 static JSVM_Value SetPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6492 {
6493 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6494 JSVM_Value returnValue;
6495 JSVM_Value object;
6496 OH_JSVM_CreateObject(env, &returnValue);
6497 OH_JSVM_CreateObject(env, &object);
6498 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6499 OH_JSVM_SetProperty(nullptr, object, 0, object));
6500 OH_JSVM_SetProperty(env, nullptr, 0, object);
6501 AddLastStatus(env, "nullprtObject", returnValue);
6502 OH_JSVM_SetProperty(env, object, 0, nullptr);
6503 AddLastStatus(env, "nullptrValue", returnValue);
6504 return returnValue;
6505 }
6506
GetProperty(JSVM_Env env,JSVM_CallbackInfo info)6507 static JSVM_Value GetProperty(JSVM_Env env, JSVM_CallbackInfo info)
6508 {
6509 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6510 size_t argc = 2;
6511 JSVM_Value args[2] = {nullptr};
6512 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6513 JSVM_Value result;
6514 JSVM_Status status = OH_JSVM_GetProperty(env, args[0], args[1], &result);
6515 if (status != JSVM_OK) {
6516 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetProperty fail");
6517 }
6518 GetResultType(env, result);
6519 return result;
6520 }
6521
GetPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6522 static JSVM_Value GetPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6523 {
6524 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6525 JSVM_Value returnValue;
6526 JSVM_Value object;
6527 OH_JSVM_CreateObject(env, &returnValue);
6528 OH_JSVM_CreateObject(env, &object);
6529 JSVM_Value key;
6530 OH_JSVM_CreateInt32(env, 0, &key);
6531 JSVM_Value result;
6532 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6533 OH_JSVM_GetProperty(nullptr, object, key, &result));
6534 OH_JSVM_GetProperty(env, nullptr, key, &result);
6535 AddLastStatus(env, "nullprtObject", returnValue);
6536 OH_JSVM_GetProperty(env, object, key, nullptr);
6537 AddLastStatus(env, "nullptrResult", returnValue);
6538 return returnValue;
6539 }
6540
HasProperty(JSVM_Env env,JSVM_CallbackInfo info)6541 static JSVM_Value HasProperty(JSVM_Env env, JSVM_CallbackInfo info)
6542 {
6543 JSVM_Status status;
6544 size_t argc = 2;
6545 JSVM_Value args[2] = {nullptr};
6546 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6547 bool result;
6548 status = OH_JSVM_HasProperty(env, args[0], args[1], &result);
6549 if (status != JSVM_OK) {
6550 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasProperty fail");
6551 }
6552 JSVM_Value returnReslut;
6553 OH_JSVM_GetBoolean(env, result, &returnReslut);
6554 return returnReslut;
6555 }
6556
HasPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6557 static JSVM_Value HasPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6558 {
6559 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6560 JSVM_Value returnValue;
6561 JSVM_Value object;
6562 OH_JSVM_CreateObject(env, &returnValue);
6563 OH_JSVM_CreateObject(env, &object);
6564 JSVM_Value key;
6565 OH_JSVM_CreateInt32(env, 0, &key);
6566 bool result;
6567 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6568 OH_JSVM_HasProperty(nullptr, object, key, &result));
6569 OH_JSVM_HasProperty(env, nullptr, key, &result);
6570 AddLastStatus(env, "nullprtObject", returnValue);
6571 OH_JSVM_HasProperty(env, object, nullptr, &result);
6572 AddLastStatus(env, "nullprtKey", returnValue);
6573 OH_JSVM_HasProperty(env, object, key, nullptr);
6574 AddLastStatus(env, "nullptrResult", returnValue);
6575 return returnValue;
6576 }
6577
DeleteProperty(JSVM_Env env,JSVM_CallbackInfo info)6578 static JSVM_Value DeleteProperty(JSVM_Env env, JSVM_CallbackInfo info)
6579 {
6580 size_t argc = 2;
6581 JSVM_Value args[2] = {nullptr};
6582 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6583
6584 JSVM_ValueType valueType;
6585 OH_JSVM_Typeof(env, args[0], &valueType);
6586 if (valueType != JSVM_OBJECT) {
6587 OH_JSVM_ThrowError(env, nullptr, "Expects an object as argument.");
6588 }
6589 bool result = false;
6590 JSVM_Status status = OH_JSVM_DeleteProperty(env, args[0], args[1], &result);
6591 if (status != JSVM_OK) {
6592 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_DeleteProperty failed");
6593 }
6594 JSVM_Value ret;
6595 OH_JSVM_GetBoolean(env, result, &ret);
6596 return ret;
6597 }
6598
DeletePropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6599 static JSVM_Value DeletePropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6600 {
6601 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6602 JSVM_Value returnValue;
6603 JSVM_Value object;
6604 OH_JSVM_CreateObject(env, &returnValue);
6605 OH_JSVM_CreateObject(env, &object);
6606 JSVM_Value key;
6607 OH_JSVM_CreateInt32(env, 0, &key);
6608 bool result;
6609 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6610 OH_JSVM_DeleteProperty(nullptr, object, key, &result));
6611 OH_JSVM_DeleteProperty(env, nullptr, key, &result);
6612 AddLastStatus(env, "nullprtObject", returnValue);
6613 OH_JSVM_DeleteProperty(env, object, nullptr, &result);
6614 AddLastStatus(env, "nullprtKey", returnValue);
6615 return returnValue;
6616 }
6617
HasOwnProperty(JSVM_Env env,JSVM_CallbackInfo info)6618 static JSVM_Value HasOwnProperty(JSVM_Env env, JSVM_CallbackInfo info)
6619 {
6620 size_t argc = 2;
6621 JSVM_Value args[2] = {nullptr};
6622 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6623 JSVM_ValueType valueType1;
6624 OH_JSVM_Typeof(env, args[0], &valueType1);
6625 if (valueType1 != JSVM_OBJECT) {
6626 OH_JSVM_ThrowError(env, nullptr, "First argument must be an object.");
6627 }
6628 JSVM_ValueType valuetype2;
6629 OH_JSVM_Typeof(env, args[1], &valuetype2);
6630 if (valuetype2 != JSVM_STRING && valuetype2 != JSVM_SYMBOL) {
6631 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6632 JSVM_Value result;
6633 OH_JSVM_CreateStringUtf8(env, "name expected status.", JSVM_AUTO_LENGTH, &result);
6634 return result;
6635 }
6636 bool hasProperty;
6637 JSVM_Status status = OH_JSVM_HasOwnProperty(env, args[0], args[1], &hasProperty);
6638 if (status != JSVM_OK) {
6639 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasOwnProperty failed");
6640 }
6641 JSVM_Value result;
6642 OH_JSVM_GetBoolean(env, hasProperty, &result);
6643 return result;
6644 }
6645
HasOwnPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6646 static JSVM_Value HasOwnPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6647 {
6648 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6649 JSVM_Value returnValue;
6650 JSVM_Value object;
6651 OH_JSVM_CreateObject(env, &returnValue);
6652 OH_JSVM_CreateObject(env, &object);
6653 JSVM_Value key;
6654 OH_JSVM_CreateInt32(env, 0, &key);
6655 bool result;
6656 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6657 OH_JSVM_HasOwnProperty(nullptr, object, key, &result));
6658 OH_JSVM_HasOwnProperty(env, nullptr, key, &result);
6659 AddLastStatus(env, "nullprtObject", returnValue);
6660 OH_JSVM_HasOwnProperty(env, object, nullptr, &result);
6661 AddLastStatus(env, "nullprtKey", returnValue);
6662 OH_JSVM_HasOwnProperty(env, object, key, nullptr);
6663 AddLastStatus(env, "nullptrResult", returnValue);
6664 return returnValue;
6665 }
6666 static const int32_t NAME_PROPERTY_NUM = 1234;
SetNamedPropertyCreateObject(JSVM_Env env,JSVM_CallbackInfo info)6667 static JSVM_Value SetNamedPropertyCreateObject(JSVM_Env env, JSVM_CallbackInfo info)
6668 {
6669 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6670 size_t argc = 1;
6671 JSVM_Value str;
6672 char strKey[32] = "";
6673 OH_JSVM_GetCbInfo(env, info, &argc, &str, nullptr, nullptr);
6674 size_t keyLength;
6675 OH_JSVM_GetValueStringUtf8(env, str, strKey, DIFF_VALUE_KEYLEN, &keyLength);
6676 JSVM_Value newObj;
6677 OH_JSVM_CreateObject(env, &newObj);
6678 int32_t value = NAME_PROPERTY_NUM;
6679 JSVM_Value numValue;
6680 OH_JSVM_CreateInt32(env, value, &numValue);
6681 JSVM_Status status = OH_JSVM_SetNamedProperty(env, newObj, strKey, numValue);
6682 if (status != JSVM_OK) {
6683 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetNamedProperty failed");
6684 }
6685 return newObj;
6686 }
6687
SetNamedProperty(JSVM_Env env,JSVM_CallbackInfo info)6688 static JSVM_Value SetNamedProperty(JSVM_Env env, JSVM_CallbackInfo info)
6689 {
6690 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6691 size_t argc = DIFF_VALUE_THREE;
6692 JSVM_Value args[3] = {nullptr};
6693 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6694 JSVM_ValueType valueType;
6695 OH_JSVM_Typeof(env, args[0], &valueType);
6696 if (valueType != JSVM_OBJECT) {
6697 OH_JSVM_ThrowError(env, nullptr, "First argument must be an object.");
6698 }
6699 char strKey[32] = "";
6700 OH_JSVM_GetValueStringUtf8(env, args[1], strKey, DIFF_VALUE_KEYLEN, nullptr);
6701 JSVM_Status status = OH_JSVM_SetNamedProperty(env, args[0], strKey, args[2]);
6702 if (status != JSVM_OK) {
6703 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetNamedProperty failed");
6704 }
6705 return args[0];
6706 }
6707
SetNamedPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6708 static JSVM_Value SetNamedPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6709 {
6710 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6711 JSVM_Value returnValue;
6712 JSVM_Value object;
6713 OH_JSVM_CreateObject(env, &returnValue);
6714 OH_JSVM_CreateObject(env, &object);
6715 char key[32] = "";
6716 JSVM_Value result = nullptr;
6717 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6718 OH_JSVM_SetNamedProperty(nullptr, object, key, result));
6719 OH_JSVM_SetNamedProperty(env, nullptr, key, result);
6720 AddLastStatus(env, "nullprtObject", returnValue);
6721 OH_JSVM_SetNamedProperty(env, object, nullptr, result);
6722 AddLastStatus(env, "nullprtKey", returnValue);
6723 OH_JSVM_SetNamedProperty(env, object, key, nullptr);
6724 AddLastStatus(env, "nullptrResult", returnValue);
6725 return returnValue;
6726 }
6727
GetNamedProperty(JSVM_Env env,JSVM_CallbackInfo info)6728 static JSVM_Value GetNamedProperty(JSVM_Env env, JSVM_CallbackInfo info)
6729 {
6730 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6731 size_t argc = 2;
6732 JSVM_Value args[2] = {nullptr};
6733 char strKey[32] = "";
6734 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6735 size_t keyLength;
6736 OH_JSVM_GetValueStringUtf8(env, args[1], strKey, DIFF_VALUE_KEYLEN, &keyLength);
6737 JSVM_Value result;
6738 JSVM_Status status = OH_JSVM_GetNamedProperty(env, args[0], strKey, &result);
6739 if (status != JSVM_OK) {
6740 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetNamedProperty failed");
6741 }
6742 GetResultType(env, result);
6743 return result;
6744 }
6745
GetNamedPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6746 static JSVM_Value GetNamedPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6747 {
6748 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6749 JSVM_Value returnValue;
6750 JSVM_Value object;
6751 OH_JSVM_CreateObject(env, &returnValue);
6752 OH_JSVM_CreateObject(env, &object);
6753 char key[32] = "";
6754 JSVM_Value result;
6755 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6756 OH_JSVM_GetNamedProperty(nullptr, object, key, &result));
6757 OH_JSVM_GetNamedProperty(env, nullptr, key, &result);
6758 AddLastStatus(env, "nullprtObject", returnValue);
6759 OH_JSVM_GetNamedProperty(env, object, nullptr, &result);
6760 AddLastStatus(env, "nullprtKey", returnValue);
6761 OH_JSVM_GetNamedProperty(env, object, key, nullptr);
6762 AddLastStatus(env, "nullptrResult", returnValue);
6763 return returnValue;
6764 }
6765
HasNamedProperty(JSVM_Env env,JSVM_CallbackInfo info)6766 static JSVM_Value HasNamedProperty(JSVM_Env env, JSVM_CallbackInfo info)
6767 {
6768 size_t argc = 2;
6769 JSVM_Value args[2] = {nullptr};
6770 char strKey[32] = "";
6771 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6772 size_t keyLength;
6773 OH_JSVM_GetValueStringUtf8(env, args[1], strKey, DIFF_VALUE_KEYLEN, &keyLength);
6774 bool hasProperty = false;
6775 JSVM_Status status = OH_JSVM_HasNamedProperty(env, args[0], strKey, &hasProperty);
6776 if (status != JSVM_OK) {
6777 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasNamedProperty failed");
6778 }
6779 JSVM_Value result;
6780 OH_JSVM_GetBoolean(env, hasProperty, &result);
6781 return result;
6782 }
6783
HasNamedPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6784 static JSVM_Value HasNamedPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6785 {
6786 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6787 JSVM_Value returnValue;
6788 JSVM_Value object;
6789 OH_JSVM_CreateObject(env, &returnValue);
6790 OH_JSVM_CreateObject(env, &object);
6791 char key[32] = "";
6792 bool result;
6793 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6794 OH_JSVM_HasNamedProperty(nullptr, object, key, &result));
6795 OH_JSVM_HasNamedProperty(env, nullptr, key, &result);
6796 AddLastStatus(env, "nullprtObject", returnValue);
6797 OH_JSVM_HasNamedProperty(env, object, nullptr, &result);
6798 AddLastStatus(env, "nullprtKey", returnValue);
6799 OH_JSVM_HasNamedProperty(env, object, key, nullptr);
6800 AddLastStatus(env, "nullptrResult", returnValue);
6801 return returnValue;
6802 }
6803
SetElementWithLength(JSVM_Env env,JSVM_CallbackInfo info)6804 static JSVM_Value SetElementWithLength(JSVM_Env env, JSVM_CallbackInfo info)
6805 {
6806 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6807 size_t argc = 1;
6808 JSVM_Value args;
6809 OH_JSVM_GetCbInfo(env, info, &argc, &args, nullptr, nullptr);
6810 int32_t arrLength = 0;
6811 OH_JSVM_GetValueInt32(env, args, &arrLength);
6812 JSVM_Value result;
6813 OH_JSVM_CreateArrayWithLength(env, arrLength, &result);
6814 for (int32_t i = 0; i < arrLength; i++) {
6815 JSVM_Value value;
6816 OH_JSVM_CreateInt32(env, i + DIFF_VALUE_TWO, &value);
6817 OH_JSVM_SetElement(env, result, i, value);
6818 }
6819 return result;
6820 }
6821
SetElement(JSVM_Env env,JSVM_CallbackInfo info)6822 static JSVM_Value SetElement(JSVM_Env env, JSVM_CallbackInfo info)
6823 {
6824 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6825 size_t argc = DIFF_VALUE_THREE;
6826 JSVM_Value args[3] = {nullptr};
6827 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6828 int32_t index = 0;
6829 OH_JSVM_GetValueInt32(env, args[1], &index);
6830 OH_JSVM_SetElement(env, args[0], index, args[DIFF_VALUE_TWO]);
6831 return args[0];
6832 }
6833
SetElementFailed(JSVM_Env env,JSVM_CallbackInfo info)6834 static JSVM_Value SetElementFailed(JSVM_Env env, JSVM_CallbackInfo info)
6835 {
6836 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6837 JSVM_Value returnValue;
6838 JSVM_Value object;
6839 OH_JSVM_CreateObject(env, &returnValue);
6840 OH_JSVM_CreateObject(env, &object);
6841 JSVM_Value value = nullptr;
6842 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6843 OH_JSVM_SetElement(nullptr, object, 0, value));
6844 OH_JSVM_SetElement(env, nullptr, 0, value);
6845 AddLastStatus(env, "nullprtObject", returnValue);
6846 OH_JSVM_SetElement(env, object, 0, nullptr);
6847 AddLastStatus(env, "nullprtValue", returnValue);
6848 return returnValue;
6849 }
6850
GetElement(JSVM_Env env,JSVM_CallbackInfo info)6851 static JSVM_Value GetElement(JSVM_Env env, JSVM_CallbackInfo info)
6852 {
6853 size_t argc = 2;
6854 JSVM_Value args[2] = {nullptr};
6855 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6856 uint32_t index;
6857 OH_JSVM_GetValueUint32(env, args[1], &index);
6858 JSVM_Value result = nullptr;
6859 OH_JSVM_GetElement(env, args[0], index, &result);
6860 GetResultType(env, result);
6861 return result;
6862 }
6863
GetElementFailed(JSVM_Env env,JSVM_CallbackInfo info)6864 static JSVM_Value GetElementFailed(JSVM_Env env, JSVM_CallbackInfo info)
6865 {
6866 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6867 JSVM_Value returnValue;
6868 JSVM_Value object;
6869 OH_JSVM_CreateObject(env, &returnValue);
6870 OH_JSVM_CreateObject(env, &object);
6871 JSVM_Value value;
6872 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6873 OH_JSVM_GetElement(nullptr, object, 0, &value));
6874 OH_JSVM_GetElement(env, nullptr, 0, &value);
6875 AddLastStatus(env, "nullprtObject", returnValue);
6876 OH_JSVM_GetElement(env, object, 0, nullptr);
6877 AddLastStatus(env, "nullprtValue", returnValue);
6878 return returnValue;
6879 }
6880
HasElement(JSVM_Env env,JSVM_CallbackInfo info)6881 static JSVM_Value HasElement(JSVM_Env env, JSVM_CallbackInfo info)
6882 {
6883 size_t argc = 2;
6884 JSVM_Value args[2] = {nullptr};
6885 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6886 uint32_t index;
6887 OH_JSVM_GetValueUint32(env, args[1], &index);
6888 bool hasElement = true;
6889 OH_JSVM_HasElement(env, args[0], index, &hasElement);
6890 JSVM_Value result = nullptr;
6891 OH_JSVM_GetBoolean(env, hasElement, &result);
6892 return result;
6893 }
6894
HasElementFailed(JSVM_Env env,JSVM_CallbackInfo info)6895 static JSVM_Value HasElementFailed(JSVM_Env env, JSVM_CallbackInfo info)
6896 {
6897 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6898 JSVM_Value returnValue;
6899 JSVM_Value object;
6900 OH_JSVM_CreateObject(env, &returnValue);
6901 OH_JSVM_CreateObject(env, &object);
6902 bool result;
6903 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6904 OH_JSVM_HasElement(nullptr, object, 0, &result));
6905 OH_JSVM_HasElement(env, nullptr, 0, &result);
6906 AddLastStatus(env, "nullprtObject", returnValue);
6907 OH_JSVM_HasElement(env, object, 0, nullptr);
6908 AddLastStatus(env, "nullprtresult", returnValue);
6909 return returnValue;
6910 }
6911
DeleteElement(JSVM_Env env,JSVM_CallbackInfo info)6912 static JSVM_Value DeleteElement(JSVM_Env env, JSVM_CallbackInfo info)
6913 {
6914 size_t argc = 2;
6915 JSVM_Value args[2] = {nullptr};
6916 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6917 uint32_t index;
6918 OH_JSVM_GetValueUint32(env, args[1], &index);
6919 bool deleted = true;
6920 OH_JSVM_DeleteElement(env, args[0], index, &deleted);
6921 JSVM_Value result = nullptr;
6922 OH_JSVM_GetBoolean(env, deleted, &result);
6923 return result;
6924 }
6925
DeleteElementAndCheck(JSVM_Env env,JSVM_CallbackInfo info)6926 static JSVM_Value DeleteElementAndCheck(JSVM_Env env, JSVM_CallbackInfo info)
6927 {
6928 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6929 size_t argc = 2;
6930 JSVM_Value args[2] = {nullptr};
6931 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6932 uint32_t index;
6933 OH_JSVM_GetValueUint32(env, args[1], &index);
6934 OH_JSVM_DeleteElement(env, args[0], index, nullptr);
6935 return args[0];
6936 }
6937
DeleteElementFailed(JSVM_Env env,JSVM_CallbackInfo info)6938 static JSVM_Value DeleteElementFailed(JSVM_Env env, JSVM_CallbackInfo info)
6939 {
6940 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6941 JSVM_Value returnValue;
6942 JSVM_Value object;
6943 OH_JSVM_CreateObject(env, &returnValue);
6944 OH_JSVM_CreateObject(env, &object);
6945 bool result;
6946 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6947 OH_JSVM_DeleteElement(nullptr, object, 0, &result));
6948 OH_JSVM_DeleteElement(env, nullptr, 0, &result);
6949 AddLastStatus(env, "nullprtObject", returnValue);
6950 return returnValue;
6951 }
6952
6953 static const int32_t METHOD_NUM = 26;
DefineMethodPropertiesExample(JSVM_Env env,JSVM_CallbackInfo info)6954 static JSVM_Value DefineMethodPropertiesExample(JSVM_Env env, JSVM_CallbackInfo info)
6955 {
6956 JSVM_Value returnValue;
6957 OH_JSVM_CreateInt32(env, METHOD_NUM, &returnValue);
6958 return returnValue;
6959 }
DefineMethodProperties(JSVM_Env env,JSVM_CallbackInfo info)6960 static JSVM_Value DefineMethodProperties(JSVM_Env env, JSVM_CallbackInfo info)
6961 {
6962 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6963 size_t argc = 1;
6964 JSVM_Value argv[1];
6965 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
6966 JSVM_CallbackStruct param[] = {
6967 {.callback = DefineMethodPropertiesExample, .data = nullptr},
6968 };
6969 JSVM_PropertyDescriptor descriptor[] = {
6970 {"defineMethodPropertiesExample", nullptr, ¶m[0], nullptr, nullptr, nullptr, JSVM_DEFAULT}
6971 };
6972 OH_JSVM_DefineProperties(env, *argv, sizeof(descriptor) / sizeof(descriptor[0]), descriptor);
6973 const char *srcMethod = R"JS(
6974 obj.defineMethodPropertiesExample();
6975 )JS";
6976 JSVM_Value sourceCodeValue = nullptr;
6977 OH_JSVM_CreateStringUtf8(env, srcMethod, strlen(srcMethod), &sourceCodeValue);
6978 JSVM_Script script;
6979 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
6980 JSVM_Value jsVmResult;
6981 OH_JSVM_RunScript(env, script, &jsVmResult);
6982 return jsVmResult;
6983 }
6984
DefineStringProperties(JSVM_Env env,JSVM_CallbackInfo info)6985 static JSVM_Value DefineStringProperties(JSVM_Env env, JSVM_CallbackInfo info)
6986 {
6987 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6988 size_t argc = 1;
6989 JSVM_Value argv[1];
6990 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
6991 JSVM_Value stringValue;
6992 OH_JSVM_CreateStringUtf8(env, "Hello!", JSVM_AUTO_LENGTH, &stringValue);
6993 JSVM_PropertyDescriptor descriptor[] = {
6994 {"defineStringPropertiesExample", nullptr, nullptr, nullptr, nullptr, stringValue, JSVM_DEFAULT}
6995 };
6996 OH_JSVM_DefineProperties(env, *argv, sizeof(descriptor) / sizeof(descriptor[0]), descriptor);
6997 const char *srcMethod = R"JS(
6998 obj.defineStringPropertiesExample;
6999 )JS";
7000 JSVM_Value sourceCodeValue = nullptr;
7001 OH_JSVM_CreateStringUtf8(env, srcMethod, strlen(srcMethod), &sourceCodeValue);
7002 JSVM_Script script;
7003 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
7004 JSVM_Value jsVmResult;
7005 OH_JSVM_RunScript(env, script, &jsVmResult);
7006 return jsVmResult;
7007 }
7008
GetterCallback(JSVM_Env env,JSVM_CallbackInfo info)7009 static JSVM_Value GetterCallback(JSVM_Env env, JSVM_CallbackInfo info)
7010 {
7011 JSVM_Value result;
7012 const char *str = "Hello world!";
7013 size_t length = strlen(str);
7014 OH_JSVM_CreateStringUtf8(env, str, length, &result);
7015 return result;
7016 }
7017
DefineGetterProperties(JSVM_Env env,JSVM_CallbackInfo info)7018 static JSVM_Value DefineGetterProperties(JSVM_Env env, JSVM_CallbackInfo info)
7019 {
7020 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7021 size_t argc = 1;
7022 JSVM_Value argv[1];
7023 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7024 JSVM_CallbackStruct param[] = {
7025 {.callback = GetterCallback, .data = nullptr},
7026 };
7027 JSVM_PropertyDescriptor descriptor[] = {
7028 {"getterCallback", nullptr, nullptr, ¶m[0], nullptr, nullptr, JSVM_DEFAULT}
7029 };
7030 OH_JSVM_DefineProperties(env, *argv, 1, descriptor);
7031 const char *srcMethod = R"JS(
7032 obj.getterCallback;
7033 )JS";
7034 JSVM_Value sourceCodeValue = nullptr;
7035 OH_JSVM_CreateStringUtf8(env, srcMethod, strlen(srcMethod), &sourceCodeValue);
7036 JSVM_Script script;
7037 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
7038 JSVM_Value jsVmResult;
7039 OH_JSVM_RunScript(env, script, &jsVmResult);
7040 return jsVmResult;
7041 }
7042
DefinePropertiesFailed(JSVM_Env env,JSVM_CallbackInfo info)7043 static JSVM_Value DefinePropertiesFailed(JSVM_Env env, JSVM_CallbackInfo info)
7044 {
7045 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7046 JSVM_Value returnValue;
7047 JSVM_Value object;
7048 OH_JSVM_CreateObject(env, &returnValue);
7049 OH_JSVM_CreateObject(env, &object);
7050 JSVM_CallbackStruct param[] = {
7051 {.callback = DefinePropertiesFailed, .data = nullptr},
7052 };
7053 JSVM_PropertyDescriptor desc[] = {{"prop", nullptr, ¶m[0], nullptr, nullptr, nullptr, JSVM_DEFAULT}};
7054 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7055 OH_JSVM_DefineProperties(nullptr, object, 1, desc));
7056 OH_JSVM_DefineProperties(env, nullptr, 1, desc);
7057 AddLastStatus(env, "nullprtObject", returnValue);
7058 OH_JSVM_DefineProperties(env, object, 1, nullptr);
7059 AddLastStatus(env, "nullptrDescriptorList", returnValue);
7060 desc->utf8name = nullptr;
7061 OH_JSVM_DefineProperties(env, object, 1, nullptr);
7062 AddLastStatus(env, "nullptrUtf8name", returnValue);
7063 return returnValue;
7064 }
7065
ObjectFreeze(JSVM_Env env,JSVM_CallbackInfo info)7066 static JSVM_Value ObjectFreeze(JSVM_Env env, JSVM_CallbackInfo info)
7067 {
7068 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7069 size_t argc = 1;
7070 JSVM_Value argv[1];
7071 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7072 OH_JSVM_ObjectFreeze(env, argv[0]);
7073 JSVM_Value value;
7074 OH_JSVM_CreateInt32(env, 0, &value);
7075 OH_JSVM_SetNamedProperty(env, argv[0], "data", value);
7076 OH_JSVM_SetNamedProperty(env, argv[0], "newKey", value);
7077 JSVM_Value key;
7078 OH_JSVM_CreateStringUtf8(env, "message", JSVM_AUTO_LENGTH, &key);
7079 OH_JSVM_DeleteProperty(env, argv[0], key, nullptr);
7080 return argv[0];
7081 }
7082
ObjectSeal(JSVM_Env env,JSVM_CallbackInfo info)7083 static JSVM_Value ObjectSeal(JSVM_Env env, JSVM_CallbackInfo info)
7084 {
7085 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7086 size_t argc = 1;
7087 JSVM_Value argv[1];
7088 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7089 OH_JSVM_ObjectSeal(env, argv[0]);
7090
7091 JSVM_Value changeValue;
7092 OH_JSVM_CreateInt32(env, METHOD_NUM, &changeValue);
7093 OH_JSVM_SetNamedProperty(env, argv[0], "data", changeValue);
7094 JSVM_Value deleteProperty;
7095 OH_JSVM_CreateStringUtf8(env, "message", JSVM_AUTO_LENGTH, &deleteProperty);
7096 OH_JSVM_DeleteProperty(env, argv[0], deleteProperty, nullptr);
7097 JSVM_Value addValue;
7098 OH_JSVM_CreateStringUtf8(env, "addValue", JSVM_AUTO_LENGTH, &addValue);
7099 OH_JSVM_SetNamedProperty(env, argv[0], "newProperty", addValue);
7100 return argv[0];
7101 }
7102
GetVersion(JSVM_Env env,JSVM_CallbackInfo info)7103 static JSVM_Value GetVersion(JSVM_Env env, JSVM_CallbackInfo info)
7104 {
7105 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7106 uint32_t jsVersion;
7107 OH_JSVM_GetVersion(env, &jsVersion);
7108 JSVM_Value result;
7109 OH_JSVM_CreateUint32(env, jsVersion, &result);
7110 return result;
7111 }
7112
GetVersionFailed(JSVM_Env env,JSVM_CallbackInfo info)7113 static JSVM_Value GetVersionFailed(JSVM_Env env, JSVM_CallbackInfo info)
7114 {
7115 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7116 JSVM_Value returnValue;
7117 OH_JSVM_CreateObject(env, &returnValue);
7118 uint32_t jsVersion;
7119 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7120 OH_JSVM_GetVersion(nullptr, &jsVersion));
7121 OH_JSVM_GetVersion(env, nullptr);
7122 AddLastStatus(env, "nullptrResult", returnValue);
7123 return returnValue;
7124 }
7125
7126
GetVMInfo(JSVM_Env env,JSVM_CallbackInfo info)7127 static JSVM_Value GetVMInfo(JSVM_Env env, JSVM_CallbackInfo info)
7128 {
7129 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7130 JSVM_VMInfo result;
7131 OH_JSVM_GetVMInfo(&result);
7132 JSVM_Value version;
7133 OH_JSVM_CreateUint32(env, result.apiVersion, &version);
7134 return version;
7135 }
7136
JsonStringify(JSVM_Env env,JSVM_CallbackInfo info)7137 static JSVM_Value JsonStringify(JSVM_Env env, JSVM_CallbackInfo info)
7138 {
7139 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7140 size_t argc = 1;
7141 JSVM_Value argv[1] = {nullptr};
7142 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7143 JSVM_Value result;
7144 OH_JSVM_JsonStringify(env, *argv, &result);
7145 return result;
7146 }
7147
JsonStringifyFailed(JSVM_Env env,JSVM_CallbackInfo info)7148 static JSVM_Value JsonStringifyFailed(JSVM_Env env, JSVM_CallbackInfo info)
7149 {
7150 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7151 JSVM_Value returnValue;
7152 OH_JSVM_CreateObject(env, &returnValue);
7153 JSVM_Value jsonObject = nullptr;
7154 JSVM_Value ret = nullptr;
7155 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7156 OH_JSVM_JsonStringify(nullptr, jsonObject, &ret));
7157 OH_JSVM_JsonStringify(env, nullptr, &ret);
7158 AddLastStatus(env, "nullptrJsonObject", returnValue);
7159 return returnValue;
7160 }
7161
JsonParseNumber(JSVM_Env env,JSVM_CallbackInfo info)7162 static JSVM_Value JsonParseNumber(JSVM_Env env, JSVM_CallbackInfo info)
7163 {
7164 strcpy_s(g_dataType, sizeof(g_dataType), "double");
7165 const char *strNumber = "10.555";
7166 JSVM_Value jsonString = nullptr;
7167 OH_JSVM_CreateStringUtf8(env, strNumber, strlen(strNumber), &jsonString);
7168 JSVM_Value result = nullptr;
7169 OH_JSVM_JsonParse(env, jsonString, &result);
7170 return result;
7171 }
7172
JsonParseObject(JSVM_Env env,JSVM_CallbackInfo info)7173 static JSVM_Value JsonParseObject(JSVM_Env env, JSVM_CallbackInfo info)
7174 {
7175 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7176 const char *strObject = "{\"first\": \"one\", \"second\": \"two\"}";
7177 JSVM_Value strJson = nullptr;
7178 OH_JSVM_CreateStringUtf8(env, strObject, strlen(strObject), &strJson);
7179 JSVM_Value ret = nullptr;
7180 OH_JSVM_JsonParse(env, strJson, &ret);
7181 return ret;
7182 }
7183
JsonParse(JSVM_Env env,JSVM_CallbackInfo info)7184 static JSVM_Value JsonParse(JSVM_Env env, JSVM_CallbackInfo info)
7185 {
7186 size_t argc = 1;
7187 JSVM_Value argv[1] = {nullptr};
7188 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7189 JSVM_Value result = nullptr;
7190 OH_JSVM_JsonParse(env, *argv, &result);
7191 GetResultType(env, result);
7192 return result;
7193 }
7194
JsonParseFailed(JSVM_Env env,JSVM_CallbackInfo info)7195 static JSVM_Value JsonParseFailed(JSVM_Env env, JSVM_CallbackInfo info)
7196 {
7197 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7198 JSVM_Value returnValue = nullptr;
7199 OH_JSVM_CreateObject(env, &returnValue);
7200 const char *str = "";
7201 JSVM_Value strJson = nullptr;
7202 OH_JSVM_CreateStringUtf8(env, str, strlen(str), &strJson);
7203 JSVM_Value ret = nullptr;
7204 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7205 OH_JSVM_JsonParse(nullptr, strJson, &ret));
7206 OH_JSVM_JsonParse(env, nullptr, &ret);
7207 AddLastStatus(env, "nullptrJsonString", returnValue);
7208 return returnValue;
7209 }
7210
CallFunction(JSVM_Env env,JSVM_CallbackInfo info)7211 static JSVM_Value CallFunction(JSVM_Env env, JSVM_CallbackInfo info)
7212 {
7213 size_t argc = 1;
7214 JSVM_Value args[1] = {nullptr};
7215 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7216 JSVM_Value global = nullptr;
7217 OH_JSVM_GetGlobal(env, &global);
7218 JSVM_Value result = nullptr;
7219 OH_JSVM_CallFunction(env, global, args[0], 0, nullptr, &result);
7220 GetResultType(env, result);
7221 return result;
7222 }
7223
CallFunctionWithArg(JSVM_Env env,JSVM_CallbackInfo info)7224 static JSVM_Value CallFunctionWithArg(JSVM_Env env, JSVM_CallbackInfo info)
7225 {
7226 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7227 size_t argc = 1;
7228 JSVM_Value args[1] = {nullptr};
7229 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7230 JSVM_Value global = nullptr;
7231 OH_JSVM_GetGlobal(env, &global);
7232 JSVM_Value arg[2] = {nullptr};
7233 OH_JSVM_CreateStringUtf8(env, "Hello", JSVM_AUTO_LENGTH, &arg[0]);
7234 OH_JSVM_CreateStringUtf8(env, " World!", JSVM_AUTO_LENGTH, &arg[1]);
7235 JSVM_Value result = nullptr;
7236 OH_JSVM_CallFunction(env, global, args[0], DIFF_VALUE_TWO, arg, &result);
7237 return result;
7238 }
7239
CallFunctionWithObject(JSVM_Env env,JSVM_CallbackInfo info)7240 static JSVM_Value CallFunctionWithObject(JSVM_Env env, JSVM_CallbackInfo info)
7241 {
7242 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7243 size_t argc = 1;
7244 JSVM_Value args[1] = {nullptr};
7245 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7246 JSVM_Value func = nullptr;
7247 OH_JSVM_GetNamedProperty(env, args[0], "myMethod", &func);
7248 JSVM_Value global = nullptr;
7249 OH_JSVM_GetGlobal(env, &global);
7250 JSVM_Value arg[2] = {nullptr};
7251 OH_JSVM_CreateInt32(env, METHOD_NUM, &arg[0]);
7252 OH_JSVM_CreateInt32(env, METHOD_NUM, &arg[1]);
7253 JSVM_Value result = nullptr;
7254 OH_JSVM_CallFunction(env, global, func, DIFF_VALUE_TWO, arg, &result);
7255 return result;
7256 }
7257
CallFunctionFailed(JSVM_Env env,JSVM_CallbackInfo info)7258 static JSVM_Value CallFunctionFailed(JSVM_Env env, JSVM_CallbackInfo info)
7259 {
7260 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7261 size_t argc = 1;
7262 JSVM_Value args[1] = {nullptr};
7263 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7264 JSVM_Value returnValue = nullptr;
7265 OH_JSVM_CreateObject(env, &returnValue);
7266 JSVM_Value global = nullptr;
7267 OH_JSVM_GetGlobal(env, &global);
7268 JSVM_Value result = nullptr;
7269 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7270 OH_JSVM_CallFunction(nullptr, global, args[0], 0, nullptr, &result));
7271 OH_JSVM_CallFunction(env, nullptr, args[0], 0, nullptr, &result);
7272 AddLastStatus(env, "nullprtGlobal", returnValue);
7273 OH_JSVM_CallFunction(env, global, args[0], 1, nullptr, &result);
7274 AddLastStatus(env, "nullprtArgv", returnValue);
7275 OH_JSVM_CallFunction(env, global, nullptr, 0, nullptr, &result);
7276 AddLastStatus(env, "nullptrFunc", returnValue);
7277 return returnValue;
7278 }
7279
CalculateArea(JSVM_Env env,JSVM_CallbackInfo info)7280 static JSVM_Value CalculateArea(JSVM_Env env, JSVM_CallbackInfo info)
7281 {
7282 size_t argc = 2;
7283 JSVM_Value args[2] = {nullptr};
7284 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7285
7286 double width;
7287 OH_JSVM_GetValueDouble(env, args[0], &width);
7288 double height;
7289 OH_JSVM_GetValueDouble(env, args[1], &height);
7290 JSVM_Value area;
7291 OH_JSVM_CreateDouble(env, width * height, &area);
7292 return area;
7293 }
7294 static double g_width = 1.4;
7295 static double g_height = 5.0;
CreateFunction(JSVM_Env env,JSVM_CallbackInfo info)7296 static JSVM_Value CreateFunction(JSVM_Env env, JSVM_CallbackInfo info)
7297 {
7298 strcpy_s(g_dataType, sizeof(g_dataType), "double");
7299 JSVM_CallbackStruct param;
7300 param.data = nullptr;
7301 param.callback = CalculateArea;
7302 JSVM_Value funcValue = nullptr;
7303 JSVM_Status status = OH_JSVM_CreateFunction(env, "calculateArea", JSVM_AUTO_LENGTH, ¶m, &funcValue);
7304 if (funcValue == nullptr || status != JSVM_OK) {
7305 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CreateFunction failed");
7306 }
7307 JSVM_Value args[2] = {nullptr};
7308 OH_JSVM_CreateDouble(env, g_width, &args[0]);
7309 OH_JSVM_CreateDouble(env, g_height, &args[1]);
7310 JSVM_Value global = nullptr;
7311 OH_JSVM_GetGlobal(env, &global);
7312 JSVM_Value ret = nullptr;
7313 OH_JSVM_CallFunction(env, global, funcValue, DIFF_VALUE_TWO, args, &ret);
7314 return ret;
7315 }
7316
CreateFunctionFailed(JSVM_Env env,JSVM_CallbackInfo info)7317 static JSVM_Value CreateFunctionFailed(JSVM_Env env, JSVM_CallbackInfo info)
7318 {
7319 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7320 size_t argc = 1;
7321 JSVM_Value args[1] = {nullptr};
7322 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7323 JSVM_Value returnValue = nullptr;
7324 OH_JSVM_CreateObject(env, &returnValue);
7325 JSVM_CallbackStruct param;
7326 param.data = nullptr;
7327 param.callback = CalculateArea;
7328 JSVM_Value result = nullptr;
7329 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7330 OH_JSVM_CreateFunction(nullptr, "myFunc", JSVM_AUTO_LENGTH, ¶m, &result));
7331 OH_JSVM_CreateFunction(env, "myFunc", JSVM_AUTO_LENGTH, nullptr, &result);
7332 AddLastStatus(env, "nullprtCD", returnValue);
7333 OH_JSVM_CreateFunction(env, "myFunc", JSVM_AUTO_LENGTH, ¶m, nullptr);
7334 AddLastStatus(env, "nullptrResult", returnValue);
7335 return returnValue;
7336 }
7337
CreateFunctionWithScript(JSVM_Env env,JSVM_CallbackInfo info)7338 static JSVM_Value CreateFunctionWithScript(JSVM_Env env, JSVM_CallbackInfo info)
7339 {
7340 size_t argc = 3;
7341 JSVM_Value argv[3] = { nullptr};
7342 JSVM_Value thisArg = nullptr;
7343 OH_JSVM_GetCbInfo(env, info, &argc, argv, &thisArg, nullptr);
7344
7345 uint32_t arrayLen = 0;
7346 JSVM_CALL(env, OH_JSVM_GetArrayLength(env, argv[1], &arrayLen));
7347 if (arrayLen <= 0 || arrayLen > std::numeric_limits<uint32_t>::max() / sizeof(JSVM_Value)) {
7348 OH_JSVM_ThrowError(env, nullptr, "Invalid array length.");
7349 return nullptr;
7350 } else {
7351 JSVM_Value *args = new JSVM_Value[arrayLen];
7352 for (auto i = 0; i < arrayLen; i++) {
7353 JSVM_CALL(env, OH_JSVM_GetElement(env, argv[1], i, &args[i]));
7354 }
7355 JSVM_Value js_string;
7356 JSVM_CALL(env, OH_JSVM_CoerceToString(env, argv[0], &js_string));
7357 size_t length = 0;
7358 JSVM_CALL(env, OH_JSVM_GetValueStringUtf8(env, js_string, NULL, 0, &length));
7359 size_t capacity = length + 1;
7360 char *buffer = new char[capacity];
7361 size_t copyLength = 0;
7362 JSVM_CALL(env, OH_JSVM_GetValueStringUtf8(env, js_string, buffer, capacity, ©Length));
7363
7364 JSVM_Value func = nullptr;
7365 JSVM_CALL(env, OH_JSVM_CreateFunctionWithScript(env, buffer, JSVM_AUTO_LENGTH, arrayLen,
7366 args, argv[ argc - 1 ], &func));
7367 return func;
7368 }
7369 }
7370
GetHeapStatistics(JSVM_Env env,JSVM_CallbackInfo info)7371 static JSVM_Value GetHeapStatistics(JSVM_Env env, JSVM_CallbackInfo info)
7372 {
7373 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7374 JSVM_VM testVm;
7375 OH_JSVM_GetVM(env, &testVm);
7376 JSVM_HeapStatistics result;
7377 OH_JSVM_GetHeapStatistics(testVm, &result);
7378 JSVM_Value nativeContextsCnt = nullptr;
7379 OH_JSVM_CreateInt64(env, result.numberOfNativeContexts, &nativeContextsCnt);
7380 return nativeContextsCnt;
7381 }
7382
GetVM(JSVM_Env env,JSVM_CallbackInfo info)7383 static JSVM_Value GetVM(JSVM_Env env, JSVM_CallbackInfo info)
7384 {
7385 JSVM_VM testVm;
7386 JSVM_Status status = OH_JSVM_GetVM(env, &testVm);
7387 if (status != JSVM_OK) {
7388 OH_JSVM_ThrowError(env, nullptr, "Test JSVM OH_JSVM_GetVM failed");
7389 }
7390 JSVM_Value result;
7391 OH_JSVM_GetBoolean(env, true, &result);
7392 return result;
7393 }
7394
GetTypeof(JSVM_Env env,JSVM_CallbackInfo info)7395 static JSVM_Value GetTypeof(JSVM_Env env, JSVM_CallbackInfo info)
7396 {
7397 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7398 size_t argc = 1;
7399 JSVM_Value args[1] = {nullptr};
7400 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7401 JSVM_ValueType valueType;
7402 OH_JSVM_Typeof(env, args[0], &valueType);
7403 JSVM_Value type = nullptr;
7404 switch (valueType) {
7405 case JSVM_UNDEFINED:
7406 OH_JSVM_CreateStringUtf8(env, "undefined", JSVM_AUTO_LENGTH, &type);
7407 break;
7408 case JSVM_NULL:
7409 OH_JSVM_CreateStringUtf8(env, "null", JSVM_AUTO_LENGTH, &type);
7410 break;
7411 case JSVM_BOOLEAN:
7412 OH_JSVM_CreateStringUtf8(env, "boolean", JSVM_AUTO_LENGTH, &type);
7413 break;
7414 case JSVM_NUMBER:
7415 OH_JSVM_CreateStringUtf8(env, "number", JSVM_AUTO_LENGTH, &type);
7416 break;
7417 case JSVM_STRING:
7418 OH_JSVM_CreateStringUtf8(env, "string", JSVM_AUTO_LENGTH, &type);
7419 break;
7420 case JSVM_OBJECT:
7421 OH_JSVM_CreateStringUtf8(env, "object", JSVM_AUTO_LENGTH, &type);
7422 break;
7423 case JSVM_FUNCTION:
7424 OH_JSVM_CreateStringUtf8(env, "function", JSVM_AUTO_LENGTH, &type);
7425 break;
7426 case JSVM_BIGINT:
7427 OH_JSVM_CreateStringUtf8(env, "bigint", JSVM_AUTO_LENGTH, &type);
7428 break;
7429 default:
7430 OH_JSVM_CreateStringUtf8(env, " ", NAPI_AUTO_LENGTH, &type);
7431 break;
7432 }
7433 return type;
7434 }
7435
InstanceOf(JSVM_Env env,JSVM_CallbackInfo info)7436 static JSVM_Value InstanceOf(JSVM_Env env, JSVM_CallbackInfo info)
7437 {
7438 size_t argc = 2;
7439 JSVM_Value args[2] = {nullptr};
7440 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7441 bool result = false;
7442 OH_JSVM_Instanceof(env, args[0], args[1], &result);
7443 JSVM_Value returnValue = nullptr;
7444 OH_JSVM_GetBoolean(env, result, &returnValue);
7445 return returnValue;
7446 }
7447
IsArray(JSVM_Env env,JSVM_CallbackInfo info)7448 static JSVM_Value IsArray(JSVM_Env env, JSVM_CallbackInfo info)
7449 {
7450 size_t argc = 1;
7451 JSVM_Value args[1] = {nullptr};
7452 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7453 bool result = false;
7454 OH_JSVM_IsArray(env, args[0], &result);
7455 JSVM_Value returnValue = nullptr;
7456 OH_JSVM_GetBoolean(env, result, &returnValue);
7457 return returnValue;
7458 }
7459
IsArrayBuffer(JSVM_Env env,JSVM_CallbackInfo info)7460 static JSVM_Value IsArrayBuffer(JSVM_Env env, JSVM_CallbackInfo info)
7461 {
7462 size_t argc = 1;
7463 JSVM_Value args[1] = {nullptr};
7464 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7465 bool isArrayBuffer = false;
7466 JSVM_Status status = OH_JSVM_IsArraybuffer(env, args[0], &isArrayBuffer);
7467 if (status != JSVM_OK) {
7468 return nullptr;
7469 }
7470 JSVM_Value boolean = nullptr;
7471 OH_JSVM_GetBoolean(env, isArrayBuffer, &boolean);
7472 return boolean;
7473 }
7474
IsTypedarray(JSVM_Env env,JSVM_CallbackInfo info)7475 static JSVM_Value IsTypedarray(JSVM_Env env, JSVM_CallbackInfo info)
7476 {
7477 size_t argc = 1;
7478 JSVM_Value args[1] = {nullptr};
7479 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7480 bool result = false;
7481 OH_JSVM_IsTypedarray(env, args[0], &result);
7482 JSVM_Value isTypedArray = nullptr;
7483 OH_JSVM_GetBoolean(env, result, &isTypedArray);
7484 return isTypedArray;
7485 }
7486
IsDataView(JSVM_Env env,JSVM_CallbackInfo info)7487 static JSVM_Value IsDataView(JSVM_Env env, JSVM_CallbackInfo info)
7488 {
7489 size_t argc = 1;
7490 JSVM_Value args[1] = {nullptr};
7491 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7492 bool result = false;
7493 OH_JSVM_IsDataview(env, args[0], &result);
7494 JSVM_Value isDateView = nullptr;
7495 OH_JSVM_GetBoolean(env, result, &isDateView);
7496 return isDateView;
7497 }
7498
IsStrictEquals(JSVM_Env env,JSVM_CallbackInfo info)7499 static JSVM_Value IsStrictEquals(JSVM_Env env, JSVM_CallbackInfo info)
7500 {
7501 size_t argc = 2;
7502 JSVM_Value args[2] = {nullptr};
7503 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7504 bool result = false;
7505 OH_JSVM_StrictEquals(env, args[0], args[1], &result);
7506 JSVM_Value isStrictEqual = nullptr;
7507 OH_JSVM_GetBoolean(env, result, &isStrictEqual);
7508 return isStrictEqual;
7509 }
7510
DetachArraybuffer(JSVM_Env env,JSVM_CallbackInfo info)7511 static JSVM_Value DetachArraybuffer(JSVM_Env env, JSVM_CallbackInfo info)
7512 {
7513 strcpy_s(g_dataType, sizeof(g_dataType), "arraybuffer");
7514 size_t argc = 1;
7515 JSVM_Value args[1] = {nullptr};
7516 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7517 OH_JSVM_DetachArraybuffer(env, args[0]);
7518 return args[0];
7519 }
7520
IsDetachedArraybuffer(JSVM_Env env,JSVM_CallbackInfo info)7521 static JSVM_Value IsDetachedArraybuffer(JSVM_Env env, JSVM_CallbackInfo info)
7522 {
7523 strcpy_s(g_dataType, sizeof(g_dataType), "arraybuffer");
7524 size_t argc = 1;
7525 JSVM_Value args[1] = {nullptr};
7526 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7527 OH_JSVM_DetachArraybuffer(env, args[0]);
7528 bool result = false;
7529 OH_JSVM_IsDetachedArraybuffer(env, args[0], &result);
7530 JSVM_Value isDetached = nullptr;
7531 OH_JSVM_GetBoolean(env, result, &isDetached);
7532 return isDetached;
7533 }
7534
IsRegExp(JSVM_Env env,JSVM_CallbackInfo info)7535 static JSVM_Value IsRegExp(JSVM_Env env, JSVM_CallbackInfo info)
7536 {
7537 size_t argc = 1;
7538 JSVM_Value args[1] = {nullptr};
7539 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7540 bool result = false;
7541 OH_JSVM_IsRegExp(env, args[0], &result);
7542 JSVM_Value isRegExp = nullptr;
7543 OH_JSVM_GetBoolean(env, result, &isRegExp);
7544 return isRegExp;
7545 }
7546
CoerceToBigInt(JSVM_Env env,JSVM_CallbackInfo info)7547 static JSVM_Value CoerceToBigInt(JSVM_Env env, JSVM_CallbackInfo info)
7548 {
7549 size_t argc = 1;
7550 JSVM_Value args[1] = {nullptr};
7551 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7552 JSVM_Value bigInt;
7553 OH_JSVM_GetUndefined(env, &bigInt);
7554 JSVM_Status status = OH_JSVM_CoerceToBigInt(env, args[0], &bigInt);
7555 if (status != JSVM_OK) {
7556 OH_JSVM_ThrowError(env, nullptr, "Failed to coerce to bigint");
7557 }
7558 return bigInt;
7559 }
7560
CoerceToNumber(JSVM_Env env,JSVM_CallbackInfo info)7561 static JSVM_Value CoerceToNumber(JSVM_Env env, JSVM_CallbackInfo info)
7562 {
7563 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7564 size_t argc = 1;
7565 JSVM_Value args[1] = {nullptr};
7566 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7567 JSVM_Value number = nullptr;
7568 OH_JSVM_CoerceToNumber(env, args[0], &number);
7569 return number;
7570 }
7571
CoerceToObject(JSVM_Env env,JSVM_CallbackInfo info)7572 static JSVM_Value CoerceToObject(JSVM_Env env, JSVM_CallbackInfo info)
7573 {
7574 strcpy_s(g_dataType, sizeof(g_dataType), "object");
7575 size_t argc = 1;
7576 JSVM_Value args[1] = {nullptr};
7577 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7578 JSVM_Value obj = nullptr;
7579 JSVM_Status status = OH_JSVM_CoerceToObject(env, args[0], &obj);
7580 if (status != JSVM_OK) {
7581 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CoerceToObject fail");
7582 }
7583 bool isCoerce = true;
7584 JSVM_Value result;
7585 OH_JSVM_GetBoolean(env, isCoerce, &result);
7586 return result;
7587 }
7588
CoerceToString(JSVM_Env env,JSVM_CallbackInfo info)7589 static JSVM_Value CoerceToString(JSVM_Env env, JSVM_CallbackInfo info)
7590 {
7591 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7592 size_t argc = 1;
7593 JSVM_Value args[1] = {nullptr};
7594 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7595 JSVM_Value str = nullptr;
7596 OH_JSVM_CoerceToString(env, args[0], &str);
7597 return str;
7598 }
7599
CoerceToBool(JSVM_Env env,JSVM_CallbackInfo info)7600 static JSVM_Value CoerceToBool(JSVM_Env env, JSVM_CallbackInfo info)
7601 {
7602 size_t argc = 1;
7603 JSVM_Value args[1] = {nullptr};
7604 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7605 JSVM_Value boolean = nullptr;
7606 OH_JSVM_CoerceToBool(env, args[0], &boolean);
7607 return boolean;
7608 }
7609
IsPromise(JSVM_Env env,JSVM_CallbackInfo info)7610 static JSVM_Value IsPromise(JSVM_Env env, JSVM_CallbackInfo info)
7611 {
7612 size_t argc = 1;
7613 JSVM_Value args[1] = {nullptr};
7614 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7615 bool isPromise = false;
7616 OH_JSVM_IsPromise(env, args[0], &isPromise);
7617 JSVM_Value result = nullptr;
7618 OH_JSVM_GetBoolean(env, isPromise, &result);
7619 return result;
7620 }
7621
CreatePromise(JSVM_Env env,JSVM_CallbackInfo info)7622 static JSVM_Value CreatePromise(JSVM_Env env, JSVM_CallbackInfo info)
7623 {
7624 JSVM_Deferred defer;
7625 JSVM_Value promise = nullptr;
7626 OH_JSVM_CreatePromise(env, &defer, &promise);
7627 bool isPromise = false;
7628 JSVM_Value returnIsPromise = nullptr;
7629 OH_JSVM_IsPromise(env, promise, &isPromise);
7630 OH_JSVM_GetBoolean(env, isPromise, &returnIsPromise);
7631 return returnIsPromise;
7632 }
7633
ResolveRejectDeferred(JSVM_Env env,JSVM_CallbackInfo info)7634 static JSVM_Value ResolveRejectDeferred(JSVM_Env env, JSVM_CallbackInfo info)
7635 {
7636 size_t argc = DIFF_VALUE_THREE;
7637 JSVM_Value args[3] = {nullptr};
7638 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7639 bool status = false;
7640 OH_JSVM_GetValueBool(env, args[DIFF_VALUE_TWO], &status);
7641
7642 JSVM_Deferred deferred = nullptr;
7643 JSVM_Value promise = nullptr;
7644 JSVM_Status createStatus = OH_JSVM_CreatePromise(env, &deferred, &promise);
7645 if (createStatus != JSVM_OK) {
7646 OH_JSVM_ThrowError(env, nullptr, "Create promise failed");
7647 }
7648 if (status) {
7649 OH_JSVM_ResolveDeferred(env, deferred, args[0]);
7650 } else {
7651 OH_JSVM_RejectDeferred(env, deferred, args[1]);
7652 }
7653 JSVM_Value result = nullptr;
7654 OH_JSVM_GetBoolean(env, true, &result);
7655 return result;
7656 }
7657
GetCbArgs(JSVM_Env env,JSVM_CallbackInfo info)7658 static JSVM_Value GetCbArgs(JSVM_Env env, JSVM_CallbackInfo info)
7659 {
7660 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7661 size_t argc = 1;
7662 JSVM_Value args[1] = {nullptr};
7663 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7664 return args[0];
7665 }
7666
NewInstance(JSVM_Env env,JSVM_CallbackInfo info)7667 static JSVM_Value NewInstance(JSVM_Env env, JSVM_CallbackInfo info)
7668 {
7669 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7670 size_t argc = 2;
7671 JSVM_Value args[2] = {nullptr};
7672 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7673 JSVM_Value result = nullptr;
7674 OH_JSVM_NewInstance(env, args[0], 1, &args[1], &result);
7675 return result;
7676 }
7677
7678 #define NUMBERINT_FOUR 4
7679 static const JSVM_TypeTag TagsData[NUMBERINT_FOUR] = {
7680 {0x9e4b2449547061b3, 0x33999f8a6516c499},
7681 {0x1d55a794c53a726d, 0x43633f509f9c944e},
7682 {0, 0},
7683 {0x6a971439f5b2e5d7, 0x531dc28a7e5317c0},
7684 };
7685
SetTypeTagToObject(JSVM_Env env,JSVM_CallbackInfo info)7686 static JSVM_Value SetTypeTagToObject(JSVM_Env env, JSVM_CallbackInfo info)
7687 {
7688 size_t argc = 2;
7689 JSVM_Value args[2] = {nullptr};
7690 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7691 int32_t index = 0;
7692 OH_JSVM_GetValueInt32(env, args[1], &index);
7693
7694 JSVM_Status status = OH_JSVM_TypeTagObject(env, args[0], &TagsData[index]);
7695 if (status != JSVM_OK) {
7696 OH_JSVM_ThrowError(env, "Reconnect error", "OH_JSVM_TypeTagObject failed");
7697 }
7698 JSVM_Value result = nullptr;
7699 OH_JSVM_GetBoolean(env, true, &result);
7700 return result;
7701 }
7702
CheckObjectTypeTag(JSVM_Env env,JSVM_CallbackInfo info)7703 static JSVM_Value CheckObjectTypeTag(JSVM_Env env, JSVM_CallbackInfo info)
7704 {
7705 size_t argc = 2;
7706 JSVM_Value args[2] = {nullptr};
7707 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7708 int32_t index = 0;
7709 OH_JSVM_GetValueInt32(env, args[1], &index);
7710 bool checkResult = false;
7711 OH_JSVM_CheckObjectTypeTag(env, args[0], &TagsData[index], &checkResult);
7712 JSVM_Value checked = nullptr;
7713 OH_JSVM_GetBoolean(env, checkResult, &checked);
7714 return checked;
7715 }
7716
7717 static const int64_t MEMORY_SIZE = 1024;
AdjustExternalMemory(JSVM_Env env,JSVM_CallbackInfo info)7718 static JSVM_Value AdjustExternalMemory(JSVM_Env env, JSVM_CallbackInfo info)
7719 {
7720 int64_t change = MEMORY_SIZE * MEMORY_SIZE;
7721 int64_t adjustedValue;
7722 OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue);
7723 JSVM_Value checked = nullptr;
7724 OH_JSVM_GetBoolean(env, true, &checked);
7725 return checked;
7726 }
7727
MemoryPressureNotification(JSVM_Env env,JSVM_CallbackInfo info)7728 static JSVM_Value MemoryPressureNotification(JSVM_Env env, JSVM_CallbackInfo info)
7729 {
7730 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7731 OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
7732 JSVM_Value level = nullptr;
7733 OH_JSVM_CreateInt32(env, 0, &level);
7734 return level;
7735 }
7736
JsVmThrow(JSVM_Env env,JSVM_CallbackInfo info)7737 static JSVM_Value JsVmThrow(JSVM_Env env, JSVM_CallbackInfo info)
7738 {
7739 size_t argc = 1;
7740 JSVM_Value argv[1] = {nullptr};
7741 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7742 JSVM_Value errorCode = nullptr;
7743 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7744 JSVM_Value errorMessage = nullptr;
7745 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7746 JSVM_Value error = nullptr;
7747 OH_JSVM_CreateError(env, errorCode, errorMessage, &error);
7748 OH_JSVM_Throw(env, error);
7749 return nullptr;
7750 }
7751
JsVmThrowFromJS(JSVM_Env env,JSVM_CallbackInfo info)7752 static JSVM_Value JsVmThrowFromJS(JSVM_Env env, JSVM_CallbackInfo info)
7753 {
7754 size_t argc = 1;
7755 JSVM_Value argv[1];
7756 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7757 JSVM_Value jsError = argv[0];
7758 bool result = false;
7759 OH_JSVM_IsError(env, jsError, &result);
7760 if (!result) {
7761 JSVM_Value errorCode;
7762 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7763 JSVM_Value errorMessage;
7764 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7765 OH_JSVM_CreateError(env, errorCode, errorMessage, &jsError);
7766 }
7767 OH_JSVM_Throw(env, jsError);
7768 return nullptr;
7769 }
7770
JsVmThrowError(JSVM_Env env,JSVM_CallbackInfo info)7771 static JSVM_Value JsVmThrowError(JSVM_Env env, JSVM_CallbackInfo info)
7772 {
7773 size_t argc = 1;
7774 JSVM_Value argv[1];
7775 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7776 if (argc == 0) {
7777 OH_JSVM_ThrowError(env, "-1", "has Error");
7778 } else if (argc == 1) {
7779 size_t length;
7780 OH_JSVM_GetValueStringUtf8(env, argv[0], nullptr, 0, &length);
7781 char *buffer = new char[length + 1];
7782 OH_JSVM_GetValueStringUtf8(env, argv[0], buffer, length + 1, nullptr);
7783 OH_JSVM_ThrowError(env, nullptr, buffer);
7784 delete[] buffer;
7785 }
7786 return nullptr;
7787 }
7788
JsVmThrowTypeError(JSVM_Env env,JSVM_CallbackInfo info)7789 static JSVM_Value JsVmThrowTypeError(JSVM_Env env, JSVM_CallbackInfo info)
7790 {
7791 size_t argc = 1;
7792 JSVM_Value argv[1];
7793 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7794 if (argc == 0) {
7795 OH_JSVM_ThrowTypeError(env, "-1", "throwing type error");
7796 } else if (argc == 1) {
7797 size_t length;
7798 OH_JSVM_GetValueStringUtf8(env, argv[0], nullptr, 0, &length);
7799 char *buffer = new char[length + 1];
7800 OH_JSVM_GetValueStringUtf8(env, argv[0], buffer, length + 1, nullptr);
7801 OH_JSVM_ThrowTypeError(env, nullptr, buffer);
7802 delete[] buffer;
7803 }
7804 return nullptr;
7805 }
7806
JsVmThrowRangeError(JSVM_Env env,JSVM_CallbackInfo info)7807 static JSVM_Value JsVmThrowRangeError(JSVM_Env env, JSVM_CallbackInfo info)
7808 {
7809 size_t argc = 2;
7810 JSVM_Value argv[2];
7811 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7812 if (argc != DIFF_VALUE_TWO) {
7813 OH_JSVM_ThrowRangeError(env, "OH_JSVM_ThrowRangeError", "Expected two numbers as arguments");
7814 }
7815 JSVM_Value result;
7816 OH_JSVM_GetBoolean(env, true, &result);
7817 return result;
7818 }
7819
JsVmThrowSyntaxError(JSVM_Env env,JSVM_CallbackInfo info)7820 static JSVM_Value JsVmThrowSyntaxError(JSVM_Env env, JSVM_CallbackInfo info)
7821 {
7822 OH_JSVM_ThrowSyntaxError(env, "JsVmThrowSyntaxError", "throw syntax error");
7823 return nullptr;
7824 }
7825
JsVmIsError(JSVM_Env env,JSVM_CallbackInfo info)7826 static JSVM_Value JsVmIsError(JSVM_Env env, JSVM_CallbackInfo info)
7827 {
7828 size_t argc = 1;
7829 JSVM_Value args[1] = {nullptr};
7830 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7831 bool result = false;
7832 OH_JSVM_IsError(env, args[0], &result);
7833 JSVM_Value returnValue;
7834 OH_JSVM_GetBoolean(env, result, &returnValue);
7835 return returnValue;
7836 }
7837
JsVmCreateError(JSVM_Env env,JSVM_CallbackInfo info)7838 static JSVM_Value JsVmCreateError(JSVM_Env env, JSVM_CallbackInfo info)
7839 {
7840 JSVM_Value errorCode;
7841
7842 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7843 JSVM_Value errorMessage;
7844 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7845 JSVM_Value result;
7846 OH_JSVM_CreateError(env, errorCode, errorMessage, &result);
7847 return result;
7848 }
7849
JsVmCreateTypeError(JSVM_Env env,JSVM_CallbackInfo info)7850 static JSVM_Value JsVmCreateTypeError(JSVM_Env env, JSVM_CallbackInfo info)
7851 {
7852 JSVM_Value errorCode;
7853 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7854 JSVM_Value errorMessage;
7855 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7856 JSVM_Value result;
7857 OH_JSVM_CreateTypeError(env, errorCode, errorMessage, &result);
7858 return result;
7859 }
7860
JsVmCreateRangeError(JSVM_Env env,JSVM_CallbackInfo info)7861 static JSVM_Value JsVmCreateRangeError(JSVM_Env env, JSVM_CallbackInfo info)
7862 {
7863 JSVM_Value errorCode;
7864 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7865 JSVM_Value errorMessage;
7866 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7867 JSVM_Value result;
7868 OH_JSVM_CreateRangeError(env, errorCode, errorMessage, &result);
7869 return result;
7870 }
7871
JsVmCreateSyntaxError(JSVM_Env env,JSVM_CallbackInfo info)7872 static JSVM_Value JsVmCreateSyntaxError(JSVM_Env env, JSVM_CallbackInfo info)
7873 {
7874 JSVM_Value errorCode;
7875 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7876 JSVM_Value errorMessage;
7877 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7878 JSVM_Value result;
7879 OH_JSVM_CreateSyntaxError(env, errorCode, errorMessage, &result);
7880 return result;
7881 }
7882
JsVmGetAndClearLastException(JSVM_Env env,JSVM_CallbackInfo info)7883 static JSVM_Value JsVmGetAndClearLastException(JSVM_Env env, JSVM_CallbackInfo info)
7884 {
7885 OH_JSVM_ThrowError(env, "OH_JSVM_ThrowError", "OH_JSVM_ThrowError");
7886 JSVM_Value result = nullptr;
7887 JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result);
7888 if (status != JSVM_OK) {
7889 return nullptr;
7890 }
7891 return result;
7892 }
7893
JsVmIsExceptionPending(JSVM_Env env,JSVM_CallbackInfo info)7894 static JSVM_Value JsVmIsExceptionPending(JSVM_Env env, JSVM_CallbackInfo info)
7895 {
7896 JSVM_Status status;
7897 bool isExceptionPending = false;
7898 OH_JSVM_ThrowError(env, "OH_JSVM_ThrowError", "OH_JSVM_ThrowError");
7899 status = OH_JSVM_IsExceptionPending(env, &isExceptionPending);
7900 if (status != JSVM_OK) {
7901 return nullptr;
7902 }
7903 if (isExceptionPending) {
7904 JSVM_Value result = nullptr;
7905 status = OH_JSVM_GetAndClearLastException(env, &result);
7906 if (status != JSVM_OK) {
7907 return nullptr;
7908 }
7909 return result;
7910 }
7911 return nullptr;
7912 }
7913
JsVmGetLastErrorInfo(JSVM_Env env,JSVM_CallbackInfo info)7914 static JSVM_Value JsVmGetLastErrorInfo(JSVM_Env env, JSVM_CallbackInfo info)
7915 {
7916 size_t argc = 1;
7917 JSVM_Value args[1] = {nullptr};
7918 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7919 int32_t value = 0;
7920 JSVM_Status status = OH_JSVM_GetValueInt32(env, args[0], &value);
7921 JSVM_ASSERT(env, status != JSVM_Status::JSVM_OK, "FAILED to produce error condition");
7922 const JSVM_ExtendedErrorInfo *errorInfo;
7923 OH_JSVM_GetLastErrorInfo(env, &errorInfo);
7924 JSVM_Value result;
7925 OH_JSVM_CreateInt32(env, DIFF_VALUE_NOE, &result);
7926 return result;
7927 }
7928
7929 struct Object {
7930 char name[DIFF_VALUE_SIX];
7931 int32_t age;
7932 };
7933
7934 struct Object *g_obj = (struct Object *)malloc(sizeof(struct Object));
7935
DerekItem(JSVM_Env env,void * data,void * hint)7936 static void DerekItem(JSVM_Env env, void *data, void *hint)
7937 {
7938 (void)hint;
7939 }
7940
WrapObject(JSVM_Env env,JSVM_CallbackInfo info)7941 static JSVM_Value WrapObject(JSVM_Env env, JSVM_CallbackInfo info)
7942 {
7943 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7944
7945 strcpy_s(g_obj->name, sizeof(g_obj->name), "lilei");
7946 g_obj->age = METHOD_NUM;
7947 size_t argc = 1;
7948 JSVM_Value toWrap = nullptr;
7949 OH_JSVM_GetCbInfo(env, info, &argc, &toWrap, nullptr, nullptr);
7950 OH_JSVM_Wrap(env, toWrap, reinterpret_cast<void *>(g_obj), DerekItem, nullptr, nullptr);
7951 struct Object *data;
7952 struct Object *data1;
7953 OH_JSVM_Unwrap(env, toWrap, reinterpret_cast<void **>(&data));
7954 OH_JSVM_RemoveWrap(env, toWrap, reinterpret_cast<void **>(&g_obj));
7955 OH_JSVM_Unwrap(env, toWrap, reinterpret_cast<void **>(&data1));
7956 JSVM_Value checked = nullptr;
7957 OH_JSVM_GetBoolean(env, true, &checked);
7958 return checked;
7959 }
7960
RemoveWrap(JSVM_Env env,JSVM_CallbackInfo info)7961 static JSVM_Value RemoveWrap(JSVM_Env env, JSVM_CallbackInfo info)
7962 {
7963 size_t argc = 1;
7964 JSVM_Value wrapped = nullptr;
7965 OH_JSVM_GetCbInfo(env, info, &argc, &wrapped, nullptr, nullptr);
7966 void *data;
7967 OH_JSVM_RemoveWrap(env, wrapped, &data);
7968 JSVM_Value checked = nullptr;
7969 OH_JSVM_GetBoolean(env, true, &checked);
7970 return checked;
7971 }
7972
HandleScopeFor(JSVM_Env env,JSVM_CallbackInfo info)7973 static JSVM_Value HandleScopeFor(JSVM_Env env, JSVM_CallbackInfo info)
7974 {
7975 JSVM_Value checked = nullptr;
7976 for (int i = 0; i < SCOPE_FOR_NUMBER; i++) {
7977 JSVM_HandleScope scope = nullptr;
7978 OH_JSVM_OpenHandleScope(env, &scope);
7979 if (scope == nullptr) {
7980 OH_JSVM_GetBoolean(env, false, &checked);
7981 return checked;
7982 }
7983 JSVM_Value res = nullptr;
7984 OH_JSVM_CreateObject(env, &res);
7985 OH_JSVM_CloseHandleScope(env, scope);
7986 }
7987 OH_JSVM_GetBoolean(env, true, &checked);
7988 return checked;
7989 }
7990
7991
7992 struct DefineObject {
7993 char name[DIFF_VALUE_SIX];
7994 JSVM_Ref wrapper_;
7995 };
7996 static thread_local JSVM_Ref g_ref = nullptr;
7997 struct DefineObject *g_defineObject = (struct DefineObject *)malloc(sizeof(struct DefineObject));
New(JSVM_Env env,JSVM_CallbackInfo info)7998 JSVM_Value New(JSVM_Env env, JSVM_CallbackInfo info)
7999 {
8000 strcpy_s(g_dataType, sizeof(g_dataType), "Class");
8001 JSVM_Value newTarget;
8002 OH_JSVM_GetNewTarget(env, info, &newTarget);
8003 if (newTarget != nullptr) {
8004 size_t argc = 1;
8005 JSVM_Value args[1];
8006 JSVM_Value jsThis;
8007 OH_JSVM_GetCbInfo(env, info, &argc, args, &jsThis, nullptr);
8008 double value = 0.0;
8009 JSVM_ValueType valuetype;
8010 OH_JSVM_Typeof(env, args[0], &valuetype);
8011 if (valuetype != JSVM_UNDEFINED) {
8012 OH_JSVM_GetValueDouble(env, args[0], &value);
8013 }
8014
8015 strcpy_s(g_defineObject->name, sizeof(g_defineObject->name), "lilei");
8016 return nullptr;
8017 } else {
8018 size_t argc = 1;
8019 JSVM_Value args[1];
8020 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8021 JSVM_Value cons;
8022 OH_JSVM_GetReferenceValue(env, g_ref, &cons);
8023 JSVM_Value instance;
8024 OH_JSVM_NewInstance(env, cons, argc, args, &instance);
8025 return instance;
8026 }
8027 }
8028
GetObj(napi_env env)8029 napi_value GetObj(napi_env env)
8030 {
8031 size_t strLen = sizeof("{\"name\": \"") - 1 + strlen(g_defineObject->name) + sizeof("\"}") - 1;
8032 char *str = (char *)malloc(strLen + 1);
8033 if (str == nullptr) {
8034 printf("Memory allocation failed!\n");
8035 return nullptr;
8036 }
8037 int result = sprintf_s(str, strLen + 1, "{\"name\": \"%s\"}", g_defineObject->name);
8038 if (result < 0 || result >= strLen + 1) {
8039 return nullptr;
8040 }
8041 napi_value jsResult;
8042 napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &jsResult);
8043 return jsResult;
8044 }
8045
DefineClass(JSVM_Env env,JSVM_Value exports)8046 JSVM_Value DefineClass(JSVM_Env env, JSVM_Value exports)
8047 {
8048 JSVM_CallbackStruct param1;
8049 param1.data = nullptr;
8050 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value { return New(env, info); };
8051 JSVM_Value cons;
8052 OH_JSVM_DefineClass(env, "MyObject", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &cons);
8053 JSVM_Value instanceValue = nullptr;
8054 OH_JSVM_NewInstance(env, cons, 0, nullptr, &instanceValue);
8055 return nullptr;
8056 }
8057
PrintHello(JSVM_Env env,JSVM_CallbackInfo info)8058 static JSVM_Value PrintHello(JSVM_Env env, JSVM_CallbackInfo info)
8059 {
8060 JSVM_Value output;
8061 OH_JSVM_CreateStringUtf8(env, "Hello world!", JSVM_AUTO_LENGTH, &output);
8062 return output;
8063 }
8064 static JSVM_CallbackStruct helloCb = { PrintHello, nullptr };
8065 static intptr_t g_externalsSnapshot[] = {
8066 (intptr_t)&helloCb,
8067 0,
8068 };
8069
CreateSnapshotFailed(JSVM_Env oldEnv,JSVM_CallbackInfo info)8070 static JSVM_Value CreateSnapshotFailed(JSVM_Env oldEnv, JSVM_CallbackInfo info)
8071 {
8072 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
8073 JSVM_VM vm;
8074 JSVM_CreateVMOptions vmOptions;
8075 if (memset_s(&vmOptions, sizeof(vmOptions), 0, sizeof(vmOptions)) != EOK) {
8076 printf("memset_s failed");
8077 return nullptr;
8078 }
8079 vmOptions.isForSnapshotting = false;
8080 OH_JSVM_CreateVM(&vmOptions, &vm);
8081 JSVM_Env env;
8082 JSVM_PropertyDescriptor descriptor[] = {
8083 {"printHello", nullptr, &helloCb, nullptr, nullptr, nullptr, JSVM_DEFAULT},
8084 };
8085 OH_JSVM_CreateEnv(vm, 1, descriptor, &env);
8086 const char *blobData = nullptr;
8087 size_t blobSize = 0;
8088 JSVM_Env envs[1] = {env};
8089 JSVM_Status status = OH_JSVM_CreateSnapshot(vm, 1, envs, &blobData, &blobSize);
8090 JSVM_Value error = nullptr;
8091 if (status != JSVM_OK) {
8092 OH_JSVM_CreateStringUtf8(oldEnv, "generic failure status.", JSVM_AUTO_LENGTH, &error);
8093 return error;
8094 }
8095 return nullptr;
8096 }
8097
8098
WriteDataToStream(const char * data,int size,void * streamData)8099 static bool WriteDataToStream(const char *data, int size, void *streamData)
8100 {
8101 FILE *file = (FILE *)streamData;
8102 if (data) {
8103 fwrite(data, sizeof(char), size, file);
8104 } else {
8105 fclose(file);
8106 }
8107 return true;
8108 }
8109
StartCpuProfiler(JSVM_VM vm,JSVM_CpuProfiler * cpuProfiler)8110 static void StartCpuProfiler(JSVM_VM vm, JSVM_CpuProfiler *cpuProfiler)
8111 {
8112 FILE *startHeapFile = fopen("/data/storage/el2/base/files/heapsnapshot-start.heapsnapshot", "wb");
8113 if (startHeapFile == nullptr) {
8114 printf("Error opening file!\n");
8115 return;
8116 }
8117 OH_JSVM_TakeHeapSnapshot(vm, WriteDataToStream, startHeapFile);
8118 OH_JSVM_StartCpuProfiler(vm, cpuProfiler);
8119 }
8120
StopCpuProfiler(JSVM_VM vm,JSVM_CpuProfiler cpuProfiler)8121 static void StopCpuProfiler(JSVM_VM vm, JSVM_CpuProfiler cpuProfiler)
8122 {
8123 FILE *cpFile = fopen("/data/storage/el2/base/files/cpu-profile.cpuprofile", "wb");
8124 if (cpFile == nullptr) {
8125 printf("Error opening file!\n");
8126 return;
8127 }
8128 OH_JSVM_StopCpuProfiler(vm, cpuProfiler, WriteDataToStream, cpFile);
8129 FILE *stopHeapFile = fopen("/data/storage/el2/base/files/heapsnapshot-stop.heapsnapshot", "wb");
8130 if (stopHeapFile == nullptr) {
8131 printf("Error opening file!\n");
8132 return;
8133 }
8134 OH_JSVM_TakeHeapSnapshot(vm, WriteDataToStream, stopHeapFile);
8135 }
8136
8137 static const char *SCRIPT_TO_DEBUG = R"JS(
8138 function factorialRecursive(n) {
8139 if (n === 0 || n === 1) {
8140 return 1;
8141 } else {
8142 return n * factorialRecursive(n - 1);
8143 }
8144 }
8145
8146 function calculateFactorial() {
8147 var number = 10;
8148 var result = factorialRecursive(number);
8149 return result;
8150 }
8151
8152 calculateFactorial();
8153 )JS";
8154
DebugCodeUsingCpuProfiler(JSVM_Env oldEnv,JSVM_CallbackInfo info)8155 static JSVM_Value DebugCodeUsingCpuProfiler(JSVM_Env oldEnv, JSVM_CallbackInfo info)
8156 {
8157 strcpy_s(g_dataType, sizeof(g_dataType), "int");
8158 JSVM_VM testVm;
8159 JSVM_CreateVMOptions vmOptions;
8160 if (memset_s(&vmOptions, sizeof(vmOptions), 0, sizeof(vmOptions)) != EOK) {
8161 printf("memset_s failed");
8162 return nullptr;
8163 }
8164 OH_JSVM_CreateVM(&vmOptions, &testVm);
8165 JSVM_VMScope vmScope;
8166 OH_JSVM_OpenVMScope(testVm, &vmScope);
8167 JSVM_Env env;
8168 OH_JSVM_CreateEnv(testVm, 0, nullptr, &env);
8169 JSVM_EnvScope envScope;
8170 OH_JSVM_OpenEnvScope(env, &envScope);
8171
8172 JSVM_CpuProfiler cpuProfiler;
8173 StartCpuProfiler(testVm, &cpuProfiler);
8174
8175 JSVM_HandleScope handleScope;
8176 OH_JSVM_OpenHandleScope(env, &handleScope);
8177 JSVM_Value strValue = nullptr;
8178 OH_JSVM_CreateStringUtf8(env, SCRIPT_TO_DEBUG, strlen(SCRIPT_TO_DEBUG), &strValue);
8179 JSVM_Script script;
8180 OH_JSVM_CompileScript(env, strValue, nullptr, 0, true, nullptr, &script);
8181 JSVM_Value result = nullptr;
8182 OH_JSVM_RunScript(env, script, &result);
8183 OH_JSVM_CloseHandleScope(env, handleScope);
8184
8185 StopCpuProfiler(testVm, cpuProfiler);
8186
8187 OH_JSVM_CloseEnvScope(env, envScope);
8188 OH_JSVM_CloseVMScope(testVm, vmScope);
8189 OH_JSVM_DestroyEnv(env);
8190 OH_JSVM_DestroyVM(testVm);
8191
8192 JSVM_Value ret1 = nullptr;
8193 OH_JSVM_CreateInt32(oldEnv, 0, &ret1);
8194 return ret1;
8195 }
8196
TakeHeapSnapshot(JSVM_Env oldEnv,JSVM_CallbackInfo info)8197 static JSVM_Value TakeHeapSnapshot(JSVM_Env oldEnv, JSVM_CallbackInfo info)
8198 {
8199 strcpy_s(g_dataType, sizeof(g_dataType), "int");
8200 JSVM_VM testVm;
8201 JSVM_CreateVMOptions vmOptions;
8202 if (memset_s(&vmOptions, sizeof(vmOptions), 0, sizeof(vmOptions)) != EOK) {
8203 printf("memset_s failed");
8204 return nullptr;
8205 }
8206 OH_JSVM_CreateVM(&vmOptions, &testVm);
8207 JSVM_VMScope vmScope;
8208 OH_JSVM_OpenVMScope(testVm, &vmScope);
8209
8210 FILE *file = fopen("/data/storage/el2/base/files/heapsnapshot-test.heapsnapshot", "wb");
8211 if (file == nullptr) {
8212 printf("Error opening file!\n");
8213 return nullptr;
8214 }
8215 OH_JSVM_TakeHeapSnapshot(testVm, WriteDataToStream, file);
8216
8217 OH_JSVM_CloseVMScope(testVm, vmScope);
8218 OH_JSVM_DestroyVM(testVm);
8219
8220 JSVM_Value result = nullptr;
8221 OH_JSVM_CreateInt32(oldEnv, 0, &result);
8222 return result;
8223 }
8224
8225
WaitForDebuggerFailed(JSVM_Env env,JSVM_CallbackInfo info)8226 static JSVM_Value WaitForDebuggerFailed(JSVM_Env env, JSVM_CallbackInfo info)
8227 {
8228 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
8229 JSVM_Value returnValue = nullptr;
8230 OH_JSVM_CreateObject(env, &returnValue);
8231 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
8232 OH_JSVM_WaitForDebugger(nullptr, true));
8233 OH_JSVM_WaitForDebugger(env, true);
8234 AddLastStatus(env, "notConnected", returnValue);
8235 return returnValue;
8236 }
8237
8238 static const int PORT = 9225;
OpenInspector(JSVM_Env env)8239 static void OpenInspector(JSVM_Env env)
8240 {
8241 OH_JSVM_OpenInspector(env, "localhost", PORT);
8242 }
CloseInspector(JSVM_Env env)8243 static void CloseInspector(JSVM_Env env)
8244 {
8245 OH_JSVM_CloseInspector(env);
8246 }
8247
DebugCodeUsingInspector(JSVM_Env oldEnv,JSVM_CallbackInfo info)8248 static JSVM_Value DebugCodeUsingInspector(JSVM_Env oldEnv, JSVM_CallbackInfo info)
8249 {
8250 JSVM_VM testVm;
8251 JSVM_CreateVMOptions vmOptions;
8252 if (memset_s(&vmOptions, sizeof(vmOptions), 0, sizeof(vmOptions)) != EOK) {
8253 printf("memset_s failed");
8254 return nullptr;
8255 }
8256 OH_JSVM_CreateVM(&vmOptions, &testVm);
8257 JSVM_VMScope vmScope;
8258 OH_JSVM_OpenVMScope(testVm, &vmScope);
8259 JSVM_Env env;
8260 OH_JSVM_CreateEnv(testVm, 0, nullptr, &env);
8261
8262 OpenInspector(env);
8263
8264 JSVM_EnvScope envScope;
8265 OH_JSVM_OpenEnvScope(env, &envScope);
8266 JSVM_HandleScope handleScope;
8267 OH_JSVM_OpenHandleScope(env, &handleScope);
8268 JSVM_Value strValue = nullptr;
8269 OH_JSVM_CreateStringUtf8(env, SCRIPT_TO_DEBUG, strlen(SCRIPT_TO_DEBUG), &strValue);
8270 JSVM_Script script;
8271 OH_JSVM_CompileScript(env, strValue, nullptr, 0, true, nullptr, &script);
8272 JSVM_Value result = nullptr;
8273 OH_JSVM_RunScript(env, script, &result);
8274
8275 OH_JSVM_CloseHandleScope(env, handleScope);
8276 OH_JSVM_CloseEnvScope(env, envScope);
8277 CloseInspector(env);
8278 OH_JSVM_CloseVMScope(testVm, vmScope);
8279 OH_JSVM_DestroyEnv(env);
8280 OH_JSVM_DestroyVM(testVm);
8281
8282 strcpy_s(g_dataType, sizeof(g_dataType), "int");
8283 JSVM_Value ret1 = nullptr;
8284 OH_JSVM_CreateInt32(oldEnv, 0, &ret1);
8285 return ret1;
8286 }
8287
8288 struct InstanceData {
8289 int32_t value;
8290 };
8291
InstanceFinalizeCallback(JSVM_Env env,void * finalizeData,void * finalizeHint)8292 void InstanceFinalizeCallback(JSVM_Env env, void *finalizeData, void *finalizeHint)
8293 {
8294 if (finalizeData) {
8295 InstanceData *data = reinterpret_cast<InstanceData *>(finalizeData);
8296 free(data);
8297 }
8298 }
8299
8300 // If yes, true is returned. If no, false is returned.
SetInstanceData(JSVM_Env env,JSVM_CallbackInfo info)8301 static JSVM_Value SetInstanceData(JSVM_Env env, JSVM_CallbackInfo info)
8302 {
8303 size_t argc = 1;
8304 JSVM_Value argv[1];
8305 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
8306 uint32_t instanceDataValue;
8307 OH_JSVM_GetValueUint32(env, argv[0], &instanceDataValue);
8308 InstanceData *instanceData = reinterpret_cast<InstanceData *>(malloc(sizeof(InstanceData)));
8309 if (instanceData == nullptr) {
8310 JSVM_Value result1;
8311 OH_JSVM_GetBoolean(env, false, &result1);
8312 return result1;
8313 }
8314 instanceData->value = instanceDataValue;
8315 JSVM_Status status = OH_JSVM_SetInstanceData(env, instanceData, InstanceFinalizeCallback, nullptr);
8316 bool setResult = status == JSVM_OK;
8317 JSVM_Value result;
8318 OH_JSVM_GetBoolean(env, setResult, &result);
8319 return result;
8320 }
8321
8322 // If yes, true is returned. If no, false is returned.
GetInstanceData(JSVM_Env env,JSVM_CallbackInfo info)8323 static JSVM_Value GetInstanceData(JSVM_Env env, JSVM_CallbackInfo info)
8324 {
8325 JSVM_Value result;
8326 InstanceData *instanceData = reinterpret_cast<InstanceData *>(malloc(sizeof(InstanceData)));
8327 if (instanceData == nullptr) {
8328 OH_JSVM_GetBoolean(env, false, &result);
8329 return result;
8330 }
8331 instanceData->value = DIFF_VALUE_FIVE;
8332 OH_JSVM_SetInstanceData(env, instanceData, InstanceFinalizeCallback, nullptr);
8333 InstanceData *resData = nullptr;
8334 JSVM_Status stat1 = OH_JSVM_GetInstanceData(env, (void **)&resData);
8335 bool setResult = stat1 == JSVM_OK;
8336 if (setResult == false) {
8337 OH_JSVM_GetBoolean(env, setResult, &result);
8338 return result;
8339 }
8340 setResult = resData->value == DIFF_VALUE_FIVE;
8341 if (setResult == false) {
8342 OH_JSVM_GetBoolean(env, setResult, &result);
8343 return result;
8344 }
8345 InstanceData *resData2 = nullptr;
8346 OH_JSVM_GetInstanceData(env, (void **)&resData2);
8347 setResult = resData2 == resData;
8348 OH_JSVM_GetBoolean(env, setResult, &result);
8349 return result;
8350 }
8351
IsCallable(JSVM_Env env,JSVM_CallbackInfo info)8352 static JSVM_Value IsCallable(JSVM_Env env, JSVM_CallbackInfo info)
8353 {
8354 JSVM_Value value;
8355 JSVM_Value rst;
8356 size_t argc = 1;
8357 bool isCallable = false;
8358 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, &value, NULL, NULL));
8359 JSVM_CALL(env, OH_JSVM_IsCallable(env, value, &isCallable));
8360 OH_JSVM_GetBoolean(env, isCallable, &rst);
8361 return rst;
8362 }
8363
testIsCallable(napi_env env1,napi_callback_info info)8364 static napi_value testIsCallable(napi_env env1, napi_callback_info info)
8365 {
8366 JSVM_InitOptions init_options;
8367 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8368 printf("memset_s failed");
8369 return nullptr;
8370 }
8371 init_options.externalReferences = externals;
8372 if (aa == 0) {
8373 OH_JSVM_Init(&init_options);
8374 aa++;
8375 }
8376 JSVM_VM vm;
8377 JSVM_CreateVMOptions options;
8378 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8379 printf("memset_s failed");
8380 return nullptr;
8381 }
8382 OH_JSVM_CreateVM(&options, &vm);
8383 JSVM_VMScope vm_scope;
8384 OH_JSVM_OpenVMScope(vm, &vm_scope);
8385 JSVM_CallbackStruct param[] = {{.callback = IsCallable, .data = nullptr}};
8386 JSVM_PropertyDescriptor descriptor[] = {{"isCallable", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT}};
8387 JSVM_Env env;
8388 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
8389 JSVM_EnvScope envScope;
8390 OH_JSVM_OpenEnvScope(env, &envScope);
8391 JSVM_HandleScope handlescope;
8392 OH_JSVM_OpenHandleScope(env, &handlescope);
8393 const char* sourcecodestr = R"JS(
8394 function add() { return 0;} let rst = isCallable(add);
8395 )JS";
8396 JSVM_Value sourcecodevalue;
8397 OH_JSVM_CreateStringUtf8(env, sourcecodestr, strlen(sourcecodestr), &sourcecodevalue);
8398 JSVM_Script script;
8399 OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script);
8400 JSVM_Value result;
8401 OH_JSVM_RunScript(env, script, &result);
8402 OH_JSVM_CloseHandleScope(env, handlescope);
8403 OH_JSVM_CloseEnvScope(env, envScope);
8404 OH_JSVM_DestroyEnv(env);
8405 OH_JSVM_CloseVMScope(vm, vm_scope);
8406 OH_JSVM_DestroyVM(vm);
8407 napi_value result11;
8408 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
8409 return result11;
8410 }
8411
Thread1Func(JSVM_VM vm,JSVM_Env env)8412 void Thread1Func(JSVM_VM vm, JSVM_Env env)
8413 {
8414 bool isLocked = false;
8415 OH_JSVM_IsLocked(env, &isLocked);
8416 if (!isLocked) {
8417 OH_JSVM_AcquireLock(env);
8418 g_threadFlag1 = false;
8419 }
8420 JSVM_VMScope vmScope;
8421 OH_JSVM_OpenVMScope(vm, &vmScope);
8422 JSVM_EnvScope envScope;
8423 OH_JSVM_OpenEnvScope(env, &envScope);
8424 JSVM_HandleScope handleScope;
8425 OH_JSVM_OpenHandleScope(env, &handleScope);
8426 JSVM_Value value;
8427 OH_JSVM_CreateInt32(env, 32, &value); // 32: numerical value
8428 int32_t num1;
8429 OH_JSVM_GetValueInt32(env, value, &num1);
8430 OH_JSVM_CloseHandleScope(env, handleScope);
8431 OH_JSVM_CloseEnvScope(env, envScope);
8432 OH_JSVM_CloseVMScope(vm, vmScope);
8433 OH_JSVM_IsLocked(env, &isLocked);
8434 if (isLocked) {
8435 OH_JSVM_ReleaseLock(env);
8436 g_threadFlag1 = true;
8437 }
8438 }
8439
Thread2Func(JSVM_VM vm,JSVM_Env env)8440 void Thread2Func(JSVM_VM vm, JSVM_Env env)
8441 {
8442 bool isLocked = false;
8443 OH_JSVM_IsLocked(env, &isLocked);
8444 if (!isLocked) {
8445 OH_JSVM_AcquireLock(env);
8446 g_threadFlag2 = false;
8447 }
8448 JSVM_VMScope vmScope;
8449 OH_JSVM_OpenVMScope(vm, &vmScope);
8450 JSVM_EnvScope envScope;
8451 OH_JSVM_OpenEnvScope(env, &envScope);
8452 JSVM_HandleScope handleScope;
8453 OH_JSVM_OpenHandleScope(env, &handleScope);
8454 JSVM_Value value;
8455 OH_JSVM_CreateInt32(env, 32, &value); // 32: numerical value
8456 int32_t num1;
8457 OH_JSVM_GetValueInt32(env, value, &num1);
8458 OH_JSVM_CloseHandleScope(env, handleScope);
8459 OH_JSVM_CloseEnvScope(env, envScope);
8460 OH_JSVM_CloseVMScope(vm, vmScope);
8461 OH_JSVM_IsLocked(env, &isLocked);
8462 if (isLocked) {
8463 OH_JSVM_ReleaseLock(env);
8464 g_threadFlag2 = true;
8465 }
8466 }
8467
testMultithreadFunction(napi_env env1,napi_callback_info info)8468 static napi_value testMultithreadFunction(napi_env env1, napi_callback_info info)
8469 {
8470 JSVM_InitOptions init_options;
8471 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8472 printf("memset_s failed");
8473 return nullptr;
8474 }
8475 init_options.externalReferences = externals;
8476 if (aa == 0) {
8477 OH_JSVM_Init(&init_options);
8478 aa++;
8479 }
8480 JSVM_VM vm;
8481 JSVM_CreateVMOptions options;
8482 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8483 printf("memset_s failed");
8484 return nullptr;
8485 }
8486 OH_JSVM_CreateVM(&options, &vm);
8487 JSVM_Env env;
8488 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8489 Thread1Func(vm, env);
8490 Thread2Func(vm, env);
8491 OH_JSVM_DestroyEnv(env);
8492 OH_JSVM_DestroyVM(vm);
8493 napi_value result11;
8494 if (g_threadFlag1 && g_threadFlag2) {
8495 NAPI_CALL(env1, napi_create_int32(env1, 1, &result11));
8496 } else {
8497 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
8498 }
8499 return result11;
8500 }
8501
8502 static const char *STR_TASK = R"JS(
8503 WebAssembly.instantiate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 8, 2, 96, 1, 127, 0, 96, 0, 0, 2, 8, 1, 2, 106,
8504 115, 1, 95, 0, 0, 3, 2, 1, 1, 8, 1, 1, 10, 9, 1, 7, 0, 65, 185, 10, 16, 0, 11]),
8505 {js:{_:console.log("JSVM: Called from WebAssembly Hello world")}}).then(function(obj)
8506 {
8507 console.log("Called with instance " + obj);
8508 }).catch(function(err) {
8509 console.log("Called with error " + err);
8510 });
8511 )JS";
8512
testHandleMicrotasks(napi_env env1,napi_callback_info info)8513 static napi_value testHandleMicrotasks(napi_env env1, napi_callback_info info)
8514 {
8515 JSVM_InitOptions init_options;
8516 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8517 return nullptr;
8518 }
8519 init_options.externalReferences = externals;
8520 if (aa == 0) {
8521 OH_JSVM_Init(&init_options);
8522 aa++;
8523 }
8524 JSVM_VM vm;
8525 JSVM_CreateVMOptions options;
8526 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8527 return nullptr;
8528 }
8529 OH_JSVM_CreateVM(&options, &vm);
8530 JSVM_VMScope vm_scope;
8531 OH_JSVM_OpenVMScope(vm, &vm_scope);
8532 JSVM_Env env;
8533 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8534 JSVM_EnvScope envScope;
8535 OH_JSVM_OpenEnvScope(env, &envScope);
8536 JSVM_HandleScope handlescope;
8537 OH_JSVM_OpenHandleScope(env, &handlescope);
8538 JSVM_Value sourcecodevalue;
8539 OH_JSVM_CreateStringUtf8(env, STR_TASK, strlen(STR_TASK), &sourcecodevalue);
8540 JSVM_Script script;
8541 OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script);
8542 JSVM_Value result;
8543 OH_JSVM_RunScript(env, script, &result);
8544 bool rst = false;
8545 for (int i = 0; i < 3; i++) { // 3: cycles
8546 JSVM_Status flag1 = OH_JSVM_PumpMessageLoop(vm, &rst);
8547 JSVM_Status flag2 = OH_JSVM_PerformMicrotaskCheckpoint(vm);
8548 if (rst && flag1 == JSVM_Status::JSVM_OK && flag2 == JSVM_Status::JSVM_OK) {
8549 sleep(3);
8550 break;
8551 }
8552 }
8553 OH_JSVM_CloseHandleScope(env, handlescope);
8554 OH_JSVM_CloseEnvScope(env, envScope);
8555 OH_JSVM_DestroyEnv(env);
8556 OH_JSVM_CloseVMScope(vm, vm_scope);
8557 OH_JSVM_DestroyVM(vm);
8558 napi_value result11;
8559 bool rstLast = true;
8560 NAPI_CALL(env1, napi_get_boolean(env1, rstLast, &result11));
8561 return result11;
8562 }
8563
8564 static const char *SRC_TEST = R"JS(
8565 !function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};
8566 return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r)
8567 {n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e)
8568 {"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),
8569 Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;
8570 if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);
8571 if(n.r(r),Object.defineProperty(r,"default",
8572 {enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));
8573 return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};
8574 return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}
8575 ([function(e,t,n){const r=n(1);try{const e=r.divide(10,0);print(e)}catch(e){print(e.message),print(e.stack)}},
8576 function(e,t){e.exports={divide:function(e,t){if(0===t)throw new Error("Cannot divide by zero");return e/t}}}]);
8577 )JS";
8578
testJswmInterface(napi_env env1,napi_callback_info info)8579 static napi_value testJswmInterface(napi_env env1, napi_callback_info info)
8580 {
8581 JSVM_InitOptions init_options;
8582 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8583 return nullptr;
8584 }
8585 init_options.externalReferences = externals;
8586 if (aa == 0) {
8587 OH_JSVM_Init(&init_options);
8588 aa++;
8589 }
8590 JSVM_VM vm;
8591 JSVM_CreateVMOptions options;
8592 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8593 return nullptr;
8594 }
8595 OH_JSVM_CreateVM(&options, &vm);
8596 JSVM_VMScope vm_scope;
8597 OH_JSVM_OpenVMScope(vm, &vm_scope);
8598 JSVM_Env env;
8599 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8600 JSVM_EnvScope envScope;
8601 OH_JSVM_OpenEnvScope(env, &envScope);
8602 JSVM_HandleScope handlescope;
8603 OH_JSVM_OpenHandleScope(env, &handlescope);
8604 JSVM_Value jsSrc;
8605 OH_JSVM_CreateStringUtf8(env, SRC_TEST, strlen(SRC_TEST), &jsSrc);
8606 const uint8_t *data = nullptr;
8607 size_t length = 0;
8608 bool cacheRejected = false;
8609 bool rstFlag = false;
8610 JSVM_Script script;
8611 JSVM_ScriptOrigin scriptOrgin {
8612 .sourceMapUrl = "/data/bundle.js.map/bundle.js.map",
8613 .resourceName = "bundle.js"
8614 };
8615 JSVM_Status status = OH_JSVM_CompileScriptWithOrigin(env, jsSrc, data, length,
8616 true, &cacheRejected, &scriptOrgin, &script);
8617 if (status == JSVM_Status::JSVM_OK) {
8618 rstFlag = true;
8619 }
8620 OH_JSVM_CloseHandleScope(env, handlescope);
8621 OH_JSVM_CloseEnvScope(env, envScope);
8622 OH_JSVM_DestroyEnv(env);
8623 OH_JSVM_CloseVMScope(vm, vm_scope);
8624 OH_JSVM_DestroyVM(vm);
8625 napi_value result11;
8626 NAPI_CALL(env1, napi_get_boolean(env1, rstFlag, &result11));
8627 return result11;
8628 }
8629
IsConstructor(JSVM_Env env,JSVM_CallbackInfo info)8630 static JSVM_Value IsConstructor(JSVM_Env env, JSVM_CallbackInfo info)
8631 {
8632 size_t argc = 1;
8633 JSVM_Value args[1] = {nullptr};
8634 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8635
8636 bool result = false;
8637 OH_JSVM_IsConstructor(env, args[0], &result);
8638
8639 JSVM_Value isConstructor = nullptr;
8640 OH_JSVM_GetBoolean(env, result, &isConstructor);
8641 return isConstructor;
8642 }
CreateRegExp1(JSVM_Env env,JSVM_CallbackInfo info)8643 static JSVM_Value CreateRegExp1(JSVM_Env env, JSVM_CallbackInfo info)
8644 {
8645 size_t argc = 1;
8646 JSVM_Value args[1] = {nullptr};
8647 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8648
8649 JSVM_Value returnValue = nullptr;
8650 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_IGNORE_CASE, &returnValue);
8651 if (status != JSVM_OK) {
8652 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8653 }
8654 return returnValue;
8655 }
CreateRegExp2(JSVM_Env env,JSVM_CallbackInfo info)8656 static JSVM_Value CreateRegExp2(JSVM_Env env, JSVM_CallbackInfo info)
8657 {
8658 size_t argc = 1;
8659 JSVM_Value args[1] = {nullptr};
8660 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8661
8662 JSVM_Value returnValue = nullptr;
8663 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_GLOBAL, &returnValue);
8664 if (status != JSVM_OK) {
8665 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8666 }
8667 return returnValue;
8668 }
CreateRegExp3(JSVM_Env env,JSVM_CallbackInfo info)8669 static JSVM_Value CreateRegExp3(JSVM_Env env, JSVM_CallbackInfo info)
8670 {
8671 size_t argc = 1;
8672 JSVM_Value args[1] = {nullptr};
8673 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8674
8675 JSVM_Value returnValue = nullptr;
8676 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_MULTILINE, &returnValue);
8677 if (status != JSVM_OK) {
8678 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8679 }
8680 return returnValue;
8681 }
CreateRegExp4(JSVM_Env env,JSVM_CallbackInfo info)8682 static JSVM_Value CreateRegExp4(JSVM_Env env, JSVM_CallbackInfo info)
8683 {
8684 size_t argc = 1;
8685 JSVM_Value args[1] = {nullptr};
8686 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8687
8688 JSVM_Value returnValue = nullptr;
8689 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_STICKY, &returnValue);
8690 if (status != JSVM_OK) {
8691 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8692 }
8693 return returnValue;
8694 }
CreateRegExp5(JSVM_Env env,JSVM_CallbackInfo info)8695 static JSVM_Value CreateRegExp5(JSVM_Env env, JSVM_CallbackInfo info)
8696 {
8697 size_t argc = 1;
8698 JSVM_Value args[1] = {nullptr};
8699 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8700
8701 JSVM_Value returnValue = nullptr;
8702 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_DOT_ALL, &returnValue);
8703 if (status != JSVM_OK) {
8704 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8705 }
8706 return returnValue;
8707 }
CreateRegExpEnvNullptr(JSVM_Env env,JSVM_CallbackInfo info)8708 static JSVM_Value CreateRegExpEnvNullptr(JSVM_Env env, JSVM_CallbackInfo info)
8709 {
8710 size_t argc = 1;
8711 JSVM_Value args[1] = {nullptr};
8712 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8713
8714 JSVM_Value returnValue = nullptr;
8715 JSVM_Status status = OH_JSVM_CreateRegExp(nullptr, args[0], JSVM_RegExpFlags::JSVM_REGEXP_DOT_ALL, &returnValue);
8716 JSVM_Value value = nullptr;
8717 OH_JSVM_CreateInt32(env, static_cast<int32_t>(status), &value);
8718 return value;
8719 }
CreateMap(JSVM_Env env,JSVM_CallbackInfo info)8720 static JSVM_Value CreateMap(JSVM_Env env, JSVM_CallbackInfo info)
8721 {
8722 JSVM_Value returnValue = nullptr;
8723 JSVM_Status status = OH_JSVM_CreateMap(env, &returnValue);
8724 if (status != JSVM_OK) {
8725 OH_JSVM_ThrowError(env, nullptr, "Failed to create map");
8726 }
8727 return returnValue;
8728 }
8729
testCompileScriptWithOptions(JSVM_Env env)8730 bool testCompileScriptWithOptions(JSVM_Env env)
8731 {
8732 JSVM_Value jsSrc;
8733 JSVM_Script script = nullptr;
8734 std::string src(R"JS(let a = 100;a = a + 21)JS");
8735 uint8_t *cache;
8736 size_t length;
8737 JSVM_ScriptOrigin scriptOrgin {
8738 .sourceMapUrl = "bundle.js.map",
8739 .resourceName = "bundle.js"
8740 };
8741 JSVM_CompileOptions option[2];
8742 option[0] = {
8743 .id = JSVM_COMPILE_MODE,
8744 .content = { .num = JSVM_COMPILE_MODE_EAGER_COMPILE }
8745 };
8746 option[1] = {
8747 .id = JSVM_COMPILE_SCRIPT_ORIGIN,
8748 .content = { &scriptOrgin }
8749 };
8750 bool rstFlag = false;
8751 OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsSrc);
8752 rstFlag = OH_JSVM_CompileScriptWithOptions(env, jsSrc, TWO, option, &script) == JSVM_OK;
8753 OH_JSVM_CreateCodeCache(env, script, (const uint8_t**)&cache, &length);
8754 return rstFlag;
8755 }
testCompileWithOption(napi_env env1,napi_callback_info info)8756 static napi_value testCompileWithOption(napi_env env1, napi_callback_info info)
8757 {
8758 JSVM_InitOptions init_options;
8759 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8760 return nullptr;
8761 }
8762 init_options.externalReferences = externals;
8763 if (aa == 0) {
8764 OH_JSVM_Init(&init_options);
8765 aa++;
8766 }
8767 JSVM_VM vm;
8768 JSVM_CreateVMOptions options;
8769 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8770 return nullptr;
8771 }
8772 OH_JSVM_CreateVM(&options, &vm);
8773 JSVM_VMScope vm_scope;
8774 OH_JSVM_OpenVMScope(vm, &vm_scope);
8775 JSVM_Env env;
8776 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8777 JSVM_EnvScope envScope;
8778 OH_JSVM_OpenEnvScope(env, &envScope);
8779 JSVM_HandleScope handlescope;
8780 OH_JSVM_OpenHandleScope(env, &handlescope);
8781 bool rstFlag = false;
8782 rstFlag = testCompileScriptWithOptions(env);
8783 OH_JSVM_CloseHandleScope(env, handlescope);
8784 OH_JSVM_CloseEnvScope(env, envScope);
8785 OH_JSVM_DestroyEnv(env);
8786 OH_JSVM_CloseVMScope(vm, vm_scope);
8787 OH_JSVM_DestroyVM(vm);
8788 napi_value result11;
8789 NAPI_CALL(env1, napi_get_boolean(env1, rstFlag, &result11));
8790 return result11;
8791 }
8792
testRetainScript(napi_env env1,napi_callback_info info)8793 static napi_value testRetainScript(napi_env env1, napi_callback_info info)
8794 {
8795 JSVM_InitOptions init_options;
8796 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8797 return nullptr;
8798 }
8799 init_options.externalReferences = externals;
8800 if (aa == 0) {
8801 OH_JSVM_Init(&init_options);
8802 aa++;
8803 }
8804 JSVM_VM vm;
8805 JSVM_CreateVMOptions options;
8806 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8807 return nullptr;
8808 }
8809 OH_JSVM_CreateVM(&options, &vm);
8810 JSVM_VMScope vm_scope;
8811 OH_JSVM_OpenVMScope(vm, &vm_scope);
8812 JSVM_Env env;
8813 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8814 JSVM_EnvScope envScope;
8815 OH_JSVM_OpenEnvScope(env, &envScope);
8816 JSVM_HandleScope handlescope;
8817 OH_JSVM_OpenHandleScope(env, &handlescope);
8818 bool result = true;
8819 JSVM_Script script = nullptr;
8820 JSVM_HandleScope handlescopeSub;
8821 OH_JSVM_OpenHandleScope(env, &handlescopeSub);
8822 std::string src(R"JS(let a = 100;a = a + 21)JS");
8823
8824 JSVM_Value jsSrc;
8825 OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsSrc);
8826 OH_JSVM_CompileScriptWithOptions(env, jsSrc, 0, nullptr, &script);
8827 result = result && OH_JSVM_RetainScript(env, script) == JSVM_OK;
8828 OH_JSVM_CloseHandleScope(env, handlescopeSub);
8829 JSVM_Value returnValue;
8830 auto runStatus = OH_JSVM_RunScript(env, script, &returnValue) == JSVM_OK;
8831 OH_JSVM_ReleaseScript(env, script);
8832 int runResult = 0;
8833 OH_JSVM_GetValueInt32(env, returnValue, &runResult);
8834 result = runStatus && runResult == RUN_TEST_RETAIN_SCRIPT_VALUE;
8835 OH_JSVM_CloseHandleScope(env, handlescope);
8836 OH_JSVM_CloseEnvScope(env, envScope);
8837 OH_JSVM_DestroyEnv(env);
8838 OH_JSVM_CloseVMScope(vm, vm_scope);
8839 OH_JSVM_DestroyVM(vm);
8840 napi_value result11;
8841 NAPI_CALL(env1, napi_get_boolean(env1, result, &result11));
8842 return result11;
8843 }
8844
testOpenInspectorWithName(napi_env env1,napi_callback_info info)8845 static napi_value testOpenInspectorWithName(napi_env env1, napi_callback_info info)
8846 {
8847 JSVM_InitOptions init_options;
8848 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8849 return nullptr;
8850 }
8851 init_options.externalReferences = externals;
8852 if (aa == 0) {
8853 OH_JSVM_Init(&init_options);
8854 aa++;
8855 }
8856 JSVM_VM vm;
8857 JSVM_CreateVMOptions options;
8858 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8859 return nullptr;
8860 }
8861
8862 OH_JSVM_CreateVM(&options, &vm);
8863 JSVM_VMScope vmScope;
8864 OH_JSVM_OpenVMScope(vm, &vmScope);
8865 JSVM_Env env;
8866 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8867
8868 OH_JSVM_OpenInspectorWithName(env, -1, "test");
8869 JSVM_EnvScope envScope;
8870 OH_JSVM_OpenEnvScope(env, &envScope);
8871 JSVM_HandleScope handleScope;
8872 OH_JSVM_OpenHandleScope(env, &handleScope);
8873 JSVM_Value strValue = nullptr;
8874 std::string src(R"JS(let a = 100;a = a + 21)JS");
8875 OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &strValue);
8876 JSVM_Script script;
8877 OH_JSVM_CompileScript(env, strValue, nullptr, 0, true, nullptr, &script);
8878 JSVM_Value result = nullptr;
8879 OH_JSVM_RunScript(env, script, &result);
8880 int runResult = 0;
8881 OH_JSVM_GetValueInt32(env, result, &runResult);
8882 auto resultFlag = runResult == RUN_TEST_RETAIN_SCRIPT_VALUE;
8883 OH_JSVM_CloseHandleScope(env, handleScope);
8884 OH_JSVM_CloseEnvScope(env, envScope);
8885 OH_JSVM_CloseInspector(env);
8886 OH_JSVM_CloseVMScope(vm, vmScope);
8887 OH_JSVM_DestroyEnv(env);
8888 OH_JSVM_DestroyVM(vm);
8889 napi_value result11;
8890 NAPI_CALL(env1, napi_get_boolean(env1, resultFlag, &result11));
8891 return result11;
8892 }
8893
useArrayBuffer(JSVM_Env env,napi_env env1)8894 void useArrayBuffer(JSVM_Env env, napi_env env1)
8895 {
8896 JSVM_HandleScope handleScope;
8897 OH_JSVM_OpenHandleScope(env, &handleScope);
8898
8899 void *backingStore;
8900 JSVM_Value arrayBuffer;
8901 JSVM_Status stat1 = OH_JSVM_AllocateArrayBufferBackingStoreData(100, JSVM_ZERO_INITIALIZED, &backingStore);
8902 NAPI_ASSERT_RETURN_VOID(env1, stat1 == JSVM_OK, "OH_JSVM_AllocateArrayBufferBackingStoreData check status");
8903
8904 JSVM_Status stat2 = OH_JSVM_CreateArrayBufferFromBackingStoreData(env, backingStore, 100, 30, 20, &arrayBuffer);
8905 NAPI_ASSERT_RETURN_VOID(env1, stat2 == JSVM_OK, "OH_JSVM_CreateArrayBufferFromBackingStoreData check status");
8906
8907 JSVM_Value js_global;
8908 JSVM_Value name;
8909 JSVM_Status stat3 = OH_JSVM_GetGlobal(env, &js_global);
8910 NAPI_ASSERT_RETURN_VOID(env1, stat3 == JSVM_OK, "OH_JSVM_GetGlobal check status");
8911 JSVM_Status stat4 = OH_JSVM_CreateStringUtf8(env, "buffer", JSVM_AUTO_LENGTH, &name);
8912 NAPI_ASSERT_RETURN_VOID(env1, stat4 == JSVM_OK, "OH_JSVM_CreateStringUtf8 check status");
8913 JSVM_Status stat5 = OH_JSVM_SetProperty(env, js_global, name, arrayBuffer);
8914 NAPI_ASSERT_RETURN_VOID(env1, stat5 == JSVM_OK, "OH_JSVM_SetProperty check status");
8915
8916 JSVM_Script script;
8917 JSVM_Value scriptString;
8918 JSVM_Value result;
8919 const char *src = R"JS(
8920 function writeBuffer(data) {
8921 let view = new Uint8Array(data);
8922 // Write some values to the ArrayBuffer
8923 for (let i = 0; i < view.length; i++) {
8924 view[i] = i % 256;
8925 }
8926 }
8927 writeBuffer(buffer)
8928 )JS";
8929 JSVM_Status stat6 = OH_JSVM_CreateStringUtf8(env, src, JSVM_AUTO_LENGTH, &scriptString);
8930 NAPI_ASSERT_RETURN_VOID(env1, stat6 == JSVM_OK, "OH_JSVM_CreateStringUtf8 check status");
8931 JSVM_Status stat7 = OH_JSVM_CompileScriptWithOptions(env, scriptString, 0, nullptr, &script);
8932 NAPI_ASSERT_RETURN_VOID(env1, stat7 == JSVM_OK, "OH_JSVM_CompileScriptWithOption check status");
8933 JSVM_Status stat8 = OH_JSVM_RunScript(env, script, &result);
8934 NAPI_ASSERT_RETURN_VOID(env1, stat8 == JSVM_OK, "OH_JSVM_RunScript check status");
8935
8936 uint8_t *array = static_cast<uint8_t*>(backingStore);
8937 for (auto i = 0; i < 100; ++i) {
8938 if (array[i] != i % 25 % 256) {
8939 NAPI_ASSERT_RETURN_VOID(env1, true, "OH_JSVM_RunScript check status");
8940 }
8941 }
8942
8943 JSVM_Status stat9 = OH_JSVM_DetachArraybuffer(env, arrayBuffer);
8944 NAPI_ASSERT_RETURN_VOID(env1, stat9 == JSVM_OK, "OH_JSVM_DetachArraybuffer check status");
8945
8946 JSVM_Status stat10 = OH_JSVM_FreeArrayBufferBackingStoreData(backingStore);
8947 NAPI_ASSERT_RETURN_VOID(env1, stat10 == JSVM_OK, "OH_JSVM_FreeArrayBufferBackingStoreData check status");
8948
8949 OH_JSVM_CloseHandleScope(env, handleScope);
8950 }
8951
testArrayBuffer(napi_env env1,napi_callback_info info)8952 static napi_value testArrayBuffer(napi_env env1, napi_callback_info info)
8953 {
8954 JSVM_InitOptions init_options;
8955 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8956 printf("memset_s failed");
8957 return nullptr;
8958 }
8959 init_options.externalReferences = externals;
8960 if (aa == 0) {
8961 OH_JSVM_Init(&init_options);
8962 aa++;
8963 }
8964 JSVM_VM vm;
8965 JSVM_CreateVMOptions options;
8966 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8967 printf("memset_s failed");
8968 return nullptr;
8969 }
8970 OH_JSVM_CreateVM(&options, &vm);
8971 JSVM_VMScope vm_scope;
8972 OH_JSVM_OpenVMScope(vm, &vm_scope);
8973 JSVM_Env env;
8974 JSVM_CallbackStruct param[1];
8975 param[0].data = nullptr;
8976 param[0].callback = assertEqual;
8977 JSVM_PropertyDescriptor descriptor[] = {
8978 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
8979 };
8980 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
8981 JSVM_EnvScope envScope;
8982 OH_JSVM_OpenEnvScope(env, &envScope);
8983
8984 useArrayBuffer(env, env1);
8985
8986 OH_JSVM_CloseEnvScope(env, envScope);
8987 OH_JSVM_DestroyEnv(env);
8988 OH_JSVM_CloseVMScope(vm, vm_scope);
8989 OH_JSVM_DestroyVM(vm);
8990 napi_value result11;
8991 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
8992 return result11;
8993 }
8994
useArrayBuffer2(JSVM_Env env,napi_env env1)8995 void useArrayBuffer2(JSVM_Env env, napi_env env1)
8996 {
8997 void *storeData = nullptr;
8998 size_t storeLen = SIXTEEN;
8999 JSVM_Status stat1 = OH_JSVM_AllocateArrayBufferBackingStoreData(storeLen, JSVM_ZERO_INITIALIZED, &storeData);
9000 NAPI_ASSERT_RETURN_VOID(env1, stat1 == JSVM_OK, "OH_JSVM_AllocateArrayBufferBackingStoreData check status");
9001
9002 JSVM_Value arrayBuffer;
9003 JSVM_Status stat2 = OH_JSVM_CreateArrayBufferFromBackingStoreData(env, storeData, storeLen, 0, storeLen, &arrayBuffer);
9004 NAPI_ASSERT_RETURN_VOID(env1, stat2 == JSVM_OK, "OH_JSVM_CreateArrayBufferFromBackingStoreData check status");
9005
9006 void *tmpArrayBufferPtr = nullptr;
9007 size_t arrayBufferLen = 0;
9008 JSVM_Status stat3 = OH_JSVM_GetArraybufferInfo(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLen);
9009 NAPI_ASSERT_RETURN_VOID(env1, stat3 == JSVM_OK, "OH_JSVM_GetArraybufferInfo check status");
9010
9011 size_t typedArrayLen = arrayBufferLen / sizeof(int32_t);
9012 JSVM_Value typedArray;
9013 JSVM_Status stat4 = OH_JSVM_CreateTypedarray(env, JSVM_TypedarrayType::JSVM_INT32_ARRAY, typedArrayLen,
9014 arrayBuffer, 0, &typedArray);
9015 NAPI_ASSERT_RETURN_VOID(env1, stat4 == JSVM_OK, "OH_JSVM_CreateTypedarray check status");
9016
9017 JSVM_Status stat5 = OH_JSVM_DetachArraybuffer(env, arrayBuffer);
9018 NAPI_ASSERT_RETURN_VOID(env1, stat5 == JSVM_OK, "OH_JSVM_DetachArraybuffer check status");
9019
9020 JSVM_Status stat6 = OH_JSVM_FreeArrayBufferBackingStoreData(storeData);
9021 NAPI_ASSERT_RETURN_VOID(env1, stat6 == JSVM_OK, "OH_JSVM_FreeArrayBufferBackingStoreData check status");
9022 }
testArrayBuffer2(napi_env env1,napi_callback_info info)9023 static napi_value testArrayBuffer2(napi_env env1, napi_callback_info info)
9024 {
9025 JSVM_InitOptions init_options;
9026 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
9027 printf("memset_s failed");
9028 return nullptr;
9029 }
9030 init_options.externalReferences = externals;
9031 if (aa == 0) {
9032 OH_JSVM_Init(&init_options);
9033 aa++;
9034 }
9035 JSVM_VM vm;
9036 JSVM_CreateVMOptions options;
9037 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
9038 printf("memset_s failed");
9039 return nullptr;
9040 }
9041 OH_JSVM_CreateVM(&options, &vm);
9042 JSVM_VMScope vm_scope;
9043 OH_JSVM_OpenVMScope(vm, &vm_scope);
9044 JSVM_Env env;
9045 JSVM_CallbackStruct param[1];
9046 param[0].data = nullptr;
9047 param[0].callback = assertEqual;
9048 JSVM_PropertyDescriptor descriptor[] = {
9049 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
9050 };
9051 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
9052 JSVM_EnvScope envScope;
9053 OH_JSVM_OpenEnvScope(env, &envScope);
9054 JSVM_HandleScope handleScope;
9055 OH_JSVM_OpenHandleScope(env, &handleScope);
9056
9057 useArrayBuffer2(env, env1);
9058
9059 OH_JSVM_CloseHandleScope(env, handleScope);
9060 OH_JSVM_CloseEnvScope(env, envScope);
9061 OH_JSVM_DestroyEnv(env);
9062 OH_JSVM_CloseVMScope(vm, vm_scope);
9063 OH_JSVM_DestroyVM(vm);
9064 napi_value result11;
9065 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
9066 return result11;
9067 }
9068
useArrayBuffer3(JSVM_Env env,napi_env env1)9069 void useArrayBuffer3(JSVM_Env env, napi_env env1)
9070 {
9071 void *storeData = nullptr;
9072 size_t storeLen = SIXTEEN;
9073 JSVM_Status stat1 = OH_JSVM_AllocateArrayBufferBackingStoreData(storeLen, JSVM_ZERO_INITIALIZED, &storeData);
9074 NAPI_ASSERT_RETURN_VOID(env1, stat1 == JSVM_OK, "OH_JSVM_AllocateArrayBufferBackingStoreData check status");
9075
9076 JSVM_Value arrayBuffer;
9077 JSVM_Status stat2 = OH_JSVM_CreateArrayBufferFromBackingStoreData(env, storeData, storeLen, 0, storeLen, &arrayBuffer);
9078 NAPI_ASSERT_RETURN_VOID(env1, stat2 == JSVM_OK, "OH_JSVM_CreateArrayBufferFromBackingStoreData check status");
9079
9080 void *tmpArrayBufferPtr = nullptr;
9081 size_t arrayBufferLen = 0;
9082 JSVM_Status stat3 = OH_JSVM_GetArraybufferInfo(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLen);
9083 NAPI_ASSERT_RETURN_VOID(env1, stat3 == JSVM_OK, "OH_JSVM_GetArraybufferInfo check status");
9084
9085 size_t typedArrayLen = arrayBufferLen / sizeof(int32_t);
9086
9087 JSVM_Value typedArray;
9088 JSVM_Status stat4 = OH_JSVM_CreateTypedarray(env, JSVM_TypedarrayType::JSVM_INT32_ARRAY, typedArrayLen, arrayBuffer, 0, &typedArray);
9089 NAPI_ASSERT_RETURN_VOID(env1, stat4 == JSVM_OK, "OH_JSVM_CreateTypedarray check status");
9090
9091 JSVM_Status stat5 = OH_JSVM_DetachArraybuffer(env, arrayBuffer);
9092 NAPI_ASSERT_RETURN_VOID(env1, stat5 == JSVM_OK, "OH_JSVM_DetachArraybuffer check status");
9093
9094 JSVM_Value arrayBuffer2;
9095 JSVM_Status stat6 = OH_JSVM_CreateArrayBufferFromBackingStoreData(env, storeData, storeLen, 0, storeLen, &arrayBuffer2);
9096 NAPI_ASSERT_RETURN_VOID(env1, stat6 == JSVM_OK, "OH_JSVM_CreateArrayBufferFromBackingStoreData check status");
9097 JSVM_Status stat7 = OH_JSVM_DetachArraybuffer(env, arrayBuffer2);
9098 NAPI_ASSERT_RETURN_VOID(env1, stat7 == JSVM_OK, "OH_JSVM_DetachArraybuffer check status");
9099
9100 JSVM_Status stat8 = OH_JSVM_FreeArrayBufferBackingStoreData(storeData);
9101 NAPI_ASSERT_RETURN_VOID(env1, stat8 == JSVM_OK, "OH_JSVM_FreeArrayBufferBackingStoreData check status");
9102 }
testArrayBuffer3(napi_env env1,napi_callback_info info)9103 static napi_value testArrayBuffer3(napi_env env1, napi_callback_info info)
9104 {
9105 JSVM_InitOptions init_options;
9106 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
9107 printf("memset_s failed");
9108 return nullptr;
9109 }
9110 init_options.externalReferences = externals;
9111 if (aa == 0) {
9112 OH_JSVM_Init(&init_options);
9113 aa++;
9114 }
9115 JSVM_VM vm;
9116 JSVM_CreateVMOptions options;
9117 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
9118 printf("memset_s failed");
9119 return nullptr;
9120 }
9121 OH_JSVM_CreateVM(&options, &vm);
9122 JSVM_VMScope vm_scope;
9123 OH_JSVM_OpenVMScope(vm, &vm_scope);
9124 JSVM_Env env;
9125 JSVM_CallbackStruct param[1];
9126 param[0].data = nullptr;
9127 param[0].callback = assertEqual;
9128 JSVM_PropertyDescriptor descriptor[] = {
9129 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
9130 };
9131 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
9132 JSVM_EnvScope envScope;
9133 OH_JSVM_OpenEnvScope(env, &envScope);
9134 JSVM_HandleScope handleScope;
9135 OH_JSVM_OpenHandleScope(env, &handleScope);
9136
9137 useArrayBuffer3(env, env1);
9138
9139 OH_JSVM_CloseHandleScope(env, handleScope);
9140 OH_JSVM_CloseEnvScope(env, envScope);
9141 OH_JSVM_DestroyEnv(env);
9142 OH_JSVM_CloseVMScope(vm, vm_scope);
9143 OH_JSVM_DestroyVM(vm);
9144 napi_value result11;
9145 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
9146 return result11;
9147 }
9148
test_CompileWasmModule(JSVM_Env env,napi_env env1)9149 void test_CompileWasmModule(JSVM_Env env, napi_env env1)
9150 {
9151 JSVM_HandleScope handleScope;
9152 OH_JSVM_OpenHandleScope(env, &handleScope);
9153
9154 JSVM_Value wasmModule;
9155 JSVM_Status status = OH_JSVM_CompileWasmModule(env, nullptr, 0, nullptr, 0, nullptr, &wasmModule);
9156 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "OH_JSVM_CompileWasmModule check status");
9157
9158 OH_JSVM_CloseHandleScope(env, handleScope);
9159 }
9160
test_CompileWasmFunctionNULL(JSVM_Env env,napi_env env1)9161 void test_CompileWasmFunctionNULL(JSVM_Env env, napi_env env1)
9162 {
9163 JSVM_HandleScope handleScope;
9164 OH_JSVM_OpenHandleScope(env, &handleScope);
9165
9166 JSVM_Status status = OH_JSVM_CompileWasmFunction(env, nullptr, 0, JSVM_WASM_OPT_BASELINE);
9167 NAPI_ASSERT_RETURN_VOID(env1, status == JSVM_INVALID_ARG, "OH_JSVM_CompileWasmFunction check status");
9168
9169 OH_JSVM_CloseHandleScope(env, handleScope);
9170 }
9171
test_IsWasmModuleValueisNULL(JSVM_Env env,napi_env env1)9172 void test_IsWasmModuleValueisNULL(JSVM_Env env, napi_env env1)
9173 {
9174 JSVM_HandleScope handleScope;
9175 OH_JSVM_OpenHandleScope(env, &handleScope);
9176
9177 bool result = false;
9178 JSVM_Status status = OH_JSVM_IsWasmModuleObject(env, nullptr, &result);
9179 NAPI_ASSERT_RETURN_VOID(env1, status == JSVM_INVALID_ARG, "OH_JSVM_IsWasmModuleObject check status");
9180 NAPI_ASSERT_RETURN_VOID(env1, result == false, "OH_JSVM_IsWasmModuleObject check result");
9181
9182 OH_JSVM_CloseHandleScope(env, handleScope);
9183 }
9184
test_CreateWarmCache_wasmmodule_is_null(JSVM_Env env,napi_env env1)9185 void test_CreateWarmCache_wasmmodule_is_null(JSVM_Env env, napi_env env1)
9186 {
9187 JSVM_HandleScope handleScope;
9188 OH_JSVM_OpenHandleScope(env, &handleScope);
9189
9190 const uint8_t *cacheData = nullptr;
9191 size_t cacheLength = 0;
9192 JSVM_Value wasmModule = nullptr;
9193 JSVM_Status status = OH_JSVM_CreateWasmCache(env, wasmModule, &cacheData, &cacheLength);
9194 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "OH_JSVM_CreateWarmCache check status not ok 1");
9195
9196 OH_JSVM_CloseHandleScope(env, handleScope);
9197 }
9198
test_CreateWarmCache_wasmmodule_is_not_wasmmodule(JSVM_Env env,napi_env env1)9199 void test_CreateWarmCache_wasmmodule_is_not_wasmmodule(JSVM_Env env, napi_env env1)
9200 {
9201 JSVM_HandleScope handleScope;
9202 OH_JSVM_OpenHandleScope(env, &handleScope);
9203
9204 const uint8_t *cacheData = nullptr;
9205 size_t cacheLength = 0;
9206 JSVM_Value wasmModule;
9207 OH_JSVM_CreateSet(env, &wasmModule);
9208 JSVM_Status status = OH_JSVM_CreateWasmCache(env, wasmModule, &cacheData, &cacheLength);
9209 NAPI_ASSERT_RETURN_VOID(env1, status != JSVM_OK, "OH_JSVM_CreateWarmCache check status not ok 2");
9210
9211 OH_JSVM_CloseHandleScope(env, handleScope);
9212 }
9213
test_ReleaseCache_cachedata_is_null(JSVM_Env env,napi_env env1)9214 void test_ReleaseCache_cachedata_is_null(JSVM_Env env, napi_env env1)
9215 {
9216 JSVM_HandleScope handleScope;
9217 OH_JSVM_OpenHandleScope(env, &handleScope);
9218
9219 JSVM_Status status = OH_JSVM_ReleaseCache(env, nullptr, JSVM_CACHE_TYPE_JS);
9220 NAPI_ASSERT_RETURN_VOID(env1, status == JSVM_INVALID_ARG, "OH_JSVM_CreateWarmCache check status is invalid_arg");
9221
9222 OH_JSVM_CloseHandleScope(env, handleScope);
9223 }
9224
testWasmOperator(napi_env env1,napi_callback_info info)9225 static napi_value testWasmOperator(napi_env env1, napi_callback_info info)
9226 {
9227 JSVM_InitOptions init_options;
9228 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
9229 printf("memset_s failed");
9230 return nullptr;
9231 }
9232 init_options.externalReferences = externals;
9233 if (aa == 0) {
9234 OH_JSVM_Init(&init_options);
9235 aa++;
9236 }
9237 JSVM_VM vm;
9238 JSVM_CreateVMOptions options;
9239 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
9240 printf("memset_s failed");
9241 return nullptr;
9242 }
9243 OH_JSVM_CreateVM(&options, &vm);
9244 JSVM_VMScope vm_scope;
9245 OH_JSVM_OpenVMScope(vm, &vm_scope);
9246 JSVM_Env env;
9247 JSVM_CallbackStruct param[1];
9248 param[0].data = nullptr;
9249 param[0].callback = assertEqual;
9250 JSVM_PropertyDescriptor descriptor[] = {
9251 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
9252 };
9253 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
9254 JSVM_EnvScope envScope;
9255 OH_JSVM_OpenEnvScope(env, &envScope);
9256 #if !defined(JSVM_JITLESS)
9257 test_CompileWasmModule(env, env1);
9258 test_CompileWasmFunctionNULL(env, env1);
9259 test_CreateWarmCache_wasmmodule_is_null(env, env1);
9260 test_CreateWarmCache_wasmmodule_is_not_wasmmodule(env, env1);
9261 #endif
9262 test_IsWasmModuleValueisNULL(env, env1);
9263 test_ReleaseCache_cachedata_is_null(env, env1);
9264
9265 OH_JSVM_CloseEnvScope(env, envScope);
9266 OH_JSVM_DestroyEnv(env);
9267 OH_JSVM_CloseVMScope(vm, vm_scope);
9268 OH_JSVM_DestroyVM(vm);
9269 napi_value result11;
9270 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
9271 return result11;
9272 }
9273
test_set_debug_option(JSVM_Env env)9274 void test_set_debug_option(JSVM_Env env)
9275 {
9276 JSVM_HandleScope handleScope1, handleScope2, handleScope3;
9277 int num = 100;
9278 bool boolValue = false;
9279 JSVM_Value array1[num], array2[num], array3[num];
9280 OH_JSVM_SetDebugOption(env, JSVM_SCOPE_CHECK, true);
9281 OH_JSVM_OpenHandleScope(env, &handleScope1);
9282 for (int i = 0; i < num; i++) {
9283 OH_JSVM_GetBoolean(env, false, &array1[i]);
9284 OH_JSVM_IsBoolean(env, array1[i], &boolValue);
9285 }
9286 OH_JSVM_OpenHandleScope(env, &handleScope2);
9287 for (int i = 0; i < num; i++) {
9288 OH_JSVM_GetBoolean(env, false, &array2[i]);
9289 OH_JSVM_IsBoolean(env, array2[i], &boolValue);
9290 }
9291 OH_JSVM_OpenHandleScope(env, &handleScope3);
9292 for (int i = 0; i < num; i++) {
9293 OH_JSVM_GetBoolean(env, false, &array3[i]);
9294 OH_JSVM_IsBoolean(env, array3[i], &boolValue);
9295 }
9296 OH_JSVM_CloseHandleScope(env, handleScope3);
9297 OH_JSVM_CloseHandleScope(env, handleScope2);
9298 OH_JSVM_CloseHandleScope(env, handleScope1);
9299 OH_JSVM_SetDebugOption(env, JSVM_SCOPE_CHECK, false);
9300 }
9301
testSetDebugOption(napi_env env1,napi_callback_info info)9302 static napi_value testSetDebugOption(napi_env env1, napi_callback_info info)
9303 {
9304 JSVM_InitOptions init_options;
9305 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
9306 printf("memset_s failed");
9307 return nullptr;
9308 }
9309 init_options.externalReferences = externals;
9310 if (aa == 0) {
9311 OH_JSVM_Init(&init_options);
9312 aa++;
9313 }
9314 JSVM_VM vm;
9315 JSVM_CreateVMOptions options;
9316 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
9317 printf("memset_s failed");
9318 return nullptr;
9319 }
9320 OH_JSVM_CreateVM(&options, &vm);
9321 JSVM_VMScope vm_scope;
9322 OH_JSVM_OpenVMScope(vm, &vm_scope);
9323 JSVM_Env env;
9324 JSVM_CallbackStruct param[1];
9325 param[0].data = nullptr;
9326 param[0].callback = assertEqual;
9327 JSVM_PropertyDescriptor descriptor[] = {
9328 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
9329 };
9330 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
9331 JSVM_EnvScope envScope;
9332 OH_JSVM_OpenEnvScope(env, &envScope);
9333
9334 test_set_debug_option(env);
9335
9336 OH_JSVM_CloseEnvScope(env, envScope);
9337 OH_JSVM_DestroyEnv(env);
9338 OH_JSVM_CloseVMScope(vm, vm_scope);
9339 OH_JSVM_DestroyVM(vm);
9340 napi_value result11;
9341 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
9342 return result11;
9343 }
9344
9345 static JSVM_CallbackStruct param[] = {
9346 {.callback = CreateStringUtf8, .data = nullptr},
9347 {.callback = GetValueStringUtf8, .data = nullptr},
9348 {.callback = CreateStringUtf16, .data = nullptr},
9349 {.callback = GetValueStringUtf16, .data = nullptr},
9350 {.callback = CreateStringLatin1, .data = nullptr},
9351 {.callback = GetValueStringLatin1, .data = nullptr},
9352 {.callback = CreateDate, .data = nullptr},
9353 {.callback = CreateObject, .data = nullptr},
9354 {.callback = CreateExternal, .data = nullptr},
9355 {.callback = GetValueExternal, .data = nullptr},
9356 {.callback = CreateArray, .data = nullptr},
9357 {.callback = CreateArrayWithLength, .data = nullptr},
9358 {.callback = CreateArraybuffer, .data = nullptr},
9359 {.callback = GetArrayLength, .data = nullptr},
9360 {.callback = CreateInt32, .data = nullptr},
9361 {.callback = GetValueInt32, .data = nullptr},
9362 {.callback = CreateUInt32, .data = nullptr},
9363 {.callback = GetValueUint32, .data = nullptr},
9364 {.callback = CreateInt64, .data = nullptr},
9365 {.callback = GetValueInt64, .data = nullptr},
9366 {.callback = CreateDouble, .data = nullptr},
9367 {.callback = GetDouble, .data = nullptr},
9368 {.callback = GetBoolean, .data = nullptr},
9369 {.callback = GetValueBool, .data = nullptr},
9370 {.callback = CreateBigintUint64, .data = nullptr},
9371 {.callback = GetValueBigintUint64, .data = nullptr},
9372 {.callback = CreateBigintInt64, .data = nullptr},
9373 {.callback = GetBigintInt64, .data = nullptr},
9374 {.callback = CreateBigintWords, .data = nullptr},
9375 {.callback = GetValueBigintWords, .data = nullptr},
9376 {.callback = GetNull, .data = nullptr},
9377 {.callback = GetUndefined, .data = nullptr},
9378 {.callback = GetGlobal, .data = nullptr},
9379 {.callback = CreateSymbol, .data = nullptr},
9380 {.callback = SymbolFor, .data = nullptr},
9381 {.callback = GetDateValue, .data = nullptr},
9382 {.callback = IsDate, .data = nullptr},
9383 {.callback = GetArraybufferInfo, .data = nullptr},
9384 {.callback = CreateTypedArray, .data = nullptr},
9385 {.callback = GetTypedArrayInfo, .data = nullptr},
9386 {.callback = CreateDataView, .data = nullptr},
9387 {.callback = GetDataViewInfo, .data = nullptr},
9388 {.callback = GetPrototype, .data = nullptr},
9389 {.callback = CreateReference, .data = nullptr},
9390 {.callback = UseReference, .data = nullptr},
9391 {.callback = DeleteReference, .data = nullptr},
9392 {.callback = EscapableHandleScopeTest, .data = nullptr},
9393 {.callback = HandleScopeTest, .data = nullptr},
9394 {.callback = HandleScope, .data = nullptr},
9395 {.callback = GetPropertyNames, .data = nullptr},
9396 {.callback = GetAllPropertyNames, .data = nullptr},
9397 {.callback = GetEnumerableNames, .data = nullptr},
9398 {.callback = GetOwnWritableNames, .data = nullptr},
9399 {.callback = GetEnumerableConfigurableNames, .data = nullptr},
9400 {.callback = GetOwnConfigurableNames, .data = nullptr},
9401 {.callback = SetProperty, .data = nullptr},
9402 {.callback = SetPropertyFailed, .data = nullptr},
9403 {.callback = GetProperty, .data = nullptr},
9404 {.callback = GetPropertyFailed, .data = nullptr},
9405 {.callback = HasProperty, .data = nullptr},
9406 {.callback = HasPropertyFailed, .data = nullptr},
9407 {.callback = DeleteProperty, .data = nullptr},
9408 {.callback = DeletePropertyFailed, .data = nullptr},
9409 {.callback = HasOwnProperty, .data = nullptr},
9410 {.callback = HasOwnPropertyFailed, .data = nullptr},
9411 {.callback = SetNamedPropertyCreateObject, .data = nullptr},
9412 {.callback = SetNamedProperty, .data = nullptr},
9413 {.callback = SetNamedPropertyFailed, .data = nullptr},
9414 {.callback = GetNamedProperty, .data = nullptr},
9415 {.callback = GetNamedPropertyFailed, .data = nullptr},
9416 {.callback = HasNamedProperty, .data = nullptr},
9417 {.callback = HasNamedPropertyFailed, .data = nullptr},
9418 {.callback = SetElementWithLength, .data = nullptr},
9419 {.callback = SetElement, .data = nullptr},
9420 {.callback = SetElementFailed, .data = nullptr},
9421 {.callback = GetElement, .data = nullptr},
9422 {.callback = GetElementFailed, .data = nullptr},
9423 {.callback = HasElement, .data = nullptr},
9424 {.callback = HasElementFailed, .data = nullptr},
9425 {.callback = DeleteElement, .data = nullptr},
9426 {.callback = DeleteElementAndCheck, .data = nullptr},
9427 {.callback = DeleteElementFailed, .data = nullptr},
9428 {.callback = DefineMethodProperties, .data = nullptr},
9429 {.callback = DefineStringProperties, .data = nullptr},
9430 {.callback = DefineGetterProperties, .data = nullptr},
9431 {.callback = DefinePropertiesFailed, .data = nullptr},
9432 {.callback = ObjectFreeze, .data = nullptr},
9433 {.callback = ObjectSeal, .data = nullptr},
9434 {.callback = GetVersion, .data = nullptr},
9435 {.callback = GetVersionFailed, .data = nullptr},
9436 {.callback = GetVMInfo, .data = nullptr},
9437 {.callback = JsonStringify, .data = nullptr},
9438 {.callback = JsonStringifyFailed, .data = nullptr},
9439 {.callback = JsonParseNumber, .data = nullptr},
9440 {.callback = JsonParseObject, .data = nullptr},
9441 {.callback = JsonParse, .data = nullptr},
9442 {.callback = JsonParseFailed, .data = nullptr},
9443 {.callback = CallFunction, .data = nullptr},
9444 {.callback = CallFunctionWithArg, .data = nullptr},
9445 {.callback = CallFunctionWithObject, .data = nullptr},
9446 {.callback = CallFunctionFailed, .data = nullptr},
9447 {.callback = CreateFunction, .data = nullptr},
9448 {.callback = CreateFunctionFailed, .data = nullptr},
9449 {.callback = GetHeapStatistics, .data = nullptr},
9450 {.callback = GetVM, .data = nullptr},
9451 {.callback = GetTypeof, .data = nullptr},
9452 {.callback = InstanceOf, .data = nullptr},
9453 {.callback = IsArray, .data = nullptr},
9454 {.callback = IsArrayBuffer, .data = nullptr},
9455 {.callback = IsTypedarray, .data = nullptr},
9456 {.callback = IsDataView, .data = nullptr},
9457 {.callback = IsStrictEquals, .data = nullptr},
9458 {.callback = DetachArraybuffer, .data = nullptr},
9459 {.callback = IsDetachedArraybuffer, .data = nullptr},
9460 {.callback = CoerceToBool, .data = nullptr},
9461 {.callback = CoerceToNumber, .data = nullptr},
9462 {.callback = CoerceToObject, .data = nullptr},
9463 {.callback = CoerceToString, .data = nullptr},
9464 {.callback = IsPromise, .data = nullptr},
9465 {.callback = CreatePromise, .data = nullptr},
9466 {.callback = ResolveRejectDeferred, .data = nullptr},
9467 {.callback = GetCbArgs, .data = nullptr},
9468 {.callback = NewInstance, .data = nullptr},
9469 {.callback = SetTypeTagToObject, .data = nullptr},
9470 {.callback = CheckObjectTypeTag, .data = nullptr},
9471 {.callback = AdjustExternalMemory, .data = nullptr},
9472 {.callback = MemoryPressureNotification, .data = nullptr},
9473 {.callback = WrapObject, .data = nullptr},
9474 {.callback = RemoveWrap, .data = nullptr},
9475 {.callback = HandleScopeFor, .data = nullptr},
9476 {.callback = JsVmThrow, .data = nullptr},
9477 {.callback = JsVmThrowFromJS, .data = nullptr},
9478 {.callback = JsVmThrowError, .data = nullptr},
9479 {.callback = JsVmThrowTypeError, .data = nullptr},
9480 {.callback = JsVmThrowRangeError, .data = nullptr},
9481 {.callback = JsVmThrowSyntaxError, .data = nullptr},
9482 {.callback = JsVmIsError, .data = nullptr},
9483 {.callback = JsVmCreateError, .data = nullptr},
9484 {.callback = JsVmCreateTypeError, .data = nullptr},
9485 {.callback = JsVmCreateRangeError, .data = nullptr},
9486 {.callback = JsVmCreateSyntaxError, .data = nullptr},
9487 {.callback = JsVmGetLastErrorInfo, .data = nullptr},
9488 {.callback = JsVmGetAndClearLastException, .data = nullptr},
9489 {.callback = JsVmIsExceptionPending, .data = nullptr},
9490 {.callback = CreateSnapshotFailed, .data = nullptr},
9491 {.callback = DebugCodeUsingCpuProfiler, .data = nullptr},
9492 {.callback = TakeHeapSnapshot, .data = nullptr},
9493 {.callback = DebugCodeUsingInspector, .data = nullptr},
9494 {.callback = WaitForDebuggerFailed, .data = nullptr},
9495 {.callback = SetInstanceData, .data = nullptr},
9496 {.callback = GetInstanceData, .data = nullptr},
9497 {.callback = IsConstructor, .data = nullptr},
9498 {.callback = CreateRegExp1, .data = nullptr},
9499 {.callback = CreateRegExp2, .data = nullptr},
9500 {.callback = CreateRegExp3, .data = nullptr},
9501 {.callback = CreateRegExp4, .data = nullptr},
9502 {.callback = CreateRegExp5, .data = nullptr},
9503 {.callback = CreateRegExpEnvNullptr, .data = nullptr},
9504 {.callback = CreateFunctionWithScript, .data = nullptr},
9505 {.callback = CreateMap, .data = nullptr},
9506 {.callback = IsRegExp, .data = nullptr},
9507 {.callback = CoerceToBigInt, .data = nullptr},
9508 {.callback = TestGetValueStringUtf8Test1, .data = nullptr},
9509 {.callback = TestGetValueStringUtf8Test2, .data = nullptr},
9510 {.callback = TestGetValueStringUtf8Test3, .data = nullptr},
9511 {.callback = TestGetValueStringUtf8Test4, .data = nullptr},
9512 {.callback = TestGetValueStringUtf8Test5, .data = nullptr},
9513 {.callback = TestGetValueStringUtf8Test6, .data = nullptr},
9514 {.callback = TestCreateStringUtf8Test1, .data = nullptr},
9515 {.callback = TestCreateStringUtf8Test2, .data = nullptr},
9516 {.callback = TestCreateStringUtf8Test3, .data = nullptr},
9517 {.callback = TestCreateStringUtf8Test4, .data = nullptr},
9518 {.callback = TestCreateStringUtf16Test1, .data = nullptr},
9519 {.callback = TestCreateStringUtf16Test2, .data = nullptr},
9520 {.callback = TestCreateStringUtf16Test3, .data = nullptr},
9521 {.callback = TestCreateStringUtf16Test4, .data = nullptr},
9522 {.callback = TestGetValueStringUtf16Test1, .data = nullptr},
9523 {.callback = TestGetValueStringUtf16Test2, .data = nullptr},
9524 {.callback = TestGetValueStringUtf16Test3, .data = nullptr},
9525 {.callback = TestGetValueStringUtf16Test4, .data = nullptr},
9526 {.callback = TestGetValueStringUtf16Test5, .data = nullptr},
9527 {.callback = TestGetValueStringUtf16Test6, .data = nullptr},
9528 {.callback = TestCreateStringLatin1Test1, .data = nullptr},
9529 {.callback = TestCreateStringLatin1Test2, .data = nullptr},
9530 {.callback = TestCreateStringLatin1Test3, .data = nullptr},
9531 {.callback = TestCreateStringLatin1Test4, .data = nullptr},
9532 {.callback = TestGetValueStringLatin1Test1, .data = nullptr},
9533 {.callback = TestGetValueStringLatin1Test2, .data = nullptr},
9534 {.callback = TestGetValueStringLatin1Test3, .data = nullptr},
9535 {.callback = TestGetValueStringLatin1Test4, .data = nullptr},
9536 {.callback = TestGetValueStringLatin1Test5, .data = nullptr},
9537 {.callback = TestGetValueStringLatin1Test6, .data = nullptr},
9538 {.callback = TestIsStringTest1, .data = nullptr},
9539 {.callback = TestIsStringTest2, .data = nullptr},
9540 {.callback = TestIsStringTest3, .data = nullptr},
9541 {.callback = TestOtherTest1, .data = nullptr},
9542 {.callback = TestCreateArrayWithLength_01, .data = nullptr},
9543 {.callback = TestCreateArrayWithLength_02, .data = nullptr},
9544 {.callback = TestCreateArrayWithLength_03, .data = nullptr},
9545 {.callback = TestCreateArrayWithLength_04, .data = nullptr},
9546 {.callback = TestGetArrayLength_01, .data = nullptr},
9547 {.callback = TestGetArrayLength_02, .data = nullptr},
9548 {.callback = TestGetArrayLength_03, .data = nullptr},
9549 {.callback = TestGetArrayLength_04, .data = nullptr},
9550 {.callback = TestCreateTypedarray_01, .data = nullptr},
9551 {.callback = TestCreateTypedarray_02, .data = nullptr},
9552 {.callback = TestCreateTypedarray_03, .data = nullptr},
9553 {.callback = TestCreateTypedarray_04, .data = nullptr},
9554 {.callback = TestCreateTypedarray_05, .data = nullptr},
9555 {.callback = TestCreateTypedarray_06, .data = nullptr},
9556 {.callback = TestGetTypedarrayInfo_01, .data = nullptr},
9557 {.callback = TestGetTypedarrayInfo_02, .data = nullptr},
9558 {.callback = TestCreateDataview_01, .data = nullptr},
9559 {.callback = TestCreateDataview_02, .data = nullptr},
9560 {.callback = TestCreateDataview_03, .data = nullptr},
9561 {.callback = TestCreateDataview_04, .data = nullptr},
9562 {.callback = TestCreateDataview_05, .data = nullptr},
9563 {.callback = TestCreateDataview_06, .data = nullptr},
9564 {.callback = TestGetDataview_01, .data = nullptr},
9565 {.callback = TestGetDataview_02, .data = nullptr},
9566 {.callback = TestSetElement_01, .data = nullptr},
9567 {.callback = TestSetElement_02, .data = nullptr},
9568 {.callback = TestSetElement_03, .data = nullptr},
9569 {.callback = TestSetElement_04, .data = nullptr},
9570 {.callback = TestSetElement_05, .data = nullptr},
9571 {.callback = TestSetElement_06, .data = nullptr},
9572 {.callback = TestSetElement_07, .data = nullptr},
9573 {.callback = TestGetElement_01, .data = nullptr},
9574 {.callback = TestGetElement_02, .data = nullptr},
9575 {.callback = TestGetElement_03, .data = nullptr},
9576 {.callback = TestGetElement_04, .data = nullptr},
9577 {.callback = TestGetElement_05, .data = nullptr},
9578 {.callback = TestGetElement_06, .data = nullptr},
9579 {.callback = TestHasElement_01, .data = nullptr},
9580 {.callback = TestHasElement_02, .data = nullptr},
9581 {.callback = TestHasElement_03, .data = nullptr},
9582 {.callback = TestHasElement_04, .data = nullptr},
9583 {.callback = TestHasElement_05, .data = nullptr},
9584 {.callback = TestHasElement_06, .data = nullptr},
9585 {.callback = TestDeleteElement_01, .data = nullptr},
9586 {.callback = TestDeleteElement_02, .data = nullptr},
9587 {.callback = TestDeleteElement_03, .data = nullptr},
9588 {.callback = TestDeleteElement_04, .data = nullptr},
9589 {.callback = TestDeleteElement_05, .data = nullptr},
9590 {.callback = TestDeleteElement_06, .data = nullptr},
9591 {.callback = TestArrayProcessSteps_01, .data = nullptr},
9592 {.callback = TestArrayProcessSteps_02, .data = nullptr},
9593 {.callback = TestTypedarrayProcessSteps_01, .data = nullptr},
9594 {.callback = TestTypedarrayProcessSteps_02, .data = nullptr},
9595 {.callback = TestArrayDataviewProcessSteps_01, .data = nullptr},
9596 {.callback = TestArrayDataviewProcessSteps_02, .data = nullptr},
9597 {.callback = TestTypeArrayAndDataviewProcessSteps_01, .data = nullptr},
9598 {.callback = TestTypeArraySetMultipleTimes_01, .data = nullptr},
9599 {.callback = TestArraySetMultipleTimes_01, .data = nullptr},
9600 {.callback = TestArrayGetMultipleTimes_01, .data = nullptr},
9601 {.callback = TestArrayDeleteMultipleTimes_01, .data = nullptr},
9602 {.callback = TestArrayDeleteMultipleTimes_02, .data = nullptr},
9603 {.callback = TestCreateErrorTest1, .data = nullptr},
9604 {.callback = TestCreateErrorTest2, .data = nullptr},
9605 {.callback = TestCreateErrorTest3, .data = nullptr},
9606 {.callback = TestCreateErrorTest4, .data = nullptr},
9607 {.callback = TestCreateErrorTest5, .data = nullptr},
9608 {.callback = TestCreateTypeErrorTest1, .data = nullptr},
9609 {.callback = TestCreateTypeErrorTest2, .data = nullptr},
9610 {.callback = TestCreateTypeErrorTest3, .data = nullptr},
9611 {.callback = TestCreateTypeErrorTest4, .data = nullptr},
9612 {.callback = TestCreateTypeErrorTest5, .data = nullptr},
9613 {.callback = TestCreateRangeErrorTest1, .data = nullptr},
9614 {.callback = TestCreateRangeErrorTest2, .data = nullptr},
9615 {.callback = TestCreateRangeErrorTest3, .data = nullptr},
9616 {.callback = TestCreateRangeErrorTest4, .data = nullptr},
9617 {.callback = TestCreateRangeErrorTest5, .data = nullptr},
9618 {.callback = TestCreateSyntaxErrorTest1, .data = nullptr},
9619 {.callback = TestCreateSyntaxErrorTest2, .data = nullptr},
9620 {.callback = TestCreateSyntaxErrorTest3, .data = nullptr},
9621 {.callback = TestCreateSyntaxErrorTest4, .data = nullptr},
9622 {.callback = TestCreateSyntaxErrorTest5, .data = nullptr},
9623 {.callback = TestThrowTest1, .data = nullptr},
9624 {.callback = TestThrowTest2, .data = nullptr},
9625 {.callback = TestThrowTest3, .data = nullptr},
9626 {.callback = TestThrowTest4, .data = nullptr},
9627 {.callback = TestThrowTest5, .data = nullptr},
9628 {.callback = TestThrowTest6, .data = nullptr},
9629 {.callback = TestThrowErrorTest1, .data = nullptr},
9630 {.callback = TestThrowErrorTest2, .data = nullptr},
9631 {.callback = TestThrowErrorTest3, .data = nullptr},
9632 {.callback = TestThrowTypeErrorTest1, .data = nullptr},
9633 {.callback = TestThrowTypeErrorTest2, .data = nullptr},
9634 {.callback = TestThrowTypeErrorTest3, .data = nullptr},
9635 {.callback = TestThrowRangeErrorTest1, .data = nullptr},
9636 {.callback = TestThrowRangeErrorTest2, .data = nullptr},
9637 {.callback = TestThrowRangeErrorTest3, .data = nullptr},
9638 {.callback = TestThrowSyntaxErrorTest1, .data = nullptr},
9639 {.callback = TestThrowSyntaxErrorTest2, .data = nullptr},
9640 {.callback = TestThrowSyntaxErrorTest3, .data = nullptr},
9641 {.callback = TestIsErrorTest1, .data = nullptr},
9642 {.callback = TestIsErrorTest2, .data = nullptr},
9643 {.callback = TestGetAndClearLastExceptionTest1, .data = nullptr},
9644 {.callback = TestGetAndClearLastExceptionTest2, .data = nullptr},
9645 {.callback = TestGetAndClearLastExceptionTest3, .data = nullptr},
9646 {.callback = TestIsExceptionPendingTest1, .data = nullptr},
9647 {.callback = TestIsExceptionPendingTest2, .data = nullptr},
9648 {.callback = TestIsExceptionPendingTest3, .data = nullptr},
9649 {.callback = TestGetLastErrorInfoTest1, .data = nullptr},
9650 {.callback = TestGetLastErrorInfoTest2, .data = nullptr},
9651 {.callback = TestGetLastErrorInfoTest3, .data = nullptr},
9652 {.callback = TestCombinationTest1, .data = nullptr},
9653 {.callback = TestCombinationTest2, .data = nullptr},
9654 {.callback = TestCombinationTest3, .data = nullptr},
9655 {.callback = TestCombinationTest4, .data = nullptr},
9656 {.callback = TestCombinationTest5, .data = nullptr},
9657 {.callback = TestCombinationTest6, .data = nullptr},
9658 {.callback = TestCombinationTest7, .data = nullptr},
9659 {.callback = TestCombinationTest8, .data = nullptr},
9660 {.callback = TestCombinationTest9, .data = nullptr},
9661 {.callback = TestCombinationTest10, .data = nullptr},
9662 {.callback = TestCombinationTest11, .data = nullptr},
9663 {.callback = TestCombinationTest12, .data = nullptr},
9664 {.callback = TestCombinationTest13, .data = nullptr},
9665 {.callback = TestCombinationTest14, .data = nullptr},
9666 {.callback = TestCombinationTest15, .data = nullptr},
9667 {.callback = TestCombinationTest16, .data = nullptr},
9668 {.callback = TestCombinationTest17, .data = nullptr},
9669 {.callback = TestCombinationTest18, .data = nullptr},
9670 {.callback = TestCombinationTest19, .data = nullptr},
9671 {.callback = TestCombinationTest20, .data = nullptr},
9672 {.callback = TestCombinationTest21, .data = nullptr},
9673 {.callback = TestCombinationTest22, .data = nullptr},
9674 {.callback = TestCombinationTest23, .data = nullptr},
9675 {.callback = TestHasPropertyTest1, .data = nullptr},
9676 {.callback = TestHasPropertyTest2, .data = nullptr},
9677 {.callback = TestDeletePropertyTest1, .data = nullptr},
9678 {.callback = TestDeletePropertyTest2, .data = nullptr},
9679 {.callback = TestHasOwnPropertyTest1, .data = nullptr},
9680 {.callback = TestHasOwnPropertyTest2, .data = nullptr},
9681 {.callback = TestHasOwnPropertyTest3, .data = nullptr},
9682 {.callback = TestGetNamedPropertyTest1, .data = nullptr},
9683 {.callback = TestGetNamedPropertyTest2, .data = nullptr},
9684 {.callback = TestGetNamedPropertyTest3, .data = nullptr},
9685 {.callback = TestHasNamedPropertyTest1, .data = nullptr},
9686 {.callback = TestHasNamedPropertyTest2, .data = nullptr},
9687 {.callback = TestHasNamedPropertyTest3, .data = nullptr},
9688 {.callback = TestHasNamedPropertyTest4, .data = nullptr},
9689 {.callback = TestDefinePropertiesTest1, .data = nullptr},
9690 {.callback = TestDefinePropertiesTest2, .data = nullptr},
9691 {.callback = TestDefinePropertiesTest3, .data = nullptr},
9692 {.callback = TestDefinePropertiesTest4, .data = nullptr},
9693 {.callback = TestCombinationPropertyTest1, .data = nullptr},
9694 {.callback = TestCombinationPropertyTest2, .data = nullptr},
9695 {.callback = TestCombinationPropertyTest3, .data = nullptr},
9696 {.callback = TestCombinationPropertyTest4, .data = nullptr},
9697 {.callback = TestCombinationNamedPropertyTest1, .data = nullptr},
9698 {.callback = TestCombinationNamedPropertyTest2, .data = nullptr},
9699 {.callback = TestCreateObjectTest1, .data = nullptr},
9700 {.callback = TestCreateObjectTest2, .data = nullptr},
9701 {.callback = TestTypeofTest1, .data = nullptr},
9702 {.callback = TestTypeofTest2, .data = nullptr},
9703 {.callback = TestTypeofTest3, .data = nullptr},
9704 {.callback = TestTypeofTest4, .data = nullptr},
9705 {.callback = TestTypeofTest5, .data = nullptr},
9706 {.callback = TestTypeofTest6, .data = nullptr},
9707 {.callback = TestTypeofTest7, .data = nullptr},
9708 {.callback = TestTypeofTest8, .data = nullptr},
9709 {.callback = TestTypeofTest9, .data = nullptr},
9710 {.callback = TestTypeofTest10, .data = nullptr},
9711 {.callback = TestTypeofTest11, .data = nullptr},
9712 {.callback = TestTypeofTest12, .data = nullptr},
9713 {.callback = TestTypeTagObjectTest1, .data = nullptr},
9714 {.callback = TestTypeTagObjectTest2, .data = nullptr},
9715 {.callback = TestTypeTagObjectTest3, .data = nullptr},
9716 {.callback = TestTypeTagObjectTest4, .data = nullptr},
9717 {.callback = TestCheckObjectTypeTagTest1, .data = nullptr},
9718 {.callback = TestCheckObjectTypeTagTest2, .data = nullptr},
9719 {.callback = TestCheckObjectTypeTagTest3, .data = nullptr},
9720 {.callback = TestCheckObjectTypeTagTest4, .data = nullptr},
9721 {.callback = TestCheckObjectTypeTagTest5, .data = nullptr},
9722 {.callback = TestObjectCombinationTest1, .data = nullptr},
9723 {.callback = TestObjectCombinationTest2, .data = nullptr},
9724 {.callback = TestObjectCombinationTest3, .data = nullptr},
9725 {.callback = TestInitTest1, .data = nullptr},
9726 {.callback = TestCreateVMTest1, .data = nullptr},
9727 {.callback = TestCreateVMTest2, .data = nullptr},
9728 {.callback = TestDestroyVMTest1, .data = nullptr},
9729 {.callback = TestDestroyVMTest2, .data = nullptr},
9730 {.callback = TestOpenVMScopeTest1, .data = nullptr},
9731 {.callback = TestOpenVMScopeTest2, .data = nullptr},
9732 {.callback = TestCloseVMScopeTest1, .data = nullptr},
9733 {.callback = TestCloseVMScopeTest2, .data = nullptr},
9734 {.callback = TestCreateEnvTest1, .data = nullptr},
9735 {.callback = TestCreateEnvTest2, .data = nullptr},
9736 {.callback = TestDestroyEnvTest1, .data = nullptr},
9737 {.callback = TestDestroyEnvTest2, .data = nullptr},
9738 {.callback = TestOpenEnvScopeTest1, .data = nullptr},
9739 {.callback = TestOpenEnvScopeTest2, .data = nullptr},
9740 {.callback = TestOpenEnvScopeTest3, .data = nullptr},
9741 {.callback = TestCloseEnvScopeTest1, .data = nullptr},
9742 {.callback = TestCloseEnvScopeTest2, .data = nullptr},
9743 {.callback = TestDataTypeCombinationTest1, .data = nullptr},
9744 {.callback = TestCreateDateTest1, .data = nullptr},
9745 {.callback = TestGetDateValueTest1, .data = nullptr},
9746 {.callback = TestIsDateTest1, .data = nullptr},
9747 {.callback = TestDateCombinationTest1, .data = nullptr},
9748 {.callback = TestPumpMessageLoopTest1, .data = nullptr},
9749 {.callback = TestPerformMicrotaskCheckpointTest1, .data = nullptr},
9750 {.callback = TestExecuteTasksCombinationTest1, .data = nullptr},
9751 {.callback = TestJsonParseTest1, .data = nullptr},
9752 {.callback = TestJsonParseTest2, .data = nullptr},
9753 {.callback = TestJsonParseTest3, .data = nullptr},
9754 {.callback = TestJsonStringifyTest1, .data = nullptr},
9755 {.callback = TestJsonStringifyTest2, .data = nullptr},
9756 {.callback = TestJsonStringifyTest3, .data = nullptr},
9757 {.callback = TestJsonCombinationTest1, .data = nullptr},
9758 {.callback = TestStrictEqualsTest1, .data = nullptr},
9759 {.callback = TestStrictEqualsTest2, .data = nullptr},
9760 {.callback = TestStrictEqualsTest3, .data = nullptr},
9761 {.callback = TestStrictEqualsTest4, .data = nullptr},
9762 {.callback = TestStrictEqualsTest5, .data = nullptr},
9763 {.callback = TestStrictEqualsTest6, .data = nullptr},
9764 {.callback = TestStrictEqualsTest7, .data = nullptr},
9765 {.callback = TestStrictEqualsTest8, .data = nullptr},
9766 {.callback = TestStrictEqualsTest9, .data = nullptr},
9767 {.callback = TestStrictEqualsTest10, .data = nullptr},
9768 {.callback = TestStrictEqualsTest11, .data = nullptr},
9769 {.callback = TestStrictEqualsTest12, .data = nullptr},
9770 {.callback = TestStrictEqualsTest13, .data = nullptr},
9771 {.callback = TestStrictEqualsTest14, .data = nullptr},
9772 {.callback = TestStrictEqualsTest15, .data = nullptr},
9773 {.callback = TestStrictEqualsTest16, .data = nullptr},
9774 {.callback = TestStrictEqualsTest17, .data = nullptr},
9775 {.callback = TestStrictEqualsTest18, .data = nullptr},
9776 {.callback = TestStrictEqualsTest19, .data = nullptr},
9777 {.callback = TestCreateArrayBufferCase01, .data = nullptr},
9778 {.callback = TestCreateArrayBufferCase02, .data = nullptr},
9779 {.callback = TestCreateArrayBufferCase03, .data = nullptr},
9780 {.callback = TestCreateArrayBufferCase04, .data = nullptr},
9781 {.callback = TestGetArrayBufferCase01, .data = nullptr},
9782 {.callback = TestGetArrayBufferCase02, .data = nullptr},
9783 {.callback = TestIsArrayBufferCase01, .data = nullptr},
9784 {.callback = TestIsArrayBufferCase02, .data = nullptr},
9785 {.callback = TestIsArrayBufferCase03, .data = nullptr},
9786 {.callback = TestDetachArrayBufferCase01, .data = nullptr},
9787 {.callback = TestDetachArrayBufferCase02, .data = nullptr},
9788 {.callback = TestDetachArrayBufferCase03, .data = nullptr},
9789 {.callback = TestIsDetachedArraybufferCase01, .data = nullptr},
9790 {.callback = TestIsDetachedArraybufferCase02, .data = nullptr},
9791 {.callback = TestIsDetachedArraybufferCase03, .data = nullptr},
9792 {.callback = TestIsDetachedArraybufferCase04, .data = nullptr},
9793 {.callback = TestIsDetachedArraybufferCase05, .data = nullptr},
9794 {.callback = TestCreateAnyBufferDetachedCase01, .data = nullptr},
9795 {.callback = TestCreateInt32Case01, .data = nullptr},
9796 {.callback = TestCreateInt32Case02, .data = nullptr},
9797 {.callback = TestCreateInt32Case03, .data = nullptr},
9798 {.callback = TestCreateInt32Case04, .data = nullptr},
9799 {.callback = TestCreateUint32Case01, .data = nullptr},
9800 {.callback = TestCreateUint32Case02, .data = nullptr},
9801 {.callback = TestCreateUint32Case03, .data = nullptr},
9802 {.callback = TestCreateUint32Case04, .data = nullptr},
9803 {.callback = TestCreateInt64Case01, .data = nullptr},
9804 {.callback = TestCreateInt64Case02, .data = nullptr},
9805 {.callback = TestCreateInt64Case03, .data = nullptr},
9806 {.callback = TestCreateInt64Case04, .data = nullptr},
9807 {.callback = TestCreateDoubleCase01, .data = nullptr},
9808 {.callback = TestCreateDoubleCase02, .data = nullptr},
9809 {.callback = TestCreateDoubleCase03, .data = nullptr},
9810 {.callback = TestCreateDoubleCase04, .data = nullptr},
9811 {.callback = TestGetValueUint32Case01, .data = nullptr},
9812 {.callback = TestGetValueInt32Case01, .data = nullptr},
9813 {.callback = TestGetValueInt64Case01, .data = nullptr},
9814 {.callback = TestGetValueDoubleCase01, .data = nullptr},
9815 {.callback = TestBasicDataTypesCase01, .data = nullptr},
9816 {.callback = TestBasicDataTypesCase02, .data = nullptr},
9817 {.callback = TestBasicDataTypesCase03, .data = nullptr},
9818 {.callback = TestBasicDataTypesCase04, .data = nullptr},
9819 {.callback = TestCreateBigintInt64Case01, .data = nullptr},
9820 {.callback = TestCreateBigintInt64Case02, .data = nullptr},
9821 {.callback = TestCreateBigintInt64Case03, .data = nullptr},
9822 {.callback = TestCreateBigintInt64Case04, .data = nullptr},
9823 {.callback = TestCreateBigintUint64Case01, .data = nullptr},
9824 {.callback = TestCreateBigintUint64Case02, .data = nullptr},
9825 {.callback = TestCreateBigintUint64Case03, .data = nullptr},
9826 {.callback = TestCreateBigintWordsCase01, .data = nullptr},
9827 {.callback = TestCreateBigintWordsCase02, .data = nullptr},
9828 {.callback = TestCreateBigintWordsCase03, .data = nullptr},
9829 {.callback = TestCreateBigintWordsCase04, .data = nullptr},
9830 {.callback = TestCreateBigintWordsCase05, .data = nullptr},
9831 {.callback = TestGetValueBigintInt64Case01, .data = nullptr},
9832 {.callback = TestGetValueBigintInt64Case02, .data = nullptr},
9833 {.callback = TestGetValueBigintInt64Case03, .data = nullptr},
9834 {.callback = TestGetValueBigintInt64Case04, .data = nullptr},
9835 {.callback = TestGetValueBigintInt64Case05, .data = nullptr},
9836 {.callback = TestGetValueBigintInt64Case06, .data = nullptr},
9837 {.callback = TestGetValueBigintUint64Case01, .data = nullptr},
9838 {.callback = TestGetValueBigintUint64Case02, .data = nullptr},
9839 {.callback = TestGetValueBigintUint64Case03, .data = nullptr},
9840 {.callback = TestGetValueBigintUint64Case04, .data = nullptr},
9841 {.callback = TestGetValueBigintUint64Case05, .data = nullptr},
9842 {.callback = TestGetValueBigintUint64Case06, .data = nullptr},
9843 {.callback = TestGetValueBigintWordsCase01, .data = nullptr},
9844 {.callback = TestGetValueBigintWordsCase02, .data = nullptr},
9845 {.callback = TestGetValueBigintWordsCase03, .data = nullptr},
9846 {.callback = TestGetValueBigintWordsCase04, .data = nullptr},
9847 {.callback = TestGetValueBigintWordsCase05, .data = nullptr},
9848 {.callback = TestCreateAndGetBigIntProgramCase01, .data = nullptr},
9849 {.callback = TestCreateAndGetBigIntProgramCase02, .data = nullptr},
9850 {.callback = TestCreateAndGetBigIntProgramCase03, .data = nullptr},
9851 {.callback = TestNewInstanceCase01, .data = nullptr},
9852 {.callback = TestNewInstanceCase02, .data = nullptr},
9853 {.callback = TestGetNewTargetCase01, .data = nullptr},
9854 {.callback = TestGetNewTargetCase02, .data = nullptr},
9855 {.callback = TestGetNewTargetCase03, .data = nullptr},
9856 {.callback = TestDefineClassCase01, .data = nullptr},
9857 {.callback = TestDefineClassCase02, .data = nullptr},
9858 {.callback = TestWrapCase01, .data = nullptr},
9859 {.callback = TestWrapCase02, .data = nullptr},
9860 {.callback = TestWrapCase03, .data = nullptr},
9861 {.callback = TestWrapCase04, .data = nullptr},
9862 {.callback = TestUnwrapCase01, .data = nullptr},
9863 {.callback = TestUnwrapCase02, .data = nullptr},
9864 {.callback = TestUnwrapCase03, .data = nullptr},
9865 {.callback = TestUnwrapCase04, .data = nullptr},
9866 {.callback = TestRemoveWrapCase01, .data = nullptr},
9867 {.callback = TestRemoveWrapCase02, .data = nullptr},
9868 {.callback = TestRemoveWrapCase03, .data = nullptr},
9869 {.callback = TestRemoveWrapCase04, .data = nullptr},
9870 {.callback = TestClassOperationCase01, .data = nullptr},
9871 {.callback = TestCallFunction01, .data = nullptr},
9872 {.callback = TestCallFunction02, .data = nullptr},
9873 {.callback = TestCallFunction03, .data = nullptr},
9874 {.callback = TestCallFunction04, .data = nullptr},
9875 {.callback = TestCallFunction05, .data = nullptr},
9876 {.callback = TestCallFunction06, .data = nullptr},
9877 {.callback = TestCallFunction07, .data = nullptr},
9878 {.callback = TestCallFunction08, .data = nullptr},
9879 {.callback = TestCallFunction09, .data = nullptr},
9880 {.callback = TestCreateFunction01, .data = nullptr},
9881 {.callback = TestCreateFunction02, .data = nullptr},
9882 {.callback = TestCreateFunction03, .data = nullptr},
9883 {.callback = TestCreateFunction04, .data = nullptr},
9884 {.callback = TestCreateFunction05, .data = nullptr},
9885 {.callback = TestCreateFunction06, .data = nullptr},
9886 {.callback = TestGetCbInfo01, .data = nullptr},
9887 {.callback = TestGetCbInfo02, .data = nullptr},
9888 {.callback = TestGetCbInfo03, .data = nullptr},
9889 {.callback = TestGetCbInfo04, .data = nullptr},
9890 {.callback = TestGetCbInfo05, .data = nullptr},
9891 {.callback = TestIsFunction01, .data = nullptr},
9892 {.callback = TestIsFunction02, .data = nullptr},
9893 {.callback = TestIsFunction03, .data = nullptr},
9894 {.callback = TestIsFunction04, .data = nullptr},
9895 {.callback = TestOpenHandleScopeCase01, .data = nullptr},
9896 {.callback = TestOpenHandleScopeCase02, .data = nullptr},
9897 {.callback = TestOpenHandleScopeCase03, .data = nullptr},
9898 {.callback = TestOpenHandleScopeCase04, .data = nullptr},
9899 {.callback = TestCloseHandleScopeCase01, .data = nullptr},
9900 {.callback = TestCloseHandleScopeCase02, .data = nullptr},
9901 {.callback = TestOpenAndCloseHandleScopeCase01, .data = nullptr},
9902 {.callback = TestOpenAndCloseHandleScopeCase02, .data = nullptr},
9903 {.callback = TestOpenEscapableHandleScopeCase01, .data = nullptr},
9904 {.callback = TestOpenEscapableHandleScopeCase02, .data = nullptr},
9905 {.callback = TestOpenEscapableHandleScopeCase03, .data = nullptr},
9906 {.callback = TestCloseEscapableHandleScopeCase01, .data = nullptr},
9907 {.callback = TestCloseEscapableHandleScopeCase02, .data = nullptr},
9908 {.callback = TestCloseEscapableHandleScopeCase03, .data = nullptr},
9909 {.callback = TestEscapeHandleCase01, .data = nullptr},
9910 {.callback = TestEscapeHandleCase02, .data = nullptr},
9911 {.callback = TestEscapeHandleCase03, .data = nullptr},
9912 {.callback = TestEscapeHandleCase04, .data = nullptr},
9913 {.callback = TestEscapeHandleCase05, .data = nullptr},
9914 {.callback = TestOpenClosedEscapeCase01, .data = nullptr},
9915 {.callback = TestOpenClosedEscapeCase02, .data = nullptr},
9916 {.callback = TestCreateReferenceCase01, .data = nullptr},
9917 {.callback = TestCreateReferenceCase02, .data = nullptr},
9918 {.callback = TestCreateReferenceCase03, .data = nullptr},
9919 {.callback = TestDeleteReferenceCase01, .data = nullptr},
9920 {.callback = TestDeleteReferenceCase02, .data = nullptr},
9921 {.callback = TestReferenceRefCase01, .data = nullptr},
9922 {.callback = TestReferenceRefCase02, .data = nullptr},
9923 {.callback = TestReferenceRefCase03, .data = nullptr},
9924 {.callback = TestReferenceUnrefCase01, .data = nullptr},
9925 {.callback = TestReferenceUnrefCase02, .data = nullptr},
9926 {.callback = TestReferenceUnrefCase03, .data = nullptr},
9927 {.callback = TestGetReferenceValueCase01, .data = nullptr},
9928 {.callback = TestGetReferenceValueCase02, .data = nullptr},
9929 {.callback = TestHandleAndRefCase01, .data = nullptr},
9930 {.callback = TestHandleAndRefCase02, .data = nullptr},
9931 {.callback = TestHandleAndRefCase03, .data = nullptr},
9932 {.callback = TestAdjustExternalMemoryCase01, .data = nullptr},
9933 {.callback = TestAdjustExternalMemoryCase02, .data = nullptr},
9934 {.callback = TestAdjustExternalMemoryCase03, .data = nullptr},
9935 {.callback = TestAdjustExternalMemoryCase04, .data = nullptr},
9936 {.callback = TestMemoryPressureNotificationCase01, .data = nullptr},
9937 {.callback = TestMemoryPressureNotificationCase02, .data = nullptr},
9938 {.callback = TestMemoryPressureNotificationCase03, .data = nullptr},
9939 {.callback = TestMemoryManagementuseCase01, .data = nullptr},
9940 {.callback = TestIsPromiseCase01, .data = nullptr},
9941 {.callback = TestIsPromiseCase02, .data = nullptr},
9942 {.callback = TestIsPromiseCase03, .data = nullptr},
9943 {.callback = TestIsPromiseCase04, .data = nullptr},
9944 {.callback = TestCreatePromiseCase01, .data = nullptr},
9945 {.callback = TestResolveDeferredCase01, .data = nullptr},
9946 {.callback = TestResolveDeferredCase02, .data = nullptr},
9947 {.callback = TestRejectDeferredCase01, .data = nullptr},
9948 {.callback = TestRejectDeferredCase02, .data = nullptr},
9949 {.callback = TestCreateResolveDeferredCase01, .data = nullptr},
9950 {.callback = TestGetVersionCase01, .data = nullptr},
9951 {.callback = TestGetVMInfoCase01, .data = nullptr},
9952 {.callback = TestGetVMInfoCase02, .data = nullptr},
9953 {.callback = TestGetHeapStatisticsCase01, .data = nullptr},
9954 {.callback = TestStartCpuProfilerCase01, .data = nullptr},
9955 {.callback = TestStopCpuProfilerCase01, .data = nullptr},
9956 {.callback = TestTakeHeapSnapshotCase01, .data = nullptr},
9957 {.callback = TestOpenInspectorCase01, .data = nullptr},
9958 {.callback = TestOpenInspectorCase02, .data = nullptr},
9959 {.callback = TestOpenInspectorCase03, .data = nullptr},
9960 {.callback = TestCloseInspectorCase01, .data = nullptr},
9961 {.callback = TestCloseInspectorCase02, .data = nullptr},
9962 {.callback = TestCloseInspectorCase03, .data = nullptr},
9963 {.callback = TestOpenInspectorWithNameCase01, .data = nullptr},
9964 {.callback = TestOpenInspectorWithNameCase02, .data = nullptr},
9965 };
9966 static JSVM_PropertyDescriptor jsDescriptor[] = {
9967 {"createStringUtf8", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9968 {"getValueStringUtf8", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9969 {"createStringUtf16", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9970 {"getValueStringUtf16", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9971 {"createStringLatin1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9972 {"getValueStringLatin1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9973 {"createDate", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9974 {"createObject", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9975 {"createExternal", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9976 {"getValueExternal", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9977 {"createArray", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9978 {"createArrayWithLength", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9979 {"createArraybuffer", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9980 {"getArrayLength", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9981 {"createInt32", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9982 {"getValueInt32", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9983 {"createUInt32", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9984 {"getValueUint32", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9985 {"createInt64", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9986 {"getValueInt64", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9987 {"createDouble", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9988 {"getDouble", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9989 {"getBoolean", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9990 {"getValueBool", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9991 {"createBigintUint64", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9992 {"getValueBigintUint64", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9993 {"createBigintInt64", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9994 {"getBigintInt64", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9995 {"createBigintWords", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9996 {"getValueBigintWords", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9997 {"getNull", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9998 {"getUndefined", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
9999 {"getGlobal", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10000 {"createSymbol", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10001 {"symbolFor", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10002 {"getDateValue", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10003 {"isDate", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10004 {"getArraybufferInfo", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10005 {"createTypedArray", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10006 {"getTypedArrayInfo", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10007 {"createDataView", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10008 {"getDataViewInfo", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10009 {"getPrototype", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10010 {"createReference", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10011 {"useReference", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10012 {"deleteReference", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10013 {"escapableHandleScopeTest", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10014 {"handleScopeTest", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10015 {"handleScope", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10016 {"getPropertyNames", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10017 {"getAllPropertyNames", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10018 {"getEnumerableNames", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10019 {"getOwnWritableNames", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10020 {"getEnumerableConfigurableNames", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10021 {"getOwnConfigurableNames", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10022 {"jsvmSetProperty", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10023 {"setPropertyFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10024 {"getProperty", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10025 {"getPropertyFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10026 {"hasProperty", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10027 {"hasPropertyFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10028 {"deleteProperty", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10029 {"deletePropertyFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10030 {"hasOwnProperty", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10031 {"hasOwnPropertyFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10032 {"setNamedPropertyCreateObject", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10033 {"setNamedProperty", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10034 {"setNamedPropertyFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10035 {"getNamedProperty", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10036 {"getNamedPropertyFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10037 {"hasNamedProperty", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10038 {"hasNamedPropertyFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10039 {"setElementWithLength", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10040 {"setElement", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10041 {"setElementFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10042 {"getElement", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10043 {"getElementFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10044 {"hasElement", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10045 {"hasElementFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10046 {"deleteElement", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10047 {"deleteElementAndCheck", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10048 {"deleteElementFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10049 {"defineMethodProperties", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10050 {"defineStringProperties", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10051 {"defineGetterProperties", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10052 {"definePropertiesFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10053 {"objectFreeze", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10054 {"objectSeal", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10055 {"getVersion", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10056 {"getVersionFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10057 {"getVMInfo", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10058 {"jsonStringify", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10059 {"jsonStringifyFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10060 {"jsonParseNumber", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10061 {"jsonParseObject", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10062 {"jsonParse", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10063 {"jsonParseFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10064 {"callFunction", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10065 {"callFunctionWithArg", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10066 {"callFunctionWithObject", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10067 {"callFunctionFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10068 {"createFunction", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10069 {"createFunctionFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10070 {"getHeapStatistics", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10071 {"getVM", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10072 {"getTypeof", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10073 {"instanceOf", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10074 {"isArray", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10075 {"isArrayBuffer", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10076 {"isTypedarray", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10077 {"isDataView", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10078 {"isStrictEquals", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10079 {"detachArraybuffer", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10080 {"isDetachedArraybuffer", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10081 {"coerceToBool", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10082 {"coerceToNumber", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10083 {"coerceToObject", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10084 {"coerceToString", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10085 {"isPromise", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10086 {"createPromise", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10087 {"resolveRejectDeferred", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10088 {"getCbArgs", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10089 {"newInstance", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10090 {"setTypeTagToObject", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10091 {"checkObjectTypeTag", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10092 {"adjustExternalMemory", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10093 {"memoryPressureNotification", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10094 {"wrapObject", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10095 {"removeWrap", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10096 {"handleScopeFor", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10097 {"jsVmThrow", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10098 {"jsVmThrowFromJS", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10099 {"jsVmThrowError", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10100 {"jsVmThrowTypeError", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10101 {"jsVmThrowRangeError", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10102 {"jsVmThrowSyntaxError", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10103 {"jsVmIsError", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10104 {"jsVmCreateError", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10105 {"jsVmCreateTypeError", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10106 {"jsVmCreateRangeError", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10107 {"jsVmCreateSyntaxError", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10108 {"jsVmGetLastErrorInfo", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10109 {"jsVmGetAndClearLastException", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10110 {"jsVmIsExceptionPending", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10111 {"createSnapshotFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10112 {"debugCodeUsingCpuProfiler", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10113 {"takeHeapSnapshot", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10114 {"debugCodeUsingInspector", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10115 {"waitForDebuggerFailed", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10116 {"setInstanceData", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10117 {"getInstanceData", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10118 {"isConstructor", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10119 {"createRegExp1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10120 {"createRegExp2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10121 {"createRegExp3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10122 {"createRegExp4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10123 {"createRegExp5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10124 {"createRegExpEnvNullptr", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10125 {"createFunctionWithScript", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10126 {"createMap", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10127 {"isRegExp", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10128 {"coerceToBigInt", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10129 {"testGetValueStringUtf8Test1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10130 {"testGetValueStringUtf8Test2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10131 {"testGetValueStringUtf8Test3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10132 {"testGetValueStringUtf8Test4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10133 {"testGetValueStringUtf8Test5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10134 {"testGetValueStringUtf8Test6", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10135 {"testCreateStringUtf8Test1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10136 {"testCreateStringUtf8Test2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10137 {"testCreateStringUtf8Test3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10138 {"testCreateStringUtf8Test4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10139 {"testCreateStringUtf16Test1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10140 {"testCreateStringUtf16Test2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10141 {"testCreateStringUtf16Test3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10142 {"testCreateStringUtf16Test4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10143 {"testGetValueStringUtf16Test1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10144 {"testGetValueStringUtf16Test2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10145 {"testGetValueStringUtf16Test3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10146 {"testGetValueStringUtf16Test4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10147 {"testGetValueStringUtf16Test5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10148 {"testGetValueStringUtf16Test6", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10149 {"testCreateStringLatin1Test1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10150 {"testCreateStringLatin1Test2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10151 {"testCreateStringLatin1Test3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10152 {"testCreateStringLatin1Test4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10153 {"testGetValueStringLatin1Test1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10154 {"testGetValueStringLatin1Test2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10155 {"testGetValueStringLatin1Test3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10156 {"testGetValueStringLatin1Test4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10157 {"testGetValueStringLatin1Test5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10158 {"testGetValueStringLatin1Test6", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10159 {"testIsStringTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10160 {"testIsStringTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10161 {"testIsStringTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10162 {"testOtherTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10163 {"testCreateArrayWithLength_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10164 {"testCreateArrayWithLength_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10165 {"testCreateArrayWithLength_03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10166 {"testCreateArrayWithLength_04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10167 {"testGetArrayLength_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10168 {"testGetArrayLength_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10169 {"testGetArrayLength_03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10170 {"testGetArrayLength_04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10171 {"testCreateTypedarray_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10172 {"testCreateTypedarray_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10173 {"testCreateTypedarray_03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10174 {"testCreateTypedarray_04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10175 {"testCreateTypedarray_05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10176 {"testCreateTypedarray_06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10177 {"testGetTypedarrayInfo_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10178 {"testGetTypedarrayInfo_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10179 {"testCreateDataview_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10180 {"testCreateDataview_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10181 {"testCreateDataview_03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10182 {"testCreateDataview_04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10183 {"testCreateDataview_05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10184 {"testCreateDataview_06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10185 {"testGetDataview_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10186 {"testGetDataview_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10187 {"testSetElement_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10188 {"testSetElement_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10189 {"testSetElement_03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10190 {"testSetElement_04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10191 {"testSetElement_05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10192 {"testSetElement_06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10193 {"testSetElement_07", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10194 {"testGetElement_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10195 {"testGetElement_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10196 {"testGetElement_03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10197 {"testGetElement_04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10198 {"testGetElement_05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10199 {"testGetElement_06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10200 {"testHasElement_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10201 {"testHasElement_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10202 {"testHasElement_03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10203 {"testHasElement_04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10204 {"testHasElement_05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10205 {"testHasElement_06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10206 {"testDeleteElement_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10207 {"testDeleteElement_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10208 {"testDeleteElement_03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10209 {"testDeleteElement_04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10210 {"testDeleteElement_05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10211 {"testDeleteElement_06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10212 {"testArrayProcessSteps_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10213 {"testArrayProcessSteps_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10214 {"testTypedarrayProcessSteps_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10215 {"testTypedarrayProcessSteps_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10216 {"testArrayDataviewProcessSteps_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10217 {"testArrayDataviewProcessSteps_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10218 {"testTypeArrayAndDataviewProcessSteps_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10219 {"testTypeArraySetMultipleTimes_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10220 {"testArraySetMultipleTimes_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10221 {"testArrayGetMultipleTimes_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10222 {"testArrayDeleteMultipleTimes_01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10223 {"testArrayDeleteMultipleTimes_02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10224 {"testCreateErrorTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10225 {"testCreateErrorTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10226 {"testCreateErrorTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10227 {"testCreateErrorTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10228 {"testCreateErrorTest5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10229 {"testCreateTypeErrorTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10230 {"testCreateTypeErrorTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10231 {"testCreateTypeErrorTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10232 {"testCreateTypeErrorTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10233 {"testCreateTypeErrorTest5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10234 {"testCreateRangeErrorTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10235 {"testCreateRangeErrorTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10236 {"testCreateRangeErrorTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10237 {"testCreateRangeErrorTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10238 {"testCreateRangeErrorTest5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10239 {"testCreateSyntaxErrorTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10240 {"testCreateSyntaxErrorTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10241 {"testCreateSyntaxErrorTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10242 {"testCreateSyntaxErrorTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10243 {"testCreateSyntaxErrorTest5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10244 {"testThrowTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10245 {"testThrowTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10246 {"testThrowTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10247 {"testThrowTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10248 {"testThrowTest5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10249 {"testThrowTest6", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10250 {"testThrowErrorTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10251 {"testThrowErrorTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10252 {"testThrowErrorTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10253 {"testThrowTypeErrorTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10254 {"testThrowTypeErrorTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10255 {"testThrowTypeErrorTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10256 {"testThrowRangeErrorTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10257 {"testThrowRangeErrorTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10258 {"testThrowRangeErrorTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10259 {"testThrowSyntaxErrorTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10260 {"testThrowSyntaxErrorTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10261 {"testThrowSyntaxErrorTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10262 {"testIsErrorTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10263 {"testIsErrorTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10264 {"testGetAndClearLastExceptionTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10265 {"testGetAndClearLastExceptionTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10266 {"testGetAndClearLastExceptionTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10267 {"testIsExceptionPendingTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10268 {"testIsExceptionPendingTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10269 {"testIsExceptionPendingTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10270 {"testGetLastErrorInfoTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10271 {"testGetLastErrorInfoTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10272 {"testGetLastErrorInfoTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10273 {"testCombinationTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10274 {"testCombinationTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10275 {"testCombinationTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10276 {"testCombinationTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10277 {"testCombinationTest5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10278 {"testCombinationTest6", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10279 {"testCombinationTest7", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10280 {"testCombinationTest8", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10281 {"testCombinationTest9", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10282 {"testCombinationTest10", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10283 {"testCombinationTest11", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10284 {"testCombinationTest12", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10285 {"testCombinationTest13", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10286 {"testCombinationTest14", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10287 {"testCombinationTest15", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10288 {"testCombinationTest16", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10289 {"testCombinationTest17", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10290 {"testCombinationTest18", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10291 {"testCombinationTest19", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10292 {"testCombinationTest20", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10293 {"testCombinationTest21", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10294 {"testCombinationTest22", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10295 {"testCombinationTest23", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10296 {"testHasPropertyTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10297 {"testHasPropertyTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10298 {"testDeletePropertyTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10299 {"testDeletePropertyTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10300 {"testHasOwnPropertyTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10301 {"testHasOwnPropertyTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10302 {"testHasOwnPropertyTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10303 {"testGetNamedPropertyTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10304 {"testGetNamedPropertyTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10305 {"testGetNamedPropertyTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10306 {"testHasNamedPropertyTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10307 {"testHasNamedPropertyTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10308 {"testHasNamedPropertyTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10309 {"testHasNamedPropertyTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10310 {"testDefinePropertiesTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10311 {"testDefinePropertiesTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10312 {"testDefinePropertiesTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10313 {"testDefinePropertiesTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10314 {"testCombinationPropertyTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10315 {"testCombinationPropertyTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10316 {"testCombinationPropertyTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10317 {"testCombinationPropertyTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10318 {"testCombinationNamedPropertyTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10319 {"testCombinationNamedPropertyTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10320 {"testCreateObjectTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10321 {"testCreateObjectTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10322 {"testTypeofTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10323 {"testTypeofTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10324 {"testTypeofTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10325 {"testTypeofTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10326 {"testTypeofTest5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10327 {"testTypeofTest6", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10328 {"testTypeofTest7", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10329 {"testTypeofTest8", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10330 {"testTypeofTest9", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10331 {"testTypeofTest10", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10332 {"testTypeofTest11", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10333 {"testTypeofTest12", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10334 {"testTypeTagObjectTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10335 {"testTypeTagObjectTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10336 {"testTypeTagObjectTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10337 {"testTypeTagObjectTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10338 {"testCheckObjectTypeTagTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10339 {"testCheckObjectTypeTagTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10340 {"testCheckObjectTypeTagTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10341 {"testCheckObjectTypeTagTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10342 {"testCheckObjectTypeTagTest5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10343 {"testObjectCombinationTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10344 {"testObjectCombinationTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10345 {"testObjectCombinationTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10346 {"testInitTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10347 {"testCreateVMTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10348 {"testCreateVMTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10349 {"testDestroyVMTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10350 {"testDestroyVMTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10351 {"testOpenVMScopeTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10352 {"testOpenVMScopeTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10353 {"testCloseVMScopeTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10354 {"testCloseVMScopeTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10355 {"testCreateEnvTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10356 {"testCreateEnvTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10357 {"testDestroyEnvTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10358 {"testDestroyEnvTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10359 {"testOpenEnvScopeTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10360 {"testOpenEnvScopeTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10361 {"testOpenEnvScopeTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10362 {"testCloseEnvScopeTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10363 {"testCloseEnvScopeTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10364 {"testDataTypeCombinationTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10365 {"testCreateDateTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10366 {"testGetDateValueTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10367 {"testIsDateTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10368 {"testDateCombinationTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10369 {"testPumpMessageLoopTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10370 {"testPerformMicrotaskCheckpointTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10371 {"testExecuteTasksCombinationTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10372 {"testJsonParseTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10373 {"testJsonParseTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10374 {"testJsonParseTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10375 {"testJsonStringifyTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10376 {"testJsonStringifyTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10377 {"testJsonStringifyTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10378 {"testJsonCombinationTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10379 {"testStrictEqualsTest1", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10380 {"testStrictEqualsTest2", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10381 {"testStrictEqualsTest3", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10382 {"testStrictEqualsTest4", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10383 {"testStrictEqualsTest5", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10384 {"testStrictEqualsTest6", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10385 {"testStrictEqualsTest7", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10386 {"testStrictEqualsTest8", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10387 {"testStrictEqualsTest9", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10388 {"testStrictEqualsTest10", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10389 {"testStrictEqualsTest11", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10390 {"testStrictEqualsTest12", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10391 {"testStrictEqualsTest13", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10392 {"testStrictEqualsTest14", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10393 {"testStrictEqualsTest15", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10394 {"testStrictEqualsTest16", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10395 {"testStrictEqualsTest17", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10396 {"testStrictEqualsTest18", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10397 {"testStrictEqualsTest19", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10398 {"testCreateArrayBufferCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10399 {"testCreateArrayBufferCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10400 {"testCreateArrayBufferCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10401 {"testCreateArrayBufferCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10402 {"testGetArrayBufferCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10403 {"testGetArrayBufferCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10404 {"testIsArrayBufferCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10405 {"testIsArrayBufferCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10406 {"testIsArrayBufferCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10407 {"testDetachArrayBufferCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10408 {"testDetachArrayBufferCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10409 {"testDetachArrayBufferCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10410 {"testIsDetachedArraybufferCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10411 {"testIsDetachedArraybufferCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10412 {"testIsDetachedArraybufferCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10413 {"testIsDetachedArraybufferCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10414 {"testIsDetachedArraybufferCase05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10415 {"testCreateAnyBufferDetachedCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10416 {"testCreateInt32Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10417 {"testCreateInt32Case02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10418 {"testCreateInt32Case03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10419 {"testCreateInt32Case04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10420 {"testCreateUint32Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10421 {"testCreateUint32Case02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10422 {"testCreateUint32Case03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10423 {"testCreateUint32Case04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10424 {"testCreateInt64Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10425 {"testCreateInt64Case02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10426 {"testCreateInt64Case03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10427 {"testCreateInt64Case04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10428 {"testCreateDoubleCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10429 {"testCreateDoubleCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10430 {"testCreateDoubleCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10431 {"testCreateDoubleCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10432 {"testGetValueUint32Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10433 {"testGetValueInt32Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10434 {"testGetValueInt64Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10435 {"testGetValueDoubleCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10436 {"testBasicDataTypesCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10437 {"testBasicDataTypesCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10438 {"testBasicDataTypesCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10439 {"testBasicDataTypesCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10440 {"testCreateBigintInt64Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10441 {"testCreateBigintInt64Case02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10442 {"testCreateBigintInt64Case03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10443 {"testCreateBigintInt64Case04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10444 {"testCreateBigintUint64Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10445 {"testCreateBigintUint64Case02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10446 {"testCreateBigintUint64Case03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10447 {"testCreateBigintWordsCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10448 {"testCreateBigintWordsCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10449 {"testCreateBigintWordsCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10450 {"testCreateBigintWordsCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10451 {"testCreateBigintWordsCase05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10452 {"testGetValueBigintInt64Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10453 {"testGetValueBigintInt64Case02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10454 {"testGetValueBigintInt64Case03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10455 {"testGetValueBigintInt64Case04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10456 {"testGetValueBigintInt64Case05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10457 {"testGetValueBigintInt64Case06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10458 {"testGetValueBigintUint64Case01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10459 {"testGetValueBigintUint64Case02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10460 {"testGetValueBigintUint64Case03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10461 {"testGetValueBigintUint64Case04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10462 {"testGetValueBigintUint64Case05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10463 {"testGetValueBigintUint64Case06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10464 {"testGetValueBigintWordsCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10465 {"testGetValueBigintWordsCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10466 {"testGetValueBigintWordsCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10467 {"testGetValueBigintWordsCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10468 {"testGetValueBigintWordsCase05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10469 {"testCreateAndGetBigIntProgramCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10470 {"testCreateAndGetBigIntProgramCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10471 {"testCreateAndGetBigIntProgramCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10472 {"testNewInstanceCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10473 {"testNewInstanceCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10474 {"testGetNewTargetCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10475 {"testGetNewTargetCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10476 {"testGetNewTargetCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10477 {"testDefineClassCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10478 {"testDefineClassCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10479 {"testWrapCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10480 {"testWrapCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10481 {"testWrapCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10482 {"testWrapCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10483 {"testUnwrapCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10484 {"testUnwrapCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10485 {"testUnwrapCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10486 {"testUnwrapCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10487 {"testRemoveWrapCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10488 {"testRemoveWrapCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10489 {"testRemoveWrapCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10490 {"testRemoveWrapCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10491 {"testClassOperationCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10492 {"testCallFunction01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10493 {"testCallFunction02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10494 {"testCallFunction03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10495 {"testCallFunction04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10496 {"testCallFunction05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10497 {"testCallFunction06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10498 {"testCallFunction07", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10499 {"testCallFunction08", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10500 {"testCallFunction09", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10501 {"testCreateFunction01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10502 {"testCreateFunction02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10503 {"testCreateFunction03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10504 {"testCreateFunction04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10505 {"testCreateFunction05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10506 {"testCreateFunction06", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10507 {"testGetCbInfo01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10508 {"testGetCbInfo02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10509 {"testGetCbInfo03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10510 {"testGetCbInfo04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10511 {"testGetCbInfo05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10512 {"testIsFunction01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10513 {"testIsFunction02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10514 {"testIsFunction03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10515 {"testIsFunction04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10516 {"testOpenHandleScopeCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10517 {"testOpenHandleScopeCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10518 {"testOpenHandleScopeCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10519 {"testOpenHandleScopeCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10520 {"testCloseHandleScopeCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10521 {"testCloseHandleScopeCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10522 {"testOpenAndCloseHandleScopeCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10523 {"testOpenAndCloseHandleScopeCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10524 {"testOpenEscapableHandleScopeCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10525 {"testOpenEscapableHandleScopeCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10526 {"testOpenEscapableHandleScopeCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10527 {"testCloseEscapableHandleScopeCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10528 {"testCloseEscapableHandleScopeCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10529 {"testCloseEscapableHandleScopeCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10530 {"testEscapeHandleCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10531 {"testEscapeHandleCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10532 {"testEscapeHandleCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10533 {"testEscapeHandleCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10534 {"testEscapeHandleCase05", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10535 {"testOpenClosedEscapeCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10536 {"testOpenClosedEscapeCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10537 {"testCreateReferenceCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10538 {"testCreateReferenceCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10539 {"testCreateReferenceCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10540 {"testDeleteReferenceCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10541 {"testDeleteReferenceCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10542 {"testReferenceRefCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10543 {"testReferenceRefCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10544 {"testReferenceRefCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10545 {"testReferenceUnrefCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10546 {"testReferenceUnrefCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10547 {"testReferenceUnrefCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10548 {"testGetReferenceValueCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10549 {"testGetReferenceValueCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10550 {"testHandleAndRefCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10551 {"testHandleAndRefCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10552 {"testHandleAndRefCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10553 {"testAdjustExternalMemoryCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10554 {"testAdjustExternalMemoryCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10555 {"testAdjustExternalMemoryCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10556 {"testAdjustExternalMemoryCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10557 {"testMemoryPressureNotificationCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10558 {"testMemoryPressureNotificationCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10559 {"testMemoryPressureNotificationCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10560 {"testMemoryManagementuseCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10561 {"testIsPromiseCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10562 {"testIsPromiseCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10563 {"testIsPromiseCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10564 {"testIsPromiseCase04", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10565 {"testCreatePromiseCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10566 {"testResolveDeferredCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10567 {"testResolveDeferredCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10568 {"testRejectDeferredCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10569 {"testRejectDeferredCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10570 {"testCreateResolveDeferredCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10571 {"testGetVersionCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10572 {"testGetVMInfoCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10573 {"testGetVMInfoCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10574 {"testGetHeapStatisticsCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10575 {"testStartCpuProfilerCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10576 {"testStopCpuProfilerCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10577 {"testTakeHeapSnapshotCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10578 {"testOpenInspectorCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10579 {"testOpenInspectorCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10580 {"testOpenInspectorCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10581 {"testCloseInspectorCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10582 {"testCloseInspectorCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10583 {"testCloseInspectorCase03", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10584 {"testOpenInspectorWithNameCase01", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10585 {"testOpenInspectorWithNameCase02", nullptr, nullptr, nullptr, nullptr, nullptr, JSVM_DEFAULT},
10586 };
WaitPropertyDescriptor()10587 void WaitPropertyDescriptor()
10588 {
10589 size_t iSize = sizeof(jsDescriptor) / sizeof(JSVM_PropertyDescriptor);
10590 for (int i = 0; i < iSize; i++) {
10591 jsDescriptor[i].name = nullptr;
10592 jsDescriptor[i].method = ¶m[i];
10593 jsDescriptor[i].getter = nullptr;
10594 jsDescriptor[i].setter = nullptr;
10595 jsDescriptor[i].value = nullptr;
10596 jsDescriptor[i].attributes = JSVM_DEFAULT;
10597 }
10598 }
GetArrayType(JSVM_TypedarrayType typeNum)10599 napi_typedarray_type GetArrayType(JSVM_TypedarrayType typeNum)
10600 {
10601 napi_typedarray_type arrayType;
10602 if (typeNum >= JSVM_INT8_ARRAY && typeNum <= JSVM_BIGUINT64_ARRAY) {
10603 arrayType = static_cast<napi_typedarray_type>(typeNum);
10604 } else {
10605 arrayType = napi_int8_array;
10606 }
10607 return arrayType;
10608 }
10609
GetArraySize(JSVM_TypedarrayType typeNum)10610 size_t GetArraySize(JSVM_TypedarrayType typeNum)
10611 {
10612 size_t elementSize = 0;
10613 switch (typeNum) {
10614 case JSVM_INT8_ARRAY:
10615 case JSVM_UINT8_ARRAY:
10616 case JSVM_UINT8_CLAMPED_ARRAY:
10617 elementSize = sizeof(int8_t);
10618 break;
10619 case JSVM_INT16_ARRAY:
10620 case JSVM_UINT16_ARRAY:
10621 elementSize = sizeof(int16_t);
10622 break;
10623 case JSVM_INT32_ARRAY:
10624 case JSVM_UINT32_ARRAY:
10625 elementSize = sizeof(int32_t);
10626 break;
10627 case JSVM_FLOAT32_ARRAY:
10628 elementSize = sizeof(float);
10629 break;
10630 case JSVM_FLOAT64_ARRAY:
10631 elementSize = sizeof(double);
10632 break;
10633 case JSVM_BIGINT64_ARRAY:
10634 case JSVM_BIGUINT64_ARRAY:
10635 elementSize = sizeof(int64_t);
10636 break;
10637 default:
10638 elementSize = sizeof(int8_t);
10639 break;
10640 }
10641 return elementSize;
10642 }
10643
10644 void ConvertResults(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue);
10645
ConvertString(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10646 void ConvertString(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10647 {
10648 if (strcmp(g_dataType, "utf8") == 0) {
10649 size_t length;
10650 OH_JSVM_GetValueStringUtf8(jsVmEnv, jSVmValue, nullptr, 0, &length);
10651 if (length <= 0) {
10652 return;
10653 }
10654 char *buffer = (char *)malloc(length + 1);
10655 if (buffer == nullptr) {
10656 return;
10657 }
10658 OH_JSVM_GetValueStringUtf8(jsVmEnv, jSVmValue, buffer, length + 1, nullptr);
10659 napi_create_string_utf8(napiEnv, buffer, length, napiValue);
10660 free(buffer);
10661 } else if (strcmp(g_dataType, "utf16") == 0) {
10662 char16_t buffer[128];
10663 size_t bufferSize = 128;
10664 size_t copied = 0;
10665 OH_JSVM_GetValueStringUtf16(jsVmEnv, jSVmValue, buffer, bufferSize, &copied);
10666 napi_create_string_utf16(napiEnv, buffer, copied, napiValue);
10667 } else if (strcmp(g_dataType, "latin") == 0) {
10668 size_t length;
10669 OH_JSVM_GetValueStringLatin1(jsVmEnv, jSVmValue, nullptr, 0, &length);
10670 if (length <= 0) {
10671 return;
10672 }
10673 char *buffer = (char *)malloc(length + 1);
10674 if (buffer == nullptr) {
10675 return;
10676 }
10677 OH_JSVM_GetValueStringLatin1(jsVmEnv, jSVmValue, buffer, length + 1, nullptr);
10678 napi_create_string_latin1(napiEnv, buffer, length, napiValue);
10679 free(buffer);
10680 } else {
10681 size_t length;
10682 OH_JSVM_GetValueStringUtf8(jsVmEnv, jSVmValue, nullptr, 0, &length);
10683 if (length <= 0) {
10684 return;
10685 }
10686 char *buffer = (char *)malloc(length + 1);
10687 if (buffer == nullptr) {
10688 return;
10689 }
10690 OH_JSVM_GetValueStringUtf8(jsVmEnv, jSVmValue, buffer, length + 1, nullptr);
10691 napi_create_string_utf8(napiEnv, buffer, length, napiValue);
10692 free(buffer);
10693 }
10694 }
10695
ConvertNumber(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10696 void ConvertNumber(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10697 {
10698 if (strcmp(g_dataType, "double") == 0) {
10699 double doubleValue;
10700 JSVM_Status doubleStatus = OH_JSVM_GetValueDouble(jsVmEnv, jSVmValue, &doubleValue);
10701 if (doubleStatus == JSVM_OK) {
10702 napi_create_double(napiEnv, doubleValue, napiValue);
10703 } else {
10704 napi_create_double(napiEnv, 0, napiValue);
10705 }
10706 } else if (strcmp(g_dataType, "int") == 0) {
10707 int64_t value;
10708 JSVM_Status int64Status = OH_JSVM_GetValueInt64(jsVmEnv, jSVmValue, &value);
10709 if (int64Status == JSVM_OK) {
10710 napi_create_int64(napiEnv, value, napiValue);
10711 } else {
10712 napi_create_int64(napiEnv, 0, napiValue);
10713 }
10714 } else {
10715 int64_t value;
10716 JSVM_Status int64Status = OH_JSVM_GetValueInt64(jsVmEnv, jSVmValue, &value);
10717 if (int64Status == JSVM_OK) {
10718 napi_create_int64(napiEnv, value, napiValue);
10719 } else {
10720 napi_create_int64(napiEnv, 0, napiValue);
10721 }
10722 }
10723 }
10724
ConvertBigInt(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10725 void ConvertBigInt(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10726 {
10727 if (strcmp(g_dataType, "BigintInt64") == 0) {
10728 int64_t intValue;
10729 bool lossless = false;
10730 OH_JSVM_GetValueBigintInt64(jsVmEnv, jSVmValue, &intValue, &lossless);
10731 napi_create_bigint_int64(napiEnv, intValue, napiValue);
10732 }
10733 if (strcmp(g_dataType, "BigintUint64") == 0) {
10734 uint64_t uintValue;
10735 bool lossless = false;
10736 OH_JSVM_GetValueBigintUint64(jsVmEnv, jSVmValue, &uintValue, &lossless);
10737 napi_create_bigint_uint64(napiEnv, uintValue, napiValue);
10738 }
10739 if (strcmp(g_dataType, "BigintWords") == 0) {
10740 int retSignBit = 0;
10741 size_t retWordCount = 4;
10742 uint64_t wordsOut[] = {0ULL, 0ULL, 0ULL};
10743 OH_JSVM_GetValueBigintWords(jsVmEnv, jSVmValue, &retSignBit, &retWordCount, wordsOut);
10744 napi_create_bigint_words(napiEnv, 0, DIFF_VALUE_THREE, wordsOut, napiValue);
10745 }
10746 }
10747
ConvertObjectArray(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10748 void ConvertObjectArray(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10749 {
10750 uint32_t length;
10751 OH_JSVM_GetArrayLength(jsVmEnv, jSVmValue, &length);
10752 napi_create_array(napiEnv, napiValue);
10753 for (uint32_t i = 0; i < length; i++) {
10754 JSVM_Value jsvmElement;
10755 OH_JSVM_GetElement(jsVmEnv, jSVmValue, i, &jsvmElement);
10756 GetResultType(jsVmEnv, jsvmElement);
10757 napi_value napiElement;
10758 ConvertResults(jsVmEnv, jsvmElement, napiEnv, &napiElement);
10759 napi_set_element(napiEnv, *napiValue, i, napiElement);
10760 }
10761 }
10762
ConvertObjectObjStr(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10763 void ConvertObjectObjStr(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10764 {
10765 JSVM_Value propNames = nullptr;
10766 OH_JSVM_GetPropertyNames(jsVmEnv, jSVmValue, &propNames);
10767 bool isArray = false;
10768 OH_JSVM_IsArray(jsVmEnv, propNames, &isArray);
10769 uint32_t arrayLength = 0;
10770 OH_JSVM_GetArrayLength(jsVmEnv, propNames, &arrayLength);
10771 napi_create_object(napiEnv, napiValue);
10772 for (uint32_t i = 0; i < arrayLength; i++) {
10773 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
10774 JSVM_Value propName = nullptr;
10775 OH_JSVM_GetElement(jsVmEnv, propNames, i, &propName);
10776 napi_value propNameNapi;
10777 ConvertString(jsVmEnv, propName, napiEnv, &propNameNapi);
10778 JSVM_Value propValue = nullptr;
10779 OH_JSVM_GetProperty(jsVmEnv, jSVmValue, propName, &propValue);
10780 napi_value propValueNapi;
10781 GetResultType(jsVmEnv, propValue);
10782 ConvertResults(jsVmEnv, propValue, napiEnv, &propValueNapi);
10783 napi_set_property(napiEnv, *napiValue, propNameNapi, propValueNapi);
10784 }
10785 }
10786
ConvertObjectArraybuffer(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10787 void ConvertObjectArraybuffer(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10788 {
10789 JSVM_Value byteLen;
10790 OH_JSVM_GetNamedProperty(jsVmEnv, jSVmValue, "byteLength", &byteLen);
10791 int32_t length;
10792 OH_JSVM_GetValueInt32(jsVmEnv, byteLen, &length);
10793 void *data;
10794 napi_create_arraybuffer(napiEnv, length, &data, napiValue);
10795 }
10796
ConvertObjectDate(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10797 void ConvertObjectDate(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10798 {
10799 double value;
10800 OH_JSVM_GetDateValue(jsVmEnv, jSVmValue, &value);
10801 napi_create_date(napiEnv, value, napiValue);
10802 }
10803
ConvertObjectArrayBufferInfo(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10804 void ConvertObjectArrayBufferInfo(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10805 {
10806 JSVM_Value proLen;
10807 OH_JSVM_GetNamedProperty(jsVmEnv, jSVmValue, "byteLength", &proLen);
10808 int32_t len;
10809 OH_JSVM_GetValueInt32(jsVmEnv, proLen, &len);
10810
10811 napi_create_object(napiEnv, napiValue);
10812 napi_value length;
10813 napi_create_uint32(napiEnv, len, &length);
10814 napi_set_named_property(napiEnv, *napiValue, "byteLength", length);
10815 JSVM_Value vmBufferData;
10816 OH_JSVM_GetNamedProperty(jsVmEnv, jSVmValue, "buffer", &vmBufferData);
10817 size_t buffLength;
10818 void *data;
10819 OH_JSVM_GetArraybufferInfo(jsVmEnv, vmBufferData, &data, &buffLength);
10820 napi_value bufferData;
10821 napi_create_arraybuffer(napiEnv, len, &data, &bufferData);
10822 napi_set_named_property(napiEnv, *napiValue, "buffer", bufferData);
10823 }
10824
ConvertObjectTypeArray(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10825 void ConvertObjectTypeArray(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10826 {
10827 void *data;
10828 JSVM_TypedarrayType type;
10829 size_t byteOffset;
10830 size_t length;
10831 size_t elementSize = 0;
10832 JSVM_Value arraybuffer;
10833 OH_JSVM_GetTypedarrayInfo(jsVmEnv, jSVmValue, &type, &length, &data, &arraybuffer, &byteOffset);
10834 napi_typedarray_type arraytype;
10835 elementSize = GetArraySize(type);
10836 arraytype = GetArrayType(type);
10837 napi_value arrayBuffer;
10838 napi_create_arraybuffer(napiEnv, length * elementSize, (void **)&data, &arrayBuffer);
10839 napi_create_typedarray(napiEnv, arraytype, length, arrayBuffer, 0, napiValue);
10840 }
10841
ConvertJsObject(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10842 void ConvertJsObject(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10843 {
10844 JSVM_Value nameValue;
10845 JSVM_Value propNameValue;
10846 OH_JSVM_CreateStringUtf8(jsVmEnv, "name", JSVM_AUTO_LENGTH, &propNameValue);
10847
10848 OH_JSVM_GetProperty(jsVmEnv, jSVmValue, propNameValue, &nameValue);
10849 size_t bufsize = 0;
10850 OH_JSVM_GetValueStringUtf8(jsVmEnv, nameValue, nullptr, 0, &bufsize);
10851 if (bufsize <= 0) {
10852 printf("Bufsize error!\n");
10853 }
10854 char *buf = (char *)malloc(bufsize + 1);
10855 if (buf == nullptr) {
10856 printf("Memory allocation failed!\n");
10857 }
10858 OH_JSVM_GetValueStringUtf8(jsVmEnv, nameValue, buf, bufsize + 1, &bufsize);
10859 napi_create_object(napiEnv, napiValue);
10860 napi_value name;
10861 napi_create_string_utf8(napiEnv, "name", NAPI_AUTO_LENGTH, &name);
10862 napi_value value;
10863 napi_create_string_utf8(napiEnv, buf, NAPI_AUTO_LENGTH, &value);
10864 napi_set_property(napiEnv, *napiValue, name, value);
10865
10866 JSVM_Value idValue;
10867 JSVM_Value propIDValue;
10868 OH_JSVM_CreateStringUtf8(jsVmEnv, "id", JSVM_AUTO_LENGTH, &propIDValue);
10869 OH_JSVM_GetProperty(jsVmEnv, jSVmValue, propIDValue, &idValue);
10870 size_t idSize = 0;
10871 OH_JSVM_GetValueStringUtf8(jsVmEnv, idValue, nullptr, 0, &idSize);
10872 if (idSize > 0) {
10873 char *idBuf = (char *)malloc(idSize + 1);
10874 if (idBuf == nullptr) {
10875 printf("Memory allocation failed!\n");
10876 }
10877 OH_JSVM_GetValueStringUtf8(jsVmEnv, idValue, idBuf, idSize + 1, &idSize);
10878 napi_value id;
10879 napi_create_string_utf8(napiEnv, "id", NAPI_AUTO_LENGTH, &id);
10880 napi_value id_value;
10881 napi_create_string_utf8(napiEnv, idBuf, NAPI_AUTO_LENGTH, &id_value);
10882 napi_set_property(napiEnv, *napiValue, id, id_value);
10883 }
10884 }
10885
ConvertObject(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10886 void ConvertObject(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10887 {
10888 if (strcmp(g_dataType, "array") == 0) {
10889 ConvertObjectArray(jsVmEnv, jSVmValue, napiEnv, napiValue);
10890 }
10891 if (strcmp(g_dataType, "arraybuffer") == 0) {
10892 ConvertObjectArraybuffer(jsVmEnv, jSVmValue, napiEnv, napiValue);
10893 }
10894 if (strcmp(g_dataType, "date") == 0) {
10895 ConvertObjectDate(jsVmEnv, jSVmValue, napiEnv, napiValue);
10896 }
10897 if (strcmp(g_dataType, "arraybufferinfo") == 0) {
10898 ConvertObjectArrayBufferInfo(jsVmEnv, jSVmValue, napiEnv, napiValue);
10899 }
10900 if (strcmp(g_dataType, "typearray") == 0) {
10901 ConvertObjectTypeArray(jsVmEnv, jSVmValue, napiEnv, napiValue);
10902 }
10903 if (strcmp(g_dataType, "object") == 0) {
10904 ConvertJsObject(jsVmEnv, jSVmValue, napiEnv, napiValue);
10905 }
10906 if (strcmp(g_dataType, "objectstr") == 0) {
10907 ConvertObjectObjStr(jsVmEnv, jSVmValue, napiEnv, napiValue);
10908 }
10909 }
10910
ConvertResults(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)10911 void ConvertResults(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
10912 {
10913 JSVM_ValueType resultType;
10914 OH_JSVM_Typeof(jsVmEnv, jSVmValue, &resultType);
10915 switch (resultType) {
10916 case JSVM_UNDEFINED: {
10917 napi_get_undefined(napiEnv, napiValue);
10918 break;
10919 }
10920 case JSVM_NULL: {
10921 napi_get_null(napiEnv, napiValue);
10922 break;
10923 }
10924 case JSVM_BOOLEAN: {
10925 bool value = false;
10926 OH_JSVM_GetValueBool(jsVmEnv, jSVmValue, &value);
10927 napi_get_boolean(napiEnv, value, napiValue);
10928 break;
10929 }
10930 case JSVM_NUMBER: {
10931 ConvertNumber(jsVmEnv, jSVmValue, napiEnv, napiValue);
10932 break;
10933 }
10934 case JSVM_STRING: {
10935 ConvertString(jsVmEnv, jSVmValue, napiEnv, napiValue);
10936 break;
10937 }
10938 case JSVM_OBJECT: {
10939 ConvertObject(jsVmEnv, jSVmValue, napiEnv, napiValue);
10940 break;
10941 }
10942 case JSVM_EXTERNAL: {
10943 void **result = nullptr;
10944 JSVM_Status externalStatus = OH_JSVM_GetValueExternal(jsVmEnv, jSVmValue, result);
10945 if (externalStatus == JSVM_OK) {
10946 napi_create_external(napiEnv, result, nullptr, nullptr, napiValue);
10947 }
10948 break;
10949 }
10950 case JSVM_BIGINT: {
10951 ConvertBigInt(jsVmEnv, jSVmValue, napiEnv, napiValue);
10952 break;
10953 }
10954 default: {
10955 break;
10956 }
10957 }
10958 }
10959
RunScriptFail(napi_env & nEnv,JSVM_Env & env)10960 void RunScriptFail(napi_env &nEnv, JSVM_Env &env)
10961 {
10962 JSVM_Value exceptionValue;
10963 JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &exceptionValue);
10964 if (status == JSVM_OK) {
10965 JSVM_Value message;
10966 OH_JSVM_GetNamedProperty(env, exceptionValue, "message", &message);
10967 size_t length = 0;
10968 OH_JSVM_GetValueStringUtf8(env, message, nullptr, 0, &length);
10969 if (length <= 0) {
10970 printf("Get length error!\n");
10971 }
10972 char *buffer = (char *)malloc(length + 1);
10973 if (buffer == nullptr) {
10974 printf("Memory allocation failed!\n");
10975 }
10976 OH_JSVM_GetValueStringUtf8(env, message, buffer, length + 1, nullptr);
10977 OH_JSVM_GetNamedProperty(env, exceptionValue, "name", &message);
10978 size_t nameLength;
10979 OH_JSVM_GetValueStringUtf8(env, message, nullptr, 0, &nameLength);
10980 if (nameLength <= 0) {
10981 printf("Get nameLength error!\n");
10982 }
10983 char *nameBuffer = (char *)malloc(nameLength + 1);
10984 if (nameBuffer == nullptr) {
10985 printf("Memory allocation failed!\n");
10986 }
10987 OH_JSVM_GetValueStringUtf8(env, message, nameBuffer, nameLength + 1, nullptr);
10988 if (strcmp(nameBuffer, "TypeError") == 0) {
10989 napi_throw_type_error(nEnv, nameBuffer, buffer);
10990 } else if (strcmp(nameBuffer, "RangeError") == 0) {
10991 napi_throw_range_error(nEnv, nameBuffer, buffer);
10992 } else {
10993 napi_throw_error(nEnv, "-1", buffer);
10994 }
10995 free(nameBuffer);
10996 free(buffer);
10997 } else {
10998 napi_throw_error(nEnv, "-1", nullptr);
10999 }
11000 }
11001
ExecuteJsScript(napi_env nEnv,JSVM_Env env,char * buf)11002 napi_value ExecuteJsScript(napi_env nEnv, JSVM_Env env, char *buf)
11003 {
11004 const char *sourceCodeStr = buf;
11005 JSVM_Value sourceCodeValue = nullptr;
11006 OH_JSVM_CreateStringUtf8(env, sourceCodeStr, strlen(sourceCodeStr), &sourceCodeValue);
11007 JSVM_Script script;
11008 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
11009 JSVM_Value jsVmResult = nullptr;
11010 napi_value nResult = nullptr;
11011 if (strcmp(sourceCodeStr, "defineClass") == 0) {
11012 JSVM_Value obj = nullptr;
11013 DefineClass(env, obj);
11014 nResult = GetObj(nEnv);
11015 } else {
11016 JSVM_Value sourceCodeValue = nullptr;
11017 OH_JSVM_CreateStringUtf8(env, sourceCodeStr, strlen(sourceCodeStr), &sourceCodeValue);
11018 JSVM_Script script;
11019 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
11020 JSVM_Status res = OH_JSVM_RunScript(env, script, &jsVmResult);
11021 if (res != JSVM_OK) {
11022 RunScriptFail(nEnv, env);
11023 }
11024 ConvertResults(env, jsVmResult, nEnv, &nResult);
11025 }
11026 return nResult;
11027 }
11028
GetJsCode(napi_env nEnv,napi_callback_info nInfo,char * buf)11029 void GetJsCode(napi_env nEnv, napi_callback_info nInfo, char *buf)
11030 {
11031 size_t argc = 1;
11032 napi_value argv[1];
11033 napi_get_cb_info(nEnv, nInfo, &argc, argv, nullptr, nullptr);
11034 napi_get_value_string_utf8(nEnv, argv[0], buf, RUN_JS_CODE_LENGTH, nullptr);
11035 }
11036
RunJsVm(napi_env nEnv,napi_callback_info nInfo)11037 static napi_value RunJsVm(napi_env nEnv, napi_callback_info nInfo)
11038 {
11039 char *buf = (char *)malloc(RUN_JS_CODE_LENGTH);
11040 WaitPropertyDescriptor();
11041 if (buf == nullptr) {
11042 return nullptr;
11043 }
11044 GetJsCode(nEnv, nInfo, buf);
11045 JSVM_InitOptions initOptions;
11046 if (memset_s(&initOptions, sizeof(initOptions), 0, sizeof(initOptions)) != EOK) {
11047 return nullptr;
11048 }
11049 initOptions.externalReferences = g_externalsSnapshot;
11050 if (aa == 0) {
11051 aa++;
11052 OH_JSVM_Init(&initOptions);
11053 }
11054 JSVM_VM vm;
11055 JSVM_CreateVMOptions options;
11056 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
11057 return nullptr;
11058 }
11059 OH_JSVM_CreateVM(&options, &vm);
11060 JSVM_VMScope vmScope;
11061 OH_JSVM_OpenVMScope(vm, &vmScope);
11062 JSVM_Env env;
11063 OH_JSVM_CreateEnv(vm, sizeof(jsDescriptor) / sizeof(jsDescriptor[0]), jsDescriptor, &env);
11064 JSVM_EnvScope envScope;
11065 OH_JSVM_OpenEnvScope(env, &envScope);
11066 JSVM_HandleScope handleScope;
11067 OH_JSVM_OpenHandleScope(env, &handleScope);
11068 napi_value nResult = ExecuteJsScript(nEnv, env, buf);
11069 if (nResult == nullptr) {
11070 return nullptr;
11071 }
11072 OH_JSVM_CloseHandleScope(env, handleScope);
11073 OH_JSVM_CloseEnvScope(env, envScope);
11074 OH_JSVM_CloseVMScope(vm, vmScope);
11075 OH_JSVM_DestroyEnv(env);
11076 OH_JSVM_DestroyVM(vm);
11077 return nResult;
11078 }
11079
11080 #include <iostream>
11081
WasmTest(napi_env env,napi_callback_info info)11082 [[maybe_unused]] static napi_value WasmTest(napi_env env, napi_callback_info info) {
11083 (void)RunTestsWithPrefix("test_wasm.cpp");
11084 napi_value result;
11085 napi_create_int32(env, 0, &result);
11086 return result;
11087 }
11088
ArrayBufferBackingStoreTest(napi_env env,napi_callback_info info)11089 [[maybe_unused]] static napi_value ArrayBufferBackingStoreTest(napi_env env, napi_callback_info info) {
11090 (void)RunTestsWithPrefix("test_array_buffer_backing_store.cpp");
11091 napi_value result;
11092 napi_create_int32(env, 0, &result);
11093 return result;
11094 }
11095
TraceTest(napi_env env,napi_callback_info info)11096 [[maybe_unused]] static napi_value TraceTest(napi_env env, napi_callback_info info) {
11097 (void)RunTestsWithPrefix("test_trace.cpp");
11098 napi_value result;
11099 napi_create_int32(env, 0, &result);
11100 return result;
11101 }
11102
SetMicrotaskPolicy(napi_env env,napi_callback_info info)11103 [[maybe_unused]] static napi_value SetMicrotaskPolicy(napi_env env, napi_callback_info info) {
11104 (void)RunTestsWithPrefix("test_microtask_policy.cpp");
11105 napi_value result;
11106 napi_create_int32(env, 0, &result);
11107 return result;
11108 }
11109
PromiseRegisterHandler(napi_env env,napi_callback_info info)11110 [[maybe_unused]] static napi_value PromiseRegisterHandler(napi_env env, napi_callback_info info) {
11111 (void)RunTestsWithPrefix("test_promise.cpp");
11112 napi_value result;
11113 napi_create_int32(env, 0, &result);
11114 return result;
11115 }
11116
ProxyTest(napi_env env,napi_callback_info info)11117 [[maybe_unused]] static napi_value ProxyTest(napi_env env, napi_callback_info info) {
11118 (void)RunTestsWithPrefix("test_proxy.cpp");
11119 napi_value result;
11120 napi_create_int32(env, 0, &result);
11121 return result;
11122 }
11123
WrapperObjectTest(napi_env env,napi_callback_info info)11124 [[maybe_unused]] static napi_value WrapperObjectTest(napi_env env, napi_callback_info info) {
11125 (void)RunTestsWithPrefix("test_wrapper_object.cpp");
11126 napi_value result;
11127 napi_create_int32(env, 0, &result);
11128 return result;
11129 }
11130
WellKnownSymbolsTest(napi_env env,napi_callback_info info)11131 [[maybe_unused]] static napi_value WellKnownSymbolsTest(napi_env env, napi_callback_info info) {
11132 (void)RunTestsWithPrefix("test_well_known_symbols.cpp");
11133 napi_value result;
11134 napi_create_int32(env, 0, &result);
11135 return result;
11136 }
11137
DefineClassWithOptionsTest(napi_env env,napi_callback_info info)11138 [[maybe_unused]] static napi_value DefineClassWithOptionsTest(napi_env env, napi_callback_info info) {
11139 (void)RunTestsWithPrefix("test_define_class_with_options.cpp");
11140 napi_value result;
11141 napi_create_int32(env, 0, &result);
11142 return result;
11143 }
11144
TriggerExceptionsTest(napi_env env,napi_callback_info info)11145 [[maybe_unused]] static napi_value TriggerExceptionsTest(napi_env env, napi_callback_info info) {
11146 (void)RunTestsWithPrefix("test_trigger_exceptions.cpp");
11147 napi_value result;
11148 napi_create_int32(env, 0, &result);
11149 return result;
11150 }
11151
TriggerGCTest(napi_env env,napi_callback_info info)11152 [[maybe_unused]] static napi_value TriggerGCTest(napi_env env, napi_callback_info info) {
11153 (void)RunTestsWithPrefix("test_trigger_gc.cpp");
11154 napi_value result;
11155 napi_create_int32(env, 0, &result);
11156 return result;
11157 }
11158
PrivateTest(napi_env env,napi_callback_info info)11159 [[maybe_unused]] static napi_value PrivateTest(napi_env env, napi_callback_info info) {
11160 (void)RunTestsWithPrefix("test_private.cpp");
11161 napi_value result;
11162 napi_create_int32(env, 0, &result);
11163 return result;
11164 }
11165
ExternalStringTest(napi_env env,napi_callback_info info)11166 [[maybe_unused]] static napi_value ExternalStringTest(napi_env env, napi_callback_info info) {
11167 (void)RunTestsWithPrefix("test_external_string.cpp");
11168 napi_value result;
11169 napi_create_int32(env, 0, &result);
11170 return result;
11171 }
11172
11173 EXTERN_C_START
11174
Init(napi_env env,napi_value exports)11175 static napi_value Init(napi_env env, napi_value exports)
11176 {
11177 napi_value typedArrayTypes;
11178 napi_create_object(env, &typedArrayTypes);
11179 const char* typeKeys[] = {
11180 "INT8_ARRAY", "UINT8_ARRAY", "UINT8_CLAMPED_ARRAY", "INT16_ARRAY", "UINT16_ARRAY",
11181 "INT32_ARRAY", "UINT32_ARRAY", "FLOAT32_ARRAY", "FLOAT64_ARRAY", "BIGINT64_ARRAY",
11182 "BIGUINT64_ARRAY"
11183 };
11184 for (int32_t i = 0; i < sizeof(typeKeys) / sizeof(typeKeys[0]); i++) {
11185 napi_value typeIndex;
11186 napi_create_int32(env, i, &typeIndex);
11187 napi_set_named_property(env, typedArrayTypes, typeKeys[i], typeIndex);
11188 }
11189 napi_property_descriptor properties[] = {
11190 DECLARE_NAPI_FUNCTION("testEngineAndContext",testEngineAndContext),
11191 DECLARE_NAPI_FUNCTION("testError",testError),
11192 DECLARE_NAPI_FUNCTION("testTypeError",testTypeError),
11193 DECLARE_NAPI_FUNCTION("testRangeError",testRangeError),
11194 DECLARE_NAPI_FUNCTION("testSyntaxError",testSyntaxError),
11195 DECLARE_NAPI_FUNCTION("testClearError",testClearError),
11196 DECLARE_NAPI_FUNCTION("testLifeCycle",testLifeCycle),
11197 DECLARE_NAPI_FUNCTION("testLifeCycle2", testLifeCycle2),
11198 DECLARE_NAPI_FUNCTION("testCreateData1",testCreateData1),
11199 DECLARE_NAPI_FUNCTION("testCreateData2",testCreateData2),
11200 DECLARE_NAPI_FUNCTION("testCreateData3",testCreateData3),
11201 DECLARE_NAPI_FUNCTION("testCreateData4",testCreateData4),
11202 DECLARE_NAPI_FUNCTION("testCreateData5",testCreateData5),
11203 DECLARE_NAPI_FUNCTION("testGetData1",testGetData1),
11204 DECLARE_NAPI_FUNCTION("testGetData2",testGetData2),
11205 DECLARE_NAPI_FUNCTION("testValueOperation",testValueOperation),
11206 DECLARE_NAPI_FUNCTION("testValueOperation2",testValueOperation2),
11207 DECLARE_NAPI_FUNCTION("testValueOperation3", testValueOperation3),
11208 DECLARE_NAPI_FUNCTION("testValueOperation4", testValueOperation4),
11209 DECLARE_NAPI_FUNCTION("testValueOperation5", testValueOperation5),
11210 DECLARE_NAPI_FUNCTION("testValueOperation6", testValueOperation6),
11211 DECLARE_NAPI_FUNCTION("testValueOperation7", testValueOperation7),
11212 DECLARE_NAPI_FUNCTION("testValueOperation8", testValueOperation8),
11213 DECLARE_NAPI_FUNCTION("testGetPropertyNames",testGetPropertyNames),
11214 DECLARE_NAPI_FUNCTION("testGetAllPropertyNames",testGetAllPropertyNames),
11215 DECLARE_NAPI_FUNCTION("testProperty",testProperty),
11216 DECLARE_NAPI_FUNCTION("testDefineProperties",testDefineProperties),
11217 DECLARE_NAPI_FUNCTION("testWrap",testWrap),
11218 DECLARE_NAPI_FUNCTION("testOthers",testOthers),
11219 DECLARE_NAPI_FUNCTION("Add",Add),
11220 DECLARE_NAPI_FUNCTION("Add1",Add1),
11221 DECLARE_NAPI_FUNCTION("testSecondOperations",testSecondOperations),
11222 DECLARE_NAPI_FUNCTION("testDefinePropertyHandle",testDefinePropertyHandle),
11223 DECLARE_NAPI_FUNCTION("testIsCallable", testIsCallable),
11224 DECLARE_NAPI_FUNCTION("testHandleMicrotasks", testHandleMicrotasks),
11225 DECLARE_NAPI_FUNCTION("runJsVm", RunJsVm),
11226 {"TypedArrayTypes", nullptr, nullptr, nullptr, nullptr, typedArrayTypes, napi_default, nullptr},
11227 DECLARE_NAPI_FUNCTION("testMultithreadFunction", testMultithreadFunction),
11228 DECLARE_NAPI_FUNCTION("testJswmInterface", testJswmInterface),
11229 DECLARE_NAPI_FUNCTION("testCompileWithOption", testCompileWithOption),
11230 DECLARE_NAPI_FUNCTION("testRetainScript", testRetainScript),
11231 DECLARE_NAPI_FUNCTION("testOpenInspectorWithName", testOpenInspectorWithName),
11232 DECLARE_NAPI_FUNCTION("testArrayBuffer", testArrayBuffer),
11233 DECLARE_NAPI_FUNCTION("testArrayBuffer2", testArrayBuffer2),
11234 DECLARE_NAPI_FUNCTION("testArrayBuffer3", testArrayBuffer3),
11235 DECLARE_NAPI_FUNCTION("testWasmOperator", testWasmOperator),
11236 DECLARE_NAPI_FUNCTION("wrapperObjectTest", WrapperObjectTest),
11237 DECLARE_NAPI_FUNCTION("wellKnownSymbolsTest", WellKnownSymbolsTest),
11238 DECLARE_NAPI_FUNCTION("traceTest", TraceTest),
11239 DECLARE_NAPI_FUNCTION("defineClassWithOptionsTest", DefineClassWithOptionsTest),
11240 DECLARE_NAPI_FUNCTION("triggerExceptionsTest", TriggerExceptionsTest),
11241 DECLARE_NAPI_FUNCTION("triggerGCTest", TriggerGCTest),
11242 DECLARE_NAPI_FUNCTION("SetMicrotaskPolicy", SetMicrotaskPolicy),
11243 DECLARE_NAPI_FUNCTION("PromiseRegisterHandler", PromiseRegisterHandler),
11244 DECLARE_NAPI_FUNCTION("ProxyTest", ProxyTest),
11245 DECLARE_NAPI_FUNCTION("privateTest", PrivateTest),
11246 DECLARE_NAPI_FUNCTION("externalStringTest", ExternalStringTest),
11247 DECLARE_NAPI_FUNCTION("wasmTest", WasmTest),
11248 DECLARE_NAPI_FUNCTION("arrayBufferBackingStoreTest", ArrayBufferBackingStoreTest),
11249 DECLARE_NAPI_FUNCTION("testSetDebugOption", testSetDebugOption),
11250 };
11251 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties));
11252 return exports;
11253 }
11254
11255 EXTERN_C_END
11256
11257 static napi_module demoModule = {
11258 .nm_version = 1,
11259 .nm_flags = 0,
11260 .nm_filename = nullptr,
11261 .nm_register_func = Init,
11262 .nm_modname = "napitest",
11263 .nm_priv = ((void *)0),
11264 .reserved = {0},
11265 };
11266
RegisterModule(void)11267 extern "C" __attribute__((constructor)) void RegisterModule(void)
11268 {
11269 napi_module_register(&demoModule);
11270 }
11271