1 /*
2 * Copyright (c) 2024 SwanLink (Jiangsu) Technology Development Co., LTD.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "cstring"
16 #include "napi/native_api.h"
17 #include "napi_functioncall_test.h"
18
19 const int DIFF_VALUE_NOE = 1;
20 const int DIFF_VALUE_TWO = 2;
21 //OH_JSVM_CallFunction:recv nullptr,CreateFunction without input parameters, argc 0, argv nullptr, result not null
22 //return 0, result is consistent with the expected value of the function in CreateFunction
TestCallFunction01(JSVM_Env env,JSVM_CallbackInfo info)23 [[maybe_unused]] JSVM_Value TestCallFunction01(JSVM_Env env, JSVM_CallbackInfo info)
24 {
25 JSVM_Value result;
26 JSVM_Value funcValue = nullptr;
27 JSVM_CallbackStruct param;
28 JSVM_Status status = OH_JSVM_CreateFunction(env, "func", NAPI_AUTO_LENGTH, ¶m, &funcValue);
29 if (status != JSVM_OK) {
30 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction01:OH_JSVM_CreateFunction status Failed.");
31 return nullptr;
32 }
33
34 status = OH_JSVM_CallFunction(env, nullptr, funcValue, 0, nullptr, &result);
35 if (status != JSVM_INVALID_ARG) {
36 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction01:OH_JSVM_CallFunction status Failed.");
37 return nullptr;
38 }
39
40 bool setValue = true;
41 JSVM_Value retValue = nullptr;
42 OH_JSVM_GetBoolean(env, setValue, &retValue);
43 return retValue;
44 }
45 //recv undefined, CreateFunction without input parameters,argc 0, argv nullptr, result not null
46 //return 0, result is consistent with the expected value of the function in CreateFunction
CalculateArea(JSVM_Env env,JSVM_CallbackInfo info)47 static JSVM_Value CalculateArea(JSVM_Env env, JSVM_CallbackInfo info)
48 {
49 size_t argc = 2;
50 JSVM_Value args[2] = {nullptr};
51 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
52 double width;
53 OH_JSVM_GetValueDouble(env, args[0], &width);
54 double height;
55 OH_JSVM_GetValueDouble(env, args[1], &height);
56 JSVM_Value area;
57 OH_JSVM_CreateDouble(env, width * height, &area);
58 return area;
59 }
60
TestCallFunction02(JSVM_Env env,JSVM_CallbackInfo info)61 [[maybe_unused]] JSVM_Value TestCallFunction02(JSVM_Env env, JSVM_CallbackInfo info)
62 {
63 JSVM_Value funcValue = nullptr;
64 JSVM_CallbackStruct param;
65 param.data = nullptr;
66 param.callback = CalculateArea;
67 JSVM_Status status = OH_JSVM_CreateFunction(env, "CalculateArea", NAPI_AUTO_LENGTH, ¶m, &funcValue);
68 if (status != JSVM_OK) {
69 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction02:OH_JSVM_CreateFunction status Failed.");
70 return nullptr;
71 }
72
73 JSVM_Value recv = nullptr;
74 JSVM_Value result;
75 status = OH_JSVM_GetUndefined(env, &recv);
76 if (status != JSVM_OK) {
77 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction02:OH_JSVM_GetUndefined status Failed.");
78 return nullptr;
79 }
80 status = OH_JSVM_CallFunction(env, recv, funcValue, 0, nullptr, &result);
81 if (status != JSVM_OK) {
82 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction02:OH_JSVM_CallFunction status Failed.");
83 return nullptr;
84 }
85
86 int32_t rst = 0;
87 OH_JSVM_GetValueInt32(env, result, &rst);
88 if (rst != 0) {
89 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction02:call back result is Abnormal.");
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 //recv undefined,CreateFunction without input parameters, argc 0, argv nullptr, result not null
99 //return not ok
TestCallFunction03(JSVM_Env env,JSVM_CallbackInfo info)100 [[maybe_unused]] JSVM_Value TestCallFunction03(JSVM_Env env, JSVM_CallbackInfo info)
101 {
102 size_t argc = 3;
103 JSVM_Value argv[3] = { nullptr};
104 JSVM_Value thisArg = nullptr;
105 OH_JSVM_GetCbInfo(env, info, &argc, argv, &thisArg, nullptr);
106
107 uint32_t arrayLen = 0;
108 OH_JSVM_GetArrayLength(env, argv[1], &arrayLen);
109 if (arrayLen < 0 || arrayLen > std::numeric_limits<uint32_t>::max() / sizeof(JSVM_Value)) {
110 OH_JSVM_ThrowError(env, nullptr, "Invalid array length.");
111 return nullptr;
112 } else {
113 JSVM_Value *args = new JSVM_Value[arrayLen];
114 for (auto i = 0; i < arrayLen; i++) {
115 OH_JSVM_GetElement(env, argv[1], i, &args[i]);
116 }
117 }
118 JSVM_Value jsString;
119 JSVM_Status status = OH_JSVM_CoerceToString(env, argv[0], &jsString);
120 if (status != JSVM_OK) {
121 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction03:OH_JSVM_CoerceToString status Failed.");
122 return nullptr;
123 }
124 size_t length = 0;
125 status = OH_JSVM_GetValueStringUtf8(env, jsString, NULL, 0, &length);
126 if (status != JSVM_OK) {
127 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction03:OH_JSVM_GetValueStringUtf8 status Failed.");
128 return nullptr;
129 }
130 size_t capacity = length + 1;
131 char *buffer = new char[capacity];
132 size_t copyLength = 0;
133 status = OH_JSVM_GetValueStringUtf8(env, jsString, buffer, capacity, ©Length);
134 if (status != JSVM_OK) {
135 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction03:OH_JSVM_GetValueStringUtf8 status Failed.");
136 return nullptr;
137 }
138 JSVM_Value func = nullptr;
139 status = OH_JSVM_CreateFunctionWithScript(env,
140 buffer,
141 JSVM_AUTO_LENGTH,
142 arrayLen,
143 0,
144 nullptr,
145 &func);
146 if (status != JSVM_INVALID_ARG) {
147 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction03:OH_JSVM_CreateFunction status Failed.");
148 return nullptr;
149 }
150
151 JSVM_Value result;
152 JSVM_Value recv = nullptr;
153 OH_JSVM_GetUndefined(env, &recv);
154 status = OH_JSVM_CallFunction(env, recv, func, 0, nullptr, &result);
155 if (status != JSVM_INVALID_ARG) {
156 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction03:OH_JSVM_CallFunction status Failed.");
157 return nullptr;
158 }
159
160 bool setValue = true;
161 JSVM_Value retValue = nullptr;
162 OH_JSVM_GetBoolean(env, setValue, &retValue);
163 return retValue;
164 }
165
TestCallFunction04(JSVM_Env env,JSVM_CallbackInfo info)166 [[maybe_unused]] JSVM_Value TestCallFunction04(JSVM_Env env, JSVM_CallbackInfo info)
167 {
168 JSVM_CallbackStruct param;
169 param.data = nullptr;
170 param.callback = CalculateArea;
171 JSVM_Value funcValue = nullptr;
172 JSVM_Status status = OH_JSVM_CreateFunction(env, "calculateArea", NAPI_AUTO_LENGTH, ¶m, &funcValue);
173 if (status != JSVM_OK) {
174 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction04:OH_JSVM_CreateFunction status Failed.");
175 return nullptr;
176 }
177
178 JSVM_Value globalRecv = nullptr;
179 JSVM_Value result = nullptr;
180 status = OH_JSVM_GetGlobal(env, &globalRecv);
181 if (status != JSVM_OK) {
182 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction04:OH_JSVM_GetGlobal status Failed.");
183 return nullptr;
184 }
185 status = OH_JSVM_CallFunction(env, globalRecv, funcValue, 0, nullptr, &result);
186 if (status != JSVM_OK) {
187 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction04:OH_JSVM_CallFunction status Failed.");
188 return nullptr;
189 }
190 int32_t rst = 0;
191 OH_JSVM_GetValueInt32(env, result, &rst);
192 if (rst != 0) {
193 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction04:call back result is Abnormal.");
194 return nullptr;
195 }
196
197 bool setValue = true;
198 JSVM_Value retValue = nullptr;
199 OH_JSVM_GetBoolean(env, setValue, &retValue);
200 return retValue;
201 }
202 //recv global,CreateFunction interface that requires one input parameter, argc 1,argv not null,len 1, result not null
203 //return 0,result is consistent with the expected value of the function in CreateFunction
valueAddOne(JSVM_Env env,JSVM_CallbackInfo info)204 [[maybe_unused]] JSVM_Value valueAddOne(JSVM_Env env, JSVM_CallbackInfo info)
205 {
206 size_t argc = 1;
207 JSVM_Value argv[1] = {nullptr};
208 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
209
210 int32_t val;
211 OH_JSVM_GetValueInt32(env, argv[0], &val);
212
213 JSVM_Value rst;
214 OH_JSVM_CreateInt32(env, val + 1, &rst);
215 return rst;
216 }
TestCallFunction05(JSVM_Env env,JSVM_CallbackInfo info)217 [[maybe_unused]] JSVM_Value TestCallFunction05(JSVM_Env env, JSVM_CallbackInfo info)
218 {
219 JSVM_Value funcValue = nullptr;
220 JSVM_CallbackStruct param;
221 param.data = nullptr;
222 param.callback = valueAddOne;
223 JSVM_Status status = OH_JSVM_CreateFunction(env, "valueAddOne", NAPI_AUTO_LENGTH, ¶m, &funcValue);
224 if (status != JSVM_OK) {
225 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction05:OH_JSVM_CreateFunction status Failed.");
226 return nullptr;
227 }
228
229 JSVM_Value gloablRecv = nullptr;
230 OH_JSVM_GetGlobal(env, &gloablRecv);
231 JSVM_Value param1;
232 OH_JSVM_CreateInt32(env, 1, ¶m1);
233 JSVM_Value argv[1] = {param1};
234 JSVM_Value result;
235 status = OH_JSVM_CallFunction(env, gloablRecv, funcValue, DIFF_VALUE_NOE, argv, &result);
236 if (status != JSVM_OK) {
237 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction05:OH_JSVM_CallFunction status Failed.");
238 return nullptr;
239 }
240 int32_t rst = 0;
241 size_t tempNum = 2;
242 OH_JSVM_GetValueInt32(env, result, &rst);
243 if (rst != tempNum) {
244 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction05:call back result is Abnormal.");
245 return nullptr;
246 }
247
248 bool setValue = true;
249 JSVM_Value retValue = nullptr;
250 OH_JSVM_GetBoolean(env, setValue, &retValue);
251 return retValue;
252 }
253 //recv global,CreateFunction interface that requires one input parameter, argc 2, argv len 2, result not null
254 //return 0,result is consistent with the expected value of the function in CreateFunction
TestCallFunction06(JSVM_Env env,JSVM_CallbackInfo info)255 [[maybe_unused]] JSVM_Value TestCallFunction06(JSVM_Env env, JSVM_CallbackInfo info)
256 {
257 JSVM_Value result = nullptr;
258 JSVM_Value funcValue = nullptr;
259 JSVM_CallbackStruct param;
260 param.data = nullptr;
261 param.callback = valueAddOne;
262 JSVM_Status status = OH_JSVM_CreateFunction(env, "valueAddOne", NAPI_AUTO_LENGTH, ¶m, &funcValue);
263 if (status != JSVM_OK) {
264 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction06:OH_JSVM_CreateFunction status Failed.");
265 return nullptr;
266 }
267
268 JSVM_Value gloablRecv = nullptr;
269 OH_JSVM_GetGlobal(env, &gloablRecv);
270 JSVM_Value param1;
271 JSVM_Value param2;
272 OH_JSVM_CreateInt32(env, 0, ¶m1);
273 status = OH_JSVM_CreateInt32(env, 1, ¶m2);
274 if (status != JSVM_OK) {
275 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction06:OH_JSVM_CreateInt32 status Failed.");
276 return nullptr;
277 }
278 JSVM_Value argv[2] = {param1, param2};
279 status = OH_JSVM_CallFunction(env, gloablRecv, funcValue, DIFF_VALUE_TWO, argv, &result);
280 if (status != JSVM_OK) {
281 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction06:OH_JSVM_CallFunction status Failed.");
282 return nullptr;
283 }
284
285 int32_t rst = 0;
286 OH_JSVM_GetValueInt32(env, result, &rst);
287 if (rst != 1) {
288 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction05:call back result is Abnormal.");
289 return nullptr;
290 }
291
292 bool setValue = true;
293 JSVM_Value retValue = nullptr;
294 OH_JSVM_GetBoolean(env, setValue, &retValue);
295 return retValue;
296 }
297 //recv global,CreateFunction interface that requires two input parameters, argc 2,argv len 2,result not null
298 //return 0,The result cannot be obtained normally
EqualValueComparison(JSVM_Env env,JSVM_CallbackInfo info)299 [[maybe_unused]] JSVM_Value EqualValueComparison(JSVM_Env env, JSVM_CallbackInfo info)
300 {
301 size_t argc = 2;
302 JSVM_Value args[2];
303 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
304
305 bool isStrictEquals = false;
306 JSVM_Value retValue = nullptr;
307 OH_JSVM_StrictEquals(env, args[0], args[1], &isStrictEquals);
308 OH_JSVM_GetBoolean(env, isStrictEquals, &retValue);
309
310 return retValue;
311 }
312
TestCallFunction07(JSVM_Env env,JSVM_CallbackInfo info)313 [[maybe_unused]] JSVM_Value TestCallFunction07(JSVM_Env env, JSVM_CallbackInfo info)
314 {
315 JSVM_Value result = nullptr;
316 JSVM_Value funcValue = nullptr;
317 JSVM_CallbackStruct param;
318 param.data = nullptr;
319 param.callback = EqualValueComparison;
320 JSVM_Status status = OH_JSVM_CreateFunction(env, "EqualValueComparison", NAPI_AUTO_LENGTH, ¶m, &funcValue);
321 if (status != JSVM_OK) {
322 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction07:OH_JSVM_CreateFunction status Failed.");
323 return nullptr;
324 }
325
326 JSVM_Value gloablRecv = nullptr;
327 OH_JSVM_GetGlobal(env, &gloablRecv);
328 JSVM_Value param1;
329 JSVM_Value param2;
330 OH_JSVM_CreateStringUtf8(env, "a", JSVM_AUTO_LENGTH, ¶m1);
331 status = OH_JSVM_CreateStringUtf8(env, "b", JSVM_AUTO_LENGTH, ¶m2);
332 if (status != JSVM_OK) {
333 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction07:OH_JSVM_CreateStringUtf8 status Failed.");
334 return nullptr;
335 }
336 JSVM_Value argv[2] = {param1, param2};
337 status = OH_JSVM_CallFunction(env, gloablRecv, funcValue, DIFF_VALUE_TWO, argv, &result);
338 if (status != JSVM_OK) {
339 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction07:OH_JSVM_CallFunction status Failed.");
340 return nullptr;
341 }
342
343 bool rst = false;
344 OH_JSVM_GetValueBool(env, result, &rst);
345 if (rst != false) {
346 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction07:Call Back Function Failed.");
347 return nullptr;
348 }
349
350 bool setValue = true;
351 JSVM_Value retValue = nullptr;
352 OH_JSVM_GetBoolean(env, setValue, &retValue);
353 return retValue;
354 }
355 //recv global,CreateFunction interface that requires two input parameters, argc 1, argv len 1, result not null
356 //return 0,The result cannot be obtained normally
TestCallFunction08(JSVM_Env env,JSVM_CallbackInfo info)357 [[maybe_unused]] JSVM_Value TestCallFunction08(JSVM_Env env, JSVM_CallbackInfo info)
358 {
359 JSVM_Value result = nullptr;
360 JSVM_Value funcvalue = nullptr;
361 JSVM_CallbackStruct param;
362 param.data = nullptr;
363 param.callback = EqualValueComparison;
364 JSVM_Status status = OH_JSVM_CreateFunction(env, "EqualValueComparison", NAPI_AUTO_LENGTH, ¶m, &funcvalue);
365 if (status != JSVM_OK) {
366 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction08:OH_JSVM_CreateFunction status Failed.");
367 return nullptr;
368 }
369
370 JSVM_Value gloablrecv = nullptr;
371 status = OH_JSVM_GetGlobal(env, &gloablrecv);
372 if (status != JSVM_OK) {
373 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction08:OH_JSVM_GetGlobal status Failed.");
374 return nullptr;
375 }
376 JSVM_Value param1;
377 OH_JSVM_CreateStringUtf8(env, "test", JSVM_AUTO_LENGTH, ¶m1);
378 JSVM_Value argv[1] = {param1};
379 status = OH_JSVM_CallFunction(env, gloablrecv, funcvalue, DIFF_VALUE_NOE, argv, &result);
380 if (status != JSVM_OK) {
381 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction08:OH_JSVM_CallFunction status Failed.");
382 return nullptr;
383 }
384 if (!result) {
385 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction08:OH_JSVM_CallFunction result abnormal.");
386 return nullptr;
387 }
388
389 bool setValue = true;
390 JSVM_Value retValue = nullptr;
391 OH_JSVM_GetBoolean(env, setValue, &retValue);
392 return retValue;
393 }
394 //recv global,The function obtained from JS code that requires one input parameter,argc 1,argv len 1,result not null
395 //return 0,The result is consistent with the expected value of the function in CreateFunction
TestCallFunction09(JSVM_Env env,JSVM_CallbackInfo info)396 [[maybe_unused]] JSVM_Value TestCallFunction09(JSVM_Env env, JSVM_CallbackInfo info)
397 {
398 const char *jsFunction = R"JS(
399 function add(a, b) {
400 return a + b;
401 };
402 )JS";
403
404 // compile and run script
405 JSVM_Value runResult;
406 JSVM_Script script;
407 JSVM_Value jsFunctionStr;
408 JSVM_Status status = OH_JSVM_CreateStringUtf8(env, jsFunction, JSVM_AUTO_LENGTH, &jsFunctionStr);
409 if (status != JSVM_OK) {
410 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction09:OH_JSVM_CreateStringUtf8 status abnormal.");
411 return nullptr;
412 }
413 status = OH_JSVM_CompileScript(env, jsFunctionStr, nullptr, 0, false, nullptr, &script);
414 if (status != JSVM_OK) {
415 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction09:OH_JSVM_CompileScript status abnormal.");
416 return nullptr;
417 }
418 status = OH_JSVM_RunScript(env, script, &runResult);
419 if (status != JSVM_OK) {
420 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction09:OH_JSVM_RunScript status abnormal.");
421 return nullptr;
422 }
423 // get global
424 JSVM_Value global;
425 status = OH_JSVM_GetGlobal(env, &global);
426 if (status != JSVM_OK) {
427 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction09:OH_JSVM_RunScript status abnormal.");
428 return nullptr;
429 }
430 // get property
431 JSVM_Value add_func;
432 JSVM_Value add_func_str;
433 OH_JSVM_CreateStringUtf8(env, "add", JSVM_AUTO_LENGTH, &add_func_str);
434 status = OH_JSVM_GetProperty(env, global, add_func_str, &add_func);
435 if (status != JSVM_OK) {
436 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction09:OH_JSVM_GetProperty status abnormal.");
437 return nullptr;
438 }
439 // create param
440 size_t num1 = 3;
441 size_t num2 = 4;
442 size_t tempNum = 2;
443 JSVM_Value param1;
444 JSVM_Value param2;
445 OH_JSVM_CreateInt32(env, num1, ¶m1);
446 status = OH_JSVM_CreateInt32(env, num2, ¶m2);
447 if (status != JSVM_OK) {
448 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction09:OH_JSVM_CreateInt32 status abnormal.");
449 return nullptr;
450 }
451 JSVM_Value argv[] = {param1, param2};
452
453 // call function
454 JSVM_Value result;
455 status = OH_JSVM_CallFunction(env, global, add_func, tempNum, argv, &result);
456 if (status != JSVM_OK) {
457 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction09:OH_JSVM_CallFunction status abnormal.");
458 return nullptr;
459 }
460 // result check
461 int32_t addResult;
462 size_t len = 7;
463 OH_JSVM_GetValueInt32(env, result, &addResult);
464 if (addResult != len) {
465 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction09:OH_JSVM_CallFunction addResult abnormal.");
466 return nullptr;
467 }
468
469 bool setValue = true;
470 JSVM_Value retValue = nullptr;
471 OH_JSVM_GetBoolean(env, setValue, &retValue);
472 return retValue;
473 }
474 //OH_JSVM_CreateFunction:utf8name nullptr,length=0,set callback,data is null,result not null
475 //return 0,func name is nullptr
TestCreateFunction01(JSVM_Env env,JSVM_CallbackInfo info)476 [[maybe_unused]] JSVM_Value TestCreateFunction01(JSVM_Env env, JSVM_CallbackInfo info)
477 {
478 JSVM_Value funcvalue = nullptr;
479 JSVM_CallbackStruct param;
480 JSVM_Status status = OH_JSVM_CreateFunction(env, nullptr, 0, ¶m, &funcvalue);
481 if (status != JSVM_OK) {
482 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction01:OH_JSVM_CreateFunction status Failed.");
483 return nullptr;
484 }
485
486 bool setValue = true;
487 JSVM_Value retValue = nullptr;
488 OH_JSVM_GetBoolean(env, setValue, &retValue);
489 return retValue;
490 }
491 //utf8name not null,length>=utf8name,set callback,data null,result not null
492 //return 0,funcname len = utf8name len
TestCreateFunction02(JSVM_Env env,JSVM_CallbackInfo info)493 [[maybe_unused]] JSVM_Value TestCreateFunction02(JSVM_Env env, JSVM_CallbackInfo info)
494 {
495 size_t funcNameLen = 4;
496 const char funcname[] = "func";
497 JSVM_Value result = nullptr;
498 JSVM_CallbackStruct param;
499 JSVM_Status status = OH_JSVM_CreateFunction(env, funcname, funcNameLen + 1, ¶m, &result);
500 if (status != JSVM_OK) {
501 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction02:OH_JSVM_CreateFunction status Failed.");
502 return nullptr;
503 }
504 JSVM_Value key;
505 OH_JSVM_CreateStringUtf8(env, "name", JSVM_AUTO_LENGTH, &key);
506 JSVM_Value propResult = nullptr;
507 OH_JSVM_GetProperty(env, result, key, &propResult);
508
509 char strValue[10];
510 size_t size;
511 size_t bufferSize = 10;
512 OH_JSVM_GetValueStringUtf8(env, propResult, strValue, bufferSize, &size);
513 if (size != funcNameLen + 1) {
514 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction02:OH_JSVM_CreateFunction namelen != srcnamelen.");
515 return nullptr;
516 }
517
518 bool setValue = true;
519 JSVM_Value retValue = nullptr;
520 OH_JSVM_GetBoolean(env, setValue, &retValue);
521 return retValue;
522 }
523 //utf8name not null,length<utf8name,set callback,data null,result not null
524 //return 0,funcname len = length
TestCreateFunction03(JSVM_Env env,JSVM_CallbackInfo info)525 [[maybe_unused]] JSVM_Value TestCreateFunction03(JSVM_Env env, JSVM_CallbackInfo info)
526 {
527 size_t funcNameLen = 4;
528 const char funcname[] = "test";
529 JSVM_Value result = nullptr;
530 JSVM_CallbackStruct param;
531 JSVM_Status status = OH_JSVM_CreateFunction(env, funcname, strlen(funcname)-1, ¶m, &result);
532 if (status != JSVM_OK) {
533 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction03:OH_JSVM_CreateFunction status Failed.");
534 return nullptr;
535 }
536
537 JSVM_Value key;
538 status = OH_JSVM_CreateStringUtf8(env, "name", JSVM_AUTO_LENGTH, &key);
539 if (status != JSVM_OK) {
540 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction03:OH_JSVM_CreateFunction status Failed.");
541 return nullptr;
542 }
543 JSVM_Value propResult = nullptr;
544 OH_JSVM_GetProperty(env, result, key, &propResult);
545
546 char strValue[4];
547 size_t size;
548 size_t bufferSize = 4;
549 OH_JSVM_GetValueStringUtf8(env, propResult, strValue, bufferSize, &size);
550 if (size != funcNameLen - 1) {
551 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction03:OH_JSVM_CreateFunction namelen != srcnamelen.");
552 return nullptr;
553 }
554
555 bool setValue = true;
556 JSVM_Value retValue = nullptr;
557 OH_JSVM_GetBoolean(env, setValue, &retValue);
558 return retValue;
559 }
560 //utf8name not null,length>=utf8name,set callback,data not null, result not null
561 //return 0
TestCreateFunction04(JSVM_Env env,JSVM_CallbackInfo info)562 [[maybe_unused]] JSVM_Value TestCreateFunction04(JSVM_Env env, JSVM_CallbackInfo info)
563 {
564 const char funcname[] = "test";
565 JSVM_Value funcvalue = nullptr;
566 JSVM_CallbackStruct param;
567 param.data = (void *)"Hello";
568 param.callback = EqualValueComparison;
569
570 JSVM_Status status = OH_JSVM_CreateFunction(env, funcname, strlen(funcname), ¶m, &funcvalue);
571 if (status != JSVM_OK) {
572 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction04:OH_JSVM_CreateFunction status Failed.");
573 return nullptr;
574 }
575
576 bool setValue = true;
577 JSVM_Value retValue = nullptr;
578 OH_JSVM_GetBoolean(env, setValue, &retValue);
579 return retValue;
580 }
581 //utf8name not null,length>=utf8name,set callback,data not null,result nullptr
582 //return not 0
SayHello(JSVM_Env env,JSVM_CallbackInfo info)583 [[maybe_unused]] JSVM_Value SayHello(JSVM_Env env, JSVM_CallbackInfo info)
584 {
585 return nullptr;
586 }
587
TestCreateFunction05(JSVM_Env env,JSVM_CallbackInfo info)588 [[maybe_unused]] JSVM_Value TestCreateFunction05(JSVM_Env env, JSVM_CallbackInfo info)
589 {
590 const char funcname[] = "test";
591 JSVM_CallbackStruct param;
592 param.data = (void *)"Hello";
593 param.callback = SayHello;
594
595 JSVM_Status status = OH_JSVM_CreateFunction(env, funcname, strlen(funcname)+1, ¶m, nullptr);
596 if (status != JSVM_INVALID_ARG) {
597 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction04:OH_JSVM_CreateFunction status Failed.");
598 return nullptr;
599 }
600
601 bool setValue = true;
602 JSVM_Value retValue = nullptr;
603 OH_JSVM_GetBoolean(env, setValue, &retValue);
604 return retValue;
605 }
606 //utf8name not null,length=utf8name,set callback is nullptr,data null,result not null
607 //retrun 0,error
TestCreateFunction06(JSVM_Env env,JSVM_CallbackInfo info)608 [[maybe_unused]] JSVM_Value TestCreateFunction06(JSVM_Env env, JSVM_CallbackInfo info)
609 {
610 const char funcname[] = "test";
611 JSVM_CallbackStruct param;
612 param.callback = nullptr;
613 JSVM_Value funcvalue = nullptr;
614 JSVM_Status status = OH_JSVM_CreateFunction(env, funcname, strlen(funcname), ¶m, &funcvalue);
615 if (status != JSVM_OK) {
616 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction06:OH_JSVM_CreateFunction status Failed.");
617 return nullptr;
618 }
619
620 bool setValue = true;
621 JSVM_Value retValue = nullptr;
622 OH_JSVM_GetBoolean(env, setValue, &retValue);
623 return retValue;
624 }
625 //OH_JSVM_GetCbInfo:cbinfo is callback cbinfo,argc NULL,argv NULL,thisArg null,data not null
626 //return 0, returnval is consistent with the data passed to the callback function in the CreateFunction interface
Callbackdata(JSVM_Env env,JSVM_CallbackInfo info)627 [[maybe_unused]] JSVM_Value Callbackdata(JSVM_Env env, JSVM_CallbackInfo info)
628 {
629 void* data = nullptr;
630 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, nullptr, &data);
631 if (strcmp("say", (char *)data) != 0) {
632 OH_JSVM_ThrowError(env, nullptr, "Callbackdata:OH_JSVM_CreateFunction data is error.");
633 return nullptr;
634 }
635
636 bool setValue = true;
637 JSVM_Value retValue = nullptr;
638 OH_JSVM_GetBoolean(env, setValue, &retValue);
639 return retValue;
640 }
641
TestGetCbInfo01(JSVM_Env env,JSVM_CallbackInfo info)642 [[maybe_unused]] JSVM_Value TestGetCbInfo01(JSVM_Env env, JSVM_CallbackInfo info)
643 {
644 JSVM_CallbackStruct param;
645 param.callback = Callbackdata;
646 param.data = (void *)"say";
647 JSVM_Value funcvalue = nullptr;
648 JSVM_Status status = OH_JSVM_CreateFunction(env, "Hello", JSVM_AUTO_LENGTH, ¶m, &funcvalue);
649 if (status != JSVM_OK) {
650 OH_JSVM_ThrowError(env, nullptr, "TestGetCbInfo01:OH_JSVM_CreateFunction status Failed.");
651 return nullptr;
652 }
653
654 bool setValue = true;
655 JSVM_Value retValue = nullptr;
656 OH_JSVM_GetBoolean(env, setValue, &retValue);
657 return retValue;
658 }
659 //cbinfo is callback cbinfo, argc NULL,argv NULL, thisArg not null,data NULL
660 //return 0, The objects mounted by thisArg and Callfunction are consistent
CallbackThisVar(JSVM_Env env,JSVM_CallbackInfo info)661 [[maybe_unused]] JSVM_Value CallbackThisVar(JSVM_Env env, JSVM_CallbackInfo info)
662 {
663 JSVM_Value thisVar = nullptr;
664 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, &thisVar, nullptr);
665 if (thisVar == nullptr) {
666 OH_JSVM_ThrowError(env, nullptr, "CallbackThisVar:CallbackThisVar thisVar is nullptr abnormal.");
667 return nullptr;
668 }
669
670 bool setValue = true;
671 JSVM_Value retValue = nullptr;
672 OH_JSVM_GetBoolean(env, setValue, &retValue);
673 return retValue;
674 }
675
TestGetCbInfo02(JSVM_Env env,JSVM_CallbackInfo info)676 [[maybe_unused]] JSVM_Value TestGetCbInfo02(JSVM_Env env, JSVM_CallbackInfo info)
677 {
678 JSVM_CallbackStruct param;
679 param.callback = CallbackThisVar;
680 param.data = (void *)"say";
681 JSVM_Value funcvalue = nullptr;
682 JSVM_Status status = OH_JSVM_CreateFunction(env, "CallbackThisVar", JSVM_AUTO_LENGTH, ¶m, &funcvalue);
683 if (status != JSVM_OK) {
684 OH_JSVM_ThrowError(env, nullptr, "TestGetCbInfo02:OH_JSVM_CreateFunction status Failed.");
685 return nullptr;
686 }
687
688 JSVM_Value gloablrecv = nullptr;
689 status = OH_JSVM_GetGlobal(env, &gloablrecv);
690 if (status != JSVM_OK) {
691 OH_JSVM_ThrowError(env, nullptr, "TestGetCbInfo02:OH_JSVM_GetGlobal status Failed.");
692 return nullptr;
693 }
694 JSVM_Value param1;
695 status = OH_JSVM_CreateStringUtf8(env, "abc", JSVM_AUTO_LENGTH, ¶m1);
696 if (status != JSVM_OK) {
697 OH_JSVM_ThrowError(env, nullptr, "TestGetCbInfo02:OH_JSVM_CreateStringUtf8 status Failed.");
698 return nullptr;
699 }
700
701 JSVM_Value argv[1] = {param1};
702 JSVM_Value result;
703 status = OH_JSVM_CallFunction(env, gloablrecv, funcvalue, DIFF_VALUE_NOE, argv, &result);
704 if (status != JSVM_OK) {
705 OH_JSVM_ThrowError(env, nullptr, "TestGetCbInfo02:OH_JSVM_CallFunction status Failed.");
706 return nullptr;
707 }
708
709 bool setValue = true;
710 JSVM_Value retValue = nullptr;
711 OH_JSVM_GetBoolean(env, setValue, &retValue);
712 return retValue;
713 }
714 //cbinfo is callback cbinfo,argc not null,argv not null,argv len > argc set len,thisArg,data NULL
715 //return 0, argc elements the same as input,argv index>argc,result undefined
Callbackargc1(JSVM_Env env,JSVM_CallbackInfo info)716 [[maybe_unused]] JSVM_Value Callbackargc1(JSVM_Env env, JSVM_CallbackInfo info)
717 {
718 size_t argc = 1;
719 size_t testLen = 1;
720 JSVM_Value argv[5] = {nullptr};
721 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
722 uint32_t ret = 0;
723 OH_JSVM_GetValueUint32(env, argv[0], &ret);
724 size_t argvLen = 5;
725 if (ret != argvLen) {
726 OH_JSVM_ThrowError(env, nullptr, "Callbackargc1:Callbackargc args[0] error.");
727 return nullptr;
728 }
729 if (argc <= testLen) {
730 OH_JSVM_ThrowError(env, nullptr, "Callbackargc1:Callbackargc argc error.");
731 return nullptr;
732 }
733
734 return argv[0];
735 }
736
TestGetCbInfo03(JSVM_Env env,JSVM_CallbackInfo info)737 [[maybe_unused]] JSVM_Value TestGetCbInfo03(JSVM_Env env, JSVM_CallbackInfo info)
738 {
739 JSVM_CallbackStruct param;
740 param.callback = Callbackargc1;
741 param.data = (void *)"say";
742 JSVM_Value funcvalue = nullptr;
743 JSVM_Status status = OH_JSVM_CreateFunction(env, "Hello", JSVM_AUTO_LENGTH, ¶m, &funcvalue);
744 if (status != JSVM_OK) {
745 OH_JSVM_ThrowError(env, nullptr, "TestCreateFunction06:OH_JSVM_CreateFunction status Failed.");
746 return nullptr;
747 }
748
749 JSVM_Value gloablrecv = nullptr;
750 OH_JSVM_GetGlobal(env, &gloablrecv);
751 JSVM_Value jsIndex1;
752 JSVM_Value jsIndex2;
753 uint32_t index1 = 5;
754 uint32_t index2 = 10;
755 OH_JSVM_CreateUint32(env, index1, &jsIndex1);
756 status = OH_JSVM_CreateUint32(env, index2, &jsIndex2);
757 if (status != JSVM_OK) {
758 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction05:OH_JSVM_CreateUint32 status Failed.");
759 return nullptr;
760 }
761 JSVM_Value argv[2] = {jsIndex1, jsIndex2};
762 JSVM_Value result;
763 status = OH_JSVM_CallFunction(env, gloablrecv, funcvalue, DIFF_VALUE_TWO, argv, &result);
764 if (status != JSVM_OK) {
765 OH_JSVM_ThrowError(env, nullptr, "TestCallFunction05:OH_JSVM_CallFunction status Failed.");
766 return nullptr;
767 }
768
769 bool setValue = true;
770 JSVM_Value retValue = nullptr;
771 OH_JSVM_GetBoolean(env, setValue, &retValue);
772 return retValue;
773 }
774 //cbinfo is callback cbinfo,argc not null,argv not null,argv len > argc len,thisArg,data NULL
775 //return 0, argv index>argc,undefined
Callbackargc2(JSVM_Env env,JSVM_CallbackInfo info)776 [[maybe_unused]] JSVM_Value Callbackargc2(JSVM_Env env, JSVM_CallbackInfo info)
777 {
778 size_t argc = 2;
779 JSVM_Value argv[2] = {nullptr};
780 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
781 if (argc >= sizeof(argv)) {
782 OH_JSVM_ThrowError(env, nullptr, "Callbackargc2:Callbackargc argc >= argv index error.");
783 return nullptr;
784 }
785
786 return argv[0];
787 }
788
TestGetCbInfo04(JSVM_Env env,JSVM_CallbackInfo info)789 [[maybe_unused]] JSVM_Value TestGetCbInfo04(JSVM_Env env, JSVM_CallbackInfo info)
790 {
791 JSVM_CallbackStruct param;
792 param.callback = Callbackargc2;
793 param.data = (void *)"say";
794 JSVM_Value funcvalue = nullptr;
795 JSVM_Status status = OH_JSVM_CreateFunction(env, "Hello", JSVM_AUTO_LENGTH, ¶m, &funcvalue);
796 if (status != JSVM_OK) {
797 OH_JSVM_ThrowError(env, nullptr, "TestGetCbInfo04:OH_JSVM_CreateFunction status Failed.");
798 return nullptr;
799 }
800
801 JSVM_Value gloablrecv = nullptr;
802 OH_JSVM_GetGlobal(env, &gloablrecv);
803 JSVM_Value jsIndex1;
804 JSVM_Value jsIndex2;
805 uint32_t index1 = 128;
806 uint32_t index2 = 256;
807 OH_JSVM_CreateUint32(env, index1, &jsIndex1);
808 OH_JSVM_CreateUint32(env, index2, &jsIndex2);
809 JSVM_Value argv[2] = {jsIndex1, jsIndex2};
810 JSVM_Value result;
811 status = OH_JSVM_CallFunction(env, gloablrecv, funcvalue, 1, argv, &result);
812 if (status != JSVM_OK) {
813 OH_JSVM_ThrowError(env, nullptr, "TestGetCbInfo04:OH_JSVM_CallFunction status Failed.");
814 return nullptr;
815 }
816
817 bool setValue = true;
818 JSVM_Value retValue = nullptr;
819 OH_JSVM_GetBoolean(env, setValue, &retValue);
820 return retValue;
821 }
822 //cbinfo is callback cbinfo,argc not null,argv not null,argv len = argc,thisArg,data NULL
823 //return 0,argc = argv
Callbackargc3(JSVM_Env env,JSVM_CallbackInfo info)824 [[maybe_unused]] JSVM_Value Callbackargc3(JSVM_Env env, JSVM_CallbackInfo info)
825 {
826 size_t argc = 2;
827 JSVM_Value argv[2] = {nullptr};
828 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
829 uint32_t ret = 0;
830 OH_JSVM_GetValueUint32(env, argv[0], &ret);
831 size_t tempNum = 128;
832 if (ret != tempNum) {
833 OH_JSVM_ThrowError(env, nullptr, "Callbackargc3:Callbackargc args[0] error.");
834 return nullptr;
835 }
836
837 return argv[0];
838 }
839
TestGetCbInfo05(JSVM_Env env,JSVM_CallbackInfo info)840 [[maybe_unused]] JSVM_Value TestGetCbInfo05(JSVM_Env env, JSVM_CallbackInfo info)
841 {
842 JSVM_CallbackStruct param;
843 param.callback = Callbackargc3;
844 param.data = nullptr;
845 JSVM_Value funcvalue = nullptr;
846 JSVM_Status status = OH_JSVM_CreateFunction(env, "abc", JSVM_AUTO_LENGTH, ¶m, &funcvalue);
847 if (status != JSVM_OK) {
848 OH_JSVM_ThrowError(env, nullptr, "TestGetCbInfo05:OH_JSVM_CreateFunction status Failed.");
849 return nullptr;
850 }
851
852 JSVM_Value gloablrecv = nullptr;
853 OH_JSVM_GetGlobal(env, &gloablrecv);
854 JSVM_Value jsIndex1;
855 JSVM_Value jsIndex2;
856 uint32_t index1 = 128;
857 uint32_t index2 = 256;
858 OH_JSVM_CreateUint32(env, index1, &jsIndex1);
859 OH_JSVM_CreateUint32(env, index2, &jsIndex2);
860 JSVM_Value argv[2] = {jsIndex1, jsIndex2};
861 JSVM_Value result;
862 status = OH_JSVM_CallFunction(env, gloablrecv, funcvalue, DIFF_VALUE_TWO, argv, &result);
863 if (status != JSVM_OK) {
864 OH_JSVM_ThrowError(env, nullptr, "TestGetCbInfo05:OH_JSVM_CallFunction status Failed.");
865 return nullptr;
866 }
867
868 bool setValue = true;
869 JSVM_Value retValue = nullptr;
870 OH_JSVM_GetBoolean(env, setValue, &retValue);
871 return retValue;
872 }
873 //OH_JSVM_IsFunction:value is function, isFunction not null
874 //return 0,isFunction true
TestIsFunction01(JSVM_Env env,JSVM_CallbackInfo info)875 [[maybe_unused]] JSVM_Value TestIsFunction01(JSVM_Env env, JSVM_CallbackInfo info)
876 {
877 JSVM_CallbackStruct param;
878 JSVM_Value funcvalue = nullptr;
879 JSVM_Status status = OH_JSVM_CreateFunction(env, "Hello", JSVM_AUTO_LENGTH, ¶m, &funcvalue);
880 if (status != JSVM_OK) {
881 OH_JSVM_ThrowError(env, nullptr, "TestIsFunction01:OH_JSVM_CreateFunction status Failed.");
882 return nullptr;
883 }
884
885 bool isfun = false;
886 status = OH_JSVM_IsFunction(env, funcvalue, &isfun);
887 if (status != JSVM_OK) {
888 OH_JSVM_ThrowError(env, nullptr, "TestIsFunction01:OH_JSVM_IsFunction status Failed.");
889 return nullptr;
890 }
891
892 if (!isfun) {
893 OH_JSVM_ThrowError(env, nullptr, "TestIsFunction01:Create FuncValue is not Function.");
894 return nullptr;
895 }
896
897 bool setValue = true;
898 JSVM_Value retValue = nullptr;
899 OH_JSVM_GetBoolean(env, setValue, &retValue);
900 return retValue;
901 }
902 //value is not function, isFunction not null
903 //return 0,isFunction false
TestIsFunction02(JSVM_Env env,JSVM_CallbackInfo info)904 [[maybe_unused]] JSVM_Value TestIsFunction02(JSVM_Env env, JSVM_CallbackInfo info)
905 {
906 const char testStr[] = "foo";
907 JSVM_Value param = nullptr;
908 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), ¶m);
909
910 bool isfun = false;
911 JSVM_Status status = OH_JSVM_IsFunction(env, param, &isfun);
912 if (status != JSVM_OK) {
913 OH_JSVM_ThrowError(env, nullptr, "TestIsFunction02:OH_JSVM_IsFunction status Failed.");
914 return nullptr;
915 }
916
917 if (isfun) {
918 OH_JSVM_ThrowError(env, nullptr, "TestIsFunction02:Function type judgment error.");
919 return nullptr;
920 }
921
922 bool setValue = true;
923 JSVM_Value retValue = nullptr;
924 OH_JSVM_GetBoolean(env, setValue, &retValue);
925 return retValue;
926 }
927 //value nullptr, isFunction not null
928 //return not 0
TestIsFunction03(JSVM_Env env,JSVM_CallbackInfo info)929 [[maybe_unused]] JSVM_Value TestIsFunction03(JSVM_Env env, JSVM_CallbackInfo info)
930 {
931 const char testStr[] = "foo";
932 JSVM_Value param = nullptr;
933 OH_JSVM_CreateStringUtf8(env, testStr, strlen(testStr), ¶m);
934
935 bool isfun = false;
936 JSVM_Status status = OH_JSVM_IsFunction(env, nullptr, &isfun);
937 if (status != JSVM_INVALID_ARG) {
938 OH_JSVM_ThrowError(env, nullptr, "TestIsFunction03:OH_JSVM_IsFunction status Failed.");
939 return nullptr;
940 }
941
942 if (isfun) {
943 OH_JSVM_ThrowError(env, nullptr, "TestIsFunction03:Function type judgment error.");
944 return nullptr;
945 }
946
947 bool setValue = true;
948 JSVM_Value retValue = nullptr;
949 OH_JSVM_GetBoolean(env, setValue, &retValue);
950 return retValue;
951 }
952 //value function, isFunction nullptr
953 //return not 0
TestIsFunction04(JSVM_Env env,JSVM_CallbackInfo info)954 [[maybe_unused]] JSVM_Value TestIsFunction04(JSVM_Env env, JSVM_CallbackInfo info)
955 {
956 JSVM_CallbackStruct param;
957 JSVM_Value funcvalue = nullptr;
958 JSVM_Status status = OH_JSVM_CreateFunction(env, "test", JSVM_AUTO_LENGTH, ¶m, &funcvalue);
959 if (status != JSVM_OK) {
960 OH_JSVM_ThrowError(env, nullptr, "TestIsFunction04:OH_JSVM_CreateFunction status Failed.");
961 return nullptr;
962 }
963
964 status = OH_JSVM_IsFunction(env, funcvalue, nullptr);
965 if (status == JSVM_OK) {
966 OH_JSVM_ThrowError(env, nullptr, "TestIsFunction04:OH_JSVM_IsFunction status Failed.");
967 return nullptr;
968 }
969
970 bool setValue = true;
971 JSVM_Value retValue = nullptr;
972 OH_JSVM_GetBoolean(env, setValue, &retValue);
973 return retValue;
974 }