1 /*
2 * Copyright (c) 2023 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 "nodeapi.h"
17
18 static const char *TAG = "[nodeapi_status]";
19
testNapiStatus(napi_env env,napi_callback_info info)20 napi_value testNapiStatus(napi_env env, napi_callback_info info)
21 {
22 // pages/nodeapi/datatypes/napistatus
23 size_t argc = PARAM2;
24 napi_value argv[PARAM2];
25 napi_status status;
26
27 // 解析传入的参数
28 status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
29 if (status != napi_ok) {
30 napi_throw_error(env, NULL, "Failed to parse arguments");
31 return NULL;
32 }
33
34 // 检查参数数量
35 if (argc < PARAM2) {
36 napi_throw_error(env, NULL, "Expected 2 arguments");
37 return NULL;
38 }
39
40 int32_t num1 = 0;
41 int32_t num2 = 0;
42 status = napi_get_value_int32(env, argv[0], &num1);
43 if (status != napi_ok) {
44 napi_throw_error(env, NULL, "Invalid first argument");
45 return NULL;
46 }
47
48 status = napi_get_value_int32(env, argv[1], &num2);
49 if (status != napi_ok) {
50 napi_throw_error(env, NULL, "Invalid second argument");
51 return NULL;
52 }
53
54 // 执行加法操作
55 int32_t result = num1 + num2;
56 if (result > PARAM10) {
57 napi_throw_error(env, NULL, "Invalid result > 10.");
58 return NULL;
59 }
60
61 // 返回结果
62 napi_value resultValue;
63 status = napi_create_int32(env, result, &resultValue);
64 if (status != napi_ok) {
65 napi_throw_error(env, NULL, "Failed to create result value");
66 return NULL;
67 }
68
69 return resultValue;
70 }
71