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
getSampleCreateObjNextOut(napi_env env,napi_value cJSON_CreateObjectOut)18 napi_value getSampleCreateObjNextOut(napi_env env, napi_value cJSON_CreateObjectOut)
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]:
34 * 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js env:
35 * 当前环境的句柄 object: 要设置属性的js对象,该对象是由上文napi_create_object创建的 utf8name:
36 * 属性的名称,是一个以UTF-8编码的字符串 value:
37 * 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
38 */
39 status = napi_set_named_property(env, cJSON_CreateObjectOut, "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_CreateObjectOut;
46 }
47
getSampleCreateObjPrevOut(napi_env env,napi_value cJSON_CreateObjectOut)48 napi_value getSampleCreateObjPrevOut(napi_env env, napi_value cJSON_CreateObjectOut)
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_CreateObjectOut, "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_CreateObjectOut;
76 }
77
getSampleCreateObjChildOut(napi_env env,napi_value cJSON_CreateObjectOut)78 napi_value getSampleCreateObjChildOut(napi_env env, napi_value cJSON_CreateObjectOut)
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_CreateObjectOut, "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_CreateObjectOut;
106 }
107
getSampleCreateObjTypeOut(napi_env env,napi_value cJSON_CreateObjectOut,cJSON * jsonObject)108 napi_value getSampleCreateObjTypeOut(napi_env env, napi_value cJSON_CreateObjectOut, cJSON *jsonObject)
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 == NULL ? 0 : 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_CreateObjectOut, "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_CreateObjectOut;
137 }
138
getSampleCreateObjValuestringOut(napi_env env,napi_value cJSON_CreateObjectOut,cJSON * jsonObject)139 napi_value getSampleCreateObjValuestringOut(napi_env env, napi_value cJSON_CreateObjectOut, cJSON *jsonObject)
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 const char *valuestringVal = jsonObject == NULL ? ""
146 : jsonObject->valuestring == NULL ? ""
147 : jsonObject->valuestring;
148 /* [NAPI_GEN]:
149 * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
150 * env: 当前环境的句柄
151 * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以valuestring举例,用户可根据需求修改
152 * length:
153 * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
154 * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
155 */
156 status = napi_create_string_utf8(env, valuestringVal, NAPI_AUTO_LENGTH, &valuestringOut);
157 if (status != napi_ok) {
158 /*错误处理*/
159 getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
160 return nullptr;
161 }
162 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
163 * env: 当前环境的句柄
164 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
165 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
166 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
167 */
168 status = napi_set_named_property(env, cJSON_CreateObjectOut, "valuestring", valuestringOut);
169 if (status != napi_ok) {
170 /* [NAPI_GEN]: 错误处理*/
171 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
172 return nullptr;
173 }
174 return cJSON_CreateObjectOut;
175 }
176
getSampleCreateObjValueintOut(napi_env env,napi_value cJSON_CreateObjectOut,cJSON * jsonObject)177 napi_value getSampleCreateObjValueintOut(napi_env env, napi_value cJSON_CreateObjectOut, cJSON *jsonObject)
178 {
179 napi_status status;
180 const napi_extended_error_info *extended_error_info;
181 const char *tag = "[KH361_cJSON_CreateObject]";
182 napi_value valueintOut;
183 /* [NAPI_GEN]: 返回值是int32_t类型时,napi_create_int32 创建一个包含32位整数(int32_t)的js数值(Number)对象
184 * env: 当前环境的句柄
185 * value: 要准换成js数值的int32_t的值,这里以传入1为例,用例新增业务代码时可根据自身需求修改
186 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
187 */
188 status = napi_create_int32(env, jsonObject == NULL ? 0 : jsonObject->valueint, &valueintOut);
189 if (status != napi_ok) {
190 getErrMessage(status, env, extended_error_info, "napi_create_int32", tag);
191 return nullptr;
192 }
193 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
194 * env: 当前环境的句柄
195 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
196 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
197 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
198 */
199 status = napi_set_named_property(env, cJSON_CreateObjectOut, "valueint", valueintOut);
200 if (status != napi_ok) {
201 /* [NAPI_GEN]: 错误处理*/
202 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
203 return nullptr;
204 }
205 return cJSON_CreateObjectOut;
206 }
207
getSampleCreateObjValuedoubleOut(napi_env env,napi_value cJSON_CreateObjectOut,cJSON * jsonObject)208 napi_value getSampleCreateObjValuedoubleOut(napi_env env, napi_value cJSON_CreateObjectOut, cJSON *jsonObject)
209 {
210 napi_status status;
211 const napi_extended_error_info *extended_error_info;
212 const char *tag = "[KH361_cJSON_CreateObject]";
213 napi_value valuedoubleOut;
214 /* [NAPI_GEN]: 返回值是double类型时,napi_create_double 创建一个包含双精度浮点数的js数值(Number)对象
215 * env: 当前环境的句柄
216 * value: 要传递给js的双精度浮点数值,这里以传入1.0为例,用例新增业务代码时可根据自身需求修改
217 * result: 指向napi_value的指针,这个指针会被设置为新创建的js数值对象
218 */
219 status = napi_create_double(env, jsonObject == NULL ?0 : jsonObject->valuedouble, &valuedoubleOut);
220 if (status != napi_ok) {
221 getErrMessage(status, env, extended_error_info, "napi_create_double", tag);
222 return nullptr;
223 }
224 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
225 * env: 当前环境的句柄
226 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
227 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
228 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
229 */
230 status = napi_set_named_property(env, cJSON_CreateObjectOut, "valuedouble", valuedoubleOut);
231 if (status != napi_ok) {
232 /* [NAPI_GEN]: 错误处理*/
233 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
234 return nullptr;
235 }
236 return cJSON_CreateObjectOut;
237 }
238
getSampleCreateObjStringOut(napi_env env,napi_value cJSON_CreateObjectOut,cJSON * jsonObject)239 napi_value getSampleCreateObjStringOut(napi_env env, napi_value cJSON_CreateObjectOut, cJSON *jsonObject)
240 {
241 napi_status status;
242 const napi_extended_error_info *extended_error_info;
243 const char *tag = "[KH361_cJSON_CreateObject]";
244 napi_value stringOut;
245 const char *stringVal = jsonObject == NULL ?"" :jsonObject->string == NULL ?"" :jsonObject->string;
246 /* [NAPI_GEN]:
247 * 返回值是字符串时,napi_create_string_utf8用于在原生代码中创建一个新的js字符串。这个函数会根据提供的UTF-8编码的字符串创建一个等价的js字符串
248 * env: 当前环境的句柄
249 * str: 指向以null结尾的UTF-8编码的C字符串的指针,这里以string举例,用户可根据需求修改
250 * length:
251 * 字符串的长度,可以是具体的字节数,或者使用特殊的值NAPI_AUTO_LENGTH来让函数自己计算长度(假定字符串以null结尾)
252 * result: 指向napi_value的指针,函数执行成功后这个指针将指向新创建的js字符串
253 */
254 status = napi_create_string_utf8(env, stringVal, NAPI_AUTO_LENGTH, &stringOut);
255 if (status != napi_ok) {
256 /*错误处理*/
257 getErrMessage(status, env, extended_error_info, "napi_create_string_utf8", tag);
258 return nullptr;
259 }
260 /* [NAPI_GEN]: 返回值是对象时,将native侧的对象的属性和值依次塞入napi_create_object创建出的对象,最终将该对象返回js
261 * env: 当前环境的句柄
262 * object: 要设置属性的js对象,该对象是由上文napi_create_object创建的
263 * utf8name: 属性的名称,是一个以UTF-8编码的字符串
264 * value: 与属性名称关联的值,这个值可以是任何js类型(如一个数值、字符串、另一个对象等)
265 */
266 status = napi_set_named_property(env, cJSON_CreateObjectOut, "string", stringOut);
267 if (status != napi_ok) {
268 /* [NAPI_GEN]: 错误处理*/
269 getErrMessage(status, env, extended_error_info, "napi_set_named_property", tag);
270 return nullptr;
271 }
272 return cJSON_CreateObjectOut;
273 }
274
275 /* [NAPI_GEN]:对应cJSON.h中: CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);的napi方法,
276 * 输入为空
277 * 输出创建的cJSON对象
278 */
KH361_cJSON_CreateObject(napi_env env,napi_callback_info info)279 napi_value KH361_cJSON_CreateObject(napi_env env, napi_callback_info info)
280 {
281 napi_status status;
282 /* [NAPI_GEN]: Node.js在其N-API中用来提供错误的扩展信息的结构体,结构体包含以下字段
283 * error_message: 一个指向错误详细字符串的指针,提供了关于错误的文本描述
284 * engin_reserved: 一个保留给Js引擎使用的指针
285 * error_code: 错误码,指示了错误的种类,比如napi_pending_exception表示有一个JavaScript异常未被清理。
286 * engine_error_code:一个引擎特定的错误码,为引擎实现保留,具体含义依赖于使用的JavaScript引擎。
287 * error_message_len:错误消息字符串的长度。
288 */
289 const napi_extended_error_info *extended_error_info;
290 /* [NAPI_GEN]: tag: 日志打印标签*/
291 const char *tag = "[KH361_cJSON_CreateObject]";
292
293 // Todo: add business logic. 在这前后代码为框架所生成
294 cJSON *jsonObject = cJSON_CreateObject();
295 if (jsonObject == NULL) {
296 return NULL;
297 }
298
299 /* [NAPI_GEN]: function return value*/
300 napi_value cJSON_CreateObjectOut;
301 /* [NAPI_GEN]: 返回值是对象时,需要使用napi_create_object创建一个js的对象与js代码交互
302 * env: 当前环境的句柄
303 * result: 一个napi_value的指针,该指针将被设置为新创建的js对象
304 */
305 status = napi_create_object(env, &cJSON_CreateObjectOut);
306 if (status != napi_ok) {
307 getErrMessage(status, env, extended_error_info, "napi_create_object", tag);
308 return nullptr;
309 }
310 cJSON_CreateObjectOut = getSampleCreateObjNextOut(env, cJSON_CreateObjectOut);
311 cJSON_CreateObjectOut = getSampleCreateObjPrevOut(env, cJSON_CreateObjectOut);
312 cJSON_CreateObjectOut = getSampleCreateObjChildOut(env, cJSON_CreateObjectOut);
313 cJSON_CreateObjectOut = getSampleCreateObjTypeOut(env, cJSON_CreateObjectOut, jsonObject);
314 cJSON_CreateObjectOut = getSampleCreateObjValuestringOut(env, cJSON_CreateObjectOut, jsonObject);
315 cJSON_CreateObjectOut = getSampleCreateObjValueintOut(env, cJSON_CreateObjectOut, jsonObject);
316 cJSON_CreateObjectOut = getSampleCreateObjValuedoubleOut(env, cJSON_CreateObjectOut, jsonObject);
317 cJSON_CreateObjectOut = getSampleCreateObjStringOut(env, cJSON_CreateObjectOut, jsonObject);
318
319 // 释放cJSON内存
320 cJSON_Delete(jsonObject);
321 return cJSON_CreateObjectOut;
322 }
323