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 <vector>
33 #define LOG_DEMAIN 0x0202
34 using namespace std;
35 static int aa = 0;
36 static bool g_temp = false;
37 const int DIFF_VALUE_ZERO = 0;
38 const int DIFF_VALUE_NOE = 1;
39 const int DIFF_VALUE_TWO = 2;
40 const int DIFF_VALUE_THREE = 3;
41 const int DIFF_VALUE_FOUR = 4;
42 const int DIFF_VALUE_FIVE = 5;
43 const int DIFF_VALUE_SIX = 6;
44 const int DIFF_VALUE_SEVEN = 7;
45 const int DIFF_VALUE_EIGHT = 8;
46 const int DIFF_VALUE_NINE = 9;
47 const int DIFF_VALUE_TEN = 10;
48 const int DIFF_VALUE_ELEVEN = 11;
49 const int DIFF_VALUE_TWELVE = 12;
50 const int DIFF_VALUE_THIRTEEN = 13;
51 const int DIFF_VALUE_FOURTEEN = 14;
52 const int DIFF_VALUE_FITEEN = 15;
53 const int DIFF_VALUE_SIXTEEN = 16;
54 const int DIFF_VALUE_SEVENTEEN = 17;
55 const int DIFF_VALUE_EIGHTEEN = 18;
56 const int DIFF_VALUE_NINETEEN = 19;
57 const int DIFF_VALUE_TWENTY = 20;
58 const int DIFF_VALUE_TWENTYONE = 21;
59 const int SCOPE_FOR_NUMBER = 100000;
60 const int RUN_JS_CODE_LENGTH = 1000;
61 const int DIFF_VALUE_INT32 = -20;
62 const int64_t DIFF_VALUE_INT = 2147483648;
63 const int64_t DIFF_VALUE_BIGINT = -5555555555555555555;
64 const uint64_t DIFF_VALUE_BIGUINT = 5555555555555555555;
65 const int32_t DIFF_VALUE_KEYLEN = 32;
66 const uint32_t DIFF_VALUE_UINT = 26;
67 const double DIFF_VALUE_DOUBLE = 1.234;
68 const double DIFF_VALUE_DATE = 1501924876711;
69 static char g_dataType[32] = "";
70 static bool g_threadFlag1 = false;
71 static bool g_threadFlag2 = false;
72 const int RUN_TEST_RETAIN_SCRIPT_VALUE = 121;
73 const int TWO = 2;
74 const int SIXTEEN = 16;
75
76 #define EOK 0
77 #define OPENCODE
78 #ifdef OPENCODE
addReturnedStatus(JSVM_Env env,const char * key,JSVM_Value object,const char * expected_message,JSVM_Status expected_status,JSVM_Status actual_status)79 static void addReturnedStatus(JSVM_Env env, const char *key, JSVM_Value object, const char *expected_message,
80 JSVM_Status expected_status, JSVM_Status actual_status) {
81 char napi_message_string[100] = "";
82 JSVM_Value prop_value;
83
84 if (actual_status != expected_status) {
85 printf("Invalid status [%d]", actual_status);
86 }
87
88 JSVM_CALL_RETURN_VOID(
89 env, OH_JSVM_CreateStringUtf8(env, (actual_status == expected_status ? expected_message : napi_message_string),
90 NAPI_AUTO_LENGTH, &prop_value));
91 JSVM_CALL_RETURN_VOID(env, OH_JSVM_SetNamedProperty(env, object, key, prop_value));
92 }
93
addLastStatus(JSVM_Env env,const char * key,JSVM_Value return_value)94 static void addLastStatus(JSVM_Env env, const char *key, JSVM_Value return_value) {
95 JSVM_Value prop_value;
96 const JSVM_ExtendedErrorInfo *p_last_error;
97 JSVM_CALL_RETURN_VOID(env, OH_JSVM_GetLastErrorInfo(env, &p_last_error));
98
99 JSVM_CALL_RETURN_VOID(
100 env, OH_JSVM_CreateStringUtf8(
101 env, (p_last_error->errorMessage == NULL ? "JSVM_Status::JSVM_OK" : p_last_error->errorMessage),
102 NAPI_AUTO_LENGTH, &prop_value));
103 JSVM_CALL_RETURN_VOID(env, OH_JSVM_SetNamedProperty(env, return_value, key, prop_value));
104 }
105 #endif
defineProperties(JSVM_Env env,JSVM_CallbackInfo info)106 static JSVM_Value defineProperties(JSVM_Env env, JSVM_CallbackInfo info) {
107 JSVM_Value object, return_value;
108 JSVM_CallbackStruct param;
109 param.data = nullptr;
110 param.callback = defineProperties;
111 JSVM_PropertyDescriptor desc = {"prop", NULL, ¶m, NULL, NULL, NULL, JSVM_PropertyAttributes::JSVM_ENUMERABLE};
112
113 JSVM_CALL(env, OH_JSVM_CreateObject(env, &object));
114 JSVM_CALL(env, OH_JSVM_CreateObject(env, &return_value));
115
116 addReturnedStatus(env, "envIsNull", return_value, "Invalid argument", JSVM_Status::JSVM_INVALID_ARG,
117 OH_JSVM_DefineProperties(NULL, object, 1, &desc));
118
119 OH_JSVM_DefineProperties(env, NULL, 1, &desc);
120 addLastStatus(env, "objectIsNull", return_value);
121
122 OH_JSVM_DefineProperties(env, object, 1, NULL);
123 addLastStatus(env, "descriptorListIsNull", return_value);
124
125 desc.utf8name = NULL;
126 OH_JSVM_DefineProperties(env, object, 1, NULL);
127 addLastStatus(env, "utf8nameIsNull", return_value);
128 desc.utf8name = "prop";
129
130 desc.method = NULL;
131 OH_JSVM_DefineProperties(env, object, 1, NULL);
132 addLastStatus(env, "methodIsNull", return_value);
133 desc.method = ¶m;
134
135 return return_value;
136 }
assertEqual(JSVM_Env env,JSVM_CallbackInfo info)137 static JSVM_Value assertEqual(JSVM_Env env, JSVM_CallbackInfo info) {
138 size_t argc = 2;
139 JSVM_Value args[2];
140 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL));
141
142 bool isStrictEquals = false;
143 OH_JSVM_StrictEquals(env, args[0], args[1], &isStrictEquals);
144 return nullptr;
145 }
146
assertTrue(JSVM_Env env,JSVM_CallbackInfo info)147 static JSVM_Value assertTrue(JSVM_Env env, JSVM_CallbackInfo info) {
148 size_t argc = 1;
149 JSVM_Value args[1];
150 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL));
151
152 bool value = false;
153 JSVM_CALL(env, OH_JSVM_GetValueBool(env, args[0], &value));
154 return nullptr;
155 }
156
assertFalse(JSVM_Env env,JSVM_CallbackInfo info)157 static JSVM_Value assertFalse(JSVM_Env env, JSVM_CallbackInfo info) {
158 size_t argc = 1;
159 JSVM_Value args[1];
160 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL));
161
162 bool value = false;
163 JSVM_CALL(env, OH_JSVM_GetValueBool(env, args[0], &value));
164 return nullptr;
165 }
166
consoleinfo(JSVM_Env env,JSVM_CallbackInfo info)167 static JSVM_Value consoleinfo(JSVM_Env env, JSVM_CallbackInfo info) {
168 size_t argc = 1;
169 JSVM_Value args[1];
170 char log[256] = "";
171 size_t log_length;
172 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL));
173
174 JSVM_CALL(env, OH_JSVM_GetValueStringUtf8(env, args[0], log, 255, &log_length));
175 return nullptr;
176 }
177
178
179 // OH_JSVM_IsArray
isArray(JSVM_Env env,JSVM_CallbackInfo info)180 static JSVM_Value isArray(JSVM_Env env, JSVM_CallbackInfo info) {
181 JSVM_Value array = nullptr;
182 OH_JSVM_CreateArray(env, &array);
183 JSVM_ASSERT(env, array != nullptr, "OH_JSVM_GetUndefined success");
184 bool isArray = false;
185 OH_JSVM_IsArray(env, array, &isArray);
186 JSVM_ASSERT(env, isArray, "OH_JSVM_IsArrayd success");
187
188 JSVM_Value _value;
189 JSVM_CALL(env, OH_JSVM_CreateInt32(env, 0, &_value));
190 return _value;
191 }
hello(JSVM_Env env,JSVM_CallbackInfo info)192 static JSVM_Value hello(JSVM_Env env, JSVM_CallbackInfo info) {
193 JSVM_Value output;
194 void *data = nullptr;
195 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, nullptr, &data);
196 OH_JSVM_CreateStringUtf8(env, (char *)data, strlen((char *)data), &output);
197 return output;
198 }
199 static JSVM_CallbackStruct hello_cb = {hello, (void *)"Hello"};
200 static intptr_t externals[] = {
201 (intptr_t)&hello_cb,
202 0,
203 };
204
205 static JSVM_PropertyHandlerConfigurationStruct propertyCfg{
206 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr
207 };
208
testEngineAndContext(napi_env env1,napi_callback_info info)209 static napi_value testEngineAndContext(napi_env env1, napi_callback_info info){
210 JSVM_InitOptions init_options;
211 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
212 printf("memset_s failed");
213 return nullptr;
214 }
215 init_options.externalReferences = externals;
216 if (aa == 0) {
217 OH_JSVM_Init(&init_options);
218 aa++;
219 }
220 JSVM_VM vm;
221 JSVM_CreateVMOptions options;
222 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
223 printf("memset_s failed");
224 return nullptr;
225 }
226 OH_JSVM_CreateVM(&options, &vm);
227 JSVM_VMScope vm_scope;
228 OH_JSVM_OpenVMScope(vm, &vm_scope);
229 JSVM_Env env;
230 JSVM_CallbackStruct param[1];
231 param[0].data = nullptr;
232 param[0].callback = assertEqual;
233
234
235 JSVM_PropertyDescriptor descriptor[] = {
236 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
237 };
238 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
239 JSVM_EnvScope envScope;
240 OH_JSVM_OpenEnvScope(env, &envScope);
241 JSVM_HandleScope handlescope;
242 OH_JSVM_OpenHandleScope(env, &handlescope);
243 const char *sourcecodestr = "\
244 {\
245 var length = 5; var arr = new Array(length); arr[4] = 123;\
246 }\
247 ";
248 JSVM_Value sourcecodevalue;
249 OH_JSVM_CreateStringUtf8(env, sourcecodestr, strlen(sourcecodestr), &sourcecodevalue);
250 JSVM_Script script;
251 OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script);
252 JSVM_Value result;
253 OH_JSVM_RunScript(env, script, &result);
254 OH_JSVM_CloseHandleScope(env, handlescope);
255 OH_JSVM_CloseEnvScope(env, envScope);
256 OH_JSVM_DestroyEnv(env);
257 OH_JSVM_CloseVMScope(vm, vm_scope);
258 OH_JSVM_DestroyVM(vm);
259 napi_value result11;
260 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
261 return result11;
262 }
testError(napi_env env1,napi_callback_info info)263 static napi_value testError(napi_env env1, napi_callback_info info){
264 JSVM_InitOptions init_options;
265 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
266 printf("memset_s failed");
267 return nullptr;
268 }
269 init_options.externalReferences = externals;
270 if (aa == 0) {
271 OH_JSVM_Init(&init_options);
272 aa++;
273 }
274 JSVM_VM vm;
275 JSVM_CreateVMOptions options;
276 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
277 printf("memset_s failed");
278 return nullptr;
279 }
280 OH_JSVM_CreateVM(&options, &vm);
281 JSVM_VMScope vm_scope;
282 OH_JSVM_OpenVMScope(vm, &vm_scope);
283 JSVM_Env env;
284 JSVM_CallbackStruct param[1];
285 param[0].data = nullptr;
286 param[0].callback = assertEqual;
287 JSVM_PropertyDescriptor descriptor[] = {
288 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
289 };
290 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
291 JSVM_EnvScope envScope;
292 OH_JSVM_OpenEnvScope(env, &envScope);
293 JSVM_HandleScope handlescope;
294 OH_JSVM_OpenHandleScope(env, &handlescope);
295 //error
296 JSVM_Value code = nullptr;
297 JSVM_Value message = nullptr;
298 OH_JSVM_CreateStringLatin1(env, "600", NAPI_AUTO_LENGTH, &code);
299 OH_JSVM_CreateStringLatin1(env, "test error", NAPI_AUTO_LENGTH, &message);
300 JSVM_Value error = nullptr;
301 OH_JSVM_CreateError(env, code, message, &error);
302 bool isError = false;
303 OH_JSVM_IsError(env, error, &isError);
304 OH_JSVM_ThrowError(env, "500", "Common error");
305 OH_JSVM_CloseHandleScope(env, handlescope);
306 OH_JSVM_CloseEnvScope(env, envScope);
307 OH_JSVM_DestroyEnv(env);
308 OH_JSVM_CloseVMScope(vm, vm_scope);
309 OH_JSVM_DestroyVM(vm);
310 napi_value result11;
311 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
312 return result11;
313 }
testTypeError(napi_env env1,napi_callback_info info)314 static napi_value testTypeError(napi_env env1, napi_callback_info info){
315 JSVM_InitOptions init_options;
316 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
317 printf("memset_s failed");
318 return nullptr;
319 }
320 init_options.externalReferences = externals;
321 if (aa == 0) {
322 OH_JSVM_Init(&init_options);
323 aa++;
324 }
325 JSVM_VM vm;
326 JSVM_CreateVMOptions options;
327 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
328 printf("memset_s failed");
329 return nullptr;
330 }
331 OH_JSVM_CreateVM(&options, &vm);
332 JSVM_VMScope vm_scope;
333 OH_JSVM_OpenVMScope(vm, &vm_scope);
334 JSVM_Env env;
335 JSVM_CallbackStruct param[1];
336 param[0].data = nullptr;
337 param[0].callback = assertEqual;
338
339 JSVM_PropertyDescriptor descriptor[] = {
340 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
341 };
342 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
343 JSVM_EnvScope envScope;
344 OH_JSVM_OpenEnvScope(env, &envScope);
345 JSVM_HandleScope handlescope;
346 OH_JSVM_OpenHandleScope(env, &handlescope);
347 // typeError
348 JSVM_Value code1 = nullptr;
349 JSVM_Value message1 = nullptr;
350 OH_JSVM_CreateStringLatin1(env, "500", NAPI_AUTO_LENGTH, &code1);
351 OH_JSVM_CreateStringLatin1(env, "type error 500", NAPI_AUTO_LENGTH, &message1);
352 JSVM_Value error1 = nullptr;
353 OH_JSVM_CreateTypeError(env, code1, message1, &error1);
354 bool isError1 = false;
355 OH_JSVM_IsError(env, error1, &isError1);
356 OH_JSVM_ThrowTypeError(env, NULL, "type error1");
357 OH_JSVM_CloseHandleScope(env, handlescope);
358 OH_JSVM_CloseEnvScope(env, envScope);
359 OH_JSVM_DestroyEnv(env);
360 OH_JSVM_CloseVMScope(vm, vm_scope);
361 OH_JSVM_DestroyVM(vm);
362 napi_value result11;
363 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
364 return result11;
365 }
testRangeError(napi_env env1,napi_callback_info info)366 static napi_value testRangeError(napi_env env1, napi_callback_info info){
367 JSVM_InitOptions init_options;
368 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
369 printf("memset_s failed");
370 return nullptr;
371 }
372 init_options.externalReferences = externals;
373 if (aa == 0) {
374 OH_JSVM_Init(&init_options);
375 aa++;
376 }
377 JSVM_VM vm;
378 JSVM_CreateVMOptions options;
379 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
380 printf("memset_s failed");
381 return nullptr;
382 }
383 OH_JSVM_CreateVM(&options, &vm);
384 JSVM_VMScope vm_scope;
385 OH_JSVM_OpenVMScope(vm, &vm_scope);
386 JSVM_Env env;
387 JSVM_CallbackStruct param[1];
388 param[0].data = nullptr;
389 param[0].callback = assertEqual;
390
391 JSVM_PropertyDescriptor descriptor[] = {
392 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
393 };
394 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
395 JSVM_EnvScope envScope;
396 OH_JSVM_OpenEnvScope(env, &envScope);
397 JSVM_HandleScope handlescope;
398 OH_JSVM_OpenHandleScope(env, &handlescope);
399 // rangeError
400 JSVM_Value code2 = nullptr;
401 JSVM_Value message2 = nullptr;
402 OH_JSVM_CreateStringLatin1(env, "500", NAPI_AUTO_LENGTH, &code2);
403 OH_JSVM_CreateStringLatin1(env, "range error 500", NAPI_AUTO_LENGTH, &message2);
404 JSVM_Value error2 = nullptr;
405 OH_JSVM_CreateRangeError(env, code2, message2, &error2);
406 bool isError2 = false;
407 OH_JSVM_IsError(env, error2, &isError2);
408 OH_JSVM_ThrowRangeError(env, NULL, "range error");
409 OH_JSVM_CloseHandleScope(env, handlescope);
410 OH_JSVM_CloseEnvScope(env, envScope);
411 OH_JSVM_DestroyEnv(env);
412 OH_JSVM_CloseVMScope(vm, vm_scope);
413 OH_JSVM_DestroyVM(vm);
414 napi_value result11;
415 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
416 return result11;
417 }
testSyntaxError(napi_env env1,napi_callback_info info)418 static napi_value testSyntaxError(napi_env env1, napi_callback_info info){
419 JSVM_InitOptions init_options;
420 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
421 printf("memset_s failed");
422 return nullptr;
423 }
424 init_options.externalReferences = externals;
425 if (aa == 0) {
426 OH_JSVM_Init(&init_options);
427 aa++;
428 }
429 JSVM_VM vm;
430 JSVM_CreateVMOptions options;
431 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
432 printf("memset_s failed");
433 return nullptr;
434 }
435 OH_JSVM_CreateVM(&options, &vm);
436 JSVM_VMScope vm_scope;
437 OH_JSVM_OpenVMScope(vm, &vm_scope);
438 JSVM_Env env;
439 JSVM_CallbackStruct param[1];
440 param[0].data = nullptr;
441 param[0].callback = assertEqual;
442
443 JSVM_PropertyDescriptor descriptor[] = {
444 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
445 };
446 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
447 JSVM_EnvScope envScope;
448 OH_JSVM_OpenEnvScope(env, &envScope);
449 JSVM_HandleScope handlescope;
450 OH_JSVM_OpenHandleScope(env, &handlescope);
451 // syntaxError
452 JSVM_Value code3 = nullptr;
453 JSVM_Value message3 = nullptr;
454 OH_JSVM_CreateStringLatin1(env, "500", NAPI_AUTO_LENGTH, &code3);
455 OH_JSVM_CreateStringLatin1(env, "syntax error 500", NAPI_AUTO_LENGTH, &message3);
456 JSVM_Value error3 = nullptr;
457 OH_JSVM_CreateSyntaxError(env, code3, message3, &error3);
458 bool isError3 = false;
459 OH_JSVM_IsError(env, error3, &isError3);
460 OH_JSVM_ThrowSyntaxError(env, NULL, "syntax error");
461 OH_JSVM_CloseHandleScope(env, handlescope);
462 OH_JSVM_CloseEnvScope(env, envScope);
463 OH_JSVM_DestroyEnv(env);
464 OH_JSVM_CloseVMScope(vm, vm_scope);
465 OH_JSVM_DestroyVM(vm);
466 napi_value result11;
467 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
468 return result11;
469 }
testClearError(napi_env env1,napi_callback_info info)470 static napi_value testClearError(napi_env env1, napi_callback_info info){
471 JSVM_InitOptions init_options;
472 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
473 printf("memset_s failed");
474 return nullptr;
475 }
476 init_options.externalReferences = externals;
477 if (aa == 0) {
478 OH_JSVM_Init(&init_options);
479 aa++;
480 }
481 JSVM_VM vm;
482 JSVM_CreateVMOptions options;
483 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
484 printf("memset_s failed");
485 return nullptr;
486 }
487 OH_JSVM_CreateVM(&options, &vm);
488 JSVM_VMScope vm_scope;
489 OH_JSVM_OpenVMScope(vm, &vm_scope);
490 JSVM_Env env;
491 JSVM_CallbackStruct param[1];
492 param[0].data = nullptr;
493 param[0].callback = assertEqual;
494
495 JSVM_PropertyDescriptor descriptor[] = {
496 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
497 };
498 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
499 JSVM_EnvScope envScope;
500 OH_JSVM_OpenEnvScope(env, &envScope);
501 JSVM_HandleScope handlescope;
502 OH_JSVM_OpenHandleScope(env, &handlescope);
503 // getLastErrorInfo
504 JSVM_Value value;
505 OH_JSVM_CreateStringUtf8(env, "xyz", 3, &value);
506 double double_value;
507 OH_JSVM_GetValueDouble(env, value, &double_value);
508 const JSVM_ExtendedErrorInfo *error_info = 0;
509 OH_JSVM_GetLastErrorInfo(env, &error_info);
510 // getAndClearLastException
511 JSVM_Value code4 = nullptr;
512 JSVM_Value message4 = nullptr;
513 OH_JSVM_CreateStringLatin1(env, "600", NAPI_AUTO_LENGTH, &code4);
514 OH_JSVM_CreateStringLatin1(env, "tag range error", NAPI_AUTO_LENGTH, &message4);
515 JSVM_Value error4 = nullptr;
516 OH_JSVM_CreateRangeError(env, code4, message4, &error4);
517 bool isError4 = false;
518 OH_JSVM_IsError(env, error4, &isError4);
519 JSVM_Value ex;
520 OH_JSVM_GetAndClearLastException(env, &ex);
521 bool exceptionWasPending = true;
522 OH_JSVM_IsExceptionPending(env, &exceptionWasPending);
523 OH_JSVM_Throw(env, error4);
524 OH_JSVM_IsExceptionPending(env, &exceptionWasPending);
525 JSVM_Value ex1;
526 OH_JSVM_GetAndClearLastException(env, &ex1);
527 isError4 = false;
528 OH_JSVM_IsError(env, ex1, &isError4);
529 OH_JSVM_IsExceptionPending(env, &exceptionWasPending);
530 OH_JSVM_CloseHandleScope(env, handlescope);
531 OH_JSVM_CloseEnvScope(env, envScope);
532 OH_JSVM_DestroyEnv(env);
533 // shaoshuai
534 OH_JSVM_CloseVMScope(vm, vm_scope);
535 OH_JSVM_DestroyVM(vm);
536 napi_value result11;
537 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
538 return result11;
539 }
testLifeCycle(napi_env env1,napi_callback_info info)540 static napi_value testLifeCycle(napi_env env1, napi_callback_info info){
541 JSVM_InitOptions init_options;
542 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
543 printf("memset_s failed");
544 return nullptr;
545 }
546 init_options.externalReferences = externals;
547 if (aa == 0) {
548 OH_JSVM_Init(&init_options);
549 aa++;
550 }
551 JSVM_VM vm;
552 JSVM_CreateVMOptions options;
553 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
554 printf("memset_s failed");
555 return nullptr;
556 }
557 OH_JSVM_CreateVM(&options, &vm);
558 JSVM_VMScope vm_scope;
559 OH_JSVM_OpenVMScope(vm, &vm_scope);
560 JSVM_Env env;
561 JSVM_CallbackStruct param[1];
562 param[0].data = nullptr;
563 param[0].callback = assertEqual;
564 JSVM_PropertyDescriptor descriptor[] = {
565 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
566 };
567 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
568 JSVM_EnvScope envScope;
569 OH_JSVM_OpenEnvScope(env, &envScope);
570 JSVM_HandleScope handlescope;
571 OH_JSVM_OpenHandleScope(env, &handlescope);
572 JSVM_EscapableHandleScope handlescope1;
573 OH_JSVM_OpenEscapableHandleScope(env,&handlescope1);
574 JSVM_Value output = NULL;
575 JSVM_Value escapee = NULL;
576 OH_JSVM_CreateObject(env, &output);
577 OH_JSVM_EscapeHandle(env, handlescope1, output, &escapee);
578 JSVM_Value result = nullptr;
579 JSVM_Ref resultRef = nullptr;
580 uint32_t resultRefCount = 0;
581 OH_JSVM_CreateObject(env, &result);
582 OH_JSVM_CreateReference(env, result, 1, &resultRef);
583 OH_JSVM_ReferenceRef(env, resultRef, &resultRefCount);
584 OH_JSVM_ReferenceUnref(env, resultRef, &resultRefCount);
585 JSVM_Value refValue = nullptr;
586 OH_JSVM_GetReferenceValue(env, resultRef, &refValue);
587 OH_JSVM_DeleteReference(env, resultRef);
588 OH_JSVM_CloseEscapableHandleScope(env,handlescope1);
589 OH_JSVM_CloseHandleScope(env, handlescope);
590 OH_JSVM_CloseEnvScope(env, envScope);
591 OH_JSVM_DestroyEnv(env);
592 OH_JSVM_CloseVMScope(vm, vm_scope);
593 OH_JSVM_DestroyVM(vm);
594 napi_value result11;
595 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
596 return result11;
597 }
CreateReference_test_1(JSVM_Env env)598 void CreateReference_test_1(JSVM_Env env)
599 {
600 JSVM_HandleScope handleScope;
601 OH_JSVM_OpenHandleScope(env, &handleScope);
602 JSVM_EscapableHandleScope handleScope1;
603 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
604 const char testStr[] = "foo";
605 JSVM_Value value = nullptr;
606 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
607 JSVM_Ref reference = nullptr;
608 OH_JSVM_CreateReference(env, value, 1, &reference);
609 JSVM_Value result = nullptr;
610 OH_JSVM_GetReferenceValue(env, reference, &result);
611 OH_JSVM_DeleteReference(env, reference);
612 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
613 OH_JSVM_CloseHandleScope(env, handleScope);
614 }
CreateReference_test_2(JSVM_Env env)615 void CreateReference_test_2(JSVM_Env env)
616 {
617 JSVM_HandleScope handleScope;
618 OH_JSVM_OpenHandleScope(env, &handleScope);
619 JSVM_EscapableHandleScope handleScope1;
620 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
621 JSVM_Value value = nullptr;
622 OH_JSVM_GetUndefined(env, &value);
623 JSVM_Ref reference = nullptr;
624 OH_JSVM_CreateReference(env, value, 1, &reference);
625 JSVM_Value result = nullptr;
626 OH_JSVM_GetReferenceValue(env, reference, &result);
627 OH_JSVM_DeleteReference(env, reference);
628 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
629 OH_JSVM_CloseHandleScope(env, handleScope);
630 }
CreateReference_test_3(JSVM_Env env)631 void CreateReference_test_3(JSVM_Env env)
632 {
633 JSVM_HandleScope handleScope;
634 OH_JSVM_OpenHandleScope(env, &handleScope);
635 JSVM_EscapableHandleScope handleScope1;
636 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
637 JSVM_Value value = nullptr;
638 OH_JSVM_GetNull(env, &value);
639 JSVM_Ref reference = nullptr;
640 OH_JSVM_CreateReference(env, value, 1, &reference);
641 JSVM_Value result = nullptr;
642 OH_JSVM_GetReferenceValue(env, reference, &result);
643 OH_JSVM_DeleteReference(env, reference);
644 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
645 OH_JSVM_CloseHandleScope(env, handleScope);
646 }
CreateReference_test_4(JSVM_Env env)647 void CreateReference_test_4(JSVM_Env env)
648 {
649 JSVM_HandleScope handleScope;
650 OH_JSVM_OpenHandleScope(env, &handleScope);
651 JSVM_EscapableHandleScope handleScope1;
652 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
653 JSVM_Value value = nullptr;
654 bool x = true;
655 OH_JSVM_GetBoolean(env, x, &value);
656 JSVM_Ref reference = nullptr;
657 OH_JSVM_CreateReference(env, value, 1, &reference);
658 JSVM_Value result = nullptr;
659 OH_JSVM_GetReferenceValue(env, reference, &result);
660 OH_JSVM_DeleteReference(env, reference);
661 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
662 OH_JSVM_CloseHandleScope(env, handleScope);
663 }
CreateReference_test_6(JSVM_Env env)664 void CreateReference_test_6(JSVM_Env env)
665 {
666 JSVM_HandleScope handleScope;
667 OH_JSVM_OpenHandleScope(env, &handleScope);
668 JSVM_EscapableHandleScope handleScope1;
669 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
670 JSVM_Value value = NULL;
671 OH_JSVM_CreateObject(env, &value);
672 const char *testNameStr = "bar";
673 JSVM_Value propValue = nullptr;
674 JSVM_Value key;
675 OH_JSVM_CreateStringUtf8(env, "foo", JSVM_AUTO_LENGTH, &key);
676 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue);
677 JSVM_Ref reference = nullptr;
678 OH_JSVM_CreateReference(env, value, 1, &reference);
679 JSVM_Value result = nullptr;
680 OH_JSVM_GetReferenceValue(env, reference, &result);
681 OH_JSVM_DeleteReference(env, reference);
682 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
683 OH_JSVM_CloseHandleScope(env, handleScope);
684 }
CreateReference_test_7(JSVM_Env env)685 void CreateReference_test_7(JSVM_Env env)
686 {
687 JSVM_HandleScope handleScope;
688 OH_JSVM_OpenHandleScope(env, &handleScope);
689 JSVM_EscapableHandleScope handleScope1;
690 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
691 JSVM_Value value;
692 OH_JSVM_CreateInt32(env, 0, &value);
693 JSVM_Ref reference = nullptr;
694 OH_JSVM_CreateReference(env, value, 1, &reference);
695 JSVM_Value result = nullptr;
696 OH_JSVM_GetReferenceValue(env, reference, &result);
697 OH_JSVM_DeleteReference(env, reference);
698 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
699 OH_JSVM_CloseHandleScope(env, handleScope);
700 }
CreateReference_test_8(JSVM_Env env)701 void CreateReference_test_8(JSVM_Env env)
702 {
703 JSVM_HandleScope handleScope;
704 OH_JSVM_OpenHandleScope(env, &handleScope);
705 JSVM_EscapableHandleScope handleScope1;
706 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
707 size_t arrayLength = 2;
708 JSVM_Value value;
709 OH_JSVM_CreateArrayWithLength(env, arrayLength, &value);
710 for (uint32_t i = 0; i < arrayLength; i++) {
711 JSVM_Value element;
712 OH_JSVM_CreateUint32(env, i + 1, &element);
713 OH_JSVM_SetElement(env, value, i, element);
714 }
715 JSVM_Ref reference = nullptr;
716 OH_JSVM_CreateReference(env, value, 1, &reference);
717 JSVM_Value result = nullptr;
718 OH_JSVM_GetReferenceValue(env, reference, &result);
719 OH_JSVM_DeleteReference(env, reference);
720 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
721 OH_JSVM_CloseHandleScope(env, handleScope);
722 }
CreateReference_test_9(JSVM_Env env)723 void CreateReference_test_9(JSVM_Env env)
724 {
725 JSVM_HandleScope handleScope;
726 OH_JSVM_OpenHandleScope(env, &handleScope);
727 JSVM_EscapableHandleScope handleScope1;
728 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
729 const char testStr[] = "foo";
730 JSVM_Value oldresult = nullptr;
731 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &oldresult);
732 JSVM_Value value;
733 OH_JSVM_CoerceToObject(env, oldresult, &value);
734 JSVM_Ref reference = nullptr;
735 OH_JSVM_CreateReference(env, value, 1, &reference);
736 JSVM_Value result = nullptr;
737 OH_JSVM_GetReferenceValue(env, reference, &result);
738 OH_JSVM_DeleteReference(env, reference);
739 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
740 OH_JSVM_CloseHandleScope(env, handleScope);
741 }
CreateReference_test_10(JSVM_Env env)742 void CreateReference_test_10(JSVM_Env env)
743 {
744 JSVM_HandleScope handleScope;
745 OH_JSVM_OpenHandleScope(env, &handleScope);
746 JSVM_EscapableHandleScope handleScope1;
747 OH_JSVM_OpenEscapableHandleScope(env, &handleScope1);
748 JSVM_Value value;
749 OH_JSVM_CreateDouble(env, 0.0/0.0, &value);
750 JSVM_Ref reference = nullptr;
751 OH_JSVM_CreateReference(env, value, 1, &reference);
752 JSVM_Value result = nullptr;
753 OH_JSVM_GetReferenceValue(env, reference, &result);
754 OH_JSVM_DeleteReference(env, reference);
755 OH_JSVM_CloseEscapableHandleScope(env, handleScope1);
756 OH_JSVM_CloseHandleScope(env, handleScope);
757 }
testLifeCycle2(napi_env env1,napi_callback_info info)758 static napi_value testLifeCycle2(napi_env env1, napi_callback_info info)
759 {
760 JSVM_InitOptions init_options;
761 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
762 printf("memset_s failed");
763 return nullptr;
764 }
765 init_options.externalReferences = externals;
766 if (aa == 0) {
767 OH_JSVM_Init(&init_options);
768 aa++;
769 }
770 JSVM_VM vm;
771 JSVM_CreateVMOptions options;
772 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
773 printf("memset_s failed");
774 return nullptr;
775 }
776 OH_JSVM_CreateVM(&options, &vm);
777 JSVM_VMScope vm_scope;
778 OH_JSVM_OpenVMScope(vm, &vm_scope);
779 JSVM_Env env;
780 JSVM_CallbackStruct param[1];
781 param[0].data = nullptr;
782 param[0].callback = assertEqual;
783 JSVM_PropertyDescriptor descriptor[] = {
784 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
785 };
786 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
787 JSVM_EnvScope envScope;
788 OH_JSVM_OpenEnvScope(env, &envScope);
789
790 CreateReference_test_1(env);
791 CreateReference_test_2(env);
792 CreateReference_test_3(env);
793 CreateReference_test_4(env);
794 CreateReference_test_6(env);
795 CreateReference_test_7(env);
796 CreateReference_test_8(env);
797 CreateReference_test_9(env);
798 CreateReference_test_10(env);
799
800 OH_JSVM_CloseEnvScope(env, envScope);
801 OH_JSVM_DestroyEnv(env);
802 OH_JSVM_CloseVMScope(vm, vm_scope);
803 OH_JSVM_DestroyVM(vm);
804 napi_value result11;
805 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
806 return result11;
807 }
testCreateData1(napi_env env1,napi_callback_info info)808 static napi_value testCreateData1(napi_env env1, napi_callback_info info){
809 JSVM_InitOptions init_options;
810 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
811 printf("memset_s failed");
812 return nullptr;
813 }
814 init_options.externalReferences = externals;
815 if (aa == 0) {
816 OH_JSVM_Init(&init_options);
817 aa++;
818 }
819 JSVM_VM vm;
820 JSVM_CreateVMOptions options;
821 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
822 printf("memset_s failed");
823 return nullptr;
824 }
825 OH_JSVM_CreateVM(&options, &vm);
826 JSVM_VMScope vm_scope;
827 OH_JSVM_OpenVMScope(vm, &vm_scope);
828 JSVM_Env env;
829 JSVM_CallbackStruct param[1];
830 param[0].data = nullptr;
831 param[0].callback = assertEqual;
832 JSVM_PropertyDescriptor descriptor[] = {
833 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
834 };
835 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
836 JSVM_EnvScope envScope;
837 OH_JSVM_OpenEnvScope(env, &envScope);
838 JSVM_HandleScope handlescope;
839 OH_JSVM_OpenHandleScope(env, &handlescope);
840 JSVM_Value ret;
841 OH_JSVM_CreateArray(env, &ret);
842 bool is_array = false;
843 OH_JSVM_IsArray(env,ret,&is_array);
844 uint32_t array_length = 10;
845 JSVM_Value ret1;
846 OH_JSVM_CreateArrayWithLength(env, array_length, &ret1);
847 uint32_t length;
848 OH_JSVM_GetArrayLength(env, ret1, &length);
849 JSVM_Value arrayBuffer = nullptr;
850 void *arrayBufferPtr = nullptr;
851 size_t arrayBufferSize = 1024;
852 JSVM_Status status = OH_JSVM_CreateArraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
853 JSVM_ASSERT(env, status == JSVM_OK, "success to OH_JSVM_CreateArraybuffer");
854 JSVM_ASSERT(env, arrayBuffer != nullptr, "success create_arryBuffer");
855 JSVM_Value createResult = nullptr;
856 double time = 202110181203150;
857 OH_JSVM_CreateDate(env, time, &createResult);
858 double getTime;
859 OH_JSVM_GetDateValue(env, createResult, &getTime);
860 bool is_date = false;
861 OH_JSVM_IsDate(env,createResult,&is_date);
862 const char testStr[] = "test";
863 JSVM_Value external = nullptr;
864 OH_JSVM_CreateExternal(
865 env, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, (void *)testStr, &external);
866 void *tempExternal = nullptr;
867 OH_JSVM_GetValueExternal(env, external, &tempExternal);
868 OH_JSVM_DetachArraybuffer(env, arrayBuffer);
869 OH_JSVM_CloseHandleScope(env, handlescope);
870 OH_JSVM_CloseEnvScope(env, envScope);
871 OH_JSVM_DestroyEnv(env);
872 // shaoshuai
873 OH_JSVM_CloseVMScope(vm, vm_scope);
874 OH_JSVM_DestroyVM(vm);
875 napi_value result11;
876 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
877 return result11;
878 }
testCreateData2(napi_env env1,napi_callback_info info)879 static napi_value testCreateData2(napi_env env1, napi_callback_info info){
880 JSVM_InitOptions init_options;
881 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
882 printf("memset_s failed");
883 return nullptr;
884 }
885 init_options.externalReferences = externals;
886 if (aa == 0) {
887 OH_JSVM_Init(&init_options);
888 aa++;
889 }
890 JSVM_VM vm;
891 JSVM_CreateVMOptions options;
892 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
893 printf("memset_s failed");
894 return nullptr;
895 }
896 OH_JSVM_CreateVM(&options, &vm);
897 JSVM_VMScope vm_scope;
898 OH_JSVM_OpenVMScope(vm, &vm_scope);
899 JSVM_Env env;
900 JSVM_CallbackStruct param[1];
901 param[0].data = nullptr;
902 param[0].callback = assertEqual;
903 JSVM_PropertyDescriptor descriptor[] = {
904 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
905 };
906 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
907 JSVM_EnvScope envScope;
908 OH_JSVM_OpenEnvScope(env, &envScope);
909 JSVM_HandleScope handlescope;
910 OH_JSVM_OpenHandleScope(env, &handlescope);
911 JSVM_Value description;
912 OH_JSVM_CreateStringUtf8(env,"teststr",7,&description);
913 char buffer[128];
914 size_t bufferSize = 128;
915 size_t copied = 0;
916 OH_JSVM_GetValueStringUtf8(env,description, buffer, bufferSize, &copied);
917 JSVM_Value symbol;
918 OH_JSVM_CreateSymbol(env, description, &symbol);
919 JSVM_Value result_symbol;
920 OH_JSVM_SymbolFor(env, NULL, 0, &result_symbol);
921 JSVM_Value arrayBuffer = nullptr;
922 void *arrayBufferPtr = nullptr;
923 size_t arrayBufferSize = 16;
924 size_t typedArrayLength = 4;
925 OH_JSVM_CreateArraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
926 void *tmpArrayBufferPtr = nullptr;
927 size_t arrayBufferLength = 0;
928 OH_JSVM_GetArraybufferInfo(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLength);
929 JSVM_Value _value;
930 OH_JSVM_CreateTypedarray(env, JSVM_TypedarrayType::JSVM_INT32_ARRAY, typedArrayLength, arrayBuffer, 0, &_value);
931 bool isTypedArray = false;
932 OH_JSVM_IsTypedarray(env, _value, &isTypedArray);
933 JSVM_TypedarrayType type;
934 size_t length = 0;
935 void *data = nullptr;
936 JSVM_Value retArrayBuffer;
937 size_t byteOffset = -1;
938 OH_JSVM_GetTypedarrayInfo(env, _value, &type, &length, &data, &retArrayBuffer, &byteOffset);
939 OH_JSVM_DetachArraybuffer(env,retArrayBuffer);
940 bool isDetachedArrayBuffer = false;
941 OH_JSVM_IsDetachedArraybuffer(env, retArrayBuffer,&isDetachedArrayBuffer);
942 OH_JSVM_CloseHandleScope(env, handlescope);
943 OH_JSVM_CloseEnvScope(env, envScope);
944 OH_JSVM_DestroyEnv(env);
945 OH_JSVM_CloseVMScope(vm, vm_scope);
946 OH_JSVM_DestroyVM(vm);
947 napi_value result11;
948 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
949 return result11;
950 }
testCreateData3(napi_env env1,napi_callback_info info)951 static napi_value testCreateData3(napi_env env1, napi_callback_info info){
952 JSVM_InitOptions init_options;
953 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
954 printf("memset_s failed");
955 return nullptr;
956 }
957 init_options.externalReferences = externals;
958 if (aa == 0) {
959 OH_JSVM_Init(&init_options);
960 aa++;
961 }
962 JSVM_VM vm;
963 JSVM_CreateVMOptions options;
964 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
965 printf("memset_s failed");
966 return nullptr;
967 }
968 OH_JSVM_CreateVM(&options, &vm);
969 JSVM_VMScope vm_scope;
970 OH_JSVM_OpenVMScope(vm, &vm_scope);
971 JSVM_Env env;
972 JSVM_CallbackStruct param[1];
973 param[0].data = nullptr;
974 param[0].callback = assertEqual;
975 JSVM_PropertyDescriptor descriptor[] = {
976 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
977 };
978 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
979 JSVM_EnvScope envScope;
980 OH_JSVM_OpenEnvScope(env, &envScope);
981 JSVM_HandleScope handlescope;
982 OH_JSVM_OpenHandleScope(env, &handlescope);
983 JSVM_Value arrayBuffer = nullptr;
984 void *arrayBufferPtr = nullptr;
985 size_t arrayBufferSize = 16;
986 JSVM_Status status = OH_JSVM_CreateArraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
987 JSVM_ASSERT(env, status == JSVM_OK, "success to OH_JSVM_CreateArraybuffer");
988 JSVM_ASSERT(env, arrayBuffer != nullptr, "success create_arryBuffer");
989 bool isArrayBuffer = false;
990 OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer);
991 JSVM_Value result = nullptr;
992 OH_JSVM_CreateDataview(env, arrayBufferSize, arrayBuffer, 0, &result);
993 bool isDataView = false;
994 OH_JSVM_IsDataview(env, result, &isDataView);
995 JSVM_Value retArrayBuffer = nullptr;
996 void *data = nullptr;
997 size_t byteLength = 0;
998 size_t byteOffset = -1;
999 OH_JSVM_GetDataviewInfo(env, result, &byteLength, &data, &retArrayBuffer, &byteOffset);
1000 OH_JSVM_DetachArraybuffer(env, arrayBuffer);
1001 OH_JSVM_CloseHandleScope(env, handlescope);
1002 OH_JSVM_CloseEnvScope(env, envScope);
1003 OH_JSVM_DestroyEnv(env);
1004 OH_JSVM_CloseVMScope(vm, vm_scope);
1005 OH_JSVM_DestroyVM(vm);
1006 napi_value result11;
1007 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1008 return result11;
1009 }
testCreateData4(napi_env env1,napi_callback_info info)1010 static napi_value testCreateData4(napi_env env1, napi_callback_info info){
1011 JSVM_InitOptions init_options;
1012 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1013 printf("memset_s failed");
1014 return nullptr;
1015 }
1016 init_options.externalReferences = externals;
1017 if (aa == 0) {
1018 OH_JSVM_Init(&init_options);
1019 aa++;
1020 }
1021 JSVM_VM vm;
1022 JSVM_CreateVMOptions options;
1023 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1024 printf("memset_s failed");
1025 return nullptr;
1026 }
1027 OH_JSVM_CreateVM(&options, &vm);
1028 JSVM_VMScope vm_scope;
1029 OH_JSVM_OpenVMScope(vm, &vm_scope);
1030 JSVM_Env env;
1031 JSVM_CallbackStruct param[1];
1032 param[0].data = nullptr;
1033 param[0].callback = assertEqual;
1034 JSVM_PropertyDescriptor descriptor[] = {
1035 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1036 };
1037 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1038 JSVM_EnvScope envScope;
1039 OH_JSVM_OpenEnvScope(env, &envScope);
1040 JSVM_HandleScope handlescope;
1041 OH_JSVM_OpenHandleScope(env, &handlescope);
1042 JSVM_Value output1;
1043 OH_JSVM_CreateInt32(env, 0, &output1);
1044 int32_t value1;
1045 OH_JSVM_GetValueInt32(env, output1, &value1);
1046 JSVM_Value output2;
1047 OH_JSVM_CreateUint32(env,0, &output2);
1048 uint32_t value2;
1049 OH_JSVM_GetValueUint32(env,output2, &value2);
1050 JSVM_Value output3;
1051 OH_JSVM_CreateInt64(env, (double)0, &output3);
1052 int64_t value3;
1053 OH_JSVM_GetValueInt64(env,output3, &value3);
1054 int64_t resultValue = 0;
1055 bool flag = false;
1056 OH_JSVM_GetValueBigintInt64(env, output3, &resultValue, &flag);
1057 uint64_t testValue = UINT64_MAX;
1058 JSVM_Value result = nullptr;
1059 OH_JSVM_CreateBigintUint64(env, testValue, &result);
1060
1061 uint64_t resultValue1 = 0;
1062 bool flag1 = false;
1063 OH_JSVM_GetValueBigintUint64(env, result, &resultValue1, &flag1);
1064 double value = 1.0;
1065 JSVM_Value output4;
1066 OH_JSVM_CreateDouble(env, value, &output4);
1067 double resultDouble;
1068 OH_JSVM_GetValueDouble(env, output4, &resultDouble);
1069 OH_JSVM_CloseHandleScope(env, handlescope);
1070 OH_JSVM_CloseEnvScope(env, envScope);
1071 OH_JSVM_DestroyEnv(env);
1072 OH_JSVM_CloseVMScope(vm, vm_scope);
1073 OH_JSVM_DestroyVM(vm);
1074 napi_value result11;
1075 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1076 return result11;
1077 }
testCreateData5(napi_env env1,napi_callback_info info)1078 static napi_value testCreateData5(napi_env env1, napi_callback_info info) {
1079 JSVM_InitOptions init_options;
1080 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1081 printf("memset_s failed");
1082 return nullptr;
1083 }
1084 init_options.externalReferences = externals;
1085 if (aa == 0) {
1086 OH_JSVM_Init(&init_options);
1087 aa++;
1088 }
1089 JSVM_VM vm;
1090 JSVM_CreateVMOptions options;
1091 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1092 printf("memset_s failed");
1093 return nullptr;
1094 }
1095 OH_JSVM_CreateVM(&options, &vm);
1096 JSVM_VMScope vm_scope;
1097 OH_JSVM_OpenVMScope(vm, &vm_scope);
1098 JSVM_Env env;
1099 JSVM_CallbackStruct param[1];
1100 param[0].data = nullptr;
1101 param[0].callback = assertEqual;
1102 JSVM_PropertyDescriptor descriptor[] = {
1103 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1104 };
1105 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1106 JSVM_EnvScope envScope;
1107 OH_JSVM_OpenEnvScope(env, &envScope);
1108 JSVM_HandleScope handlescope;
1109 OH_JSVM_OpenHandleScope(env, &handlescope);
1110 int64_t testValue = INT64_MAX;
1111 JSVM_Value result = nullptr;
1112 OH_JSVM_CreateBigintInt64(env, testValue, &result);
1113 uint64_t testValue1 = UINT64_MAX;
1114 JSVM_Value result1 = nullptr;
1115 OH_JSVM_CreateBigintUint64(env, testValue1, &result1);
1116 int signBit = 0;
1117 size_t wordCount = 4;
1118 uint64_t words[] = {0ULL, 34ULL, 56ULL, 2ULL};
1119 uint64_t wordsOut[] = {0ULL, 0ULL, 0ULL, 0ULL};
1120 JSVM_Value result2 = nullptr;
1121 OH_JSVM_CreateBigintWords(env, signBit, wordCount, words, &result2);
1122 int retSignBit = -1;
1123 size_t retWordCount = 4;
1124 OH_JSVM_GetValueBigintWords(env, result2, &retSignBit, &retWordCount, wordsOut);
1125 JSVM_Value description;
1126 OH_JSVM_CreateStringLatin1(env, "teststr", 7, &description);
1127 char buffer[128];
1128 size_t bufferSize = 128;
1129 size_t copied = 0;
1130 OH_JSVM_GetValueStringLatin1(env,description, buffer, bufferSize, &copied);
1131 const char16_t *str = u"he";
1132 JSVM_Value result4;
1133 OH_JSVM_CreateStringUtf16(env, str, 2, &result4);
1134 char16_t buffer1[128]; // 128: char16_t type of element size
1135 size_t bufferSize1 = 128; // 128: char16_t type of element size
1136 size_t copied1 = 0;
1137 OH_JSVM_GetValueStringUtf16(env,result4, buffer1, bufferSize1, &copied1);
1138 OH_JSVM_CloseHandleScope(env, handlescope);
1139 OH_JSVM_CloseEnvScope(env, envScope);
1140 OH_JSVM_DestroyEnv(env);
1141 OH_JSVM_CloseVMScope(vm, vm_scope);
1142 OH_JSVM_DestroyVM(vm);
1143 napi_value result11;
1144 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1145 return result11;
1146 }
testGetData1(napi_env env1,napi_callback_info info)1147 static napi_value testGetData1(napi_env env1, napi_callback_info info){
1148 JSVM_InitOptions init_options;
1149 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1150 printf("memset_s failed");
1151 return nullptr;
1152 }
1153 init_options.externalReferences = externals;
1154 if (aa == 0) {
1155 OH_JSVM_Init(&init_options);
1156 aa++;
1157 }
1158 JSVM_VM vm;
1159 JSVM_CreateVMOptions options;
1160 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1161 printf("memset_s failed");
1162 return nullptr;
1163 }
1164 OH_JSVM_CreateVM(&options, &vm);
1165 JSVM_VMScope vm_scope;
1166 OH_JSVM_OpenVMScope(vm, &vm_scope);
1167 JSVM_Env env;
1168 JSVM_CallbackStruct param[1];
1169 param[0].data = nullptr;
1170 param[0].callback = assertEqual;
1171 JSVM_PropertyDescriptor descriptor[] = {
1172 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1173 };
1174 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1175 JSVM_EnvScope envScope;
1176 OH_JSVM_OpenEnvScope(env, &envScope);
1177 JSVM_HandleScope handlescope;
1178 OH_JSVM_OpenHandleScope(env, &handlescope);
1179 JSVM_Value testWrapClass = nullptr;
1180 JSVM_CallbackStruct param1;
1181 param1.data = nullptr;
1182 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
1183 JSVM_Value thisVar = nullptr;
1184 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
1185 return thisVar;
1186 };
1187 OH_JSVM_DefineClass(env, "TestWrapClass", NAPI_AUTO_LENGTH, ¶m1, 0, nullptr, &testWrapClass);
1188 JSVM_Value customClassPrototype = nullptr;
1189 OH_JSVM_GetPrototype(env, testWrapClass, &customClassPrototype);
1190 bool value1 = true;
1191 JSVM_Value boolValue;
1192 OH_JSVM_GetBoolean(env,value1,&boolValue);
1193 bool value2;
1194 OH_JSVM_GetValueBool(env,boolValue, &value2);
1195 OH_JSVM_CloseHandleScope(env, handlescope);
1196 OH_JSVM_CloseEnvScope(env, envScope);
1197 OH_JSVM_DestroyEnv(env);
1198 OH_JSVM_CloseVMScope(vm, vm_scope);
1199 OH_JSVM_DestroyVM(vm);
1200 napi_value result11;
1201 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1202 return result11;
1203 }
testGetData2(napi_env env1,napi_callback_info info)1204 static napi_value testGetData2(napi_env env1, napi_callback_info info) {
1205 JSVM_InitOptions init_options;
1206 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1207 printf("memset_s failed");
1208 return nullptr;
1209 }
1210 init_options.externalReferences = externals;
1211 if (aa == 0) {
1212 OH_JSVM_Init(&init_options);
1213 aa++;
1214 }
1215 JSVM_VM vm;
1216 JSVM_CreateVMOptions options;
1217 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1218 printf("memset_s failed");
1219 return nullptr;
1220 }
1221 OH_JSVM_CreateVM(&options, &vm);
1222 JSVM_VMScope vm_scope;
1223 OH_JSVM_OpenVMScope(vm, &vm_scope);
1224 JSVM_Env env;
1225 JSVM_CallbackStruct param[1];
1226 param[0].data = nullptr;
1227 param[0].callback = assertEqual;
1228 JSVM_PropertyDescriptor descriptor[] = {
1229 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1230 };
1231 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1232 JSVM_EnvScope envScope;
1233 OH_JSVM_OpenEnvScope(env, &envScope);
1234 JSVM_HandleScope handlescope;
1235 OH_JSVM_OpenHandleScope(env, &handlescope);
1236 JSVM_Value result = nullptr;
1237 OH_JSVM_GetGlobal(env, &result);
1238 JSVM_Value result1 = nullptr;
1239 OH_JSVM_GetNull(env, &result1);
1240 JSVM_ValueType type;
1241 OH_JSVM_Typeof(env, result1, &type);
1242 JSVM_Value result2 = nullptr;
1243 OH_JSVM_GetUndefined(env, &result2);
1244 OH_JSVM_CloseHandleScope(env, handlescope);
1245 OH_JSVM_CloseEnvScope(env, envScope);
1246 OH_JSVM_DestroyEnv(env);
1247 OH_JSVM_CloseVMScope(vm, vm_scope);
1248 OH_JSVM_DestroyVM(vm);
1249 napi_value result11;
1250 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1251 return result11;
1252 }
testValueOperation(napi_env env1,napi_callback_info info)1253 static napi_value testValueOperation(napi_env env1, napi_callback_info info){
1254 JSVM_InitOptions init_options;
1255 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1256 printf("memset_s failed");
1257 return nullptr;
1258 }
1259 init_options.externalReferences = externals;
1260 if (aa == 0) {
1261 OH_JSVM_Init(&init_options);
1262 aa++;
1263 }
1264 JSVM_VM vm;
1265 JSVM_CreateVMOptions options;
1266 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1267 printf("memset_s failed");
1268 return nullptr;
1269 }
1270 OH_JSVM_CreateVM(&options, &vm);
1271 JSVM_VMScope vm_scope;
1272 OH_JSVM_OpenVMScope(vm, &vm_scope);
1273 JSVM_Env env;
1274 JSVM_CallbackStruct param[1];
1275 param[0].data = nullptr;
1276 param[0].callback = assertEqual;
1277 JSVM_PropertyDescriptor descriptor[] = {
1278 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1279 };
1280 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1281 JSVM_EnvScope envScope;
1282 OH_JSVM_OpenEnvScope(env, &envScope);
1283 JSVM_HandleScope handlescope;
1284 OH_JSVM_OpenHandleScope(env, &handlescope);
1285 JSVM_Value value1;
1286 OH_JSVM_CreateInt32(env,0,&value1);
1287 JSVM_Value value2;
1288 OH_JSVM_CoerceToBool(env,value1,&value2);
1289 JSVM_Value value3;
1290 OH_JSVM_CreateStringUtf8(env, "12", 2, &value3);
1291 JSVM_Value value4;
1292 OH_JSVM_CoerceToNumber(env,value3,&value4);
1293 JSVM_Value ret;
1294 OH_JSVM_CreateArray(env, &ret);
1295 JSVM_Value value5;
1296 OH_JSVM_CoerceToObject(env, ret,&value5);
1297 JSVM_Value value6;
1298 OH_JSVM_CoerceToString(env,value1,&value6);
1299 OH_JSVM_CloseHandleScope(env, handlescope);
1300 OH_JSVM_CloseEnvScope(env, envScope);
1301 OH_JSVM_DestroyEnv(env);
1302 OH_JSVM_CloseVMScope(vm, vm_scope);
1303 OH_JSVM_DestroyVM(vm);
1304 napi_value result11;
1305 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1306 return result11;
1307 }
testValueOperation2(napi_env env1,napi_callback_info info)1308 static napi_value testValueOperation2(napi_env env1, napi_callback_info info) {
1309 JSVM_InitOptions init_options;
1310 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1311 printf("memset_s failed");
1312 return nullptr;
1313 }
1314 init_options.externalReferences = externals;
1315 if (aa == 0) {
1316 OH_JSVM_Init(&init_options);
1317 aa++;
1318 }
1319 JSVM_VM vm;
1320 JSVM_CreateVMOptions options;
1321 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1322 printf("memset_s failed");
1323 return nullptr;
1324 }
1325 OH_JSVM_CreateVM(&options, &vm);
1326 JSVM_VMScope vm_scope;
1327 OH_JSVM_OpenVMScope(vm, &vm_scope);
1328 JSVM_Env env;
1329 JSVM_CallbackStruct param[1];
1330 param[0].data = nullptr;
1331 param[0].callback = assertEqual;
1332 JSVM_PropertyDescriptor descriptor[] = {
1333 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1334 };
1335 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1336 JSVM_EnvScope envScope;
1337 OH_JSVM_OpenEnvScope(env, &envScope);
1338 JSVM_HandleScope handlescope;
1339 OH_JSVM_OpenHandleScope(env, &handlescope);
1340 JSVM_Value customClass = nullptr;
1341 JSVM_CallbackStruct param1;
1342 param1.data = nullptr;
1343 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
1344 JSVM_Value thisVar = nullptr;
1345 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
1346 return thisVar;
1347 };
1348 OH_JSVM_DefineClass(env, "TestWrapClass", NAPI_AUTO_LENGTH, ¶m1, 0, nullptr, &customClass);
1349 JSVM_Value customClassPrototype = nullptr;
1350 OH_JSVM_GetPrototype(env, customClass, &customClassPrototype);
1351 JSVM_Value customInstance = nullptr;
1352 OH_JSVM_NewInstance(env, customClass, 0, nullptr, &customInstance);
1353 bool isInstanceOf = false;
1354 OH_JSVM_Instanceof(env, customInstance, customClass, &isInstanceOf);
1355 const char *testStringStr = "test";
1356 JSVM_Value testString = nullptr;
1357 OH_JSVM_CreateStringUtf8(env, testStringStr, strlen(testStringStr), &testString);
1358 bool isStrictEquals = false;
1359 OH_JSVM_StrictEquals(env, testString, testString, &isStrictEquals);
1360 JSVM_Value testObject = nullptr;
1361 OH_JSVM_CreateObject(env, &testObject);
1362 isStrictEquals = false;
1363 OH_JSVM_StrictEquals(env, testObject, testObject, &isStrictEquals);
1364 OH_JSVM_CloseHandleScope(env, handlescope);
1365 OH_JSVM_CloseEnvScope(env, envScope);
1366 OH_JSVM_DestroyEnv(env);
1367 OH_JSVM_CloseVMScope(vm, vm_scope);
1368 OH_JSVM_DestroyVM(vm);
1369 napi_value result11;
1370 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1371 return result11;
1372 }
equals_test_1(JSVM_Env env)1373 void equals_test_1(JSVM_Env env)
1374 {
1375 JSVM_HandleScope handlescope;
1376 OH_JSVM_OpenHandleScope(env, &handlescope);
1377 JSVM_Value lhs = nullptr;
1378 OH_JSVM_GetUndefined(env, &lhs);
1379 JSVM_Value rhs = nullptr;
1380 OH_JSVM_GetUndefined(env, &rhs);
1381 bool isEquals = false;
1382 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1383 OH_JSVM_CloseHandleScope(env, handlescope);
1384 }
equals_test_2(JSVM_Env env)1385 void equals_test_2(JSVM_Env env)
1386 {
1387 JSVM_HandleScope handlescope;
1388 OH_JSVM_OpenHandleScope(env, &handlescope);
1389 JSVM_Value lhs = nullptr;
1390 OH_JSVM_GetNull(env, &lhs);
1391 JSVM_Value rhs = nullptr;
1392 OH_JSVM_GetNull(env, &rhs);
1393 bool isEquals = false;
1394 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1395 OH_JSVM_CloseHandleScope(env, handlescope);
1396 }
equals_test_3(JSVM_Env env)1397 void equals_test_3(JSVM_Env env)
1398 {
1399 JSVM_HandleScope handlescope;
1400 OH_JSVM_OpenHandleScope(env, &handlescope);
1401 JSVM_Value lhs = nullptr;
1402 bool x = true;
1403 OH_JSVM_GetBoolean(env, x, &lhs);
1404 JSVM_Value rhs = nullptr;
1405 bool y = true;
1406 OH_JSVM_GetBoolean(env, y, &rhs);
1407 bool isEquals = false;
1408 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1409 OH_JSVM_CloseHandleScope(env, handlescope);
1410 }
equals_test_4(JSVM_Env env)1411 void equals_test_4(JSVM_Env env)
1412 {
1413 JSVM_HandleScope handlescope;
1414 OH_JSVM_OpenHandleScope(env, &handlescope);
1415 JSVM_Value lhs = nullptr;
1416 bool x = false;
1417 OH_JSVM_GetBoolean(env, x, &lhs);
1418 JSVM_Value rhs = nullptr;
1419 bool y = false;
1420 OH_JSVM_GetBoolean(env, y, &rhs);
1421 bool isEquals = false;
1422 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1423 OH_JSVM_CloseHandleScope(env, handlescope);
1424 }
equals_test_5(JSVM_Env env)1425 void equals_test_5(JSVM_Env env)
1426 {
1427 JSVM_HandleScope handlescope;
1428 OH_JSVM_OpenHandleScope(env, &handlescope);
1429 const char testStr1[] = "foo";
1430 JSVM_Value lhs = nullptr;
1431 OH_JSVM_CreateStringUtf8(env, testStr1, strlen(testStr1), &lhs);
1432 const char testStr2[] = "foo";
1433 JSVM_Value rhs = nullptr;
1434 OH_JSVM_CreateStringUtf8(env, testStr2, strlen(testStr2), &rhs);
1435 bool isEquals = false;
1436 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1437 OH_JSVM_CloseHandleScope(env, handlescope);
1438 }
equals_test_6(JSVM_Env env)1439 void equals_test_6(JSVM_Env env)
1440 {
1441 JSVM_HandleScope handlescope;
1442 OH_JSVM_OpenHandleScope(env, &handlescope);
1443 JSVM_Value result = NULL;
1444 OH_JSVM_CreateObject(env, &result);
1445 const char *testNameStr = "bar";
1446 JSVM_Value propValue = nullptr;
1447 JSVM_Value key;
1448 OH_JSVM_CreateStringUtf8(env, "foo", JSVM_AUTO_LENGTH, &key);
1449 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue);
1450 OH_JSVM_SetProperty(env, result, key, propValue);
1451 bool isEquals = false;
1452 OH_JSVM_Equals(env, result, result, &isEquals);
1453 OH_JSVM_CloseHandleScope(env, handlescope);
1454 }
equals_test_7(JSVM_Env env)1455 void equals_test_7(JSVM_Env env)
1456 {
1457 JSVM_HandleScope handlescope;
1458 OH_JSVM_OpenHandleScope(env, &handlescope);
1459 JSVM_Value lhs;
1460 OH_JSVM_CreateInt32(env, 0, &lhs);
1461 JSVM_Value rhs;
1462 OH_JSVM_CreateInt32(env, 0, &rhs);
1463 bool isEquals = false;
1464 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1465 OH_JSVM_CloseHandleScope(env, handlescope);
1466 }
equals_test_8(JSVM_Env env)1467 void equals_test_8(JSVM_Env env)
1468 {
1469 JSVM_HandleScope handlescope;
1470 OH_JSVM_OpenHandleScope(env, &handlescope);
1471 JSVM_Value lhs;
1472 OH_JSVM_CreateInt32(env, +0, &lhs);
1473 JSVM_Value rhs;
1474 OH_JSVM_CreateInt32(env, -0, &rhs);
1475 bool isEquals = false;
1476 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1477 OH_JSVM_CloseHandleScope(env, handlescope);
1478 }
equals_test_9(JSVM_Env env)1479 void equals_test_9(JSVM_Env env)
1480 {
1481 JSVM_HandleScope handlescope;
1482 OH_JSVM_OpenHandleScope(env, &handlescope);
1483 JSVM_Value lhs;
1484 OH_JSVM_CreateInt32(env, 0, &lhs);
1485 JSVM_Value rhs = nullptr;
1486 bool y = false;
1487 OH_JSVM_GetBoolean(env, y, &rhs);
1488 bool isEquals = false;
1489 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1490 OH_JSVM_CloseHandleScope(env, handlescope);
1491 }
equals_test_10(JSVM_Env env)1492 void equals_test_10(JSVM_Env env)
1493 {
1494 JSVM_HandleScope handlescope;
1495 OH_JSVM_OpenHandleScope(env, &handlescope);
1496 const char testStr[] = "";
1497 JSVM_Value lhs = nullptr;
1498 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &lhs);
1499 JSVM_Value rhs = nullptr;
1500 bool y = false;
1501 OH_JSVM_GetBoolean(env, y, &rhs);
1502 bool isEquals = false;
1503 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1504 OH_JSVM_CloseHandleScope(env, handlescope);
1505 }
equals_test_11(JSVM_Env env)1506 void equals_test_11(JSVM_Env env)
1507 {
1508 JSVM_HandleScope handlescope;
1509 OH_JSVM_OpenHandleScope(env, &handlescope);
1510 const char testStr[] = "";
1511 JSVM_Value lhs = nullptr;
1512 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &lhs);
1513 JSVM_Value rhs;
1514 OH_JSVM_CreateInt32(env, 0, &rhs);
1515 bool isEquals = false;
1516 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1517 OH_JSVM_CloseHandleScope(env, handlescope);
1518 }
equals_test_12(JSVM_Env env)1519 void equals_test_12(JSVM_Env env)
1520 {
1521 JSVM_HandleScope handlescope;
1522 OH_JSVM_OpenHandleScope(env, &handlescope);
1523 const char testStr[] = "0";
1524 JSVM_Value lhs = nullptr;
1525 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &lhs);
1526 JSVM_Value rhs;
1527 OH_JSVM_CreateInt32(env, 0, &rhs);
1528 bool isEquals = false;
1529 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1530 OH_JSVM_CloseHandleScope(env, handlescope);
1531 }
equals_test_13(JSVM_Env env)1532 void equals_test_13(JSVM_Env env)
1533 {
1534 JSVM_HandleScope handlescope;
1535 OH_JSVM_OpenHandleScope(env, &handlescope);
1536 const char testStr[] = "1";
1537 JSVM_Value lhs = nullptr;
1538 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &lhs);
1539 JSVM_Value rhs;
1540 OH_JSVM_CreateInt32(env, 1, &rhs);
1541 bool isEquals = false;
1542 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1543 OH_JSVM_CloseHandleScope(env, handlescope);
1544 }
equals_test_14(JSVM_Env env)1545 void equals_test_14(JSVM_Env env)
1546 {
1547 JSVM_HandleScope handlescope;
1548 OH_JSVM_OpenHandleScope(env, &handlescope);
1549 size_t arrayLength = 2;
1550 JSVM_Value lhs;
1551 OH_JSVM_CreateArrayWithLength(env, arrayLength, &lhs);
1552 for (uint32_t i = 0; i < arrayLength; i++) {
1553 JSVM_Value element;
1554 OH_JSVM_CreateUint32(env, i + 1, &element);
1555 OH_JSVM_SetElement(env, lhs, i, element);
1556 }
1557 const char testStr[] = "1,2";
1558 JSVM_Value rhs = nullptr;
1559 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &rhs);
1560 bool isEquals = false;
1561 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1562 OH_JSVM_CloseHandleScope(env, handlescope);
1563 }
equals_test_15(JSVM_Env env)1564 void equals_test_15(JSVM_Env env)
1565 {
1566 JSVM_HandleScope handlescope;
1567 OH_JSVM_OpenHandleScope(env, &handlescope);
1568 const char testStr1[] = "foo";
1569 JSVM_Value oldresult = nullptr;
1570 OH_JSVM_CreateStringUtf8(env, testStr1, strlen(testStr1), &oldresult);
1571 JSVM_Value lhs;
1572 OH_JSVM_CoerceToObject(env, oldresult, &lhs);
1573 const char testStr2[] = "foo";
1574 JSVM_Value rhs = nullptr;
1575 OH_JSVM_CreateStringUtf8(env, testStr2, strlen(testStr2), &rhs);
1576 bool isEquals = false;
1577 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1578 OH_JSVM_CloseHandleScope(env, handlescope);
1579 }
equals_test_16(JSVM_Env env)1580 void equals_test_16(JSVM_Env env)
1581 {
1582 JSVM_HandleScope handlescope;
1583 OH_JSVM_OpenHandleScope(env, &handlescope);
1584 JSVM_Value lhs = nullptr;
1585 OH_JSVM_GetNull(env, &lhs);
1586 JSVM_Value rhs = nullptr;
1587 OH_JSVM_GetUndefined(env, &rhs);
1588 bool isEquals = false;
1589 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1590 OH_JSVM_CloseHandleScope(env, handlescope);
1591 }
equals_test_17(JSVM_Env env)1592 void equals_test_17(JSVM_Env env)
1593 {
1594 JSVM_HandleScope handlescope;
1595 OH_JSVM_OpenHandleScope(env, &handlescope);
1596 JSVM_Value lhs = nullptr;
1597 OH_JSVM_GetNull(env, &lhs);
1598 JSVM_Value rhs = nullptr;
1599 bool y = false;
1600 OH_JSVM_GetBoolean(env, y, &rhs);
1601 bool isEquals = false;
1602 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1603 OH_JSVM_CloseHandleScope(env, handlescope);
1604 }
equals_test_18(JSVM_Env env)1605 void equals_test_18(JSVM_Env env)
1606 {
1607 JSVM_HandleScope handlescope;
1608 OH_JSVM_OpenHandleScope(env, &handlescope);
1609 JSVM_Value lhs = nullptr;
1610 OH_JSVM_GetUndefined(env, &lhs);
1611 JSVM_Value rhs = nullptr;
1612 bool y = false;
1613 OH_JSVM_GetBoolean(env, y, &rhs);
1614 bool isEquals = false;
1615 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1616 OH_JSVM_CloseHandleScope(env, handlescope);
1617 }
equals_test_19(JSVM_Env env)1618 void equals_test_19(JSVM_Env env)
1619 {
1620 JSVM_HandleScope handlescope;
1621 OH_JSVM_OpenHandleScope(env, &handlescope);
1622 JSVM_Value lhs = NULL;
1623 OH_JSVM_CreateObject(env, &lhs);
1624 const char *testNameStr = "bar";
1625 JSVM_Value propValue1 = nullptr;
1626 JSVM_Value key1;
1627 OH_JSVM_CreateStringUtf8(env, "foo", JSVM_AUTO_LENGTH, &key1);
1628 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue1);
1629 OH_JSVM_SetProperty(env, lhs, key1, propValue1);
1630 JSVM_Value rhs = NULL;
1631 OH_JSVM_CreateObject(env, &rhs);
1632 JSVM_Value propValue2 = nullptr;
1633 JSVM_Value key2;
1634 OH_JSVM_CreateStringUtf8(env, "foo", JSVM_AUTO_LENGTH, &key2);
1635 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue2);
1636 OH_JSVM_SetProperty(env, rhs, key2, propValue2);
1637 bool isEquals = false;
1638 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1639 OH_JSVM_CloseHandleScope(env, handlescope);
1640 }
equals_test_20(JSVM_Env env)1641 void equals_test_20(JSVM_Env env)
1642 {
1643 JSVM_HandleScope handlescope;
1644 OH_JSVM_OpenHandleScope(env, &handlescope);
1645 const char testStr1[] = "foo";
1646 JSVM_Value oldresult1 = nullptr;
1647 OH_JSVM_CreateStringUtf8(env, testStr1, strlen(testStr1), &oldresult1);
1648 JSVM_Value lhs;
1649 OH_JSVM_CoerceToObject(env, oldresult1, &lhs);
1650 const char testStr2[] = "foo";
1651 JSVM_Value oldresult2 = nullptr;
1652 OH_JSVM_CreateStringUtf8(env, testStr2, strlen(testStr2), &oldresult2);
1653 JSVM_Value rhs;
1654 OH_JSVM_CoerceToObject(env, oldresult2, &rhs);
1655 bool isEquals = false;
1656 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1657 OH_JSVM_CloseHandleScope(env, handlescope);
1658 }
equals_test_21(JSVM_Env env)1659 void equals_test_21(JSVM_Env env)
1660 {
1661 JSVM_HandleScope handlescope;
1662 OH_JSVM_OpenHandleScope(env, &handlescope);
1663 JSVM_Value lhs;
1664 OH_JSVM_CreateInt32(env, 0, &lhs);
1665 JSVM_Value rhs = nullptr;
1666 OH_JSVM_GetNull(env, &rhs);
1667 bool isEquals = false;
1668 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1669 OH_JSVM_CloseHandleScope(env, handlescope);
1670 }
equals_test_22(JSVM_Env env)1671 void equals_test_22(JSVM_Env env)
1672 {
1673 JSVM_HandleScope handlescope;
1674 OH_JSVM_OpenHandleScope(env, &handlescope);
1675 JSVM_Value lhs;
1676 OH_JSVM_CreateInt32(env, 0, &lhs);
1677 JSVM_Value rhs;
1678 OH_JSVM_CreateDouble(env, 0.0/0.0, &rhs);
1679 bool isEquals = false;
1680 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1681 OH_JSVM_CloseHandleScope(env, handlescope);
1682 }
equals_test_23(JSVM_Env env)1683 void equals_test_23(JSVM_Env env)
1684 {
1685 JSVM_HandleScope handlescope;
1686 OH_JSVM_OpenHandleScope(env, &handlescope);
1687 const char testStr1[] = "foo";
1688 JSVM_Value lhs = nullptr;
1689 OH_JSVM_CreateStringUtf8(env, testStr1, strlen(testStr1), &lhs);
1690 JSVM_Value rhs;
1691 OH_JSVM_CreateDouble(env, 0.0/0.0, &rhs);
1692 bool isEquals = false;
1693 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1694 OH_JSVM_CloseHandleScope(env, handlescope);
1695 }
equals_test_24(JSVM_Env env)1696 void equals_test_24(JSVM_Env env)
1697 {
1698 JSVM_HandleScope handlescope;
1699 OH_JSVM_OpenHandleScope(env, &handlescope);
1700 JSVM_Value lhs;
1701 OH_JSVM_CreateDouble(env, 0.0/0.0, &lhs);
1702 JSVM_Value rhs;
1703 OH_JSVM_CreateDouble(env, 0.0/0.0, &rhs);
1704 bool isEquals = false;
1705 OH_JSVM_Equals(env, lhs, rhs, &isEquals);
1706 OH_JSVM_CloseHandleScope(env, handlescope);
1707 }
testValueOperation3(napi_env env1,napi_callback_info info)1708 static napi_value testValueOperation3(napi_env env1, napi_callback_info info)
1709 {
1710 JSVM_InitOptions init_options;
1711 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1712 printf("memset_s failed");
1713 return nullptr;
1714 }
1715 init_options.externalReferences = externals;
1716 if (aa == 0) {
1717 OH_JSVM_Init(&init_options);
1718 aa++;
1719 }
1720 JSVM_VM vm;
1721 JSVM_CreateVMOptions options;
1722 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1723 printf("memset_s failed");
1724 return nullptr;
1725 }
1726 OH_JSVM_CreateVM(&options, &vm);
1727 JSVM_VMScope vm_scope;
1728 OH_JSVM_OpenVMScope(vm, &vm_scope);
1729 JSVM_Env env;
1730 JSVM_CallbackStruct param[1];
1731 param[0].data = nullptr;
1732 param[0].callback = assertEqual;
1733 JSVM_PropertyDescriptor descriptor[] = {
1734 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1735 };
1736 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1737 JSVM_EnvScope envScope;
1738 OH_JSVM_OpenEnvScope(env, &envScope);
1739
1740 equals_test_1(env);
1741 equals_test_2(env);
1742 equals_test_3(env);
1743 equals_test_4(env);
1744 equals_test_5(env);
1745 equals_test_6(env);
1746 equals_test_7(env);
1747 equals_test_8(env);
1748 equals_test_9(env);
1749 equals_test_10(env);
1750 equals_test_11(env);
1751 equals_test_12(env);
1752
1753 OH_JSVM_CloseEnvScope(env, envScope);
1754 OH_JSVM_DestroyEnv(env);
1755 OH_JSVM_CloseVMScope(vm, vm_scope);
1756 OH_JSVM_DestroyVM(vm);
1757 napi_value result11;
1758 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1759 return result11;
1760 }
testValueOperation4(napi_env env1,napi_callback_info info)1761 static napi_value testValueOperation4(napi_env env1, napi_callback_info info)
1762 {
1763 JSVM_InitOptions init_options;
1764 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1765 printf("memset_s failed");
1766 return nullptr;
1767 }
1768 init_options.externalReferences = externals;
1769 if (aa == 0) {
1770 OH_JSVM_Init(&init_options);
1771 aa++;
1772 }
1773 JSVM_VM vm;
1774 JSVM_CreateVMOptions options;
1775 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1776 printf("memset_s failed");
1777 return nullptr;
1778 }
1779 OH_JSVM_CreateVM(&options, &vm);
1780 JSVM_VMScope vm_scope;
1781 OH_JSVM_OpenVMScope(vm, &vm_scope);
1782 JSVM_Env env;
1783 JSVM_CallbackStruct param[1];
1784 param[0].data = nullptr;
1785 param[0].callback = assertEqual;
1786 JSVM_PropertyDescriptor descriptor[] = {
1787 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1788 };
1789 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1790 JSVM_EnvScope envScope;
1791 OH_JSVM_OpenEnvScope(env, &envScope);
1792
1793 equals_test_13(env);
1794 equals_test_14(env);
1795 equals_test_15(env);
1796 equals_test_16(env);
1797 equals_test_17(env);
1798 equals_test_18(env);
1799 equals_test_19(env);
1800 equals_test_20(env);
1801 equals_test_21(env);
1802 equals_test_22(env);
1803 equals_test_23(env);
1804 equals_test_24(env);
1805
1806 OH_JSVM_CloseEnvScope(env, envScope);
1807 OH_JSVM_DestroyEnv(env);
1808 OH_JSVM_CloseVMScope(vm, vm_scope);
1809 OH_JSVM_DestroyVM(vm);
1810 napi_value result11;
1811 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1812 return result11;
1813 }
type_test(JSVM_Env env,JSVM_Value value)1814 void type_test(JSVM_Env env, JSVM_Value value)
1815 {
1816 bool isValue = false;
1817 OH_JSVM_IsUndefined(env, value, &isValue);
1818 OH_JSVM_IsNull(env, value, &isValue);
1819 OH_JSVM_IsNullOrUndefined(env, value, &isValue);
1820 OH_JSVM_IsBoolean(env, value, &isValue);
1821 OH_JSVM_IsNumber(env, value, &isValue);
1822 OH_JSVM_IsString(env, value, &isValue);
1823 OH_JSVM_IsSymbol(env, value, &isValue);
1824 OH_JSVM_IsFunction(env, value, &isValue);
1825 OH_JSVM_IsObject(env, value, &isValue);
1826 OH_JSVM_IsBigInt(env, value, &isValue);
1827 OH_JSVM_IsConstructor(env, value, &isValue);
1828 OH_JSVM_IsMap(env, value, &isValue);
1829 }
type_test_Undefined(JSVM_Env env)1830 void type_test_Undefined(JSVM_Env env)
1831 {
1832 JSVM_HandleScope handleScope;
1833 OH_JSVM_OpenHandleScope(env, &handleScope);
1834
1835 JSVM_Value value = nullptr;
1836 OH_JSVM_GetUndefined(env, &value);
1837 type_test(env, value);
1838
1839 OH_JSVM_CloseHandleScope(env, handleScope);
1840 }
type_test_Null(JSVM_Env env)1841 void type_test_Null(JSVM_Env env)
1842 {
1843 JSVM_HandleScope handleScope;
1844 OH_JSVM_OpenHandleScope(env, &handleScope);
1845
1846 JSVM_Value value = nullptr;
1847 OH_JSVM_GetNull(env, &value);
1848 type_test(env, value);
1849
1850 OH_JSVM_CloseHandleScope(env, handleScope);
1851 }
type_test_Boolean(JSVM_Env env)1852 void type_test_Boolean(JSVM_Env env)
1853 {
1854 JSVM_HandleScope handleScope;
1855 OH_JSVM_OpenHandleScope(env, &handleScope);
1856
1857 JSVM_Value value = nullptr;
1858 bool x = true;
1859 OH_JSVM_GetBoolean(env, x, &value);
1860 type_test(env, value);
1861
1862 OH_JSVM_CloseHandleScope(env, handleScope);
1863 }
type_test_Int32(JSVM_Env env)1864 void type_test_Int32(JSVM_Env env)
1865 {
1866 JSVM_HandleScope handleScope;
1867 OH_JSVM_OpenHandleScope(env, &handleScope);
1868
1869 JSVM_Value value;
1870 OH_JSVM_CreateInt32(env, 1, &value);
1871 type_test(env, value);
1872
1873 OH_JSVM_CloseHandleScope(env, handleScope);
1874 }
type_test_String(JSVM_Env env)1875 void type_test_String(JSVM_Env env)
1876 {
1877 JSVM_HandleScope handleScope;
1878 OH_JSVM_OpenHandleScope(env, &handleScope);
1879
1880 const char testStr[] = "foo";
1881 JSVM_Value value = nullptr;
1882 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
1883 type_test(env, value);
1884
1885 OH_JSVM_CloseHandleScope(env, handleScope);
1886 }
type_test_Symbol(JSVM_Env env)1887 void type_test_Symbol(JSVM_Env env)
1888 {
1889 JSVM_HandleScope handleScope;
1890 OH_JSVM_OpenHandleScope(env, &handleScope);
1891
1892 JSVM_Value value = nullptr;
1893 JSVM_Value description;
1894 const char testStr[] = "1234567";
1895 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &description);
1896 OH_JSVM_CreateSymbol(env, description, &value);
1897 type_test(env, value);
1898
1899 OH_JSVM_CloseHandleScope(env, handleScope);
1900 }
type_test_Function(JSVM_Env env)1901 void type_test_Function(JSVM_Env env)
1902 {
1903 JSVM_HandleScope handleScope;
1904 OH_JSVM_OpenHandleScope(env, &handleScope);
1905
1906 JSVM_Value value = nullptr;
1907 JSVM_CallbackStruct param;
1908 OH_JSVM_CreateFunction(env, "func", JSVM_AUTO_LENGTH, ¶m, &value);
1909 type_test(env, value);
1910
1911 OH_JSVM_CloseHandleScope(env, handleScope);
1912 }
type_test_Object(JSVM_Env env)1913 void type_test_Object(JSVM_Env env)
1914 {
1915 JSVM_HandleScope handleScope;
1916 OH_JSVM_OpenHandleScope(env, &handleScope);
1917
1918 JSVM_Value value = nullptr;
1919 OH_JSVM_CreateObject(env, &value);
1920 type_test(env, value);
1921
1922 OH_JSVM_CloseHandleScope(env, handleScope);
1923 }
type_test_Bigint(JSVM_Env env)1924 void type_test_Bigint(JSVM_Env env)
1925 {
1926 JSVM_HandleScope handleScope;
1927 OH_JSVM_OpenHandleScope(env, &handleScope);
1928
1929 JSVM_Value value = nullptr;
1930 int64_t testValue = INT64_MAX;
1931 OH_JSVM_CreateBigintInt64(env, testValue, &value);
1932 type_test(env, value);
1933
1934 OH_JSVM_CloseHandleScope(env, handleScope);
1935 }
type_test_Map(JSVM_Env env)1936 void type_test_Map(JSVM_Env env)
1937 {
1938 JSVM_HandleScope handleScope;
1939 OH_JSVM_OpenHandleScope(env, &handleScope);
1940
1941 JSVM_Value value = nullptr;
1942 OH_JSVM_CreateMap(env, &value);
1943 type_test(env, value);
1944
1945 OH_JSVM_CloseHandleScope(env, handleScope);
1946 }
testValueOperation5(napi_env env1,napi_callback_info info)1947 static napi_value testValueOperation5(napi_env env1, napi_callback_info info)
1948 {
1949 JSVM_InitOptions init_options;
1950 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
1951 printf("memset_s failed");
1952 return nullptr;
1953 }
1954 init_options.externalReferences = externals;
1955 if (aa == 0) {
1956 OH_JSVM_Init(&init_options);
1957 aa++;
1958 }
1959 JSVM_VM vm;
1960 JSVM_CreateVMOptions options;
1961 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
1962 printf("memset_s failed");
1963 return nullptr;
1964 }
1965 OH_JSVM_CreateVM(&options, &vm);
1966 JSVM_VMScope vm_scope;
1967 OH_JSVM_OpenVMScope(vm, &vm_scope);
1968 JSVM_Env env;
1969 JSVM_CallbackStruct param[1];
1970 param[0].data = nullptr;
1971 param[0].callback = assertEqual;
1972 JSVM_PropertyDescriptor descriptor[] = {
1973 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1974 };
1975 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
1976 JSVM_EnvScope envScope;
1977 OH_JSVM_OpenEnvScope(env, &envScope);
1978
1979 type_test_Undefined(env);
1980 type_test_Null(env);
1981 type_test_Boolean(env);
1982 type_test_Int32(env);
1983 type_test_String(env);
1984 type_test_Symbol(env);
1985 type_test_Function(env);
1986 type_test_Object(env);
1987 type_test_Bigint(env);
1988 type_test_Map(env);
1989
1990 OH_JSVM_CloseEnvScope(env, envScope);
1991 OH_JSVM_DestroyEnv(env);
1992 OH_JSVM_CloseVMScope(vm, vm_scope);
1993 OH_JSVM_DestroyVM(vm);
1994 napi_value result11;
1995 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
1996 return result11;
1997 }
type_test_RegExp_UNICODE(JSVM_Env env)1998 void type_test_RegExp_UNICODE(JSVM_Env env)
1999 {
2000 JSVM_HandleScope handleScope;
2001 OH_JSVM_OpenHandleScope(env, &handleScope);
2002
2003 JSVM_Value value = nullptr;
2004 const char testStr[] = "ab+c";
2005 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2006 JSVM_Value result = nullptr;
2007 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2008 type_test(env, result);
2009
2010 OH_JSVM_CloseHandleScope(env, handleScope);
2011 }
type_test_RegExp_GLOBAL(JSVM_Env env)2012 void type_test_RegExp_GLOBAL(JSVM_Env env)
2013 {
2014 JSVM_HandleScope handleScope;
2015 OH_JSVM_OpenHandleScope(env, &handleScope);
2016
2017 JSVM_Value value = nullptr;
2018 const char testStr[] = "ab+c";
2019 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2020 JSVM_Value result = nullptr;
2021 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_GLOBAL, &result);
2022 type_test(env, result);
2023
2024 OH_JSVM_CloseHandleScope(env, handleScope);
2025 }
type_test_RegExp_IGNORE_CASE(JSVM_Env env)2026 void type_test_RegExp_IGNORE_CASE(JSVM_Env env)
2027 {
2028 JSVM_HandleScope handleScope;
2029 OH_JSVM_OpenHandleScope(env, &handleScope);
2030
2031 JSVM_Value value = nullptr;
2032 const char testStr[] = "ab+c";
2033 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2034 JSVM_Value result = nullptr;
2035 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_IGNORE_CASE, &result);
2036 type_test(env, result);
2037
2038 OH_JSVM_CloseHandleScope(env, handleScope);
2039 }
type_test_RegExp_MULTILINE(JSVM_Env env)2040 void type_test_RegExp_MULTILINE(JSVM_Env env)
2041 {
2042 JSVM_HandleScope handleScope;
2043 OH_JSVM_OpenHandleScope(env, &handleScope);
2044
2045 JSVM_Value value = nullptr;
2046 const char testStr[] = "ab+c";
2047 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2048 JSVM_Value result = nullptr;
2049 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_MULTILINE, &result);
2050 type_test(env, result);
2051
2052 OH_JSVM_CloseHandleScope(env, handleScope);
2053 }
type_test_RegExp_STICKY(JSVM_Env env)2054 void type_test_RegExp_STICKY(JSVM_Env env)
2055 {
2056 JSVM_HandleScope handleScope;
2057 OH_JSVM_OpenHandleScope(env, &handleScope);
2058
2059 JSVM_Value value = nullptr;
2060 const char testStr[] = "ab+c";
2061 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2062 JSVM_Value result = nullptr;
2063 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_STICKY, &result);
2064 type_test(env, result);
2065
2066 OH_JSVM_CloseHandleScope(env, handleScope);
2067 }
type_test_RegExp_DOT_ALL(JSVM_Env env)2068 void type_test_RegExp_DOT_ALL(JSVM_Env env)
2069 {
2070 JSVM_HandleScope handleScope;
2071 OH_JSVM_OpenHandleScope(env, &handleScope);
2072
2073 JSVM_Value value = nullptr;
2074 const char testStr[] = "ab+c";
2075 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2076 JSVM_Value result = nullptr;
2077 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_DOT_ALL, &result);
2078 type_test(env, result);
2079
2080 OH_JSVM_CloseHandleScope(env, handleScope);
2081 }
type_test_RegExp_LINEAR(JSVM_Env env)2082 void type_test_RegExp_LINEAR(JSVM_Env env)
2083 {
2084 JSVM_HandleScope handleScope;
2085 OH_JSVM_OpenHandleScope(env, &handleScope);
2086
2087 JSVM_Value value = nullptr;
2088 const char testStr[] = "ab+c";
2089 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2090 JSVM_Value result = nullptr;
2091 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_LINEAR, &result);
2092 type_test(env, result);
2093
2094 OH_JSVM_CloseHandleScope(env, handleScope);
2095 }
type_test_RegExp_HAS_INDICES(JSVM_Env env)2096 void type_test_RegExp_HAS_INDICES(JSVM_Env env)
2097 {
2098 JSVM_HandleScope handleScope;
2099 OH_JSVM_OpenHandleScope(env, &handleScope);
2100
2101 JSVM_Value value = nullptr;
2102 const char testStr[] = "ab+c";
2103 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2104 JSVM_Value result = nullptr;
2105 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_HAS_INDICES, &result);
2106 type_test(env, result);
2107
2108 OH_JSVM_CloseHandleScope(env, handleScope);
2109 }
type_test_RegExp_UNICODE_SETS(JSVM_Env env)2110 void type_test_RegExp_UNICODE_SETS(JSVM_Env env)
2111 {
2112 JSVM_HandleScope handleScope;
2113 OH_JSVM_OpenHandleScope(env, &handleScope);
2114
2115 JSVM_Value value = nullptr;
2116 const char testStr[] = "ab+c";
2117 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &value);
2118 JSVM_Value result = nullptr;
2119 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE_SETS, &result);
2120 type_test(env, result);
2121
2122 OH_JSVM_CloseHandleScope(env, handleScope);
2123 }
testValueOperation6(napi_env env1,napi_callback_info info)2124 static napi_value testValueOperation6(napi_env env1, napi_callback_info info)
2125 {
2126 JSVM_InitOptions init_options;
2127 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2128 printf("memset_s failed");
2129 return nullptr;
2130 }
2131 init_options.externalReferences = externals;
2132 if (aa == 0) {
2133 OH_JSVM_Init(&init_options);
2134 aa++;
2135 }
2136 JSVM_VM vm;
2137 JSVM_CreateVMOptions options;
2138 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2139 printf("memset_s failed");
2140 return nullptr;
2141 }
2142 OH_JSVM_CreateVM(&options, &vm);
2143 JSVM_VMScope vm_scope;
2144 OH_JSVM_OpenVMScope(vm, &vm_scope);
2145 JSVM_Env env;
2146 JSVM_CallbackStruct param[1];
2147 param[0].data = nullptr;
2148 param[0].callback = assertEqual;
2149 JSVM_PropertyDescriptor descriptor[] = {
2150 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2151 };
2152 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2153 JSVM_EnvScope envScope;
2154 OH_JSVM_OpenEnvScope(env, &envScope);
2155
2156 type_test_RegExp_UNICODE(env);
2157 type_test_RegExp_GLOBAL(env);
2158 type_test_RegExp_IGNORE_CASE(env);
2159 type_test_RegExp_MULTILINE(env);
2160 type_test_RegExp_STICKY(env);
2161 type_test_RegExp_DOT_ALL(env);
2162 type_test_RegExp_LINEAR(env);
2163 type_test_RegExp_HAS_INDICES(env);
2164 type_test_RegExp_UNICODE_SETS(env);
2165
2166 OH_JSVM_CloseEnvScope(env, envScope);
2167 OH_JSVM_DestroyEnv(env);
2168 OH_JSVM_CloseVMScope(vm, vm_scope);
2169 OH_JSVM_DestroyVM(vm);
2170 napi_value result11;
2171 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2172 return result11;
2173 }
type_test_RegExp_Undefined(JSVM_Env env)2174 void type_test_RegExp_Undefined(JSVM_Env env)
2175 {
2176 JSVM_HandleScope handleScope;
2177 OH_JSVM_OpenHandleScope(env, &handleScope);
2178
2179 JSVM_Value value = nullptr;
2180 OH_JSVM_GetUndefined(env, &value);
2181 JSVM_Value result = nullptr;
2182 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2183 type_test(env, result);
2184
2185 OH_JSVM_CloseHandleScope(env, handleScope);
2186 }
type_test_RegExp_Null(JSVM_Env env)2187 void type_test_RegExp_Null(JSVM_Env env)
2188 {
2189 JSVM_HandleScope handleScope;
2190 OH_JSVM_OpenHandleScope(env, &handleScope);
2191
2192 JSVM_Value value = nullptr;
2193 OH_JSVM_GetNull(env, &value);
2194 JSVM_Value result = nullptr;
2195 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2196 type_test(env, result);
2197
2198 OH_JSVM_CloseHandleScope(env, handleScope);
2199 }
type_test_RegExp_Boolean(JSVM_Env env)2200 void type_test_RegExp_Boolean(JSVM_Env env)
2201 {
2202 JSVM_HandleScope handleScope;
2203 OH_JSVM_OpenHandleScope(env, &handleScope);
2204
2205 JSVM_Value value = nullptr;
2206 bool x = true;
2207 OH_JSVM_GetBoolean(env, x, &value);
2208 JSVM_Value result = nullptr;
2209 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2210 type_test(env, result);
2211
2212 OH_JSVM_CloseHandleScope(env, handleScope);
2213 }
type_test_RegExp_Int32(JSVM_Env env)2214 void type_test_RegExp_Int32(JSVM_Env env)
2215 {
2216 JSVM_HandleScope handleScope;
2217 OH_JSVM_OpenHandleScope(env, &handleScope);
2218
2219 JSVM_Value value;
2220 OH_JSVM_CreateInt32(env, 1, &value);
2221 JSVM_Value result = nullptr;
2222 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2223 type_test(env, result);
2224
2225 OH_JSVM_CloseHandleScope(env, handleScope);
2226 }
type_test_RegExp_Symbol(JSVM_Env env)2227 void type_test_RegExp_Symbol(JSVM_Env env)
2228 {
2229 JSVM_HandleScope handleScope;
2230 OH_JSVM_OpenHandleScope(env, &handleScope);
2231
2232 JSVM_Value value = nullptr;
2233 JSVM_Value description;
2234 const char testStr[] = "1234567";
2235 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &description);
2236 OH_JSVM_CreateSymbol(env, description, &value);
2237 JSVM_Value result = nullptr;
2238 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2239 type_test(env, result);
2240
2241 OH_JSVM_CloseHandleScope(env, handleScope);
2242 }
type_test_RegExp_Function(JSVM_Env env)2243 void type_test_RegExp_Function(JSVM_Env env)
2244 {
2245 JSVM_HandleScope handleScope;
2246 OH_JSVM_OpenHandleScope(env, &handleScope);
2247
2248 JSVM_Value value = nullptr;
2249 JSVM_CallbackStruct param;
2250 OH_JSVM_CreateFunction(env, "func", JSVM_AUTO_LENGTH, ¶m, &value);
2251 JSVM_Value result = nullptr;
2252 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2253 type_test(env, result);
2254
2255 OH_JSVM_CloseHandleScope(env, handleScope);
2256 }
type_test_RegExp_Object(JSVM_Env env)2257 void type_test_RegExp_Object(JSVM_Env env)
2258 {
2259 JSVM_HandleScope handleScope;
2260 OH_JSVM_OpenHandleScope(env, &handleScope);
2261
2262 JSVM_Value value = nullptr;
2263 OH_JSVM_CreateObject(env, &value);
2264 JSVM_Value result = nullptr;
2265 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2266 type_test(env, result);
2267
2268 OH_JSVM_CloseHandleScope(env, handleScope);
2269 }
type_test_RegExp_BigintInt64(JSVM_Env env)2270 void type_test_RegExp_BigintInt64(JSVM_Env env)
2271 {
2272 JSVM_HandleScope handleScope;
2273 OH_JSVM_OpenHandleScope(env, &handleScope);
2274
2275 JSVM_Value value = nullptr;
2276 int64_t testValue = INT64_MAX;
2277 OH_JSVM_CreateBigintInt64(env, testValue, &value);
2278 JSVM_Value result = nullptr;
2279 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2280 type_test(env, result);
2281
2282 OH_JSVM_CloseHandleScope(env, handleScope);
2283 }
type_test_RegExp_Map(JSVM_Env env)2284 void type_test_RegExp_Map(JSVM_Env env)
2285 {
2286 JSVM_HandleScope handleScope;
2287 OH_JSVM_OpenHandleScope(env, &handleScope);
2288
2289 JSVM_Value value = nullptr;
2290 OH_JSVM_CreateMap(env, &value);
2291 JSVM_Value result = nullptr;
2292 OH_JSVM_CreateRegExp(env, value, JSVM_RegExpFlags::JSVM_REGEXP_UNICODE, &result);
2293 type_test(env, result);
2294
2295 OH_JSVM_CloseHandleScope(env, handleScope);
2296 }
testValueOperation7(napi_env env1,napi_callback_info info)2297 static napi_value testValueOperation7(napi_env env1, napi_callback_info info)
2298 {
2299 JSVM_InitOptions init_options;
2300 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2301 printf("memset_s failed");
2302 return nullptr;
2303 }
2304 init_options.externalReferences = externals;
2305 if (aa == 0) {
2306 OH_JSVM_Init(&init_options);
2307 aa++;
2308 }
2309 JSVM_VM vm;
2310 JSVM_CreateVMOptions options;
2311 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2312 printf("memset_s failed");
2313 return nullptr;
2314 }
2315 OH_JSVM_CreateVM(&options, &vm);
2316 JSVM_VMScope vm_scope;
2317 OH_JSVM_OpenVMScope(vm, &vm_scope);
2318 JSVM_Env env;
2319 JSVM_CallbackStruct param[1];
2320 param[0].data = nullptr;
2321 param[0].callback = assertEqual;
2322 JSVM_PropertyDescriptor descriptor[] = {
2323 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2324 };
2325 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2326 JSVM_EnvScope envScope;
2327 OH_JSVM_OpenEnvScope(env, &envScope);
2328
2329 type_test_RegExp_Undefined(env);
2330 type_test_RegExp_Null(env);
2331 type_test_RegExp_Boolean(env);
2332 type_test_RegExp_Int32(env);
2333 type_test_RegExp_Symbol(env);
2334 type_test_RegExp_Function(env);
2335 type_test_RegExp_Object(env);
2336 type_test_RegExp_BigintInt64(env);
2337 type_test_RegExp_Map(env);
2338
2339 OH_JSVM_CloseEnvScope(env, envScope);
2340 OH_JSVM_DestroyEnv(env);
2341 OH_JSVM_CloseVMScope(vm, vm_scope);
2342 OH_JSVM_DestroyVM(vm);
2343 napi_value result11;
2344 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2345 return result11;
2346 }
2347
prototypeof_test_1(JSVM_Env env)2348 void prototypeof_test_1(JSVM_Env env)
2349 {
2350 JSVM_HandleScope handleScope;
2351 OH_JSVM_OpenHandleScope(env, &handleScope);
2352
2353 JSVM_Value obj;
2354 OH_JSVM_CreateObject(env, &obj);
2355 JSVM_Value result;
2356 OH_JSVM_ObjectGetPrototypeOf(env, obj, &result);
2357 JSVM_ASSERT_RETURN_VOID(env, result != nullptr, "check OH_JSVM_ObjectGetPrototype");
2358
2359 OH_JSVM_CloseHandleScope(env, handleScope);
2360 }
2361
prototypeof_test_2(JSVM_Env env)2362 void prototypeof_test_2(JSVM_Env env)
2363 {
2364 JSVM_HandleScope handleScope;
2365 OH_JSVM_OpenHandleScope(env, &handleScope);
2366
2367 JSVM_Value obj;
2368 OH_JSVM_CreateObject(env, &obj);
2369 JSVM_Value mySet;
2370 OH_JSVM_CreateSet(env, &mySet);
2371 JSVM_Status status = OH_JSVM_ObjectSetPrototypeOf(env, obj, mySet);
2372 JSVM_ASSERT_RETURN_VOID(env, status == JSVM_OK, "OH_JSVM_ObjectSetPrototypeOf check status");
2373 JSVM_Value proto;
2374 OH_JSVM_ObjectGetPrototypeOf(env, obj, &proto);
2375 bool result = false;
2376 OH_JSVM_StrictEquals(env, proto, mySet, &result);
2377 JSVM_ASSERT_RETURN_VOID(env, result, "check OH_JSVM_ObjectGetPrototype");
2378 OH_JSVM_CloseHandleScope(env, handleScope);
2379 }
2380
prototypeof_test_3(JSVM_Env env)2381 void prototypeof_test_3(JSVM_Env env)
2382 {
2383 JSVM_HandleScope handleScope;
2384 OH_JSVM_OpenHandleScope(env, &handleScope);
2385
2386 JSVM_Value obj;
2387 OH_JSVM_GetUndefined(env, &obj);
2388 JSVM_Value proto;
2389 JSVM_Status status = OH_JSVM_ObjectGetPrototypeOf(env, obj, &proto);
2390 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "check OH_JSVM_ObjectGetPrototype");
2391 OH_JSVM_CloseHandleScope(env, handleScope);
2392 }
2393
prototypeof_test_4(JSVM_Env env)2394 void prototypeof_test_4(JSVM_Env env)
2395 {
2396 JSVM_HandleScope handleScope;
2397 OH_JSVM_OpenHandleScope(env, &handleScope);
2398
2399 JSVM_Value obj;
2400 OH_JSVM_GetUndefined(env, &obj);
2401 JSVM_Value proto = nullptr;
2402 JSVM_Status status = OH_JSVM_ObjectSetPrototypeOf(env, obj, proto);
2403 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype obj not an object");
2404 status = OH_JSVM_ObjectSetPrototypeOf(env, nullptr, proto);
2405 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype obj is null");
2406 OH_JSVM_CloseHandleScope(env, handleScope);
2407 }
2408
prototypeof_test_5(JSVM_Env env)2409 void prototypeof_test_5(JSVM_Env env)
2410 {
2411 JSVM_HandleScope handleScope;
2412 OH_JSVM_OpenHandleScope(env, &handleScope);
2413
2414 JSVM_Value obj;
2415 OH_JSVM_CreateObject(env, &obj);
2416 JSVM_Status status = OH_JSVM_ObjectSetPrototypeOf(env, obj, nullptr);
2417 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype prototype is null");
2418 JSVM_Value proto;
2419 OH_JSVM_GetUndefined(env, &proto);
2420 status = OH_JSVM_ObjectSetPrototypeOf(env, obj, proto);
2421 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype prototype is not an object");
2422 OH_JSVM_CloseHandleScope(env, handleScope);
2423 }
2424
prototypeof_test_6(JSVM_Env env)2425 void prototypeof_test_6(JSVM_Env env)
2426 {
2427 JSVM_HandleScope handleScope;
2428 OH_JSVM_OpenHandleScope(env, &handleScope);
2429
2430 JSVM_Value obj;
2431 OH_JSVM_CreateObject(env, &obj);
2432 JSVM_Status status = OH_JSVM_ObjectSetPrototypeOf(nullptr, obj, nullptr);
2433 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype prototype is null");
2434 JSVM_Value proto;
2435 OH_JSVM_GetUndefined(env, &proto);
2436 status = OH_JSVM_ObjectSetPrototypeOf(nullptr, obj, proto);
2437 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "check OH_JSVM_ObjectSetPrototype prototype is not an object");
2438 OH_JSVM_CloseHandleScope(env, handleScope);
2439 }
2440
set_test_1(JSVM_Env env)2441 void set_test_1(JSVM_Env env)
2442 {
2443 JSVM_HandleScope handleScope;
2444 OH_JSVM_OpenHandleScope(env, &handleScope);
2445 JSVM_Status status = OH_JSVM_CreateSet(env, nullptr);
2446 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "OH_JSVM_CreateSet check status");
2447
2448 OH_JSVM_CloseHandleScope(env, handleScope);
2449 }
2450
set_test_2(JSVM_Env env)2451 void set_test_2(JSVM_Env env)
2452 {
2453 JSVM_HandleScope handleScope;
2454 OH_JSVM_OpenHandleScope(env, &handleScope);
2455
2456 JSVM_Value mySet;
2457 JSVM_Status status = OH_JSVM_CreateSet(env, &mySet);
2458 JSVM_ASSERT_RETURN_VOID(env, status == JSVM_OK, "OH_JSVM_CreateSet check status");
2459 bool isSet = false;
2460 OH_JSVM_IsSet(env, mySet, &isSet);
2461 JSVM_ASSERT_RETURN_VOID(env, mySet != nullptr, "OH_JSVM_CreateSet check status");
2462 OH_JSVM_CloseHandleScope(env, handleScope);
2463 }
2464
set_test_3(JSVM_Env env)2465 void set_test_3(JSVM_Env env)
2466 {
2467 JSVM_HandleScope handleScope;
2468 OH_JSVM_OpenHandleScope(env, &handleScope);
2469
2470 JSVM_Value mySet = nullptr;
2471 bool isSet = false;
2472 OH_JSVM_IsSet(env, mySet, &isSet);
2473 JSVM_ASSERT_RETURN_VOID(env, !isSet, "check OH_JSVM_IsSet when not a set");
2474
2475 OH_JSVM_CloseHandleScope(env, handleScope);
2476 }
2477
set_test_4(JSVM_Env env)2478 void set_test_4(JSVM_Env env)
2479 {
2480 JSVM_HandleScope handleScope;
2481 OH_JSVM_OpenHandleScope(env, &handleScope);
2482 JSVM_Status status = OH_JSVM_CreateSet(nullptr, nullptr);
2483 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "OH_JSVM_CreateSet check status");
2484 bool isSet = false;
2485 JSVM_Status status2 = OH_JSVM_IsSet(nullptr, nullptr, &isSet);
2486 JSVM_ASSERT_RETURN_VOID(env, status2 != JSVM_OK, "OH_JSVM_IsSet check status");
2487 OH_JSVM_CloseHandleScope(env, handleScope);
2488 }
2489
testValueOperation8(napi_env env1,napi_callback_info info)2490 static napi_value testValueOperation8(napi_env env1, napi_callback_info info)
2491 {
2492 JSVM_InitOptions init_options;
2493 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2494 printf("memset_s failed");
2495 return nullptr;
2496 }
2497 init_options.externalReferences = externals;
2498 if (aa == 0) {
2499 OH_JSVM_Init(&init_options);
2500 aa++;
2501 }
2502 JSVM_VM vm;
2503 JSVM_CreateVMOptions options;
2504 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2505 printf("memset_s failed");
2506 return nullptr;
2507 }
2508 OH_JSVM_CreateVM(&options, &vm);
2509 JSVM_VMScope vm_scope;
2510 OH_JSVM_OpenVMScope(vm, &vm_scope);
2511 JSVM_Env env;
2512 JSVM_CallbackStruct param[1];
2513 param[0].data = nullptr;
2514 param[0].callback = assertEqual;
2515 JSVM_PropertyDescriptor descriptor[] = {
2516 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2517 };
2518 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2519 JSVM_EnvScope envScope;
2520 OH_JSVM_OpenEnvScope(env, &envScope);
2521
2522 prototypeof_test_1(env);
2523 prototypeof_test_2(env);
2524 prototypeof_test_3(env);
2525 prototypeof_test_4(env);
2526 prototypeof_test_5(env);
2527 prototypeof_test_6(env);
2528
2529 set_test_1(env);
2530 set_test_2(env);
2531 set_test_3(env);
2532 set_test_4(env);
2533
2534 OH_JSVM_CloseEnvScope(env, envScope);
2535 OH_JSVM_DestroyEnv(env);
2536 OH_JSVM_CloseVMScope(vm, vm_scope);
2537 OH_JSVM_DestroyVM(vm);
2538 napi_value result11;
2539 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2540 return result11;
2541 }
2542
testGetPropertyNames(napi_env env1,napi_callback_info info)2543 static napi_value testGetPropertyNames(napi_env env1, napi_callback_info info) {
2544 JSVM_InitOptions init_options;
2545 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2546 printf("memset_s failed");
2547 return nullptr;
2548 }
2549 init_options.externalReferences = externals;
2550 if (aa == 0) {
2551 OH_JSVM_Init(&init_options);
2552 aa++;
2553 }
2554 JSVM_VM vm;
2555 JSVM_CreateVMOptions options;
2556 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2557 printf("memset_s failed");
2558 return nullptr;
2559 }
2560 OH_JSVM_CreateVM(&options, &vm);
2561 JSVM_VMScope vm_scope;
2562 OH_JSVM_OpenVMScope(vm, &vm_scope);
2563 JSVM_Env env;
2564 JSVM_CallbackStruct param[1];
2565 param[0].data = nullptr;
2566 param[0].callback = assertEqual;
2567 JSVM_PropertyDescriptor descriptor[] = {
2568 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2569 };
2570 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2571 JSVM_EnvScope envScope;
2572 OH_JSVM_OpenEnvScope(env, &envScope);
2573 JSVM_HandleScope handlescope;
2574 OH_JSVM_OpenHandleScope(env, &handlescope);
2575 JSVM_Value result = nullptr;
2576 OH_JSVM_CreateObject(env, &result);
2577 const char testStr[] = "1234567";
2578 JSVM_Value strAttribute = nullptr;
2579 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &strAttribute);
2580 OH_JSVM_SetNamedProperty(env, result, "strAttribute", strAttribute);
2581 bool hasNamedProperty = false;
2582 OH_JSVM_HasNamedProperty(env, result, "strAttribute", &hasNamedProperty);
2583 JSVM_Value retStrAttribute = nullptr;
2584 OH_JSVM_GetNamedProperty(env, result, "strAttribute", &retStrAttribute);
2585 OH_JSVM_CloseHandleScope(env, handlescope);
2586 OH_JSVM_CloseEnvScope(env, envScope);
2587 OH_JSVM_DestroyEnv(env);
2588 OH_JSVM_CloseVMScope(vm, vm_scope);
2589 OH_JSVM_DestroyVM(vm);
2590 napi_value result11;
2591 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2592 return result11;
2593 }
testGetAllPropertyNames(napi_env env1,napi_callback_info info)2594 static napi_value testGetAllPropertyNames(napi_env env1, napi_callback_info info){
2595 JSVM_InitOptions init_options;
2596 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2597 printf("memset_s failed");
2598 return nullptr;
2599 }
2600 init_options.externalReferences = externals;
2601 if (aa == 0) {
2602 OH_JSVM_Init(&init_options);
2603 aa++;
2604 }
2605 JSVM_VM vm;
2606 JSVM_CreateVMOptions options;
2607 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2608 printf("memset_s failed");
2609 return nullptr;
2610 }
2611 OH_JSVM_CreateVM(&options, &vm);
2612 JSVM_VMScope vm_scope;
2613 OH_JSVM_OpenVMScope(vm, &vm_scope);
2614 JSVM_Env env;
2615 JSVM_CallbackStruct param[1];
2616 param[0].data = nullptr;
2617 param[0].callback = assertEqual;
2618 JSVM_PropertyDescriptor descriptor[] = {
2619 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2620 };
2621 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2622 JSVM_EnvScope envScope;
2623 OH_JSVM_OpenEnvScope(env, &envScope);
2624 JSVM_HandleScope handlescope;
2625 OH_JSVM_OpenHandleScope(env, &handlescope);
2626 JSVM_Value returnValue, props;
2627 JSVM_CALL(env, OH_JSVM_CreateObject(env, &returnValue));
2628 addReturnedStatus(env, "envIsNull", returnValue, "Invalid argument", JSVM_Status::JSVM_INVALID_ARG,
2629 OH_JSVM_GetAllPropertyNames(nullptr, returnValue, JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
2630 JSVM_KeyFilter::JSVM_KEY_WRITABLE,
2631 JSVM_KeyConversion::JSVM_KEY_KEEP_NUMBERS, &props));
2632 OH_JSVM_GetAllPropertyNames(env, nullptr, JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
2633 JSVM_KeyFilter::JSVM_KEY_WRITABLE, JSVM_KeyConversion::JSVM_KEY_KEEP_NUMBERS, &props);
2634 addLastStatus(env, "objectIsNull", returnValue);
2635 OH_JSVM_GetAllPropertyNames(env, returnValue, JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
2636 JSVM_KeyFilter::JSVM_KEY_WRITABLE, JSVM_KeyConversion::JSVM_KEY_KEEP_NUMBERS, nullptr);
2637 addLastStatus(env, "valueIsNull", returnValue);
2638 OH_JSVM_CloseHandleScope(env, handlescope);
2639 OH_JSVM_CloseEnvScope(env, envScope);
2640 OH_JSVM_DestroyEnv(env);
2641 OH_JSVM_CloseVMScope(vm, vm_scope);
2642 OH_JSVM_DestroyVM(vm);
2643 napi_value result11;
2644 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2645 return result11;
2646 }
testProperty(napi_env env1,napi_callback_info info)2647 static napi_value testProperty(napi_env env1, napi_callback_info info) {
2648 JSVM_InitOptions init_options;
2649 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2650 printf("memset_s failed");
2651 return nullptr;
2652 }
2653 init_options.externalReferences = externals;
2654 if (aa == 0) {
2655 OH_JSVM_Init(&init_options);
2656 aa++;
2657 }
2658 JSVM_VM vm;
2659 JSVM_CreateVMOptions options;
2660 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2661 printf("memset_s failed");
2662 return nullptr;
2663 }
2664 OH_JSVM_CreateVM(&options, &vm);
2665 JSVM_VMScope vm_scope;
2666 OH_JSVM_OpenVMScope(vm, &vm_scope);
2667 JSVM_Env env;
2668 JSVM_CallbackStruct param[1];
2669 param[0].data = nullptr;
2670 param[0].callback = assertEqual;
2671 JSVM_PropertyDescriptor descriptor[] = {
2672 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2673 };
2674 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2675 JSVM_EnvScope envScope;
2676 OH_JSVM_OpenEnvScope(env, &envScope);
2677 JSVM_HandleScope handlescope;
2678 OH_JSVM_OpenHandleScope(env, &handlescope);
2679 JSVM_Value result = nullptr;
2680 OH_JSVM_CreateObject(env, &result);
2681 const char testStr[] = "1234567";
2682 JSVM_Value strAttribute = nullptr;
2683 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &strAttribute);
2684 OH_JSVM_SetNamedProperty(env, result, "strAttribute", strAttribute);
2685
2686 JSVM_Value retStrAttribute = nullptr;
2687 OH_JSVM_GetNamedProperty(env, result, "strAttribute", &retStrAttribute);
2688
2689 int32_t testNumber = 12345;
2690 JSVM_Value numberAttribute = nullptr;
2691 OH_JSVM_CreateInt32(env, testNumber, &numberAttribute);
2692 OH_JSVM_SetNamedProperty(env, result, "numberAttribute", numberAttribute);
2693 JSVM_Value propNames = nullptr;
2694 OH_JSVM_GetPropertyNames(env, result, &propNames);
2695
2696 bool isArray = false;
2697 OH_JSVM_IsArray(env, propNames, &isArray);
2698 uint32_t arrayLength = 0;
2699 OH_JSVM_GetArrayLength(env, propNames, &arrayLength);
2700
2701 for (uint32_t i = 0; i < arrayLength; i++) {
2702 bool hasElement = false;
2703 OH_JSVM_HasElement(env, propNames, i, &hasElement);
2704 JSVM_Value propName = nullptr;
2705 OH_JSVM_GetElement(env, propNames, i, &propName);
2706 bool hasProp = false;
2707 OH_JSVM_HasProperty(env, result, propName, &hasProp);
2708 JSVM_Value propValue = nullptr;
2709 OH_JSVM_GetProperty(env, result, propName, &propValue);
2710 }
2711 bool deletion = false;
2712 OH_JSVM_DeleteElement(env, propNames, 1, &deletion);
2713 bool hasElement = true;
2714 OH_JSVM_HasElement(env, propNames, 1, &hasElement);
2715 OH_JSVM_CloseHandleScope(env, handlescope);
2716 OH_JSVM_CloseEnvScope(env, envScope);
2717 OH_JSVM_DestroyEnv(env);
2718 OH_JSVM_CloseVMScope(vm, vm_scope);
2719 OH_JSVM_DestroyVM(vm);
2720 napi_value result11;
2721 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2722 return result11;
2723 }
testDefineProperties(napi_env env1,napi_callback_info info)2724 static napi_value testDefineProperties(napi_env env1, napi_callback_info info) {
2725 JSVM_InitOptions init_options;
2726 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2727 printf("memset_s failed");
2728 return nullptr;
2729 }
2730 init_options.externalReferences = externals;
2731 if (aa == 0) {
2732 OH_JSVM_Init(&init_options);
2733 aa++;
2734 }
2735 JSVM_VM vm;
2736 JSVM_CreateVMOptions options;
2737 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2738 printf("memset_s failed");
2739 return nullptr;
2740 }
2741 OH_JSVM_CreateVM(&options, &vm);
2742 JSVM_VMScope vm_scope;
2743 OH_JSVM_OpenVMScope(vm, &vm_scope);
2744 JSVM_Env env;
2745 JSVM_CallbackStruct param[1];
2746 param[0].data = nullptr;
2747 param[0].callback = assertEqual;
2748 JSVM_PropertyDescriptor descriptor[] = {
2749 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2750 };
2751 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2752 JSVM_EnvScope envScope;
2753 OH_JSVM_OpenEnvScope(env, &envScope);
2754 JSVM_HandleScope handlescope;
2755 OH_JSVM_OpenHandleScope(env, &handlescope);
2756 JSVM_Value object, return_value;
2757 JSVM_CallbackStruct param1;
2758 param1.data = nullptr;
2759 param1.callback = defineProperties;
2760 JSVM_PropertyDescriptor desc = {"prop", NULL, ¶m1, NULL, NULL, NULL, JSVM_PropertyAttributes::JSVM_ENUMERABLE};
2761
2762 JSVM_CALL(env, OH_JSVM_CreateObject(env, &object));
2763 JSVM_CALL(env, OH_JSVM_CreateObject(env, &return_value));
2764
2765 addReturnedStatus(env, "envIsNull", return_value, "Invalid argument", JSVM_Status::JSVM_INVALID_ARG,
2766 OH_JSVM_DefineProperties(NULL, object, 1, &desc));
2767 OH_JSVM_DefineProperties(env, NULL, 1, &desc);
2768 OH_JSVM_ObjectFreeze(env, object);
2769 OH_JSVM_ObjectSeal(env, object);
2770 OH_JSVM_CloseHandleScope(env, handlescope);
2771 OH_JSVM_CloseEnvScope(env, envScope);
2772 OH_JSVM_DestroyEnv(env);
2773 OH_JSVM_CloseVMScope(vm, vm_scope);
2774 OH_JSVM_DestroyVM(vm);
2775 napi_value result11;
2776 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2777 return result11;
2778 }
testWrap(napi_env env1,napi_callback_info info)2779 static napi_value testWrap(napi_env env1, napi_callback_info info){
2780 JSVM_InitOptions init_options;
2781 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2782 printf("memset_s failed");
2783 return nullptr;
2784 }
2785 init_options.externalReferences = externals;
2786 if (aa == 0) {
2787 OH_JSVM_Init(&init_options);
2788 aa++;
2789 }
2790 JSVM_VM vm;
2791 JSVM_CreateVMOptions options;
2792 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2793 printf("memset_s failed");
2794 return nullptr;
2795 }
2796 OH_JSVM_CreateVM(&options, &vm);
2797 JSVM_VMScope vm_scope;
2798 OH_JSVM_OpenVMScope(vm, &vm_scope);
2799 JSVM_Env env;
2800 JSVM_CallbackStruct param[1];
2801 param[0].data = nullptr;
2802 param[0].callback = assertEqual;
2803 JSVM_PropertyDescriptor descriptor[] = {
2804 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2805 };
2806 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2807 JSVM_EnvScope envScope;
2808 OH_JSVM_OpenEnvScope(env, &envScope);
2809 JSVM_HandleScope handlescope;
2810 OH_JSVM_OpenHandleScope(env, &handlescope);
2811 JSVM_Value testClass = nullptr;
2812 JSVM_CallbackStruct param1;
2813 param1.data = nullptr;
2814 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
2815 JSVM_Value thisVar = nullptr;
2816 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
2817
2818 return thisVar;
2819 };
2820 OH_JSVM_DefineClass(env, "TestClass", NAPI_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
2821
2822 JSVM_Value instanceValue = nullptr;
2823 OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
2824
2825 const char *testStr = "test";
2826 OH_JSVM_Wrap(
2827 env, instanceValue, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, nullptr, nullptr);
2828 const char *tmpTestStr = nullptr;
2829 OH_JSVM_Unwrap(env, instanceValue, (void **)&tmpTestStr);
2830 const char *tmpTestStr1 = nullptr;
2831 OH_JSVM_RemoveWrap(env, instanceValue, (void **)&tmpTestStr1);
2832 OH_JSVM_Unwrap(env, instanceValue, (void **)&tmpTestStr1);
2833 OH_JSVM_CloseHandleScope(env, handlescope);
2834 OH_JSVM_CloseEnvScope(env, envScope);
2835 OH_JSVM_DestroyEnv(env);
2836 OH_JSVM_CloseVMScope(vm, vm_scope);
2837 OH_JSVM_DestroyVM(vm);
2838 napi_value result11;
2839 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2840 return result11;
2841 }
2842 static JSVM_Deferred deferred = nullptr;
testOthers(napi_env env1,napi_callback_info info)2843 static napi_value testOthers(napi_env env1, napi_callback_info info){
2844 JSVM_InitOptions init_options;
2845 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2846 printf("memset_s failed");
2847 return nullptr;
2848 }
2849 init_options.externalReferences = externals;
2850 if (aa == 0) {
2851 OH_JSVM_Init(&init_options);
2852 aa++;
2853 }
2854 JSVM_VM vm;
2855 JSVM_CreateVMOptions options;
2856 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2857 printf("memset_s failed");
2858 return nullptr;
2859 }
2860 OH_JSVM_CreateVM(&options, &vm);
2861 JSVM_VMScope vm_scope;
2862 OH_JSVM_OpenVMScope(vm, &vm_scope);
2863 JSVM_Env env;
2864 JSVM_CallbackStruct param[1];
2865 param[0].data = nullptr;
2866 param[0].callback = assertEqual;
2867 JSVM_PropertyDescriptor descriptor[] = {
2868 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2869 };
2870 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2871 JSVM_EnvScope envScope;
2872 OH_JSVM_OpenEnvScope(env, &envScope);
2873 JSVM_HandleScope handlescope;
2874 OH_JSVM_OpenHandleScope(env, &handlescope);
2875 uint32_t result;
2876 OH_JSVM_GetVersion(env, &result);
2877 JSVM_VMInfo result1;
2878 OH_JSVM_GetVMInfo(&result1);
2879 int64_t adjustedValue;
2880 OH_JSVM_AdjustExternalMemory(env, 1, &adjustedValue);
2881 JSVM_Value promise;
2882 OH_JSVM_CreatePromise(env, &deferred, &promise);
2883 bool is_promise;
2884 OH_JSVM_IsPromise(env, promise, &is_promise);
2885 const char *blob_data = nullptr;
2886 size_t blob_size = 0;
2887 JSVM_Env envs[1] = {env};
2888 OH_JSVM_CreateSnapshot(vm, 1, envs, &blob_data, &blob_size);
2889 OH_JSVM_CloseHandleScope(env, handlescope);
2890 OH_JSVM_CloseEnvScope(env, envScope);
2891 OH_JSVM_DestroyEnv(env);
2892 OH_JSVM_CloseVMScope(vm, vm_scope);
2893 OH_JSVM_DestroyVM(vm);
2894 napi_value result11;
2895 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
2896 return result11;
2897 }
2898
2899
Add(napi_env env1,napi_callback_info info)2900 static napi_value Add(napi_env env1, napi_callback_info info) {
2901 JSVM_InitOptions init_options;
2902 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
2903 printf("memset_s failed");
2904 return nullptr;
2905 }
2906 init_options.externalReferences = externals;
2907 if (aa == 0) {
2908 OH_JSVM_Init(&init_options);
2909 aa++;
2910 }
2911 JSVM_VM vm;
2912 JSVM_CreateVMOptions options;
2913 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
2914 printf("memset_s failed");
2915 return nullptr;
2916 }
2917 OH_JSVM_CreateVM(&options, &vm);
2918 JSVM_VMScope vm_scope;
2919 OH_JSVM_OpenVMScope(vm, &vm_scope);
2920 JSVM_Env env;
2921
2922 JSVM_CallbackStruct param[5];
2923 for (int i = 0; i < 5; i++) {
2924 param[i].data = nullptr;
2925 }
2926 param[0].callback = assertEqual;
2927 param[1].callback = assertTrue;
2928 param[2].callback = assertFalse;
2929 param[3].callback = consoleinfo;
2930 param[4].callback = isArray;
2931
2932 JSVM_PropertyDescriptor descriptor[] = {
2933 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
2934 {"assertTrue", NULL, ¶m[1], NULL, NULL, NULL, JSVM_DEFAULT},
2935 {"assertFalse", NULL, ¶m[2], NULL, NULL, NULL, JSVM_DEFAULT},
2936 {"consoleinfo", NULL, ¶m[3], NULL, NULL, NULL, JSVM_DEFAULT},
2937 {"isArray", NULL, ¶m[4], NULL, NULL, NULL, JSVM_DEFAULT},
2938 };
2939
2940 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
2941 JSVM_EnvScope envScope;
2942 OH_JSVM_OpenEnvScope(env, &envScope);
2943 JSVM_HandleScope handlescope;
2944 OH_JSVM_OpenHandleScope(env, &handlescope);
2945 const char* sourcecodestr = "\
2946 {\
2947 var length = 5; var arr = new Array(length); arr[4] = 123;\
2948 }\
2949 ";
2950 JSVM_Value sourcecodevalue;
2951 OH_JSVM_CreateStringUtf8(env, sourcecodestr, strlen(sourcecodestr), &sourcecodevalue);
2952 JSVM_Script script;
2953 OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script);
2954 JSVM_Value result;
2955 OH_JSVM_RunScript(env, script, &result);
2956 int32_t value2;
2957 JSVM_CALL(env, OH_JSVM_GetValueInt32(env, result, &value2));
2958 OH_JSVM_CloseHandleScope(env, handlescope);
2959 OH_JSVM_CloseEnvScope(env, envScope);
2960 OH_JSVM_DestroyEnv(env);
2961 // shaoshuai
2962 OH_JSVM_CloseVMScope(vm, vm_scope);
2963 OH_JSVM_DestroyVM(vm);
2964 return nullptr;
2965 }
test(JSVM_Env env,JSVM_CallbackInfo info)2966 static JSVM_Value test(JSVM_Env env, JSVM_CallbackInfo info) {
2967 JSVM_VM vm1 = nullptr;
2968 JSVM_Env env2;
2969 JSVM_Script script = nullptr;
2970 OH_JSVM_CreateEnvFromSnapshot(vm1, 0, &env2);
2971 const uint8_t **data_ptr = nullptr;
2972 size_t *length_ptr =nullptr;
2973 OH_JSVM_CreateCodeCache(env, script, data_ptr, length_ptr);
2974 OH_JSVM_SetInstanceData(
2975 env2, 0, [](JSVM_Env env, void *data, void *hint) { ++*static_cast<int *>(data); }, nullptr);
2976 JSVM_Ref *refValues;
2977 OH_JSVM_GetInstanceData(env, (void **)&refValues);
2978 JSVM_Value object = nullptr;
2979 OH_JSVM_SetProperty(env, object, 0, NULL);
2980 JSVM_Value key = nullptr;
2981 bool result;
2982 OH_JSVM_DeleteProperty(env, object, key, &result);
2983 OH_JSVM_HasOwnProperty(env, object, key, &result);
2984 OH_JSVM_SetElement(NULL, object, 0, object);
2985 JSVM_Value ret;
2986 JSVM_Value funcValue = nullptr;
2987 JSVM_CallbackStruct param;
2988 OH_JSVM_CreateFunction(env, "func", NAPI_AUTO_LENGTH, ¶m, &funcValue);
2989 OH_JSVM_CallFunction(env, nullptr, funcValue, 0, nullptr, &ret);
2990 JSVM_Value constructor = nullptr;
2991 JSVM_CallbackInfo info1 = nullptr;
2992 OH_JSVM_GetNewTarget(env, info1, &constructor);
2993 JSVM_Value value = nullptr;
2994 OH_JSVM_TypeTagObject(env, value,nullptr);
2995 OH_JSVM_CheckObjectTypeTag(env, object, nullptr, &result);
2996 JSVM_Finalize finalizeCb = nullptr;
2997 JSVM_Ref result1;
2998 OH_JSVM_AddFinalizer(env, object, NULL, finalizeCb, NULL, &result1);
2999 OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_NONE);
3000 JSVM_Deferred deferred = nullptr;
3001 OH_JSVM_ResolveDeferred(env, deferred,value);
3002 OH_JSVM_RejectDeferred(env, deferred, value);
3003 JSVM_Value value1;
3004 OH_JSVM_JsonParse(env, value, &value1);
3005 OH_JSVM_JsonStringify(env, object, &value1);
3006 return nullptr;
3007 }
Add1(napi_env env1,napi_callback_info info)3008 static napi_value Add1(napi_env env1, napi_callback_info info) {
3009 JSVM_InitOptions init_options;
3010 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3011 printf("memset_s failed");
3012 return nullptr;
3013 }
3014 init_options.externalReferences = externals;
3015 if (aa == 0) {
3016 OH_JSVM_Init(&init_options);
3017 aa++;
3018 }
3019 JSVM_VM vm;
3020 JSVM_CreateVMOptions options;
3021 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3022 printf("memset_s failed");
3023 return nullptr;
3024 }
3025 OH_JSVM_CreateVM(&options, &vm);
3026 JSVM_VMScope vm_scope;
3027 OH_JSVM_OpenVMScope(vm, &vm_scope);
3028 JSVM_Env env;
3029 JSVM_CallbackStruct param[1];
3030 param[0].data = nullptr;
3031 param[0].callback = test;
3032 JSVM_PropertyDescriptor descriptor[] = {
3033 {"test", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3034 };
3035 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3036 JSVM_EnvScope envScope;
3037 OH_JSVM_OpenEnvScope(env, &envScope);
3038 JSVM_HandleScope handlescope;
3039 OH_JSVM_OpenHandleScope(env, &handlescope);
3040 OH_JSVM_CloseHandleScope(env, handlescope);
3041 OH_JSVM_CloseEnvScope(env, envScope);
3042 OH_JSVM_DestroyEnv(env);
3043 OH_JSVM_CloseVMScope(vm, vm_scope);
3044 OH_JSVM_DestroyVM(vm);
3045 napi_value result11;
3046 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3047 return result11;
3048 }
output_stream(const char * data,int size,void * stream_data)3049 static bool output_stream(const char *data, int size, void *stream_data) {
3050 return true;
3051 }
theSecondOperations(JSVM_Env env,JSVM_CallbackInfo info)3052 static JSVM_Value theSecondOperations(JSVM_Env env, JSVM_CallbackInfo info) {
3053 JSVM_VM vm;
3054 OH_JSVM_CreateVM(nullptr, &vm);
3055 void *data = nullptr;
3056 JSVM_HeapStatistics stats;
3057 OH_JSVM_GetHeapStatistics(vm, &stats);
3058 OH_JSVM_TakeHeapSnapshot(vm,output_stream,data);
3059 JSVM_CpuProfiler cpu_profiler;
3060 OH_JSVM_StartCpuProfiler(vm, &cpu_profiler);
3061 OH_JSVM_StopCpuProfiler( vm,cpu_profiler,output_stream,data);
3062 return nullptr;
3063 }
testSecondOperations(napi_env env1,napi_callback_info info)3064 static napi_value testSecondOperations(napi_env env1, napi_callback_info info) {
3065 JSVM_InitOptions init_options;
3066 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3067 printf("memset_s failed");
3068 return nullptr;
3069 }
3070 init_options.externalReferences = externals;
3071 if (aa == 0) {
3072 OH_JSVM_Init(&init_options);
3073 aa++;
3074 }
3075 JSVM_VM vm;
3076 JSVM_CreateVMOptions options;
3077 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3078 printf("memset_s failed");
3079 return nullptr;
3080 }
3081 OH_JSVM_CreateVM(&options, &vm);
3082 JSVM_VMScope vm_scope;
3083 OH_JSVM_OpenVMScope(vm, &vm_scope);
3084 JSVM_Env env;
3085 JSVM_CallbackStruct param[1];
3086 param[0].data = nullptr;
3087 param[0].callback = theSecondOperations;
3088 JSVM_PropertyDescriptor descriptor[] = {
3089 {"theSecondOperations", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3090 };
3091 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3092 uint16_t num = 9229;
3093 OH_JSVM_OpenInspector(env, "localhost", num);
3094 JSVM_EnvScope envScope;
3095 OH_JSVM_OpenEnvScope(env, &envScope);
3096 JSVM_HandleScope handlescope;
3097 OH_JSVM_OpenHandleScope(env, &handlescope);
3098 OH_JSVM_CloseHandleScope(env, handlescope);
3099 OH_JSVM_CloseInspector(env);
3100 OH_JSVM_CloseEnvScope(env, envScope);
3101 OH_JSVM_DestroyEnv(env);
3102 OH_JSVM_CloseVMScope(vm, vm_scope);
3103 OH_JSVM_DestroyVM(vm);
3104 napi_value result11;
3105 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3106 return result11;
3107 }
3108
3109 // ========= arguments check =========
checkArgs(JSVM_Env env,JSVM_CallbackInfo info)3110 static JSVM_Value checkArgs(JSVM_Env env, JSVM_CallbackInfo info)
3111 {
3112 JSVM_CallbackStruct param;
3113 auto func = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3114 JSVM_Value thisVar = nullptr;
3115 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3116 return thisVar;
3117 };
3118 param.callback = func;
3119 param.data = nullptr;
3120 JSVM_Value testWrapClass = nullptr;
3121
3122 // case 1. If env is null ptr, return JSVM_INVALID_ARG.
3123 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(nullptr, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3124 &propertyCfg, ¶m, &testWrapClass);
3125 if (res == JSVM_INVALID_ARG) {
3126 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr env");
3127 }
3128
3129 // case 1. If utf8name is nullptr, return JSVM_INVALID_ARG.
3130 res = OH_JSVM_DefineClassWithPropertyHandler(env, nullptr, NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3131 &propertyCfg, ¶m, &testWrapClass);
3132 if (res == JSVM_INVALID_ARG) {
3133 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr utf8name");
3134 }
3135
3136 // case 3. if constructor is nullptr, return JSVM_INVALID_ARG.
3137 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, nullptr, 0, nullptr, &propertyCfg,
3138 ¶m, &testWrapClass);
3139 if (res == JSVM_INVALID_ARG) {
3140 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr constructor");
3141 }
3142
3143 // case 4. If the callback of the constructor is nullptr, return JSVM_INVALID_ARG.
3144 param.callback = nullptr;
3145 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3146 &propertyCfg, ¶m, &testWrapClass);
3147 if (res == JSVM_INVALID_ARG) {
3148 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr constructor callback");
3149 }
3150 param.callback = func;
3151
3152 // case 5. If propertyCount is greater than 0, properties are nullptr
3153 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 1, nullptr, &propertyCfg,
3154 ¶m, &testWrapClass);
3155 if (res == JSVM_INVALID_ARG) {
3156 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr properties");
3157 }
3158
3159 // case 6. If propertyHandlerCfg is nullptr, return JSVM_INVALID_ARG.
3160 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, nullptr,
3161 ¶m, &testWrapClass);
3162 if (res == JSVM_INVALID_ARG) {
3163 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr propertyHandlerCfg");
3164 }
3165
3166 // case 7. If result is nullptr, return JSVM_INVALID_ARG.
3167 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg,
3168 ¶m, nullptr);
3169 if (res == JSVM_INVALID_ARG) {
3170 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler failed due to nullptr result");
3171 }
3172
3173 // case 8. Valid input parameter, return JSVM_OK.
3174 res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test1", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg,
3175 nullptr, &testWrapClass);
3176 if (res == JSVM_OK) {
3177 JSVM_ASSERT(env, res == JSVM_INVALID_ARG, "OH_JSVM_DefineClassWithPropertyHandler successfully");
3178 }
3179 return nullptr;
3180 }
3181
testCheckArgs(napi_env env1,napi_callback_info info)3182 static napi_value testCheckArgs(napi_env env1, napi_callback_info info)
3183 {
3184 JSVM_InitOptions init_options;
3185 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3186 printf("memset_s failed");
3187 return nullptr;
3188 }
3189 init_options.externalReferences = externals;
3190 if (aa == 0) {
3191 OH_JSVM_Init(&init_options);
3192 aa++;
3193 }
3194 JSVM_VM vm;
3195 JSVM_CreateVMOptions options;
3196 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3197 printf("memset_s failed");
3198 return nullptr;
3199 }
3200 OH_JSVM_CreateVM(&options, &vm);
3201 JSVM_VMScope vm_scope;
3202 OH_JSVM_OpenVMScope(vm, &vm_scope);
3203 JSVM_Env env;
3204 JSVM_CallbackStruct param[1];
3205 param[0].callback = nullptr;
3206 param[0].callback = checkArgs;
3207
3208 JSVM_PropertyDescriptor descriptor[] = {
3209 {"checkArgs", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3210 };
3211 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3212 JSVM_EnvScope envScope;
3213 OH_JSVM_OpenEnvScope(env, &envScope);
3214 JSVM_HandleScope handlescope;
3215 OH_JSVM_OpenHandleScope(env, &handlescope);
3216 OH_JSVM_CloseHandleScope(env, handlescope);
3217 OH_JSVM_CloseEnvScope(env, envScope);
3218 OH_JSVM_DestroyEnv(env);
3219 OH_JSVM_CloseVMScope(vm, vm_scope);
3220 OH_JSVM_DestroyVM(vm);
3221 napi_value result11;
3222 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3223 return result11;
3224 }
3225
3226 // ============ empty propertyHandlerCfg ============================
3227 // case 9. setProperty
setProperty(JSVM_Env env,JSVM_CallbackInfo info)3228 static JSVM_Value setProperty(JSVM_Env env, JSVM_CallbackInfo info)
3229 {
3230 JSVM_CallbackStruct param;
3231 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3232 JSVM_Value thisVar = nullptr;
3233 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3234 return thisVar;
3235 };
3236 param.data = nullptr;
3237 JSVM_Value testWrapClass = nullptr;
3238
3239 OH_JSVM_DefineClassWithPropertyHandler(env, "Test2", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg, nullptr,
3240 &testWrapClass);
3241 JSVM_Value instanceValue = nullptr;
3242 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3243
3244 const char testStr[] = "hello world";
3245 JSVM_Value setvalueName = nullptr;
3246 JSVM_Value result = nullptr;
3247 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3248 JSVM_Status res = OH_JSVM_SetNamedProperty(env, instanceValue, "mySettedProperty", setvalueName);
3249 if (res == JSVM_OK) {
3250 char resultStr[] = "set property successfully";
3251 OH_JSVM_CreateStringUtf8(env, resultStr, strlen(resultStr), &result);
3252 }
3253 return result;
3254 }
3255
testSetProperty(napi_env env1,napi_callback_info info)3256 static napi_value testSetProperty(napi_env env1, napi_callback_info info)
3257 {
3258 JSVM_InitOptions init_options;
3259 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3260 printf("memset_s failed");
3261 return nullptr;
3262 }
3263 init_options.externalReferences = externals;
3264 if (aa == 0) {
3265 OH_JSVM_Init(&init_options);
3266 aa++;
3267 }
3268 JSVM_VM vm;
3269 JSVM_CreateVMOptions options;
3270 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3271 printf("memset_s failed");
3272 return nullptr;
3273 }
3274 OH_JSVM_CreateVM(&options, &vm);
3275 JSVM_VMScope vm_scope;
3276 OH_JSVM_OpenVMScope(vm, &vm_scope);
3277 JSVM_Env env;
3278
3279 JSVM_CallbackStruct param[1];
3280 param[0].callback = nullptr;
3281 param[0].callback = setProperty;
3282
3283 JSVM_PropertyDescriptor descriptor[] = {
3284 {"setProperty", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3285 };
3286 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3287 JSVM_EnvScope envScope;
3288 OH_JSVM_OpenEnvScope(env, &envScope);
3289 JSVM_HandleScope handlescope;
3290 OH_JSVM_OpenHandleScope(env, &handlescope);
3291 OH_JSVM_CloseHandleScope(env, handlescope);
3292 OH_JSVM_CloseEnvScope(env, envScope);
3293 OH_JSVM_DestroyEnv(env);
3294 OH_JSVM_CloseVMScope(vm, vm_scope);
3295 OH_JSVM_DestroyVM(vm);
3296 napi_value result11;
3297 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3298 return result11;
3299 }
3300
3301 // case 10. getProperty
getProperty(JSVM_Env env,JSVM_CallbackInfo info)3302 static JSVM_Value getProperty(JSVM_Env env, JSVM_CallbackInfo info)
3303 {
3304 JSVM_CallbackStruct param;
3305 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3306 JSVM_Value thisVar = nullptr;
3307 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3308 return thisVar;
3309 };
3310 param.data = nullptr;
3311 JSVM_Value testWrapClass = nullptr;
3312 OH_JSVM_DefineClassWithPropertyHandler(env, "Test3", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg, nullptr,
3313 &testWrapClass);
3314 JSVM_Value instanceValue = nullptr;
3315 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3316
3317 const char testStr[] = "hello world";
3318 JSVM_Value setvalueName = nullptr;
3319 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3320 OH_JSVM_SetNamedProperty(env, instanceValue, "mySettedProperty", setvalueName);
3321
3322 JSVM_Value valueName = nullptr;
3323 JSVM_Status res = OH_JSVM_GetNamedProperty(env, instanceValue, "mySettedProperty", &valueName);
3324 if (res == JSVM_OK) {
3325 printf("JSVM getProperty successfully");
3326 }
3327 return valueName;
3328 }
3329
testGetProperty(napi_env env1,napi_callback_info info)3330 static napi_value testGetProperty(napi_env env1, napi_callback_info info)
3331 {
3332 JSVM_InitOptions init_options;
3333 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3334 printf("memset_s failed");
3335 return nullptr;
3336 }
3337 init_options.externalReferences = externals;
3338 if (aa == 0) {
3339 OH_JSVM_Init(&init_options);
3340 aa++;
3341 }
3342 JSVM_VM vm;
3343 JSVM_CreateVMOptions options;
3344 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3345 printf("memset_s failed");
3346 return nullptr;
3347 }
3348 OH_JSVM_CreateVM(&options, &vm);
3349 JSVM_VMScope vm_scope;
3350 OH_JSVM_OpenVMScope(vm, &vm_scope);
3351 JSVM_Env env;
3352 JSVM_CallbackStruct param[1];
3353 param[0].callback = nullptr;
3354 param[0].callback = getProperty;
3355 JSVM_PropertyDescriptor descriptor[] = {
3356 {"getProperty", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3357 };
3358 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3359 JSVM_EnvScope envScope;
3360 OH_JSVM_OpenEnvScope(env, &envScope);
3361 JSVM_HandleScope handlescope;
3362 OH_JSVM_OpenHandleScope(env, &handlescope);
3363 OH_JSVM_CloseHandleScope(env, handlescope);
3364 OH_JSVM_CloseEnvScope(env, envScope);
3365 OH_JSVM_DestroyEnv(env);
3366 OH_JSVM_CloseVMScope(vm, vm_scope);
3367 OH_JSVM_DestroyVM(vm);
3368 napi_value result11;
3369 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3370 return result11;
3371 }
3372
3373 // case 11 and case 12
CallObjectAsFunction(JSVM_Env env,JSVM_CallbackInfo info)3374 static JSVM_Value CallObjectAsFunction(JSVM_Env env, JSVM_CallbackInfo info)
3375 {
3376 JSVM_CallbackStruct param;
3377 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3378 JSVM_Value thisVar = nullptr;
3379 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3380 return thisVar;
3381 };
3382 param.data = nullptr;
3383
3384 JSVM_CallbackStruct callbackStruct;
3385 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3386 JSVM_Value thisVar = nullptr;
3387 void *innerData;
3388 size_t argc = 1;
3389 JSVM_Value args[1];
3390 OH_JSVM_GetCbInfo(env, info, &argc, args, &thisVar, &innerData);
3391 uint32_t ret = 0;
3392 OH_JSVM_GetValueUint32(env, args[0], &ret);
3393 const char testStr[] = "hello world 111111";
3394 JSVM_Value setvalueName = nullptr;
3395 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3396 return setvalueName;
3397 };
3398 char data[100] = "hello world";
3399 callbackStruct.data = data;
3400 JSVM_Value testWrapClass = nullptr;
3401 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test12", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3402 &propertyCfg, &callbackStruct, &testWrapClass);
3403 if (res == JSVM_OK) {
3404 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3405 }
3406 JSVM_Value instanceValue = nullptr;
3407 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3408
3409 JSVM_Value gloablObj = nullptr;
3410 OH_JSVM_GetGlobal(env, &gloablObj);
3411 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance", instanceValue);
3412 return nullptr;
3413 }
3414
testCallFunction(napi_env env1,napi_callback_info info)3415 static napi_value testCallFunction(napi_env env1, napi_callback_info info)
3416 {
3417 JSVM_InitOptions init_options;
3418 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3419 printf("memset_s failed");
3420 return nullptr;
3421 }
3422 init_options.externalReferences = externals;
3423 if (aa == 0) {
3424 OH_JSVM_Init(&init_options);
3425 aa++;
3426 }
3427 JSVM_VM vm;
3428 JSVM_CreateVMOptions options;
3429 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3430 printf("memset_s failed");
3431 return nullptr;
3432 }
3433 OH_JSVM_CreateVM(&options, &vm);
3434 JSVM_VMScope vm_scope;
3435 OH_JSVM_OpenVMScope(vm, &vm_scope);
3436 JSVM_Env env;
3437
3438 JSVM_CallbackStruct param[1];
3439 param[0].callback = nullptr;
3440 param[0].callback = CallObjectAsFunction;
3441
3442 JSVM_PropertyDescriptor descriptor[] = {
3443 {"CallObjectAsFunction", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3444 };
3445 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3446 JSVM_EnvScope envScope;
3447 OH_JSVM_OpenEnvScope(env, &envScope);
3448 JSVM_HandleScope handlescope;
3449 OH_JSVM_OpenHandleScope(env, &handlescope);
3450 OH_JSVM_CloseHandleScope(env, handlescope);
3451 OH_JSVM_CloseEnvScope(env, envScope);
3452 OH_JSVM_DestroyEnv(env);
3453 OH_JSVM_CloseVMScope(vm, vm_scope);
3454 OH_JSVM_DestroyVM(vm);
3455 napi_value result11;
3456 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3457 return result11;
3458 }
3459
3460 // ============================= test nullptr return for namedProperty handle ====================================
3461 // 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)3462 static JSVM_Value SetNamedPropertyCbInfo1(JSVM_Env env, JSVM_Value name, JSVM_Value property, JSVM_Value thisArg,
3463 JSVM_Value data)
3464 {
3465 char strValue[100];
3466 size_t size;
3467 size_t bufferSize = 300;
3468 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
3469 char str[100];
3470 size_t size1;
3471 OH_JSVM_GetValueStringUtf8(env, property, str, strlen(str), &size1);
3472 JSVM_Value gloablObj = nullptr;
3473 OH_JSVM_GetGlobal(env, &gloablObj);
3474 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
3475 return nullptr;
3476 }
3477
NameHandler(JSVM_Env env,JSVM_CallbackInfo info)3478 static JSVM_Value NameHandler(JSVM_Env env, JSVM_CallbackInfo info)
3479 {
3480 JSVM_CallbackStruct param;
3481 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3482 JSVM_Value thisVar = nullptr;
3483 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3484 return thisVar;
3485 };
3486 param.data = nullptr;
3487 propertyCfg.genericNamedPropertySetterCallback = SetNamedPropertyCbInfo1;
3488 JSVM_CallbackStruct callbackStruct;
3489 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3490 printf("call as a function called");
3491 return nullptr;
3492 };
3493 callbackStruct.data = nullptr;
3494 JSVM_Value testWrapClass = nullptr;
3495 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test4", NAPI_AUTO_LENGTH, ¶m, 0, nullptr, &propertyCfg, &callbackStruct, &testWrapClass);
3496 if (res == JSVM_OK) {
3497 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3498 } else {
3499 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3500 return nullptr;
3501 }
3502 JSVM_Value instanceValue = nullptr;
3503 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3504
3505 const char testStr[] = "hello world1";
3506 JSVM_Value setvalueName = nullptr;
3507 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3508 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty1", setvalueName);
3509 JSVM_Value valueName = nullptr;
3510 OH_JSVM_GetNamedProperty(env, instanceValue, "nameProperty1", &valueName);
3511 propertyCfg.genericNamedPropertySetterCallback = nullptr;
3512 return valueName;
3513 }
3514
testSetNamedProperty01(napi_env env1,napi_callback_info info)3515 static napi_value testSetNamedProperty01(napi_env env1, napi_callback_info info)
3516 {
3517 JSVM_InitOptions init_options;
3518 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3519 printf("memset_s failed");
3520 return nullptr;
3521 }
3522 init_options.externalReferences = externals;
3523 if (aa == 0) {
3524 OH_JSVM_Init(&init_options);
3525 aa++;
3526 }
3527 JSVM_VM vm;
3528 JSVM_CreateVMOptions options;
3529 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3530 printf("memset_s failed");
3531 return nullptr;
3532 }
3533 OH_JSVM_CreateVM(&options, &vm);
3534 JSVM_VMScope vm_scope;
3535 OH_JSVM_OpenVMScope(vm, &vm_scope);
3536 JSVM_Env env;
3537
3538 JSVM_CallbackStruct param[1];
3539 param[0].callback = nullptr;
3540 param[0].callback = NameHandler;
3541
3542 JSVM_PropertyDescriptor descriptor[] = {
3543 {"NameHandler", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3544 };
3545 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3546 JSVM_EnvScope envScope;
3547 OH_JSVM_OpenEnvScope(env, &envScope);
3548 JSVM_HandleScope handlescope;
3549 OH_JSVM_OpenHandleScope(env, &handlescope);
3550 OH_JSVM_CloseHandleScope(env, handlescope);
3551 OH_JSVM_CloseEnvScope(env, envScope);
3552 OH_JSVM_DestroyEnv(env);
3553 OH_JSVM_CloseVMScope(vm, vm_scope);
3554 OH_JSVM_DestroyVM(vm);
3555 napi_value result11;
3556 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3557 return result11;
3558 }
3559
3560 // ============================= test non-nullptr return for namedProperty handle ================================
3561 // 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)3562 static JSVM_Value SetNamedPropertyCbInfo2(JSVM_Env env, JSVM_Value name, JSVM_Value property, JSVM_Value thisArg,
3563 JSVM_Value data)
3564 {
3565 return property;
3566 }
3567
NameHandler1(JSVM_Env env,JSVM_CallbackInfo info)3568 static JSVM_Value NameHandler1(JSVM_Env env, JSVM_CallbackInfo info)
3569 {
3570 JSVM_CallbackStruct param;
3571 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3572 JSVM_Value thisVar = nullptr;
3573 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3574 return thisVar;
3575 };
3576 param.data = nullptr;
3577 propertyCfg.genericNamedPropertySetterCallback = SetNamedPropertyCbInfo2;
3578 JSVM_Value testWrapClass = nullptr;
3579 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test5", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3580 &propertyCfg, nullptr, &testWrapClass);
3581 if (res == JSVM_OK) {
3582 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3583 } else {
3584 return nullptr;
3585 }
3586 JSVM_Value instanceValue = nullptr;
3587 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3588
3589 const char testStr[] = "hello world2";
3590 JSVM_Value setvalueName = nullptr;
3591 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3592 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty2", setvalueName);
3593
3594 const char testStr2[] = "nameProperty2";
3595 JSVM_Value setvalueName2 = nullptr;
3596 OH_JSVM_CreateStringUtf8(env, testStr2, strlen(testStr2), &setvalueName2);
3597 bool isExisted = false;
3598 OH_JSVM_HasProperty(env, instanceValue, setvalueName2, &isExisted);
3599 propertyCfg.genericNamedPropertySetterCallback = nullptr;
3600 return nullptr;
3601 }
3602
testSetNamedProperty02(napi_env env1,napi_callback_info info)3603 static napi_value testSetNamedProperty02(napi_env env1, napi_callback_info info)
3604 {
3605 JSVM_InitOptions init_options;
3606 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3607 printf("memset_s failed");
3608 return nullptr;
3609 }
3610 init_options.externalReferences = externals;
3611 if (aa == 0) {
3612 OH_JSVM_Init(&init_options);
3613 aa++;
3614 }
3615 JSVM_VM vm;
3616 JSVM_CreateVMOptions options;
3617 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3618 printf("memset_s failed");
3619 return nullptr;
3620 }
3621 OH_JSVM_CreateVM(&options, &vm);
3622 JSVM_VMScope vm_scope;
3623 OH_JSVM_OpenVMScope(vm, &vm_scope);
3624 JSVM_Env env;
3625
3626 JSVM_CallbackStruct param[1];
3627 param[0].callback = nullptr;
3628 param[0].callback = NameHandler1;
3629
3630 JSVM_PropertyDescriptor descriptor[] = {
3631 {"NameHandler1", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3632 };
3633 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3634 JSVM_EnvScope envScope;
3635 OH_JSVM_OpenEnvScope(env, &envScope);
3636 JSVM_HandleScope handlescope;
3637 OH_JSVM_OpenHandleScope(env, &handlescope);
3638 OH_JSVM_CloseHandleScope(env, handlescope);
3639 OH_JSVM_CloseEnvScope(env, envScope);
3640 OH_JSVM_DestroyEnv(env);
3641 OH_JSVM_CloseVMScope(vm, vm_scope);
3642 OH_JSVM_DestroyVM(vm);
3643 napi_value result11;
3644 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3645 return result11;
3646 }
3647
3648 // ============================= test nullptr return for indexedProperty handle ===================================
3649 // 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)3650 static JSVM_Value SetIndexPropertyCbInfo1(JSVM_Env env, JSVM_Value index, JSVM_Value property, JSVM_Value thisArg,
3651 JSVM_Value data)
3652 {
3653 uint32_t value;
3654 OH_JSVM_GetValueUint32(env, index, &value);
3655 char str[100];
3656 size_t size;
3657 OH_JSVM_GetValueStringUtf8(env, property, str, strlen(str), &size);
3658 JSVM_Value gloablObj = nullptr;
3659 OH_JSVM_GetGlobal(env, &gloablObj);
3660 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
3661 return nullptr;
3662 }
3663
IndexHandler1(JSVM_Env env,JSVM_CallbackInfo info)3664 static JSVM_Value IndexHandler1(JSVM_Env env, JSVM_CallbackInfo info)
3665 {
3666 JSVM_CallbackStruct param;
3667 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3668 JSVM_Value thisVar = nullptr;
3669 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3670 return thisVar;
3671 };
3672 param.data = nullptr;
3673
3674 propertyCfg.genericIndexedPropertySetterCallback = SetIndexPropertyCbInfo1;
3675 JSVM_CallbackStruct callbackStruct;
3676 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3677 printf("call as a function called");
3678 return nullptr;
3679 };
3680 callbackStruct.data = nullptr;
3681 JSVM_Value testWrapClass = nullptr;
3682 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test6", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3683 &propertyCfg, &callbackStruct, &testWrapClass);
3684 if (res == JSVM_OK) {
3685 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3686 } else {
3687 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3688 return nullptr;
3689 }
3690 JSVM_Value instanceValue = nullptr;
3691 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3692
3693 JSVM_Value jsIndex = nullptr;
3694 uint32_t index = 0;
3695 OH_JSVM_CreateUint32(env, index, &jsIndex);
3696 const char testStr[] = "hello world1";
3697 JSVM_Value setvalueName = nullptr;
3698 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3699 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
3700 propertyCfg.genericIndexedPropertySetterCallback = nullptr;
3701 return setvalueName;
3702 }
3703
testSetIndexProperty01(napi_env env1,napi_callback_info info)3704 static napi_value testSetIndexProperty01(napi_env env1, napi_callback_info info)
3705 {
3706 JSVM_InitOptions init_options;
3707 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3708 printf("memset_s failed");
3709 return nullptr;
3710 }
3711 init_options.externalReferences = externals;
3712 if (aa == 0) {
3713 OH_JSVM_Init(&init_options);
3714 aa++;
3715 }
3716 JSVM_VM vm;
3717 JSVM_CreateVMOptions options;
3718 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3719 printf("memset_s failed");
3720 return nullptr;
3721 }
3722 OH_JSVM_CreateVM(&options, &vm);
3723 JSVM_VMScope vm_scope;
3724 OH_JSVM_OpenVMScope(vm, &vm_scope);
3725 JSVM_Env env;
3726
3727 JSVM_CallbackStruct param[1];
3728 param[0].callback = nullptr;
3729 param[0].callback = IndexHandler1;
3730
3731 JSVM_PropertyDescriptor descriptor[] = {
3732 {"IndexHandler1", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3733 };
3734 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3735 JSVM_EnvScope envScope;
3736 OH_JSVM_OpenEnvScope(env, &envScope);
3737 JSVM_HandleScope handlescope;
3738 OH_JSVM_OpenHandleScope(env, &handlescope);
3739 OH_JSVM_CloseHandleScope(env, handlescope);
3740 OH_JSVM_CloseEnvScope(env, envScope);
3741 OH_JSVM_DestroyEnv(env);
3742 OH_JSVM_CloseVMScope(vm, vm_scope);
3743 OH_JSVM_DestroyVM(vm);
3744 napi_value result11;
3745 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3746 return result11;
3747 }
3748
3749 // ============================= test non-nullptr return for indexedProperty handle ==================================
3750 // //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)3751 static JSVM_Value SetIndexPropertyCbInfo2(JSVM_Env env, JSVM_Value index, JSVM_Value property, JSVM_Value thisArg,
3752 JSVM_Value data)
3753 {
3754 return property;
3755 }
3756
IndexHandler2(JSVM_Env env,JSVM_CallbackInfo info)3757 static JSVM_Value IndexHandler2(JSVM_Env env, JSVM_CallbackInfo info)
3758 {
3759 JSVM_CallbackStruct param;
3760 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3761 JSVM_Value thisVar = nullptr;
3762 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3763 return thisVar;
3764 };
3765 param.data = nullptr;
3766 propertyCfg.genericIndexedPropertySetterCallback = SetIndexPropertyCbInfo2;
3767 JSVM_Value testWrapClass = nullptr;
3768 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test7", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3769 &propertyCfg, nullptr, &testWrapClass);
3770 if (res == JSVM_OK) {
3771 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3772 } else {
3773 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3774 return nullptr;
3775 }
3776 JSVM_Value instanceValue = nullptr;
3777 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3778
3779 JSVM_Value jsIndex = nullptr;
3780 uint32_t index = 0;
3781 OH_JSVM_CreateUint32(env, index, &jsIndex);
3782 const char testStr[] = "hello world1";
3783 JSVM_Value setvalueName = nullptr;
3784 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3785 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
3786 bool isExisted = false;
3787 OH_JSVM_HasProperty(env, instanceValue, jsIndex, &isExisted);
3788 propertyCfg.genericIndexedPropertySetterCallback = nullptr;
3789 return nullptr;
3790 }
3791
testSetIndexProperty02(napi_env env1,napi_callback_info info)3792 static napi_value testSetIndexProperty02(napi_env env1, napi_callback_info info)
3793 {
3794 JSVM_InitOptions init_options;
3795 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3796 printf("memset_s failed");
3797 return nullptr;
3798 }
3799 init_options.externalReferences = externals;
3800 if (aa == 0) {
3801 OH_JSVM_Init(&init_options);
3802 aa++;
3803 }
3804 JSVM_VM vm;
3805 JSVM_CreateVMOptions options;
3806 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3807 printf("memset_s failed");
3808 return nullptr;
3809 }
3810 OH_JSVM_CreateVM(&options, &vm);
3811 JSVM_VMScope vm_scope;
3812 OH_JSVM_OpenVMScope(vm, &vm_scope);
3813 JSVM_Env env;
3814
3815 JSVM_CallbackStruct param[1];
3816 param[0].callback = nullptr;
3817 param[0].callback = IndexHandler2;
3818
3819 JSVM_PropertyDescriptor descriptor[] = {
3820 {"IndexHandler2", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3821 };
3822 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3823 JSVM_EnvScope envScope;
3824 OH_JSVM_OpenEnvScope(env, &envScope);
3825 JSVM_HandleScope handlescope;
3826 OH_JSVM_OpenHandleScope(env, &handlescope);
3827 OH_JSVM_CloseHandleScope(env, handlescope);
3828 OH_JSVM_CloseEnvScope(env, envScope);
3829 OH_JSVM_DestroyEnv(env);
3830 OH_JSVM_CloseVMScope(vm, vm_scope);
3831 OH_JSVM_DestroyVM(vm);
3832 napi_value result11;
3833 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3834 return result11;
3835 }
3836
3837 // ============================= test nullptr return for namedProperty handle ===============
3838 // case 23~25
GetNamedPropertyCbInfo1(JSVM_Env env,JSVM_Value name,JSVM_Value thisArg,JSVM_Value data)3839 static JSVM_Value GetNamedPropertyCbInfo1(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
3840 {
3841 char strValue[100];
3842 size_t size;
3843 size_t bufferSize = 300;
3844 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
3845 JSVM_Value gloablObj = nullptr;
3846 OH_JSVM_GetGlobal(env, &gloablObj);
3847 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
3848 return nullptr;
3849 }
3850
NameHandler2(JSVM_Env env,JSVM_CallbackInfo info)3851 static JSVM_Value NameHandler2(JSVM_Env env, JSVM_CallbackInfo info)
3852 {
3853 JSVM_CallbackStruct param;
3854 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3855 JSVM_Value thisVar = nullptr;
3856 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3857 return thisVar;
3858 };
3859 param.data = nullptr;
3860 propertyCfg.genericNamedPropertyGetterCallback = GetNamedPropertyCbInfo1;
3861 JSVM_CallbackStruct callbackStruct;
3862 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3863 printf("call as a function called");
3864 return nullptr;
3865 };
3866 callbackStruct.data = nullptr;
3867 JSVM_Value testWrapClass = nullptr;
3868 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test8", NAPI_AUTO_LENGTH, ¶m, 0,
3869 nullptr, &propertyCfg, &callbackStruct, &testWrapClass);
3870 if (res == JSVM_OK) {
3871 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3872 } else {
3873 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3874 return nullptr;
3875 }
3876 JSVM_Value instanceValue = nullptr;
3877 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3878
3879 const char testStr[] = "hello world1";
3880 JSVM_Value setvalueName = nullptr;
3881 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3882 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty1", setvalueName);
3883 JSVM_Value valueName = nullptr;
3884 OH_JSVM_GetNamedProperty(env, instanceValue, "nameProperty1", &valueName);
3885 propertyCfg.genericNamedPropertyGetterCallback = nullptr;
3886 return valueName;
3887 }
3888
testGetNamedProperty01(napi_env env1,napi_callback_info info)3889 static napi_value testGetNamedProperty01(napi_env env1, napi_callback_info info)
3890 {
3891 JSVM_InitOptions init_options;
3892 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3893 printf("memset_s failed");
3894 return nullptr;
3895 }
3896 init_options.externalReferences = externals;
3897 if (aa == 0) {
3898 OH_JSVM_Init(&init_options);
3899 aa++;
3900 }
3901 JSVM_VM vm;
3902 JSVM_CreateVMOptions options;
3903 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
3904 printf("memset_s failed");
3905 return nullptr;
3906 }
3907 OH_JSVM_CreateVM(&options, &vm);
3908 JSVM_VMScope vm_scope;
3909 OH_JSVM_OpenVMScope(vm, &vm_scope);
3910 JSVM_Env env;
3911
3912 JSVM_CallbackStruct param[1];
3913 param[0].callback = nullptr;
3914 param[0].callback = NameHandler2;
3915
3916 JSVM_PropertyDescriptor descriptor[] = {
3917 {"NameHandler2", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
3918 };
3919 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
3920 JSVM_EnvScope envScope;
3921 OH_JSVM_OpenEnvScope(env, &envScope);
3922 JSVM_HandleScope handlescope;
3923 OH_JSVM_OpenHandleScope(env, &handlescope);
3924 OH_JSVM_CloseHandleScope(env, handlescope);
3925 OH_JSVM_CloseEnvScope(env, envScope);
3926 OH_JSVM_DestroyEnv(env);
3927 OH_JSVM_CloseVMScope(vm, vm_scope);
3928 OH_JSVM_DestroyVM(vm);
3929 napi_value result11;
3930 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
3931 return result11;
3932 }
3933
3934 // ========================= test non-nullptr return for namedProperty handle ============
3935 // case 26
GetNamedPropertyCbInfo2(JSVM_Env env,JSVM_Value name,JSVM_Value thisArg,JSVM_Value data)3936 static JSVM_Value GetNamedPropertyCbInfo2(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
3937 {
3938 JSVM_Value newResult = nullptr;
3939 if (g_temp) {
3940 char newStr[] = "hi from name handler";
3941 OH_JSVM_CreateStringUtf8(env, newStr, strlen(newStr), &newResult);
3942 }
3943 return newResult;
3944 }
3945
NameHandler3(JSVM_Env env,JSVM_CallbackInfo info)3946 static JSVM_Value NameHandler3(JSVM_Env env, JSVM_CallbackInfo info)
3947 {
3948 JSVM_CallbackStruct param;
3949 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
3950 JSVM_Value thisVar = nullptr;
3951 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
3952 return thisVar;
3953 };
3954 param.data = nullptr;
3955
3956 propertyCfg.genericNamedPropertyGetterCallback = GetNamedPropertyCbInfo2;
3957 JSVM_Value testWrapClass = nullptr;
3958 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test9", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
3959 &propertyCfg, nullptr, &testWrapClass);
3960 if (res == JSVM_OK) {
3961 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
3962 } else {
3963 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
3964 return nullptr;
3965 }
3966 JSVM_Value instanceValue = nullptr;
3967 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
3968
3969 const char testStr[] = "hello world1";
3970 JSVM_Value setvalueName = nullptr;
3971 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
3972 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty2", setvalueName);
3973
3974 g_temp = true;
3975 JSVM_Value valueName = nullptr;
3976 OH_JSVM_GetNamedProperty(env, instanceValue, "nameProperty2", &valueName);
3977 char str[100];
3978 size_t size;
3979 OH_JSVM_GetValueStringUtf8(env, valueName, str, strlen(str), &size);
3980 g_temp = false;
3981 OH_JSVM_GetNamedProperty(env, instanceValue, "nameProperty2", &valueName);
3982 char str2[100];
3983 size_t size2;
3984 OH_JSVM_GetValueStringUtf8(env, valueName, str2, strlen(str2), &size2);
3985 propertyCfg.genericNamedPropertyGetterCallback = nullptr;
3986 return valueName;
3987 }
3988
testGetNamedProperty02(napi_env env1,napi_callback_info info)3989 static napi_value testGetNamedProperty02(napi_env env1, napi_callback_info info)
3990 {
3991 JSVM_InitOptions init_options;
3992 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
3993 printf("memset_s failed");
3994 return nullptr;
3995 }
3996 init_options.externalReferences = externals;
3997 if (aa == 0) {
3998 OH_JSVM_Init(&init_options);
3999 aa++;
4000 }
4001 JSVM_VM vm;
4002 JSVM_CreateVMOptions options;
4003 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4004 printf("memset_s failed");
4005 return nullptr;
4006 }
4007 OH_JSVM_CreateVM(&options, &vm);
4008 JSVM_VMScope vm_scope;
4009 OH_JSVM_OpenVMScope(vm, &vm_scope);
4010 JSVM_Env env;
4011
4012 JSVM_CallbackStruct param[1];
4013 param[0].callback = nullptr;
4014 param[0].callback = NameHandler3;
4015
4016 JSVM_PropertyDescriptor descriptor[] = {
4017 {"NameHandler3", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4018 };
4019 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4020 JSVM_EnvScope envScope;
4021 OH_JSVM_OpenEnvScope(env, &envScope);
4022 JSVM_HandleScope handlescope;
4023 OH_JSVM_OpenHandleScope(env, &handlescope);
4024 OH_JSVM_CloseHandleScope(env, handlescope);
4025 OH_JSVM_CloseEnvScope(env, envScope);
4026 OH_JSVM_DestroyEnv(env);
4027 OH_JSVM_CloseVMScope(vm, vm_scope);
4028 OH_JSVM_DestroyVM(vm);
4029 napi_value result11;
4030 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4031 return result11;
4032 }
4033
4034 // ========================== test nullptr return for indexedProperty handle ================
4035 // case 27~29
GetIndexPropertyCbInfo1(JSVM_Env env,JSVM_Value index,JSVM_Value thisArg,JSVM_Value data)4036 static JSVM_Value GetIndexPropertyCbInfo1(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4037 {
4038 uint32_t value;
4039 OH_JSVM_GetValueUint32(env, index, &value);
4040 JSVM_Value gloablObj = nullptr;
4041 OH_JSVM_GetGlobal(env, &gloablObj);
4042 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance2", thisArg);
4043 return nullptr;
4044 }
4045
IndexHandler3(JSVM_Env env,JSVM_CallbackInfo info)4046 static JSVM_Value IndexHandler3(JSVM_Env env, JSVM_CallbackInfo info)
4047 {
4048 JSVM_CallbackStruct param;
4049 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4050 JSVM_Value thisVar = nullptr;
4051 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4052 return thisVar;
4053 };
4054 param.data = nullptr;
4055 propertyCfg.genericIndexedPropertyGetterCallback = GetIndexPropertyCbInfo1;
4056 JSVM_CallbackStruct callbackStruct;
4057 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4058 printf("call as a function called");
4059 return nullptr;
4060 };
4061 callbackStruct.data = nullptr;
4062 JSVM_Value testWrapClass = nullptr;
4063 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test10", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4064 &propertyCfg, &callbackStruct, &testWrapClass);
4065 if (res == JSVM_OK) {
4066 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4067 } else {
4068 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4069 return nullptr;
4070 }
4071 JSVM_Value instanceValue = nullptr;
4072 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4073
4074 JSVM_Value jsIndex = nullptr;
4075 uint32_t index = 0;
4076 OH_JSVM_CreateUint32(env, index, &jsIndex);
4077 const char testStr[] = "hello world1";
4078 JSVM_Value setvalueName = nullptr;
4079 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4080 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4081 JSVM_Value valueName = nullptr;
4082 OH_JSVM_GetProperty(env, instanceValue, jsIndex, &valueName);
4083 propertyCfg.genericIndexedPropertyGetterCallback = nullptr;
4084 return valueName;
4085 }
4086
testGetIndexProperty01(napi_env env1,napi_callback_info info)4087 static napi_value testGetIndexProperty01(napi_env env1, napi_callback_info info)
4088 {
4089 JSVM_InitOptions init_options;
4090 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4091 printf("memset_s failed");
4092 return nullptr;
4093 }
4094 init_options.externalReferences = externals;
4095 if (aa == 0) {
4096 OH_JSVM_Init(&init_options);
4097 aa++;
4098 }
4099 JSVM_VM vm;
4100 JSVM_CreateVMOptions options;
4101 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4102 printf("memset_s failed");
4103 return nullptr;
4104 }
4105 OH_JSVM_CreateVM(&options, &vm);
4106 JSVM_VMScope vm_scope;
4107 OH_JSVM_OpenVMScope(vm, &vm_scope);
4108 JSVM_Env env;
4109 JSVM_CallbackStruct param[1];
4110 param[0].callback = nullptr;
4111 param[0].callback = IndexHandler3;
4112
4113 JSVM_PropertyDescriptor descriptor[] = {
4114 {"IndexHandler3", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4115 };
4116 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4117 JSVM_EnvScope envScope;
4118 OH_JSVM_OpenEnvScope(env, &envScope);
4119 JSVM_HandleScope handlescope;
4120 OH_JSVM_OpenHandleScope(env, &handlescope);
4121 OH_JSVM_CloseHandleScope(env, handlescope);
4122 OH_JSVM_CloseEnvScope(env, envScope);
4123 OH_JSVM_DestroyEnv(env);
4124 OH_JSVM_CloseVMScope(vm, vm_scope);
4125 OH_JSVM_DestroyVM(vm);
4126 napi_value result11;
4127 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4128 return result11;
4129 }
4130
4131 // =====================test non-nullptr return for indexedProperty handle =========
4132 // case 30
GetIndexPropertyCbInfo2(JSVM_Env env,JSVM_Value index,JSVM_Value thisArg,JSVM_Value data)4133 static JSVM_Value GetIndexPropertyCbInfo2(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4134 {
4135 JSVM_Value newResult = nullptr;
4136 if (g_temp) {
4137 char newStr[] = "hi from index handler";
4138 OH_JSVM_CreateStringUtf8(env, newStr, strlen(newStr), &newResult);
4139 }
4140 return newResult;
4141 }
4142
IndexHandler4(JSVM_Env env,JSVM_CallbackInfo info)4143 static JSVM_Value IndexHandler4(JSVM_Env env, JSVM_CallbackInfo info)
4144 {
4145 JSVM_CallbackStruct param;
4146 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4147 JSVM_Value thisVar = nullptr;
4148 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4149 return thisVar;
4150 };
4151 param.data = nullptr;
4152 propertyCfg.genericIndexedPropertyGetterCallback = GetIndexPropertyCbInfo2;
4153 JSVM_Value testWrapClass = nullptr;
4154 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test11", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4155 &propertyCfg, nullptr, &testWrapClass);
4156 if (res == JSVM_OK) {
4157 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4158 } else {
4159 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4160 return nullptr;
4161 }
4162 JSVM_Value instanceValue = nullptr;
4163 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4164
4165 JSVM_Value jsIndex = nullptr;
4166 uint32_t index = 0;
4167 OH_JSVM_CreateUint32(env, index, &jsIndex);
4168 const char testStr[] = "hello world1";
4169 JSVM_Value setvalueName = nullptr;
4170 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4171 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4172 g_temp = true;
4173 JSVM_Value valueName = nullptr;
4174 OH_JSVM_GetProperty(env, instanceValue, jsIndex, &valueName);
4175 char str[100];
4176 size_t size;
4177 OH_JSVM_GetValueStringUtf8(env, valueName, str, strlen(str), &size);
4178 g_temp = false;
4179 OH_JSVM_GetProperty(env, instanceValue, jsIndex, &valueName);
4180 char str2[100];
4181 size_t size2;
4182 OH_JSVM_GetValueStringUtf8(env, valueName, str2, strlen(str2), &size2);
4183 propertyCfg.genericIndexedPropertyGetterCallback = nullptr;
4184 return valueName;
4185 }
4186
testGetIndexProperty02(napi_env env1,napi_callback_info info)4187 static napi_value testGetIndexProperty02(napi_env env1, napi_callback_info info)
4188 {
4189 JSVM_InitOptions init_options;
4190 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4191 printf("memset_s failed");
4192 return nullptr;
4193 }
4194 init_options.externalReferences = externals;
4195 if (aa == 0) {
4196 OH_JSVM_Init(&init_options);
4197 aa++;
4198 }
4199 JSVM_VM vm;
4200 JSVM_CreateVMOptions options;
4201 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4202 printf("memset_s failed");
4203 return nullptr;
4204 }
4205 OH_JSVM_CreateVM(&options, &vm);
4206 JSVM_VMScope vm_scope;
4207 OH_JSVM_OpenVMScope(vm, &vm_scope);
4208 JSVM_Env env;
4209
4210 JSVM_CallbackStruct param[1];
4211 param[0].callback = nullptr;
4212 param[0].callback = IndexHandler4;
4213
4214 JSVM_PropertyDescriptor descriptor[] = {
4215 {"IndexHandler4", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4216 };
4217 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4218 JSVM_EnvScope envScope;
4219 OH_JSVM_OpenEnvScope(env, &envScope);
4220 JSVM_HandleScope handlescope;
4221 OH_JSVM_OpenHandleScope(env, &handlescope);
4222 OH_JSVM_CloseHandleScope(env, handlescope);
4223 OH_JSVM_CloseEnvScope(env, envScope);
4224 OH_JSVM_DestroyEnv(env);
4225 OH_JSVM_CloseVMScope(vm, vm_scope);
4226 OH_JSVM_DestroyVM(vm);
4227 napi_value result11;
4228 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4229 return result11;
4230 }
4231
4232 // ======================== test nullptr return for namedProperty handle ==================
4233 // cases 31-33 when returning nullptr, delete listening trigger, only listen without intercepting.
4234 // 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)4235 static JSVM_Value DeleterNamedPropertyCbInfo1(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
4236 {
4237 char strValue[100];
4238 size_t size;
4239 size_t bufferSize = 300;
4240 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
4241 JSVM_Value gloablObj = nullptr;
4242 OH_JSVM_GetGlobal(env, &gloablObj);
4243 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
4244 return nullptr;
4245 }
4246
NameHandler4(JSVM_Env env,JSVM_CallbackInfo info)4247 static JSVM_Value NameHandler4(JSVM_Env env, JSVM_CallbackInfo info)
4248 {
4249 JSVM_CallbackStruct param;
4250 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4251 JSVM_Value thisVar = nullptr;
4252 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4253 return thisVar;
4254 };
4255 param.data = nullptr;
4256 propertyCfg.genericNamedPropertyDeleterCallback = DeleterNamedPropertyCbInfo1;
4257 JSVM_CallbackStruct callbackStruct;
4258 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4259 printf("call as a function called");
4260 return nullptr;
4261 };
4262 callbackStruct.data = nullptr;
4263 JSVM_Value testWrapClass = nullptr;
4264 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test13", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4265 &propertyCfg, &callbackStruct, &testWrapClass);
4266 if (res == JSVM_OK) {
4267 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4268 } else {
4269 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4270 return nullptr;
4271 }
4272 JSVM_Value instanceValue = nullptr;
4273 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4274 const char testStr[] = "hello world1";
4275 JSVM_Value setvalueName = nullptr;
4276 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4277 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty1", setvalueName);
4278 bool result = false;
4279 JSVM_Value propertyName = nullptr;
4280 char propertyChar[] = "nameProperty1";
4281 OH_JSVM_CreateStringUtf8(env, propertyChar, strlen(propertyChar), &propertyName);
4282 OH_JSVM_DeleteProperty(env, instanceValue, propertyName, &result);
4283 bool isExisted = false;
4284 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
4285 propertyCfg.genericNamedPropertyDeleterCallback = nullptr;
4286 return nullptr;
4287 }
4288
testDeleterNamedProperty01(napi_env env1,napi_callback_info info)4289 static napi_value testDeleterNamedProperty01(napi_env env1, napi_callback_info info)
4290 {
4291 JSVM_InitOptions init_options;
4292 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4293 printf("memset_s failed");
4294 return nullptr;
4295 }
4296 init_options.externalReferences = externals;
4297 if (aa == 0) {
4298 OH_JSVM_Init(&init_options);
4299 aa++;
4300 }
4301 JSVM_VM vm;
4302 JSVM_CreateVMOptions options;
4303 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4304 printf("memset_s failed");
4305 return nullptr;
4306 }
4307 OH_JSVM_CreateVM(&options, &vm);
4308 JSVM_VMScope vm_scope;
4309 OH_JSVM_OpenVMScope(vm, &vm_scope);
4310 JSVM_Env env;
4311
4312 JSVM_CallbackStruct param[1];
4313 param[0].callback = nullptr;
4314 param[0].callback = NameHandler4;
4315 JSVM_PropertyDescriptor descriptor[] = {
4316 {"NameHandler4", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4317 };
4318 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4319 JSVM_EnvScope envScope;
4320 OH_JSVM_OpenEnvScope(env, &envScope);
4321 JSVM_HandleScope handlescope;
4322 OH_JSVM_OpenHandleScope(env, &handlescope);
4323 OH_JSVM_CloseHandleScope(env, handlescope);
4324 OH_JSVM_CloseEnvScope(env, envScope);
4325 OH_JSVM_DestroyEnv(env);
4326 OH_JSVM_CloseVMScope(vm, vm_scope);
4327 OH_JSVM_DestroyVM(vm);
4328 napi_value result11;
4329 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4330 return result11;
4331 }
4332
4333 // ===================== test return true for namedProperty handle =======================
4334 // case 34 when returning true, the deletion of the listening trigger is triggered, and the interception
4335 // 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)4336 static JSVM_Value DeleterNamedPropertyCbInfo2(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
4337 {
4338 char strValue[100];
4339 size_t size;
4340 size_t bufferSize = 300;
4341 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
4342 JSVM_Value newResult = nullptr;
4343 bool returnValue = true;
4344 OH_JSVM_GetBoolean(env, returnValue, &newResult);
4345 return newResult;
4346 }
4347
NameHandler5(JSVM_Env env,JSVM_CallbackInfo info)4348 static JSVM_Value NameHandler5(JSVM_Env env, JSVM_CallbackInfo info)
4349 {
4350 JSVM_CallbackStruct param;
4351 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4352 JSVM_Value thisVar = nullptr;
4353 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4354 return thisVar;
4355 };
4356 param.data = nullptr;
4357
4358 propertyCfg.genericIndexedPropertyEnumeratorCallback = nullptr;
4359 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4360 propertyCfg.genericIndexedPropertySetterCallback = nullptr;
4361 propertyCfg.genericIndexedPropertyGetterCallback = nullptr;
4362 propertyCfg.genericNamedPropertyEnumeratorCallback = nullptr;
4363 propertyCfg.genericNamedPropertyDeleterCallback = DeleterNamedPropertyCbInfo2;
4364 propertyCfg.genericNamedPropertySetterCallback = nullptr;
4365 propertyCfg.genericNamedPropertyGetterCallback = nullptr;
4366 JSVM_Value testWrapClass = nullptr;
4367 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test14", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4368 &propertyCfg, nullptr, &testWrapClass);
4369 if (res == JSVM_OK) {
4370 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4371 } else {
4372 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4373 return nullptr;
4374 }
4375 JSVM_Value instanceValue = nullptr;
4376 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4377
4378 const char testStr[] = "hello world2";
4379 JSVM_Value setvalueName = nullptr;
4380 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4381 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty2", setvalueName);
4382
4383 bool result = false;
4384 JSVM_Value propertyName = nullptr;
4385 char propertyChar[] = "nameProperty2";
4386 OH_JSVM_CreateStringUtf8(env, propertyChar, strlen(propertyChar), &propertyName);
4387 OH_JSVM_DeleteProperty(env, instanceValue, propertyName, &result);
4388 bool isExisted = false;
4389 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
4390 return nullptr;
4391 }
4392
testDeleterNamedProperty02(napi_env env1,napi_callback_info info)4393 static napi_value testDeleterNamedProperty02(napi_env env1, napi_callback_info info)
4394 {
4395 JSVM_InitOptions init_options;
4396 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4397 printf("memset_s failed");
4398 return nullptr;
4399 }
4400 init_options.externalReferences = externals;
4401 if (aa == 0) {
4402 OH_JSVM_Init(&init_options);
4403 aa++;
4404 }
4405 JSVM_VM vm;
4406 JSVM_CreateVMOptions options;
4407 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4408 printf("memset_s failed");
4409 return nullptr;
4410 }
4411 OH_JSVM_CreateVM(&options, &vm);
4412 JSVM_VMScope vm_scope;
4413 OH_JSVM_OpenVMScope(vm, &vm_scope);
4414 JSVM_Env env;
4415 JSVM_CallbackStruct param[1];
4416 param[0].callback = nullptr;
4417 param[0].callback = NameHandler5;
4418 JSVM_PropertyDescriptor descriptor[] = {
4419 {"NameHandler5", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4420 };
4421 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4422 JSVM_EnvScope envScope;
4423 OH_JSVM_OpenEnvScope(env, &envScope);
4424 JSVM_HandleScope handlescope;
4425 OH_JSVM_OpenHandleScope(env, &handlescope);
4426 OH_JSVM_CloseHandleScope(env, handlescope);
4427 OH_JSVM_CloseEnvScope(env, envScope);
4428 OH_JSVM_DestroyEnv(env);
4429 OH_JSVM_CloseVMScope(vm, vm_scope);
4430 OH_JSVM_DestroyVM(vm);
4431 napi_value result11;
4432 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4433 return result11;
4434 }
4435
4436 // ======================= test return false for namedProperty handle ========================
4437 // case 35 when false is returned, the deletion of the listening trigger is triggered, and the
4438 // 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)4439 static JSVM_Value DeleterNamedPropertyCbInfo3(JSVM_Env env, JSVM_Value name, JSVM_Value thisArg, JSVM_Value data)
4440 {
4441 char strValue[100];
4442 size_t size;
4443 size_t bufferSize = 300;
4444 OH_JSVM_GetValueStringUtf8(env, name, strValue, bufferSize, &size);
4445 JSVM_Value newResult = nullptr;
4446 bool returnValue = false;
4447 OH_JSVM_GetBoolean(env, returnValue, &newResult);
4448 return newResult;
4449 }
4450
NameHandler6(JSVM_Env env,JSVM_CallbackInfo info)4451 static JSVM_Value NameHandler6(JSVM_Env env, JSVM_CallbackInfo info)
4452 {
4453 JSVM_CallbackStruct param;
4454 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4455 JSVM_Value thisVar = nullptr;
4456 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4457 return thisVar;
4458 };
4459 param.data = nullptr;
4460
4461 propertyCfg.genericIndexedPropertyEnumeratorCallback = nullptr;
4462 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4463 propertyCfg.genericIndexedPropertySetterCallback = nullptr;
4464 propertyCfg.genericIndexedPropertyGetterCallback = nullptr;
4465 propertyCfg.genericNamedPropertyEnumeratorCallback = nullptr;
4466 propertyCfg.genericNamedPropertyDeleterCallback = DeleterNamedPropertyCbInfo3;
4467 propertyCfg.genericNamedPropertySetterCallback = nullptr;
4468 propertyCfg.genericNamedPropertyGetterCallback = nullptr;
4469 JSVM_Value testWrapClass = nullptr;
4470 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test15", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4471 &propertyCfg, nullptr, &testWrapClass);
4472 if (res == JSVM_OK) {
4473 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4474 } else {
4475 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4476 return nullptr;
4477 }
4478 JSVM_Value instanceValue = nullptr;
4479 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4480 const char testStr[] = "hello world3";
4481 JSVM_Value setvalueName = nullptr;
4482 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4483 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty3", setvalueName);
4484 bool result = false;
4485 JSVM_Value propertyName = nullptr;
4486 char propertyChar[] = "nameProperty3";
4487 OH_JSVM_CreateStringUtf8(env, propertyChar, strlen(propertyChar), &propertyName);
4488 OH_JSVM_DeleteProperty(env, instanceValue, propertyName, &result);
4489 bool isExisted = false;
4490 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
4491 return nullptr;
4492 }
4493
testDeleterNamedProperty03(napi_env env1,napi_callback_info info)4494 static napi_value testDeleterNamedProperty03(napi_env env1, napi_callback_info info)
4495 {
4496 JSVM_InitOptions init_options;
4497 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4498 printf("memset_s failed");
4499 return nullptr;
4500 }
4501 init_options.externalReferences = externals;
4502 if (aa == 0) {
4503 OH_JSVM_Init(&init_options);
4504 aa++;
4505 }
4506 JSVM_VM vm;
4507 JSVM_CreateVMOptions options;
4508 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4509 printf("memset_s failed");
4510 return nullptr;
4511 }
4512 OH_JSVM_CreateVM(&options, &vm);
4513 JSVM_VMScope vm_scope;
4514 OH_JSVM_OpenVMScope(vm, &vm_scope);
4515 JSVM_Env env;
4516 JSVM_CallbackStruct param[1];
4517 param[0].callback = nullptr;
4518 param[0].callback = NameHandler6;
4519 JSVM_PropertyDescriptor descriptor[] = {
4520 {"NameHandler6", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4521 };
4522 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4523 JSVM_EnvScope envScope;
4524 OH_JSVM_OpenEnvScope(env, &envScope);
4525 JSVM_HandleScope handlescope;
4526 OH_JSVM_OpenHandleScope(env, &handlescope);
4527 OH_JSVM_CloseHandleScope(env, handlescope);
4528 OH_JSVM_CloseEnvScope(env, envScope);
4529 OH_JSVM_DestroyEnv(env);
4530 OH_JSVM_CloseVMScope(vm, vm_scope);
4531 OH_JSVM_DestroyVM(vm);
4532 napi_value result11;
4533 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4534 return result11;
4535 }
4536
4537 // ==================test nullptr return for indexedProperty handle =====================
4538 // cases 36-38, when returning nullptr, delete listening trigger, only listen without intercepting.
4539 // 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)4540 static JSVM_Value DeleterIndexedPropertyCbInfo1(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4541 {
4542 uint32_t value;
4543 OH_JSVM_GetValueUint32(env, index, &value);
4544 JSVM_Value gloablObj = nullptr;
4545 OH_JSVM_GetGlobal(env, &gloablObj);
4546 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
4547 return nullptr;
4548 }
4549
IndexHandler5(JSVM_Env env,JSVM_CallbackInfo info)4550 static JSVM_Value IndexHandler5(JSVM_Env env, JSVM_CallbackInfo info)
4551 {
4552 JSVM_CallbackStruct param;
4553 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4554 JSVM_Value thisVar = nullptr;
4555 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4556 return thisVar;
4557 };
4558 param.data = nullptr;
4559 propertyCfg.genericIndexedPropertyDeleterCallback = DeleterIndexedPropertyCbInfo1;
4560 JSVM_CallbackStruct callbackStruct;
4561 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4562 printf("call as a function called");
4563 return nullptr;
4564 };
4565 callbackStruct.data = nullptr;
4566 JSVM_Value testWrapClass = nullptr;
4567 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test16", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4568 &propertyCfg, &callbackStruct, &testWrapClass);
4569 if (res == JSVM_OK) {
4570 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4571 } else {
4572 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4573 return nullptr;
4574 }
4575 JSVM_Value instanceValue = nullptr;
4576 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4577
4578 const char testStr[] = "hello world1";
4579 JSVM_Value setvalueName = nullptr;
4580 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4581 JSVM_Value jsIndex = nullptr;
4582 uint32_t index = 0;
4583 OH_JSVM_CreateUint32(env, index, &jsIndex);
4584 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4585 bool result = false;
4586 OH_JSVM_DeleteProperty(env, instanceValue, jsIndex, &result);
4587 bool isExisted = false;
4588 OH_JSVM_HasProperty(env, instanceValue, jsIndex, &isExisted);
4589 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4590 return nullptr;
4591 }
4592
testDeleterIndexedProperty01(napi_env env1,napi_callback_info info)4593 static napi_value testDeleterIndexedProperty01(napi_env env1, napi_callback_info info)
4594 {
4595 JSVM_InitOptions init_options;
4596 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4597 printf("memset_s failed");
4598 return nullptr;
4599 }
4600 init_options.externalReferences = externals;
4601 if (aa == 0) {
4602 OH_JSVM_Init(&init_options);
4603 aa++;
4604 }
4605 JSVM_VM vm;
4606 JSVM_CreateVMOptions options;
4607 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4608 printf("memset_s failed");
4609 return nullptr;
4610 }
4611 OH_JSVM_CreateVM(&options, &vm);
4612 JSVM_VMScope vm_scope;
4613 OH_JSVM_OpenVMScope(vm, &vm_scope);
4614 JSVM_Env env;
4615 JSVM_CallbackStruct param[1];
4616 param[0].callback = nullptr;
4617 param[0].callback = IndexHandler5;
4618 JSVM_PropertyDescriptor descriptor[] = {
4619 {"IndexHandler5", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4620 };
4621 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4622 JSVM_EnvScope envScope;
4623 OH_JSVM_OpenEnvScope(env, &envScope);
4624 JSVM_HandleScope handlescope;
4625 OH_JSVM_OpenHandleScope(env, &handlescope);
4626 OH_JSVM_CloseHandleScope(env, handlescope);
4627 OH_JSVM_CloseEnvScope(env, envScope);
4628 OH_JSVM_DestroyEnv(env);
4629 OH_JSVM_CloseVMScope(vm, vm_scope);
4630 OH_JSVM_DestroyVM(vm);
4631 napi_value result11;
4632 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4633 return result11;
4634 }
4635
4636 // =================== test return true for indexedProperty handle ========================
4637 // case 39 When returning true, the deletion of the listening trigger is triggered, and the interception
4638 // 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)4639 static JSVM_Value DeleterIndexedPropertyCbInfo2(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4640 {
4641 uint32_t value;
4642 OH_JSVM_GetValueUint32(env, index, &value);
4643 JSVM_Value newResult = nullptr;
4644 bool returnValue = true;
4645 OH_JSVM_GetBoolean(env, returnValue, &newResult);
4646 return newResult;
4647 }
4648
IndexHandler6(JSVM_Env env,JSVM_CallbackInfo info)4649 static JSVM_Value IndexHandler6(JSVM_Env env, JSVM_CallbackInfo info)
4650 {
4651 JSVM_CallbackStruct param;
4652 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4653 JSVM_Value thisVar = nullptr;
4654 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4655 return thisVar;
4656 };
4657 param.data = nullptr;
4658 propertyCfg.genericIndexedPropertyDeleterCallback = DeleterIndexedPropertyCbInfo2;
4659 JSVM_Value testWrapClass = nullptr;
4660 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test17", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4661 &propertyCfg, nullptr, &testWrapClass);
4662 if (res == JSVM_OK) {
4663 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4664 } else {
4665 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4666 return nullptr;
4667 }
4668 JSVM_Value instanceValue = nullptr;
4669 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4670
4671 JSVM_Value jsIndex = nullptr;
4672 uint32_t index = 0;
4673 OH_JSVM_CreateUint32(env, index, &jsIndex);
4674 const char testStr[] = "hello world2";
4675 JSVM_Value setvalueName = nullptr;
4676 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4677 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4678 bool result = false;
4679 OH_JSVM_DeleteProperty(env, instanceValue, jsIndex, &result);
4680 bool isExisted = false;
4681 OH_JSVM_HasProperty(env, instanceValue, jsIndex, &isExisted);
4682 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4683 return nullptr;
4684 }
4685
testDeleterIndexedProperty02(napi_env env1,napi_callback_info info)4686 static napi_value testDeleterIndexedProperty02(napi_env env1, napi_callback_info info)
4687 {
4688 JSVM_InitOptions init_options;
4689 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4690 printf("memset_s failed");
4691 return nullptr;
4692 }
4693 init_options.externalReferences = externals;
4694 if (aa == 0) {
4695 OH_JSVM_Init(&init_options);
4696 aa++;
4697 }
4698 JSVM_VM vm;
4699 JSVM_CreateVMOptions options;
4700 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4701 printf("memset_s failed");
4702 return nullptr;
4703 }
4704 OH_JSVM_CreateVM(&options, &vm);
4705 JSVM_VMScope vm_scope;
4706 OH_JSVM_OpenVMScope(vm, &vm_scope);
4707 JSVM_Env env;
4708 JSVM_CallbackStruct param[1];
4709 param[0].callback = nullptr;
4710 param[0].callback = IndexHandler6;
4711 JSVM_PropertyDescriptor descriptor[] = {
4712 {"IndexHandler6", NULL, ¶m[1], NULL, NULL, NULL, JSVM_DEFAULT},
4713 };
4714 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4715 JSVM_EnvScope envScope;
4716 OH_JSVM_OpenEnvScope(env, &envScope);
4717 JSVM_HandleScope handlescope;
4718 OH_JSVM_OpenHandleScope(env, &handlescope);
4719 OH_JSVM_CloseHandleScope(env, handlescope);
4720 OH_JSVM_CloseEnvScope(env, envScope);
4721 OH_JSVM_DestroyEnv(env);
4722 OH_JSVM_CloseVMScope(vm, vm_scope);
4723 OH_JSVM_DestroyVM(vm);
4724 napi_value result11;
4725 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4726 return result11;
4727 }
4728
4729 // ============================= test return false for indexedProperty handle =======================
4730 // case 40 When returning false, the deletion of the listening trigger is triggered, and the interception
4731 // 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)4732 static JSVM_Value DeleterIndexedPropertyCbInfo3(JSVM_Env env, JSVM_Value index, JSVM_Value thisArg, JSVM_Value data)
4733 {
4734 uint32_t value;
4735 OH_JSVM_GetValueUint32(env, index, &value);
4736 JSVM_Value newResult = nullptr;
4737 bool returnValue = false;
4738 OH_JSVM_GetBoolean(env, returnValue, &newResult);
4739 return newResult;
4740 }
4741
IndexHandler7(JSVM_Env env,JSVM_CallbackInfo info)4742 static JSVM_Value IndexHandler7(JSVM_Env env, JSVM_CallbackInfo info)
4743 {
4744 JSVM_CallbackStruct param;
4745 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4746 JSVM_Value thisVar = nullptr;
4747 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4748 return thisVar;
4749 };
4750 param.data = nullptr;
4751 propertyCfg.genericIndexedPropertyDeleterCallback = DeleterIndexedPropertyCbInfo3;
4752 JSVM_Value testWrapClass = nullptr;
4753 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test18", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4754 &propertyCfg, nullptr, &testWrapClass);
4755 if (res == JSVM_OK) {
4756 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4757 } else {
4758 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4759 return nullptr;
4760 }
4761 JSVM_Value instanceValue = nullptr;
4762 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4763
4764 JSVM_Value jsIndex = nullptr;
4765 uint32_t index = 0;
4766 OH_JSVM_CreateUint32(env, index, &jsIndex);
4767 const char testStr[] = "hello world2";
4768 JSVM_Value setvalueName = nullptr;
4769 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4770 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
4771 bool result = false;
4772 OH_JSVM_DeleteProperty(env, instanceValue, jsIndex, &result);
4773 bool isExisted = false;
4774 OH_JSVM_HasProperty(env, instanceValue, jsIndex, &isExisted);
4775 propertyCfg.genericIndexedPropertyDeleterCallback = nullptr;
4776 return nullptr;
4777 }
4778
testDeleterIndexedProperty03(napi_env env1,napi_callback_info info)4779 static napi_value testDeleterIndexedProperty03(napi_env env1, napi_callback_info info)
4780 {
4781 JSVM_InitOptions init_options;
4782 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4783 printf("memset_s failed");
4784 return nullptr;
4785 }
4786 init_options.externalReferences = externals;
4787 if (aa == 0) {
4788 OH_JSVM_Init(&init_options);
4789 aa++;
4790 }
4791 JSVM_VM vm;
4792 JSVM_CreateVMOptions options;
4793 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4794 printf("memset_s failed");
4795 return nullptr;
4796 }
4797 OH_JSVM_CreateVM(&options, &vm);
4798 JSVM_VMScope vm_scope;
4799 OH_JSVM_OpenVMScope(vm, &vm_scope);
4800 JSVM_Env env;
4801 JSVM_CallbackStruct param[1];
4802 param[0].callback = nullptr;
4803 param[0].callback = IndexHandler7;
4804 JSVM_PropertyDescriptor descriptor[] = {
4805 {"IndexHandler7", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4806 };
4807 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4808 JSVM_EnvScope envScope;
4809 OH_JSVM_OpenEnvScope(env, &envScope);
4810 JSVM_HandleScope handlescope;
4811 OH_JSVM_OpenHandleScope(env, &handlescope);
4812 OH_JSVM_CloseHandleScope(env, handlescope);
4813 OH_JSVM_CloseEnvScope(env, envScope);
4814 OH_JSVM_DestroyEnv(env);
4815 OH_JSVM_CloseVMScope(vm, vm_scope);
4816 OH_JSVM_DestroyVM(vm);
4817 napi_value result11;
4818 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4819 return result11;
4820 }
4821
4822 // =================test nullptr return for namedProperty handle =======
4823 // case 41 and case 42
4824 // When returning nullptr, the enumerator listens and triggers, only listens but not
4825 // intercepts.OH_JSVM_GetAllPropertyNames can retrieve the property names that have already been set.
EnumeratorNamedPropertyCbInfo1(JSVM_Env env,JSVM_Value thisArg,JSVM_Value data)4826 static JSVM_Value EnumeratorNamedPropertyCbInfo1(JSVM_Env env, JSVM_Value thisArg, JSVM_Value data)
4827 {
4828 JSVM_Value gloablObj = nullptr;
4829 OH_JSVM_GetGlobal(env, &gloablObj);
4830 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
4831 return nullptr;
4832 }
4833
NameHandler7(JSVM_Env env,JSVM_CallbackInfo info)4834 static JSVM_Value NameHandler7(JSVM_Env env, JSVM_CallbackInfo info)
4835 {
4836 JSVM_CallbackStruct param;
4837 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4838 JSVM_Value thisVar = nullptr;
4839 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4840 return thisVar;
4841 };
4842 param.data = nullptr;
4843 propertyCfg.genericNamedPropertyEnumeratorCallback = EnumeratorNamedPropertyCbInfo1;
4844 JSVM_CallbackStruct callbackStruct;
4845 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4846 printf("call as a function called");
4847 return nullptr;
4848 };
4849 callbackStruct.data = nullptr;
4850 JSVM_Value testWrapClass = nullptr;
4851 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test19", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4852 &propertyCfg, &callbackStruct, &testWrapClass);
4853 if (res == JSVM_OK) {
4854 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4855 } else {
4856 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4857 return nullptr;
4858 }
4859
4860 JSVM_Value instanceValue = nullptr;
4861 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4862
4863 const char testStr[] = "hello world1";
4864 JSVM_Value setvalueName = nullptr;
4865 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4866 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty1", setvalueName);
4867 OH_JSVM_SetNamedProperty(env, instanceValue, "nameProperty2", setvalueName);
4868
4869 JSVM_Value allPropertyNames = nullptr;
4870 OH_JSVM_GetAllPropertyNames(
4871 env,
4872 instanceValue,
4873 JSVM_KEY_OWN_ONLY,
4874 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_SKIP_SYMBOLS),
4875 JSVM_KEY_NUMBERS_TO_STRINGS,
4876 &allPropertyNames);
4877 uint32_t nameSize = 0;
4878 OH_JSVM_GetArrayLength(env, allPropertyNames, &nameSize);
4879 JSVM_Value propertyName = nullptr;
4880 for (uint32_t i = 0; i < nameSize; ++i) {
4881 OH_JSVM_GetElement(env, allPropertyNames, i, &propertyName);
4882 char str[100];
4883 size_t size;
4884 OH_JSVM_GetValueStringUtf8(env, propertyName, str, strlen(str), &size);
4885 bool isExisted = false;
4886 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
4887 }
4888 propertyCfg.genericNamedPropertyEnumeratorCallback = nullptr;
4889 return nullptr;
4890 }
4891
testEnumeratorNamedProperty01(napi_env env1,napi_callback_info info)4892 static napi_value testEnumeratorNamedProperty01(napi_env env1, napi_callback_info info)
4893 {
4894 JSVM_InitOptions init_options;
4895 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
4896 printf("memset_s failed");
4897 return nullptr;
4898 }
4899 init_options.externalReferences = externals;
4900 if (aa == 0) {
4901 OH_JSVM_Init(&init_options);
4902 aa++;
4903 }
4904 JSVM_VM vm;
4905 JSVM_CreateVMOptions options;
4906 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
4907 printf("memset_s failed");
4908 return nullptr;
4909 }
4910 OH_JSVM_CreateVM(&options, &vm);
4911 JSVM_VMScope vm_scope;
4912 OH_JSVM_OpenVMScope(vm, &vm_scope);
4913 JSVM_Env env;
4914 JSVM_CallbackStruct param[1];
4915 param[0].callback = nullptr;
4916 param[0].callback = NameHandler7;
4917 JSVM_PropertyDescriptor descriptor[] = {
4918 {"NameHandler7", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
4919 };
4920 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
4921 JSVM_EnvScope envScope;
4922 OH_JSVM_OpenEnvScope(env, &envScope);
4923 JSVM_HandleScope handlescope;
4924 OH_JSVM_OpenHandleScope(env, &handlescope);
4925 OH_JSVM_CloseHandleScope(env, handlescope);
4926 OH_JSVM_CloseEnvScope(env, envScope);
4927 OH_JSVM_DestroyEnv(env);
4928 OH_JSVM_CloseVMScope(vm, vm_scope);
4929 OH_JSVM_DestroyVM(vm);
4930 napi_value result11;
4931 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
4932 return result11;
4933 }
4934
4935 // ================== test return true for namedProperty handle =========
4936 // case 43
4937 // When returning nullptr, the enumerator listens and triggers, only listens but not
4938 // intercepts. OH_JSVM_GetAllPropertyNames can retrieve the property names that have already been set.
EnumeratorNamedPropertyCbInfo2(JSVM_Env env,JSVM_Value thisArg,JSVM_Value data)4939 static JSVM_Value EnumeratorNamedPropertyCbInfo2(JSVM_Env env, JSVM_Value thisArg, JSVM_Value data)
4940 {
4941 uint32_t arrayLength = 2;
4942 JSVM_Value testArray = nullptr;
4943 OH_JSVM_CreateArrayWithLength(env, arrayLength, &testArray);
4944 JSVM_Value name1 = nullptr;
4945 char newStr1[] = "hahaha";
4946 OH_JSVM_CreateStringUtf8(env, newStr1, strlen(newStr1), &name1);
4947 JSVM_Value name2 = nullptr;
4948 char newStr2[] = "heheheh";
4949 OH_JSVM_CreateStringUtf8(env, newStr2, strlen(newStr2), &name2);
4950 OH_JSVM_SetElement(env, testArray, 0, name1);
4951 OH_JSVM_SetElement(env, testArray, 1, name2);
4952 return testArray;
4953 }
4954
NameHandler8(JSVM_Env env,JSVM_CallbackInfo info)4955 static JSVM_Value NameHandler8(JSVM_Env env, JSVM_CallbackInfo info)
4956 {
4957 JSVM_CallbackStruct param;
4958 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
4959 JSVM_Value thisVar = nullptr;
4960 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
4961 return thisVar;
4962 };
4963 param.data = nullptr;
4964 propertyCfg.genericNamedPropertyEnumeratorCallback = EnumeratorNamedPropertyCbInfo2;
4965 JSVM_Value testWrapClass = nullptr;
4966 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test20", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
4967 &propertyCfg, nullptr, &testWrapClass);
4968 if (res == JSVM_OK) {
4969 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
4970 } else {
4971 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
4972 return nullptr;
4973 }
4974 JSVM_Value instanceValue = nullptr;
4975 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
4976 const char testStr[] = "hello world2";
4977 JSVM_Value setvalueName = nullptr;
4978 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
4979 OH_JSVM_SetNamedProperty(env, instanceValue, "str11", setvalueName);
4980 OH_JSVM_SetNamedProperty(env, instanceValue, "str123", setvalueName);
4981 JSVM_Value allPropertyNames = nullptr;
4982 OH_JSVM_GetAllPropertyNames(
4983 env,
4984 instanceValue,
4985 JSVM_KEY_OWN_ONLY,
4986 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_SKIP_SYMBOLS),
4987 JSVM_KEY_NUMBERS_TO_STRINGS,
4988 &allPropertyNames);
4989 uint32_t nameSize = 0;
4990 OH_JSVM_GetArrayLength(env, allPropertyNames, &nameSize);
4991 JSVM_Value propertyName = nullptr;
4992 for (uint32_t i = 0; i < nameSize; ++i) {
4993 OH_JSVM_GetElement(env, allPropertyNames, i, &propertyName);
4994 char str[100];
4995 size_t size;
4996 OH_JSVM_GetValueStringUtf8(env, propertyName, str, strlen(str), &size);
4997 bool isExisted = false;
4998 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
4999 }
5000 propertyCfg.genericNamedPropertyEnumeratorCallback = nullptr;
5001 return nullptr;
5002 }
5003
testEnumeratorNamedProperty02(napi_env env1,napi_callback_info info)5004 static napi_value testEnumeratorNamedProperty02(napi_env env1, napi_callback_info info)
5005 {
5006 JSVM_InitOptions init_options;
5007 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
5008 printf("memset_s failed");
5009 return nullptr;
5010 }
5011 init_options.externalReferences = externals;
5012 if (aa == 0) {
5013 OH_JSVM_Init(&init_options);
5014 aa++;
5015 }
5016 JSVM_VM vm;
5017 JSVM_CreateVMOptions options;
5018 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
5019 printf("memset_s failed");
5020 return nullptr;
5021 }
5022 OH_JSVM_CreateVM(&options, &vm);
5023 JSVM_VMScope vm_scope;
5024 OH_JSVM_OpenVMScope(vm, &vm_scope);
5025 JSVM_Env env;
5026 JSVM_CallbackStruct param[1];
5027 param[0].callback = nullptr;
5028 param[0].callback = NameHandler8;
5029 JSVM_PropertyDescriptor descriptor[] = {
5030 {"NameHandler8", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
5031 };
5032 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
5033 JSVM_EnvScope envScope;
5034 OH_JSVM_OpenEnvScope(env, &envScope);
5035 JSVM_HandleScope handlescope;
5036 OH_JSVM_OpenHandleScope(env, &handlescope);
5037 OH_JSVM_CloseHandleScope(env, handlescope);
5038 OH_JSVM_CloseEnvScope(env, envScope);
5039 OH_JSVM_DestroyEnv(env);
5040 OH_JSVM_CloseVMScope(vm, vm_scope);
5041 OH_JSVM_DestroyVM(vm);
5042 napi_value result11;
5043 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
5044 return result11;
5045 }
5046
5047 // ================== test nullptr return for indexedProperty handle =========
5048 // case 44 and case 45
5049 // When returning nullptr, the enumerator listens and triggers, only listens but not intercepts.
5050 // OH_JSVM_GetAllPropertyNames can retrieve the property names that have already been set
EnumeratorIndexedPropertyCbInfo1(JSVM_Env env,JSVM_Value thisArg,JSVM_Value data)5051 static JSVM_Value EnumeratorIndexedPropertyCbInfo1(JSVM_Env env, JSVM_Value thisArg, JSVM_Value data)
5052 {
5053 JSVM_Value gloablObj = nullptr;
5054 OH_JSVM_GetGlobal(env, &gloablObj);
5055 OH_JSVM_SetNamedProperty(env, gloablObj, "myTestInstance1", thisArg);
5056 return nullptr;
5057 }
5058
IndexHandler8(JSVM_Env env,JSVM_CallbackInfo info)5059 static JSVM_Value IndexHandler8(JSVM_Env env, JSVM_CallbackInfo info)
5060 {
5061 JSVM_CallbackStruct param;
5062 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
5063 JSVM_Value thisVar = nullptr;
5064 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
5065 return thisVar;
5066 };
5067 param.data = nullptr;
5068 propertyCfg.genericIndexedPropertyEnumeratorCallback = EnumeratorIndexedPropertyCbInfo1;
5069 JSVM_CallbackStruct callbackStruct;
5070 callbackStruct.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
5071 printf("call as a function called");
5072 return nullptr;
5073 };
5074 callbackStruct.data = nullptr;
5075 JSVM_Value testWrapClass = nullptr;
5076 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test21", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
5077 &propertyCfg, &callbackStruct, &testWrapClass);
5078 if (res == JSVM_OK) {
5079 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
5080 } else {
5081 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
5082 return nullptr;
5083 }
5084 JSVM_Value instanceValue = nullptr;
5085 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
5086
5087 const char testStr[] = "hello world1";
5088 JSVM_Value setvalueName = nullptr;
5089 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
5090 JSVM_Value jsIndex = nullptr;
5091 uint32_t index = 0;
5092 OH_JSVM_CreateUint32(env, index, &jsIndex);
5093 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
5094 JSVM_Value jsIndex1 = nullptr;
5095 uint32_t index1 = 1;
5096 OH_JSVM_CreateUint32(env, index1, &jsIndex1);
5097 OH_JSVM_SetProperty(env, instanceValue, jsIndex1, setvalueName);
5098 JSVM_Value allPropertyNames = nullptr;
5099 OH_JSVM_GetAllPropertyNames(
5100 env,
5101 instanceValue,
5102 JSVM_KEY_OWN_ONLY,
5103 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_SKIP_SYMBOLS),
5104 JSVM_KEY_NUMBERS_TO_STRINGS,
5105 &allPropertyNames);
5106 uint32_t nameSize = 0;
5107 OH_JSVM_GetArrayLength(env, allPropertyNames, &nameSize);
5108 JSVM_Value propertyName = nullptr;
5109 for (uint32_t i = 0; i < nameSize; ++i) {
5110 OH_JSVM_GetElement(env, allPropertyNames, i, &propertyName);
5111 char str[100];
5112 size_t size;
5113 OH_JSVM_GetValueStringUtf8(env, propertyName, str, strlen(str), &size);
5114 bool isExisted = false;
5115 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
5116 }
5117 propertyCfg.genericIndexedPropertyEnumeratorCallback = nullptr;
5118 return nullptr;
5119 }
5120
testEnumeratorIndexedProperty01(napi_env env1,napi_callback_info info)5121 static napi_value testEnumeratorIndexedProperty01(napi_env env1, napi_callback_info info)
5122 {
5123 JSVM_InitOptions init_options;
5124 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
5125 printf("memset_s failed");
5126 return nullptr;
5127 }
5128 init_options.externalReferences = externals;
5129 if (aa == 0) {
5130 OH_JSVM_Init(&init_options);
5131 aa++;
5132 }
5133 JSVM_VM vm;
5134 JSVM_CreateVMOptions options;
5135 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
5136 printf("memset_s failed");
5137 return nullptr;
5138 }
5139 OH_JSVM_CreateVM(&options, &vm);
5140 JSVM_VMScope vm_scope;
5141 OH_JSVM_OpenVMScope(vm, &vm_scope);
5142 JSVM_Env env;
5143 JSVM_CallbackStruct param[1];
5144 param[0].callback = nullptr;
5145 param[0].callback = IndexHandler8;
5146 JSVM_PropertyDescriptor descriptor[] = {
5147 {"indexHandler8", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
5148 };
5149 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
5150 JSVM_EnvScope envScope;
5151 OH_JSVM_OpenEnvScope(env, &envScope);
5152 JSVM_HandleScope handlescope;
5153 OH_JSVM_OpenHandleScope(env, &handlescope);
5154 OH_JSVM_CloseHandleScope(env, handlescope);
5155 OH_JSVM_CloseEnvScope(env, envScope);
5156 OH_JSVM_DestroyEnv(env);
5157 OH_JSVM_CloseVMScope(vm, vm_scope);
5158 OH_JSVM_DestroyVM(vm);
5159 napi_value result11;
5160 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
5161 return result11;
5162 }
5163
5164 // =================== test return true for indexedProperty handle ============
5165 // case 46
5166 // When returning the array, the enumerator listens and triggers, listens and intercepts, and
5167 // OH_JSVM_GetAllPropertyNames can retrieve the already set property names You can obtain the property
5168 // 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)5169 static JSVM_Value EnumeratorIndexedPropertyCbInfo2(JSVM_Env env, JSVM_Value thisArg, JSVM_Value data)
5170 {
5171 JSVM_Value testArray = nullptr;
5172 uint32_t arrayLength = 2;
5173 OH_JSVM_CreateArrayWithLength(env, arrayLength, &testArray);
5174 JSVM_Value index1 = nullptr;
5175 uint32_t num1 = 2;
5176 OH_JSVM_CreateUint32(env, num1, &index1);
5177 JSVM_Value index2 = nullptr;
5178 uint32_t num2 = 3;
5179 OH_JSVM_CreateUint32(env, num2, &index2);
5180 OH_JSVM_SetElement(env, testArray, 0, index1);
5181 OH_JSVM_SetElement(env, testArray, 1, index2);
5182 return testArray;
5183 }
5184
IndexHandler9(JSVM_Env env,JSVM_CallbackInfo info)5185 static JSVM_Value IndexHandler9(JSVM_Env env, JSVM_CallbackInfo info)
5186 {
5187 JSVM_CallbackStruct param;
5188 param.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
5189 JSVM_Value thisVar = nullptr;
5190 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
5191 return thisVar;
5192 };
5193 param.data = nullptr;
5194 propertyCfg.genericIndexedPropertyEnumeratorCallback = EnumeratorIndexedPropertyCbInfo2;
5195 JSVM_Value testWrapClass = nullptr;
5196 JSVM_Status res = OH_JSVM_DefineClassWithPropertyHandler(env, "Test22", NAPI_AUTO_LENGTH, ¶m, 0, nullptr,
5197 &propertyCfg, nullptr, &testWrapClass);
5198 if (res == JSVM_OK) {
5199 printf("OH_JSVM_DefineClassWithPropertyHandler successfully");
5200 } else {
5201 printf("OH_JSVM_DefineClassWithPropertyHandler failed");
5202 return nullptr;
5203 }
5204 JSVM_Value instanceValue = nullptr;
5205 OH_JSVM_NewInstance(env, testWrapClass, 0, nullptr, &instanceValue);
5206
5207 const char testStr[] = "hello world2";
5208 JSVM_Value setvalueName = nullptr;
5209 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &setvalueName);
5210 JSVM_Value jsIndex = nullptr;
5211 uint32_t index = 0;
5212 OH_JSVM_CreateUint32(env, index, &jsIndex);
5213 OH_JSVM_SetProperty(env, instanceValue, jsIndex, setvalueName);
5214 JSVM_Value jsIndex1 = nullptr;
5215 uint32_t index1 = 1;
5216 OH_JSVM_CreateUint32(env, index1, &jsIndex1);
5217 OH_JSVM_SetProperty(env, instanceValue, jsIndex1, setvalueName);
5218 JSVM_Value allPropertyNames = nullptr;
5219 OH_JSVM_GetAllPropertyNames(
5220 env,
5221 instanceValue,
5222 JSVM_KEY_OWN_ONLY,
5223 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_SKIP_SYMBOLS),
5224 JSVM_KEY_NUMBERS_TO_STRINGS,
5225 &allPropertyNames);
5226 uint32_t nameSize = 0;
5227 OH_JSVM_GetArrayLength(env, allPropertyNames, &nameSize);
5228 JSVM_Value propertyName = nullptr;
5229 for (uint32_t i = 0; i < nameSize; ++i) {
5230 OH_JSVM_GetElement(env, allPropertyNames, i, &propertyName);
5231 char str[100];
5232 size_t size;
5233 OH_JSVM_GetValueStringUtf8(env, propertyName, str, strlen(str), &size);
5234 bool isExisted = false;
5235 OH_JSVM_HasProperty(env, instanceValue, propertyName, &isExisted);
5236 }
5237 propertyCfg.genericIndexedPropertyEnumeratorCallback = nullptr;
5238 return nullptr;
5239 }
5240
testEnumeratorIndexedProperty02(napi_env env1,napi_callback_info info)5241 static napi_value testEnumeratorIndexedProperty02(napi_env env1, napi_callback_info info)
5242 {
5243 JSVM_InitOptions init_options;
5244 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
5245 printf("memset_s failed");
5246 return nullptr;
5247 }
5248 init_options.externalReferences = externals;
5249 if (aa == 0) {
5250 OH_JSVM_Init(&init_options);
5251 aa++;
5252 }
5253 JSVM_VM vm;
5254 JSVM_CreateVMOptions options;
5255 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
5256 printf("memset_s failed");
5257 return nullptr;
5258 }
5259 OH_JSVM_CreateVM(&options, &vm);
5260 JSVM_VMScope vm_scope;
5261 OH_JSVM_OpenVMScope(vm, &vm_scope);
5262 JSVM_Env env;
5263 JSVM_CallbackStruct param[1];
5264 param[0].callback = nullptr;
5265 param[0].callback = IndexHandler9;
5266 JSVM_PropertyDescriptor descriptor[] = {
5267 {"indexHandler9", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
5268 };
5269 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
5270 JSVM_EnvScope envScope;
5271 OH_JSVM_OpenEnvScope(env, &envScope);
5272 JSVM_HandleScope handlescope;
5273 OH_JSVM_OpenHandleScope(env, &handlescope);
5274 OH_JSVM_CloseHandleScope(env, handlescope);
5275 OH_JSVM_CloseEnvScope(env, envScope);
5276 OH_JSVM_DestroyEnv(env);
5277 OH_JSVM_CloseVMScope(vm, vm_scope);
5278 OH_JSVM_DestroyVM(vm);
5279 napi_value result11;
5280 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
5281 return result11;
5282 }
5283
testDefinePropertyHandle(napi_env env,napi_callback_info info)5284 static napi_value testDefinePropertyHandle(napi_env env, napi_callback_info info)
5285 {
5286 size_t argc = 1;
5287 napi_value args[1];
5288 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
5289 NAPI_ASSERT(env, argc > 0, "Wrong number of arguments");
5290
5291 int32_t value;
5292 napi_value result = 0;
5293 NAPI_CALL(env, napi_get_value_int32(env, args[0], &value));
5294 if (value == DIFF_VALUE_ZERO) {
5295 result = testCheckArgs(env, info);
5296 } else if (value == DIFF_VALUE_NOE) {
5297 result = testSetProperty(env, info);
5298 } else if (value == DIFF_VALUE_TWO) {
5299 result = testGetProperty(env, info);
5300 } else if (value == DIFF_VALUE_THREE) {
5301 result = testCallFunction(env, info);
5302 } else if (value == DIFF_VALUE_FOUR) {
5303 result = testSetNamedProperty01(env, info);
5304 } else if (value == DIFF_VALUE_FIVE) {
5305 result = testSetNamedProperty02(env, info);
5306 } else if (value == DIFF_VALUE_SIX) {
5307 result = testSetIndexProperty01(env, info);
5308 } else if (value == DIFF_VALUE_SEVEN) {
5309 result = testSetIndexProperty02(env, info);
5310 } else if (value == DIFF_VALUE_EIGHT) {
5311 result = testGetNamedProperty01(env, info);
5312 } else if (value == DIFF_VALUE_NINE) {
5313 result = testGetNamedProperty02(env, info);
5314 } else if (value == DIFF_VALUE_TEN) {
5315 result = testGetIndexProperty01(env, info);
5316 } else if (value == DIFF_VALUE_ELEVEN) {
5317 result = testGetIndexProperty02(env, info);
5318 } else if (value == DIFF_VALUE_TWELVE) {
5319 result = testDeleterNamedProperty01(env, info);
5320 } else if (value == DIFF_VALUE_THIRTEEN) {
5321 result = testDeleterNamedProperty02(env, info);
5322 } else if (value == DIFF_VALUE_FOURTEEN) {
5323 result = testDeleterNamedProperty03(env, info);
5324 } else if (value == DIFF_VALUE_FITEEN) {
5325 result = testDeleterIndexedProperty01(env, info);
5326 } else if (value == DIFF_VALUE_SIXTEEN) {
5327 result = testDeleterIndexedProperty02(env, info);
5328 } else if (value == DIFF_VALUE_SEVENTEEN) {
5329 result = testDeleterIndexedProperty03(env, info);
5330 } else if (value == DIFF_VALUE_EIGHTEEN) {
5331 result = testEnumeratorNamedProperty01(env, info);
5332 } else if (value == DIFF_VALUE_NINETEEN) {
5333 result = testEnumeratorNamedProperty02(env, info);
5334 } else if (value == DIFF_VALUE_TWENTY) {
5335 result = testEnumeratorIndexedProperty01(env, info);
5336 } else if (value == DIFF_VALUE_TWENTYONE) {
5337 result = testEnumeratorIndexedProperty02(env, info);
5338 }
5339 return result;
5340 }
5341
CreateStringUtf8(JSVM_Env env,JSVM_CallbackInfo info)5342 static JSVM_Value CreateStringUtf8(JSVM_Env env, JSVM_CallbackInfo info)
5343 {
5344 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
5345 const char *str = u8"你好, World!, successes to create UTF-8 string!";
5346 JSVM_Value result;
5347 JSVM_Status status = OH_JSVM_CreateStringUtf8(env, str, strlen(str), &result);
5348 if (status != JSVM_OK) {
5349 OH_JSVM_ThrowError(env, nullptr, "Failed to create UTF-8 string");
5350 }
5351 return result;
5352 }
5353
GetValueStringUtf8(JSVM_Env env,JSVM_CallbackInfo info)5354 static JSVM_Value GetValueStringUtf8(JSVM_Env env, JSVM_CallbackInfo info)
5355 {
5356 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
5357 size_t argc = 1;
5358 JSVM_Value args[1] = {nullptr};
5359 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5360 size_t length = 0;
5361 OH_JSVM_GetValueStringUtf8(env, args[0], nullptr, 0, &length);
5362 if (length <= 0) {
5363 return nullptr;
5364 }
5365 char *buf = (char *)malloc(length + 1);
5366 if (buf == nullptr) {
5367 printf("Memory allocation failed!\n");
5368 return nullptr;
5369 }
5370 JSVM_Status status = OH_JSVM_GetValueStringUtf8(env, args[0], buf, length + 1, &length);
5371 if (status != JSVM_OK) {
5372 return nullptr;
5373 }
5374 JSVM_Value result;
5375 OH_JSVM_CreateStringUtf8(env, buf, length, &result);
5376 return result;
5377 }
5378
CreateStringUtf16(JSVM_Env env,JSVM_CallbackInfo info)5379 static JSVM_Value CreateStringUtf16(JSVM_Env env, JSVM_CallbackInfo info)
5380 {
5381 strcpy_s(g_dataType, sizeof(g_dataType), "utf16");
5382 const char16_t *str = u"你好, World!, successes to create UTF-16 string!";
5383 size_t length = 0;
5384 while (str[length] != u'\0') {
5385 ++length;
5386 }
5387 JSVM_Value result;
5388 JSVM_Status status = OH_JSVM_CreateStringUtf16(env, str, length, &result);
5389 if (status != JSVM_OK) {
5390 OH_JSVM_ThrowError(env, nullptr, "Failed to create UTF-16 string");
5391 }
5392 return result;
5393 }
5394
5395 static const int MAX_BUFFER_SIZE = 128;
GetValueStringUtf16(JSVM_Env env,JSVM_CallbackInfo info)5396 static JSVM_Value GetValueStringUtf16(JSVM_Env env, JSVM_CallbackInfo info)
5397 {
5398 strcpy_s(g_dataType, sizeof(g_dataType), "utf16");
5399 size_t argc = 1;
5400 JSVM_Value args[1] = {nullptr};
5401 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5402 JSVM_Value result;
5403 size_t length;
5404 char16_t buffer[MAX_BUFFER_SIZE];
5405
5406 size_t bufferSize = MAX_BUFFER_SIZE;
5407 JSVM_Status status = OH_JSVM_GetValueStringUtf16(env, args[0], buffer, bufferSize, &length);
5408 if (status != JSVM_OK) {
5409 return nullptr;
5410 }
5411
5412 OH_JSVM_CreateStringUtf16(env, buffer, length, &result);
5413 return result;
5414 }
5415
CreateStringLatin1(JSVM_Env env,JSVM_CallbackInfo info)5416 static JSVM_Value CreateStringLatin1(JSVM_Env env, JSVM_CallbackInfo info)
5417 {
5418 strcpy_s(g_dataType, sizeof(g_dataType), "latin");
5419 const char *str = "Hello, World! éçñ, successes to create Latin1 string! 111";
5420 JSVM_Value result = nullptr;
5421 JSVM_Status status = OH_JSVM_CreateStringLatin1(env, str, JSVM_AUTO_LENGTH, &result);
5422 if (status != JSVM_OK) {
5423 OH_JSVM_ThrowError(env, nullptr, "Failed to create Latin1 string");
5424 }
5425 return result;
5426 }
5427
GetValueStringLatin1(JSVM_Env env,JSVM_CallbackInfo info)5428 static JSVM_Value GetValueStringLatin1(JSVM_Env env, JSVM_CallbackInfo info)
5429 {
5430 strcpy_s(g_dataType, sizeof(g_dataType), "latin");
5431 size_t argc = 1;
5432 JSVM_Value args[1] = {nullptr};
5433 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5434 char buf[MAX_BUFFER_SIZE];
5435 size_t length = 0;
5436 JSVM_Value jsvmRes;
5437 JSVM_Status status = OH_JSVM_GetValueStringLatin1(env, args[0], buf, MAX_BUFFER_SIZE, &length);
5438 if (status != JSVM_OK) {
5439 return nullptr;
5440 }
5441 OH_JSVM_CreateStringLatin1(env, buf, length, &jsvmRes);
5442 return jsvmRes;
5443 }
5444
GetResultType(JSVM_Env env,JSVM_Value result)5445 void GetResultType(JSVM_Env env, JSVM_Value result)
5446 {
5447 JSVM_ValueType valueType;
5448 OH_JSVM_Typeof(env, result, &valueType);
5449 switch (valueType) {
5450 case JSVM_NUMBER: {
5451 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5452 break;
5453 }
5454 case JSVM_STRING: {
5455 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
5456 break;
5457 }
5458 case JSVM_OBJECT: {
5459 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
5460 break;
5461 }
5462 case JSVM_BIGINT: {
5463 strcpy_s(g_dataType, sizeof(g_dataType), "BigintUint64");
5464 break;
5465 }
5466 default: {
5467 break;
5468 }
5469 }
5470 }
5471
CreateDate(JSVM_Env env,JSVM_CallbackInfo info)5472 static JSVM_Value CreateDate(JSVM_Env env, JSVM_CallbackInfo info)
5473 {
5474 strcpy_s(g_dataType, sizeof(g_dataType), "date");
5475 JSVM_Value returnValue = nullptr;
5476 OH_JSVM_CreateDate(env, DIFF_VALUE_DATE, &returnValue);
5477 return returnValue;
5478 }
5479
CreateObject(JSVM_Env env,JSVM_CallbackInfo info)5480 static JSVM_Value CreateObject(JSVM_Env env, JSVM_CallbackInfo info)
5481 {
5482 strcpy_s(g_dataType, sizeof(g_dataType), "object");
5483 JSVM_Value object;
5484 OH_JSVM_CreateObject(env, &object);
5485 JSVM_Value name;
5486 OH_JSVM_CreateStringUtf8(env, "name", JSVM_AUTO_LENGTH, &name);
5487 JSVM_Value value;
5488 OH_JSVM_CreateStringUtf8(env, "Hello OH_JSVM_CreateObject!", NAPI_AUTO_LENGTH, &value);
5489 OH_JSVM_SetProperty(env, object, name, value);
5490 return object;
5491 }
5492
CreateExternal(JSVM_Env env,JSVM_CallbackInfo info)5493 static JSVM_Value CreateExternal(JSVM_Env env, JSVM_CallbackInfo info)
5494 {
5495 void *data = malloc(DIFF_VALUE_TEN);
5496 if (data == nullptr) {
5497 printf("Memory allocation failed!\n");
5498 return nullptr;
5499 }
5500 if (memset_s(data, DIFF_VALUE_TEN, 0, DIFF_VALUE_TEN) != EOK) {
5501 printf("memset_s failed");
5502 return nullptr;
5503 }
5504 const char testStr[] = "test";
5505 JSVM_Value external = nullptr;
5506 JSVM_Status status = OH_JSVM_CreateExternal(
5507 env, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, (void *)testStr, &external);
5508 JSVM_Value returnValue;
5509 bool type = false;
5510 if (status != JSVM_OK) {
5511 return nullptr;
5512 } else {
5513 type = true;
5514 }
5515 OH_JSVM_GetBoolean(env, type, &returnValue);
5516 return returnValue;
5517 }
5518
GetValueExternal(JSVM_Env env,JSVM_CallbackInfo info)5519 static JSVM_Value GetValueExternal(JSVM_Env env, JSVM_CallbackInfo info)
5520 {
5521 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5522 void *data = reinterpret_cast<void *>(0x12345);
5523 JSVM_Value externalValue;
5524 JSVM_Status status = OH_JSVM_CreateExternal(env, data, nullptr, nullptr, &externalValue);
5525 if (status != JSVM_OK) {
5526 return nullptr;
5527 }
5528 void *dataValue;
5529 status = OH_JSVM_GetValueExternal(env, externalValue, &dataValue);
5530 if (status != JSVM_OK) {
5531 return nullptr;
5532 }
5533 JSVM_Value returnValue;
5534 OH_JSVM_CreateInt32(env, 0, &returnValue);
5535 return returnValue;
5536 }
5537
CreateArray(JSVM_Env env,JSVM_CallbackInfo info)5538 static JSVM_Value CreateArray(JSVM_Env env, JSVM_CallbackInfo info)
5539 {
5540 strcpy_s(g_dataType, sizeof(g_dataType), "array");
5541 JSVM_Value array = nullptr;
5542 OH_JSVM_CreateArray(env, &array);
5543 for (int i = 0; i < DIFF_VALUE_FIVE; i++) {
5544 JSVM_Value element;
5545 OH_JSVM_CreateInt32(env, i, &element);
5546 OH_JSVM_SetElement(env, array, i, element);
5547 }
5548 return array;
5549 }
5550
CreateArrayWithLength(JSVM_Env env,JSVM_CallbackInfo info)5551 static JSVM_Value CreateArrayWithLength(JSVM_Env env, JSVM_CallbackInfo info)
5552 {
5553 strcpy_s(g_dataType, sizeof(g_dataType), "array");
5554 size_t argc = 1;
5555 JSVM_Value argv[1] = {nullptr};
5556 JSVM_Value result = nullptr;
5557 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
5558 int32_t length = 0;
5559 OH_JSVM_GetValueInt32(env, argv[0], &length);
5560 JSVM_Status status = OH_JSVM_CreateArrayWithLength(env, length, &result);
5561 if (status == JSVM_OK) {
5562 for (int32_t i = 0; i < length; i++) {
5563 JSVM_Value value;
5564 OH_JSVM_CreateInt32(env, i, &value);
5565 OH_JSVM_SetElement(env, result, i, value);
5566 }
5567 }
5568 return result;
5569 }
5570
CreateArraybuffer(JSVM_Env env,JSVM_CallbackInfo info)5571 static JSVM_Value CreateArraybuffer(JSVM_Env env, JSVM_CallbackInfo info)
5572 {
5573 strcpy_s(g_dataType, sizeof(g_dataType), "arraybuffer");
5574 size_t argc = 1;
5575 JSVM_Value argv[1] = {nullptr};
5576 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
5577 int32_t value;
5578 size_t length;
5579 OH_JSVM_GetValueInt32(env, argv[0], &value);
5580 length = size_t(value);
5581 void *data;
5582 JSVM_Value result = nullptr;
5583 OH_JSVM_CreateArraybuffer(env, length, &data, &result);
5584 return result;
5585 }
5586
GetArrayLength(JSVM_Env env,JSVM_CallbackInfo info)5587 static JSVM_Value GetArrayLength(JSVM_Env env, JSVM_CallbackInfo info)
5588 {
5589 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5590 size_t argc = 1;
5591 JSVM_Value args[1] = {nullptr};
5592 uint32_t length;
5593 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5594
5595 bool isArray = false;
5596 OH_JSVM_IsArray(env, args[0], &isArray);
5597 if (!isArray) {
5598 return nullptr;
5599 }
5600 OH_JSVM_GetArrayLength(env, args[0], &length);
5601 JSVM_Value result = nullptr;
5602 OH_JSVM_CreateInt32(env, length, &result);
5603 return result;
5604 }
5605
CreateInt32(JSVM_Env env,JSVM_CallbackInfo info)5606 static JSVM_Value CreateInt32(JSVM_Env env, JSVM_CallbackInfo info)
5607 {
5608 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5609 JSVM_Value result;
5610 JSVM_Status status = OH_JSVM_CreateInt32(env, DIFF_VALUE_INT32, &result);
5611 if (status != JSVM_OK) {
5612 return nullptr;
5613 }
5614 return result;
5615 }
5616
GetValueInt32(JSVM_Env env,JSVM_CallbackInfo info)5617 static JSVM_Value GetValueInt32(JSVM_Env env, JSVM_CallbackInfo info)
5618 {
5619 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5620 size_t argc = 1;
5621 JSVM_Value args[1] = {nullptr};
5622 int32_t result;
5623 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5624 JSVM_Status status = OH_JSVM_GetValueInt32(env, args[0], &result);
5625 if (status != JSVM_OK) {
5626 return nullptr;
5627 }
5628 JSVM_Value jsvmResult;
5629 status = OH_JSVM_CreateInt32(env, result, &jsvmResult);
5630 if (status != JSVM_OK) {
5631 return nullptr;
5632 }
5633 return jsvmResult;
5634 }
5635
CreateUInt32(JSVM_Env env,JSVM_CallbackInfo info)5636 static JSVM_Value CreateUInt32(JSVM_Env env, JSVM_CallbackInfo info)
5637 {
5638 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5639 JSVM_Value result;
5640 JSVM_Status status = OH_JSVM_CreateUint32(env, DIFF_VALUE_UINT, &result);
5641 if (status != JSVM_OK) {
5642 return nullptr;
5643 }
5644 return result;
5645 }
5646
GetValueUint32(JSVM_Env env,JSVM_CallbackInfo info)5647 static JSVM_Value GetValueUint32(JSVM_Env env, JSVM_CallbackInfo info)
5648 {
5649 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5650 size_t argc = 1;
5651 JSVM_Value argv[1] = {nullptr};
5652 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
5653
5654 uint32_t number;
5655 JSVM_Status status = OH_JSVM_GetValueUint32(env, argv[0], &number);
5656 JSVM_Value result = nullptr;
5657 OH_JSVM_CreateUint32(env, number, &result);
5658 if (status != JSVM_OK) {
5659 return nullptr;
5660 }
5661 return result;
5662 }
5663
CreateInt64(JSVM_Env env,JSVM_CallbackInfo info)5664 static JSVM_Value CreateInt64(JSVM_Env env, JSVM_CallbackInfo info)
5665 {
5666 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5667 JSVM_Value result;
5668 JSVM_Status status = OH_JSVM_CreateInt64(env, DIFF_VALUE_INT, &result);
5669 if (status != JSVM_OK) {
5670 return nullptr;
5671 }
5672 return result;
5673 }
5674
GetValueInt64(JSVM_Env env,JSVM_CallbackInfo info)5675 static JSVM_Value GetValueInt64(JSVM_Env env, JSVM_CallbackInfo info)
5676 {
5677 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5678 size_t argc = 1;
5679 JSVM_Value args[1] = {nullptr};
5680 int64_t result;
5681 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5682 JSVM_Status status = OH_JSVM_GetValueInt64(env, args[0], &result);
5683 JSVM_Value jsvmResult;
5684 OH_JSVM_CreateInt64(env, result, &jsvmResult);
5685 if (status != JSVM_OK) {
5686 return nullptr;
5687 }
5688 return jsvmResult;
5689 }
5690
CreateDouble(JSVM_Env env,JSVM_CallbackInfo info)5691 static JSVM_Value CreateDouble(JSVM_Env env, JSVM_CallbackInfo info)
5692 {
5693 strcpy_s(g_dataType, sizeof(g_dataType), "double");
5694 JSVM_Value result;
5695 JSVM_Status status = OH_JSVM_CreateDouble(env, DIFF_VALUE_DOUBLE, &result);
5696 if (status != JSVM_OK) {
5697 return nullptr;
5698 }
5699 return result;
5700 }
5701
GetDouble(JSVM_Env env,JSVM_CallbackInfo info)5702 static JSVM_Value GetDouble(JSVM_Env env, JSVM_CallbackInfo info)
5703 {
5704 strcpy_s(g_dataType, sizeof(g_dataType), "double");
5705 size_t argc = 1;
5706 JSVM_Value args[1] = {nullptr};
5707 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5708
5709 double value;
5710 JSVM_Status status = OH_JSVM_GetValueDouble(env, args[0], &value);
5711 if (status != JSVM_OK) {
5712 return nullptr;
5713 }
5714 JSVM_Value result;
5715 OH_JSVM_CreateDouble(env, value, &result);
5716 return result;
5717 }
5718
GetBoolean(JSVM_Env env,JSVM_CallbackInfo info)5719 static JSVM_Value GetBoolean(JSVM_Env env, JSVM_CallbackInfo info)
5720 {
5721 size_t argc = 2;
5722 JSVM_Value argv[2] = {nullptr};
5723 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
5724 int32_t paramData;
5725 OH_JSVM_GetValueInt32(env, argv[0], ¶mData);
5726 int32_t paramValue;
5727 OH_JSVM_GetValueInt32(env, argv[1], ¶mValue);
5728 JSVM_Value returnValue;
5729 bool type = false;
5730 if (paramData == paramValue) {
5731 type = true;
5732 }
5733 OH_JSVM_GetBoolean(env, type, &returnValue);
5734 return returnValue;
5735 }
5736
GetValueBool(JSVM_Env env,JSVM_CallbackInfo info)5737 static JSVM_Value GetValueBool(JSVM_Env env, JSVM_CallbackInfo info)
5738 {
5739 size_t argc = 1;
5740 JSVM_Value args[1] = {nullptr};
5741 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5742 bool boolC;
5743 JSVM_Status status = OH_JSVM_GetValueBool(env, args[0], &boolC);
5744 if (status == JSVM_BOOLEAN_EXPECTED) {
5745 return nullptr;
5746 }
5747 JSVM_Value bool_napi;
5748 OH_JSVM_GetBoolean(env, boolC, &bool_napi);
5749 return bool_napi;
5750 }
5751
CreateBigintUint64(JSVM_Env env,JSVM_CallbackInfo info)5752 static JSVM_Value CreateBigintUint64(JSVM_Env env, JSVM_CallbackInfo info)
5753 {
5754 strcpy_s(g_dataType, sizeof(g_dataType), "BigintUint64");
5755 JSVM_Value returnValue;
5756 OH_JSVM_CreateBigintUint64(env, DIFF_VALUE_BIGUINT, &returnValue);
5757 return returnValue;
5758 }
5759
GetValueBigintUint64(JSVM_Env env,JSVM_CallbackInfo info)5760 static JSVM_Value GetValueBigintUint64(JSVM_Env env, JSVM_CallbackInfo info)
5761 {
5762 strcpy_s(g_dataType, sizeof(g_dataType), "BigintUint64");
5763 size_t argc = 1;
5764 JSVM_Value args[1] = {nullptr};
5765 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5766 uint64_t value = 0;
5767 bool lossLess = false;
5768 OH_JSVM_GetValueBigintUint64(env, args[0], &value, &lossLess);
5769 if (!lossLess) {
5770 OH_JSVM_ThrowError(env, nullptr, "BigInt values have no lossless converted");
5771 }
5772 JSVM_Value returnValue = nullptr;
5773 OH_JSVM_CreateBigintUint64(env, value, &returnValue);
5774 return returnValue;
5775 }
5776
CreateBigintInt64(JSVM_Env env,JSVM_CallbackInfo info)5777 static JSVM_Value CreateBigintInt64(JSVM_Env env, JSVM_CallbackInfo info)
5778 {
5779 strcpy_s(g_dataType, sizeof(g_dataType), "BigintInt64");
5780 JSVM_Value returnValue = nullptr;
5781 OH_JSVM_CreateBigintInt64(env, DIFF_VALUE_BIGINT, &returnValue);
5782 return returnValue;
5783 }
5784
GetBigintInt64(JSVM_Env env,JSVM_CallbackInfo info)5785 static JSVM_Value GetBigintInt64(JSVM_Env env, JSVM_CallbackInfo info)
5786 {
5787 strcpy_s(g_dataType, sizeof(g_dataType), "BigintInt64");
5788 size_t argc = 1;
5789 JSVM_Value args[1] = {nullptr};
5790 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5791 int64_t value = 0;
5792 bool lossLess = false;
5793 OH_JSVM_GetValueBigintInt64(env, args[0], &value, &lossLess);
5794 if (!lossLess) {
5795 OH_JSVM_ThrowError(env, nullptr, "BigInt values have not been lossless converted");
5796 }
5797 JSVM_Value returnValue = nullptr;
5798 OH_JSVM_CreateBigintInt64(env, value, &returnValue);
5799 return returnValue;
5800 }
5801
CreateBigintWords(JSVM_Env env,JSVM_CallbackInfo info)5802 static JSVM_Value CreateBigintWords(JSVM_Env env, JSVM_CallbackInfo info)
5803 {
5804 strcpy_s(g_dataType, sizeof(g_dataType), "BigintWords");
5805 int signBit = 0;
5806 size_t wordCount = DIFF_VALUE_THREE;
5807 uint64_t words[] = {12ULL, 34ULL, 56ULL};
5808 JSVM_Value returnValue = nullptr;
5809 JSVM_Status status = OH_JSVM_CreateBigintWords(env, signBit, wordCount, words, &returnValue);
5810 if (status != JSVM_OK) {
5811 OH_JSVM_ThrowError(env, nullptr, "OH_JSVM_CreateBigintWords fail");
5812 }
5813 return returnValue;
5814 }
5815
GetValueBigintWords(JSVM_Env env,JSVM_CallbackInfo info)5816 static JSVM_Value GetValueBigintWords(JSVM_Env env, JSVM_CallbackInfo info)
5817 {
5818 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5819 size_t argc = 1;
5820 JSVM_Value args[1] = {nullptr};
5821 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5822
5823 int signBit = 0;
5824 size_t wordCount = 0;
5825 uint64_t words;
5826
5827 OH_JSVM_GetValueBigintWords(env, args[0], nullptr, &wordCount, nullptr);
5828 JSVM_Status status = OH_JSVM_GetValueBigintWords(env, args[0], &signBit, &wordCount, &words);
5829 if (status != JSVM_OK) {
5830 return nullptr;
5831 }
5832
5833 JSVM_Value returnValue = nullptr;
5834 OH_JSVM_CreateInt32(env, signBit, &returnValue);
5835 return returnValue;
5836 }
5837
GetNull(JSVM_Env env,JSVM_CallbackInfo info)5838 static JSVM_Value GetNull(JSVM_Env env, JSVM_CallbackInfo info)
5839 {
5840 JSVM_Value nullValue;
5841 OH_JSVM_GetNull(env, &nullValue);
5842 return nullValue;
5843 }
5844
GetUndefined(JSVM_Env env,JSVM_CallbackInfo info)5845 static JSVM_Value GetUndefined(JSVM_Env env, JSVM_CallbackInfo info)
5846 {
5847 size_t argc = 1;
5848 JSVM_Value args[1] = {nullptr};
5849 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5850 JSVM_Value value = nullptr;
5851 OH_JSVM_GetUndefined(env, &value);
5852 return value;
5853 }
5854
GetGlobal(JSVM_Env env,JSVM_CallbackInfo info)5855 static JSVM_Value GetGlobal(JSVM_Env env, JSVM_CallbackInfo info)
5856 {
5857 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
5858 JSVM_Value value = nullptr;
5859 JSVM_Value global = nullptr;
5860 OH_JSVM_CreateInt32(env, DIFF_VALUE_NOE, &value);
5861 OH_JSVM_GetGlobal(env, &global);
5862 OH_JSVM_SetNamedProperty(env, global, "Row", value);
5863 return global;
5864 }
5865
CreateSymbol(JSVM_Env env,JSVM_CallbackInfo info)5866 static JSVM_Value CreateSymbol(JSVM_Env env, JSVM_CallbackInfo info)
5867 {
5868 JSVM_Value result;
5869 const char *des = "only";
5870 OH_JSVM_CreateStringUtf8(env, des, JSVM_AUTO_LENGTH, &result);
5871 JSVM_Value returnSymbol;
5872 OH_JSVM_CreateSymbol(env, result, &returnSymbol);
5873 JSVM_Value returnValue;
5874 bool type = false;
5875 JSVM_ValueType valuetypeSymbol;
5876 OH_JSVM_Typeof(env, returnSymbol, &valuetypeSymbol);
5877 if (valuetypeSymbol == JSVM_SYMBOL) {
5878 type = true;
5879 }
5880 OH_JSVM_GetBoolean(env, type, &returnValue);
5881 return returnValue;
5882 }
5883
SymbolFor(JSVM_Env env,JSVM_CallbackInfo info)5884 static JSVM_Value SymbolFor(JSVM_Env env, JSVM_CallbackInfo info)
5885 {
5886 JSVM_Value description;
5887 OH_JSVM_CreateStringUtf8(env, "test_demo", DIFF_VALUE_NINE, &description);
5888 char buffer[MAX_BUFFER_SIZE];
5889 size_t copied = 0;
5890 OH_JSVM_GetValueStringUtf8(env, description, buffer, MAX_BUFFER_SIZE, &copied);
5891 JSVM_Value symbol;
5892 OH_JSVM_CreateSymbol(env, description, &symbol);
5893 JSVM_Value result_symbol;
5894 JSVM_Status status = OH_JSVM_SymbolFor(env, nullptr, 0, &result_symbol);
5895 JSVM_Value returnValue;
5896 bool type = false;
5897 JSVM_ValueType valuetypeSymbol;
5898 OH_JSVM_Typeof(env, result_symbol, &valuetypeSymbol);
5899 if (valuetypeSymbol == JSVM_SYMBOL && status == JSVM_OK) {
5900 type = true;
5901 }
5902 OH_JSVM_GetBoolean(env, type, &returnValue);
5903 return returnValue;
5904 }
5905
GetDateValue(JSVM_Env env,JSVM_CallbackInfo info)5906 static JSVM_Value GetDateValue(JSVM_Env env, JSVM_CallbackInfo info)
5907 {
5908 strcpy_s(g_dataType, sizeof(g_dataType), "double");
5909 size_t argc = 1;
5910 JSVM_Value args[1] = {nullptr};
5911 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5912 double value;
5913 JSVM_Status status = OH_JSVM_GetDateValue(env, args[0], &value);
5914 if (status != JSVM_OK) {
5915 OH_JSVM_ThrowError(env, nullptr, "napi_get_date_value fail");
5916 }
5917 JSVM_Value returnValue = nullptr;
5918 OH_JSVM_CreateDouble(env, value, &returnValue);
5919 return returnValue;
5920 }
5921
IsDate(JSVM_Env env,JSVM_CallbackInfo info)5922 static JSVM_Value IsDate(JSVM_Env env, JSVM_CallbackInfo info)
5923 {
5924 size_t argc = 1;
5925 JSVM_Value args[1] = {nullptr};
5926 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5927
5928 bool result = false;
5929 OH_JSVM_IsDate(env, args[0], &result);
5930
5931 JSVM_Value isDate = nullptr;
5932 OH_JSVM_GetBoolean(env, result, &isDate);
5933 return isDate;
5934 }
5935
5936
GetArraybufferInfo(JSVM_Env env,JSVM_CallbackInfo info)5937 static JSVM_Value GetArraybufferInfo(JSVM_Env env, JSVM_CallbackInfo info)
5938 {
5939 size_t argc = 2;
5940 JSVM_Value args[2] = {nullptr};
5941 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5942 int32_t infoTypeParam;
5943 OH_JSVM_GetValueInt32(env, args[1], &infoTypeParam);
5944 bool isArrayBuffer = false;
5945 OH_JSVM_IsArraybuffer(env, args[0], &isArrayBuffer);
5946 if (!isArrayBuffer) {
5947 return nullptr;
5948 }
5949 enum InfoType {
5950 ARRAY_LENGTH,
5951 ARRAY_BUFFER
5952 };
5953 void *data;
5954 size_t byteLength;
5955 JSVM_Status status = OH_JSVM_GetArraybufferInfo(env, args[0], &data, &byteLength);
5956 if (status != JSVM_OK) {
5957 return nullptr;
5958 }
5959 JSVM_Value result = nullptr;
5960 switch (infoTypeParam) {
5961 case ARRAY_LENGTH:
5962 strcpy_s(g_dataType, sizeof(g_dataType), "int");
5963 JSVM_Value jsvmLength;
5964 OH_JSVM_CreateInt32(env, byteLength, &jsvmLength);
5965 result = jsvmLength;
5966 break;
5967 case ARRAY_BUFFER:
5968 strcpy_s(g_dataType, sizeof(g_dataType), "arraybuffer");
5969 JSVM_Value bufferData;
5970 OH_JSVM_CreateArraybuffer(env, byteLength, &data, &bufferData);
5971 result = bufferData;
5972 break;
5973 default:
5974 break;
5975 }
5976 return result;
5977 }
5978
CreateTypedArray(JSVM_Env env,JSVM_CallbackInfo info)5979 static JSVM_Value CreateTypedArray(JSVM_Env env, JSVM_CallbackInfo info)
5980 {
5981 strcpy_s(g_dataType, sizeof(g_dataType), "typearray");
5982 size_t argc = 1;
5983 JSVM_Value args[1] = {nullptr};
5984 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
5985 int32_t typeNum;
5986 OH_JSVM_GetValueInt32(env, args[0], &typeNum);
5987 JSVM_TypedarrayType arrayType;
5988 size_t elementSize = 0;
5989 arrayType = static_cast<JSVM_TypedarrayType>(typeNum);
5990 switch (typeNum) {
5991 case JSVM_INT8_ARRAY:
5992 case JSVM_UINT8_ARRAY:
5993 case JSVM_UINT8_CLAMPED_ARRAY:
5994 elementSize = sizeof(int8_t);
5995 break;
5996 case JSVM_INT16_ARRAY:
5997 case JSVM_UINT16_ARRAY:
5998 elementSize = sizeof(int16_t);
5999 break;
6000 case JSVM_INT32_ARRAY:
6001 case JSVM_UINT32_ARRAY:
6002 elementSize = sizeof(int32_t);
6003 break;
6004 case JSVM_FLOAT32_ARRAY:
6005 elementSize = sizeof(float);
6006 break;
6007 case JSVM_FLOAT64_ARRAY:
6008 elementSize = sizeof(double);
6009 break;
6010 case JSVM_BIGINT64_ARRAY:
6011 case JSVM_BIGUINT64_ARRAY:
6012 elementSize = sizeof(int64_t);
6013 break;
6014 default:
6015 arrayType = JSVM_INT8_ARRAY;
6016 elementSize = sizeof(int8_t);
6017 break;
6018 }
6019 size_t length = DIFF_VALUE_THREE;
6020 JSVM_Value arrayBuffer = nullptr;
6021 JSVM_Value typedArray = nullptr;
6022 void *data;
6023 OH_JSVM_CreateArraybuffer(env, length * elementSize, (void **)&data, &arrayBuffer);
6024 OH_JSVM_CreateTypedarray(env, arrayType, length, arrayBuffer, DIFF_VALUE_ZERO, &typedArray);
6025 return typedArray;
6026 }
6027
6028
GetTypedArrayInfo(JSVM_Env env,JSVM_CallbackInfo info)6029 static JSVM_Value GetTypedArrayInfo(JSVM_Env env, JSVM_CallbackInfo info)
6030 {
6031 size_t argc = 2;
6032 JSVM_Value args[2] = {nullptr};
6033 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6034 int32_t infoTypeParam;
6035 OH_JSVM_GetValueInt32(env, args[1], &infoTypeParam);
6036 enum InfoType {
6037 INFO_TYPE,
6038 INFO_LENGTH,
6039 INFO_ARRAY_BUFFER,
6040 INFO_BYTE_OFFSET
6041 };
6042 void *data;
6043 JSVM_TypedarrayType type;
6044 size_t byteOffset;
6045 size_t length;
6046 JSVM_Value arrayBuffer = nullptr;
6047 OH_JSVM_GetTypedarrayInfo(env, args[0], &type, &length, &data, &arrayBuffer, &byteOffset);
6048
6049 JSVM_Value result = nullptr;
6050 switch (infoTypeParam) {
6051 case INFO_TYPE:
6052 JSVM_Value int8_type;
6053 OH_JSVM_GetBoolean(env, type == JSVM_INT8_ARRAY, &int8_type);
6054 result = int8_type;
6055 break;
6056 case INFO_LENGTH:
6057 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6058 JSVM_Value jsvmLength;
6059 OH_JSVM_CreateInt32(env, length, &jsvmLength);
6060 result = jsvmLength;
6061 break;
6062 case INFO_BYTE_OFFSET:
6063 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6064 JSVM_Value jsvmOffset;
6065 OH_JSVM_CreateInt32(env, byteOffset, &jsvmOffset);
6066 result = jsvmOffset;
6067 break;
6068 case INFO_ARRAY_BUFFER:
6069 bool isArrayBuffer;
6070 OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer);
6071 JSVM_Value isArray;
6072 OH_JSVM_GetBoolean(env, isArrayBuffer, &isArray);
6073 result = isArray;
6074 break;
6075 default:
6076 break;
6077 }
6078 return result;
6079 }
6080
6081
CreateDataView(JSVM_Env env,JSVM_CallbackInfo info)6082 static JSVM_Value CreateDataView(JSVM_Env env, JSVM_CallbackInfo info)
6083 {
6084 size_t argc = 2;
6085 JSVM_Value args[2] = {nullptr};
6086 JSVM_Value arraybuffer = nullptr;
6087 JSVM_Value result = nullptr;
6088 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6089 OH_JSVM_CoerceToObject(env, args[0], &arraybuffer);
6090 OH_JSVM_CreateDataview(env, DIFF_VALUE_TWELVE, arraybuffer, DIFF_VALUE_FOUR, &result);
6091 uint8_t *data = nullptr;
6092 size_t length = 0;
6093 for (size_t i = 0; i < length; i++) {
6094 data[i] = static_cast<uint8_t>(i + 1);
6095 }
6096 int32_t infoType;
6097 OH_JSVM_GetValueInt32(env, args[1], &infoType);
6098 size_t returnLength;
6099 JSVM_Value returnArrayBuffer = nullptr;
6100 size_t returnOffset;
6101 enum InfoType {
6102 BYTE_LENGTHE,
6103 ARRAY_BUFFERE,
6104 BYTE_OFFSET
6105 };
6106 OH_JSVM_GetDataviewInfo(env, result, &returnLength, (void **)&data, &returnArrayBuffer, &returnOffset);
6107 JSVM_Value returnResult = nullptr;
6108 switch (infoType) {
6109 case BYTE_LENGTHE:
6110 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6111 OH_JSVM_CreateInt32(env, returnLength, &returnResult);
6112 break;
6113 case ARRAY_BUFFERE:
6114 bool isArrayBuffer;
6115 OH_JSVM_IsArraybuffer(env, returnArrayBuffer, &isArrayBuffer);
6116 OH_JSVM_GetBoolean(env, isArrayBuffer, &returnResult);
6117 break;
6118 case BYTE_OFFSET:
6119 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6120 OH_JSVM_CreateInt32(env, returnOffset, &returnResult);
6121 break;
6122 default:
6123 break;
6124 }
6125 return returnResult;
6126 }
6127
GetDataViewInfo(JSVM_Env env,JSVM_CallbackInfo info)6128 static JSVM_Value GetDataViewInfo(JSVM_Env env, JSVM_CallbackInfo info)
6129 {
6130 size_t argc = 2;
6131 JSVM_Value args[2] = {nullptr};
6132
6133 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6134 int32_t infoType;
6135 OH_JSVM_GetValueInt32(env, args[1], &infoType);
6136
6137 size_t byteLength;
6138 void *data;
6139 JSVM_Value arrayBuffer = nullptr;
6140 size_t byteOffset;
6141 enum InfoType {
6142 BYTE_LENGTHE,
6143 ARRAY_BUFFERE,
6144 BYTE_OFFSET
6145 };
6146 OH_JSVM_GetDataviewInfo(env, args[0], &byteLength, &data, &arrayBuffer, &byteOffset);
6147 JSVM_Value result = nullptr;
6148 switch (infoType) {
6149 case BYTE_LENGTHE:
6150 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6151 JSVM_Value len;
6152 OH_JSVM_CreateInt32(env, byteLength, &len);
6153 result = len;
6154 break;
6155 case ARRAY_BUFFERE:
6156 bool isArrayBuffer;
6157 OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer);
6158 JSVM_Value isArray;
6159 OH_JSVM_GetBoolean(env, isArrayBuffer, &isArray);
6160 result = isArray;
6161 break;
6162 case BYTE_OFFSET:
6163 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6164 JSVM_Value offset;
6165 OH_JSVM_CreateInt32(env, byteOffset, &offset);
6166 result = offset;
6167 break;
6168 default:
6169 break;
6170 }
6171 return result;
6172 }
6173
GetPrototype(JSVM_Env env,JSVM_CallbackInfo info)6174 static JSVM_Value GetPrototype(JSVM_Env env, JSVM_CallbackInfo info)
6175 {
6176 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6177 JSVM_Value obj = nullptr;
6178 OH_JSVM_CreateObject(env, &obj);
6179 const char *testNameStr = "set and get proto";
6180 JSVM_Value propValue = nullptr;
6181 JSVM_Value key;
6182 OH_JSVM_CreateStringUtf8(env, "name", JSVM_AUTO_LENGTH, &key);
6183 OH_JSVM_CreateStringUtf8(env, testNameStr, strlen(testNameStr), &propValue);
6184 OH_JSVM_SetProperty(env, obj, key, propValue);
6185
6186 JSVM_Value propResult = nullptr;
6187 JSVM_Status status = OH_JSVM_GetProperty(env, obj, key, &propResult);
6188 if (status != JSVM_OK) {
6189 return nullptr;
6190 }
6191 return propResult;
6192 }
6193
CreateReference(JSVM_Env env,JSVM_CallbackInfo info)6194 static JSVM_Value CreateReference(JSVM_Env env, JSVM_CallbackInfo info)
6195 {
6196 JSVM_Ref gRef = nullptr;
6197 strcpy_s(g_dataType, sizeof(g_dataType), "object");
6198 JSVM_Value obj = nullptr;
6199 OH_JSVM_CreateObject(env, &obj);
6200 JSVM_Value value = nullptr;
6201 OH_JSVM_CreateStringUtf8(env, "CreateReference", JSVM_AUTO_LENGTH, &value);
6202 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6203 JSVM_Status status = OH_JSVM_CreateReference(env, obj, 1, &gRef);
6204 if (status != JSVM_OK) {
6205 return nullptr;
6206 }
6207 JSVM_Finalize jSVM_Finalize = nullptr;
6208 OH_JSVM_AddFinalizer(env, obj, nullptr, jSVM_Finalize, nullptr, &gRef);
6209 uint32_t result;
6210 OH_JSVM_ReferenceRef(env, gRef, &result);
6211 if (result != DIFF_VALUE_TWO) {
6212 return nullptr;
6213 }
6214 return obj;
6215 }
6216
UseReference(JSVM_Env env,JSVM_CallbackInfo info)6217 static JSVM_Value UseReference(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, "UseReference", 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 JSVM_Value object = nullptr;
6238 status = OH_JSVM_GetReferenceValue(env, gRef, &object);
6239 if (status != JSVM_OK) {
6240 return nullptr;
6241 }
6242 return object;
6243 }
6244
DeleteReference(JSVM_Env env,JSVM_CallbackInfo info)6245 static JSVM_Value DeleteReference(JSVM_Env env, JSVM_CallbackInfo info)
6246 {
6247 JSVM_Ref gRef = nullptr;
6248 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6249 JSVM_Value obj = nullptr;
6250 OH_JSVM_CreateObject(env, &obj);
6251 JSVM_Value value = nullptr;
6252 OH_JSVM_CreateStringUtf8(env, "DeleteReference", JSVM_AUTO_LENGTH, &value);
6253 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6254 JSVM_Status status = OH_JSVM_CreateReference(env, obj, 1, &gRef);
6255 if (status != JSVM_OK) {
6256 return nullptr;
6257 }
6258 JSVM_Finalize jSVM_Finalize = nullptr;
6259 OH_JSVM_AddFinalizer(env, obj, nullptr, jSVM_Finalize, nullptr, &gRef);
6260 uint32_t result;
6261 OH_JSVM_ReferenceRef(env, gRef, &result);
6262 if (result != DIFF_VALUE_TWO) {
6263 return nullptr;
6264 }
6265 uint32_t num;
6266 OH_JSVM_ReferenceUnref(env, gRef, &num);
6267 if (num != 1) {
6268 return nullptr;
6269 }
6270 status = OH_JSVM_DeleteReference(env, gRef);
6271 if (status != JSVM_OK) {
6272 return nullptr;
6273 }
6274 JSVM_Value returnResult = nullptr;
6275 OH_JSVM_CreateStringUtf8(env, "OH_JSVM_DeleteReference success", JSVM_AUTO_LENGTH, &returnResult);
6276 return returnResult;
6277 }
6278
EscapableHandleScopeTest(JSVM_Env env,JSVM_CallbackInfo info)6279 static JSVM_Value EscapableHandleScopeTest(JSVM_Env env, JSVM_CallbackInfo info)
6280 {
6281 strcpy_s(g_dataType, sizeof(g_dataType), "object");
6282 JSVM_EscapableHandleScope scope = nullptr;
6283 OH_JSVM_OpenEscapableHandleScope(env, &scope);
6284 JSVM_Value obj = nullptr;
6285 OH_JSVM_CreateObject(env, &obj);
6286 JSVM_Value value = nullptr;
6287 OH_JSVM_CreateStringUtf8(env, "Test jsvm_escapable_handle_scope", JSVM_AUTO_LENGTH, &value);
6288 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6289 JSVM_Value escapedObj = nullptr;
6290 OH_JSVM_EscapeHandle(env, scope, obj, &escapedObj);
6291 OH_JSVM_CloseEscapableHandleScope(env, scope);
6292 OH_JSVM_CreateStringUtf8(env, "001", JSVM_AUTO_LENGTH, &value);
6293 OH_JSVM_SetNamedProperty(env, obj, "id", value);
6294 return escapedObj;
6295 }
6296
HandleScopeTest(JSVM_Env env,JSVM_CallbackInfo info)6297 static JSVM_Value HandleScopeTest(JSVM_Env env, JSVM_CallbackInfo info)
6298 {
6299 strcpy_s(g_dataType, sizeof(g_dataType), "object");
6300 JSVM_HandleScope scope = nullptr;
6301 JSVM_Status status = OH_JSVM_OpenHandleScope(env, &scope);
6302 if (status != JSVM_OK) {
6303 return nullptr;
6304 }
6305 JSVM_Value obj = nullptr;
6306 OH_JSVM_CreateObject(env, &obj);
6307 JSVM_Value value = nullptr;
6308 OH_JSVM_CreateStringUtf8(env, "test handleScope", JSVM_AUTO_LENGTH, &value);
6309 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6310 status = OH_JSVM_CloseHandleScope(env, scope);
6311 if (status != JSVM_OK) {
6312 return nullptr;
6313 }
6314 return obj;
6315 }
6316
HandleScope(JSVM_Env env,JSVM_CallbackInfo info)6317 static JSVM_Value HandleScope(JSVM_Env env, JSVM_CallbackInfo info)
6318 {
6319 strcpy_s(g_dataType, sizeof(g_dataType), "object");
6320 JSVM_HandleScope scope = nullptr;
6321 JSVM_Status status = OH_JSVM_OpenHandleScope(env, &scope);
6322 if (status != JSVM_OK) {
6323 return nullptr;
6324 }
6325 JSVM_Value obj = nullptr;
6326 OH_JSVM_CreateObject(env, &obj);
6327 JSVM_Value value = nullptr;
6328 OH_JSVM_CreateStringUtf8(env, "handleScope", JSVM_AUTO_LENGTH, &value);
6329 OH_JSVM_SetNamedProperty(env, obj, "name", value);
6330 status = OH_JSVM_CloseHandleScope(env, scope);
6331 if (status != JSVM_OK) {
6332 return nullptr;
6333 }
6334 OH_JSVM_CreateStringUtf8(env, "001", JSVM_AUTO_LENGTH, &value);
6335 OH_JSVM_SetNamedProperty(env, obj, "id", value);
6336 return obj;
6337 }
6338
6339
GetPropertyNames(JSVM_Env env,JSVM_CallbackInfo info)6340 static JSVM_Value GetPropertyNames(JSVM_Env env, JSVM_CallbackInfo info)
6341 {
6342 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6343 size_t argc = 1;
6344 JSVM_Value args[1] = {nullptr};
6345 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6346
6347 JSVM_Value result = nullptr;
6348 JSVM_Status status = OH_JSVM_GetPropertyNames(env, args[0], &result);
6349 if (status != JSVM_OK) {
6350 OH_JSVM_ThrowError(env, nullptr, "Failed to get propertynames");
6351 }
6352 return result;
6353 }
6354
GetAllPropertyNames(JSVM_Env env,JSVM_CallbackInfo info)6355 static JSVM_Value GetAllPropertyNames(JSVM_Env env, JSVM_CallbackInfo info)
6356 {
6357 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6358 size_t argc = 1;
6359 JSVM_Value args[1] = {nullptr};
6360 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6361 JSVM_Value result = nullptr;
6362 JSVM_Status status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
6363 JSVM_KeyFilter::JSVM_KEY_WRITABLE, JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6364 if (status != JSVM_OK) {
6365 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6366 }
6367 return result;
6368 }
6369
GetEnumerableNames(JSVM_Env env,JSVM_CallbackInfo info)6370 static JSVM_Value GetEnumerableNames(JSVM_Env env, JSVM_CallbackInfo info)
6371 {
6372 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6373 JSVM_Status status;
6374 size_t argc = 1;
6375 JSVM_Value args[1] = {nullptr};
6376 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6377 JSVM_Value result;
6378 status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_INCLUDE_PROTOTYPES,
6379 JSVM_KeyFilter::JSVM_KEY_ENUMERABLE, JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6380 if (status != JSVM_OK) {
6381 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6382 }
6383 return result;
6384 }
6385
GetOwnWritableNames(JSVM_Env env,JSVM_CallbackInfo info)6386 static JSVM_Value GetOwnWritableNames(JSVM_Env env, JSVM_CallbackInfo info)
6387 {
6388 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6389 JSVM_Status status;
6390 size_t argc = 1;
6391 JSVM_Value args[1] = {nullptr};
6392 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6393 JSVM_Value result;
6394 status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
6395 JSVM_KeyFilter::JSVM_KEY_WRITABLE, JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6396 if (status != JSVM_OK) {
6397 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6398 }
6399 return result;
6400 }
6401
GetEnumerableConfigurableNames(JSVM_Env env,JSVM_CallbackInfo info)6402 static JSVM_Value GetEnumerableConfigurableNames(JSVM_Env env, JSVM_CallbackInfo info)
6403 {
6404 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6405 JSVM_Status status;
6406 size_t argc = 1;
6407 JSVM_Value args[1] = {nullptr};
6408 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6409 JSVM_Value result;
6410 status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_INCLUDE_PROTOTYPES,
6411 static_cast<JSVM_KeyFilter>(JSVM_KEY_ENUMERABLE | JSVM_KEY_CONFIGURABLE),
6412 JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6413 if (status != JSVM_OK) {
6414 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6415 }
6416 return result;
6417 }
6418
GetOwnConfigurableNames(JSVM_Env env,JSVM_CallbackInfo info)6419 static JSVM_Value GetOwnConfigurableNames(JSVM_Env env, JSVM_CallbackInfo info)
6420 {
6421 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6422 JSVM_Status status;
6423 size_t argc = 1;
6424 JSVM_Value args[1] = {nullptr};
6425 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6426 JSVM_Value result;
6427 status = OH_JSVM_GetAllPropertyNames(env, args[0], JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY,
6428 JSVM_KeyFilter::JSVM_KEY_CONFIGURABLE, JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result);
6429 if (status != JSVM_OK) {
6430 OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames");
6431 }
6432 return result;
6433 }
6434
AddReturnedStatus(JSVM_Env env,const char * key,JSVM_Value object,JSVM_Status expectedStatus,JSVM_Status actualStatus)6435 static void AddReturnedStatus(JSVM_Env env, const char *key, JSVM_Value object, JSVM_Status expectedStatus,
6436 JSVM_Status actualStatus)
6437 {
6438 char messageString[100] = "";
6439 JSVM_Value propValue;
6440 OH_JSVM_CreateStringUtf8(env, (actualStatus == expectedStatus ? "Invalid argument" : messageString),
6441 JSVM_AUTO_LENGTH, &propValue);
6442 OH_JSVM_SetNamedProperty(env, object, key, propValue);
6443 }
6444
AddLastStatus(JSVM_Env env,const char * key,JSVM_Value returnValue)6445 static void AddLastStatus(JSVM_Env env, const char *key, JSVM_Value returnValue)
6446 {
6447 JSVM_Value propValue;
6448 const JSVM_ExtendedErrorInfo *pLastError;
6449 OH_JSVM_GetLastErrorInfo(env, &pLastError);
6450 OH_JSVM_CreateStringUtf8(env,
6451 (pLastError->errorMessage == nullptr ? "JSVM_status::JSVM_OK" : pLastError->errorMessage), JSVM_AUTO_LENGTH,
6452 &propValue);
6453 OH_JSVM_SetNamedProperty(env, returnValue, key, propValue);
6454 }
6455
SetProperty(JSVM_Env env,JSVM_CallbackInfo info)6456 static JSVM_Value SetProperty(JSVM_Env env, JSVM_CallbackInfo info)
6457 {
6458 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6459 size_t argc = 3;
6460 JSVM_Value args[3] = {nullptr};
6461 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6462
6463 JSVM_Status status = OH_JSVM_SetProperty(env, args[0], args[1], args[2]);
6464 if (status != JSVM_OK) {
6465 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetProperty fail");
6466 }
6467 return args[0];
6468 }
6469
SetPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6470 static JSVM_Value SetPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6471 {
6472 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6473 JSVM_Value returnValue;
6474 JSVM_Value object;
6475 OH_JSVM_CreateObject(env, &returnValue);
6476 OH_JSVM_CreateObject(env, &object);
6477 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6478 OH_JSVM_SetProperty(nullptr, object, 0, object));
6479 OH_JSVM_SetProperty(env, nullptr, 0, object);
6480 AddLastStatus(env, "nullprtObject", returnValue);
6481 OH_JSVM_SetProperty(env, object, 0, nullptr);
6482 AddLastStatus(env, "nullptrValue", returnValue);
6483 return returnValue;
6484 }
6485
GetProperty(JSVM_Env env,JSVM_CallbackInfo info)6486 static JSVM_Value GetProperty(JSVM_Env env, JSVM_CallbackInfo info)
6487 {
6488 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6489 size_t argc = 2;
6490 JSVM_Value args[2] = {nullptr};
6491 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6492 JSVM_Value result;
6493 JSVM_Status status = OH_JSVM_GetProperty(env, args[0], args[1], &result);
6494 if (status != JSVM_OK) {
6495 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetProperty fail");
6496 }
6497 GetResultType(env, result);
6498 return result;
6499 }
6500
GetPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6501 static JSVM_Value GetPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6502 {
6503 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6504 JSVM_Value returnValue;
6505 JSVM_Value object;
6506 OH_JSVM_CreateObject(env, &returnValue);
6507 OH_JSVM_CreateObject(env, &object);
6508 JSVM_Value key;
6509 OH_JSVM_CreateInt32(env, 0, &key);
6510 JSVM_Value result;
6511 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6512 OH_JSVM_GetProperty(nullptr, object, key, &result));
6513 OH_JSVM_GetProperty(env, nullptr, key, &result);
6514 AddLastStatus(env, "nullprtObject", returnValue);
6515 OH_JSVM_GetProperty(env, object, key, nullptr);
6516 AddLastStatus(env, "nullptrResult", returnValue);
6517 return returnValue;
6518 }
6519
HasProperty(JSVM_Env env,JSVM_CallbackInfo info)6520 static JSVM_Value HasProperty(JSVM_Env env, JSVM_CallbackInfo info)
6521 {
6522 JSVM_Status status;
6523 size_t argc = 2;
6524 JSVM_Value args[2] = {nullptr};
6525 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6526 bool result;
6527 status = OH_JSVM_HasProperty(env, args[0], args[1], &result);
6528 if (status != JSVM_OK) {
6529 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasProperty fail");
6530 }
6531 JSVM_Value returnReslut;
6532 OH_JSVM_GetBoolean(env, result, &returnReslut);
6533 return returnReslut;
6534 }
6535
HasPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6536 static JSVM_Value HasPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6537 {
6538 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6539 JSVM_Value returnValue;
6540 JSVM_Value object;
6541 OH_JSVM_CreateObject(env, &returnValue);
6542 OH_JSVM_CreateObject(env, &object);
6543 JSVM_Value key;
6544 OH_JSVM_CreateInt32(env, 0, &key);
6545 bool result;
6546 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6547 OH_JSVM_HasProperty(nullptr, object, key, &result));
6548 OH_JSVM_HasProperty(env, nullptr, key, &result);
6549 AddLastStatus(env, "nullprtObject", returnValue);
6550 OH_JSVM_HasProperty(env, object, nullptr, &result);
6551 AddLastStatus(env, "nullprtKey", returnValue);
6552 OH_JSVM_HasProperty(env, object, key, nullptr);
6553 AddLastStatus(env, "nullptrResult", returnValue);
6554 return returnValue;
6555 }
6556
DeleteProperty(JSVM_Env env,JSVM_CallbackInfo info)6557 static JSVM_Value DeleteProperty(JSVM_Env env, JSVM_CallbackInfo info)
6558 {
6559 size_t argc = 2;
6560 JSVM_Value args[2] = {nullptr};
6561 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6562
6563 JSVM_ValueType valueType;
6564 OH_JSVM_Typeof(env, args[0], &valueType);
6565 if (valueType != JSVM_OBJECT) {
6566 OH_JSVM_ThrowError(env, nullptr, "Expects an object as argument.");
6567 }
6568 bool result = false;
6569 JSVM_Status status = OH_JSVM_DeleteProperty(env, args[0], args[1], &result);
6570 if (status != JSVM_OK) {
6571 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_DeleteProperty failed");
6572 }
6573 JSVM_Value ret;
6574 OH_JSVM_GetBoolean(env, result, &ret);
6575 return ret;
6576 }
6577
DeletePropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6578 static JSVM_Value DeletePropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6579 {
6580 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6581 JSVM_Value returnValue;
6582 JSVM_Value object;
6583 OH_JSVM_CreateObject(env, &returnValue);
6584 OH_JSVM_CreateObject(env, &object);
6585 JSVM_Value key;
6586 OH_JSVM_CreateInt32(env, 0, &key);
6587 bool result;
6588 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6589 OH_JSVM_DeleteProperty(nullptr, object, key, &result));
6590 OH_JSVM_DeleteProperty(env, nullptr, key, &result);
6591 AddLastStatus(env, "nullprtObject", returnValue);
6592 OH_JSVM_DeleteProperty(env, object, nullptr, &result);
6593 AddLastStatus(env, "nullprtKey", returnValue);
6594 return returnValue;
6595 }
6596
HasOwnProperty(JSVM_Env env,JSVM_CallbackInfo info)6597 static JSVM_Value HasOwnProperty(JSVM_Env env, JSVM_CallbackInfo info)
6598 {
6599 size_t argc = 2;
6600 JSVM_Value args[2] = {nullptr};
6601 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6602 JSVM_ValueType valueType1;
6603 OH_JSVM_Typeof(env, args[0], &valueType1);
6604 if (valueType1 != JSVM_OBJECT) {
6605 OH_JSVM_ThrowError(env, nullptr, "First argument must be an object.");
6606 }
6607 JSVM_ValueType valuetype2;
6608 OH_JSVM_Typeof(env, args[1], &valuetype2);
6609 if (valuetype2 != JSVM_STRING && valuetype2 != JSVM_SYMBOL) {
6610 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6611 JSVM_Value result;
6612 OH_JSVM_CreateStringUtf8(env, "name expected status.", JSVM_AUTO_LENGTH, &result);
6613 return result;
6614 }
6615 bool hasProperty;
6616 JSVM_Status status = OH_JSVM_HasOwnProperty(env, args[0], args[1], &hasProperty);
6617 if (status != JSVM_OK) {
6618 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasOwnProperty failed");
6619 }
6620 JSVM_Value result;
6621 OH_JSVM_GetBoolean(env, hasProperty, &result);
6622 return result;
6623 }
6624
HasOwnPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6625 static JSVM_Value HasOwnPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6626 {
6627 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6628 JSVM_Value returnValue;
6629 JSVM_Value object;
6630 OH_JSVM_CreateObject(env, &returnValue);
6631 OH_JSVM_CreateObject(env, &object);
6632 JSVM_Value key;
6633 OH_JSVM_CreateInt32(env, 0, &key);
6634 bool result;
6635 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6636 OH_JSVM_HasOwnProperty(nullptr, object, key, &result));
6637 OH_JSVM_HasOwnProperty(env, nullptr, key, &result);
6638 AddLastStatus(env, "nullprtObject", returnValue);
6639 OH_JSVM_HasOwnProperty(env, object, nullptr, &result);
6640 AddLastStatus(env, "nullprtKey", returnValue);
6641 OH_JSVM_HasOwnProperty(env, object, key, nullptr);
6642 AddLastStatus(env, "nullptrResult", returnValue);
6643 return returnValue;
6644 }
6645 static const int32_t NAME_PROPERTY_NUM = 1234;
SetNamedPropertyCreateObject(JSVM_Env env,JSVM_CallbackInfo info)6646 static JSVM_Value SetNamedPropertyCreateObject(JSVM_Env env, JSVM_CallbackInfo info)
6647 {
6648 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6649 size_t argc = 1;
6650 JSVM_Value str;
6651 char strKey[32] = "";
6652 OH_JSVM_GetCbInfo(env, info, &argc, &str, nullptr, nullptr);
6653 size_t keyLength;
6654 OH_JSVM_GetValueStringUtf8(env, str, strKey, DIFF_VALUE_KEYLEN, &keyLength);
6655 JSVM_Value newObj;
6656 OH_JSVM_CreateObject(env, &newObj);
6657 int32_t value = NAME_PROPERTY_NUM;
6658 JSVM_Value numValue;
6659 OH_JSVM_CreateInt32(env, value, &numValue);
6660 JSVM_Status status = OH_JSVM_SetNamedProperty(env, newObj, strKey, numValue);
6661 if (status != JSVM_OK) {
6662 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetNamedProperty failed");
6663 }
6664 return newObj;
6665 }
6666
SetNamedProperty(JSVM_Env env,JSVM_CallbackInfo info)6667 static JSVM_Value SetNamedProperty(JSVM_Env env, JSVM_CallbackInfo info)
6668 {
6669 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6670 size_t argc = DIFF_VALUE_THREE;
6671 JSVM_Value args[3] = {nullptr};
6672 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6673 JSVM_ValueType valueType;
6674 OH_JSVM_Typeof(env, args[0], &valueType);
6675 if (valueType != JSVM_OBJECT) {
6676 OH_JSVM_ThrowError(env, nullptr, "First argument must be an object.");
6677 }
6678 char strKey[32] = "";
6679 OH_JSVM_GetValueStringUtf8(env, args[1], strKey, DIFF_VALUE_KEYLEN, nullptr);
6680 JSVM_Status status = OH_JSVM_SetNamedProperty(env, args[0], strKey, args[2]);
6681 if (status != JSVM_OK) {
6682 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetNamedProperty failed");
6683 }
6684 return args[0];
6685 }
6686
SetNamedPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6687 static JSVM_Value SetNamedPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6688 {
6689 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6690 JSVM_Value returnValue;
6691 JSVM_Value object;
6692 OH_JSVM_CreateObject(env, &returnValue);
6693 OH_JSVM_CreateObject(env, &object);
6694 char key[32] = "";
6695 JSVM_Value result = nullptr;
6696 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6697 OH_JSVM_SetNamedProperty(nullptr, object, key, result));
6698 OH_JSVM_SetNamedProperty(env, nullptr, key, result);
6699 AddLastStatus(env, "nullprtObject", returnValue);
6700 OH_JSVM_SetNamedProperty(env, object, nullptr, result);
6701 AddLastStatus(env, "nullprtKey", returnValue);
6702 OH_JSVM_SetNamedProperty(env, object, key, nullptr);
6703 AddLastStatus(env, "nullptrResult", returnValue);
6704 return returnValue;
6705 }
6706
GetNamedProperty(JSVM_Env env,JSVM_CallbackInfo info)6707 static JSVM_Value GetNamedProperty(JSVM_Env env, JSVM_CallbackInfo info)
6708 {
6709 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6710 size_t argc = 2;
6711 JSVM_Value args[2] = {nullptr};
6712 char strKey[32] = "";
6713 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6714 size_t keyLength;
6715 OH_JSVM_GetValueStringUtf8(env, args[1], strKey, DIFF_VALUE_KEYLEN, &keyLength);
6716 JSVM_Value result;
6717 JSVM_Status status = OH_JSVM_GetNamedProperty(env, args[0], strKey, &result);
6718 if (status != JSVM_OK) {
6719 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetNamedProperty failed");
6720 }
6721 GetResultType(env, result);
6722 return result;
6723 }
6724
GetNamedPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6725 static JSVM_Value GetNamedPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6726 {
6727 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6728 JSVM_Value returnValue;
6729 JSVM_Value object;
6730 OH_JSVM_CreateObject(env, &returnValue);
6731 OH_JSVM_CreateObject(env, &object);
6732 char key[32] = "";
6733 JSVM_Value result;
6734 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6735 OH_JSVM_GetNamedProperty(nullptr, object, key, &result));
6736 OH_JSVM_GetNamedProperty(env, nullptr, key, &result);
6737 AddLastStatus(env, "nullprtObject", returnValue);
6738 OH_JSVM_GetNamedProperty(env, object, nullptr, &result);
6739 AddLastStatus(env, "nullprtKey", returnValue);
6740 OH_JSVM_GetNamedProperty(env, object, key, nullptr);
6741 AddLastStatus(env, "nullptrResult", returnValue);
6742 return returnValue;
6743 }
6744
HasNamedProperty(JSVM_Env env,JSVM_CallbackInfo info)6745 static JSVM_Value HasNamedProperty(JSVM_Env env, JSVM_CallbackInfo info)
6746 {
6747 size_t argc = 2;
6748 JSVM_Value args[2] = {nullptr};
6749 char strKey[32] = "";
6750 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6751 size_t keyLength;
6752 OH_JSVM_GetValueStringUtf8(env, args[1], strKey, DIFF_VALUE_KEYLEN, &keyLength);
6753 bool hasProperty = false;
6754 JSVM_Status status = OH_JSVM_HasNamedProperty(env, args[0], strKey, &hasProperty);
6755 if (status != JSVM_OK) {
6756 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasNamedProperty failed");
6757 }
6758 JSVM_Value result;
6759 OH_JSVM_GetBoolean(env, hasProperty, &result);
6760 return result;
6761 }
6762
HasNamedPropertyFailed(JSVM_Env env,JSVM_CallbackInfo info)6763 static JSVM_Value HasNamedPropertyFailed(JSVM_Env env, JSVM_CallbackInfo info)
6764 {
6765 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6766 JSVM_Value returnValue;
6767 JSVM_Value object;
6768 OH_JSVM_CreateObject(env, &returnValue);
6769 OH_JSVM_CreateObject(env, &object);
6770 char key[32] = "";
6771 bool result;
6772 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6773 OH_JSVM_HasNamedProperty(nullptr, object, key, &result));
6774 OH_JSVM_HasNamedProperty(env, nullptr, key, &result);
6775 AddLastStatus(env, "nullprtObject", returnValue);
6776 OH_JSVM_HasNamedProperty(env, object, nullptr, &result);
6777 AddLastStatus(env, "nullprtKey", returnValue);
6778 OH_JSVM_HasNamedProperty(env, object, key, nullptr);
6779 AddLastStatus(env, "nullptrResult", returnValue);
6780 return returnValue;
6781 }
6782
SetElementWithLength(JSVM_Env env,JSVM_CallbackInfo info)6783 static JSVM_Value SetElementWithLength(JSVM_Env env, JSVM_CallbackInfo info)
6784 {
6785 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6786 size_t argc = 1;
6787 JSVM_Value args;
6788 OH_JSVM_GetCbInfo(env, info, &argc, &args, nullptr, nullptr);
6789 int32_t arrLength = 0;
6790 OH_JSVM_GetValueInt32(env, args, &arrLength);
6791 JSVM_Value result;
6792 OH_JSVM_CreateArrayWithLength(env, arrLength, &result);
6793 for (int32_t i = 0; i < arrLength; i++) {
6794 JSVM_Value value;
6795 OH_JSVM_CreateInt32(env, i + DIFF_VALUE_TWO, &value);
6796 OH_JSVM_SetElement(env, result, i, value);
6797 }
6798 return result;
6799 }
6800
SetElement(JSVM_Env env,JSVM_CallbackInfo info)6801 static JSVM_Value SetElement(JSVM_Env env, JSVM_CallbackInfo info)
6802 {
6803 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6804 size_t argc = DIFF_VALUE_THREE;
6805 JSVM_Value args[3] = {nullptr};
6806 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6807 int32_t index = 0;
6808 OH_JSVM_GetValueInt32(env, args[1], &index);
6809 OH_JSVM_SetElement(env, args[0], index, args[DIFF_VALUE_TWO]);
6810 return args[0];
6811 }
6812
SetElementFailed(JSVM_Env env,JSVM_CallbackInfo info)6813 static JSVM_Value SetElementFailed(JSVM_Env env, JSVM_CallbackInfo info)
6814 {
6815 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6816 JSVM_Value returnValue;
6817 JSVM_Value object;
6818 OH_JSVM_CreateObject(env, &returnValue);
6819 OH_JSVM_CreateObject(env, &object);
6820 JSVM_Value value = nullptr;
6821 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6822 OH_JSVM_SetElement(nullptr, object, 0, value));
6823 OH_JSVM_SetElement(env, nullptr, 0, value);
6824 AddLastStatus(env, "nullprtObject", returnValue);
6825 OH_JSVM_SetElement(env, object, 0, nullptr);
6826 AddLastStatus(env, "nullprtValue", returnValue);
6827 return returnValue;
6828 }
6829
GetElement(JSVM_Env env,JSVM_CallbackInfo info)6830 static JSVM_Value GetElement(JSVM_Env env, JSVM_CallbackInfo info)
6831 {
6832 size_t argc = 2;
6833 JSVM_Value args[2] = {nullptr};
6834 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6835 uint32_t index;
6836 OH_JSVM_GetValueUint32(env, args[1], &index);
6837 JSVM_Value result = nullptr;
6838 OH_JSVM_GetElement(env, args[0], index, &result);
6839 GetResultType(env, result);
6840 return result;
6841 }
6842
GetElementFailed(JSVM_Env env,JSVM_CallbackInfo info)6843 static JSVM_Value GetElementFailed(JSVM_Env env, JSVM_CallbackInfo info)
6844 {
6845 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6846 JSVM_Value returnValue;
6847 JSVM_Value object;
6848 OH_JSVM_CreateObject(env, &returnValue);
6849 OH_JSVM_CreateObject(env, &object);
6850 JSVM_Value value;
6851 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6852 OH_JSVM_GetElement(nullptr, object, 0, &value));
6853 OH_JSVM_GetElement(env, nullptr, 0, &value);
6854 AddLastStatus(env, "nullprtObject", returnValue);
6855 OH_JSVM_GetElement(env, object, 0, nullptr);
6856 AddLastStatus(env, "nullprtValue", returnValue);
6857 return returnValue;
6858 }
6859
HasElement(JSVM_Env env,JSVM_CallbackInfo info)6860 static JSVM_Value HasElement(JSVM_Env env, JSVM_CallbackInfo info)
6861 {
6862 size_t argc = 2;
6863 JSVM_Value args[2] = {nullptr};
6864 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6865 uint32_t index;
6866 OH_JSVM_GetValueUint32(env, args[1], &index);
6867 bool hasElement = true;
6868 OH_JSVM_HasElement(env, args[0], index, &hasElement);
6869 JSVM_Value result = nullptr;
6870 OH_JSVM_GetBoolean(env, hasElement, &result);
6871 return result;
6872 }
6873
HasElementFailed(JSVM_Env env,JSVM_CallbackInfo info)6874 static JSVM_Value HasElementFailed(JSVM_Env env, JSVM_CallbackInfo info)
6875 {
6876 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6877 JSVM_Value returnValue;
6878 JSVM_Value object;
6879 OH_JSVM_CreateObject(env, &returnValue);
6880 OH_JSVM_CreateObject(env, &object);
6881 bool result;
6882 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6883 OH_JSVM_HasElement(nullptr, object, 0, &result));
6884 OH_JSVM_HasElement(env, nullptr, 0, &result);
6885 AddLastStatus(env, "nullprtObject", returnValue);
6886 OH_JSVM_HasElement(env, object, 0, nullptr);
6887 AddLastStatus(env, "nullprtresult", returnValue);
6888 return returnValue;
6889 }
6890
DeleteElement(JSVM_Env env,JSVM_CallbackInfo info)6891 static JSVM_Value DeleteElement(JSVM_Env env, JSVM_CallbackInfo info)
6892 {
6893 size_t argc = 2;
6894 JSVM_Value args[2] = {nullptr};
6895 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6896 uint32_t index;
6897 OH_JSVM_GetValueUint32(env, args[1], &index);
6898 bool deleted = true;
6899 OH_JSVM_DeleteElement(env, args[0], index, &deleted);
6900 JSVM_Value result = nullptr;
6901 OH_JSVM_GetBoolean(env, deleted, &result);
6902 return result;
6903 }
6904
DeleteElementAndCheck(JSVM_Env env,JSVM_CallbackInfo info)6905 static JSVM_Value DeleteElementAndCheck(JSVM_Env env, JSVM_CallbackInfo info)
6906 {
6907 strcpy_s(g_dataType, sizeof(g_dataType), "array");
6908 size_t argc = 2;
6909 JSVM_Value args[2] = {nullptr};
6910 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
6911 uint32_t index;
6912 OH_JSVM_GetValueUint32(env, args[1], &index);
6913 OH_JSVM_DeleteElement(env, args[0], index, nullptr);
6914 return args[0];
6915 }
6916
DeleteElementFailed(JSVM_Env env,JSVM_CallbackInfo info)6917 static JSVM_Value DeleteElementFailed(JSVM_Env env, JSVM_CallbackInfo info)
6918 {
6919 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
6920 JSVM_Value returnValue;
6921 JSVM_Value object;
6922 OH_JSVM_CreateObject(env, &returnValue);
6923 OH_JSVM_CreateObject(env, &object);
6924 bool result;
6925 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
6926 OH_JSVM_DeleteElement(nullptr, object, 0, &result));
6927 OH_JSVM_DeleteElement(env, nullptr, 0, &result);
6928 AddLastStatus(env, "nullprtObject", returnValue);
6929 return returnValue;
6930 }
6931
6932 static const int32_t METHOD_NUM = 26;
DefineMethodPropertiesExample(JSVM_Env env,JSVM_CallbackInfo info)6933 static JSVM_Value DefineMethodPropertiesExample(JSVM_Env env, JSVM_CallbackInfo info)
6934 {
6935 JSVM_Value returnValue;
6936 OH_JSVM_CreateInt32(env, METHOD_NUM, &returnValue);
6937 return returnValue;
6938 }
DefineMethodProperties(JSVM_Env env,JSVM_CallbackInfo info)6939 static JSVM_Value DefineMethodProperties(JSVM_Env env, JSVM_CallbackInfo info)
6940 {
6941 strcpy_s(g_dataType, sizeof(g_dataType), "int");
6942 size_t argc = 1;
6943 JSVM_Value argv[1];
6944 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
6945 JSVM_CallbackStruct param[] = {
6946 {.data = nullptr, .callback = DefineMethodPropertiesExample},
6947 };
6948 JSVM_PropertyDescriptor descriptor[] = {
6949 {"defineMethodPropertiesExample", nullptr, ¶m[0], nullptr, nullptr, nullptr, JSVM_DEFAULT}
6950 };
6951 OH_JSVM_DefineProperties(env, *argv, sizeof(descriptor) / sizeof(descriptor[0]), descriptor);
6952 const char *srcMethod = R"JS(
6953 obj.defineMethodPropertiesExample();
6954 )JS";
6955 JSVM_Value sourceCodeValue = nullptr;
6956 OH_JSVM_CreateStringUtf8(env, srcMethod, strlen(srcMethod), &sourceCodeValue);
6957 JSVM_Script script;
6958 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
6959 JSVM_Value jsVmResult;
6960 OH_JSVM_RunScript(env, script, &jsVmResult);
6961 return jsVmResult;
6962 }
6963
DefineStringProperties(JSVM_Env env,JSVM_CallbackInfo info)6964 static JSVM_Value DefineStringProperties(JSVM_Env env, JSVM_CallbackInfo info)
6965 {
6966 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
6967 size_t argc = 1;
6968 JSVM_Value argv[1];
6969 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
6970 JSVM_Value stringValue;
6971 OH_JSVM_CreateStringUtf8(env, "Hello!", JSVM_AUTO_LENGTH, &stringValue);
6972 JSVM_PropertyDescriptor descriptor[] = {
6973 {"defineStringPropertiesExample", nullptr, nullptr, nullptr, nullptr, stringValue, JSVM_DEFAULT}
6974 };
6975 OH_JSVM_DefineProperties(env, *argv, sizeof(descriptor) / sizeof(descriptor[0]), descriptor);
6976 const char *srcMethod = R"JS(
6977 obj.defineStringPropertiesExample;
6978 )JS";
6979 JSVM_Value sourceCodeValue = nullptr;
6980 OH_JSVM_CreateStringUtf8(env, srcMethod, strlen(srcMethod), &sourceCodeValue);
6981 JSVM_Script script;
6982 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
6983 JSVM_Value jsVmResult;
6984 OH_JSVM_RunScript(env, script, &jsVmResult);
6985 return jsVmResult;
6986 }
6987
GetterCallback(JSVM_Env env,JSVM_CallbackInfo info)6988 static JSVM_Value GetterCallback(JSVM_Env env, JSVM_CallbackInfo info)
6989 {
6990 JSVM_Value result;
6991 const char *str = "Hello world!";
6992 size_t length = strlen(str);
6993 OH_JSVM_CreateStringUtf8(env, str, length, &result);
6994 return result;
6995 }
6996
DefineGetterProperties(JSVM_Env env,JSVM_CallbackInfo info)6997 static JSVM_Value DefineGetterProperties(JSVM_Env env, JSVM_CallbackInfo info)
6998 {
6999 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7000 size_t argc = 1;
7001 JSVM_Value argv[1];
7002 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7003 JSVM_CallbackStruct param[] = {
7004 {.data = nullptr, .callback = GetterCallback},
7005 };
7006 JSVM_PropertyDescriptor descriptor[] = {
7007 {"getterCallback", nullptr, nullptr, ¶m[0], nullptr, nullptr, JSVM_DEFAULT}
7008 };
7009 OH_JSVM_DefineProperties(env, *argv, 1, descriptor);
7010 const char *srcMethod = R"JS(
7011 obj.getterCallback;
7012 )JS";
7013 JSVM_Value sourceCodeValue = nullptr;
7014 OH_JSVM_CreateStringUtf8(env, srcMethod, strlen(srcMethod), &sourceCodeValue);
7015 JSVM_Script script;
7016 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
7017 JSVM_Value jsVmResult;
7018 OH_JSVM_RunScript(env, script, &jsVmResult);
7019 return jsVmResult;
7020 }
7021
DefinePropertiesFailed(JSVM_Env env,JSVM_CallbackInfo info)7022 static JSVM_Value DefinePropertiesFailed(JSVM_Env env, JSVM_CallbackInfo info)
7023 {
7024 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7025 JSVM_Value returnValue;
7026 JSVM_Value object;
7027 OH_JSVM_CreateObject(env, &returnValue);
7028 OH_JSVM_CreateObject(env, &object);
7029 JSVM_CallbackStruct param[] = {
7030 {.data = nullptr, .callback = DefinePropertiesFailed},
7031 };
7032 JSVM_PropertyDescriptor desc[] = {{"prop", nullptr, ¶m[0], nullptr, nullptr, nullptr, JSVM_DEFAULT}};
7033 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7034 OH_JSVM_DefineProperties(nullptr, object, 1, desc));
7035 OH_JSVM_DefineProperties(env, nullptr, 1, desc);
7036 AddLastStatus(env, "nullprtObject", returnValue);
7037 OH_JSVM_DefineProperties(env, object, 1, nullptr);
7038 AddLastStatus(env, "nullptrDescriptorList", returnValue);
7039 desc->utf8name = nullptr;
7040 OH_JSVM_DefineProperties(env, object, 1, nullptr);
7041 AddLastStatus(env, "nullptrUtf8name", returnValue);
7042 return returnValue;
7043 }
7044
ObjectFreeze(JSVM_Env env,JSVM_CallbackInfo info)7045 static JSVM_Value ObjectFreeze(JSVM_Env env, JSVM_CallbackInfo info)
7046 {
7047 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7048 size_t argc = 1;
7049 JSVM_Value argv[1];
7050 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7051 OH_JSVM_ObjectFreeze(env, argv[0]);
7052 JSVM_Value value;
7053 OH_JSVM_CreateInt32(env, 0, &value);
7054 OH_JSVM_SetNamedProperty(env, argv[0], "data", value);
7055 OH_JSVM_SetNamedProperty(env, argv[0], "newKey", value);
7056 JSVM_Value key;
7057 OH_JSVM_CreateStringUtf8(env, "message", JSVM_AUTO_LENGTH, &key);
7058 OH_JSVM_DeleteProperty(env, argv[0], key, nullptr);
7059 return argv[0];
7060 }
7061
ObjectSeal(JSVM_Env env,JSVM_CallbackInfo info)7062 static JSVM_Value ObjectSeal(JSVM_Env env, JSVM_CallbackInfo info)
7063 {
7064 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7065 size_t argc = 1;
7066 JSVM_Value argv[1];
7067 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7068 OH_JSVM_ObjectSeal(env, argv[0]);
7069
7070 JSVM_Value changeValue;
7071 OH_JSVM_CreateInt32(env, METHOD_NUM, &changeValue);
7072 OH_JSVM_SetNamedProperty(env, argv[0], "data", changeValue);
7073 JSVM_Value deleteProperty;
7074 OH_JSVM_CreateStringUtf8(env, "message", JSVM_AUTO_LENGTH, &deleteProperty);
7075 OH_JSVM_DeleteProperty(env, argv[0], deleteProperty, nullptr);
7076 JSVM_Value addValue;
7077 OH_JSVM_CreateStringUtf8(env, "addValue", JSVM_AUTO_LENGTH, &addValue);
7078 OH_JSVM_SetNamedProperty(env, argv[0], "newProperty", addValue);
7079 return argv[0];
7080 }
7081
GetVersion(JSVM_Env env,JSVM_CallbackInfo info)7082 static JSVM_Value GetVersion(JSVM_Env env, JSVM_CallbackInfo info)
7083 {
7084 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7085 uint32_t jsVersion;
7086 OH_JSVM_GetVersion(env, &jsVersion);
7087 JSVM_Value result;
7088 OH_JSVM_CreateUint32(env, jsVersion, &result);
7089 return result;
7090 }
7091
GetVersionFailed(JSVM_Env env,JSVM_CallbackInfo info)7092 static JSVM_Value GetVersionFailed(JSVM_Env env, JSVM_CallbackInfo info)
7093 {
7094 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7095 JSVM_Value returnValue;
7096 OH_JSVM_CreateObject(env, &returnValue);
7097 uint32_t jsVersion;
7098 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7099 OH_JSVM_GetVersion(nullptr, &jsVersion));
7100 OH_JSVM_GetVersion(env, nullptr);
7101 AddLastStatus(env, "nullptrResult", returnValue);
7102 return returnValue;
7103 }
7104
7105
GetVMInfo(JSVM_Env env,JSVM_CallbackInfo info)7106 static JSVM_Value GetVMInfo(JSVM_Env env, JSVM_CallbackInfo info)
7107 {
7108 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7109 JSVM_VMInfo result;
7110 OH_JSVM_GetVMInfo(&result);
7111 JSVM_Value version;
7112 OH_JSVM_CreateUint32(env, result.apiVersion, &version);
7113 return version;
7114 }
7115
JsonStringify(JSVM_Env env,JSVM_CallbackInfo info)7116 static JSVM_Value JsonStringify(JSVM_Env env, JSVM_CallbackInfo info)
7117 {
7118 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7119 size_t argc = 1;
7120 JSVM_Value argv[1] = {nullptr};
7121 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7122 JSVM_Value result;
7123 OH_JSVM_JsonStringify(env, *argv, &result);
7124 return result;
7125 }
7126
JsonStringifyFailed(JSVM_Env env,JSVM_CallbackInfo info)7127 static JSVM_Value JsonStringifyFailed(JSVM_Env env, JSVM_CallbackInfo info)
7128 {
7129 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7130 JSVM_Value returnValue;
7131 OH_JSVM_CreateObject(env, &returnValue);
7132 JSVM_Value jsonObject = nullptr;
7133 JSVM_Value ret = nullptr;
7134 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7135 OH_JSVM_JsonStringify(nullptr, jsonObject, &ret));
7136 OH_JSVM_JsonStringify(env, nullptr, &ret);
7137 AddLastStatus(env, "nullptrJsonObject", returnValue);
7138 return returnValue;
7139 }
7140
JsonParseNumber(JSVM_Env env,JSVM_CallbackInfo info)7141 static JSVM_Value JsonParseNumber(JSVM_Env env, JSVM_CallbackInfo info)
7142 {
7143 strcpy_s(g_dataType, sizeof(g_dataType), "double");
7144 const char *strNumber = "10.555";
7145 JSVM_Value jsonString = nullptr;
7146 OH_JSVM_CreateStringUtf8(env, strNumber, strlen(strNumber), &jsonString);
7147 JSVM_Value result = nullptr;
7148 OH_JSVM_JsonParse(env, jsonString, &result);
7149 return result;
7150 }
7151
JsonParseObject(JSVM_Env env,JSVM_CallbackInfo info)7152 static JSVM_Value JsonParseObject(JSVM_Env env, JSVM_CallbackInfo info)
7153 {
7154 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7155 const char *strObject = "{\"first\": \"one\", \"second\": \"two\"}";
7156 JSVM_Value strJson = nullptr;
7157 OH_JSVM_CreateStringUtf8(env, strObject, strlen(strObject), &strJson);
7158 JSVM_Value ret = nullptr;
7159 OH_JSVM_JsonParse(env, strJson, &ret);
7160 return ret;
7161 }
7162
JsonParse(JSVM_Env env,JSVM_CallbackInfo info)7163 static JSVM_Value JsonParse(JSVM_Env env, JSVM_CallbackInfo info)
7164 {
7165 size_t argc = 1;
7166 JSVM_Value argv[1] = {nullptr};
7167 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7168 JSVM_Value result = nullptr;
7169 OH_JSVM_JsonParse(env, *argv, &result);
7170 GetResultType(env, result);
7171 return result;
7172 }
7173
JsonParseFailed(JSVM_Env env,JSVM_CallbackInfo info)7174 static JSVM_Value JsonParseFailed(JSVM_Env env, JSVM_CallbackInfo info)
7175 {
7176 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7177 JSVM_Value returnValue = nullptr;
7178 OH_JSVM_CreateObject(env, &returnValue);
7179 const char *str = "";
7180 JSVM_Value strJson = nullptr;
7181 OH_JSVM_CreateStringUtf8(env, str, strlen(str), &strJson);
7182 JSVM_Value ret = nullptr;
7183 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7184 OH_JSVM_JsonParse(nullptr, strJson, &ret));
7185 OH_JSVM_JsonParse(env, nullptr, &ret);
7186 AddLastStatus(env, "nullptrJsonString", returnValue);
7187 return returnValue;
7188 }
7189
CallFunction(JSVM_Env env,JSVM_CallbackInfo info)7190 static JSVM_Value CallFunction(JSVM_Env env, JSVM_CallbackInfo info)
7191 {
7192 size_t argc = 1;
7193 JSVM_Value args[1] = {nullptr};
7194 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7195 JSVM_Value global = nullptr;
7196 OH_JSVM_GetGlobal(env, &global);
7197 JSVM_Value result = nullptr;
7198 OH_JSVM_CallFunction(env, global, args[0], 0, nullptr, &result);
7199 GetResultType(env, result);
7200 return result;
7201 }
7202
CallFunctionWithArg(JSVM_Env env,JSVM_CallbackInfo info)7203 static JSVM_Value CallFunctionWithArg(JSVM_Env env, JSVM_CallbackInfo info)
7204 {
7205 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7206 size_t argc = 1;
7207 JSVM_Value args[1] = {nullptr};
7208 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7209 JSVM_Value global = nullptr;
7210 OH_JSVM_GetGlobal(env, &global);
7211 JSVM_Value arg[2] = {nullptr};
7212 OH_JSVM_CreateStringUtf8(env, "Hello", JSVM_AUTO_LENGTH, &arg[0]);
7213 OH_JSVM_CreateStringUtf8(env, " World!", JSVM_AUTO_LENGTH, &arg[1]);
7214 JSVM_Value result = nullptr;
7215 OH_JSVM_CallFunction(env, global, args[0], DIFF_VALUE_TWO, arg, &result);
7216 return result;
7217 }
7218
CallFunctionWithObject(JSVM_Env env,JSVM_CallbackInfo info)7219 static JSVM_Value CallFunctionWithObject(JSVM_Env env, JSVM_CallbackInfo info)
7220 {
7221 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7222 size_t argc = 1;
7223 JSVM_Value args[1] = {nullptr};
7224 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7225 JSVM_Value func = nullptr;
7226 OH_JSVM_GetNamedProperty(env, args[0], "myMethod", &func);
7227 JSVM_Value global = nullptr;
7228 OH_JSVM_GetGlobal(env, &global);
7229 JSVM_Value arg[2] = {nullptr};
7230 OH_JSVM_CreateInt32(env, METHOD_NUM, &arg[0]);
7231 OH_JSVM_CreateInt32(env, METHOD_NUM, &arg[1]);
7232 JSVM_Value result = nullptr;
7233 OH_JSVM_CallFunction(env, global, func, DIFF_VALUE_TWO, arg, &result);
7234 return result;
7235 }
7236
CallFunctionFailed(JSVM_Env env,JSVM_CallbackInfo info)7237 static JSVM_Value CallFunctionFailed(JSVM_Env env, JSVM_CallbackInfo info)
7238 {
7239 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7240 size_t argc = 1;
7241 JSVM_Value args[1] = {nullptr};
7242 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7243 JSVM_Value returnValue = nullptr;
7244 OH_JSVM_CreateObject(env, &returnValue);
7245 JSVM_Value global = nullptr;
7246 OH_JSVM_GetGlobal(env, &global);
7247 JSVM_Value result = nullptr;
7248 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7249 OH_JSVM_CallFunction(nullptr, global, args[0], 0, nullptr, &result));
7250 OH_JSVM_CallFunction(env, nullptr, args[0], 0, nullptr, &result);
7251 AddLastStatus(env, "nullprtGlobal", returnValue);
7252 OH_JSVM_CallFunction(env, global, args[0], 1, nullptr, &result);
7253 AddLastStatus(env, "nullprtArgv", returnValue);
7254 OH_JSVM_CallFunction(env, global, nullptr, 0, nullptr, &result);
7255 AddLastStatus(env, "nullptrFunc", returnValue);
7256 return returnValue;
7257 }
7258
CalculateArea(JSVM_Env env,JSVM_CallbackInfo info)7259 static JSVM_Value CalculateArea(JSVM_Env env, JSVM_CallbackInfo info)
7260 {
7261 size_t argc = 2;
7262 JSVM_Value args[2] = {nullptr};
7263 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7264
7265 double width;
7266 OH_JSVM_GetValueDouble(env, args[0], &width);
7267 double height;
7268 OH_JSVM_GetValueDouble(env, args[1], &height);
7269 JSVM_Value area;
7270 OH_JSVM_CreateDouble(env, width * height, &area);
7271 return area;
7272 }
7273 static double g_width = 1.4;
7274 static double g_height = 5.0;
CreateFunction(JSVM_Env env,JSVM_CallbackInfo info)7275 static JSVM_Value CreateFunction(JSVM_Env env, JSVM_CallbackInfo info)
7276 {
7277 strcpy_s(g_dataType, sizeof(g_dataType), "double");
7278 JSVM_CallbackStruct param;
7279 param.data = nullptr;
7280 param.callback = CalculateArea;
7281 JSVM_Value funcValue = nullptr;
7282 JSVM_Status status = OH_JSVM_CreateFunction(env, "calculateArea", JSVM_AUTO_LENGTH, ¶m, &funcValue);
7283 if (funcValue == nullptr || status != JSVM_OK) {
7284 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CreateFunction failed");
7285 }
7286 JSVM_Value args[2] = {nullptr};
7287 OH_JSVM_CreateDouble(env, g_width, &args[0]);
7288 OH_JSVM_CreateDouble(env, g_height, &args[1]);
7289 JSVM_Value global = nullptr;
7290 OH_JSVM_GetGlobal(env, &global);
7291 JSVM_Value ret = nullptr;
7292 OH_JSVM_CallFunction(env, global, funcValue, DIFF_VALUE_TWO, args, &ret);
7293 return ret;
7294 }
7295
CreateFunctionFailed(JSVM_Env env,JSVM_CallbackInfo info)7296 static JSVM_Value CreateFunctionFailed(JSVM_Env env, JSVM_CallbackInfo info)
7297 {
7298 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7299 size_t argc = 1;
7300 JSVM_Value args[1] = {nullptr};
7301 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7302 JSVM_Value returnValue = nullptr;
7303 OH_JSVM_CreateObject(env, &returnValue);
7304 JSVM_CallbackStruct param;
7305 param.data = nullptr;
7306 param.callback = CalculateArea;
7307 JSVM_Value result = nullptr;
7308 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
7309 OH_JSVM_CreateFunction(nullptr, "myFunc", JSVM_AUTO_LENGTH, ¶m, &result));
7310 OH_JSVM_CreateFunction(env, "myFunc", JSVM_AUTO_LENGTH, nullptr, &result);
7311 AddLastStatus(env, "nullprtCD", returnValue);
7312 OH_JSVM_CreateFunction(env, "myFunc", JSVM_AUTO_LENGTH, ¶m, nullptr);
7313 AddLastStatus(env, "nullptrResult", returnValue);
7314 return returnValue;
7315 }
7316
CreateFunctionWithScript(JSVM_Env env,JSVM_CallbackInfo info)7317 static JSVM_Value CreateFunctionWithScript(JSVM_Env env, JSVM_CallbackInfo info)
7318 {
7319 size_t argc = 3;
7320 JSVM_Value argv[3] = { nullptr};
7321 JSVM_Value thisArg = nullptr;
7322 OH_JSVM_GetCbInfo(env, info, &argc, argv, &thisArg, nullptr);
7323
7324 uint32_t arrayLen = 0;
7325 JSVM_CALL(env, OH_JSVM_GetArrayLength(env, argv[1], &arrayLen));
7326 if (arrayLen <= 0 || arrayLen > std::numeric_limits<uint32_t>::max() / sizeof(JSVM_Value)) {
7327 OH_JSVM_ThrowError(env, nullptr, "Invalid array length.");
7328 return nullptr;
7329 } else {
7330 JSVM_Value *args = new JSVM_Value[arrayLen];
7331 for (auto i = 0; i < arrayLen; i++) {
7332 JSVM_CALL(env, OH_JSVM_GetElement(env, argv[1], i, &args[i]));
7333 }
7334 JSVM_Value js_string;
7335 JSVM_CALL(env, OH_JSVM_CoerceToString(env, argv[0], &js_string));
7336 size_t length = 0;
7337 JSVM_CALL(env, OH_JSVM_GetValueStringUtf8(env, js_string, NULL, 0, &length));
7338 size_t capacity = length + 1;
7339 char *buffer = new char[capacity];
7340 size_t copyLength = 0;
7341 JSVM_CALL(env, OH_JSVM_GetValueStringUtf8(env, js_string, buffer, capacity, ©Length));
7342
7343 JSVM_Value func = nullptr;
7344 JSVM_CALL(env, OH_JSVM_CreateFunctionWithScript(env, buffer, JSVM_AUTO_LENGTH, arrayLen,
7345 args, argv[ argc - 1 ], &func));
7346 return func;
7347 }
7348 }
7349
GetHeapStatistics(JSVM_Env env,JSVM_CallbackInfo info)7350 static JSVM_Value GetHeapStatistics(JSVM_Env env, JSVM_CallbackInfo info)
7351 {
7352 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7353 JSVM_VM testVm;
7354 OH_JSVM_GetVM(env, &testVm);
7355 JSVM_HeapStatistics result;
7356 OH_JSVM_GetHeapStatistics(testVm, &result);
7357 JSVM_Value nativeContextsCnt = nullptr;
7358 OH_JSVM_CreateInt64(env, result.numberOfNativeContexts, &nativeContextsCnt);
7359 return nativeContextsCnt;
7360 }
7361
GetVM(JSVM_Env env,JSVM_CallbackInfo info)7362 static JSVM_Value GetVM(JSVM_Env env, JSVM_CallbackInfo info)
7363 {
7364 JSVM_VM testVm;
7365 JSVM_Status status = OH_JSVM_GetVM(env, &testVm);
7366 if (status != JSVM_OK) {
7367 OH_JSVM_ThrowError(env, nullptr, "Test JSVM OH_JSVM_GetVM failed");
7368 }
7369 JSVM_Value result;
7370 OH_JSVM_GetBoolean(env, true, &result);
7371 return result;
7372 }
7373
GetTypeof(JSVM_Env env,JSVM_CallbackInfo info)7374 static JSVM_Value GetTypeof(JSVM_Env env, JSVM_CallbackInfo info)
7375 {
7376 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7377 size_t argc = 1;
7378 JSVM_Value args[1] = {nullptr};
7379 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7380 JSVM_ValueType valueType;
7381 OH_JSVM_Typeof(env, args[0], &valueType);
7382 JSVM_Value type = nullptr;
7383 switch (valueType) {
7384 case JSVM_UNDEFINED:
7385 OH_JSVM_CreateStringUtf8(env, "undefined", JSVM_AUTO_LENGTH, &type);
7386 break;
7387 case JSVM_NULL:
7388 OH_JSVM_CreateStringUtf8(env, "null", JSVM_AUTO_LENGTH, &type);
7389 break;
7390 case JSVM_BOOLEAN:
7391 OH_JSVM_CreateStringUtf8(env, "boolean", JSVM_AUTO_LENGTH, &type);
7392 break;
7393 case JSVM_NUMBER:
7394 OH_JSVM_CreateStringUtf8(env, "number", JSVM_AUTO_LENGTH, &type);
7395 break;
7396 case JSVM_STRING:
7397 OH_JSVM_CreateStringUtf8(env, "string", JSVM_AUTO_LENGTH, &type);
7398 break;
7399 case JSVM_OBJECT:
7400 OH_JSVM_CreateStringUtf8(env, "object", JSVM_AUTO_LENGTH, &type);
7401 break;
7402 case JSVM_FUNCTION:
7403 OH_JSVM_CreateStringUtf8(env, "function", JSVM_AUTO_LENGTH, &type);
7404 break;
7405 case JSVM_BIGINT:
7406 OH_JSVM_CreateStringUtf8(env, "bigint", JSVM_AUTO_LENGTH, &type);
7407 break;
7408 default:
7409 OH_JSVM_CreateStringUtf8(env, " ", NAPI_AUTO_LENGTH, &type);
7410 break;
7411 }
7412 return type;
7413 }
7414
InstanceOf(JSVM_Env env,JSVM_CallbackInfo info)7415 static JSVM_Value InstanceOf(JSVM_Env env, JSVM_CallbackInfo info)
7416 {
7417 size_t argc = 2;
7418 JSVM_Value args[2] = {nullptr};
7419 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7420 bool result = false;
7421 OH_JSVM_Instanceof(env, args[0], args[1], &result);
7422 JSVM_Value returnValue = nullptr;
7423 OH_JSVM_GetBoolean(env, result, &returnValue);
7424 return returnValue;
7425 }
7426
IsArray(JSVM_Env env,JSVM_CallbackInfo info)7427 static JSVM_Value IsArray(JSVM_Env env, JSVM_CallbackInfo info)
7428 {
7429 size_t argc = 1;
7430 JSVM_Value args[1] = {nullptr};
7431 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7432 bool result = false;
7433 OH_JSVM_IsArray(env, args[0], &result);
7434 JSVM_Value returnValue = nullptr;
7435 OH_JSVM_GetBoolean(env, result, &returnValue);
7436 return returnValue;
7437 }
7438
IsArrayBuffer(JSVM_Env env,JSVM_CallbackInfo info)7439 static JSVM_Value IsArrayBuffer(JSVM_Env env, JSVM_CallbackInfo info)
7440 {
7441 size_t argc = 1;
7442 JSVM_Value args[1] = {nullptr};
7443 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7444 bool isArrayBuffer = false;
7445 JSVM_Status status = OH_JSVM_IsArraybuffer(env, args[0], &isArrayBuffer);
7446 if (status != JSVM_OK) {
7447 return nullptr;
7448 }
7449 JSVM_Value boolean = nullptr;
7450 OH_JSVM_GetBoolean(env, isArrayBuffer, &boolean);
7451 return boolean;
7452 }
7453
IsTypedarray(JSVM_Env env,JSVM_CallbackInfo info)7454 static JSVM_Value IsTypedarray(JSVM_Env env, JSVM_CallbackInfo info)
7455 {
7456 size_t argc = 1;
7457 JSVM_Value args[1] = {nullptr};
7458 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7459 bool result = false;
7460 OH_JSVM_IsTypedarray(env, args[0], &result);
7461 JSVM_Value isTypedArray = nullptr;
7462 OH_JSVM_GetBoolean(env, result, &isTypedArray);
7463 return isTypedArray;
7464 }
7465
IsDataView(JSVM_Env env,JSVM_CallbackInfo info)7466 static JSVM_Value IsDataView(JSVM_Env env, JSVM_CallbackInfo info)
7467 {
7468 size_t argc = 1;
7469 JSVM_Value args[1] = {nullptr};
7470 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7471 bool result = false;
7472 OH_JSVM_IsDataview(env, args[0], &result);
7473 JSVM_Value isDateView = nullptr;
7474 OH_JSVM_GetBoolean(env, result, &isDateView);
7475 return isDateView;
7476 }
7477
IsStrictEquals(JSVM_Env env,JSVM_CallbackInfo info)7478 static JSVM_Value IsStrictEquals(JSVM_Env env, JSVM_CallbackInfo info)
7479 {
7480 size_t argc = 2;
7481 JSVM_Value args[2] = {nullptr};
7482 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7483 bool result = false;
7484 OH_JSVM_StrictEquals(env, args[0], args[1], &result);
7485 JSVM_Value isStrictEqual = nullptr;
7486 OH_JSVM_GetBoolean(env, result, &isStrictEqual);
7487 return isStrictEqual;
7488 }
7489
DetachArraybuffer(JSVM_Env env,JSVM_CallbackInfo info)7490 static JSVM_Value DetachArraybuffer(JSVM_Env env, JSVM_CallbackInfo info)
7491 {
7492 strcpy_s(g_dataType, sizeof(g_dataType), "arraybuffer");
7493 size_t argc = 1;
7494 JSVM_Value args[1] = {nullptr};
7495 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7496 OH_JSVM_DetachArraybuffer(env, args[0]);
7497 return args[0];
7498 }
7499
IsDetachedArraybuffer(JSVM_Env env,JSVM_CallbackInfo info)7500 static JSVM_Value IsDetachedArraybuffer(JSVM_Env env, JSVM_CallbackInfo info)
7501 {
7502 strcpy_s(g_dataType, sizeof(g_dataType), "arraybuffer");
7503 size_t argc = 1;
7504 JSVM_Value args[1] = {nullptr};
7505 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7506 OH_JSVM_DetachArraybuffer(env, args[0]);
7507 bool result = false;
7508 OH_JSVM_IsDetachedArraybuffer(env, args[0], &result);
7509 JSVM_Value isDetached = nullptr;
7510 OH_JSVM_GetBoolean(env, result, &isDetached);
7511 return isDetached;
7512 }
7513
IsRegExp(JSVM_Env env,JSVM_CallbackInfo info)7514 static JSVM_Value IsRegExp(JSVM_Env env, JSVM_CallbackInfo info)
7515 {
7516 size_t argc = 1;
7517 JSVM_Value args[1] = {nullptr};
7518 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7519 bool result = false;
7520 OH_JSVM_IsRegExp(env, args[0], &result);
7521 JSVM_Value isRegExp = nullptr;
7522 OH_JSVM_GetBoolean(env, result, &isRegExp);
7523 return isRegExp;
7524 }
7525
CoerceToBigInt(JSVM_Env env,JSVM_CallbackInfo info)7526 static JSVM_Value CoerceToBigInt(JSVM_Env env, JSVM_CallbackInfo info)
7527 {
7528 size_t argc = 1;
7529 JSVM_Value args[1] = {nullptr};
7530 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7531 JSVM_Value bigInt;
7532 OH_JSVM_GetUndefined(env, &bigInt);
7533 JSVM_Status status = OH_JSVM_CoerceToBigInt(env, args[0], &bigInt);
7534 if (status != JSVM_OK) {
7535 OH_JSVM_ThrowError(env, nullptr, "Failed to coerce to bigint");
7536 }
7537 return bigInt;
7538 }
7539
CoerceToNumber(JSVM_Env env,JSVM_CallbackInfo info)7540 static JSVM_Value CoerceToNumber(JSVM_Env env, JSVM_CallbackInfo info)
7541 {
7542 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7543 size_t argc = 1;
7544 JSVM_Value args[1] = {nullptr};
7545 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7546 JSVM_Value number = nullptr;
7547 OH_JSVM_CoerceToNumber(env, args[0], &number);
7548 return number;
7549 }
7550
CoerceToObject(JSVM_Env env,JSVM_CallbackInfo info)7551 static JSVM_Value CoerceToObject(JSVM_Env env, JSVM_CallbackInfo info)
7552 {
7553 strcpy_s(g_dataType, sizeof(g_dataType), "object");
7554 size_t argc = 1;
7555 JSVM_Value args[1] = {nullptr};
7556 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7557 JSVM_Value obj = nullptr;
7558 JSVM_Status status = OH_JSVM_CoerceToObject(env, args[0], &obj);
7559 if (status != JSVM_OK) {
7560 OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CoerceToObject fail");
7561 }
7562 bool isCoerce = true;
7563 JSVM_Value result;
7564 OH_JSVM_GetBoolean(env, isCoerce, &result);
7565 return result;
7566 }
7567
CoerceToString(JSVM_Env env,JSVM_CallbackInfo info)7568 static JSVM_Value CoerceToString(JSVM_Env env, JSVM_CallbackInfo info)
7569 {
7570 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7571 size_t argc = 1;
7572 JSVM_Value args[1] = {nullptr};
7573 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7574 JSVM_Value str = nullptr;
7575 OH_JSVM_CoerceToString(env, args[0], &str);
7576 return str;
7577 }
7578
CoerceToBool(JSVM_Env env,JSVM_CallbackInfo info)7579 static JSVM_Value CoerceToBool(JSVM_Env env, JSVM_CallbackInfo info)
7580 {
7581 size_t argc = 1;
7582 JSVM_Value args[1] = {nullptr};
7583 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7584 JSVM_Value boolean = nullptr;
7585 OH_JSVM_CoerceToBool(env, args[0], &boolean);
7586 return boolean;
7587 }
7588
IsPromise(JSVM_Env env,JSVM_CallbackInfo info)7589 static JSVM_Value IsPromise(JSVM_Env env, JSVM_CallbackInfo info)
7590 {
7591 size_t argc = 1;
7592 JSVM_Value args[1] = {nullptr};
7593 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7594 bool isPromise = false;
7595 OH_JSVM_IsPromise(env, args[0], &isPromise);
7596 JSVM_Value result = nullptr;
7597 OH_JSVM_GetBoolean(env, isPromise, &result);
7598 return result;
7599 }
7600
CreatePromise(JSVM_Env env,JSVM_CallbackInfo info)7601 static JSVM_Value CreatePromise(JSVM_Env env, JSVM_CallbackInfo info)
7602 {
7603 JSVM_Deferred defer;
7604 JSVM_Value promise = nullptr;
7605 OH_JSVM_CreatePromise(env, &defer, &promise);
7606 bool isPromise = false;
7607 JSVM_Value returnIsPromise = nullptr;
7608 OH_JSVM_IsPromise(env, promise, &isPromise);
7609 OH_JSVM_GetBoolean(env, isPromise, &returnIsPromise);
7610 return returnIsPromise;
7611 }
7612
ResolveRejectDeferred(JSVM_Env env,JSVM_CallbackInfo info)7613 static JSVM_Value ResolveRejectDeferred(JSVM_Env env, JSVM_CallbackInfo info)
7614 {
7615 size_t argc = DIFF_VALUE_THREE;
7616 JSVM_Value args[3] = {nullptr};
7617 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7618 bool status = false;
7619 OH_JSVM_GetValueBool(env, args[DIFF_VALUE_TWO], &status);
7620
7621 JSVM_Deferred deferred = nullptr;
7622 JSVM_Value promise = nullptr;
7623 JSVM_Status createStatus = OH_JSVM_CreatePromise(env, &deferred, &promise);
7624 if (createStatus != JSVM_OK) {
7625 OH_JSVM_ThrowError(env, nullptr, "Create promise failed");
7626 }
7627 if (status) {
7628 OH_JSVM_ResolveDeferred(env, deferred, args[0]);
7629 } else {
7630 OH_JSVM_RejectDeferred(env, deferred, args[1]);
7631 }
7632 JSVM_Value result = nullptr;
7633 OH_JSVM_GetBoolean(env, true, &result);
7634 return result;
7635 }
7636
GetCbArgs(JSVM_Env env,JSVM_CallbackInfo info)7637 static JSVM_Value GetCbArgs(JSVM_Env env, JSVM_CallbackInfo info)
7638 {
7639 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
7640 size_t argc = 1;
7641 JSVM_Value args[1] = {nullptr};
7642 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7643 return args[0];
7644 }
7645
NewInstance(JSVM_Env env,JSVM_CallbackInfo info)7646 static JSVM_Value NewInstance(JSVM_Env env, JSVM_CallbackInfo info)
7647 {
7648 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7649 size_t argc = 2;
7650 JSVM_Value args[2] = {nullptr};
7651 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7652 JSVM_Value result = nullptr;
7653 OH_JSVM_NewInstance(env, args[0], 1, &args[1], &result);
7654 return result;
7655 }
7656
7657 #define NUMBERINT_FOUR 4
7658 static const JSVM_TypeTag TagsData[NUMBERINT_FOUR] = {
7659 {0x9e4b2449547061b3, 0x33999f8a6516c499},
7660 {0x1d55a794c53a726d, 0x43633f509f9c944e},
7661 {0, 0},
7662 {0x6a971439f5b2e5d7, 0x531dc28a7e5317c0},
7663 };
7664
SetTypeTagToObject(JSVM_Env env,JSVM_CallbackInfo info)7665 static JSVM_Value SetTypeTagToObject(JSVM_Env env, JSVM_CallbackInfo info)
7666 {
7667 size_t argc = 2;
7668 JSVM_Value args[2] = {nullptr};
7669 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7670 int32_t index = 0;
7671 OH_JSVM_GetValueInt32(env, args[1], &index);
7672
7673 JSVM_Status status = OH_JSVM_TypeTagObject(env, args[0], &TagsData[index]);
7674 if (status != JSVM_OK) {
7675 OH_JSVM_ThrowError(env, "Reconnect error", "OH_JSVM_TypeTagObject failed");
7676 }
7677 JSVM_Value result = nullptr;
7678 OH_JSVM_GetBoolean(env, true, &result);
7679 return result;
7680 }
7681
CheckObjectTypeTag(JSVM_Env env,JSVM_CallbackInfo info)7682 static JSVM_Value CheckObjectTypeTag(JSVM_Env env, JSVM_CallbackInfo info)
7683 {
7684 size_t argc = 2;
7685 JSVM_Value args[2] = {nullptr};
7686 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7687 int32_t index = 0;
7688 OH_JSVM_GetValueInt32(env, args[1], &index);
7689 bool checkResult = false;
7690 OH_JSVM_CheckObjectTypeTag(env, args[0], &TagsData[index], &checkResult);
7691 JSVM_Value checked = nullptr;
7692 OH_JSVM_GetBoolean(env, checkResult, &checked);
7693 return checked;
7694 }
7695
7696 static const int64_t MEMORY_SIZE = 1024;
AdjustExternalMemory(JSVM_Env env,JSVM_CallbackInfo info)7697 static JSVM_Value AdjustExternalMemory(JSVM_Env env, JSVM_CallbackInfo info)
7698 {
7699 int64_t change = MEMORY_SIZE * MEMORY_SIZE;
7700 int64_t adjustedValue;
7701 OH_JSVM_AdjustExternalMemory(env, change, &adjustedValue);
7702 JSVM_Value checked = nullptr;
7703 OH_JSVM_GetBoolean(env, true, &checked);
7704 return checked;
7705 }
7706
MemoryPressureNotification(JSVM_Env env,JSVM_CallbackInfo info)7707 static JSVM_Value MemoryPressureNotification(JSVM_Env env, JSVM_CallbackInfo info)
7708 {
7709 strcpy_s(g_dataType, sizeof(g_dataType), "int");
7710 OH_JSVM_MemoryPressureNotification(env, JSVM_MEMORY_PRESSURE_LEVEL_CRITICAL);
7711 JSVM_Value level = nullptr;
7712 OH_JSVM_CreateInt32(env, 0, &level);
7713 return level;
7714 }
7715
JsVmThrow(JSVM_Env env,JSVM_CallbackInfo info)7716 static JSVM_Value JsVmThrow(JSVM_Env env, JSVM_CallbackInfo info)
7717 {
7718 size_t argc = 1;
7719 JSVM_Value argv[1] = {nullptr};
7720 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7721 JSVM_Value errorCode = nullptr;
7722 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7723 JSVM_Value errorMessage = nullptr;
7724 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7725 JSVM_Value error = nullptr;
7726 OH_JSVM_CreateError(env, errorCode, errorMessage, &error);
7727 OH_JSVM_Throw(env, error);
7728 return nullptr;
7729 }
7730
JsVmThrowFromJS(JSVM_Env env,JSVM_CallbackInfo info)7731 static JSVM_Value JsVmThrowFromJS(JSVM_Env env, JSVM_CallbackInfo info)
7732 {
7733 size_t argc = 1;
7734 JSVM_Value argv[1];
7735 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7736 JSVM_Value jsError = argv[0];
7737 bool result = false;
7738 OH_JSVM_IsError(env, jsError, &result);
7739 if (!result) {
7740 JSVM_Value errorCode;
7741 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7742 JSVM_Value errorMessage;
7743 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7744 OH_JSVM_CreateError(env, errorCode, errorMessage, &jsError);
7745 }
7746 OH_JSVM_Throw(env, jsError);
7747 return nullptr;
7748 }
7749
JsVmThrowError(JSVM_Env env,JSVM_CallbackInfo info)7750 static JSVM_Value JsVmThrowError(JSVM_Env env, JSVM_CallbackInfo info)
7751 {
7752 size_t argc = 1;
7753 JSVM_Value argv[1];
7754 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7755 if (argc == 0) {
7756 OH_JSVM_ThrowError(env, "-1", "has Error");
7757 } else if (argc == 1) {
7758 size_t length;
7759 OH_JSVM_GetValueStringUtf8(env, argv[0], nullptr, 0, &length);
7760 char *buffer = new char[length + 1];
7761 OH_JSVM_GetValueStringUtf8(env, argv[0], buffer, length + 1, nullptr);
7762 OH_JSVM_ThrowError(env, nullptr, buffer);
7763 delete[] buffer;
7764 }
7765 return nullptr;
7766 }
7767
JsVmThrowTypeError(JSVM_Env env,JSVM_CallbackInfo info)7768 static JSVM_Value JsVmThrowTypeError(JSVM_Env env, JSVM_CallbackInfo info)
7769 {
7770 size_t argc = 1;
7771 JSVM_Value argv[1];
7772 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7773 if (argc == 0) {
7774 OH_JSVM_ThrowTypeError(env, "-1", "throwing type error");
7775 } else if (argc == 1) {
7776 size_t length;
7777 OH_JSVM_GetValueStringUtf8(env, argv[0], nullptr, 0, &length);
7778 char *buffer = new char[length + 1];
7779 OH_JSVM_GetValueStringUtf8(env, argv[0], buffer, length + 1, nullptr);
7780 OH_JSVM_ThrowTypeError(env, nullptr, buffer);
7781 delete[] buffer;
7782 }
7783 return nullptr;
7784 }
7785
JsVmThrowRangeError(JSVM_Env env,JSVM_CallbackInfo info)7786 static JSVM_Value JsVmThrowRangeError(JSVM_Env env, JSVM_CallbackInfo info)
7787 {
7788 size_t argc = 2;
7789 JSVM_Value argv[2];
7790 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
7791 if (argc != DIFF_VALUE_TWO) {
7792 OH_JSVM_ThrowRangeError(env, "OH_JSVM_ThrowRangeError", "Expected two numbers as arguments");
7793 }
7794 JSVM_Value result;
7795 OH_JSVM_GetBoolean(env, true, &result);
7796 return result;
7797 }
7798
JsVmThrowSyntaxError(JSVM_Env env,JSVM_CallbackInfo info)7799 static JSVM_Value JsVmThrowSyntaxError(JSVM_Env env, JSVM_CallbackInfo info)
7800 {
7801 OH_JSVM_ThrowSyntaxError(env, "JsVmThrowSyntaxError", "throw syntax error");
7802 return nullptr;
7803 }
7804
JsVmIsError(JSVM_Env env,JSVM_CallbackInfo info)7805 static JSVM_Value JsVmIsError(JSVM_Env env, JSVM_CallbackInfo info)
7806 {
7807 size_t argc = 1;
7808 JSVM_Value args[1] = {nullptr};
7809 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7810 bool result = false;
7811 OH_JSVM_IsError(env, args[0], &result);
7812 JSVM_Value returnValue;
7813 OH_JSVM_GetBoolean(env, result, &returnValue);
7814 return returnValue;
7815 }
7816
JsVmCreateError(JSVM_Env env,JSVM_CallbackInfo info)7817 static JSVM_Value JsVmCreateError(JSVM_Env env, JSVM_CallbackInfo info)
7818 {
7819 JSVM_Value errorCode;
7820
7821 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7822 JSVM_Value errorMessage;
7823 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7824 JSVM_Value result;
7825 OH_JSVM_CreateError(env, errorCode, errorMessage, &result);
7826 return result;
7827 }
7828
JsVmCreateTypeError(JSVM_Env env,JSVM_CallbackInfo info)7829 static JSVM_Value JsVmCreateTypeError(JSVM_Env env, JSVM_CallbackInfo info)
7830 {
7831 JSVM_Value errorCode;
7832 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7833 JSVM_Value errorMessage;
7834 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7835 JSVM_Value result;
7836 OH_JSVM_CreateTypeError(env, errorCode, errorMessage, &result);
7837 return result;
7838 }
7839
JsVmCreateRangeError(JSVM_Env env,JSVM_CallbackInfo info)7840 static JSVM_Value JsVmCreateRangeError(JSVM_Env env, JSVM_CallbackInfo info)
7841 {
7842 JSVM_Value errorCode;
7843 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7844 JSVM_Value errorMessage;
7845 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7846 JSVM_Value result;
7847 OH_JSVM_CreateRangeError(env, errorCode, errorMessage, &result);
7848 return result;
7849 }
7850
JsVmCreateSyntaxError(JSVM_Env env,JSVM_CallbackInfo info)7851 static JSVM_Value JsVmCreateSyntaxError(JSVM_Env env, JSVM_CallbackInfo info)
7852 {
7853 JSVM_Value errorCode;
7854 OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
7855 JSVM_Value errorMessage;
7856 OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
7857 JSVM_Value result;
7858 OH_JSVM_CreateSyntaxError(env, errorCode, errorMessage, &result);
7859 return result;
7860 }
7861
JsVmGetAndClearLastException(JSVM_Env env,JSVM_CallbackInfo info)7862 static JSVM_Value JsVmGetAndClearLastException(JSVM_Env env, JSVM_CallbackInfo info)
7863 {
7864 OH_JSVM_ThrowError(env, "OH_JSVM_ThrowError", "OH_JSVM_ThrowError");
7865 JSVM_Value result = nullptr;
7866 JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result);
7867 if (status != JSVM_OK) {
7868 return nullptr;
7869 }
7870 return result;
7871 }
7872
JsVmIsExceptionPending(JSVM_Env env,JSVM_CallbackInfo info)7873 static JSVM_Value JsVmIsExceptionPending(JSVM_Env env, JSVM_CallbackInfo info)
7874 {
7875 JSVM_Status status;
7876 bool isExceptionPending = false;
7877 OH_JSVM_ThrowError(env, "OH_JSVM_ThrowError", "OH_JSVM_ThrowError");
7878 status = OH_JSVM_IsExceptionPending(env, &isExceptionPending);
7879 if (status != JSVM_OK) {
7880 return nullptr;
7881 }
7882 if (isExceptionPending) {
7883 JSVM_Value result = nullptr;
7884 status = OH_JSVM_GetAndClearLastException(env, &result);
7885 if (status != JSVM_OK) {
7886 return nullptr;
7887 }
7888 return result;
7889 }
7890 return nullptr;
7891 }
7892
JsVmGetLastErrorInfo(JSVM_Env env,JSVM_CallbackInfo info)7893 static JSVM_Value JsVmGetLastErrorInfo(JSVM_Env env, JSVM_CallbackInfo info)
7894 {
7895 size_t argc = 1;
7896 JSVM_Value args[1] = {nullptr};
7897 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
7898 int32_t value = 0;
7899 JSVM_Status status = OH_JSVM_GetValueInt32(env, args[0], &value);
7900 JSVM_ASSERT(env, status != JSVM_Status::JSVM_OK, "FAILED to produce error condition");
7901 const JSVM_ExtendedErrorInfo *errorInfo;
7902 OH_JSVM_GetLastErrorInfo(env, &errorInfo);
7903 JSVM_Value result;
7904 OH_JSVM_CreateInt32(env, DIFF_VALUE_NOE, &result);
7905 return result;
7906 }
7907
7908 struct Object {
7909 char name[DIFF_VALUE_SIX];
7910 int32_t age;
7911 };
7912
7913 struct Object *g_obj = (struct Object *)malloc(sizeof(struct Object));
7914
DerekItem(JSVM_Env env,void * data,void * hint)7915 static void DerekItem(JSVM_Env env, void *data, void *hint)
7916 {
7917 (void)hint;
7918 }
7919
WrapObject(JSVM_Env env,JSVM_CallbackInfo info)7920 static JSVM_Value WrapObject(JSVM_Env env, JSVM_CallbackInfo info)
7921 {
7922 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
7923
7924 strcpy_s(g_obj->name, sizeof(g_obj->name), "lilei");
7925 g_obj->age = METHOD_NUM;
7926 size_t argc = 1;
7927 JSVM_Value toWrap = nullptr;
7928 OH_JSVM_GetCbInfo(env, info, &argc, &toWrap, nullptr, nullptr);
7929 OH_JSVM_Wrap(env, toWrap, reinterpret_cast<void *>(g_obj), DerekItem, nullptr, nullptr);
7930 struct Object *data;
7931 struct Object *data1;
7932 OH_JSVM_Unwrap(env, toWrap, reinterpret_cast<void **>(&data));
7933 OH_JSVM_RemoveWrap(env, toWrap, reinterpret_cast<void **>(&g_obj));
7934 OH_JSVM_Unwrap(env, toWrap, reinterpret_cast<void **>(&data1));
7935 JSVM_Value checked = nullptr;
7936 OH_JSVM_GetBoolean(env, true, &checked);
7937 return checked;
7938 }
7939
RemoveWrap(JSVM_Env env,JSVM_CallbackInfo info)7940 static JSVM_Value RemoveWrap(JSVM_Env env, JSVM_CallbackInfo info)
7941 {
7942 size_t argc = 1;
7943 JSVM_Value wrapped = nullptr;
7944 OH_JSVM_GetCbInfo(env, info, &argc, &wrapped, nullptr, nullptr);
7945 void *data;
7946 OH_JSVM_RemoveWrap(env, wrapped, &data);
7947 JSVM_Value checked = nullptr;
7948 OH_JSVM_GetBoolean(env, true, &checked);
7949 return checked;
7950 }
7951
HandleScopeFor(JSVM_Env env,JSVM_CallbackInfo info)7952 static JSVM_Value HandleScopeFor(JSVM_Env env, JSVM_CallbackInfo info)
7953 {
7954 JSVM_Value checked = nullptr;
7955 for (int i = 0; i < SCOPE_FOR_NUMBER; i++) {
7956 JSVM_HandleScope scope = nullptr;
7957 OH_JSVM_OpenHandleScope(env, &scope);
7958 if (scope == nullptr) {
7959 OH_JSVM_GetBoolean(env, false, &checked);
7960 return checked;
7961 }
7962 JSVM_Value res = nullptr;
7963 OH_JSVM_CreateObject(env, &res);
7964 OH_JSVM_CloseHandleScope(env, scope);
7965 }
7966 OH_JSVM_GetBoolean(env, true, &checked);
7967 return checked;
7968 }
7969
7970
7971 struct DefineObject {
7972 char name[DIFF_VALUE_SIX];
7973 JSVM_Ref wrapper_;
7974 };
7975 static thread_local JSVM_Ref g_ref = nullptr;
7976 struct DefineObject *g_defineObject = (struct DefineObject *)malloc(sizeof(struct DefineObject));
New(JSVM_Env env,JSVM_CallbackInfo info)7977 JSVM_Value New(JSVM_Env env, JSVM_CallbackInfo info)
7978 {
7979 strcpy_s(g_dataType, sizeof(g_dataType), "Class");
7980 JSVM_Value newTarget;
7981 OH_JSVM_GetNewTarget(env, info, &newTarget);
7982 if (newTarget != nullptr) {
7983 size_t argc = 1;
7984 JSVM_Value args[1];
7985 JSVM_Value jsThis;
7986 OH_JSVM_GetCbInfo(env, info, &argc, args, &jsThis, nullptr);
7987 double value = 0.0;
7988 JSVM_ValueType valuetype;
7989 OH_JSVM_Typeof(env, args[0], &valuetype);
7990 if (valuetype != JSVM_UNDEFINED) {
7991 OH_JSVM_GetValueDouble(env, args[0], &value);
7992 }
7993
7994 strcpy_s(g_defineObject->name, sizeof(g_defineObject->name), "lilei");
7995 return nullptr;
7996 } else {
7997 size_t argc = 1;
7998 JSVM_Value args[1];
7999 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8000 JSVM_Value cons;
8001 OH_JSVM_GetReferenceValue(env, g_ref, &cons);
8002 JSVM_Value instance;
8003 OH_JSVM_NewInstance(env, cons, argc, args, &instance);
8004 return instance;
8005 }
8006 }
8007
GetObj(napi_env env)8008 napi_value GetObj(napi_env env)
8009 {
8010 size_t strLen = sizeof("{\"name\": \"") - 1 + strlen(g_defineObject->name) + sizeof("\"}") - 1;
8011 char *str = (char *)malloc(strLen + 1);
8012 if (str == nullptr) {
8013 printf("Memory allocation failed!\n");
8014 return nullptr;
8015 }
8016 int result = sprintf_s(str, strLen + 1, "{\"name\": \"%s\"}", g_defineObject->name);
8017 if (result < 0 || result >= strLen + 1) {
8018 return nullptr;
8019 }
8020 napi_value jsResult;
8021 napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &jsResult);
8022 return jsResult;
8023 }
8024
DefineClass(JSVM_Env env,JSVM_Value exports)8025 JSVM_Value DefineClass(JSVM_Env env, JSVM_Value exports)
8026 {
8027 JSVM_CallbackStruct param1;
8028 param1.data = nullptr;
8029 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value { return New(env, info); };
8030 JSVM_Value cons;
8031 OH_JSVM_DefineClass(env, "MyObject", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &cons);
8032 JSVM_Value instanceValue = nullptr;
8033 OH_JSVM_NewInstance(env, cons, 0, nullptr, &instanceValue);
8034 return nullptr;
8035 }
8036
PrintHello(JSVM_Env env,JSVM_CallbackInfo info)8037 static JSVM_Value PrintHello(JSVM_Env env, JSVM_CallbackInfo info)
8038 {
8039 JSVM_Value output;
8040 OH_JSVM_CreateStringUtf8(env, "Hello world!", JSVM_AUTO_LENGTH, &output);
8041 return output;
8042 }
8043 static JSVM_CallbackStruct helloCb = { PrintHello, nullptr };
8044 static intptr_t g_externalsSnapshot[] = {
8045 (intptr_t)&helloCb,
8046 0,
8047 };
8048
CreateSnapshotFailed(JSVM_Env oldEnv,JSVM_CallbackInfo info)8049 static JSVM_Value CreateSnapshotFailed(JSVM_Env oldEnv, JSVM_CallbackInfo info)
8050 {
8051 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
8052 JSVM_VM vm;
8053 JSVM_CreateVMOptions vmOptions;
8054 if (memset_s(&vmOptions, sizeof(vmOptions), 0, sizeof(vmOptions)) != EOK) {
8055 printf("memset_s failed");
8056 return nullptr;
8057 }
8058 vmOptions.isForSnapshotting = false;
8059 OH_JSVM_CreateVM(&vmOptions, &vm);
8060 JSVM_Env env;
8061 JSVM_PropertyDescriptor descriptor[] = {
8062 {"printHello", nullptr, &helloCb, nullptr, nullptr, nullptr, JSVM_DEFAULT},
8063 };
8064 OH_JSVM_CreateEnv(vm, 1, descriptor, &env);
8065 const char *blobData = nullptr;
8066 size_t blobSize = 0;
8067 JSVM_Env envs[1] = {env};
8068 JSVM_Status status = OH_JSVM_CreateSnapshot(vm, 1, envs, &blobData, &blobSize);
8069 JSVM_Value error = nullptr;
8070 if (status != JSVM_OK) {
8071 OH_JSVM_CreateStringUtf8(oldEnv, "generic failure status.", JSVM_AUTO_LENGTH, &error);
8072 return error;
8073 }
8074 return nullptr;
8075 }
8076
8077
WriteDataToStream(const char * data,int size,void * streamData)8078 static bool WriteDataToStream(const char *data, int size, void *streamData)
8079 {
8080 FILE *file = (FILE *)streamData;
8081 if (data) {
8082 fwrite(data, sizeof(char), size, file);
8083 } else {
8084 fclose(file);
8085 }
8086 return true;
8087 }
8088
StartCpuProfiler(JSVM_VM vm,JSVM_CpuProfiler * cpuProfiler)8089 static void StartCpuProfiler(JSVM_VM vm, JSVM_CpuProfiler *cpuProfiler)
8090 {
8091 FILE *startHeapFile = fopen("/data/storage/el2/base/files/heapsnapshot-start.heapsnapshot", "wb");
8092 if (startHeapFile == nullptr) {
8093 printf("Error opening file!\n");
8094 return;
8095 }
8096 OH_JSVM_TakeHeapSnapshot(vm, WriteDataToStream, startHeapFile);
8097 OH_JSVM_StartCpuProfiler(vm, cpuProfiler);
8098 }
8099
StopCpuProfiler(JSVM_VM vm,JSVM_CpuProfiler cpuProfiler)8100 static void StopCpuProfiler(JSVM_VM vm, JSVM_CpuProfiler cpuProfiler)
8101 {
8102 FILE *cpFile = fopen("/data/storage/el2/base/files/cpu-profile.cpuprofile", "wb");
8103 if (cpFile == nullptr) {
8104 printf("Error opening file!\n");
8105 return;
8106 }
8107 OH_JSVM_StopCpuProfiler(vm, cpuProfiler, WriteDataToStream, cpFile);
8108 FILE *stopHeapFile = fopen("/data/storage/el2/base/files/heapsnapshot-stop.heapsnapshot", "wb");
8109 if (stopHeapFile == nullptr) {
8110 printf("Error opening file!\n");
8111 return;
8112 }
8113 OH_JSVM_TakeHeapSnapshot(vm, WriteDataToStream, stopHeapFile);
8114 }
8115
8116 static const char *SCRIPT_TO_DEBUG = R"JS(
8117 function factorialRecursive(n) {
8118 if (n === 0 || n === 1) {
8119 return 1;
8120 } else {
8121 return n * factorialRecursive(n - 1);
8122 }
8123 }
8124
8125 function calculateFactorial() {
8126 var number = 10;
8127 var result = factorialRecursive(number);
8128 return result;
8129 }
8130
8131 calculateFactorial();
8132 )JS";
8133
DebugCodeUsingCpuProfiler(JSVM_Env oldEnv,JSVM_CallbackInfo info)8134 static JSVM_Value DebugCodeUsingCpuProfiler(JSVM_Env oldEnv, JSVM_CallbackInfo info)
8135 {
8136 strcpy_s(g_dataType, sizeof(g_dataType), "int");
8137 JSVM_VM testVm;
8138 JSVM_CreateVMOptions vmOptions;
8139 if (memset_s(&vmOptions, sizeof(vmOptions), 0, sizeof(vmOptions)) != EOK) {
8140 printf("memset_s failed");
8141 return nullptr;
8142 }
8143 OH_JSVM_CreateVM(&vmOptions, &testVm);
8144 JSVM_VMScope vmScope;
8145 OH_JSVM_OpenVMScope(testVm, &vmScope);
8146 JSVM_Env env;
8147 OH_JSVM_CreateEnv(testVm, 0, nullptr, &env);
8148 JSVM_EnvScope envScope;
8149 OH_JSVM_OpenEnvScope(env, &envScope);
8150
8151 JSVM_CpuProfiler cpuProfiler;
8152 StartCpuProfiler(testVm, &cpuProfiler);
8153
8154 JSVM_HandleScope handleScope;
8155 OH_JSVM_OpenHandleScope(env, &handleScope);
8156 JSVM_Value strValue = nullptr;
8157 OH_JSVM_CreateStringUtf8(env, SCRIPT_TO_DEBUG, strlen(SCRIPT_TO_DEBUG), &strValue);
8158 JSVM_Script script;
8159 OH_JSVM_CompileScript(env, strValue, nullptr, 0, true, nullptr, &script);
8160 JSVM_Value result = nullptr;
8161 OH_JSVM_RunScript(env, script, &result);
8162 OH_JSVM_CloseHandleScope(env, handleScope);
8163
8164 StopCpuProfiler(testVm, cpuProfiler);
8165
8166 OH_JSVM_CloseEnvScope(env, envScope);
8167 OH_JSVM_CloseVMScope(testVm, vmScope);
8168 OH_JSVM_DestroyEnv(env);
8169 OH_JSVM_DestroyVM(testVm);
8170
8171 JSVM_Value ret1 = nullptr;
8172 OH_JSVM_CreateInt32(oldEnv, 0, &ret1);
8173 return ret1;
8174 }
8175
TakeHeapSnapshot(JSVM_Env oldEnv,JSVM_CallbackInfo info)8176 static JSVM_Value TakeHeapSnapshot(JSVM_Env oldEnv, JSVM_CallbackInfo info)
8177 {
8178 strcpy_s(g_dataType, sizeof(g_dataType), "int");
8179 JSVM_VM testVm;
8180 JSVM_CreateVMOptions vmOptions;
8181 if (memset_s(&vmOptions, sizeof(vmOptions), 0, sizeof(vmOptions)) != EOK) {
8182 printf("memset_s failed");
8183 return nullptr;
8184 }
8185 OH_JSVM_CreateVM(&vmOptions, &testVm);
8186 JSVM_VMScope vmScope;
8187 OH_JSVM_OpenVMScope(testVm, &vmScope);
8188
8189 FILE *file = fopen("/data/storage/el2/base/files/heapsnapshot-test.heapsnapshot", "wb");
8190 if (file == nullptr) {
8191 printf("Error opening file!\n");
8192 return nullptr;
8193 }
8194 OH_JSVM_TakeHeapSnapshot(testVm, WriteDataToStream, file);
8195
8196 OH_JSVM_CloseVMScope(testVm, vmScope);
8197 OH_JSVM_DestroyVM(testVm);
8198
8199 JSVM_Value result = nullptr;
8200 OH_JSVM_CreateInt32(oldEnv, 0, &result);
8201 return result;
8202 }
8203
8204
WaitForDebuggerFailed(JSVM_Env env,JSVM_CallbackInfo info)8205 static JSVM_Value WaitForDebuggerFailed(JSVM_Env env, JSVM_CallbackInfo info)
8206 {
8207 strcpy_s(g_dataType, sizeof(g_dataType), "objectstr");
8208 JSVM_Value returnValue = nullptr;
8209 OH_JSVM_CreateObject(env, &returnValue);
8210 AddReturnedStatus(env, "nullptrEnv", returnValue, JSVM_Status::JSVM_INVALID_ARG,
8211 OH_JSVM_WaitForDebugger(nullptr, true));
8212 OH_JSVM_WaitForDebugger(env, true);
8213 AddLastStatus(env, "notConnected", returnValue);
8214 return returnValue;
8215 }
8216
8217 static const int PORT = 9225;
OpenInspector(JSVM_Env env)8218 static void OpenInspector(JSVM_Env env)
8219 {
8220 OH_JSVM_OpenInspector(env, "localhost", PORT);
8221 }
CloseInspector(JSVM_Env env)8222 static void CloseInspector(JSVM_Env env)
8223 {
8224 OH_JSVM_CloseInspector(env);
8225 }
8226
DebugCodeUsingInspector(JSVM_Env oldEnv,JSVM_CallbackInfo info)8227 static JSVM_Value DebugCodeUsingInspector(JSVM_Env oldEnv, JSVM_CallbackInfo info)
8228 {
8229 JSVM_VM testVm;
8230 JSVM_CreateVMOptions vmOptions;
8231 if (memset_s(&vmOptions, sizeof(vmOptions), 0, sizeof(vmOptions)) != EOK) {
8232 printf("memset_s failed");
8233 return nullptr;
8234 }
8235 OH_JSVM_CreateVM(&vmOptions, &testVm);
8236 JSVM_VMScope vmScope;
8237 OH_JSVM_OpenVMScope(testVm, &vmScope);
8238 JSVM_Env env;
8239 OH_JSVM_CreateEnv(testVm, 0, nullptr, &env);
8240
8241 OpenInspector(env);
8242
8243 JSVM_EnvScope envScope;
8244 OH_JSVM_OpenEnvScope(env, &envScope);
8245 JSVM_HandleScope handleScope;
8246 OH_JSVM_OpenHandleScope(env, &handleScope);
8247 JSVM_Value strValue = nullptr;
8248 OH_JSVM_CreateStringUtf8(env, SCRIPT_TO_DEBUG, strlen(SCRIPT_TO_DEBUG), &strValue);
8249 JSVM_Script script;
8250 OH_JSVM_CompileScript(env, strValue, nullptr, 0, true, nullptr, &script);
8251 JSVM_Value result = nullptr;
8252 OH_JSVM_RunScript(env, script, &result);
8253
8254 OH_JSVM_CloseHandleScope(env, handleScope);
8255 OH_JSVM_CloseEnvScope(env, envScope);
8256 CloseInspector(env);
8257 OH_JSVM_CloseVMScope(testVm, vmScope);
8258 OH_JSVM_DestroyEnv(env);
8259 OH_JSVM_DestroyVM(testVm);
8260
8261 strcpy_s(g_dataType, sizeof(g_dataType), "int");
8262 JSVM_Value ret1 = nullptr;
8263 OH_JSVM_CreateInt32(oldEnv, 0, &ret1);
8264 return ret1;
8265 }
8266
8267 struct InstanceData {
8268 int32_t value;
8269 };
8270
InstanceFinalizeCallback(JSVM_Env env,void * finalizeData,void * finalizeHint)8271 void InstanceFinalizeCallback(JSVM_Env env, void *finalizeData, void *finalizeHint)
8272 {
8273 if (finalizeData) {
8274 InstanceData *data = reinterpret_cast<InstanceData *>(finalizeData);
8275 free(data);
8276 }
8277 }
8278
8279 // If yes, true is returned. If no, false is returned.
SetInstanceData(JSVM_Env env,JSVM_CallbackInfo info)8280 static JSVM_Value SetInstanceData(JSVM_Env env, JSVM_CallbackInfo info)
8281 {
8282 size_t argc = 1;
8283 JSVM_Value argv[1];
8284 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
8285 uint32_t instanceDataValue;
8286 OH_JSVM_GetValueUint32(env, argv[0], &instanceDataValue);
8287 InstanceData *instanceData = reinterpret_cast<InstanceData *>(malloc(sizeof(InstanceData)));
8288 if (instanceData == nullptr) {
8289 JSVM_Value result1;
8290 OH_JSVM_GetBoolean(env, false, &result1);
8291 return result1;
8292 }
8293 instanceData->value = instanceDataValue;
8294 JSVM_Status status = OH_JSVM_SetInstanceData(env, instanceData, InstanceFinalizeCallback, nullptr);
8295 bool setResult = status == JSVM_OK;
8296 JSVM_Value result;
8297 OH_JSVM_GetBoolean(env, setResult, &result);
8298 return result;
8299 }
8300
8301 // If yes, true is returned. If no, false is returned.
GetInstanceData(JSVM_Env env,JSVM_CallbackInfo info)8302 static JSVM_Value GetInstanceData(JSVM_Env env, JSVM_CallbackInfo info)
8303 {
8304 JSVM_Value result;
8305 InstanceData *instanceData = reinterpret_cast<InstanceData *>(malloc(sizeof(InstanceData)));
8306 if (instanceData == nullptr) {
8307 OH_JSVM_GetBoolean(env, false, &result);
8308 return result;
8309 }
8310 instanceData->value = DIFF_VALUE_FIVE;
8311 OH_JSVM_SetInstanceData(env, instanceData, InstanceFinalizeCallback, nullptr);
8312 InstanceData *resData = nullptr;
8313 JSVM_Status stat1 = OH_JSVM_GetInstanceData(env, (void **)&resData);
8314 bool setResult = stat1 == JSVM_OK;
8315 if (setResult == false) {
8316 OH_JSVM_GetBoolean(env, setResult, &result);
8317 return result;
8318 }
8319 setResult = resData->value == DIFF_VALUE_FIVE;
8320 if (setResult == false) {
8321 OH_JSVM_GetBoolean(env, setResult, &result);
8322 return result;
8323 }
8324 InstanceData *resData2 = nullptr;
8325 OH_JSVM_GetInstanceData(env, (void **)&resData2);
8326 setResult = resData2 == resData;
8327 OH_JSVM_GetBoolean(env, setResult, &result);
8328 return result;
8329 }
8330
IsCallable(JSVM_Env env,JSVM_CallbackInfo info)8331 static JSVM_Value IsCallable(JSVM_Env env, JSVM_CallbackInfo info)
8332 {
8333 JSVM_Value value;
8334 JSVM_Value rst;
8335 size_t argc = 1;
8336 bool isCallable = false;
8337 JSVM_CALL(env, OH_JSVM_GetCbInfo(env, info, &argc, &value, NULL, NULL));
8338 JSVM_CALL(env, OH_JSVM_IsCallable(env, value, &isCallable));
8339 OH_JSVM_GetBoolean(env, isCallable, &rst);
8340 return rst;
8341 }
8342
testIsCallable(napi_env env1,napi_callback_info info)8343 static napi_value testIsCallable(napi_env env1, napi_callback_info info)
8344 {
8345 JSVM_InitOptions init_options;
8346 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8347 printf("memset_s failed");
8348 return nullptr;
8349 }
8350 init_options.externalReferences = externals;
8351 if (aa == 0) {
8352 OH_JSVM_Init(&init_options);
8353 aa++;
8354 }
8355 JSVM_VM vm;
8356 JSVM_CreateVMOptions options;
8357 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8358 printf("memset_s failed");
8359 return nullptr;
8360 }
8361 OH_JSVM_CreateVM(&options, &vm);
8362 JSVM_VMScope vm_scope;
8363 OH_JSVM_OpenVMScope(vm, &vm_scope);
8364 JSVM_CallbackStruct param[] = {{.data = nullptr, .callback = IsCallable}};
8365 JSVM_PropertyDescriptor descriptor[] = {{"isCallable", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT}};
8366 JSVM_Env env;
8367 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
8368 JSVM_EnvScope envScope;
8369 OH_JSVM_OpenEnvScope(env, &envScope);
8370 JSVM_HandleScope handlescope;
8371 OH_JSVM_OpenHandleScope(env, &handlescope);
8372 const char* sourcecodestr = R"JS(
8373 function add() { return 0;} let rst = isCallable(add);
8374 )JS";
8375 JSVM_Value sourcecodevalue;
8376 OH_JSVM_CreateStringUtf8(env, sourcecodestr, strlen(sourcecodestr), &sourcecodevalue);
8377 JSVM_Script script;
8378 OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script);
8379 JSVM_Value result;
8380 OH_JSVM_RunScript(env, script, &result);
8381 OH_JSVM_CloseHandleScope(env, handlescope);
8382 OH_JSVM_CloseEnvScope(env, envScope);
8383 OH_JSVM_DestroyEnv(env);
8384 OH_JSVM_CloseVMScope(vm, vm_scope);
8385 OH_JSVM_DestroyVM(vm);
8386 napi_value result11;
8387 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
8388 return result11;
8389 }
8390
Thread1Func(JSVM_VM vm,JSVM_Env env)8391 void Thread1Func(JSVM_VM vm, JSVM_Env env)
8392 {
8393 bool isLocked = false;
8394 OH_JSVM_IsLocked(env, &isLocked);
8395 if (!isLocked) {
8396 OH_JSVM_AcquireLock(env);
8397 g_threadFlag1 = false;
8398 }
8399 JSVM_VMScope vmScope;
8400 OH_JSVM_OpenVMScope(vm, &vmScope);
8401 JSVM_EnvScope envScope;
8402 OH_JSVM_OpenEnvScope(env, &envScope);
8403 JSVM_HandleScope handleScope;
8404 OH_JSVM_OpenHandleScope(env, &handleScope);
8405 JSVM_Value value;
8406 OH_JSVM_CreateInt32(env, 32, &value); // 32: numerical value
8407 int32_t num1;
8408 OH_JSVM_GetValueInt32(env, value, &num1);
8409 OH_JSVM_CloseHandleScope(env, handleScope);
8410 OH_JSVM_CloseEnvScope(env, envScope);
8411 OH_JSVM_CloseVMScope(vm, vmScope);
8412 OH_JSVM_IsLocked(env, &isLocked);
8413 if (isLocked) {
8414 OH_JSVM_ReleaseLock(env);
8415 g_threadFlag1 = true;
8416 }
8417 }
8418
Thread2Func(JSVM_VM vm,JSVM_Env env)8419 void Thread2Func(JSVM_VM vm, JSVM_Env env)
8420 {
8421 bool isLocked = false;
8422 OH_JSVM_IsLocked(env, &isLocked);
8423 if (!isLocked) {
8424 OH_JSVM_AcquireLock(env);
8425 g_threadFlag2 = false;
8426 }
8427 JSVM_VMScope vmScope;
8428 OH_JSVM_OpenVMScope(vm, &vmScope);
8429 JSVM_EnvScope envScope;
8430 OH_JSVM_OpenEnvScope(env, &envScope);
8431 JSVM_HandleScope handleScope;
8432 OH_JSVM_OpenHandleScope(env, &handleScope);
8433 JSVM_Value value;
8434 OH_JSVM_CreateInt32(env, 32, &value); // 32: numerical value
8435 int32_t num1;
8436 OH_JSVM_GetValueInt32(env, value, &num1);
8437 OH_JSVM_CloseHandleScope(env, handleScope);
8438 OH_JSVM_CloseEnvScope(env, envScope);
8439 OH_JSVM_CloseVMScope(vm, vmScope);
8440 OH_JSVM_IsLocked(env, &isLocked);
8441 if (isLocked) {
8442 OH_JSVM_ReleaseLock(env);
8443 g_threadFlag2 = true;
8444 }
8445 }
8446
testMultithreadFunction(napi_env env1,napi_callback_info info)8447 static napi_value testMultithreadFunction(napi_env env1, napi_callback_info info)
8448 {
8449 JSVM_InitOptions init_options;
8450 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8451 printf("memset_s failed");
8452 return nullptr;
8453 }
8454 init_options.externalReferences = externals;
8455 if (aa == 0) {
8456 OH_JSVM_Init(&init_options);
8457 aa++;
8458 }
8459 JSVM_VM vm;
8460 JSVM_CreateVMOptions options;
8461 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8462 printf("memset_s failed");
8463 return nullptr;
8464 }
8465 OH_JSVM_CreateVM(&options, &vm);
8466 JSVM_Env env;
8467 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8468 Thread1Func(vm, env);
8469 Thread2Func(vm, env);
8470 OH_JSVM_DestroyEnv(env);
8471 OH_JSVM_DestroyVM(vm);
8472 napi_value result11;
8473 if (g_threadFlag1 && g_threadFlag2) {
8474 NAPI_CALL(env1, napi_create_int32(env1, 1, &result11));
8475 } else {
8476 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
8477 }
8478 return result11;
8479 }
8480
8481 static const char *STR_TASK = R"JS(
8482 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,
8483 115, 1, 95, 0, 0, 3, 2, 1, 1, 8, 1, 1, 10, 9, 1, 7, 0, 65, 185, 10, 16, 0, 11]),
8484 {js:{_:console.log("JSVM: Called from WebAssembly Hello world")}}).then(function(obj)
8485 {
8486 console.log("Called with instance " + obj);
8487 }).catch(function(err) {
8488 console.log("Called with error " + err);
8489 });
8490 )JS";
8491
testHandleMicrotasks(napi_env env1,napi_callback_info info)8492 static napi_value testHandleMicrotasks(napi_env env1, napi_callback_info info)
8493 {
8494 JSVM_InitOptions init_options;
8495 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8496 return nullptr;
8497 }
8498 init_options.externalReferences = externals;
8499 if (aa == 0) {
8500 OH_JSVM_Init(&init_options);
8501 aa++;
8502 }
8503 JSVM_VM vm;
8504 JSVM_CreateVMOptions options;
8505 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8506 return nullptr;
8507 }
8508 OH_JSVM_CreateVM(&options, &vm);
8509 JSVM_VMScope vm_scope;
8510 OH_JSVM_OpenVMScope(vm, &vm_scope);
8511 JSVM_Env env;
8512 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8513 JSVM_EnvScope envScope;
8514 OH_JSVM_OpenEnvScope(env, &envScope);
8515 JSVM_HandleScope handlescope;
8516 OH_JSVM_OpenHandleScope(env, &handlescope);
8517 JSVM_Value sourcecodevalue;
8518 OH_JSVM_CreateStringUtf8(env, STR_TASK, strlen(STR_TASK), &sourcecodevalue);
8519 JSVM_Script script;
8520 OH_JSVM_CompileScript(env, sourcecodevalue, nullptr, 0, true, nullptr, &script);
8521 JSVM_Value result;
8522 OH_JSVM_RunScript(env, script, &result);
8523 bool rst = false;
8524 for (int i = 0; i < 3; i++) { // 3: cycles
8525 sleep(3); // 3 seconds
8526 JSVM_Status flag1 = OH_JSVM_PumpMessageLoop(vm, &rst);
8527 sleep(3); // 3 sec onds
8528 JSVM_Status flag2 = OH_JSVM_PerformMicrotaskCheckpoint(vm);
8529 if (rst && flag1 == JSVM_Status::JSVM_OK && flag2 == JSVM_Status::JSVM_OK) {
8530 break;
8531 }
8532 }
8533 OH_JSVM_CloseHandleScope(env, handlescope);
8534 OH_JSVM_CloseEnvScope(env, envScope);
8535 OH_JSVM_DestroyEnv(env);
8536 OH_JSVM_CloseVMScope(vm, vm_scope);
8537 OH_JSVM_DestroyVM(vm);
8538 napi_value result11;
8539 NAPI_CALL(env1, napi_get_boolean(env1, rst, &result11));
8540 return result11;
8541 }
8542
8543 static const char *SRC_TEST = R"JS(
8544 !function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};
8545 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)
8546 {n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e)
8547 {"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),
8548 Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;
8549 if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);
8550 if(n.r(r),Object.defineProperty(r,"default",
8551 {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));
8552 return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};
8553 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)}
8554 ([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)}},
8555 function(e,t){e.exports={divide:function(e,t){if(0===t)throw new Error("Cannot divide by zero");return e/t}}}]);
8556 )JS";
8557
testJswmInterface(napi_env env1,napi_callback_info info)8558 static napi_value testJswmInterface(napi_env env1, napi_callback_info info)
8559 {
8560 JSVM_InitOptions init_options;
8561 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8562 return nullptr;
8563 }
8564 init_options.externalReferences = externals;
8565 if (aa == 0) {
8566 OH_JSVM_Init(&init_options);
8567 aa++;
8568 }
8569 JSVM_VM vm;
8570 JSVM_CreateVMOptions options;
8571 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8572 return nullptr;
8573 }
8574 OH_JSVM_CreateVM(&options, &vm);
8575 JSVM_VMScope vm_scope;
8576 OH_JSVM_OpenVMScope(vm, &vm_scope);
8577 JSVM_Env env;
8578 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8579 JSVM_EnvScope envScope;
8580 OH_JSVM_OpenEnvScope(env, &envScope);
8581 JSVM_HandleScope handlescope;
8582 OH_JSVM_OpenHandleScope(env, &handlescope);
8583 JSVM_Value jsSrc;
8584 OH_JSVM_CreateStringUtf8(env, SRC_TEST, strlen(SRC_TEST), &jsSrc);
8585 const uint8_t *data = nullptr;
8586 size_t length = 0;
8587 bool cacheRejected = false;
8588 bool rstFlag = false;
8589 JSVM_Script script;
8590 JSVM_ScriptOrigin scriptOrgin {
8591 .sourceMapUrl = "/data/bundle.js.map/bundle.js.map",
8592 .resourceName = "bundle.js"
8593 };
8594 JSVM_Status status = OH_JSVM_CompileScriptWithOrigin(env, jsSrc, data, length,
8595 true, &cacheRejected, &scriptOrgin, &script);
8596 if (status == JSVM_Status::JSVM_OK) {
8597 rstFlag = true;
8598 }
8599 OH_JSVM_CloseHandleScope(env, handlescope);
8600 OH_JSVM_CloseEnvScope(env, envScope);
8601 OH_JSVM_DestroyEnv(env);
8602 OH_JSVM_CloseVMScope(vm, vm_scope);
8603 OH_JSVM_DestroyVM(vm);
8604 napi_value result11;
8605 NAPI_CALL(env1, napi_get_boolean(env1, rstFlag, &result11));
8606 return result11;
8607 }
8608
IsConstructor(JSVM_Env env,JSVM_CallbackInfo info)8609 static JSVM_Value IsConstructor(JSVM_Env env, JSVM_CallbackInfo info)
8610 {
8611 size_t argc = 1;
8612 JSVM_Value args[1] = {nullptr};
8613 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8614
8615 bool result = false;
8616 OH_JSVM_IsConstructor(env, args[0], &result);
8617
8618 JSVM_Value isConstructor = nullptr;
8619 OH_JSVM_GetBoolean(env, result, &isConstructor);
8620 return isConstructor;
8621 }
CreateRegExp1(JSVM_Env env,JSVM_CallbackInfo info)8622 static JSVM_Value CreateRegExp1(JSVM_Env env, JSVM_CallbackInfo info)
8623 {
8624 size_t argc = 1;
8625 JSVM_Value args[1] = {nullptr};
8626 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8627
8628 JSVM_Value returnValue = nullptr;
8629 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_IGNORE_CASE, &returnValue);
8630 if (status != JSVM_OK) {
8631 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8632 }
8633 return returnValue;
8634 }
CreateRegExp2(JSVM_Env env,JSVM_CallbackInfo info)8635 static JSVM_Value CreateRegExp2(JSVM_Env env, JSVM_CallbackInfo info)
8636 {
8637 size_t argc = 1;
8638 JSVM_Value args[1] = {nullptr};
8639 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8640
8641 JSVM_Value returnValue = nullptr;
8642 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_GLOBAL, &returnValue);
8643 if (status != JSVM_OK) {
8644 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8645 }
8646 return returnValue;
8647 }
CreateRegExp3(JSVM_Env env,JSVM_CallbackInfo info)8648 static JSVM_Value CreateRegExp3(JSVM_Env env, JSVM_CallbackInfo info)
8649 {
8650 size_t argc = 1;
8651 JSVM_Value args[1] = {nullptr};
8652 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8653
8654 JSVM_Value returnValue = nullptr;
8655 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_MULTILINE, &returnValue);
8656 if (status != JSVM_OK) {
8657 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8658 }
8659 return returnValue;
8660 }
CreateRegExp4(JSVM_Env env,JSVM_CallbackInfo info)8661 static JSVM_Value CreateRegExp4(JSVM_Env env, JSVM_CallbackInfo info)
8662 {
8663 size_t argc = 1;
8664 JSVM_Value args[1] = {nullptr};
8665 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8666
8667 JSVM_Value returnValue = nullptr;
8668 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_STICKY, &returnValue);
8669 if (status != JSVM_OK) {
8670 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8671 }
8672 return returnValue;
8673 }
CreateRegExp5(JSVM_Env env,JSVM_CallbackInfo info)8674 static JSVM_Value CreateRegExp5(JSVM_Env env, JSVM_CallbackInfo info)
8675 {
8676 size_t argc = 1;
8677 JSVM_Value args[1] = {nullptr};
8678 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8679
8680 JSVM_Value returnValue = nullptr;
8681 JSVM_Status status = OH_JSVM_CreateRegExp(env, args[0], JSVM_RegExpFlags::JSVM_REGEXP_DOT_ALL, &returnValue);
8682 if (status != JSVM_OK) {
8683 OH_JSVM_ThrowError(env, nullptr, "Failed to create regexp");
8684 }
8685 return returnValue;
8686 }
CreateRegExpEnvNullptr(JSVM_Env env,JSVM_CallbackInfo info)8687 static JSVM_Value CreateRegExpEnvNullptr(JSVM_Env env, JSVM_CallbackInfo info)
8688 {
8689 size_t argc = 1;
8690 JSVM_Value args[1] = {nullptr};
8691 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
8692
8693 JSVM_Value returnValue = nullptr;
8694 JSVM_Status status = OH_JSVM_CreateRegExp(nullptr, args[0], JSVM_RegExpFlags::JSVM_REGEXP_DOT_ALL, &returnValue);
8695 JSVM_Value value = nullptr;
8696 OH_JSVM_CreateInt32(env, static_cast<int32_t>(status), &value);
8697 return value;
8698 }
CreateMap(JSVM_Env env,JSVM_CallbackInfo info)8699 static JSVM_Value CreateMap(JSVM_Env env, JSVM_CallbackInfo info)
8700 {
8701 JSVM_Value returnValue = nullptr;
8702 JSVM_Status status = OH_JSVM_CreateMap(env, &returnValue);
8703 if (status != JSVM_OK) {
8704 OH_JSVM_ThrowError(env, nullptr, "Failed to create map");
8705 }
8706 return returnValue;
8707 }
8708
testCompileScriptWithOptions(JSVM_Env env)8709 bool testCompileScriptWithOptions(JSVM_Env env)
8710 {
8711 JSVM_Value jsSrc;
8712 JSVM_Script script = nullptr;
8713 std::string src(R"JS(let a = 100;a = a + 21)JS");
8714 uint8_t *cache;
8715 size_t length;
8716 JSVM_ScriptOrigin scriptOrgin {
8717 .sourceMapUrl = "bundle.js.map",
8718 .resourceName = "bundle.js"
8719 };
8720 JSVM_CompileOptions option[2];
8721 option[0] = {
8722 .id = JSVM_COMPILE_MODE,
8723 .content = { .num = JSVM_COMPILE_MODE_EAGER_COMPILE }
8724 };
8725 option[1] = {
8726 .id = JSVM_COMPILE_SCRIPT_ORIGIN,
8727 .content = { &scriptOrgin }
8728 };
8729 bool rstFlag = false;
8730 OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsSrc);
8731 rstFlag = OH_JSVM_CompileScriptWithOptions(env, jsSrc, TWO, option, &script) == JSVM_OK;
8732 OH_JSVM_CreateCodeCache(env, script, (const uint8_t**)&cache, &length);
8733 return rstFlag;
8734 }
testCompileWithOption(napi_env env1,napi_callback_info info)8735 static napi_value testCompileWithOption(napi_env env1, napi_callback_info info)
8736 {
8737 JSVM_InitOptions init_options;
8738 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8739 return nullptr;
8740 }
8741 init_options.externalReferences = externals;
8742 if (aa == 0) {
8743 OH_JSVM_Init(&init_options);
8744 aa++;
8745 }
8746 JSVM_VM vm;
8747 JSVM_CreateVMOptions options;
8748 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8749 return nullptr;
8750 }
8751 OH_JSVM_CreateVM(&options, &vm);
8752 JSVM_VMScope vm_scope;
8753 OH_JSVM_OpenVMScope(vm, &vm_scope);
8754 JSVM_Env env;
8755 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8756 JSVM_EnvScope envScope;
8757 OH_JSVM_OpenEnvScope(env, &envScope);
8758 JSVM_HandleScope handlescope;
8759 OH_JSVM_OpenHandleScope(env, &handlescope);
8760 bool rstFlag = false;
8761 rstFlag = testCompileScriptWithOptions(env);
8762 OH_JSVM_CloseHandleScope(env, handlescope);
8763 OH_JSVM_CloseEnvScope(env, envScope);
8764 OH_JSVM_DestroyEnv(env);
8765 OH_JSVM_CloseVMScope(vm, vm_scope);
8766 OH_JSVM_DestroyVM(vm);
8767 napi_value result11;
8768 NAPI_CALL(env1, napi_get_boolean(env1, rstFlag, &result11));
8769 return result11;
8770 }
8771
testRetainScript(napi_env env1,napi_callback_info info)8772 static napi_value testRetainScript(napi_env env1, napi_callback_info info)
8773 {
8774 JSVM_InitOptions init_options;
8775 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8776 return nullptr;
8777 }
8778 init_options.externalReferences = externals;
8779 if (aa == 0) {
8780 OH_JSVM_Init(&init_options);
8781 aa++;
8782 }
8783 JSVM_VM vm;
8784 JSVM_CreateVMOptions options;
8785 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8786 return nullptr;
8787 }
8788 OH_JSVM_CreateVM(&options, &vm);
8789 JSVM_VMScope vm_scope;
8790 OH_JSVM_OpenVMScope(vm, &vm_scope);
8791 JSVM_Env env;
8792 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8793 JSVM_EnvScope envScope;
8794 OH_JSVM_OpenEnvScope(env, &envScope);
8795 JSVM_HandleScope handlescope;
8796 OH_JSVM_OpenHandleScope(env, &handlescope);
8797 bool result = true;
8798 JSVM_Script script = nullptr;
8799 JSVM_HandleScope handlescopeSub;
8800 OH_JSVM_OpenHandleScope(env, &handlescopeSub);
8801 std::string src(R"JS(let a = 100;a = a + 21)JS");
8802
8803 JSVM_Value jsSrc;
8804 OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &jsSrc);
8805 OH_JSVM_CompileScriptWithOptions(env, jsSrc, 0, nullptr, &script);
8806 result = result && OH_JSVM_RetainScript(env, script) == JSVM_OK;
8807 OH_JSVM_CloseHandleScope(env, handlescopeSub);
8808 JSVM_Value returnValue;
8809 auto runStatus = OH_JSVM_RunScript(env, script, &returnValue) == JSVM_OK;
8810 OH_JSVM_ReleaseScript(env, script);
8811 int runResult = 0;
8812 OH_JSVM_GetValueInt32(env, returnValue, &runResult);
8813 result = runStatus && runResult == RUN_TEST_RETAIN_SCRIPT_VALUE;
8814 OH_JSVM_CloseHandleScope(env, handlescope);
8815 OH_JSVM_CloseEnvScope(env, envScope);
8816 OH_JSVM_DestroyEnv(env);
8817 OH_JSVM_CloseVMScope(vm, vm_scope);
8818 OH_JSVM_DestroyVM(vm);
8819 napi_value result11;
8820 NAPI_CALL(env1, napi_get_boolean(env1, result, &result11));
8821 return result11;
8822 }
8823
testOpenInspectorWithName(napi_env env1,napi_callback_info info)8824 static napi_value testOpenInspectorWithName(napi_env env1, napi_callback_info info)
8825 {
8826 JSVM_InitOptions init_options;
8827 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8828 return nullptr;
8829 }
8830 init_options.externalReferences = externals;
8831 if (aa == 0) {
8832 OH_JSVM_Init(&init_options);
8833 aa++;
8834 }
8835 JSVM_VM vm;
8836 JSVM_CreateVMOptions options;
8837 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8838 return nullptr;
8839 }
8840
8841 OH_JSVM_CreateVM(&options, &vm);
8842 JSVM_VMScope vmScope;
8843 OH_JSVM_OpenVMScope(vm, &vmScope);
8844 JSVM_Env env;
8845 OH_JSVM_CreateEnv(vm, 0, nullptr, &env);
8846
8847 OH_JSVM_OpenInspectorWithName(env, -1, "test");
8848 JSVM_EnvScope envScope;
8849 OH_JSVM_OpenEnvScope(env, &envScope);
8850 JSVM_HandleScope handleScope;
8851 OH_JSVM_OpenHandleScope(env, &handleScope);
8852 JSVM_Value strValue = nullptr;
8853 std::string src(R"JS(let a = 100;a = a + 21)JS");
8854 OH_JSVM_CreateStringUtf8(env, src.c_str(), src.size(), &strValue);
8855 JSVM_Script script;
8856 OH_JSVM_CompileScript(env, strValue, nullptr, 0, true, nullptr, &script);
8857 JSVM_Value result = nullptr;
8858 OH_JSVM_RunScript(env, script, &result);
8859 int runResult = 0;
8860 OH_JSVM_GetValueInt32(env, result, &runResult);
8861 auto resultFlag = runResult == RUN_TEST_RETAIN_SCRIPT_VALUE;
8862 OH_JSVM_CloseHandleScope(env, handleScope);
8863 OH_JSVM_CloseEnvScope(env, envScope);
8864 OH_JSVM_CloseInspector(env);
8865 OH_JSVM_CloseVMScope(vm, vmScope);
8866 OH_JSVM_DestroyEnv(env);
8867 OH_JSVM_DestroyVM(vm);
8868 napi_value result11;
8869 NAPI_CALL(env1, napi_get_boolean(env1, resultFlag, &result11));
8870 return result11;
8871 }
8872
useArrayBuffer(JSVM_Env env)8873 void useArrayBuffer(JSVM_Env env)
8874 {
8875 JSVM_HandleScope handleScope;
8876 OH_JSVM_OpenHandleScope(env, &handleScope);
8877
8878 void *backingStore;
8879 JSVM_Value arrayBuffer;
8880 JSVM_Status stat1 = OH_JSVM_AllocateArrayBufferBackingStoreData(100, JSVM_ZERO_INITIALIZED, &backingStore);
8881 JSVM_ASSERT_RETURN_VOID(env, stat1 != JSVM_OK, "OH_JSVM_AllocateArrayBufferBackingStoreData check status");
8882
8883 JSVM_Status stat2 = OH_JSVM_CreateArrayBufferFromBackingStoreData(env, backingStore, 100, 30, 20, &arrayBuffer);
8884 JSVM_ASSERT_RETURN_VOID(env, stat2 != JSVM_OK, "OH_JSVM_CreateArrayBufferFromBackingStoreData check status");
8885
8886 JSVM_Value js_global;
8887 JSVM_Value name;
8888 JSVM_Status stat3 = OH_JSVM_GetGlobal(env, &js_global);
8889 JSVM_ASSERT_RETURN_VOID(env, stat3 != JSVM_OK, "OH_JSVM_GetGlobal check status");
8890 JSVM_Status stat4 = OH_JSVM_CreateStringUtf8(env, "buffer", JSVM_AUTO_LENGTH, &name);
8891 JSVM_ASSERT_RETURN_VOID(env, stat4 != JSVM_OK, "OH_JSVM_CreateStringUtf8 check status");
8892 JSVM_Status stat5 = OH_JSVM_SetProperty(env, js_global, name, arrayBuffer);
8893 JSVM_ASSERT_RETURN_VOID(env, stat5 != JSVM_OK, "OH_JSVM_SetProperty check status");
8894
8895 JSVM_Script script;
8896 JSVM_Value scriptString;
8897 JSVM_Value result;
8898 const char *src = R"JS(
8899 function writeBuffer(data) {
8900 let view = new Uint8Array(data);
8901 // Write some values to the ArrayBuffer
8902 for (let i = 0; i < view.length; i++) {
8903 view[i] = i % 256;
8904 }
8905 }
8906 writeBuffer(buffer)
8907 )JS";
8908 JSVM_Status stat6 = OH_JSVM_CreateStringUtf8(env, src, JSVM_AUTO_LENGTH, &scriptString);
8909 JSVM_ASSERT_RETURN_VOID(env, stat6 != JSVM_OK, "OH_JSVM_CreateStringUtf8 check status");
8910 JSVM_Status stat7 = OH_JSVM_CompileScriptWithOptions(env, scriptString, 0, nullptr, &script);
8911 JSVM_ASSERT_RETURN_VOID(env, stat7 != JSVM_OK, "OH_JSVM_CompileScriptWithOption check status");
8912 JSVM_Status stat8 = OH_JSVM_RunScript(env, script, &result);
8913 JSVM_ASSERT_RETURN_VOID(env, stat8 != JSVM_OK, "OH_JSVM_RunScript check status");
8914
8915 uint8_t *array = static_cast<uint8_t*>(backingStore);
8916 for (auto i = 0; i < 100; ++i) {
8917 if (array[i] != i % 25 % 256) {
8918 JSVM_ASSERT_RETURN_VOID(env, true, "OH_JSVM_RunScript check status");
8919 }
8920 }
8921
8922 JSVM_Status stat9 = OH_JSVM_DetachArraybuffer(env, arrayBuffer);
8923 JSVM_ASSERT_RETURN_VOID(env, stat9 != JSVM_OK, "OH_JSVM_DetachArraybuffer check status");
8924
8925 JSVM_Status stat10 = OH_JSVM_FreeArrayBufferBackingStoreData(backingStore);
8926 JSVM_ASSERT_RETURN_VOID(env, stat10 != JSVM_OK, "OH_JSVM_FreeArrayBufferBackingStoreData check status");
8927
8928 OH_JSVM_CloseHandleScope(env, handleScope);
8929 }
8930
testArrayBuffer(napi_env env1,napi_callback_info info)8931 static napi_value testArrayBuffer(napi_env env1, napi_callback_info info)
8932 {
8933 JSVM_InitOptions init_options;
8934 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
8935 printf("memset_s failed");
8936 return nullptr;
8937 }
8938 init_options.externalReferences = externals;
8939 if (aa == 0) {
8940 OH_JSVM_Init(&init_options);
8941 aa++;
8942 }
8943 JSVM_VM vm;
8944 JSVM_CreateVMOptions options;
8945 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
8946 printf("memset_s failed");
8947 return nullptr;
8948 }
8949 OH_JSVM_CreateVM(&options, &vm);
8950 JSVM_VMScope vm_scope;
8951 OH_JSVM_OpenVMScope(vm, &vm_scope);
8952 JSVM_Env env;
8953 JSVM_CallbackStruct param[1];
8954 param[0].data = nullptr;
8955 param[0].callback = assertEqual;
8956 JSVM_PropertyDescriptor descriptor[] = {
8957 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
8958 };
8959 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
8960 JSVM_EnvScope envScope;
8961 OH_JSVM_OpenEnvScope(env, &envScope);
8962
8963 useArrayBuffer(env);
8964
8965 OH_JSVM_CloseEnvScope(env, envScope);
8966 OH_JSVM_DestroyEnv(env);
8967 OH_JSVM_CloseVMScope(vm, vm_scope);
8968 OH_JSVM_DestroyVM(vm);
8969 napi_value result11;
8970 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
8971 return result11;
8972 }
8973
8974
useArrayBuffer2(JSVM_Env env)8975 void useArrayBuffer2(JSVM_Env env)
8976 {
8977 void *storeData = nullptr;
8978 size_t storeLen = SIXTEEN;
8979 JSVM_Status stat1 = OH_JSVM_AllocateArrayBufferBackingStoreData(storeLen, JSVM_ZERO_INITIALIZED, &storeData);
8980 JSVM_ASSERT_RETURN_VOID(env, stat1 != JSVM_OK, "OH_JSVM_AllocateArrayBufferBackingStoreData check status");
8981
8982 JSVM_Value arrayBuffer;
8983 JSVM_Status stat2 = OH_JSVM_CreateArrayBufferFromBackingStoreData(env, storeData, storeLen, 0, storeLen, &arrayBuffer);
8984 JSVM_ASSERT_RETURN_VOID(env, stat2 != JSVM_OK, "OH_JSVM_CreateArrayBufferFromBackingStoreData check status");
8985
8986 void *tmpArrayBufferPtr = nullptr;
8987 size_t arrayBufferLen = 0;
8988 JSVM_Status stat3 = OH_JSVM_GetArraybufferInfo(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLen);
8989 JSVM_ASSERT_RETURN_VOID(env, stat3 != JSVM_OK, "OH_JSVM_GetArraybufferInfo check status");
8990
8991 size_t typedArrayLen = arrayBufferLen / sizeof(int32_t);
8992 JSVM_Value typedArray;
8993 JSVM_Status stat4 = OH_JSVM_CreateTypedarray(env, JSVM_TypedarrayType::JSVM_INT32_ARRAY, typedArrayLen,
8994 arrayBuffer, 0, &typedArray);
8995 JSVM_ASSERT_RETURN_VOID(env, stat4 != JSVM_OK, "OH_JSVM_CreateTypedarray check status");
8996
8997 JSVM_Status stat5 = OH_JSVM_DetachArraybuffer(env, arrayBuffer);
8998 JSVM_ASSERT_RETURN_VOID(env, stat5 != JSVM_OK, "OH_JSVM_DetachArraybuffer check status");
8999
9000 JSVM_Status stat6 = OH_JSVM_FreeArrayBufferBackingStoreData(storeData);
9001 JSVM_ASSERT_RETURN_VOID(env, stat6 != JSVM_OK, "OH_JSVM_FreeArrayBufferBackingStoreData check status");
9002 }
testArrayBuffer2(napi_env env1,napi_callback_info info)9003 static napi_value testArrayBuffer2(napi_env env1, napi_callback_info info)
9004 {
9005 JSVM_InitOptions init_options;
9006 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
9007 printf("memset_s failed");
9008 return nullptr;
9009 }
9010 init_options.externalReferences = externals;
9011 if (aa == 0) {
9012 OH_JSVM_Init(&init_options);
9013 aa++;
9014 }
9015 JSVM_VM vm;
9016 JSVM_CreateVMOptions options;
9017 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
9018 printf("memset_s failed");
9019 return nullptr;
9020 }
9021 OH_JSVM_CreateVM(&options, &vm);
9022 JSVM_VMScope vm_scope;
9023 OH_JSVM_OpenVMScope(vm, &vm_scope);
9024 JSVM_Env env;
9025 JSVM_CallbackStruct param[1];
9026 param[0].data = nullptr;
9027 param[0].callback = assertEqual;
9028 JSVM_PropertyDescriptor descriptor[] = {
9029 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
9030 };
9031 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
9032 JSVM_EnvScope envScope;
9033 OH_JSVM_OpenEnvScope(env, &envScope);
9034 JSVM_HandleScope handleScope;
9035 OH_JSVM_OpenHandleScope(env, &handleScope);
9036
9037 useArrayBuffer2(env);
9038
9039 OH_JSVM_CloseHandleScope(env, handleScope);
9040 OH_JSVM_CloseEnvScope(env, envScope);
9041 OH_JSVM_DestroyEnv(env);
9042 OH_JSVM_CloseVMScope(vm, vm_scope);
9043 OH_JSVM_DestroyVM(vm);
9044 napi_value result11;
9045 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
9046 return result11;
9047 }
9048
9049
useArrayBuffer3(JSVM_Env env)9050 void useArrayBuffer3(JSVM_Env env)
9051 {
9052 void *storeData = nullptr;
9053 size_t storeLen = SIXTEEN;
9054 JSVM_Status stat1 = OH_JSVM_AllocateArrayBufferBackingStoreData(storeLen, JSVM_ZERO_INITIALIZED, &storeData);
9055 JSVM_ASSERT_RETURN_VOID(env, stat1 != JSVM_OK, "OH_JSVM_AllocateArrayBufferBackingStoreData check status");
9056
9057 JSVM_Value arrayBuffer;
9058 JSVM_Status stat2 = OH_JSVM_CreateArrayBufferFromBackingStoreData(env, storeData, storeLen, 0, storeLen, &arrayBuffer);
9059 JSVM_ASSERT_RETURN_VOID(env, stat2 != JSVM_OK, "OH_JSVM_CreateArrayBufferFromBackingStoreData check status");
9060
9061 void *tmpArrayBufferPtr = nullptr;
9062 size_t arrayBufferLen = 0;
9063 JSVM_Status stat3 = OH_JSVM_GetArraybufferInfo(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLen);
9064 JSVM_ASSERT_RETURN_VOID(env, stat3 != JSVM_OK, "OH_JSVM_GetArraybufferInfo check status");
9065
9066 size_t typedArrayLen = arrayBufferLen / sizeof(int32_t);
9067
9068 JSVM_Value typedArray;
9069 JSVM_Status stat4 = OH_JSVM_CreateTypedarray(env, JSVM_TypedarrayType::JSVM_INT32_ARRAY, typedArrayLen, arrayBuffer, 0, &typedArray);
9070 JSVM_ASSERT_RETURN_VOID(env, stat4 != JSVM_OK, "OH_JSVM_CreateTypedarray check status");
9071
9072 JSVM_Status stat5 = OH_JSVM_DetachArraybuffer(env, arrayBuffer);
9073 JSVM_ASSERT_RETURN_VOID(env, stat5 != JSVM_OK, "OH_JSVM_DetachArraybuffer check status");
9074
9075 JSVM_Value arrayBuffer2;
9076 JSVM_Status stat6 = OH_JSVM_CreateArrayBufferFromBackingStoreData(env, storeData, storeLen, 0, storeLen, &arrayBuffer2);
9077 JSVM_ASSERT_RETURN_VOID(env, stat6 != JSVM_OK, "OH_JSVM_CreateArrayBufferFromBackingStoreData check status");
9078 JSVM_Status stat7 = OH_JSVM_DetachArraybuffer(env, arrayBuffer2);
9079 JSVM_ASSERT_RETURN_VOID(env, stat7 != JSVM_OK, "OH_JSVM_DetachArraybuffer check status");
9080
9081 JSVM_Status stat8 = OH_JSVM_FreeArrayBufferBackingStoreData(storeData);
9082 JSVM_ASSERT_RETURN_VOID(env, stat8 != JSVM_OK, "OH_JSVM_FreeArrayBufferBackingStoreData check status");
9083 }
testArrayBuffer3(napi_env env1,napi_callback_info info)9084 static napi_value testArrayBuffer3(napi_env env1, napi_callback_info info)
9085 {
9086 JSVM_InitOptions init_options;
9087 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
9088 printf("memset_s failed");
9089 return nullptr;
9090 }
9091 init_options.externalReferences = externals;
9092 if (aa == 0) {
9093 OH_JSVM_Init(&init_options);
9094 aa++;
9095 }
9096 JSVM_VM vm;
9097 JSVM_CreateVMOptions options;
9098 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
9099 printf("memset_s failed");
9100 return nullptr;
9101 }
9102 OH_JSVM_CreateVM(&options, &vm);
9103 JSVM_VMScope vm_scope;
9104 OH_JSVM_OpenVMScope(vm, &vm_scope);
9105 JSVM_Env env;
9106 JSVM_CallbackStruct param[1];
9107 param[0].data = nullptr;
9108 param[0].callback = assertEqual;
9109 JSVM_PropertyDescriptor descriptor[] = {
9110 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
9111 };
9112 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
9113 JSVM_EnvScope envScope;
9114 OH_JSVM_OpenEnvScope(env, &envScope);
9115 JSVM_HandleScope handleScope;
9116 OH_JSVM_OpenHandleScope(env, &handleScope);
9117
9118 useArrayBuffer3(env);
9119
9120 OH_JSVM_CloseHandleScope(env, handleScope);
9121 OH_JSVM_CloseEnvScope(env, envScope);
9122 OH_JSVM_DestroyEnv(env);
9123 OH_JSVM_CloseVMScope(vm, vm_scope);
9124 OH_JSVM_DestroyVM(vm);
9125 napi_value result11;
9126 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
9127 return result11;
9128 }
9129
test_CompileWasmModule(JSVM_Env env)9130 void test_CompileWasmModule(JSVM_Env env)
9131 {
9132 JSVM_HandleScope handleScope;
9133 OH_JSVM_OpenHandleScope(env, &handleScope);
9134
9135 JSVM_Value wasmModule;
9136 JSVM_Status status = OH_JSVM_CompileWasmModule(env, nullptr, 0, nullptr, 0, nullptr, &wasmModule);
9137 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "OH_JSVM_CompileWasmModule check status");
9138
9139 OH_JSVM_CloseHandleScope(env, handleScope);
9140 }
9141
test_CompileWasmFunctionNULL(JSVM_Env env)9142 void test_CompileWasmFunctionNULL(JSVM_Env env)
9143 {
9144 JSVM_HandleScope handleScope;
9145 OH_JSVM_OpenHandleScope(env, &handleScope);
9146
9147 JSVM_Status status = OH_JSVM_CompileWasmFunction(env, nullptr, 0, JSVM_WASM_OPT_BASELINE);
9148 JSVM_ASSERT_RETURN_VOID(env, status == JSVM_INVALID_ARG, "OH_JSVM_CompileWasmFunction check status");
9149
9150 OH_JSVM_CloseHandleScope(env, handleScope);
9151 }
9152
test_IsWasmModuleValueisNULL(JSVM_Env env)9153 void test_IsWasmModuleValueisNULL(JSVM_Env env)
9154 {
9155 JSVM_HandleScope handleScope;
9156 OH_JSVM_OpenHandleScope(env, &handleScope);
9157
9158 bool result = false;
9159 JSVM_Status status = OH_JSVM_IsWasmModuleObject(env, nullptr, &result);
9160 JSVM_ASSERT_RETURN_VOID(env, status == JSVM_INVALID_ARG, "OH_JSVM_IsWasmModuleObject check status");
9161 JSVM_ASSERT_RETURN_VOID(env, result == false, "OH_JSVM_IsWasmModuleObject check result");
9162
9163 OH_JSVM_CloseHandleScope(env, handleScope);
9164 }
9165
test_CreateWarmCache_wasmmodule_is_null(JSVM_Env env)9166 void test_CreateWarmCache_wasmmodule_is_null(JSVM_Env env)
9167 {
9168 JSVM_HandleScope handleScope;
9169 OH_JSVM_OpenHandleScope(env, &handleScope);
9170
9171 const uint8_t *cacheData = nullptr;
9172 size_t cacheLength = 0;
9173 JSVM_Value wasmModule = nullptr;
9174 JSVM_Status status = OH_JSVM_CreateWasmCache(env, wasmModule, &cacheData, &cacheLength);
9175 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "OH_JSVM_CreateWarmCache check status not ok");
9176
9177 OH_JSVM_CloseHandleScope(env, handleScope);
9178 }
9179
test_CreateWarmCache_wasmmodule_is_not_wasmmodule(JSVM_Env env)9180 void test_CreateWarmCache_wasmmodule_is_not_wasmmodule(JSVM_Env env)
9181 {
9182 JSVM_HandleScope handleScope;
9183 OH_JSVM_OpenHandleScope(env, &handleScope);
9184
9185 const uint8_t *cacheData = nullptr;
9186 size_t cacheLength = 0;
9187 JSVM_Value wasmModule;
9188 OH_JSVM_CreateSet(env, &wasmModule);
9189 JSVM_Status status = OH_JSVM_CreateWasmCache(env, wasmModule, &cacheData, &cacheLength);
9190 JSVM_ASSERT_RETURN_VOID(env, status != JSVM_OK, "OH_JSVM_CreateWarmCache check status not ok");
9191
9192 OH_JSVM_CloseHandleScope(env, handleScope);
9193 }
9194
test_ReleaseCache_cachedata_is_null(JSVM_Env env)9195 void test_ReleaseCache_cachedata_is_null(JSVM_Env env)
9196 {
9197 JSVM_HandleScope handleScope;
9198 OH_JSVM_OpenHandleScope(env, &handleScope);
9199
9200 JSVM_Status status = OH_JSVM_ReleaseCache(env, nullptr, JSVM_CACHE_TYPE_JS);
9201 JSVM_ASSERT_RETURN_VOID(env, status == JSVM_INVALID_ARG, "OH_JSVM_CreateWarmCache check status is invalid_arg");
9202
9203 OH_JSVM_CloseHandleScope(env, handleScope);
9204 }
9205
testWasmOperator(napi_env env1,napi_callback_info info)9206 static napi_value testWasmOperator(napi_env env1, napi_callback_info info)
9207 {
9208 JSVM_InitOptions init_options;
9209 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != EOK) {
9210 printf("memset_s failed");
9211 return nullptr;
9212 }
9213 init_options.externalReferences = externals;
9214 if (aa == 0) {
9215 OH_JSVM_Init(&init_options);
9216 aa++;
9217 }
9218 JSVM_VM vm;
9219 JSVM_CreateVMOptions options;
9220 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
9221 printf("memset_s failed");
9222 return nullptr;
9223 }
9224 OH_JSVM_CreateVM(&options, &vm);
9225 JSVM_VMScope vm_scope;
9226 OH_JSVM_OpenVMScope(vm, &vm_scope);
9227 JSVM_Env env;
9228 JSVM_CallbackStruct param[1];
9229 param[0].data = nullptr;
9230 param[0].callback = assertEqual;
9231 JSVM_PropertyDescriptor descriptor[] = {
9232 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
9233 };
9234 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env);
9235 JSVM_EnvScope envScope;
9236 OH_JSVM_OpenEnvScope(env, &envScope);
9237
9238 test_CompileWasmModule(env);
9239 test_CompileWasmFunctionNULL(env);
9240 test_IsWasmModuleValueisNULL(env);
9241 test_CreateWarmCache_wasmmodule_is_null(env);
9242 test_CreateWarmCache_wasmmodule_is_not_wasmmodule(env);
9243 test_ReleaseCache_cachedata_is_null(env);
9244
9245 OH_JSVM_CloseEnvScope(env, envScope);
9246 OH_JSVM_DestroyEnv(env);
9247 OH_JSVM_CloseVMScope(vm, vm_scope);
9248 OH_JSVM_DestroyVM(vm);
9249 napi_value result11;
9250 NAPI_CALL(env1, napi_create_int32(env1, 0, &result11));
9251 return result11;
9252 }
9253
9254 static JSVM_CallbackStruct param[] = {
9255 {.data = nullptr, .callback = CreateStringUtf8},
9256 {.data = nullptr, .callback = GetValueStringUtf8},
9257 {.data = nullptr, .callback = CreateStringUtf16},
9258 {.data = nullptr, .callback = GetValueStringUtf16},
9259 {.data = nullptr, .callback = CreateStringLatin1},
9260 {.data = nullptr, .callback = GetValueStringLatin1},
9261 {.data = nullptr, .callback = CreateDate},
9262 {.data = nullptr, .callback = CreateObject},
9263 {.data = nullptr, .callback = CreateExternal},
9264 {.data = nullptr, .callback = GetValueExternal},
9265 {.data = nullptr, .callback = CreateArray},
9266 {.data = nullptr, .callback = CreateArrayWithLength},
9267 {.data = nullptr, .callback = CreateArraybuffer},
9268 {.data = nullptr, .callback = GetArrayLength},
9269 {.data = nullptr, .callback = CreateInt32},
9270 {.data = nullptr, .callback = GetValueInt32},
9271 {.data = nullptr, .callback = CreateUInt32},
9272 {.data = nullptr, .callback = GetValueUint32},
9273 {.data = nullptr, .callback = CreateInt64},
9274 {.data = nullptr, .callback = GetValueInt64},
9275 {.data = nullptr, .callback = CreateDouble},
9276 {.data = nullptr, .callback = GetDouble},
9277 {.data = nullptr, .callback = GetBoolean},
9278 {.data = nullptr, .callback = GetValueBool},
9279 {.data = nullptr, .callback = CreateBigintUint64},
9280 {.data = nullptr, .callback = GetValueBigintUint64},
9281 {.data = nullptr, .callback = CreateBigintInt64},
9282 {.data = nullptr, .callback = GetBigintInt64},
9283 {.data = nullptr, .callback = CreateBigintWords},
9284 {.data = nullptr, .callback = GetValueBigintWords},
9285 {.data = nullptr, .callback = GetNull},
9286 {.data = nullptr, .callback = GetUndefined},
9287 {.data = nullptr, .callback = GetGlobal},
9288 {.data = nullptr, .callback = CreateSymbol},
9289 {.data = nullptr, .callback = SymbolFor},
9290 {.data = nullptr, .callback = GetDateValue},
9291 {.data = nullptr, .callback = IsDate},
9292 {.data = nullptr, .callback = GetArraybufferInfo},
9293 {.data = nullptr, .callback = CreateTypedArray},
9294 {.data = nullptr, .callback = GetTypedArrayInfo},
9295 {.data = nullptr, .callback = CreateDataView},
9296 {.data = nullptr, .callback = GetDataViewInfo},
9297 {.data = nullptr, .callback = GetPrototype},
9298 {.data = nullptr, .callback = CreateReference},
9299 {.data = nullptr, .callback = UseReference},
9300 {.data = nullptr, .callback = DeleteReference},
9301 {.data = nullptr, .callback = EscapableHandleScopeTest},
9302 {.data = nullptr, .callback = HandleScopeTest},
9303 {.data = nullptr, .callback = HandleScope},
9304 {.data = nullptr, .callback = GetPropertyNames},
9305 {.data = nullptr, .callback = GetAllPropertyNames},
9306 {.data = nullptr, .callback = GetEnumerableNames},
9307 {.data = nullptr, .callback = GetOwnWritableNames},
9308 {.data = nullptr, .callback = GetEnumerableConfigurableNames},
9309 {.data = nullptr, .callback = GetOwnConfigurableNames},
9310 {.data = nullptr, .callback = SetProperty},
9311 {.data = nullptr, .callback = SetPropertyFailed},
9312 {.data = nullptr, .callback = GetProperty},
9313 {.data = nullptr, .callback = GetPropertyFailed},
9314 {.data = nullptr, .callback = HasProperty},
9315 {.data = nullptr, .callback = HasPropertyFailed},
9316 {.data = nullptr, .callback = DeleteProperty},
9317 {.data = nullptr, .callback = DeletePropertyFailed},
9318 {.data = nullptr, .callback = HasOwnProperty},
9319 {.data = nullptr, .callback = HasOwnPropertyFailed},
9320 {.data = nullptr, .callback = SetNamedPropertyCreateObject},
9321 {.data = nullptr, .callback = SetNamedProperty},
9322 {.data = nullptr, .callback = SetNamedPropertyFailed},
9323 {.data = nullptr, .callback = GetNamedProperty},
9324 {.data = nullptr, .callback = GetNamedPropertyFailed},
9325 {.data = nullptr, .callback = HasNamedProperty},
9326 {.data = nullptr, .callback = HasNamedPropertyFailed},
9327 {.data = nullptr, .callback = SetElementWithLength},
9328 {.data = nullptr, .callback = SetElement},
9329 {.data = nullptr, .callback = SetElementFailed},
9330 {.data = nullptr, .callback = GetElement},
9331 {.data = nullptr, .callback = GetElementFailed},
9332 {.data = nullptr, .callback = HasElement},
9333 {.data = nullptr, .callback = HasElementFailed},
9334 {.data = nullptr, .callback = DeleteElement},
9335 {.data = nullptr, .callback = DeleteElementAndCheck},
9336 {.data = nullptr, .callback = DeleteElementFailed},
9337 {.data = nullptr, .callback = DefineMethodProperties},
9338 {.data = nullptr, .callback = DefineStringProperties},
9339 {.data = nullptr, .callback = DefineGetterProperties},
9340 {.data = nullptr, .callback = DefinePropertiesFailed},
9341 {.data = nullptr, .callback = ObjectFreeze},
9342 {.data = nullptr, .callback = ObjectSeal},
9343 {.data = nullptr, .callback = GetVersion},
9344 {.data = nullptr, .callback = GetVersionFailed},
9345 {.data = nullptr, .callback = GetVMInfo},
9346 {.data = nullptr, .callback = JsonStringify},
9347 {.data = nullptr, .callback = JsonStringifyFailed},
9348 {.data = nullptr, .callback = JsonParseNumber},
9349 {.data = nullptr, .callback = JsonParseObject},
9350 {.data = nullptr, .callback = JsonParse},
9351 {.data = nullptr, .callback = JsonParseFailed},
9352 {.data = nullptr, .callback = CallFunction},
9353 {.data = nullptr, .callback = CallFunctionWithArg},
9354 {.data = nullptr, .callback = CallFunctionWithObject},
9355 {.data = nullptr, .callback = CallFunctionFailed},
9356 {.data = nullptr, .callback = CreateFunction},
9357 {.data = nullptr, .callback = CreateFunctionFailed},
9358 {.data = nullptr, .callback = GetHeapStatistics},
9359 {.data = nullptr, .callback = GetVM},
9360 {.data = nullptr, .callback = GetTypeof},
9361 {.data = nullptr, .callback = InstanceOf},
9362 {.data = nullptr, .callback = IsArray},
9363 {.data = nullptr, .callback = IsArrayBuffer},
9364 {.data = nullptr, .callback = IsTypedarray},
9365 {.data = nullptr, .callback = IsDataView},
9366 {.data = nullptr, .callback = IsStrictEquals},
9367 {.data = nullptr, .callback = DetachArraybuffer},
9368 {.data = nullptr, .callback = IsDetachedArraybuffer},
9369 {.data = nullptr, .callback = CoerceToBool},
9370 {.data = nullptr, .callback = CoerceToNumber},
9371 {.data = nullptr, .callback = CoerceToObject},
9372 {.data = nullptr, .callback = CoerceToString},
9373 {.data = nullptr, .callback = IsPromise},
9374 {.data = nullptr, .callback = CreatePromise},
9375 {.data = nullptr, .callback = ResolveRejectDeferred},
9376 {.data = nullptr, .callback = GetCbArgs},
9377 {.data = nullptr, .callback = NewInstance},
9378 {.data = nullptr, .callback = SetTypeTagToObject},
9379 {.data = nullptr, .callback = CheckObjectTypeTag},
9380 {.data = nullptr, .callback = AdjustExternalMemory},
9381 {.data = nullptr, .callback = MemoryPressureNotification},
9382 {.data = nullptr, .callback = WrapObject},
9383 {.data = nullptr, .callback = RemoveWrap},
9384 {.data = nullptr, .callback = HandleScopeFor},
9385 {.data = nullptr, .callback = JsVmThrow},
9386 {.data = nullptr, .callback = JsVmThrowFromJS},
9387 {.data = nullptr, .callback = JsVmThrowError},
9388 {.data = nullptr, .callback = JsVmThrowTypeError},
9389 {.data = nullptr, .callback = JsVmThrowRangeError},
9390 {.data = nullptr, .callback = JsVmThrowSyntaxError},
9391 {.data = nullptr, .callback = JsVmIsError},
9392 {.data = nullptr, .callback = JsVmCreateError},
9393 {.data = nullptr, .callback = JsVmCreateTypeError},
9394 {.data = nullptr, .callback = JsVmCreateRangeError},
9395 {.data = nullptr, .callback = JsVmCreateSyntaxError},
9396 {.data = nullptr, .callback = JsVmGetLastErrorInfo},
9397 {.data = nullptr, .callback = JsVmGetAndClearLastException},
9398 {.data = nullptr, .callback = JsVmIsExceptionPending},
9399 {.data = nullptr, .callback = CreateSnapshotFailed},
9400 {.data = nullptr, .callback = DebugCodeUsingCpuProfiler},
9401 {.data = nullptr, .callback = TakeHeapSnapshot},
9402 {.data = nullptr, .callback = DebugCodeUsingInspector},
9403 {.data = nullptr, .callback = WaitForDebuggerFailed},
9404 {.data = nullptr, .callback = SetInstanceData},
9405 {.data = nullptr, .callback = GetInstanceData},
9406 {.data = nullptr, .callback = IsConstructor},
9407 {.data = nullptr, .callback = CreateRegExp1},
9408 {.data = nullptr, .callback = CreateRegExp2},
9409 {.data = nullptr, .callback = CreateRegExp3},
9410 {.data = nullptr, .callback = CreateRegExp4},
9411 {.data = nullptr, .callback = CreateRegExp5},
9412 {.data = nullptr, .callback = CreateRegExpEnvNullptr},
9413 {.data = nullptr, .callback = CreateFunctionWithScript},
9414 {.data = nullptr, .callback = CreateMap},
9415 {.data = nullptr, .callback = IsRegExp},
9416 {.data = nullptr, .callback = CoerceToBigInt},
9417 };
9418 static JSVM_PropertyDescriptor jsDescriptor[] = {
9419 {"createStringUtf8", nullptr, ¶m[0], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9420 {"getValueStringUtf8", nullptr, ¶m[1], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9421 {"createStringUtf16", nullptr, ¶m[2], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9422 {"getValueStringUtf16", nullptr, ¶m[3], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9423 {"createStringLatin1", nullptr, ¶m[4], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9424 {"getValueStringLatin1", nullptr, ¶m[5], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9425 {"createDate", nullptr, ¶m[6], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9426 {"createObject", nullptr, ¶m[7], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9427 {"createExternal", nullptr, ¶m[8], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9428 {"getValueExternal", nullptr, ¶m[9], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9429 {"createArray", nullptr, ¶m[10], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9430 {"createArrayWithLength", nullptr, ¶m[11], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9431 {"createArraybuffer", nullptr, ¶m[12], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9432 {"getArrayLength", nullptr, ¶m[13], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9433 {"createInt32", nullptr, ¶m[14], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9434 {"getValueInt32", nullptr, ¶m[15], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9435 {"createUInt32", nullptr, ¶m[16], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9436 {"getValueUint32", nullptr, ¶m[17], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9437 {"createInt64", nullptr, ¶m[18], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9438 {"getValueInt64", nullptr, ¶m[19], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9439 {"createDouble", nullptr, ¶m[20], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9440 {"getDouble", nullptr, ¶m[21], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9441 {"getBoolean", nullptr, ¶m[22], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9442 {"getValueBool", nullptr, ¶m[23], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9443 {"createBigintUint64", nullptr, ¶m[24], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9444 {"getValueBigintUint64", nullptr, ¶m[25], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9445 {"createBigintInt64", nullptr, ¶m[26], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9446 {"getBigintInt64", nullptr, ¶m[27], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9447 {"createBigintWords", nullptr, ¶m[28], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9448 {"getValueBigintWords", nullptr, ¶m[29], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9449 {"getNull", nullptr, ¶m[30], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9450 {"getUndefined", nullptr, ¶m[31], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9451 {"getGlobal", nullptr, ¶m[32], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9452 {"createSymbol", nullptr, ¶m[33], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9453 {"symbolFor", nullptr, ¶m[34], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9454 {"getDateValue", nullptr, ¶m[35], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9455 {"isDate", nullptr, ¶m[36], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9456 {"getArraybufferInfo", nullptr, ¶m[37], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9457 {"createTypedArray", nullptr, ¶m[38], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9458 {"getTypedArrayInfo", nullptr, ¶m[39], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9459 {"createDataView", nullptr, ¶m[40], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9460 {"getDataViewInfo", nullptr, ¶m[41], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9461 {"getPrototype", nullptr, ¶m[42], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9462 {"createReference", nullptr, ¶m[43], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9463 {"useReference", nullptr, ¶m[44], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9464 {"deleteReference", nullptr, ¶m[45], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9465 {"escapableHandleScopeTest", nullptr, ¶m[46], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9466 {"handleScopeTest", nullptr, ¶m[47], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9467 {"handleScope", nullptr, ¶m[48], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9468 {"getPropertyNames", nullptr, ¶m[49], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9469 {"getAllPropertyNames", nullptr, ¶m[50], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9470 {"getEnumerableNames", nullptr, ¶m[51], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9471 {"getOwnWritableNames", nullptr, ¶m[52], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9472 {"getEnumerableConfigurableNames", nullptr, ¶m[53], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9473 {"getOwnConfigurableNames", nullptr, ¶m[54], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9474 {"jsvmSetProperty", nullptr, ¶m[55], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9475 {"setPropertyFailed", nullptr, ¶m[56], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9476 {"getProperty", nullptr, ¶m[57], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9477 {"getPropertyFailed", nullptr, ¶m[58], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9478 {"hasProperty", nullptr, ¶m[59], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9479 {"hasPropertyFailed", nullptr, ¶m[60], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9480 {"deleteProperty", nullptr, ¶m[61], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9481 {"deletePropertyFailed", nullptr, ¶m[62], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9482 {"hasOwnProperty", nullptr, ¶m[63], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9483 {"hasOwnPropertyFailed", nullptr, ¶m[64], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9484 {"setNamedPropertyCreateObject", nullptr, ¶m[65], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9485 {"setNamedProperty", nullptr, ¶m[66], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9486 {"setNamedPropertyFailed", nullptr, ¶m[67], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9487 {"getNamedProperty", nullptr, ¶m[68], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9488 {"getNamedPropertyFailed", nullptr, ¶m[69], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9489 {"hasNamedProperty", nullptr, ¶m[70], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9490 {"hasNamedPropertyFailed", nullptr, ¶m[71], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9491 {"setElementWithLength", nullptr, ¶m[72], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9492 {"setElement", nullptr, ¶m[73], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9493 {"setElementFailed", nullptr, ¶m[74], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9494 {"getElement", nullptr, ¶m[75], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9495 {"getElementFailed", nullptr, ¶m[76], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9496 {"hasElement", nullptr, ¶m[77], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9497 {"hasElementFailed", nullptr, ¶m[78], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9498 {"deleteElement", nullptr, ¶m[79], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9499 {"deleteElementAndCheck", nullptr, ¶m[80], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9500 {"deleteElementFailed", nullptr, ¶m[81], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9501 {"defineMethodProperties", nullptr, ¶m[82], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9502 {"defineStringProperties", nullptr, ¶m[83], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9503 {"defineGetterProperties", nullptr, ¶m[84], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9504 {"definePropertiesFailed", nullptr, ¶m[85], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9505 {"objectFreeze", nullptr, ¶m[86], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9506 {"objectSeal", nullptr, ¶m[87], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9507 {"getVersion", nullptr, ¶m[88], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9508 {"getVersionFailed", nullptr, ¶m[89], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9509 {"getVMInfo", nullptr, ¶m[90], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9510 {"jsonStringify", nullptr, ¶m[91], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9511 {"jsonStringifyFailed", nullptr, ¶m[92], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9512 {"jsonParseNumber", nullptr, ¶m[93], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9513 {"jsonParseObject", nullptr, ¶m[94], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9514 {"jsonParse", nullptr, ¶m[95], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9515 {"jsonParseFailed", nullptr, ¶m[96], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9516 {"callFunction", nullptr, ¶m[97], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9517 {"callFunctionWithArg", nullptr, ¶m[98], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9518 {"callFunctionWithObject", nullptr, ¶m[99], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9519 {"callFunctionFailed", nullptr, ¶m[100], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9520 {"createFunction", nullptr, ¶m[101], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9521 {"createFunctionFailed", nullptr, ¶m[102], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9522 {"getHeapStatistics", nullptr, ¶m[103], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9523 {"getVM", nullptr, ¶m[104], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9524 {"getTypeof", nullptr, ¶m[105], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9525 {"instanceOf", nullptr, ¶m[106], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9526 {"isArray", nullptr, ¶m[107], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9527 {"isArrayBuffer", nullptr, ¶m[108], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9528 {"isTypedarray", nullptr, ¶m[109], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9529 {"isDataView", nullptr, ¶m[110], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9530 {"isStrictEquals", nullptr, ¶m[111], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9531 {"detachArraybuffer", nullptr, ¶m[112], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9532 {"isDetachedArraybuffer", nullptr, ¶m[113], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9533 {"coerceToBool", nullptr, ¶m[114], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9534 {"coerceToNumber", nullptr, ¶m[115], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9535 {"coerceToObject", nullptr, ¶m[116], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9536 {"coerceToString", nullptr, ¶m[117], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9537 {"isPromise", nullptr, ¶m[118], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9538 {"createPromise", nullptr, ¶m[119], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9539 {"resolveRejectDeferred", nullptr, ¶m[120], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9540 {"getCbArgs", nullptr, ¶m[121], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9541 {"newInstance", nullptr, ¶m[122], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9542 {"setTypeTagToObject", nullptr, ¶m[123], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9543 {"checkObjectTypeTag", nullptr, ¶m[124], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9544 {"adjustExternalMemory", nullptr, ¶m[125], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9545 {"memoryPressureNotification", nullptr, ¶m[126], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9546 {"wrapObject", nullptr, ¶m[127], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9547 {"removeWrap", nullptr, ¶m[128], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9548 {"handleScopeFor", nullptr, ¶m[129], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9549 {"jsVmThrow", nullptr, ¶m[130], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9550 {"jsVmThrowFromJS", nullptr, ¶m[131], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9551 {"jsVmThrowError", nullptr, ¶m[132], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9552 {"jsVmThrowTypeError", nullptr, ¶m[133], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9553 {"jsVmThrowRangeError", nullptr, ¶m[134], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9554 {"jsVmThrowSyntaxError", nullptr, ¶m[135], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9555 {"jsVmIsError", nullptr, ¶m[136], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9556 {"jsVmCreateError", nullptr, ¶m[137], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9557 {"jsVmCreateTypeError", nullptr, ¶m[138], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9558 {"jsVmCreateRangeError", nullptr, ¶m[139], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9559 {"jsVmCreateSyntaxError", nullptr, ¶m[140], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9560 {"jsVmGetLastErrorInfo", nullptr, ¶m[141], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9561 {"jsVmGetAndClearLastException", nullptr, ¶m[142], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9562 {"jsVmIsExceptionPending", nullptr, ¶m[143], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9563 {"createSnapshotFailed", nullptr, ¶m[144], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9564 {"debugCodeUsingCpuProfiler", nullptr, ¶m[145], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9565 {"takeHeapSnapshot", nullptr, ¶m[146], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9566 {"debugCodeUsingInspector", nullptr, ¶m[147], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9567 {"waitForDebuggerFailed", nullptr, ¶m[148], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9568 {"setInstanceData", nullptr, ¶m[149], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9569 {"getInstanceData", nullptr, ¶m[150], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9570 {"isConstructor", nullptr, ¶m[151], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9571 {"createRegExp1", nullptr, ¶m[152], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9572 {"createRegExp2", nullptr, ¶m[153], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9573 {"createRegExp3", nullptr, ¶m[154], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9574 {"createRegExp4", nullptr, ¶m[155], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9575 {"createRegExp5", nullptr, ¶m[156], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9576 {"createRegExpEnvNullptr", nullptr, ¶m[157], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9577 {"createFunctionWithScript", nullptr, ¶m[158], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9578 {"createMap", nullptr, ¶m[159], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9579 {"isRegExp", nullptr, ¶m[160], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9580 {"coerceToBigInt", nullptr, ¶m[161], nullptr, nullptr, nullptr, JSVM_DEFAULT},
9581 };
9582
GetArrayType(JSVM_TypedarrayType typeNum)9583 napi_typedarray_type GetArrayType(JSVM_TypedarrayType typeNum)
9584 {
9585 napi_typedarray_type arrayType;
9586 if (typeNum >= JSVM_INT8_ARRAY && typeNum <= JSVM_BIGUINT64_ARRAY) {
9587 arrayType = static_cast<napi_typedarray_type>(typeNum);
9588 } else {
9589 arrayType = napi_int8_array;
9590 }
9591 return arrayType;
9592 }
9593
GetArraySize(JSVM_TypedarrayType typeNum)9594 size_t GetArraySize(JSVM_TypedarrayType typeNum)
9595 {
9596 size_t elementSize = 0;
9597 switch (typeNum) {
9598 case JSVM_INT8_ARRAY:
9599 case JSVM_UINT8_ARRAY:
9600 case JSVM_UINT8_CLAMPED_ARRAY:
9601 elementSize = sizeof(int8_t);
9602 break;
9603 case JSVM_INT16_ARRAY:
9604 case JSVM_UINT16_ARRAY:
9605 elementSize = sizeof(int16_t);
9606 break;
9607 case JSVM_INT32_ARRAY:
9608 case JSVM_UINT32_ARRAY:
9609 elementSize = sizeof(int32_t);
9610 break;
9611 case JSVM_FLOAT32_ARRAY:
9612 elementSize = sizeof(float);
9613 break;
9614 case JSVM_FLOAT64_ARRAY:
9615 elementSize = sizeof(double);
9616 break;
9617 case JSVM_BIGINT64_ARRAY:
9618 case JSVM_BIGUINT64_ARRAY:
9619 elementSize = sizeof(int64_t);
9620 break;
9621 default:
9622 elementSize = sizeof(int8_t);
9623 break;
9624 }
9625 return elementSize;
9626 }
9627
9628 void ConvertResults(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue);
9629
ConvertString(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9630 void ConvertString(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9631 {
9632 if (strcmp(g_dataType, "utf8") == 0) {
9633 size_t length;
9634 OH_JSVM_GetValueStringUtf8(jsVmEnv, jSVmValue, nullptr, 0, &length);
9635 if (length <= 0) {
9636 return;
9637 }
9638 char *buffer = (char *)malloc(length + 1);
9639 if (buffer == nullptr) {
9640 return;
9641 }
9642 OH_JSVM_GetValueStringUtf8(jsVmEnv, jSVmValue, buffer, length + 1, nullptr);
9643 napi_create_string_utf8(napiEnv, buffer, length, napiValue);
9644 free(buffer);
9645 } else if (strcmp(g_dataType, "utf16") == 0) {
9646 char16_t buffer[128];
9647 size_t bufferSize = 128;
9648 size_t copied = 0;
9649 OH_JSVM_GetValueStringUtf16(jsVmEnv, jSVmValue, buffer, bufferSize, &copied);
9650 napi_create_string_utf16(napiEnv, buffer, copied, napiValue);
9651 } else if (strcmp(g_dataType, "latin") == 0) {
9652 size_t length;
9653 OH_JSVM_GetValueStringLatin1(jsVmEnv, jSVmValue, nullptr, 0, &length);
9654 if (length <= 0) {
9655 return;
9656 }
9657 char *buffer = (char *)malloc(length + 1);
9658 if (buffer == nullptr) {
9659 return;
9660 }
9661 OH_JSVM_GetValueStringLatin1(jsVmEnv, jSVmValue, buffer, length + 1, nullptr);
9662 napi_create_string_latin1(napiEnv, buffer, length, napiValue);
9663 free(buffer);
9664 } else {
9665 size_t length;
9666 OH_JSVM_GetValueStringUtf8(jsVmEnv, jSVmValue, nullptr, 0, &length);
9667 if (length <= 0) {
9668 return;
9669 }
9670 char *buffer = (char *)malloc(length + 1);
9671 if (buffer == nullptr) {
9672 return;
9673 }
9674 OH_JSVM_GetValueStringUtf8(jsVmEnv, jSVmValue, buffer, length + 1, nullptr);
9675 napi_create_string_utf8(napiEnv, buffer, length, napiValue);
9676 free(buffer);
9677 }
9678 }
9679
ConvertNumber(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9680 void ConvertNumber(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9681 {
9682 if (strcmp(g_dataType, "double") == 0) {
9683 double doubleValue;
9684 JSVM_Status doubleStatus = OH_JSVM_GetValueDouble(jsVmEnv, jSVmValue, &doubleValue);
9685 if (doubleStatus == JSVM_OK) {
9686 napi_create_double(napiEnv, doubleValue, napiValue);
9687 } else {
9688 napi_create_double(napiEnv, 0, napiValue);
9689 }
9690 } else if (strcmp(g_dataType, "int") == 0) {
9691 int64_t value;
9692 JSVM_Status int64Status = OH_JSVM_GetValueInt64(jsVmEnv, jSVmValue, &value);
9693 if (int64Status == JSVM_OK) {
9694 napi_create_int64(napiEnv, value, napiValue);
9695 } else {
9696 napi_create_int64(napiEnv, 0, napiValue);
9697 }
9698 } else {
9699 int64_t value;
9700 JSVM_Status int64Status = OH_JSVM_GetValueInt64(jsVmEnv, jSVmValue, &value);
9701 if (int64Status == JSVM_OK) {
9702 napi_create_int64(napiEnv, value, napiValue);
9703 } else {
9704 napi_create_int64(napiEnv, 0, napiValue);
9705 }
9706 }
9707 }
9708
ConvertBigInt(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9709 void ConvertBigInt(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9710 {
9711 if (strcmp(g_dataType, "BigintInt64") == 0) {
9712 int64_t intValue;
9713 bool lossless = false;
9714 OH_JSVM_GetValueBigintInt64(jsVmEnv, jSVmValue, &intValue, &lossless);
9715 napi_create_bigint_int64(napiEnv, intValue, napiValue);
9716 }
9717 if (strcmp(g_dataType, "BigintUint64") == 0) {
9718 uint64_t uintValue;
9719 bool lossless = false;
9720 OH_JSVM_GetValueBigintUint64(jsVmEnv, jSVmValue, &uintValue, &lossless);
9721 napi_create_bigint_uint64(napiEnv, uintValue, napiValue);
9722 }
9723 if (strcmp(g_dataType, "BigintWords") == 0) {
9724 int retSignBit = 0;
9725 size_t retWordCount = 4;
9726 uint64_t wordsOut[] = {0ULL, 0ULL, 0ULL};
9727 OH_JSVM_GetValueBigintWords(jsVmEnv, jSVmValue, &retSignBit, &retWordCount, wordsOut);
9728 napi_create_bigint_words(napiEnv, 0, DIFF_VALUE_THREE, wordsOut, napiValue);
9729 }
9730 }
9731
ConvertObjectArray(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9732 void ConvertObjectArray(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9733 {
9734 uint32_t length;
9735 OH_JSVM_GetArrayLength(jsVmEnv, jSVmValue, &length);
9736 napi_create_array(napiEnv, napiValue);
9737 for (uint32_t i = 0; i < length; i++) {
9738 JSVM_Value jsvmElement;
9739 OH_JSVM_GetElement(jsVmEnv, jSVmValue, i, &jsvmElement);
9740 GetResultType(jsVmEnv, jsvmElement);
9741 napi_value napiElement;
9742 ConvertResults(jsVmEnv, jsvmElement, napiEnv, &napiElement);
9743 napi_set_element(napiEnv, *napiValue, i, napiElement);
9744 }
9745 }
9746
ConvertObjectObjStr(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9747 void ConvertObjectObjStr(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9748 {
9749 JSVM_Value propNames = nullptr;
9750 OH_JSVM_GetPropertyNames(jsVmEnv, jSVmValue, &propNames);
9751 bool isArray = false;
9752 OH_JSVM_IsArray(jsVmEnv, propNames, &isArray);
9753 uint32_t arrayLength = 0;
9754 OH_JSVM_GetArrayLength(jsVmEnv, propNames, &arrayLength);
9755 napi_create_object(napiEnv, napiValue);
9756 for (uint32_t i = 0; i < arrayLength; i++) {
9757 strcpy_s(g_dataType, sizeof(g_dataType), "utf8");
9758 JSVM_Value propName = nullptr;
9759 OH_JSVM_GetElement(jsVmEnv, propNames, i, &propName);
9760 napi_value propNameNapi;
9761 ConvertString(jsVmEnv, propName, napiEnv, &propNameNapi);
9762 JSVM_Value propValue = nullptr;
9763 OH_JSVM_GetProperty(jsVmEnv, jSVmValue, propName, &propValue);
9764 napi_value propValueNapi;
9765 GetResultType(jsVmEnv, propValue);
9766 ConvertResults(jsVmEnv, propValue, napiEnv, &propValueNapi);
9767 napi_set_property(napiEnv, *napiValue, propNameNapi, propValueNapi);
9768 }
9769 }
9770
ConvertObjectArraybuffer(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9771 void ConvertObjectArraybuffer(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9772 {
9773 JSVM_Value byteLen;
9774 OH_JSVM_GetNamedProperty(jsVmEnv, jSVmValue, "byteLength", &byteLen);
9775 int32_t length;
9776 OH_JSVM_GetValueInt32(jsVmEnv, byteLen, &length);
9777 void *data;
9778 napi_create_arraybuffer(napiEnv, length, &data, napiValue);
9779 }
9780
ConvertObjectDate(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9781 void ConvertObjectDate(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9782 {
9783 double value;
9784 OH_JSVM_GetDateValue(jsVmEnv, jSVmValue, &value);
9785 napi_create_date(napiEnv, value, napiValue);
9786 }
9787
ConvertObjectArrayBufferInfo(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9788 void ConvertObjectArrayBufferInfo(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9789 {
9790 JSVM_Value proLen;
9791 OH_JSVM_GetNamedProperty(jsVmEnv, jSVmValue, "byteLength", &proLen);
9792 int32_t len;
9793 OH_JSVM_GetValueInt32(jsVmEnv, proLen, &len);
9794
9795 napi_create_object(napiEnv, napiValue);
9796 napi_value length;
9797 napi_create_uint32(napiEnv, len, &length);
9798 napi_set_named_property(napiEnv, *napiValue, "byteLength", length);
9799 JSVM_Value vmBufferData;
9800 OH_JSVM_GetNamedProperty(jsVmEnv, jSVmValue, "buffer", &vmBufferData);
9801 size_t buffLength;
9802 void *data;
9803 OH_JSVM_GetArraybufferInfo(jsVmEnv, vmBufferData, &data, &buffLength);
9804 napi_value bufferData;
9805 napi_create_arraybuffer(napiEnv, len, &data, &bufferData);
9806 napi_set_named_property(napiEnv, *napiValue, "buffer", bufferData);
9807 }
9808
ConvertObjectTypeArray(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9809 void ConvertObjectTypeArray(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9810 {
9811 void *data;
9812 JSVM_TypedarrayType type;
9813 size_t byteOffset;
9814 size_t length;
9815 size_t elementSize = 0;
9816 JSVM_Value arraybuffer;
9817 OH_JSVM_GetTypedarrayInfo(jsVmEnv, jSVmValue, &type, &length, &data, &arraybuffer, &byteOffset);
9818 napi_typedarray_type arraytype;
9819 elementSize = GetArraySize(type);
9820 arraytype = GetArrayType(type);
9821 napi_value arrayBuffer;
9822 napi_create_arraybuffer(napiEnv, length * elementSize, (void **)&data, &arrayBuffer);
9823 napi_create_typedarray(napiEnv, arraytype, length, arrayBuffer, 0, napiValue);
9824 }
9825
ConvertJsObject(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9826 void ConvertJsObject(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9827 {
9828 JSVM_Value nameValue;
9829 JSVM_Value propNameValue;
9830 OH_JSVM_CreateStringUtf8(jsVmEnv, "name", JSVM_AUTO_LENGTH, &propNameValue);
9831
9832 OH_JSVM_GetProperty(jsVmEnv, jSVmValue, propNameValue, &nameValue);
9833 size_t bufsize = 0;
9834 OH_JSVM_GetValueStringUtf8(jsVmEnv, nameValue, nullptr, 0, &bufsize);
9835 if (bufsize <= 0) {
9836 printf("Bufsize error!\n");
9837 }
9838 char *buf = (char *)malloc(bufsize + 1);
9839 if (buf == nullptr) {
9840 printf("Memory allocation failed!\n");
9841 }
9842 OH_JSVM_GetValueStringUtf8(jsVmEnv, nameValue, buf, bufsize + 1, &bufsize);
9843 napi_create_object(napiEnv, napiValue);
9844 napi_value name;
9845 napi_create_string_utf8(napiEnv, "name", NAPI_AUTO_LENGTH, &name);
9846 napi_value value;
9847 napi_create_string_utf8(napiEnv, buf, NAPI_AUTO_LENGTH, &value);
9848 napi_set_property(napiEnv, *napiValue, name, value);
9849
9850 JSVM_Value idValue;
9851 JSVM_Value propIDValue;
9852 OH_JSVM_CreateStringUtf8(jsVmEnv, "id", JSVM_AUTO_LENGTH, &propIDValue);
9853 OH_JSVM_GetProperty(jsVmEnv, jSVmValue, propIDValue, &idValue);
9854 size_t idSize = 0;
9855 OH_JSVM_GetValueStringUtf8(jsVmEnv, idValue, nullptr, 0, &idSize);
9856 if (idSize > 0) {
9857 char *idBuf = (char *)malloc(idSize + 1);
9858 if (idBuf == nullptr) {
9859 printf("Memory allocation failed!\n");
9860 }
9861 OH_JSVM_GetValueStringUtf8(jsVmEnv, idValue, idBuf, idSize + 1, &idSize);
9862 napi_value id;
9863 napi_create_string_utf8(napiEnv, "id", NAPI_AUTO_LENGTH, &id);
9864 napi_value id_value;
9865 napi_create_string_utf8(napiEnv, idBuf, NAPI_AUTO_LENGTH, &id_value);
9866 napi_set_property(napiEnv, *napiValue, id, id_value);
9867 }
9868 }
9869
ConvertObject(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9870 void ConvertObject(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9871 {
9872 if (strcmp(g_dataType, "array") == 0) {
9873 ConvertObjectArray(jsVmEnv, jSVmValue, napiEnv, napiValue);
9874 }
9875 if (strcmp(g_dataType, "arraybuffer") == 0) {
9876 ConvertObjectArraybuffer(jsVmEnv, jSVmValue, napiEnv, napiValue);
9877 }
9878 if (strcmp(g_dataType, "date") == 0) {
9879 ConvertObjectDate(jsVmEnv, jSVmValue, napiEnv, napiValue);
9880 }
9881 if (strcmp(g_dataType, "arraybufferinfo") == 0) {
9882 ConvertObjectArrayBufferInfo(jsVmEnv, jSVmValue, napiEnv, napiValue);
9883 }
9884 if (strcmp(g_dataType, "typearray") == 0) {
9885 ConvertObjectTypeArray(jsVmEnv, jSVmValue, napiEnv, napiValue);
9886 }
9887 if (strcmp(g_dataType, "object") == 0) {
9888 ConvertJsObject(jsVmEnv, jSVmValue, napiEnv, napiValue);
9889 }
9890 if (strcmp(g_dataType, "objectstr") == 0) {
9891 ConvertObjectObjStr(jsVmEnv, jSVmValue, napiEnv, napiValue);
9892 }
9893 }
9894
ConvertResults(JSVM_Env jsVmEnv,JSVM_Value jSVmValue,napi_env napiEnv,napi_value * napiValue)9895 void ConvertResults(JSVM_Env jsVmEnv, JSVM_Value jSVmValue, napi_env napiEnv, napi_value *napiValue)
9896 {
9897 JSVM_ValueType resultType;
9898 OH_JSVM_Typeof(jsVmEnv, jSVmValue, &resultType);
9899 switch (resultType) {
9900 case JSVM_UNDEFINED: {
9901 napi_get_undefined(napiEnv, napiValue);
9902 break;
9903 }
9904 case JSVM_NULL: {
9905 napi_get_null(napiEnv, napiValue);
9906 break;
9907 }
9908 case JSVM_BOOLEAN: {
9909 bool value = false;
9910 OH_JSVM_GetValueBool(jsVmEnv, jSVmValue, &value);
9911 napi_get_boolean(napiEnv, value, napiValue);
9912 break;
9913 }
9914 case JSVM_NUMBER: {
9915 ConvertNumber(jsVmEnv, jSVmValue, napiEnv, napiValue);
9916 break;
9917 }
9918 case JSVM_STRING: {
9919 ConvertString(jsVmEnv, jSVmValue, napiEnv, napiValue);
9920 break;
9921 }
9922 case JSVM_OBJECT: {
9923 ConvertObject(jsVmEnv, jSVmValue, napiEnv, napiValue);
9924 break;
9925 }
9926 case JSVM_EXTERNAL: {
9927 void **result = nullptr;
9928 JSVM_Status externalStatus = OH_JSVM_GetValueExternal(jsVmEnv, jSVmValue, result);
9929 if (externalStatus == JSVM_OK) {
9930 napi_create_external(napiEnv, result, nullptr, nullptr, napiValue);
9931 }
9932 break;
9933 }
9934 case JSVM_BIGINT: {
9935 ConvertBigInt(jsVmEnv, jSVmValue, napiEnv, napiValue);
9936 break;
9937 }
9938 default: {
9939 break;
9940 }
9941 }
9942 }
9943
RunScriptFail(napi_env & nEnv,JSVM_Env & env)9944 void RunScriptFail(napi_env &nEnv, JSVM_Env &env)
9945 {
9946 JSVM_Value exceptionValue;
9947 JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &exceptionValue);
9948 if (status == JSVM_OK) {
9949 JSVM_Value message;
9950 OH_JSVM_GetNamedProperty(env, exceptionValue, "message", &message);
9951 size_t length = 0;
9952 OH_JSVM_GetValueStringUtf8(env, message, nullptr, 0, &length);
9953 if (length <= 0) {
9954 printf("Get length error!\n");
9955 }
9956 char *buffer = (char *)malloc(length + 1);
9957 if (buffer == nullptr) {
9958 printf("Memory allocation failed!\n");
9959 }
9960 OH_JSVM_GetValueStringUtf8(env, message, buffer, length + 1, nullptr);
9961 OH_JSVM_GetNamedProperty(env, exceptionValue, "name", &message);
9962 size_t nameLength;
9963 OH_JSVM_GetValueStringUtf8(env, message, nullptr, 0, &nameLength);
9964 if (nameLength <= 0) {
9965 printf("Get nameLength error!\n");
9966 }
9967 char *nameBuffer = (char *)malloc(nameLength + 1);
9968 if (nameBuffer == nullptr) {
9969 printf("Memory allocation failed!\n");
9970 }
9971 OH_JSVM_GetValueStringUtf8(env, message, nameBuffer, nameLength + 1, nullptr);
9972 if (strcmp(nameBuffer, "TypeError") == 0) {
9973 napi_throw_type_error(nEnv, nameBuffer, buffer);
9974 } else if (strcmp(nameBuffer, "RangeError") == 0) {
9975 napi_throw_range_error(nEnv, nameBuffer, buffer);
9976 } else {
9977 napi_throw_error(nEnv, "-1", buffer);
9978 }
9979 free(nameBuffer);
9980 free(buffer);
9981 } else {
9982 napi_throw_error(nEnv, "-1", nullptr);
9983 }
9984 }
9985
ExecuteJsScript(napi_env nEnv,JSVM_Env env,char * buf)9986 napi_value ExecuteJsScript(napi_env nEnv, JSVM_Env env, char *buf)
9987 {
9988 const char *sourceCodeStr = buf;
9989 JSVM_Value sourceCodeValue = nullptr;
9990 OH_JSVM_CreateStringUtf8(env, sourceCodeStr, strlen(sourceCodeStr), &sourceCodeValue);
9991 JSVM_Script script;
9992 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
9993 JSVM_Value jsVmResult = nullptr;
9994 napi_value nResult = nullptr;
9995 if (strcmp(sourceCodeStr, "defineClass") == 0) {
9996 JSVM_Value obj = nullptr;
9997 DefineClass(env, obj);
9998 nResult = GetObj(nEnv);
9999 } else {
10000 JSVM_Value sourceCodeValue = nullptr;
10001 OH_JSVM_CreateStringUtf8(env, sourceCodeStr, strlen(sourceCodeStr), &sourceCodeValue);
10002 JSVM_Script script;
10003 OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script);
10004 JSVM_Status res = OH_JSVM_RunScript(env, script, &jsVmResult);
10005 if (res != JSVM_OK) {
10006 RunScriptFail(nEnv, env);
10007 }
10008 ConvertResults(env, jsVmResult, nEnv, &nResult);
10009 }
10010 return nResult;
10011 }
10012
GetJsCode(napi_env nEnv,napi_callback_info nInfo,char * buf)10013 void GetJsCode(napi_env nEnv, napi_callback_info nInfo, char *buf)
10014 {
10015 size_t argc = 1;
10016 napi_value argv[1];
10017 napi_get_cb_info(nEnv, nInfo, &argc, argv, nullptr, nullptr);
10018 napi_get_value_string_utf8(nEnv, argv[0], buf, RUN_JS_CODE_LENGTH, nullptr);
10019 }
10020
RunJsVm(napi_env nEnv,napi_callback_info nInfo)10021 static napi_value RunJsVm(napi_env nEnv, napi_callback_info nInfo)
10022 {
10023 char *buf = (char *)malloc(RUN_JS_CODE_LENGTH);
10024 if (buf == nullptr) {
10025 return nullptr;
10026 }
10027 GetJsCode(nEnv, nInfo, buf);
10028 JSVM_InitOptions initOptions;
10029 if (memset_s(&initOptions, sizeof(initOptions), 0, sizeof(initOptions)) != EOK) {
10030 return nullptr;
10031 }
10032 initOptions.externalReferences = g_externalsSnapshot;
10033 if (aa == 0) {
10034 aa++;
10035 OH_JSVM_Init(&initOptions);
10036 }
10037 JSVM_VM vm;
10038 JSVM_CreateVMOptions options;
10039 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != EOK) {
10040 return nullptr;
10041 }
10042 OH_JSVM_CreateVM(&options, &vm);
10043 JSVM_VMScope vmScope;
10044 OH_JSVM_OpenVMScope(vm, &vmScope);
10045 JSVM_Env env;
10046 OH_JSVM_CreateEnv(vm, sizeof(jsDescriptor) / sizeof(jsDescriptor[0]), jsDescriptor, &env);
10047 JSVM_EnvScope envScope;
10048 OH_JSVM_OpenEnvScope(env, &envScope);
10049 JSVM_HandleScope handleScope;
10050 OH_JSVM_OpenHandleScope(env, &handleScope);
10051 napi_value nResult = ExecuteJsScript(nEnv, env, buf);
10052 if (nResult == nullptr) {
10053 return nullptr;
10054 }
10055 OH_JSVM_CloseHandleScope(env, handleScope);
10056 OH_JSVM_CloseEnvScope(env, envScope);
10057 OH_JSVM_CloseVMScope(vm, vmScope);
10058 OH_JSVM_DestroyEnv(env);
10059 OH_JSVM_DestroyVM(vm);
10060 return nResult;
10061 }
10062
10063 #include <iostream>
10064
WasmTest(napi_env env,napi_callback_info info)10065 [[maybe_unused]] static napi_value WasmTest(napi_env env, napi_callback_info info) {
10066 (void)RunTestsWithPrefix("test_wasm.cpp");
10067 napi_value result;
10068 napi_create_int32(env, 0, &result);
10069 return result;
10070 }
10071
ArrayBufferBackingStoreTest(napi_env env,napi_callback_info info)10072 [[maybe_unused]] static napi_value ArrayBufferBackingStoreTest(napi_env env, napi_callback_info info) {
10073 (void)RunTestsWithPrefix("test_array_buffer_backing_store.cpp");
10074 napi_value result;
10075 napi_create_int32(env, 0, &result);
10076 return result;
10077 }
10078
10079 EXTERN_C_START
10080
Init(napi_env env,napi_value exports)10081 static napi_value Init(napi_env env, napi_value exports)
10082 {
10083 napi_value typedArrayTypes;
10084 napi_create_object(env, &typedArrayTypes);
10085 const char* typeKeys[] = {
10086 "INT8_ARRAY", "UINT8_ARRAY", "UINT8_CLAMPED_ARRAY", "INT16_ARRAY", "UINT16_ARRAY",
10087 "INT32_ARRAY", "UINT32_ARRAY", "FLOAT32_ARRAY", "FLOAT64_ARRAY", "BIGINT64_ARRAY",
10088 "BIGUINT64_ARRAY"
10089 };
10090 for (int32_t i = 0; i < sizeof(typeKeys) / sizeof(typeKeys[0]); i++) {
10091 napi_value typeIndex;
10092 napi_create_int32(env, i, &typeIndex);
10093 napi_set_named_property(env, typedArrayTypes, typeKeys[i], typeIndex);
10094 }
10095 napi_property_descriptor properties[] = {
10096 DECLARE_NAPI_FUNCTION("testEngineAndContext",testEngineAndContext),
10097 DECLARE_NAPI_FUNCTION("testError",testError),
10098 DECLARE_NAPI_FUNCTION("testTypeError",testTypeError),
10099 DECLARE_NAPI_FUNCTION("testRangeError",testRangeError),
10100 DECLARE_NAPI_FUNCTION("testSyntaxError",testSyntaxError),
10101 DECLARE_NAPI_FUNCTION("testClearError",testClearError),
10102 DECLARE_NAPI_FUNCTION("testLifeCycle",testLifeCycle),
10103 DECLARE_NAPI_FUNCTION("testLifeCycle2", testLifeCycle2),
10104 DECLARE_NAPI_FUNCTION("testCreateData1",testCreateData1),
10105 DECLARE_NAPI_FUNCTION("testCreateData2",testCreateData2),
10106 DECLARE_NAPI_FUNCTION("testCreateData3",testCreateData3),
10107 DECLARE_NAPI_FUNCTION("testCreateData4",testCreateData4),
10108 DECLARE_NAPI_FUNCTION("testCreateData5",testCreateData5),
10109 DECLARE_NAPI_FUNCTION("testGetData1",testGetData1),
10110 DECLARE_NAPI_FUNCTION("testGetData2",testGetData2),
10111 DECLARE_NAPI_FUNCTION("testValueOperation",testValueOperation),
10112 DECLARE_NAPI_FUNCTION("testValueOperation2",testValueOperation2),
10113 DECLARE_NAPI_FUNCTION("testValueOperation3", testValueOperation3),
10114 DECLARE_NAPI_FUNCTION("testValueOperation4", testValueOperation4),
10115 DECLARE_NAPI_FUNCTION("testValueOperation5", testValueOperation5),
10116 DECLARE_NAPI_FUNCTION("testValueOperation6", testValueOperation6),
10117 DECLARE_NAPI_FUNCTION("testValueOperation7", testValueOperation7),
10118 DECLARE_NAPI_FUNCTION("testValueOperation8", testValueOperation8),
10119 DECLARE_NAPI_FUNCTION("testGetPropertyNames",testGetPropertyNames),
10120 DECLARE_NAPI_FUNCTION("testGetAllPropertyNames",testGetAllPropertyNames),
10121 DECLARE_NAPI_FUNCTION("testProperty",testProperty),
10122 DECLARE_NAPI_FUNCTION("testDefineProperties",testDefineProperties),
10123 DECLARE_NAPI_FUNCTION("testWrap",testWrap),
10124 DECLARE_NAPI_FUNCTION("testOthers",testOthers),
10125 DECLARE_NAPI_FUNCTION("Add",Add),
10126 DECLARE_NAPI_FUNCTION("Add1",Add1),
10127 DECLARE_NAPI_FUNCTION("testSecondOperations",testSecondOperations),
10128 DECLARE_NAPI_FUNCTION("testDefinePropertyHandle",testDefinePropertyHandle),
10129 DECLARE_NAPI_FUNCTION("testIsCallable", testIsCallable),
10130 DECLARE_NAPI_FUNCTION("testHandleMicrotasks", testHandleMicrotasks),
10131 DECLARE_NAPI_FUNCTION("runJsVm", RunJsVm),
10132 {"TypedArrayTypes", nullptr, nullptr, nullptr, nullptr, typedArrayTypes, napi_default, nullptr},
10133 DECLARE_NAPI_FUNCTION("testMultithreadFunction", testMultithreadFunction),
10134 DECLARE_NAPI_FUNCTION("testJswmInterface", testJswmInterface),
10135 DECLARE_NAPI_FUNCTION("testCompileWithOption", testCompileWithOption),
10136 DECLARE_NAPI_FUNCTION("testRetainScript", testRetainScript),
10137 DECLARE_NAPI_FUNCTION("testOpenInspectorWithName", testOpenInspectorWithName),
10138 DECLARE_NAPI_FUNCTION("testArrayBuffer", testArrayBuffer),
10139 DECLARE_NAPI_FUNCTION("testArrayBuffer2", testArrayBuffer2),
10140 DECLARE_NAPI_FUNCTION("testArrayBuffer3", testArrayBuffer3),
10141 DECLARE_NAPI_FUNCTION("testWasmOperator", testWasmOperator),
10142 DECLARE_NAPI_FUNCTION("wasmTest", WasmTest),
10143 DECLARE_NAPI_FUNCTION("arrayBufferBackingStoreTest", ArrayBufferBackingStoreTest),
10144 };
10145 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties));
10146 return exports;
10147 }
10148
10149 EXTERN_C_END
10150
10151 static napi_module demoModule = {
10152 .nm_version = 1,
10153 .nm_flags = 0,
10154 .nm_filename = nullptr,
10155 .nm_register_func = Init,
10156 .nm_modname = "napitest",
10157 .nm_priv = ((void *)0),
10158 .reserved = {0},
10159 };
10160
RegisterModule(void)10161 extern "C" __attribute__((constructor)) void RegisterModule(void)
10162 {
10163 napi_module_register(&demoModule);
10164 }
10165