1 /*
2 * Copyright (c) 2024 SwanLink (Jiangsu) Technology Development Co., LTD.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi/native_api.h"
17 #include "napi_lifecycle_test.h"
18
19 //handlescope:OH_JSVM_OpenHandleScope
20 //env null,return not ok
TestOpenHandleScopeCase01(JSVM_Env env,JSVM_CallbackInfo info)21 [[maybe_unused]] JSVM_Value TestOpenHandleScopeCase01(JSVM_Env env, JSVM_CallbackInfo info)
22 {
23 JSVM_HandleScope handleScope;
24 JSVM_Status status = OH_JSVM_OpenHandleScope(nullptr, &handleScope);
25 if (status != JSVM_INVALID_ARG) {
26 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase01:OH_JSVM_OpenHandleScope Execution exception.");
27 return nullptr;
28 }
29
30 bool setValue = true;
31 JSVM_Value retValue = nullptr;
32 OH_JSVM_GetBoolean(env, setValue, &retValue);
33 return retValue;
34 }
35 //env not null,return ok
assertEqual(JSVM_Env env,JSVM_CallbackInfo info)36 static JSVM_Value assertEqual(JSVM_Env env, JSVM_CallbackInfo info)
37 {
38 size_t argc = 2;
39 JSVM_Value args[2];
40 JSVM_Status status = OH_JSVM_GetCbInfo(env, info, &argc, args, NULL, NULL);
41 if (status != JSVM_OK) {
42 OH_JSVM_ThrowError(env, nullptr, "assertEqual Failed.");
43 return nullptr;
44 }
45
46 bool isStrictEquals = false;
47 status = OH_JSVM_StrictEquals(env, args[0], args[1], &isStrictEquals);
48 if (status != JSVM_OK) {
49 OH_JSVM_ThrowError(env, nullptr, "OH_JSVM_StrictEquals Failed.");
50 return nullptr;
51 }
52
53 bool setValue = true;
54 JSVM_Value retValue = nullptr;
55 OH_JSVM_GetBoolean(env, setValue, &retValue);
56 return retValue;
57 }
58
TestOpenHandleScopeCase02(JSVM_Env env,JSVM_CallbackInfo info)59 [[maybe_unused]] JSVM_Value TestOpenHandleScopeCase02(JSVM_Env env, JSVM_CallbackInfo info)
60 {
61 JSVM_VM vm;
62 JSVM_CreateVMOptions options;
63 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
64 return nullptr;
65 }
66 JSVM_Status status = OH_JSVM_CreateVM(&options, &vm);
67 if (status != JSVM_OK) {
68 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase02:OH_JSVM_CreateVM Failed.");
69 return nullptr;
70 }
71
72 JSVM_Env env1;
73 JSVM_CallbackStruct param[1];
74 param[0].data = nullptr;
75 param[0].callback = assertEqual;
76 JSVM_PropertyDescriptor descriptor[] = {
77 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
78 };
79 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
80 if (status != JSVM_OK) {
81 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase02:OH_JSVM_CreateEnv Failed.");
82 return nullptr;
83 }
84 JSVM_HandleScope handleScope;
85 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
86 if (status != JSVM_OK) {
87 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase02:OH_JSVM_OpenHandleScope Execution exception.");
88 return nullptr;
89 }
90
91 bool setValue = true;
92 JSVM_Value retValue = nullptr;
93 OH_JSVM_GetBoolean(env, setValue, &retValue);
94 return retValue;
95 }
96 //env not null,call this interface twice,first ok, second ok
TestOpenHandleScopeCase03(JSVM_Env env,JSVM_CallbackInfo info)97 [[maybe_unused]] JSVM_Value TestOpenHandleScopeCase03(JSVM_Env env, JSVM_CallbackInfo info)
98 {
99 JSVM_VM vm;
100 JSVM_CreateVMOptions options;
101 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
102 return nullptr;
103 }
104 JSVM_Status status = OH_JSVM_CreateVM(&options, &vm);
105 if (status != JSVM_OK) {
106 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase03:OH_JSVM_CreateVM Failed.");
107 return nullptr;
108 }
109
110 JSVM_Env env1;
111 JSVM_CallbackStruct param[1];
112 param[0].data = nullptr;
113 param[0].callback = assertEqual;
114 JSVM_PropertyDescriptor descriptor[] = {
115 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
116 };
117 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
118 if (status != JSVM_OK) {
119 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase03:OH_JSVM_CreateEnv Failed.");
120 return nullptr;
121 }
122 JSVM_HandleScope handleScope;
123 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
124 if (status != JSVM_OK) {
125 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase03:OH_JSVM_OpenHandleScope 1 Failed.");
126 return nullptr;
127 }
128
129 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
130 if (status != JSVM_OK) {
131 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase03:OH_JSVM_OpenHandleScope 2 Failed.");
132 return nullptr;
133 }
134
135 bool setValue = true;
136 JSVM_Value retValue = nullptr;
137 OH_JSVM_GetBoolean(env, setValue, &retValue);
138 return retValue;
139 }
140 //open two scope,create jsvm value,close one scope,expected inability to access jsvm value again
TestOpenHandleScopeCase04(JSVM_Env env,JSVM_CallbackInfo info)141 [[maybe_unused]] JSVM_Value TestOpenHandleScopeCase04(JSVM_Env env, JSVM_CallbackInfo info)
142 {
143 JSVM_VM vm;
144 JSVM_CreateVMOptions options;
145 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
146 return nullptr;
147 }
148 JSVM_Status status = OH_JSVM_CreateVM(&options, &vm);
149 if (status != JSVM_OK) {
150 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase04:OH_JSVM_CreateVM Failed.");
151 return nullptr;
152 }
153
154 JSVM_Env env1;
155 JSVM_CallbackStruct param[1];
156 param[0].data = nullptr;
157 param[0].callback = assertEqual;
158 JSVM_PropertyDescriptor descriptor[] = {
159 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
160 };
161 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
162 if (status != JSVM_OK) {
163 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase04:OH_JSVM_CreateEnv Failed.");
164 return nullptr;
165 }
166 JSVM_HandleScope handleScope;
167 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
168 if (status != JSVM_OK) {
169 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase04:OH_JSVM_OpenHandleScope 1 Failed.");
170 return nullptr;
171 }
172
173 JSVM_HandleScope handleScope1;
174 status = OH_JSVM_OpenHandleScope(env1, &handleScope1);
175 if (status != JSVM_OK) {
176 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase04:OH_JSVM_OpenHandleScope 2 Failed.");
177 return nullptr;
178 }
179
180 JSVM_Value rhs;
181 status = OH_JSVM_CreateInt32(env, 1, &rhs);
182 if (status != JSVM_OK) {
183 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase04:OH_JSVM_CreateInt32 Failed.");
184 return nullptr;
185 }
186
187 status = OH_JSVM_CloseHandleScope(env1, handleScope);
188 if (status != JSVM_OK) {
189 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase04:OH_JSVM_CloseHandleScope Failed.");
190 return nullptr;
191 }
192
193 int32_t num;
194 status = OH_JSVM_GetValueInt32(env, rhs, &num);
195 if (status != JSVM_OK) {
196 OH_JSVM_ThrowError(env, nullptr, "TestOpenHandleScopeCase04:OH_JSVM_GetValueInt32 Execution exception.");
197 return nullptr;
198 }
199
200 bool setValue = true;
201 JSVM_Value retValue = nullptr;
202 OH_JSVM_GetBoolean(env, setValue, &retValue);
203 return retValue;
204 }
205 //handlescope:OH_JSVM_CloseHandleScope:scope nullptr,return not ok
TestCloseHandleScopeCase01(JSVM_Env env,JSVM_CallbackInfo info)206 [[maybe_unused]] JSVM_Value TestCloseHandleScopeCase01(JSVM_Env env, JSVM_CallbackInfo info)
207 {
208 JSVM_Status status = OH_JSVM_CloseHandleScope(env, nullptr);
209 if (status == JSVM_OK) {
210 OH_JSVM_ThrowError(env, nullptr, "TestCloseHandleScopeCase01:OH_JSVM_CloseHandleScope Execution exception.");
211 return nullptr;
212 }
213
214 bool setValue = true;
215 JSVM_Value retValue = nullptr;
216 OH_JSVM_GetBoolean(env, setValue, &retValue);
217 return retValue;
218 }
219 //Call this interface after opening,return ok
TestCloseHandleScopeCase02(JSVM_Env env,JSVM_CallbackInfo info)220 [[maybe_unused]] JSVM_Value TestCloseHandleScopeCase02(JSVM_Env env, JSVM_CallbackInfo info)
221 {
222 JSVM_VM vm;
223 JSVM_CreateVMOptions options;
224 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
225 return nullptr;
226 }
227 JSVM_Status status = OH_JSVM_CreateVM(&options, &vm);
228 if (status != JSVM_OK) {
229 OH_JSVM_ThrowError(env, nullptr, "TestCloseHandleScopeCase02:OH_JSVM_CreateVM Failed.");
230 return nullptr;
231 }
232 JSVM_Env env1;
233 JSVM_CallbackStruct param[1];
234 param[0].data = nullptr;
235 param[0].callback = assertEqual;
236 JSVM_PropertyDescriptor descriptor[] = {
237 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
238 };
239 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
240 if (status != JSVM_OK) {
241 OH_JSVM_ThrowError(env, nullptr, "TestCloseHandleScopeCase02:OH_JSVM_CreateEnv Failed.");
242 return nullptr;
243 }
244 JSVM_HandleScope handleScope;
245 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
246 if (status != JSVM_OK) {
247 OH_JSVM_ThrowError(env, nullptr, "TestCloseHandleScopeCase02:OH_JSVM_OpenHandleScope Failed.");
248 return nullptr;
249 }
250
251 status = OH_JSVM_CloseHandleScope(env1, nullptr);
252 if (status != JSVM_INVALID_ARG) {
253 OH_JSVM_ThrowError(env, nullptr, "TestCloseHandleScopeCase02:OH_JSVM_CloseHandleScope Failed.");
254 return nullptr;
255 }
256
257 bool setValue = true;
258 JSVM_Value retValue = nullptr;
259 OH_JSVM_GetBoolean(env, setValue, &retValue);
260 return retValue;
261 }
262 //open twice,close twice,return ok
TestOpenAndCloseHandleScopeCase01(JSVM_Env env,JSVM_CallbackInfo info)263 [[maybe_unused]] JSVM_Value TestOpenAndCloseHandleScopeCase01(JSVM_Env env, JSVM_CallbackInfo info)
264 {
265 JSVM_VM vm;
266 JSVM_CreateVMOptions options;
267 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
268 return nullptr;
269 }
270 JSVM_Status status;
271 OH_JSVM_CreateVM(&options, &vm);
272 JSVM_Env env1;
273 JSVM_CallbackStruct param[1];
274 param[0].data = nullptr;
275 param[0].callback = assertEqual;
276 JSVM_PropertyDescriptor descriptor[] = {
277 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
278 };
279 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
280 if (status != JSVM_OK) {
281 OH_JSVM_ThrowError(env, nullptr, "TestOpenAndCloseHandleScopeCase01:OH_JSVM_CreateEnv Failed.");
282 return nullptr;
283 }
284 JSVM_HandleScope handleScope;
285 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
286 if (status != JSVM_OK) {
287 OH_JSVM_ThrowError(env, nullptr, "TestOpenAndCloseHandleScopeCase01:OH_JSVM_OpenHandleScope Failed.");
288 return nullptr;
289 }
290 JSVM_HandleScope handleScope1;
291 status = OH_JSVM_OpenHandleScope(env1, &handleScope1);
292 if (status != JSVM_OK) {
293 OH_JSVM_ThrowError(env, nullptr, "TestOpenAndCloseHandleScopeCase01:OH_JSVM_OpenHandleScope Failed.");
294 return nullptr;
295 }
296 status = OH_JSVM_CloseHandleScope(env1, handleScope1);
297 if (status != JSVM_OK) {
298 OH_JSVM_ThrowError(env, nullptr,
299 "TestOpenAndCloseHandleScopeCase01:OH_JSVM_CloseHandleScope Handlescope1 Failed.");
300 return nullptr;
301 }
302 status = OH_JSVM_CloseHandleScope(env1, handleScope);
303 if (status != JSVM_OK) {
304 OH_JSVM_ThrowError(env, nullptr,
305 "TestOpenAndCloseHandleScopeCase01:OH_JSVM_CloseHandleScope Handlescope Failed.");
306 return nullptr;
307 }
308
309 bool setValue = true;
310 JSVM_Value retValue = nullptr;
311 OH_JSVM_GetBoolean(env, setValue, &retValue);
312 return retValue;
313 }
314 //open,close twice,first ok, second not ok
TestOpenAndCloseHandleScopeCase02(JSVM_Env env,JSVM_CallbackInfo info)315 [[maybe_unused]] JSVM_Value TestOpenAndCloseHandleScopeCase02(JSVM_Env env, JSVM_CallbackInfo info)
316 {
317 JSVM_VM vm;
318 JSVM_CreateVMOptions options;
319 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
320 return nullptr;
321 }
322 JSVM_Status status = OH_JSVM_CreateVM(&options, &vm);
323 if (status != JSVM_OK) {
324 OH_JSVM_ThrowError(env, nullptr, "TestOpenAndCloseHandleScopeCase02:OH_JSVM_CreateVM Failed.");
325 return nullptr;
326 }
327 JSVM_Env env1;
328 JSVM_CallbackStruct param[1];
329 param[0].data = nullptr;
330 param[0].callback = assertEqual;
331 JSVM_PropertyDescriptor descriptor[] = {
332 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
333 };
334 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
335 if (status != JSVM_OK) {
336 OH_JSVM_ThrowError(env, nullptr, "TestOpenAndCloseHandleScopeCase02:OH_JSVM_CreateEnv Failed.");
337 return nullptr;
338 }
339 JSVM_HandleScope handleScope;
340 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
341 if (status != JSVM_OK) {
342 OH_JSVM_ThrowError(env, nullptr, "TestOpenAndCloseHandleScopeCase02:OH_JSVM_OpenHandleScope Failed.");
343 return nullptr;
344 }
345
346 status = OH_JSVM_CloseHandleScope(env1, handleScope);
347 if (status != JSVM_OK) {
348 OH_JSVM_ThrowError(env,
349 nullptr,
350 "TestOpenAndCloseHandleScopeCase02:OH_JSVM_CloseHandleScope Handlescope1 Failed.");
351 return nullptr;
352 }
353
354 status = OH_JSVM_CloseHandleScope(env1, handleScope);
355 if (status != JSVM_HANDLE_SCOPE_MISMATCH) {
356 OH_JSVM_ThrowError(env,
357 nullptr,
358 "TestOpenAndCloseHandleScopeCase02:OH_JSVM_CloseHandleScope Execution exception.");
359 return nullptr;
360 }
361
362 bool setValue = true;
363 JSVM_Value retValue = nullptr;
364 OH_JSVM_GetBoolean(env, setValue, &retValue);
365 return retValue;
366 }
367 //EscapableHandleScope:OH_JSVM_OpenEscapableHandleScope:env null,return not ok
TestOpenEscapableHandleScopeCase01(JSVM_Env env,JSVM_CallbackInfo info)368 [[maybe_unused]] JSVM_Value TestOpenEscapableHandleScopeCase01(JSVM_Env env, JSVM_CallbackInfo info)
369 {
370 JSVM_EscapableHandleScope handleScope1;
371 JSVM_Status status = OH_JSVM_OpenEscapableHandleScope(nullptr, &handleScope1);
372 if (status != JSVM_INVALID_ARG) {
373 OH_JSVM_ThrowError(env,
374 nullptr,
375 "TestOpenEscapableHandleScopeCase01:OH_JSVM_OpenEscapableHandleScope Execution exception.");
376 return nullptr;
377 }
378
379 bool setValue = true;
380 JSVM_Value retValue = nullptr;
381 OH_JSVM_GetBoolean(env, setValue, &retValue);
382 return retValue;
383 }
384 //env not null,return ok
385 static int g_tempNum = 0;
hello(JSVM_Env env,JSVM_CallbackInfo info)386 static JSVM_Value hello(JSVM_Env env, JSVM_CallbackInfo info)
387 {
388 JSVM_Value output;
389 void *data = nullptr;
390 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, nullptr, &data);
391 OH_JSVM_CreateStringUtf8(env, (char *)data, strlen((char *)data), &output);
392 return output;
393 }
394 static JSVM_CallbackStruct hello_cb = {hello, (void *)"Hello"};
395 static intptr_t g_externals[] = {
396 (intptr_t)&hello_cb,
397 0,
398 };
TestOpenEscapableHandleScopeCase02(JSVM_Env env,JSVM_CallbackInfo info)399 [[maybe_unused]] JSVM_Value TestOpenEscapableHandleScopeCase02(JSVM_Env env, JSVM_CallbackInfo info)
400 {
401 JSVM_InitOptions init_options;
402 if (memset_s(&init_options, sizeof(init_options), 0, sizeof(init_options)) != 0) {
403 return nullptr;
404 }
405 init_options.externalReferences = g_externals;
406 if (g_tempNum == 0) {
407 OH_JSVM_Init(&init_options);
408 g_tempNum++;
409 }
410 JSVM_VM vm;
411 JSVM_CreateVMOptions options;
412 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
413 return nullptr;
414 }
415 JSVM_Status status = OH_JSVM_CreateVM(&options, &vm);
416 if (status != JSVM_OK) {
417 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase02:OH_JSVM_CreateVM Failed.");
418 return nullptr;
419 }
420 JSVM_VMScope vmScope;
421 status = OH_JSVM_OpenVMScope(vm, &vmScope);
422 if (status != JSVM_OK) {
423 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase02:OH_JSVM_OpenVMScope Failed.");
424 return nullptr;
425 }
426
427 JSVM_Env env1;
428 JSVM_CallbackStruct param[1];
429 param[0].data = nullptr;
430 param[0].callback = assertEqual;
431 JSVM_PropertyDescriptor descriptor[] = {
432 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
433 };
434 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
435 if (status != JSVM_OK) {
436 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase02:OH_JSVM_CreateEnv Failed.");
437 return nullptr;
438 }
439 JSVM_EnvScope envScope;
440 status = OH_JSVM_OpenEnvScope(env1, &envScope);
441 if (status != JSVM_OK) {
442 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase02:OH_JSVM_OpenEnvScope Failed.");
443 return nullptr;
444 }
445 JSVM_HandleScope handleScope;
446 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
447 if (status != JSVM_OK) {
448 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase02:OH_JSVM_OpenHandleScope Failed.");
449 return nullptr;
450 }
451 JSVM_EscapableHandleScope handleScope1 = nullptr;
452 status = OH_JSVM_OpenEscapableHandleScope(env1, &handleScope1);
453 if (status != JSVM_OK) {
454 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase02:OH_JSVM_OpenEscapableHandleScope Failed.");
455 return nullptr;
456 }
457 OH_JSVM_CloseEscapableHandleScope(env1, handleScope1);
458 OH_JSVM_CloseHandleScope(env1, handleScope);
459 OH_JSVM_CloseEnvScope(env1, envScope);
460 OH_JSVM_DestroyEnv(env1);
461 OH_JSVM_CloseVMScope(vm, vmScope);
462 OH_JSVM_DestroyVM(vm);
463
464 bool setValue = true;
465 JSVM_Value retValue = nullptr;
466 OH_JSVM_GetBoolean(env, setValue, &retValue);
467 return retValue;
468 }
469 //env not null,call twice func,first ok, second ok
TestOpenEscapableHandleScopeCase03(JSVM_Env env,JSVM_CallbackInfo info)470 [[maybe_unused]] JSVM_Value TestOpenEscapableHandleScopeCase03(JSVM_Env env, JSVM_CallbackInfo info)
471 {
472 JSVM_VM vm;
473 JSVM_CreateVMOptions options;
474 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
475 return nullptr;
476 }
477 JSVM_Status status = OH_JSVM_CreateVM(&options, &vm);
478 if (status != JSVM_OK) {
479 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase03:OH_JSVM_CreateVM Failed.");
480 return nullptr;
481 }
482 JSVM_VMScope vmScope;
483 status = OH_JSVM_OpenVMScope(vm, &vmScope);
484 if (status != JSVM_OK) {
485 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase03:OH_JSVM_OpenVMScope Failed.");
486 return nullptr;
487 }
488 JSVM_Env env1;
489 JSVM_CallbackStruct param[1];
490 param[0].data = nullptr;
491 param[0].callback = assertEqual;
492 JSVM_PropertyDescriptor descriptor[] = {
493 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
494 };
495 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
496 if (status != JSVM_OK) {
497 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase03:OH_JSVM_CreateEnv Failed.");
498 return nullptr;
499 }
500 JSVM_EnvScope envScope;
501 status = OH_JSVM_OpenEnvScope(env1, &envScope);
502 if (status != JSVM_OK) {
503 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase03:OH_JSVM_OpenEnvScope Failed.");
504 return nullptr;
505 }
506 JSVM_HandleScope handleScope;
507 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
508 if (status != JSVM_OK) {
509 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase03:OH_JSVM_OpenHandleScope Failed.");
510 return nullptr;
511 }
512 JSVM_EscapableHandleScope handleScope1;
513 status = OH_JSVM_OpenEscapableHandleScope(env1, &handleScope1);
514 if (status != JSVM_OK) {
515 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase03:OpenEscapableHandleScope 1 Failed.");
516 return nullptr;
517 }
518
519 status = OH_JSVM_OpenEscapableHandleScope(env1, &handleScope1);
520 if (status != JSVM_OK) {
521 OH_JSVM_ThrowError(env, nullptr, "TestOpenEscapableHandleScopeCase03:OpenEscapableHandleScope 2 Failed.");
522 return nullptr;
523 }
524 OH_JSVM_CloseEscapableHandleScope(env1, handleScope1);
525 OH_JSVM_CloseHandleScope(env1, handleScope);
526 OH_JSVM_CloseEnvScope(env1, envScope);
527 OH_JSVM_DestroyEnv(env1);
528 OH_JSVM_CloseVMScope(vm, vmScope);
529 OH_JSVM_DestroyVM(vm);
530
531 bool setValue = true;
532 JSVM_Value retValue = nullptr;
533 OH_JSVM_GetBoolean(env, setValue, &retValue);
534 return retValue;
535 }
536 //EscapableHandleScope:OH_JSVM_CloseEscapableHandleScope:scope nullptr,return not ok
TestCloseEscapableHandleScopeCase01(JSVM_Env env,JSVM_CallbackInfo info)537 [[maybe_unused]] JSVM_Value TestCloseEscapableHandleScopeCase01(JSVM_Env env, JSVM_CallbackInfo info)
538 {
539 JSVM_Status status = OH_JSVM_CloseEscapableHandleScope(env, nullptr);
540 if (status != JSVM_INVALID_ARG) {
541 OH_JSVM_ThrowError(env, nullptr, "TestCloseEscapableHandleScopeCase01:CloseEscapable Execution exception.");
542 return nullptr;
543 }
544
545 bool setValue = true;
546 JSVM_Value retValue = nullptr;
547 OH_JSVM_GetBoolean(env, setValue, &retValue);
548 return retValue;
549 }
550 //open,call func,return ok
TestCloseEscapableHandleScopeCase02(JSVM_Env env,JSVM_CallbackInfo info)551 [[maybe_unused]] JSVM_Value TestCloseEscapableHandleScopeCase02(JSVM_Env env, JSVM_CallbackInfo info)
552 {
553 JSVM_EscapableHandleScope handleScope;
554 JSVM_Status status = OH_JSVM_OpenEscapableHandleScope(env, &handleScope);
555 if (status != JSVM_OK) {
556 OH_JSVM_ThrowError(env,
557 nullptr,
558 "TestCloseEscapableHandleScopeCase02:OH_JSVM_OpenEscapableHandleScope 1 Failed.");
559 return nullptr;
560 }
561
562 status = OH_JSVM_CloseEscapableHandleScope(env, handleScope);
563 if (status != JSVM_OK) {
564 OH_JSVM_ThrowError(env,
565 nullptr,
566 "TestCloseEscapableHandleScopeCase02:OH_JSVM_CloseEscapableHandleScope Failed.");
567 return nullptr;
568 }
569
570 bool setValue = true;
571 JSVM_Value retValue = nullptr;
572 OH_JSVM_GetBoolean(env, setValue, &retValue);
573 return retValue;
574 }
575 //not open,call func,failed
TestCloseEscapableHandleScopeCase03(JSVM_Env env,JSVM_CallbackInfo info)576 [[maybe_unused]] JSVM_Value TestCloseEscapableHandleScopeCase03(JSVM_Env env, JSVM_CallbackInfo info)
577 {
578 JSVM_Status status = OH_JSVM_CloseEscapableHandleScope(env, nullptr);
579 if (status != JSVM_INVALID_ARG) {
580 OH_JSVM_ThrowError(env,
581 nullptr,
582 "TestCloseEscapableHandleScopeCase03:CloseEscapableHandleScope Execution exception.");
583 return nullptr;
584 }
585
586 bool setValue = true;
587 JSVM_Value retValue = nullptr;
588 OH_JSVM_GetBoolean(env, setValue, &retValue);
589 return retValue;
590 }
591 //EscapableHandleScope:OH_JSVM_EscapeHandle:JSVM_EscapableHandleScope scope,
592 //not call OpenEscapableHandleScope,call this func,return not ok
TestEscapeHandleCase01(JSVM_Env env,JSVM_CallbackInfo info)593 [[maybe_unused]] JSVM_Value TestEscapeHandleCase01(JSVM_Env env, JSVM_CallbackInfo info)
594 {
595 JSVM_EscapableHandleScope scopeVar = nullptr;
596 JSVM_Value obj = nullptr;
597 OH_JSVM_CreateObject(env, &obj);
598 JSVM_Value escapedObj = nullptr;
599 JSVM_Status status = OH_JSVM_EscapeHandle(env, scopeVar, obj, &escapedObj);
600 if (status == JSVM_OK) {
601 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase01:OH_JSVM_EscapeHandle Execution exception.");
602 return nullptr;
603 }
604
605 bool setValue = true;
606 JSVM_Value retValue = nullptr;
607 OH_JSVM_GetBoolean(env, setValue, &retValue);
608 return retValue;
609 }
610 //JSVM_EscapableHandleScope scope null,return not ok
TestEscapeHandleCase02(JSVM_Env env,JSVM_CallbackInfo info)611 [[maybe_unused]] JSVM_Value TestEscapeHandleCase02(JSVM_Env env, JSVM_CallbackInfo info)
612 {
613 JSVM_Value obj = nullptr;
614 OH_JSVM_CreateObject(env, &obj);
615 JSVM_Value escapedObj = nullptr;
616 JSVM_Status status = OH_JSVM_EscapeHandle(env, nullptr, obj, &escapedObj);
617 if (status == JSVM_OK) {
618 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase02:OH_JSVM_EscapeHandle Execution exception.");
619 return nullptr;
620 }
621
622 bool setValue = true;
623 JSVM_Value retValue = nullptr;
624 OH_JSVM_GetBoolean(env, setValue, &retValue);
625 return retValue;
626 }
627 //JSVM_EscapableHandleScope,open,return ok
TestEscapeHandleCase03(JSVM_Env env,JSVM_CallbackInfo info)628 [[maybe_unused]] JSVM_Value TestEscapeHandleCase03(JSVM_Env env, JSVM_CallbackInfo info)
629 {
630 JSVM_VM vm;
631 JSVM_CreateVMOptions options;
632 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
633 return nullptr;
634 }
635 JSVM_Status status = OH_JSVM_CreateVM(&options, &vm);
636 if (status != JSVM_OK) {
637 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase03:OH_JSVM_CreateVM Failed.");
638 return nullptr;
639 }
640 JSVM_VMScope vmScope;
641 status = OH_JSVM_OpenVMScope(vm, &vmScope);
642 if (status != JSVM_OK) {
643 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase03:OH_JSVM_OpenVMScope Failed.");
644 return nullptr;
645 }
646 JSVM_Env env1;
647 JSVM_CallbackStruct param[1];
648 param[0].data = nullptr;
649 param[0].callback = assertEqual;
650 JSVM_PropertyDescriptor descriptor[] = {
651 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
652 };
653 status = OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
654 if (status != JSVM_OK) {
655 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase03:OH_JSVM_CreateEnv Failed.");
656 return nullptr;
657 }
658 JSVM_EnvScope envScope;
659 status = OH_JSVM_OpenEnvScope(env1, &envScope);
660 if (status != JSVM_OK) {
661 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase03:OH_JSVM_OpenEnvScope Failed.");
662 return nullptr;
663 }
664 JSVM_HandleScope handleScope;
665 status = OH_JSVM_OpenHandleScope(env1, &handleScope);
666 if (status != JSVM_OK) {
667 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase03:OH_JSVM_OpenHandleScope Failed.");
668 return nullptr;
669 }
670 JSVM_EscapableHandleScope handleScope1;
671 status = OH_JSVM_OpenEscapableHandleScope(env1, &handleScope1);
672 if (status != JSVM_OK) {
673 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase03:OH_JSVM_OpenEscapableHandleScope Failed.");
674 return nullptr;
675 }
676
677 JSVM_Value output = NULL;
678 JSVM_Value escapee = NULL;
679 OH_JSVM_CreateObject(env, &output);
680 status = OH_JSVM_EscapeHandle(env1, handleScope1, output, &escapee);
681 if (status != JSVM_OK) {
682 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase03:OH_JSVM_EscapeHandle Failed.");
683 return nullptr;
684 }
685 OH_JSVM_CloseEscapableHandleScope(env1, handleScope1);
686 OH_JSVM_CloseHandleScope(env1, handleScope);
687 OH_JSVM_CloseEnvScope(env1, envScope);
688 OH_JSVM_DestroyEnv(env1);
689 OH_JSVM_CloseVMScope(vm, vmScope);
690 OH_JSVM_DestroyVM(vm);
691
692 bool setValue = true;
693 JSVM_Value retValue = nullptr;
694 OH_JSVM_GetBoolean(env, setValue, &retValue);
695 return retValue;
696 }
697 //JSVM_Value escapee obj null,return not ok
TestEscapeHandleCase04(JSVM_Env env,JSVM_CallbackInfo info)698 [[maybe_unused]] JSVM_Value TestEscapeHandleCase04(JSVM_Env env, JSVM_CallbackInfo info)
699 {
700 JSVM_VM vm;
701 JSVM_CreateVMOptions options;
702 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
703 return nullptr;
704 }
705 OH_JSVM_CreateVM(&options, &vm);
706 JSVM_VMScope vmScope;
707 OH_JSVM_OpenVMScope(vm, &vmScope);
708 JSVM_Env env1;
709 JSVM_CallbackStruct param[1];
710 param[0].data = nullptr;
711 param[0].callback = assertEqual;
712 JSVM_PropertyDescriptor descriptor[] = {
713 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
714 };
715 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
716 JSVM_EnvScope envScope;
717 OH_JSVM_OpenEnvScope(env1, &envScope);
718 JSVM_HandleScope handleScope;
719 OH_JSVM_OpenHandleScope(env1, &handleScope);
720 JSVM_EscapableHandleScope handleScope1;
721 JSVM_Status status = OH_JSVM_OpenEscapableHandleScope(env1, &handleScope1);
722 if (status != JSVM_OK) {
723 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase03:OH_JSVM_OpenEscapableHandleScope Failed.");
724 return nullptr;
725 }
726
727 JSVM_Value escapee = NULL;
728 status = OH_JSVM_EscapeHandle(env, handleScope1, nullptr, &escapee);
729 if (status != JSVM_INVALID_ARG) {
730 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase04:OH_JSVM_EscapeHandle Execution exception.");
731 return nullptr;
732 }
733
734 OH_JSVM_CloseEscapableHandleScope(env1, handleScope1);
735 OH_JSVM_CloseHandleScope(env1, handleScope);
736 OH_JSVM_CloseEnvScope(env1, envScope);
737 OH_JSVM_DestroyEnv(env1);
738 OH_JSVM_CloseVMScope(vm, vmScope);
739 OH_JSVM_DestroyVM(vm);
740
741 bool setValue = true;
742 JSVM_Value retValue = nullptr;
743 OH_JSVM_GetBoolean(env, setValue, &retValue);
744 return retValue;
745 }
746 //JSVM_Value escapee valid value,return ok
TestEscapeHandleCase05(JSVM_Env env,JSVM_CallbackInfo info)747 [[maybe_unused]] JSVM_Value TestEscapeHandleCase05(JSVM_Env env, JSVM_CallbackInfo info)
748 {
749 JSVM_VM vm;
750 JSVM_CreateVMOptions options;
751 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
752 return nullptr;
753 }
754 OH_JSVM_CreateVM(&options, &vm);
755 JSVM_VMScope vmScope;
756 OH_JSVM_OpenVMScope(vm, &vmScope);
757 JSVM_Env env1;
758 JSVM_CallbackStruct param[1];
759 param[0].data = nullptr;
760 param[0].callback = assertEqual;
761 JSVM_PropertyDescriptor descriptor[] = {
762 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
763 };
764 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
765 JSVM_EnvScope envScope;
766 OH_JSVM_OpenEnvScope(env1, &envScope);
767 JSVM_HandleScope handleScope;
768 OH_JSVM_OpenHandleScope(env1, &handleScope);
769 JSVM_EscapableHandleScope handleScope1;
770 JSVM_Status status = OH_JSVM_OpenEscapableHandleScope(env1, &handleScope1);
771 if (status != JSVM_OK) {
772 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase05:OH_JSVM_OpenEscapableHandleScope Failed.");
773 return nullptr;
774 }
775
776 JSVM_Value obj = nullptr;
777 OH_JSVM_CreateObject(env1, &obj);
778 JSVM_Value value = nullptr;
779 OH_JSVM_CreateStringUtf8(env1, "test jsvm_escapehandle", JSVM_AUTO_LENGTH, &value);
780 OH_JSVM_SetNamedProperty(env1, obj, "name", value);
781
782 JSVM_Value escapedObj = NULL;
783 status = OH_JSVM_EscapeHandle(env1, handleScope1, obj, &escapedObj);
784 if (status != JSVM_OK) {
785 OH_JSVM_ThrowError(env, nullptr, "TestEscapeHandleCase05:OH_JSVM_EscapeHandle Failed.");
786 return nullptr;
787 }
788
789 OH_JSVM_CloseEscapableHandleScope(env1, handleScope1);
790 OH_JSVM_CloseHandleScope(env1, handleScope);
791 OH_JSVM_CloseEnvScope(env1, envScope);
792 OH_JSVM_DestroyEnv(env1);
793 OH_JSVM_CloseVMScope(vm, vmScope);
794 OH_JSVM_DestroyVM(vm);
795
796 bool setValue = true;
797 JSVM_Value retValue = nullptr;
798 OH_JSVM_GetBoolean(env, setValue, &retValue);
799 return retValue;
800 }
801 //API:EscapeHandle combination test:open twice,call close,return ok
TestOpenClosedEscapeCase01(JSVM_Env env,JSVM_CallbackInfo info)802 [[maybe_unused]] JSVM_Value TestOpenClosedEscapeCase01(JSVM_Env env, JSVM_CallbackInfo info)
803 {
804 JSVM_EscapableHandleScope scopeVar = nullptr;
805 JSVM_Status status = OH_JSVM_OpenEscapableHandleScope(env, &scopeVar);
806 if (status != JSVM_OK) {
807 OH_JSVM_ThrowError(env, nullptr, "TestOpenClosedEscapeCase01:OpenEscapable 1 Failed.");
808 return nullptr;
809 }
810
811 JSVM_EscapableHandleScope scopeVar1 = nullptr;
812 status = OH_JSVM_OpenEscapableHandleScope(env, &scopeVar1);
813 if (status != JSVM_OK) {
814 OH_JSVM_ThrowError(env, nullptr, "TestOpenClosedEscapeCase01:OpenEscapable 2 Failed.");
815 return nullptr;
816 }
817
818 status = OH_JSVM_CloseEscapableHandleScope(env, scopeVar1);
819 if (status != JSVM_OK) {
820 OH_JSVM_ThrowError(env, nullptr, "TestOpenClosedEscapeCase01:CloseEscapable 1 Failed.");
821 return nullptr;
822 }
823
824 status = OH_JSVM_CloseEscapableHandleScope(env, scopeVar);
825 if (status != JSVM_OK) {
826 OH_JSVM_ThrowError(env, nullptr, "TestOpenClosedEscapeCase01:CloseEscapable 2 Failed.");
827 return nullptr;
828 }
829
830 bool setValue = true;
831 JSVM_Value retValue = nullptr;
832 OH_JSVM_GetBoolean(env, setValue, &retValue);
833 return retValue;
834 }
835 //create two handlescope1 and handlescope2,create multiple obj,
836 //handlescope1 call escapehandle,handlescope2 use this obj
TestOpenClosedEscapeCase02(JSVM_Env env,JSVM_CallbackInfo info)837 [[maybe_unused]] JSVM_Value TestOpenClosedEscapeCase02(JSVM_Env env, JSVM_CallbackInfo info)
838 {
839 JSVM_VM vm;
840 JSVM_CreateVMOptions options;
841 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
842 return nullptr;
843 }
844 OH_JSVM_CreateVM(&options, &vm);
845 JSVM_VMScope vmScope;
846 OH_JSVM_OpenVMScope(vm, &vmScope);
847 JSVM_Env env1;
848 JSVM_CallbackStruct param[1];
849 param[0].data = nullptr;
850 param[0].callback = assertEqual;
851 JSVM_PropertyDescriptor descriptor[] = {
852 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
853 };
854 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
855 JSVM_EnvScope envScope;
856 OH_JSVM_OpenEnvScope(env1, &envScope);
857 JSVM_HandleScope handleScope;
858 OH_JSVM_OpenHandleScope(env1, &handleScope);
859 JSVM_EscapableHandleScope handleScope1;
860 JSVM_Status status = OH_JSVM_OpenEscapableHandleScope(env1, &handleScope1);
861 if (status != JSVM_OK) {
862 OH_JSVM_ThrowError(env, nullptr, "TestOpenClosedEscapeCase02:OpenEscapable Failed.");
863 return nullptr;
864 }
865
866 JSVM_EscapableHandleScope handleScope2 = nullptr;
867 status = OH_JSVM_OpenEscapableHandleScope(env1, &handleScope2);
868 if (status != JSVM_OK) {
869 OH_JSVM_ThrowError(env, nullptr, "TestOpenClosedEscapeCase02:OpenEscapable Failed.");
870 return nullptr;
871 }
872
873 JSVM_Value testObj;
874 OH_JSVM_CreateObject(env1, &testObj);
875
876 const char testStr[] = "too";
877 JSVM_Value value;
878 OH_JSVM_CreateStringUtf8(env1, testStr, strlen(testStr), &value);
879 OH_JSVM_SetNamedProperty(env1, testObj, "test", value);
880 JSVM_Value resultVar = nullptr;
881 status = OH_JSVM_EscapeHandle(env1, handleScope1, testObj, &resultVar);
882 if (status != JSVM_OK) {
883 OH_JSVM_ThrowError(env, nullptr, "TestOpenClosedEscapeCase02:OH_JSVM_EscapeHandle 1 Failed.");
884 return nullptr;
885 }
886
887 resultVar = nullptr;
888 status = OH_JSVM_EscapeHandle(env1, handleScope2, testObj, &resultVar);
889 if (status != JSVM_OK) {
890 OH_JSVM_ThrowError(env, nullptr, "TestOpenClosedEscapeCase02:OH_JSVM_EscapeHandle 2 Failed.");
891 return nullptr;
892 }
893
894 OH_JSVM_CloseEscapableHandleScope(env1, handleScope1);
895 OH_JSVM_CloseHandleScope(env1, handleScope);
896 OH_JSVM_CloseEnvScope(env1, envScope);
897 OH_JSVM_DestroyEnv(env1);
898 OH_JSVM_CloseVMScope(vm, vmScope);
899 OH_JSVM_DestroyVM(vm);
900
901 bool setValue = true;
902 JSVM_Value retValue = nullptr;
903 OH_JSVM_GetBoolean(env, setValue, &retValue);
904 return retValue;
905 }
906 //OH_JSVM_CreateReference
907 //value nullptr,call test func,return not ok
TestCreateReferenceCase01(JSVM_Env env,JSVM_CallbackInfo info)908 [[maybe_unused]] JSVM_Value TestCreateReferenceCase01(JSVM_Env env, JSVM_CallbackInfo info)
909 {
910 JSVM_Ref reference;
911 JSVM_Status status = OH_JSVM_CreateReference(env, nullptr, 1, &reference);
912 if (status == JSVM_OK) {
913 OH_JSVM_ThrowError(env, nullptr, "TestCreateReferenceCase01:OH_JSVM_CreateReference Execution exception.");
914 return nullptr;
915 }
916
917 bool setValue = true;
918 JSVM_Value retValue = nullptr;
919 OH_JSVM_GetBoolean(env, setValue, &retValue);
920 return retValue;
921 }
922 //jsvm value is normal data,initialRefcount 0,return ok
TestCreateReferenceCase02(JSVM_Env env,JSVM_CallbackInfo info)923 [[maybe_unused]] JSVM_Value TestCreateReferenceCase02(JSVM_Env env, JSVM_CallbackInfo info)
924 {
925 JSVM_Value obj = nullptr;
926 OH_JSVM_CreateObject(env, &obj);
927 JSVM_Ref reference;
928 JSVM_Status status = OH_JSVM_CreateReference(env, obj, 0, &reference);
929 if (status != JSVM_OK) {
930 OH_JSVM_ThrowError(env, nullptr, "TestCreateReferenceCase02:OH_JSVM_CreateReference Failed.");
931 return nullptr;
932 }
933
934 bool setValue = true;
935 JSVM_Value retValue = nullptr;
936 OH_JSVM_GetBoolean(env, setValue, &retValue);
937 return retValue;
938 }
939 //jsvm value is normal data,initialRefcount 1,return ok,result not nullptr
TestCreateReferenceCase03(JSVM_Env env,JSVM_CallbackInfo info)940 [[maybe_unused]] JSVM_Value TestCreateReferenceCase03(JSVM_Env env, JSVM_CallbackInfo info)
941 {
942 JSVM_Value obj = nullptr;
943 OH_JSVM_CreateObject(env, &obj);
944 JSVM_Ref reference;
945 JSVM_Status status = OH_JSVM_CreateReference(env, obj, 1, &reference);
946 if (status != JSVM_OK || reference == nullptr) {
947 OH_JSVM_ThrowError(env, nullptr, "TestCreateReferenceCase03:OH_JSVM_CreateReference Failed.");
948 return nullptr;
949 }
950
951 bool setValue = true;
952 JSVM_Value retValue = nullptr;
953 OH_JSVM_GetBoolean(env, setValue, &retValue);
954 return retValue;
955 }
956 //OH_JSVM_DeleteReference
957 //ref null,return not ok
TestDeleteReferenceCase01(JSVM_Env env,JSVM_CallbackInfo info)958 [[maybe_unused]] JSVM_Value TestDeleteReferenceCase01(JSVM_Env env, JSVM_CallbackInfo info)
959 {
960 JSVM_Status status = OH_JSVM_DeleteReference(env, nullptr);
961 if (status == JSVM_OK) {
962 OH_JSVM_ThrowError(env, nullptr, "TestDeleteReferenceCase02:OH_JSVM_DeleteReference Execution exception.");
963 return nullptr;
964 }
965
966 bool setValue = true;
967 JSVM_Value retValue = nullptr;
968 OH_JSVM_GetBoolean(env, setValue, &retValue);
969 return retValue;
970 }
971 //OH_JSVM_CreateReference create ref,return ok
TestDeleteReferenceCase02(JSVM_Env env,JSVM_CallbackInfo info)972 [[maybe_unused]] JSVM_Value TestDeleteReferenceCase02(JSVM_Env env, JSVM_CallbackInfo info)
973 {
974 JSVM_Value obj = nullptr;
975 OH_JSVM_CreateObject(env, &obj);
976 JSVM_Ref reference;
977 JSVM_Status status = OH_JSVM_CreateReference(env, obj, 1, &reference);
978 if (status != JSVM_OK || reference == nullptr) {
979 OH_JSVM_ThrowError(env, nullptr, "TestDeleteReferenceCase02:OH_JSVM_CreateReference Failed.");
980 return nullptr;
981 }
982
983 status = OH_JSVM_DeleteReference(env, reference);
984 if (status != JSVM_OK) {
985 OH_JSVM_ThrowError(env, nullptr, "TestDeleteReferenceCase02:OH_JSVM_DeleteReference Failed.");
986 return nullptr;
987 }
988
989 bool setValue = true;
990 JSVM_Value retValue = nullptr;
991 OH_JSVM_GetBoolean(env, setValue, &retValue);
992 return retValue;
993 }
994 //OH_JSVM_ReferenceRef:ref nullptr, return not ok
TestReferenceRefCase01(JSVM_Env env,JSVM_CallbackInfo info)995 [[maybe_unused]] JSVM_Value TestReferenceRefCase01(JSVM_Env env, JSVM_CallbackInfo info)
996 {
997 uint32_t resultRefCount = 0;
998 JSVM_Status status = OH_JSVM_ReferenceRef(env, nullptr, &resultRefCount);
999 if (status == JSVM_OK) {
1000 OH_JSVM_ThrowError(env, nullptr, "TestReferenceRefCase01:OH_JSVM_ReferenceRef Execution exception.");
1001 return nullptr;
1002 }
1003
1004 bool setValue = true;
1005 JSVM_Value retValue = nullptr;
1006 OH_JSVM_GetBoolean(env, setValue, &retValue);
1007 return retValue;
1008 }
1009 //create ref,increase citation count,return ok
TestReferenceRefCase02(JSVM_Env env,JSVM_CallbackInfo info)1010 [[maybe_unused]] JSVM_Value TestReferenceRefCase02(JSVM_Env env, JSVM_CallbackInfo info)
1011 {
1012 JSVM_Value testObj = nullptr;
1013 JSVM_Ref resultRef = nullptr;
1014 OH_JSVM_CreateObject(env, &testObj);
1015 JSVM_Status status = OH_JSVM_CreateReference(env, testObj, 1, &resultRef);
1016 if (status != JSVM_OK) {
1017 OH_JSVM_ThrowError(env, nullptr, "TestReferenceRefCase02:OH_JSVM_CreateReference Failed.");
1018 return nullptr;
1019 }
1020
1021 uint32_t resultRefCount = 0;
1022 status = OH_JSVM_ReferenceRef(env, resultRef, &resultRefCount);
1023 if (status != JSVM_OK) {
1024 OH_JSVM_ThrowError(env, nullptr, "TestReferenceRefCase02:OH_JSVM_ReferenceRef Failed.");
1025 return nullptr;
1026 }
1027 size_t testNum = 2;
1028 if (resultRefCount != testNum) {
1029 OH_JSVM_ThrowError(env, nullptr, "TestReferenceRefCase02:OH_JSVM_ReferenceRef resultRefCount Error.");
1030 return nullptr;
1031 }
1032
1033 bool setValue = true;
1034 JSVM_Value retValue = nullptr;
1035 OH_JSVM_GetBoolean(env, setValue, &retValue);
1036 return retValue;
1037 }
1038 //repeated call,return ok
TestReferenceRefCase03(JSVM_Env env,JSVM_CallbackInfo info)1039 [[maybe_unused]] JSVM_Value TestReferenceRefCase03(JSVM_Env env, JSVM_CallbackInfo info)
1040 {
1041 JSVM_Value testObj = nullptr;
1042 JSVM_Ref resultRef = nullptr;
1043 OH_JSVM_CreateObject(env, &testObj);
1044 JSVM_Status status = OH_JSVM_CreateReference(env, testObj, 1, &resultRef);
1045 if (status != JSVM_OK) {
1046 OH_JSVM_ThrowError(env, nullptr, "TestReferenceRefCase03:OH_JSVM_CreateReference Failed.");
1047 return nullptr;
1048 }
1049
1050 uint32_t resultRefCount = 0;
1051 status = OH_JSVM_ReferenceRef(env, resultRef, &resultRefCount);
1052 if (status != JSVM_OK) {
1053 OH_JSVM_ThrowError(env, nullptr, "TestReferenceRefCase03:OH_JSVM_ReferenceRef 1 Failed.");
1054 return nullptr;
1055 }
1056 status = OH_JSVM_ReferenceRef(env, resultRef, &resultRefCount);
1057 if (status != JSVM_OK) {
1058 OH_JSVM_ThrowError(env, nullptr, "TestReferenceRefCase03:OH_JSVM_ReferenceRef 2 Failed.");
1059 return nullptr;
1060 }
1061 status = OH_JSVM_ReferenceRef(env, resultRef, &resultRefCount);
1062 if (status != JSVM_OK) {
1063 OH_JSVM_ThrowError(env, nullptr, "TestReferenceRefCase03:OH_JSVM_ReferenceRef 3 Failed.");
1064 return nullptr;
1065 }
1066 size_t testNum = 4;
1067 if (resultRefCount != testNum) {
1068 OH_JSVM_ThrowError(env, nullptr, "TestReferenceRefCase03:OH_JSVM_ReferenceRef resultRefCount Error.");
1069 return nullptr;
1070 }
1071
1072 bool setValue = true;
1073 JSVM_Value retValue = nullptr;
1074 OH_JSVM_GetBoolean(env, setValue, &retValue);
1075 return retValue;
1076 }
1077 //OH_JSVM_ReferenceUnref:ref nullptr,return not ok
TestReferenceUnrefCase01(JSVM_Env env,JSVM_CallbackInfo info)1078 [[maybe_unused]] JSVM_Value TestReferenceUnrefCase01(JSVM_Env env, JSVM_CallbackInfo info)
1079 {
1080 uint32_t resultRefCount = 0;
1081 JSVM_Status status = OH_JSVM_ReferenceUnref(env, nullptr, &resultRefCount);
1082 if (status == JSVM_OK) {
1083 OH_JSVM_ThrowError(env, nullptr, "TestReferenceUnrefCase01:OH_JSVM_ReferenceUnref Execution exception.");
1084 return nullptr;
1085 }
1086
1087 bool setValue = true;
1088 JSVM_Value retValue = nullptr;
1089 OH_JSVM_GetBoolean(env, setValue, &retValue);
1090 return retValue;
1091 }
1092 //create ref,call this func,reduce the number of citations,return ok
TestReferenceUnrefCase02(JSVM_Env env,JSVM_CallbackInfo info)1093 [[maybe_unused]] JSVM_Value TestReferenceUnrefCase02(JSVM_Env env, JSVM_CallbackInfo info)
1094 {
1095 JSVM_Value rstObj = nullptr;
1096 JSVM_Ref resultRef = nullptr;
1097 OH_JSVM_CreateObject(env, &rstObj);
1098 JSVM_Status status = OH_JSVM_CreateReference(env, rstObj, 1, &resultRef);
1099 if (status != JSVM_OK) {
1100 OH_JSVM_ThrowError(env, nullptr, "TestReferenceUnrefCase02:OH_JSVM_CreateReference Failed.");
1101 return nullptr;
1102 }
1103
1104 uint32_t resultRefCount = 0;
1105 status = OH_JSVM_ReferenceRef(env, resultRef, &resultRefCount);
1106 if (status != JSVM_OK) {
1107 OH_JSVM_ThrowError(env, nullptr, "TestReferenceUnrefCase02:OH_JSVM_ReferenceRef Failed.");
1108 return nullptr;
1109 }
1110
1111 status = OH_JSVM_ReferenceUnref(env, resultRef, &resultRefCount);
1112 if (status != JSVM_OK) {
1113 OH_JSVM_ThrowError(env, nullptr, "TestReferenceUnrefCase02:OH_JSVM_ReferenceUnref Failed.");
1114 return nullptr;
1115 }
1116 if (resultRefCount != 1) {
1117 OH_JSVM_ThrowError(env, nullptr, "TestReferenceUnrefCase02:OH_JSVM_ReferenceUnref resultRefCount Error.");
1118 return nullptr;
1119 }
1120
1121 bool setValue = true;
1122 JSVM_Value retValue = nullptr;
1123 OH_JSVM_GetBoolean(env, setValue, &retValue);
1124 return retValue;
1125 }
1126 //repeate call,call the test interface when the ref reference count is 0,return not ok
TestReferenceUnrefCase03(JSVM_Env env,JSVM_CallbackInfo info)1127 [[maybe_unused]] JSVM_Value TestReferenceUnrefCase03(JSVM_Env env, JSVM_CallbackInfo info)
1128 {
1129 JSVM_Value objVar = nullptr;
1130 JSVM_Ref rstRef = nullptr;
1131 OH_JSVM_CreateObject(env, &objVar);
1132 JSVM_Status status = OH_JSVM_CreateReference(env, objVar, 0, &rstRef);
1133 if (status != JSVM_OK) {
1134 OH_JSVM_ThrowError(env, nullptr, "TestReferenceUnrefCase03:OH_JSVM_CreateReference Failed.");
1135 return nullptr;
1136 }
1137
1138 uint32_t rstRefCount = 0;
1139 status = OH_JSVM_ReferenceRef(env, rstRef, &rstRefCount);
1140 if (status != JSVM_OK) {
1141 OH_JSVM_ThrowError(env, nullptr, "TestReferenceUnrefCase03:OH_JSVM_ReferenceRef Failed.");
1142 return nullptr;
1143 }
1144
1145 status = OH_JSVM_ReferenceUnref(env, rstRef, &rstRefCount);
1146 if (status != JSVM_OK) {
1147 OH_JSVM_ThrowError(env, nullptr, "TestReferenceUnrefCase03:OH_JSVM_ReferenceUnref Failed.");
1148 return nullptr;
1149 }
1150
1151 status = OH_JSVM_ReferenceUnref(env, rstRef, &rstRefCount);
1152 if (status == JSVM_OK) {
1153 OH_JSVM_ThrowError(env, nullptr, "TestReferenceUnrefCase03:OH_JSVM_ReferenceUnref Execution exception.");
1154 return nullptr;
1155 }
1156
1157 bool setValue = true;
1158 JSVM_Value retValue = nullptr;
1159 OH_JSVM_GetBoolean(env, setValue, &retValue);
1160 return retValue;
1161 }
1162 //OH_JSVM_GetReferenceValue:ref nullptr,return ok,result nullptr
TestGetReferenceValueCase01(JSVM_Env env,JSVM_CallbackInfo info)1163 [[maybe_unused]] JSVM_Value TestGetReferenceValueCase01(JSVM_Env env, JSVM_CallbackInfo info)
1164 {
1165 JSVM_Value result = nullptr;
1166 JSVM_Status status = OH_JSVM_GetReferenceValue(env, nullptr, &result);
1167 if (status != JSVM_INVALID_ARG) {
1168 OH_JSVM_ThrowError(env, nullptr, "TestGetReferenceValueCase01:OH_JSVM_GetReferenceValue Failed.");
1169 return nullptr;
1170 }
1171 if (result != nullptr) {
1172 OH_JSVM_ThrowError(env, nullptr, "TestGetReferenceValueCase01:Result is not nullptr.");
1173 return nullptr;
1174 }
1175
1176 bool setValue = true;
1177 JSVM_Value retValue = nullptr;
1178 OH_JSVM_GetBoolean(env, setValue, &retValue);
1179 return retValue;
1180 }
1181 //OH_JSVM_CreateReference create ref,return ok
TestGetReferenceValueCase02(JSVM_Env env,JSVM_CallbackInfo info)1182 [[maybe_unused]] JSVM_Value TestGetReferenceValueCase02(JSVM_Env env, JSVM_CallbackInfo info)
1183 {
1184 JSVM_Ref reference = nullptr;
1185 JSVM_Value value = nullptr;
1186 OH_JSVM_GetNull(env, &value);
1187 JSVM_Status status = OH_JSVM_CreateReference(env, value, 1, &reference);
1188 if (status != JSVM_OK) {
1189 OH_JSVM_ThrowError(env, nullptr, "TestGetReferenceValueCase02:OH_JSVM_CreateReference Failed.");
1190 return nullptr;
1191 }
1192
1193 JSVM_Value result = nullptr;
1194 status = OH_JSVM_GetReferenceValue(env, reference, &result);
1195 if (status != JSVM_OK) {
1196 OH_JSVM_ThrowError(env, nullptr, "TestGetReferenceValueCase02:OH_JSVM_GetReferenceValue Failed.");
1197 return nullptr;
1198 }
1199
1200 bool setValue = true;
1201 JSVM_Value retValue = nullptr;
1202 OH_JSVM_GetBoolean(env, setValue, &retValue);
1203 return retValue;
1204 }
1205 //Reference API:open handlescope -- create obj -- create reference -- increase citation count -- reduce citation count
1206 //--get reference info -- delete reference -- get reference, no result -- close handlescope
TestHandleAndRefCase01(JSVM_Env env,JSVM_CallbackInfo info)1207 [[maybe_unused]] JSVM_Value TestHandleAndRefCase01(JSVM_Env env, JSVM_CallbackInfo info)
1208 {
1209 JSVM_HandleScope handleScope;
1210 JSVM_Status status = OH_JSVM_OpenHandleScope(env, &handleScope);
1211 JSVM_Value val = nullptr;
1212 bool flag = true;
1213 OH_JSVM_GetBoolean(env, flag, &val);
1214 JSVM_Ref rstRef = nullptr;
1215 status = OH_JSVM_CreateReference(env, val, 1, &rstRef);
1216 uint32_t rstRefNum = 0;
1217 status = OH_JSVM_ReferenceRef(env, rstRef, &rstRefNum);
1218 if (status != JSVM_OK) {
1219 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase01:OH_JSVM_ReferenceRef Failed.");
1220 return nullptr;
1221 }
1222
1223 status = OH_JSVM_ReferenceUnref(env, rstRef, &rstRefNum);
1224 if (status != JSVM_OK) {
1225 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase01:OH_JSVM_ReferenceUnref Failed.");
1226 return nullptr;
1227 }
1228
1229 JSVM_Value result = nullptr;
1230 status = OH_JSVM_GetReferenceValue(env, rstRef, &result);
1231 if (status != JSVM_OK) {
1232 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase01:OH_JSVM_GetReferenceValue Failed.");
1233 return nullptr;
1234 }
1235
1236 status = OH_JSVM_DeleteReference(env, rstRef);
1237 if (status != JSVM_OK) {
1238 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase01:OH_JSVM_DeleteReference Failed.");
1239 return nullptr;
1240 }
1241
1242 status = OH_JSVM_CloseHandleScope(env, handleScope);
1243 if (status != JSVM_OK) {
1244 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase01:OH_JSVM_CloseHandleScope Failed.");
1245 return nullptr;
1246 }
1247
1248 bool setValue = true;
1249 JSVM_Value retValue = nullptr;
1250 OH_JSVM_GetBoolean(env, setValue, &retValue);
1251 return retValue;
1252 }
1253 //open escapehandlescope -- create obj -- escapehanle -- create reference -- increase citation count -- reduce
1254 //citation count -- get reference info -- get reference,no result -- delete reference -- closed escapehandlescope
TestHandleAndRefCase02(JSVM_Env env,JSVM_CallbackInfo info)1255 [[maybe_unused]] JSVM_Value TestHandleAndRefCase02(JSVM_Env env, JSVM_CallbackInfo info)
1256 {
1257 JSVM_InitOptions initOptions;
1258 if (memset_s(&initOptions, sizeof(initOptions), 0, sizeof(initOptions)) != 0) {
1259 return nullptr;
1260 }
1261 initOptions.externalReferences = g_externals;
1262 if (g_tempNum == 0) {
1263 OH_JSVM_Init(&initOptions);
1264 g_tempNum++;
1265 }
1266 JSVM_VM vm;
1267 JSVM_CreateVMOptions options;
1268 if (memset_s(&options, sizeof(options), 0, sizeof(options)) != 0) {
1269 return nullptr;
1270 }
1271 OH_JSVM_CreateVM(&options, &vm);
1272 JSVM_VMScope vmScope;
1273 OH_JSVM_OpenVMScope(vm, &vmScope);
1274 JSVM_Env env1;
1275 JSVM_CallbackStruct param[1];
1276 param[0].data = nullptr;
1277 param[0].callback = assertEqual;
1278 JSVM_PropertyDescriptor descriptor[] = {
1279 {"assertEqual", NULL, ¶m[0], NULL, NULL, NULL, JSVM_DEFAULT},
1280 };
1281 OH_JSVM_CreateEnv(vm, sizeof(descriptor) / sizeof(descriptor[0]), descriptor, &env1);
1282 JSVM_EnvScope envScope;
1283 OH_JSVM_OpenEnvScope(env1, &envScope);
1284 JSVM_HandleScope handleScope;
1285 OH_JSVM_OpenHandleScope(env1, &handleScope);
1286 JSVM_EscapableHandleScope handleScope1;
1287 JSVM_Status status = OH_JSVM_OpenEscapableHandleScope(env1, &handleScope1);
1288 if (status != JSVM_OK) {
1289 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase02:OH_JSVM_OpenEscapableHandleScope Failed.");
1290 return nullptr;
1291 }
1292
1293 JSVM_Value objValue;
1294 OH_JSVM_CreateObject(env1, &objValue);
1295
1296 const char testStr[] = "function";
1297 JSVM_Value value = nullptr;
1298 OH_JSVM_CreateStringUtf8(env1, testStr, strlen(testStr), &value);
1299 OH_JSVM_SetNamedProperty(env1, objValue, "test", value);
1300 JSVM_Value resultVar = nullptr;
1301 status = OH_JSVM_EscapeHandle(env, handleScope1, objValue, &resultVar);
1302 if (status != JSVM_OK) {
1303 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase02:OH_JSVM_EscapeHandle Failed.");
1304 return nullptr;
1305 }
1306
1307 JSVM_Ref rstRef = nullptr;
1308 status = OH_JSVM_CreateReference(env1, objValue, 1, &rstRef);
1309 if (status != JSVM_OK) {
1310 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase02:OH_JSVM_CreateReference Failed.");
1311 return nullptr;
1312 }
1313
1314 uint32_t rstNum = 0;
1315 status = OH_JSVM_ReferenceRef(env1, rstRef, &rstNum);
1316 if (status != JSVM_OK) {
1317 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase02:OH_JSVM_ReferenceRef Failed.");
1318 return nullptr;
1319 }
1320
1321 status = OH_JSVM_ReferenceUnref(env1, rstRef, &rstNum);
1322 if (status != JSVM_OK) {
1323 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase02:OH_JSVM_ReferenceUnref Failed.");
1324 return nullptr;
1325 }
1326
1327 JSVM_Value result = nullptr;
1328 status = OH_JSVM_GetReferenceValue(env1, rstRef, &result);
1329 if (status != JSVM_OK) {
1330 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase02:OH_JSVM_GetReferenceValue Failed.");
1331 return nullptr;
1332 }
1333
1334 status = OH_JSVM_DeleteReference(env1, rstRef);
1335 if (status != JSVM_OK) {
1336 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase02:OH_JSVM_DeleteReference Failed.");
1337 return nullptr;
1338 }
1339
1340 status = OH_JSVM_CloseEscapableHandleScope(env1, handleScope1);
1341 if (status != JSVM_OK) {
1342 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase02:OH_JSVM_CloseEscapableHandleScope Failed.");
1343 return nullptr;
1344 }
1345
1346 bool setValue = true;
1347 JSVM_Value retValue = nullptr;
1348 OH_JSVM_GetBoolean(env, setValue, &retValue);
1349 return retValue;
1350 }
1351 //create multiple reference,delete multiple referencee,result normal
TestHandleAndRefCase03(JSVM_Env env,JSVM_CallbackInfo info)1352 [[maybe_unused]] JSVM_Value TestHandleAndRefCase03(JSVM_Env env, JSVM_CallbackInfo info)
1353 {
1354 JSVM_Value value = NULL;
1355 OH_JSVM_CreateObject(env, &value);
1356
1357 JSVM_Ref rstRef = nullptr;
1358 JSVM_Status status = OH_JSVM_CreateReference(env, value, 1, &rstRef);
1359 if (status != JSVM_OK) {
1360 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_CreateReference Failed.");
1361 return nullptr;
1362 }
1363
1364 JSVM_Value value1 = nullptr;
1365 bool flag = true;
1366 OH_JSVM_GetBoolean(env, flag, &value1);
1367 JSVM_Ref rstRef1 = nullptr;
1368 status = OH_JSVM_CreateReference(env, value1, 1, &rstRef1);
1369 if (status != JSVM_OK) {
1370 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_CreateReference 1 Failed.");
1371 return nullptr;
1372 }
1373
1374 JSVM_Value value2 = nullptr;
1375 OH_JSVM_GetNull(env, &value2);
1376 JSVM_Ref rstRef2 = nullptr;
1377 status = OH_JSVM_CreateReference(env, value2, 1, &rstRef2);
1378 if (status != JSVM_OK) {
1379 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_CreateReference 2 Failed.");
1380 return nullptr;
1381 }
1382
1383 JSVM_Value value3 = nullptr;
1384 OH_JSVM_GetUndefined(env, &value3);
1385 JSVM_Ref rstRef3 = nullptr;
1386 status = OH_JSVM_CreateReference(env, value3, 1, &rstRef3);
1387 if (status != JSVM_OK) {
1388 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_CreateReference 3 Failed.");
1389 return nullptr;
1390 }
1391
1392 JSVM_Value value4;
1393 OH_JSVM_CreateInt32(env, 0, &value4);
1394 JSVM_Ref rstRef4 = nullptr;
1395 status = OH_JSVM_CreateReference(env, value4, 1, &rstRef4);
1396 if (status != JSVM_OK) {
1397 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_CreateReference 4 Failed.");
1398 return nullptr;
1399 }
1400
1401 size_t arrayLength = 2;
1402 JSVM_Value value5;
1403 OH_JSVM_CreateArrayWithLength(env, arrayLength, &value5);
1404 for (uint32_t i = 0; i < arrayLength; i++) {
1405 JSVM_Value element;
1406 OH_JSVM_CreateUint32(env, i + 1, &element);
1407 OH_JSVM_SetElement(env, value5, i, element);
1408 }
1409 JSVM_Ref rstRef5 = nullptr;
1410 status = OH_JSVM_CreateReference(env, value5, 1, &rstRef5);
1411 if (status != JSVM_OK) {
1412 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_CreateReference 5 Failed.");
1413 return nullptr;
1414 }
1415
1416 const char testStr[] = "func";
1417 JSVM_Value newResult = nullptr;
1418 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), &newResult);
1419 JSVM_Value value6;
1420 OH_JSVM_CoerceToObject(env, newResult, &value6);
1421 JSVM_Ref rstRef6 = nullptr;
1422 status = OH_JSVM_CreateReference(env, value6, 1, &rstRef6);
1423 if (status != JSVM_OK) {
1424 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_CreateReference 6 Failed.");
1425 return nullptr;
1426 }
1427
1428 status = OH_JSVM_DeleteReference(env, rstRef);
1429 if (status != JSVM_OK) {
1430 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_DeleteReference Failed.");
1431 return nullptr;
1432 }
1433
1434 status = OH_JSVM_DeleteReference(env, rstRef1);
1435 if (status != JSVM_OK) {
1436 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_DeleteReference 1 Failed.");
1437 return nullptr;
1438 }
1439
1440 status = OH_JSVM_DeleteReference(env, rstRef2);
1441 if (status != JSVM_OK) {
1442 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_DeleteReference 2 Failed.");
1443 return nullptr;
1444 }
1445
1446 status = OH_JSVM_DeleteReference(env, rstRef3);
1447 if (status != JSVM_OK) {
1448 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_DeleteReference 3 Failed.");
1449 return nullptr;
1450 }
1451
1452 status = OH_JSVM_DeleteReference(env, rstRef4);
1453 if (status != JSVM_OK) {
1454 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_DeleteReference 4 Failed.");
1455 return nullptr;
1456 }
1457
1458 status = OH_JSVM_DeleteReference(env, rstRef5);
1459 if (status != JSVM_OK) {
1460 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_DeleteReference 5 Failed.");
1461 return nullptr;
1462 }
1463
1464 status = OH_JSVM_DeleteReference(env, rstRef6);
1465 if (status != JSVM_OK) {
1466 OH_JSVM_ThrowError(env, nullptr, "TestHandleAndRefCase03:OH_JSVM_DeleteReference 6 Failed.");
1467 return nullptr;
1468 }
1469
1470 bool setValue = true;
1471 JSVM_Value retValue = nullptr;
1472 OH_JSVM_GetBoolean(env, setValue, &retValue);
1473 return retValue;
1474 }