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 "cJsonNapiH/cjsonnapi.h"
17
getCreateStringNextOut(napi_env env,napi_value cJSON_CreateStringOut)18 napi_value getCreateStringNextOut(napi_env env, napi_value cJSON_CreateStringOut)
19 {
20 napi_status status;
21 const napi_extended_error_info *extended_error_info;
22 const char *tag = "[KH361_cJSON_CreateObject]";
23 napi_value nextOut;
24 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
25 * env: 当前环境的句柄
26 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
27 */
28 status = napi_create_object(env, &nextOut);
29 if (status != napi_ok) {
30 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
31 return nullptr;
32 }
33 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
34 * env: 当前环境的句柄
35 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
36 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
37 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
38 */
39 status = napi_set_named_property(env, cJSON_CreateStringOut, "next", nextOut);
40 if (status != napi_ok) {
41 /* [NAPI_GEN]: 错误处理*/
42 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
43 return nullptr;
44 }
45 return cJSON_CreateStringOut;
46 }
47
getCreateStringPrevOut(napi_env env,napi_value cJSON_CreateStringOut)48 napi_value getCreateStringPrevOut(napi_env env, napi_value cJSON_CreateStringOut)
49 {
50 napi_status status;
51 const napi_extended_error_info *extended_error_info;
52 const char *tag = "[KH361_cJSON_CreateObject]";
53 napi_value prevOut;
54 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
55 * env: 当前环境的句柄
56 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
57 */
58 status = napi_create_object(env, &prevOut);
59 if (status != napi_ok) {
60 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
61 return nullptr;
62 }
63 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
64 * env: 当前环境的句柄
65 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
66 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
67 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
68 */
69 status = napi_set_named_property(env, cJSON_CreateStringOut, "prev", prevOut);
70 if (status != napi_ok) {
71 /* [NAPI_GEN]: 错误处理*/
72 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
73 return nullptr;
74 }
75 return cJSON_CreateStringOut;
76 }
77
getCreateStringChildOut(napi_env env,napi_value cJSON_CreateStringOut)78 napi_value getCreateStringChildOut(napi_env env, napi_value cJSON_CreateStringOut)
79 {
80 napi_status status;
81 const napi_extended_error_info *extended_error_info;
82 const char *tag = "[KH361_cJSON_CreateObject]";
83 napi_value childOut;
84 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
85 * env: 当前环境的句柄
86 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
87 */
88 status = napi_create_object(env, &childOut);
89 if (status != napi_ok) {
90 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
91 return nullptr;
92 }
93 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
94 * env: 当前环境的句柄
95 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
96 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
97 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
98 */
99 status = napi_set_named_property(env, cJSON_CreateStringOut, "child", childOut);
100 if (status != napi_ok) {
101 /* [NAPI_GEN]: 错误处理*/
102 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
103 return nullptr;
104 }
105 return cJSON_CreateStringOut;
106 }
107
getCreateStringTypeOut(napi_env env,cJSON * jsonObject,napi_value cJSON_CreateStringOut)108 napi_value getCreateStringTypeOut(napi_env env, cJSON *jsonObject, napi_value cJSON_CreateStringOut)
109 {
110 napi_status status;
111 const napi_extended_error_info *extended_error_info;
112 const char *tag = "[KH361_cJSON_CreateObject]";
113 napi_value typeOut;
114 /* [NAPI_GEN]: 返回值是int32_t类型时,napi_create_int32 创建一个包含32位整数(int32_t)的js数值(Number)对象
115 * env: 当前环境的句柄
116 * value: 要准换成js数值的int32_t的值,这里以传入1为例,用例新增业务代码时可根据自身需求修改
117 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
118 */
119 status = napi_create_int32(env, jsonObject->type, &typeOut);
120 if (status != napi_ok) {
121 getErrMessage(status, env, extended_error_info, "napi_create_int32", tag);
122 return nullptr;
123 }
124 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
125 * env: 当前环境的句柄
126 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
127 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
128 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
129 */
130 status = napi_set_named_property(env, cJSON_CreateStringOut, "type", typeOut);
131 if (status != napi_ok) {
132 /* [NAPI_GEN]: 错误处理*/
133 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
134 return nullptr;
135 }
136 return cJSON_CreateStringOut;
137 }
138
getCreateStringValuestringOut(napi_env env,cJSON * jsonObject,napi_value cJSON_CreateStringOut)139 napi_value getCreateStringValuestringOut(napi_env env, cJSON *jsonObject, napi_value cJSON_CreateStringOut)
140 {
141 napi_status status;
142 const napi_extended_error_info *extended_error_info;
143 const char *tag = "[KH361_cJSON_CreateObject]";
144 napi_value valuestringOut;
145 /* [NAPI_GEN]:
146 * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
147 * env: 当前环境的句柄
148 * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以valuestring举例,用户可根据需求修改
149 * length:
150 * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
151 * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
152 */
153 status = napi_create_string_utf8(env, jsonObject->valuestring, NAPI_AUTO_LENGTH, &valuestringOut);
154 if (status != napi_ok) {
155 /*错误处理*/
156 getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
157 return nullptr;
158 }
159 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
160 * env: 当前环境的句柄
161 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
162 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
163 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
164 */
165 status = napi_set_named_property(env, cJSON_CreateStringOut, "valuestring", valuestringOut);
166 if (status != napi_ok) {
167 /* [NAPI_GEN]: 错误处理*/
168 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
169 return nullptr;
170 }
171 return cJSON_CreateStringOut;
172 }
173
getCreateStringValueintOut(napi_env env,cJSON * jsonObject,napi_value cJSON_CreateStringOut)174 napi_value getCreateStringValueintOut(napi_env env, cJSON *jsonObject, napi_value cJSON_CreateStringOut)
175 {
176 napi_status status;
177 const napi_extended_error_info *extended_error_info;
178 const char *tag = "[KH361_cJSON_CreateObject]";
179 napi_value valueintOut;
180 /* [NAPI_GEN]: 返回值是int32_t类型时,napi_create_int32 创建一个包含32位整数(int32_t)的js数值(Number)对象
181 * env: 当前环境的句柄
182 * value: 要准换成js数值的int32_t的值,这里以传入1为例,用例新增业务代码时可根据自身需求修改
183 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
184 */
185 status = napi_create_int32(env, jsonObject->valueint, &valueintOut);
186 if (status != napi_ok) {
187 getErrMessage(status, env, extended_error_info, "napi_create_int32", tag);
188 return nullptr;
189 }
190 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
191 * env: 当前环境的句柄
192 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
193 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
194 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
195 */
196 status = napi_set_named_property(env, cJSON_CreateStringOut, "valueint", valueintOut);
197 if (status != napi_ok) {
198 /* [NAPI_GEN]: 错误处理*/
199 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
200 return nullptr;
201 }
202 return cJSON_CreateStringOut;
203 }
204
getCreateStringValuedoubleOut(napi_env env,cJSON * jsonObject,napi_value cJSON_CreateStringOut)205 napi_value getCreateStringValuedoubleOut(napi_env env, cJSON *jsonObject, napi_value cJSON_CreateStringOut)
206 {
207 napi_status status;
208 const napi_extended_error_info *extended_error_info;
209 const char *tag = "[KH361_cJSON_CreateObject]";
210 napi_value valuedoubleOut;
211 /* [NAPI_GEN]: 返回值是double类型时,napi_create_double 创建一个包含双精度浮点数的js数值(Number)对象
212 * env: 当前环境的句柄
213 * value: 要传递给js的双精度浮点数值,这里以传入1.0为例,用例新增业务代码时可根据自身需求修改
214 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
215 */
216 status = napi_create_double(env, jsonObject->valuedouble, &valuedoubleOut);
217 if (status != napi_ok) {
218 getErrMessage(status, env, extended_error_info, "napi_create_double", tag);
219 return nullptr;
220 }
221 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
222 * env: 当前环境的句柄
223 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
224 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
225 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
226 */
227 status = napi_set_named_property(env, cJSON_CreateStringOut, "valuedouble", valuedoubleOut);
228 if (status != napi_ok) {
229 /* [NAPI_GEN]: 错误处理*/
230 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
231 return nullptr;
232 }
233 return cJSON_CreateStringOut;
234 }
235
getCreateStringStringOut(napi_env env,napi_value cJSON_CreateStringOut)236 napi_value getCreateStringStringOut(napi_env env, napi_value cJSON_CreateStringOut)
237 {
238 napi_status status;
239 const napi_extended_error_info *extended_error_info;
240 const char *tag = "[KH361_cJSON_CreateObject]";
241 napi_value stringOut;
242 /* [NAPI_GEN]:
243 * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
244 * env: 当前环境的句柄
245 * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以string举例,用户可根据需求修改
246 * length:
247 * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
248 * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
249 */
250 status = napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &stringOut);
251 if (status != napi_ok) {
252 /*错误处理*/
253 getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
254 return nullptr;
255 }
256 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
257 * env: 当前环境的句柄
258 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
259 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
260 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
261 */
262 status = napi_set_named_property(env, cJSON_CreateStringOut, "string", stringOut);
263 if (status != napi_ok) {
264 /* [NAPI_GEN]: 错误处理*/
265 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
266 return nullptr;
267 }
268 return cJSON_CreateStringOut;
269 }
270
getCreateStringParamIn(napi_env env,napi_value paramIn)271 char *getCreateStringParamIn(napi_env env, napi_value paramIn)
272 {
273 napi_status status;
274 const napi_extended_error_info *extended_error_info;
275 const char *tag = "[KH361_cJSON_CreateObject]";
276 /* [NAPI_GEN]: 从args数组中获取入参 */
277 napi_valuetype valuetypestring;
278 /* [NAPI_GEN]: 获取入参类型,第0个入参
279 * env: N-API环境的句柄,表示当前的上下文
280 * value: 要检查类型的js值
281 * result: 是一个指针,指向napi_valuetype枚举的值,函数会将结果存储在这里
282 */
283 status = napi_typeof(env, paramIn, &valuetypestring);
284 if (status != napi_ok) {
285 getErrMessage(status, env, extended_error_info, "napi_typeof", tag);
286 return nullptr;
287 }
288 size_t strSize0 = 0;
289 /* [NAPI_GEN]: napi_get_value_string_utf8用于将Js字符串转换为UTF-8编码的C字符串
290 * env: N-API环境的句柄,表示当前的上下文
291 * value: 要转换的JavaScript字符串
292 * buf: 用于存储结果的字符数组的指针
293 * bufsize: 缓冲区大小,以字节为单位
294 * result: 转换后的字符串的字节长度(不包括空终止符)。若干buf是NULL,则返回所需的缓冲区大小(包括空终止符)
295 */
296 /* [NAPI_GEN]: buf参数是NULL时,用于获取所需缓冲区大小*/
297 status = napi_get_value_string_utf8(env, paramIn, NULL, 0, &strSize0);
298 if (status != napi_ok) {
299 getErrMessage(status, env, extended_error_info, "get value string", tag);
300 return nullptr;
301 }
302 char *stringIn = new char[strSize0 + 1];
303 /* [NAPI_GEN]: 用于获取字符串*/
304 status = napi_get_value_string_utf8(env, paramIn, stringIn, strSize0 + 1, &strSize0);
305 if (status != napi_ok) {
306 getErrMessage(status, env, extended_error_info, "get value string", tag);
307 delete[] stringIn;
308 return nullptr;
309 }
310 return stringIn;
311 }
312
313 /* [NAPI_GEN]:对应cJSON.h中: CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);的napi方法,
314 * 输入一个字符串
315 * 输出根据字符串创建的cJSON对象
316 */
KH515_cJSON_CreateString(napi_env env,napi_callback_info info)317 napi_value KH515_cJSON_CreateString(napi_env env, napi_callback_info info)
318 {
319 napi_status status;
320 /* [NAPI_GEN]: Node.js在其N-API中用来提供错误的扩展信息的结构体,结构体包含以下字段
321 * error_message: 一个指向错误详细字符串的指针,提供了关于错误的文本描述
322 * engin_reserved: 一个保留给Js引擎使用的指针
323 * error_code: 错误码,指示了错误的种类,比如napi_pending_exception表示有一个JavaScript异常未被清理。
324 * engine_error_code:一个引擎特定的错误码,为引擎实现保留,具体含义依赖于使用的JavaScript引擎。
325 * error_message_len:错误消息字符串的长度。
326 */
327 const napi_extended_error_info *extended_error_info;
328 /* [NAPI_GEN]: tag: 日志打印标签*/
329 const char *tag = "[KH515_cJSON_CreateString]";
330 /* [NAPI_GEN]: get function param in*/
331 /* [NAPI_GEN]: argc:js传入的参数个数 */
332 size_t argc = PARAMS1;
333 /* [NAPI_GEN]: args: 一个数组,保存js传入的参数 */
334 napi_value args[PARAMS1] = {nullptr};
335 /* [NAPI_GEN]: napi_get_cb_info用于获取JS调用该函数时所传递的参数、接收参数的个数以及'this'的值
336 * env: 当前环境的句柄,代表当前的Node.js环境
337 * info: 回调信息句柄,代表当前回调的上下文
338 * argc: 指向size_t的指针,最初应包含可接受的最大参数数量,函数返回时,它将包含实际传递的参数数量
339 * args: 一个足够大的数组,用于接收传递给回调函数的所有js参数。数组的大小应至少与argc传入的值一样大。
340 * this_arg: 如果不是NULL,则返回js回调中this的值
341 * data: 如果不是NULL,则返回与回调函数关联的任何可选数据。通常用于传递在创建函数时指定的静态数据
342 */
343 status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
344 if (status != napi_ok) {
345 /* [NAPI_GEN]: 错误处理*/
346 getErrMessage(status, env, extended_error_info, "napi_get_cb_info", tag);
347 return nullptr;
348 }
349 napi_value paramIn = args[0];
350 char *stringIn = getCreateStringParamIn(env, paramIn);
351 // Todo: add business logic. 在这前后代码为框架所生成
352 cJSON *jsonObject = cJSON_CreateString(stringIn);
353 delete[] stringIn;
354 cJSON_Delete(jsonObject);
355 /* [NAPI_GEN]: function return value*/
356 napi_value cJSON_CreateStringOut;
357 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
358 * env: 当前环境的句柄
359 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
360 */
361 status = napi_create_object(env, &cJSON_CreateStringOut);
362 if (status != napi_ok) {
363 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
364 return nullptr;
365 }
366 cJSON_CreateStringOut = getCreateStringNextOut(env, cJSON_CreateStringOut);
367 cJSON_CreateStringOut = getCreateStringPrevOut(env, cJSON_CreateStringOut);
368 cJSON_CreateStringOut = getCreateStringChildOut(env, cJSON_CreateStringOut);
369 cJSON_CreateStringOut = getCreateStringTypeOut(env, jsonObject, cJSON_CreateStringOut);
370 cJSON_CreateStringOut = getCreateStringValuestringOut(env, jsonObject, cJSON_CreateStringOut);
371 cJSON_CreateStringOut = getCreateStringValueintOut(env, jsonObject, cJSON_CreateStringOut);
372 cJSON_CreateStringOut = getCreateStringValuedoubleOut(env, jsonObject, cJSON_CreateStringOut);
373 cJSON_CreateStringOut = getCreateStringStringOut(env, cJSON_CreateStringOut);
374
375 return cJSON_CreateStringOut;
376 }
377