• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Creating and Calling Functions Using Node-API
2
3## Introduction
4
5Node-API provides APIs for calling ArkTS functions and passing parameters or creating ArkTS methods in C/C++.
6
7## Basic Concepts
8
9Functions are blocks of reusable code that performs specific tasks or operations. You can define functions to implement different operations. Functions provide a way to modularize and structure code, helping make your code more organized, reusable, and maintainable.
10
11## Available APIs
12
13| API| Description|
14| -------- | -------- |
15| napi_get_cb_info | Obtains detailed information about the call, such as the parameters and **this** pointer, from the given callback information.|
16| napi_call_function | Calls an ArkTS function from a C/C++ addon.|
17| napi_create_function | Creates an ArkTS function based on a C/C++ function.|
18
19## Example
20
21If you are just starting out with Node-API, see [Node-API Development Process](use-napi-process.md). The following demonstrates only the C++ and ArkTS code related to function invocation. The **napi_create_function** API is different. For details, see the example.
22
23## napi_get_cb_info
24
25Use **napi_get_cb_info** to obtain detailed information about function calls.
26
27CPP code:
28
29```cpp
30#include "napi/native_api.h"
31// Obtain information about the parameters passed from ArkTS.
32static napi_value GetCbArgs(napi_env env, napi_callback_info info)
33{
34    size_t argc = 1;
35    napi_value args[1] = {nullptr};
36    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
37    return args[0];
38}
39// Obtain the number of parameters passed from ArkTS.
40static napi_value GetCbArgQuantity(napi_env env, napi_callback_info info)
41{
42    size_t argc = 0;
43    napi_value result = nullptr;
44    napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
45    napi_create_int32(env, argc, &result);
46    return result;
47}
48// Obtain thisArg passed from ArkTS.
49static napi_value GetCbContext(napi_env env, napi_callback_info info)
50{
51    napi_value thisArg = nullptr;
52    napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr);
53    return thisArg;
54}
55```
56
57API declaration:
58
59```ts
60// index.d.ts
61export const getCbArgs: <T>(arg: T) => T;
62// The input parameters of getCbArgQuantity are customized. In this example, two input parameters are used: string and number.
63export const getCbArgQuantity: (str: string, num: number) => number;
64export const getCbContext: () => Object;
65```
66
67ArkTS code:
68
69```ts
70import hilog from '@ohos.hilog';
71import testNapi from 'libentry.so';
72function summation(arr: Array<number>) {
73  let sum: number = 0;
74  for (let i = 0; i < arr.length; i++) {
75    sum += arr[i];
76  }
77  return sum;
78}
79const str = 'message';
80const arr = [0, 1, 2, 3, 4, 5];
81const num = 526;
82class Student {
83  name: string;
84  age: number;
85  score: number;
86  constructor(name: string, age: number, score: number) {
87    this.name = name;
88    this.age = age;
89    this.score = score;
90  }
91}
92let student = new Student('Alice', 18, 100);
93// Obtain parameters.
94hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get string arg:%{public}s', testNapi.getCbArgs(str));
95hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get array arg:%{public}s ', testNapi.getCbArgs(arr).toString());
96hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get num arg:%{public}d ', testNapi.getCbArgs(num));
97hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get undefined arg:%{public}s ', testNapi.getCbArgs(undefined));
98hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get object arg:%{public}s ', JSON.stringify(testNapi.getCbArgs(student)));
99hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get function arg:%{public}d ', testNapi.getCbArgs(summation(arr)));
100// Obtain the number of parameters.
101hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get arg quantity:%{public}d ', testNapi.getCbArgQuantity(str, num));
102// Obtain the context.
103hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_cb_info get thisArg:%{public}s ', testNapi.getCbContext().toString());
104```
105
106## napi_call_function
107
108Use **napi_call_function** to call an ArkTS function from a C/C++ addon.
109>**NOTE**<br>The length of **argv** passed by **napi_call_function** must be greater than or equal to the value of **argc** and must be initialized to **nullptr**.
110
111CPP code:
112
113```cpp
114#include "napi/native_api.h"
115
116static napi_value CallFunction(napi_env env, napi_callback_info info)
117{
118    size_t argc = 1;
119    napi_value argv[1] = {nullptr};
120    // Obtain the parameters passed from ArkTS.
121    napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
122    // Obtain the global object. Here, global is used because the second parameter of napi_call_function is the this parameter of the ArkTS function.
123    napi_value global = nullptr;
124    napi_get_global(env, &global);
125    // Call the ArkTS method.
126    napi_value result = nullptr;
127    // The length of argv passed by napi_call_function must be greater than or equal to the value of argc and must be initialized to nullptr.
128    napi_call_function(env, global, argv[0], argc, argv, &result);
129    return result;
130}
131
132static napi_value ObjCallFunction(napi_env env, napi_callback_info info)
133{
134    // Obtain the two parameters passed from ArkTS.
135    size_t argc = 2;
136    napi_value argv[2] = {nullptr};
137    // Obtain the parameters passed from ArkTS.
138    napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
139    // Call the ArkTS method.
140    napi_value result = nullptr;
141    // The length of argv passed by napi_call_function must be greater than or equal to the value of argc and must be initialized to nullptr.
142    napi_call_function(env, argv[0], argv[1], argc, argv, &result);
143    return result;
144}
145```
146
147API declaration:
148
149```ts
150// index.d.ts
151export const callFunction: (func: Function) => number;
152export const objCallFunction: (obj: Object, func: Function) => number;
153```
154
155ArkTS code:
156
157```ts
158import hilog from '@ohos.hilog';
159import testNapi from 'libentry.so';
160
161function returnNumber() {
162  return 10;
163}
164class Person {
165  age(): number {
166    return 11;
167  }
168}
169const person = new Person();
170hilog.info(0x0000, 'testTag', 'Test Node-API call_function:%{public}d', testNapi.callFunction(returnNumber));
171hilog.info(0x0000, 'testTag', 'Test Node-API call_function:%{public}d', testNapi.objCallFunction(person,person.age));
172```
173
174## napi_create_function
175
176Use **napi_create_function** to create an ArkTS function object in native code, which allows calling into the native code from ArkTS. This API returns a **napi_value** that represents the function created.
177
178CPP code:
179
180```cpp
181#include "napi/native_api.h"
182
183static napi_value CalculateArea(napi_env env, napi_callback_info info)
184{
185    // Obtain the two parameters passed from ArkTS.
186    size_t argc = 2;
187    napi_value args[2] = {nullptr};
188    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
189    double width = 0;
190    napi_get_value_double(env, args[0], &width);
191    double height = 0;
192    napi_get_value_double(env, args[1], &height);
193    napi_value area = nullptr;
194    napi_create_double(env, width * height, &area);
195    return area;
196}
197
198EXTERN_C_START
199static napi_value Init(napi_env env, napi_value exports) {
200    napi_value fn = nullptr;
201    napi_create_function(env, nullptr, 0, CalculateArea, nullptr, &fn);
202    napi_set_named_property(env, exports, "calculateArea", fn);
203    return exports;
204}
205EXTERN_C_END
206```
207
208API declaration:
209
210```ts
211// index.d.ts
212export const calculateArea: (width: number, height: number) => number;
213```
214
215ArkTS code:
216
217```ts
218import hilog from '@ohos.hilog';
219import testNapi from 'libentry.so';
220
221hilog.info(0x0000, 'testTag', 'Test Node-API create_function:%{public}d ', testNapi.calculateArea(1.2, 4));
222```
223
224To print logs in the native CPP, add the following information to the **CMakeLists.txt** file and add the header file by using **#include "hilog/log.h"**.
225
226```text
227// CMakeLists.txt
228add_definitions( "-DLOG_DOMAIN=0xd0d0" )
229add_definitions( "-DLOG_TAG=\"testTag\"" )
230target_link_libraries(entry PUBLIC libhilog_ndk.z.so)
231```
232