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 "common.h"
17 #include "javascriptapi.h"
18 #include <bits/alltypes.h>
19
20 static const char *TAG = "[javascriptapi_values";
21
testNapiCreateUInt32(napi_env env,napi_callback_info info)22 napi_value testNapiCreateUInt32(napi_env env, napi_callback_info info)
23 {
24 // pages/javascript/jsvalues/napicreateuint32
25 // 获取参数数量
26 size_t argc = 1;
27 // 准备接收参数的变量
28 napi_value argv[1];
29 uint32_t uintValue;
30 napi_value result;
31 napi_status status;
32 const napi_extended_error_info *extended_error_info;
33
34 // 获取回调函数的参数信息
35 status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
36 if (status != napi_ok) {
37 getErrMsg(status, env, extended_error_info, "Failed to get callback info", TAG);
38 return NULL;
39 }
40
41 // 检查参数数量是否符合预期
42 if (argc != 1) {
43 napi_throw_error(env, NULL, "Expected exactly one argument");
44 return NULL;
45 }
46
47 // 从JavaScript值中提取出无符号整数
48 status = napi_get_value_uint32(env, argv[0], &uintValue);
49 if (status != napi_ok) {
50 getErrMsg(status, env, extended_error_info, "Failed to convert argument to uint32", TAG);
51 return NULL;
52 }
53
54 // 检查无符号整数是否为3
55 if (uintValue != 3) {
56 napi_throw_error(env, NULL, "The number is not 3");
57 return NULL;
58 }
59
60 // 使用提取的无符号整数值创建一个新的napi_value
61 status = napi_create_uint32(env, uintValue, &result);
62 if (status != napi_ok) {
63 getErrMsg(status, env, extended_error_info, "Failed to create uint32 value", TAG);
64 return NULL;
65 }
66
67 // 返回创建的napi_value
68 return result;
69 }