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 "javascriptapi.h"
17
18 static const char *TAG = "[javascriptapi_property]";
19
testNapiHasElement(napi_env env,napi_callback_info info)20 napi_value testNapiHasElement(napi_env env, napi_callback_info info)
21 {
22 // pages/javascript/jsproperties/napihaselement
23 size_t argc = PARAM2;
24 napi_value argv[PARAM2];
25 napi_status status;
26 napi_value arrayObj;
27 napi_value elementIndex;
28 const napi_extended_error_info *extended_error_info;
29 // 解析传入的参数
30 status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
31 if (status != napi_ok) {
32 getErrMsg(status, env, extended_error_info, "get cb info", TAG);
33 return NULL;
34 }
35 // 检查参数数量
36 if (argc < PARAM2) {
37 napi_throw_error(env, NULL, "Expected 2 arguments");
38 return NULL;
39 }
40 arrayObj = argv[PARAM0];
41 elementIndex = argv[PARAM1];
42 // 判断参数有效性
43 bool resValid = validateArrayObjProperty(env, arrayObj, elementIndex, TAG);
44 if (resValid == false) {
45 return NULL;
46 }
47 uint32_t index = 0;
48 // 将第二个参数(索引)转换为本地 uint32_t
49 status = napi_get_value_uint32(env, elementIndex, &index);
50 if (status != napi_ok) {
51 getErrMsg(status, env, extended_error_info, "get value uint32", TAG);
52 return NULL;
53 }
54 bool hasElement = false;
55 // 设置数组元素
56 status = napi_has_element(env, arrayObj, index, &hasElement);
57 if (status != napi_ok) {
58 getErrMsg(status, env, extended_error_info, "has element", TAG);
59 return NULL;
60 }
61 // 返回属性是否存在的布尔值
62 napi_value result;
63 status = napi_get_boolean(env, hasElement, &result);
64 if (status != napi_ok) {
65 getErrMsg(status, env, extended_error_info, "get boolean", TAG);
66 return NULL;
67 }
68 return result;
69 }
70