• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Shenzhen Kaihong Digital Industry 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 "common.h"
17 #include "javascriptapi.h"
18 
19 static const char *TAG = "[javascriptapi_function]";
20 
testNapiCallFunction(napi_env env,napi_callback_info info)21 napi_value testNapiCallFunction(napi_env env, napi_callback_info info)
22 {
23     // pages/javascript/jsfunctions/napicallfunction
24     // 获取参数数量
25     size_t argc = PARAM2;
26     // 准备接收参数的变量
27     napi_value argv[PARAM2];
28     napi_value func;
29     napi_value result;
30     napi_status status;
31     const napi_extended_error_info *extended_error_info;
32 
33     // 获取回调函数的参数信息
34     status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
35     if (status != napi_ok) {
36         getErrMsg(status, env, extended_error_info, "Failed to get callback info", TAG);
37         return NULL;
38     }
39 
40     // 检查参数数量是否符合预期
41     if (argc != PARAM2) {
42         napi_throw_error(env, NULL, "Expected exactly one argument");
43         return NULL;
44     }
45 
46     // 检查传入参数是否为function
47     napi_valuetype resultType;
48     napi_typeof(env, argv[0], &resultType);
49     if (resultType != napi_function) {
50         napi_throw_error(env, NULL, "The incoming parameter is not a function");
51         return NULL;
52     }
53 
54     func = argv[PARAM0];
55     status = napi_call_function(env, NULL, func, PARAM1, &argv[PARAM1], &result);
56     if (status != napi_ok) {
57         getErrMsg(status, env, extended_error_info, "call function", TAG);
58         return NULL;
59     }
60 
61     return result;
62 }
63 
64