• 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 "cJsonNapiH/cjsonnapi.h"
17 
getCreateArrayTypeOut(napi_env env,napi_value cJSON_CreateArrayOut,cJSON * jsonArray)18 napi_value getCreateArrayTypeOut(napi_env env, napi_value cJSON_CreateArrayOut, cJSON *jsonArray)
19 {
20     napi_status status;
21     const napi_extended_error_info *extended_error_info;
22     const char *tag = "[KH386_cJSON_CreateArray]";
23     napi_value typeOut;
24     /* [NAPI_GEN]: 返回值是int32_t类型时,napi_create_int32 创建一个包含32位整数(int32_t)的js数值(Number)对象
25      * env: 当前环境的句柄
26      * value: 要准换成js数值的int32_t的值,这里以传入1为例,用例新增业务代码时可根据自身需求修改
27      * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
28      */
29     status = napi_create_int32(env, jsonArray->type, &typeOut);
30     if (status != napi_ok) {
31         getErrMessage(status, env, extended_error_info, "napi_create_int32", tag);
32         return nullptr;
33     }
34     /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
35      * env: 当前环境的句柄
36      * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
37      * utf8name: 属性的名称,是一个以UTF-8编码的字符串
38      * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
39      */
40     status = napi_set_named_property(env, cJSON_CreateArrayOut, "type", typeOut);
41     if (status != napi_ok) {
42         /* [NAPI_GEN]: 错误处理*/
43         getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
44         return nullptr;
45     }
46     return cJSON_CreateArrayOut;
47 }
48 
getCreateArrayValueintOut(napi_env env,napi_value cJSON_CreateArrayOut,cJSON * jsonArray)49 napi_value getCreateArrayValueintOut(napi_env env, napi_value cJSON_CreateArrayOut, cJSON *jsonArray)
50 {
51     napi_status status;
52     const napi_extended_error_info *extended_error_info;
53     const char *tag = "[KH386_cJSON_CreateArray]";
54     napi_value valueintOut;
55     /* [NAPI_GEN]: 返回值是int32_t类型时,napi_create_int32 创建一个包含32位整数(int32_t)的js数值(Number)对象
56      * env: 当前环境的句柄
57      * value: 要准换成js数值的int32_t的值,这里以传入1为例,用例新增业务代码时可根据自身需求修改
58      * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
59      */
60     status = napi_create_int32(env, jsonArray->valueint, &valueintOut);
61     if (status != napi_ok) {
62         getErrMessage(status, env, extended_error_info, "napi_create_int32", tag);
63         return nullptr;
64     }
65     /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
66      * env: 当前环境的句柄
67      * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
68      * utf8name: 属性的名称,是一个以UTF-8编码的字符串
69      * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
70      */
71     status = napi_set_named_property(env, cJSON_CreateArrayOut, "valueint", valueintOut);
72     if (status != napi_ok) {
73         /* [NAPI_GEN]: 错误处理*/
74         getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
75         return nullptr;
76     }
77     return cJSON_CreateArrayOut;
78 }
79 
getCreateArrayValuedoubleOut(napi_env env,napi_value cJSON_CreateArrayOut,cJSON * jsonArray)80 napi_value getCreateArrayValuedoubleOut(napi_env env, napi_value cJSON_CreateArrayOut, cJSON *jsonArray)
81 {
82     napi_status status;
83     const napi_extended_error_info *extended_error_info;
84     const char *tag = "[KH386_cJSON_CreateArray]";
85     napi_value valuedoubleOut;
86     /* [NAPI_GEN]: 返回值是double类型时,napi_create_double 创建一个包含双精度浮点数的js数值(Number)对象
87      * env: 当前环境的句柄
88      * value: 要传递给js的双精度浮点数值,这里以传入1.0为例,用例新增业务代码时可根据自身需求修改
89      * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
90      */
91     status = napi_create_double(env, jsonArray->valuedouble, &valuedoubleOut);
92     if (status != napi_ok) {
93         getErrMessage(status, env, extended_error_info, "napi_create_double", tag);
94         return nullptr;
95     }
96     /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
97      * env: 当前环境的句柄
98      * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
99      * utf8name: 属性的名称,是一个以UTF-8编码的字符串
100      * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
101      */
102     status = napi_set_named_property(env, cJSON_CreateArrayOut, "valuedouble", valuedoubleOut);
103     if (status != napi_ok) {
104         /* [NAPI_GEN]: 错误处理*/
105         getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
106         return nullptr;
107     }
108     return cJSON_CreateArrayOut;
109 }
110 
getCreateArrayValuestringOut(napi_env env,napi_value cJSON_CreateArrayOut,cJSON * jsonArray)111 napi_value getCreateArrayValuestringOut(napi_env env, napi_value cJSON_CreateArrayOut, cJSON *jsonArray)
112 {
113     napi_status status;
114     const napi_extended_error_info *extended_error_info;
115     const char *tag = "[KH386_cJSON_CreateArray]";
116     napi_value valuestringOut;
117     /* [NAPI_GEN]:
118      * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个
119      * 等价的js字符串
120      * env: 当前环境的句柄
121      * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以valuestring举例,用户可根据需求修改
122      * length:
123      * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
124      * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
125      */
126     status = napi_create_string_utf8(env, jsonArray->valuestring == NULL ? "" : jsonArray->valuestring,
127                                      NAPI_AUTO_LENGTH, &valuestringOut);
128     if (status != napi_ok) {
129         /*错误处理*/
130         getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
131         return nullptr;
132     }
133     /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
134      * env: 当前环境的句柄
135      * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
136      * utf8name: 属性的名称,是一个以UTF-8编码的字符串
137      * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
138      */
139     status = napi_set_named_property(env, cJSON_CreateArrayOut, "valuestring", valuestringOut);
140     if (status != napi_ok) {
141         /* [NAPI_GEN]: 错误处理*/
142         getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
143         return nullptr;
144     }
145     return cJSON_CreateArrayOut;
146 }
147 
getCreateArrayStringOut(napi_env env,napi_value cJSON_CreateArrayOut,cJSON * jsonArray)148 napi_value getCreateArrayStringOut(napi_env env, napi_value cJSON_CreateArrayOut, cJSON *jsonArray)
149 {
150     napi_status status;
151     const napi_extended_error_info *extended_error_info;
152     const char *tag = "[KH386_cJSON_CreateArray]";
153     napi_value stringOut;
154     /* [NAPI_GEN]:
155      * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
156      * env: 当前环境的句柄
157      * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以string举例,用户可根据需求修改
158      * length:
159      * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
160      * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
161      */
162     status =
163         napi_create_string_utf8(env, jsonArray->string == NULL ? "" : jsonArray->string, NAPI_AUTO_LENGTH, &stringOut);
164     if (status != napi_ok) {
165         /*错误处理*/
166         getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
167         return nullptr;
168     }
169     /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
170      * env: 当前环境的句柄
171      * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
172      * utf8name: 属性的名称,是一个以UTF-8编码的字符串
173      * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
174      */
175     status = napi_set_named_property(env, cJSON_CreateArrayOut, "string", stringOut);
176     if (status != napi_ok) {
177         /* [NAPI_GEN]: 错误处理*/
178         getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
179         return nullptr;
180     }
181     return cJSON_CreateArrayOut;
182 }
183 
getCreateArrayChildOut(napi_env env,napi_value cJSON_CreateArrayOut)184 napi_value getCreateArrayChildOut(napi_env env, napi_value cJSON_CreateArrayOut)
185 {
186     napi_status status;
187     const napi_extended_error_info *extended_error_info;
188     const char *tag = "[KH386_cJSON_CreateArray]";
189     napi_value childOut;
190     /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
191      * env: 当前环境的句柄
192      * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
193      */
194     status = napi_create_object(env, &childOut);
195     if (status != napi_ok) {
196         getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
197         return nullptr;
198     }
199 
200     /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
201      * env: 当前环境的句柄
202      * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
203      * utf8name: 属性的名称,是一个以UTF-8编码的字符串
204      * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
205      */
206     status = napi_set_named_property(env, cJSON_CreateArrayOut, "child", childOut);
207     if (status != napi_ok) {
208         /* [NAPI_GEN]: 错误处理*/
209         getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
210         return nullptr;
211     }
212     return cJSON_CreateArrayOut;
213 }
214 
getCreateArrayPrevOut(napi_env env,napi_value cJSON_CreateArrayOut)215 napi_value getCreateArrayPrevOut(napi_env env, napi_value cJSON_CreateArrayOut)
216 {
217     napi_status status;
218     const napi_extended_error_info *extended_error_info;
219     const char *tag = "[KH386_cJSON_CreateArray]";
220     napi_value prevOut;
221     /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
222      * env: 当前环境的句柄
223      * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
224      */
225     status = napi_create_object(env, &prevOut);
226     if (status != napi_ok) {
227         getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
228         return nullptr;
229     }
230     /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
231      * env: 当前环境的句柄
232      * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
233      * utf8name: 属性的名称,是一个以UTF-8编码的字符串
234      * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
235      */
236     status = napi_set_named_property(env, cJSON_CreateArrayOut, "prev", prevOut);
237     if (status != napi_ok) {
238         /* [NAPI_GEN]: 错误处理*/
239         getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
240         return nullptr;
241     }
242     return cJSON_CreateArrayOut;
243 }
244 
getCreateArrayNextOut(napi_env env,napi_value cJSON_CreateArrayOut)245 napi_value getCreateArrayNextOut(napi_env env, napi_value cJSON_CreateArrayOut)
246 {
247     napi_status status;
248     const napi_extended_error_info *extended_error_info;
249     const char *tag = "[KH386_cJSON_CreateArray]";
250     napi_value nextOut;
251     /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
252      * env: 当前环境的句柄
253      * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
254      */
255     status = napi_create_object(env, &nextOut);
256     if (status != napi_ok) {
257         getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
258         return nullptr;
259     }
260 
261     /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
262      * env: 当前环境的句柄
263      * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
264      * utf8name: 属性的名称,是一个以UTF-8编码的字符串
265      * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
266      */
267     status = napi_set_named_property(env, cJSON_CreateArrayOut, "next", nextOut);
268     if (status != napi_ok) {
269         /* [NAPI_GEN]: 错误处理*/
270         getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
271         return nullptr;
272     }
273     return cJSON_CreateArrayOut;
274 }
275 
276 /* [NAPI_GEN]:对应cJSON.h中: CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); 的napi方法,
277  * 输入:void
278  * 输出:创建的cjson array
279  */
KH386_cJSON_CreateArray(napi_env env,napi_callback_info info)280 napi_value KH386_cJSON_CreateArray(napi_env env, napi_callback_info info)
281 {
282     napi_status status;
283     /* [NAPI_GEN]: Node.js在其N-API中用来提供错误的扩展信息的结构体,结构体包含以下字段
284      * error_message: 一个指向错误详细字符串的指针,提供了关于错误的文本描述
285      * engin_reserved: 一个保留给Js引擎使用的指针
286      * error_code: 错误码,指示了错误的种类,比如napi_pending_exception表示有一个JavaScript异常未被清理。
287      * engine_error_code:一个引擎特定的错误码,为引擎实现保留,具体含义依赖于使用的JavaScript引擎。
288      * error_message_len:错误消息字符串的长度。
289      */
290     const napi_extended_error_info *extended_error_info;
291     /* [NAPI_GEN]: tag: 日志打印标签*/
292     const char *tag = "[KH386_cJSON_CreateArray]";
293 
294     // Todo: add business logic. 在这前后代码为框架所生成
295     cJSON *jsonArray = cJSON_CreateArray();
296 
297     /* [NAPI_GEN]: function return value*/
298     napi_value cJSON_CreateArrayOut;
299     /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
300      * env: 当前环境的句柄
301      * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
302      */
303     status = napi_create_object(env, &cJSON_CreateArrayOut);
304     if (status != napi_ok) {
305         getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
306         return nullptr;
307     }
308 
309     cJSON_CreateArrayOut = getCreateArrayChildOut(env, cJSON_CreateArrayOut);
310     cJSON_CreateArrayOut = getCreateArrayNextOut(env, cJSON_CreateArrayOut);
311     cJSON_CreateArrayOut = getCreateArrayPrevOut(env, cJSON_CreateArrayOut);
312     cJSON_CreateArrayOut = getCreateArrayTypeOut(env, cJSON_CreateArrayOut, jsonArray);
313     cJSON_CreateArrayOut = getCreateArrayValueintOut(env, cJSON_CreateArrayOut, jsonArray);
314     cJSON_CreateArrayOut = getCreateArrayValuestringOut(env, cJSON_CreateArrayOut, jsonArray);
315     cJSON_CreateArrayOut = getCreateArrayValuedoubleOut(env, cJSON_CreateArrayOut, jsonArray);
316     cJSON_CreateArrayOut = getCreateArrayStringOut(env, cJSON_CreateArrayOut, jsonArray);
317 
318     cJSON_Delete(jsonArray);
319     return cJSON_CreateArrayOut;
320 }
321