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_class_test.h"
18
19
20 //OH_JSVM_NewInstance:Constructor is a constructor with input parameters,argc、argv normal,result not null
TestNewInstanceCase01(JSVM_Env env,JSVM_CallbackInfo info)21 [[maybe_unused]] JSVM_Value TestNewInstanceCase01(JSVM_Env env, JSVM_CallbackInfo info)
22 {
23 JSVM_Value testClass = nullptr;
24 JSVM_CallbackStruct param1;
25 param1.data = nullptr;
26 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
27 JSVM_Value thisVar = nullptr;
28 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
29
30 return thisVar;
31 };
32
33 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
34 if (status != JSVM_OK) {
35 OH_JSVM_ThrowError(env, nullptr, "TestNewInstanceCase01:OH_JSVM_DefineClass Failed.");
36 return nullptr;
37 }
38
39 JSVM_Value instanceValue = nullptr;
40 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
41 if (status != JSVM_OK) {
42 OH_JSVM_ThrowError(env, nullptr, "TestNewInstanceCase01:OH_JSVM_NewInstance Failed.");
43 return nullptr;
44 }
45
46 bool setValue = true;
47 JSVM_Value retValue = nullptr;
48 OH_JSVM_GetBoolean(env, setValue, &retValue);
49 return retValue;
50 }
51 //Constructor is not a constructor,argc\argv nullptr, result not null
TestNewInstanceCase02(JSVM_Env env,JSVM_CallbackInfo info)52 [[maybe_unused]] JSVM_Value TestNewInstanceCase02(JSVM_Env env, JSVM_CallbackInfo info)
53 {
54 JSVM_Value testClass = nullptr;
55
56 JSVM_Value instanceValue = nullptr;
57 JSVM_Status status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
58 if (status != JSVM_INVALID_ARG) {
59 OH_JSVM_ThrowError(env, nullptr, "TestNewInstanceCase02:OH_JSVM_NewInstance Failed.");
60 return nullptr;
61 }
62
63 bool setValue = true;
64 JSVM_Value retValue = nullptr;
65 OH_JSVM_GetBoolean(env, setValue, &retValue);
66 return retValue;
67 }
68 //OH_JSVM_GetNewTarget:cbinfo not null + result not null
TestGetNewTargetCase01(JSVM_Env env,JSVM_CallbackInfo info)69 [[maybe_unused]] JSVM_Value TestGetNewTargetCase01(JSVM_Env env, JSVM_CallbackInfo info)
70 {
71 JSVM_Value newTarget = nullptr;
72 JSVM_Status status = OH_JSVM_GetNewTarget(env, info, &newTarget);
73 if (status != JSVM_OK) {
74 OH_JSVM_ThrowError(env, nullptr, "TestGetNewTargetCase01:OH_JSVM_GetNewTarget Failed.");
75 return nullptr;
76 }
77
78 bool setValue = true;
79 JSVM_Value retValue = nullptr;
80 OH_JSVM_GetBoolean(env, setValue, &retValue);
81 return retValue;
82 }
83 //cbinfo NULL , result not null
TestGetNewTargetCase02(JSVM_Env env,JSVM_CallbackInfo info)84 [[maybe_unused]] JSVM_Value TestGetNewTargetCase02(JSVM_Env env, JSVM_CallbackInfo info)
85 {
86 JSVM_Value newTarget = nullptr;
87 JSVM_Status status = OH_JSVM_GetNewTarget(env, nullptr, &newTarget);
88 if (status != JSVM_INVALID_ARG) {
89 OH_JSVM_ThrowError(env, nullptr, "TestGetNewTargetCase02:OH_JSVM_GetNewTarget Failed.");
90 return nullptr;
91 }
92
93 bool setValue = true;
94 JSVM_Value retValue = nullptr;
95 OH_JSVM_GetBoolean(env, setValue, &retValue);
96 return retValue;
97 }
98 //cbinfo not null,result NULL
TestGetNewTargetCase03(JSVM_Env env,JSVM_CallbackInfo info)99 [[maybe_unused]] JSVM_Value TestGetNewTargetCase03(JSVM_Env env, JSVM_CallbackInfo info)
100 {
101 JSVM_Status status = OH_JSVM_GetNewTarget(env, info, nullptr);
102 if (status != JSVM_INVALID_ARG) {
103 OH_JSVM_ThrowError(env, nullptr, "TestGetNewTargetCase03:OH_JSVM_GetNewTarget Failed.");
104 return nullptr;
105 }
106
107 bool setValue = true;
108 JSVM_Value retValue = nullptr;
109 OH_JSVM_GetBoolean(env, setValue, &retValue);
110 return retValue;
111 }
112 //OH_JSVM_DefineClass:name not null + length <= utf8name + properties not null + propertyCount <= properties + result
TestDefineClassCase01(JSVM_Env env,JSVM_CallbackInfo info)113 [[maybe_unused]] JSVM_Value TestDefineClassCase01(JSVM_Env env, JSVM_CallbackInfo info)
114 {
115 JSVM_Value testWrapClass = nullptr;
116 JSVM_CallbackStruct param1;
117 param1.data = nullptr;
118 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
119 JSVM_Value thisVar = nullptr;
120 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
121 return thisVar;
122 };
123
124 JSVM_Status status = OH_JSVM_DefineClass(env,
125 "TestWrapClass",
126 NAPI_AUTO_LENGTH,
127 ¶m1,
128 0,
129 nullptr,
130 &testWrapClass);
131 if (status != JSVM_OK) {
132 OH_JSVM_ThrowError(env, nullptr, "TestDefineClassCase01:OH_JSVM_DefineClass Failed.");
133 return nullptr;
134 }
135
136 bool setValue = true;
137 JSVM_Value retValue = nullptr;
138 OH_JSVM_GetBoolean(env, setValue, &retValue);
139 return retValue;
140 }
141 //name not null + length <= utf8name + properties NULL+ propertyCount 0 + result NULL
TestDefineClassCase02(JSVM_Env env,JSVM_CallbackInfo info)142 [[maybe_unused]] JSVM_Value TestDefineClassCase02(JSVM_Env env, JSVM_CallbackInfo info)
143 {
144 JSVM_CallbackStruct param1;
145 param1.data = nullptr;
146 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
147 JSVM_Value thisVar = nullptr;
148 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
149 return thisVar;
150 };
151
152 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", NAPI_AUTO_LENGTH, ¶m1, 0, nullptr, nullptr);
153 if (status != JSVM_INVALID_ARG) {
154 OH_JSVM_ThrowError(env, nullptr, "TestDefineClassCase02:OH_JSVM_DefineClass abnormal.");
155 return nullptr;
156 }
157
158 bool setValue = true;
159 JSVM_Value retValue = nullptr;
160 OH_JSVM_GetBoolean(env, setValue, &retValue);
161 return retValue;
162 }
163 //OH_JSVM_Wrap:jsobject+nativeObject+finalizeCb+finalizeHint+result not null
TestWrapCase01(JSVM_Env env,JSVM_CallbackInfo info)164 [[maybe_unused]] JSVM_Value TestWrapCase01(JSVM_Env env, JSVM_CallbackInfo info)
165 {
166 JSVM_Value testClass = nullptr;
167 JSVM_CallbackStruct param1;
168 param1.data = nullptr;
169 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
170 JSVM_Value thisVar = nullptr;
171 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
172
173 return thisVar;
174 };
175
176 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
177 if (status != JSVM_OK) {
178 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase01:OH_JSVM_DefineClass Failed.");
179 return nullptr;
180 }
181
182 JSVM_Value instanceValue = nullptr;
183 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
184 if (status != JSVM_OK) {
185 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase01:OH_JSVM_NewInstance Failed.");
186 return nullptr;
187 }
188
189 const char *testStr = "test";
190 void* finalizeHint = malloc(10);
191 JSVM_Ref resultRef = nullptr;
192 status = OH_JSVM_Wrap(
193 env, instanceValue, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, finalizeHint, &resultRef);
194 if (status != JSVM_OK) {
195 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase01:OH_JSVM_Wrap Failed.");
196 return nullptr;
197 }
198
199 bool setValue = true;
200 JSVM_Value retValue = nullptr;
201 OH_JSVM_GetBoolean(env, setValue, &retValue);
202 return retValue;
203 }
204 //jsobject|nativeObject not null + finalizeCb NULL + finalizeHint NULL + result NULL
TestWrapCase02(JSVM_Env env,JSVM_CallbackInfo info)205 [[maybe_unused]] JSVM_Value TestWrapCase02(JSVM_Env env, JSVM_CallbackInfo info)
206 {
207 JSVM_Value testClass = nullptr;
208 JSVM_CallbackStruct param1;
209 param1.data = nullptr;
210 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
211 JSVM_Value thisVar = nullptr;
212 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
213
214 return thisVar;
215 };
216
217 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
218 if (status != JSVM_OK) {
219 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase02:OH_JSVM_DefineClass Failed.");
220 return nullptr;
221 }
222
223 JSVM_Value instanceValue = nullptr;
224 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
225 if (status != JSVM_OK) {
226 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase02:OH_JSVM_NewInstance Failed.");
227 return nullptr;
228 }
229
230 const char *testStr = "test";
231 status = OH_JSVM_Wrap(env, instanceValue, (void *)testStr, nullptr, nullptr, nullptr);
232 if (status != JSVM_OK) {
233 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase02:OH_JSVM_Wrap Failed.");
234 return nullptr;
235 }
236
237 bool setValue = true;
238 JSVM_Value retValue = nullptr;
239 OH_JSVM_GetBoolean(env, setValue, &retValue);
240 return retValue;
241 }
242 //jsobject NULL + nativeObject not null + finalizeCb NULL + finalizeHint NULL + result NULL
TestWrapCase03(JSVM_Env env,JSVM_CallbackInfo info)243 [[maybe_unused]] JSVM_Value TestWrapCase03(JSVM_Env env, JSVM_CallbackInfo info)
244 {
245 const char *testStr = "test";
246 JSVM_Status status = OH_JSVM_Wrap(env, nullptr, (void *)testStr, nullptr, nullptr, nullptr);
247 if (status == JSVM_OK) {
248 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase03:OH_JSVM_Wrap Failed.");
249 return nullptr;
250 }
251
252 bool setValue = true;
253 JSVM_Value retValue = nullptr;
254 OH_JSVM_GetBoolean(env, setValue, &retValue);
255 return retValue;
256 }
257 //jsobject not null + nativeObject NULL + finalizeCb NULL + finalizeHint NULL + result NULL
TestWrapCase04(JSVM_Env env,JSVM_CallbackInfo info)258 [[maybe_unused]] JSVM_Value TestWrapCase04(JSVM_Env env, JSVM_CallbackInfo info)
259 {
260 JSVM_Value testClass = nullptr;
261 JSVM_CallbackStruct param1;
262 param1.data = nullptr;
263 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
264 JSVM_Value thisVar = nullptr;
265 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
266
267 return thisVar;
268 };
269
270 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
271 if (status != JSVM_OK) {
272 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase04:OH_JSVM_DefineClass Failed.");
273 return nullptr;
274 }
275
276 JSVM_Value instanceValue = nullptr;
277 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
278 if (status != JSVM_OK) {
279 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase04:OH_JSVM_NewInstance Failed.");
280 return nullptr;
281 }
282
283 status = OH_JSVM_Wrap(env, nullptr, instanceValue, nullptr, nullptr, nullptr);
284 if (status == JSVM_OK) {
285 OH_JSVM_ThrowError(env, nullptr, "TestWrapCase04:OH_JSVM_Wrap Failed.");
286 return nullptr;
287 }
288
289 bool setValue = true;
290 JSVM_Value retValue = nullptr;
291 OH_JSVM_GetBoolean(env, setValue, &retValue);
292 return retValue;
293 }
294 //OH_JSVM_Unwrap:JsObject has been wrapped with other object instances + result not null
TestUnwrapCase01(JSVM_Env env,JSVM_CallbackInfo info)295 [[maybe_unused]] JSVM_Value TestUnwrapCase01(JSVM_Env env, JSVM_CallbackInfo info)
296 {
297 JSVM_Value testClass = nullptr;
298 JSVM_CallbackStruct param1;
299 param1.data = nullptr;
300 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
301 JSVM_Value thisVar = nullptr;
302 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
303
304 return thisVar;
305 };
306
307 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
308 if (status != JSVM_OK) {
309 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase01:OH_JSVM_DefineClass Failed.");
310 return nullptr;
311 }
312
313 JSVM_Value instanceValue = nullptr;
314 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
315 if (status != JSVM_OK) {
316 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase01:OH_JSVM_NewInstance Failed.");
317 return nullptr;
318 }
319
320 const char *testStr = "test";
321 void* finalizeHint = malloc(0);
322 JSVM_Ref resultRef = nullptr;
323 status = OH_JSVM_Wrap(
324 env, instanceValue, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, finalizeHint, &resultRef);
325 if (status != JSVM_OK) {
326 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase01:OH_JSVM_Wrap Failed.");
327 return nullptr;
328 }
329
330 const char *tmpTestStr = nullptr;
331 status = OH_JSVM_Unwrap(env, instanceValue, (void **)&tmpTestStr);
332 if (status != JSVM_OK) {
333 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase01:OH_JSVM_Unwrap Failed.");
334 return nullptr;
335 }
336
337 bool setValue = true;
338 JSVM_Value retValue = nullptr;
339 OH_JSVM_GetBoolean(env, setValue, &retValue);
340 return retValue;
341 }
342 //JsObject is not wrapped with other object instances + result not null
TestUnwrapCase02(JSVM_Env env,JSVM_CallbackInfo info)343 [[maybe_unused]] JSVM_Value TestUnwrapCase02(JSVM_Env env, JSVM_CallbackInfo info)
344 {
345 JSVM_Value testClass = nullptr;
346 JSVM_CallbackStruct param1;
347 param1.data = nullptr;
348 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
349 JSVM_Value thisVar = nullptr;
350 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
351
352 return thisVar;
353 };
354
355 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
356 if (status != JSVM_OK) {
357 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase02:OH_JSVM_DefineClass Failed.");
358 return nullptr;
359 }
360
361 JSVM_Value instanceValue = nullptr;
362 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
363 if (status != JSVM_OK) {
364 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase02:OH_JSVM_NewInstance Failed.");
365 return nullptr;
366 }
367
368 const char *tmpTestStr = nullptr;
369 status = OH_JSVM_Unwrap(env, instanceValue, (void **)&tmpTestStr);
370 if (status != JSVM_INVALID_ARG) {
371 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase02:OH_JSVM_Unwrap Failed.");
372 return nullptr;
373 }
374
375 bool setValue = true;
376 JSVM_Value retValue = nullptr;
377 OH_JSVM_GetBoolean(env, setValue, &retValue);
378 return retValue;
379 }
380 //jsObject NULL + result not null
TestUnwrapCase03(JSVM_Env env,JSVM_CallbackInfo info)381 [[maybe_unused]] JSVM_Value TestUnwrapCase03(JSVM_Env env, JSVM_CallbackInfo info)
382 {
383 const char *tmpTestStr = nullptr;
384 JSVM_Status status = OH_JSVM_Unwrap(env, nullptr, (void **)&tmpTestStr);
385 if (status == JSVM_OK) {
386 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase03:OH_JSVM_Unwrap Failed.");
387 return nullptr;
388 }
389
390 bool setValue = true;
391 JSVM_Value retValue = nullptr;
392 OH_JSVM_GetBoolean(env, setValue, &retValue);
393 return retValue;
394 }
395 //JsObject has been wrapped with other object instances + result NULL
TestUnwrapCase04(JSVM_Env env,JSVM_CallbackInfo info)396 [[maybe_unused]] JSVM_Value TestUnwrapCase04(JSVM_Env env, JSVM_CallbackInfo info)
397 {
398 JSVM_Value testClass = nullptr;
399 JSVM_CallbackStruct param1;
400 param1.data = nullptr;
401 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
402 JSVM_Value thisVar = nullptr;
403 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
404
405 return thisVar;
406 };
407
408 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
409 if (status != JSVM_OK) {
410 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase04:OH_JSVM_DefineClass Failed.");
411 return nullptr;
412 }
413
414 JSVM_Value instanceValue = nullptr;
415 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
416 if (status != JSVM_OK) {
417 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase04:OH_JSVM_NewInstance Failed.");
418 return nullptr;
419 }
420
421 const char *testStr = "test";
422 void* finalizeHint = malloc(0);
423 JSVM_Ref resultRef = nullptr;
424 status = OH_JSVM_Wrap(
425 env, instanceValue, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, finalizeHint, &resultRef);
426 if (status != JSVM_OK) {
427 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase04:OH_JSVM_Wrap Failed.");
428 return nullptr;
429 }
430
431 status = OH_JSVM_Unwrap(env, instanceValue, nullptr);
432 if (status != JSVM_INVALID_ARG) {
433 OH_JSVM_ThrowError(env, nullptr, "TestUnwrapCase04:OH_JSVM_Unwrap Failed.");
434 return nullptr;
435 }
436
437 bool setValue = true;
438 JSVM_Value retValue = nullptr;
439 OH_JSVM_GetBoolean(env, setValue, &retValue);
440 return retValue;
441 }
442 //OH_JSVM_RemoveWrap:JsObject has been wrapped with other object instances + result not null
TestRemoveWrapCase01(JSVM_Env env,JSVM_CallbackInfo info)443 [[maybe_unused]] JSVM_Value TestRemoveWrapCase01(JSVM_Env env, JSVM_CallbackInfo info)
444 {
445 JSVM_Value testClass = nullptr;
446 JSVM_CallbackStruct param1;
447 param1.data = nullptr;
448 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
449 JSVM_Value thisVar = nullptr;
450 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
451
452 return thisVar;
453 };
454
455 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
456 if (status != JSVM_OK) {
457 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase01:OH_JSVM_DefineClass Failed.");
458 return nullptr;
459 }
460
461 JSVM_Value instanceValue = nullptr;
462 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
463 if (status != JSVM_OK) {
464 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase01:OH_JSVM_NewInstance Failed.");
465 return nullptr;
466 }
467
468 const char *testStr = "test";
469 void* finalizeHint = malloc(0);
470 JSVM_Ref resultRef = nullptr;
471 status = OH_JSVM_Wrap(
472 env, instanceValue, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, finalizeHint, &resultRef);
473 if (status != JSVM_OK) {
474 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase01:OH_JSVM_Wrap Failed.");
475 return nullptr;
476 }
477
478 const char *tmpTestStr1 = nullptr;
479 status = OH_JSVM_RemoveWrap(env, instanceValue, (void **)&tmpTestStr1);
480 if (status != JSVM_OK) {
481 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase01:OH_JSVM_RemoveWrap Failed.");
482 return nullptr;
483 }
484
485 bool setValue = true;
486 JSVM_Value retValue = nullptr;
487 OH_JSVM_GetBoolean(env, setValue, &retValue);
488 return retValue;
489 }
490 //JsObject is not wrapped with other object instances + result not null
TestRemoveWrapCase02(JSVM_Env env,JSVM_CallbackInfo info)491 [[maybe_unused]] JSVM_Value TestRemoveWrapCase02(JSVM_Env env, JSVM_CallbackInfo info)
492 {
493 JSVM_Value testClass = nullptr;
494 JSVM_CallbackStruct param1;
495 param1.data = nullptr;
496 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
497 JSVM_Value thisVar = nullptr;
498 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
499
500 return thisVar;
501 };
502
503 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
504 if (status != JSVM_OK) {
505 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase02:OH_JSVM_DefineClass Failed.");
506 return nullptr;
507 }
508
509 JSVM_Value instanceValue = nullptr;
510 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
511 if (status != JSVM_OK) {
512 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase02:OH_JSVM_NewInstance Failed.");
513 return nullptr;
514 }
515
516 const char *tmpTestStr1 = nullptr;
517 status = OH_JSVM_RemoveWrap(env, instanceValue, (void **)&tmpTestStr1);
518 if (status != JSVM_INVALID_ARG) {
519 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase02:OH_JSVM_RemoveWrap Failed.");
520 return nullptr;
521 }
522
523 bool setValue = true;
524 JSVM_Value retValue = nullptr;
525 OH_JSVM_GetBoolean(env, setValue, &retValue);
526 return retValue;
527 }
528 //jsObject NULL + result not null
TestRemoveWrapCase03(JSVM_Env env,JSVM_CallbackInfo info)529 [[maybe_unused]] JSVM_Value TestRemoveWrapCase03(JSVM_Env env, JSVM_CallbackInfo info)
530 {
531 const char *tmpTestStr1 = nullptr;
532 JSVM_Status status = OH_JSVM_RemoveWrap(env, nullptr, (void **)&tmpTestStr1);
533 if (status != JSVM_INVALID_ARG) {
534 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase03:OH_JSVM_RemoveWrap Failed.");
535 return nullptr;
536 }
537
538 bool setValue = true;
539 JSVM_Value retValue = nullptr;
540 OH_JSVM_GetBoolean(env, setValue, &retValue);
541 return retValue;
542 }
543 //JsObject has been wrapped with other object instances + result NULL
TestRemoveWrapCase04(JSVM_Env env,JSVM_CallbackInfo info)544 [[maybe_unused]] JSVM_Value TestRemoveWrapCase04(JSVM_Env env, JSVM_CallbackInfo info)
545 {
546 JSVM_Value testClass = nullptr;
547 JSVM_CallbackStruct param1;
548 param1.data = nullptr;
549 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
550 JSVM_Value thisVar = nullptr;
551 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
552
553 return thisVar;
554 };
555
556 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
557 if (status != JSVM_OK) {
558 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase04:OH_JSVM_DefineClass Failed.");
559 return nullptr;
560 }
561
562 JSVM_Value instanceValue = nullptr;
563 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
564 if (status != JSVM_OK) {
565 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase04:OH_JSVM_NewInstance Failed.");
566 return nullptr;
567 }
568
569 const char *testStr = "test";
570 void* finalizeHint = malloc(0);
571 JSVM_Ref resultRef = nullptr;
572 status = OH_JSVM_Wrap(
573 env, instanceValue, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, finalizeHint, &resultRef);
574 if (status != JSVM_OK) {
575 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase04:OH_JSVM_Wrap Failed.");
576 return nullptr;
577 }
578
579 status = OH_JSVM_RemoveWrap(env, instanceValue, nullptr);
580 if (status != JSVM_OK) {
581 OH_JSVM_ThrowError(env, nullptr, "TestRemoveWrapCase04:OH_JSVM_RemoveWrap Failed.");
582 return nullptr;
583 }
584
585 bool setValue = true;
586 JSVM_Value retValue = nullptr;
587 OH_JSVM_GetBoolean(env, setValue, &retValue);
588 return retValue;
589 }
590 //API:defineClass -> NewInstance -> Wrap -> Unwrap ok -> RemoveWrap -> unwrap fail
TestClassOperationCase01(JSVM_Env env,JSVM_CallbackInfo info)591 [[maybe_unused]] JSVM_Value TestClassOperationCase01(JSVM_Env env, JSVM_CallbackInfo info)
592 {
593 JSVM_Value testClass = nullptr;
594 JSVM_CallbackStruct param1;
595 param1.data = nullptr;
596 param1.callback = [](JSVM_Env env, JSVM_CallbackInfo info) -> JSVM_Value {
597 JSVM_Value thisVar = nullptr;
598 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
599 return thisVar;
600 };
601 JSVM_Status status = OH_JSVM_DefineClass(env, "TestClass", JSVM_AUTO_LENGTH, ¶m1, 0, nullptr, &testClass);
602
603 JSVM_Value instanceValue = nullptr;
604 status = OH_JSVM_NewInstance(env, testClass, 0, nullptr, &instanceValue);
605 if (status != JSVM_OK) {
606 OH_JSVM_ThrowError(env, nullptr, "TestClassOperationCase01:OH_JSVM_NewInstance Failed.");
607 return nullptr;
608 }
609
610 const char *testStr = "test";
611 void* finalizeHint = malloc(100);
612 JSVM_Ref resultRef = nullptr;
613 status = OH_JSVM_Wrap(
614 env, instanceValue, (void *)testStr, [](JSVM_Env env, void *data, void *hint) {}, finalizeHint, &resultRef);
615 if (status != JSVM_OK) {
616 OH_JSVM_ThrowError(env, nullptr, "TestClassOperationCase01:OH_JSVM_Wrap Failed.");
617 return nullptr;
618 }
619
620 const char *tmpTestStr1 = nullptr;
621 status = OH_JSVM_Unwrap(env, instanceValue, (void **)&tmpTestStr1);
622 if (status != JSVM_OK) {
623 OH_JSVM_ThrowError(env, nullptr, "TestClassOperationCase01:OH_JSVM_Unwrap Failed.");
624 return nullptr;
625 }
626
627 status = OH_JSVM_RemoveWrap(env, instanceValue, nullptr);
628 if (status != JSVM_OK) {
629 OH_JSVM_ThrowError(env, nullptr, "TestClassOperationCase01:OH_JSVM_RemoveWrap Failed.");
630 return nullptr;
631 }
632
633 const char *tmpTestStr2 = nullptr;
634 status = OH_JSVM_Unwrap(env, instanceValue, (void **)&tmpTestStr2);
635 if (status != JSVM_INVALID_ARG) {
636 OH_JSVM_ThrowError(env, nullptr, "TestClassOperationCase01:OH_JSVM_Unwrap Abnormal.");
637 return nullptr;
638 }
639
640 bool setValue = true;
641 JSVM_Value retValue = nullptr;
642 OH_JSVM_GetBoolean(env, setValue, &retValue);
643 return retValue;
644 }