• 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 "javascriptapi.h"
17 
18 static const char *TAG = "[javascriptapi_property]";
19 
testNapiHasProperty(napi_env env,napi_callback_info info)20 napi_value testNapiHasProperty(napi_env env, napi_callback_info info)
21 {
22     // pages/javascript/jsproperties/napihasproperty
23     size_t argc = PARAM2;
24     napi_value argv[PARAM2];
25     napi_status status;
26     napi_value obj;
27     napi_value propName;
28     const napi_extended_error_info *extended_error_info;
29 
30     // 解析传入的参数
31     status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
32     if (status != napi_ok) {
33         getErrMsg(status, env, extended_error_info, "get cb info", TAG);
34         return NULL;
35     }
36 
37     // 检查参数数量
38     if (argc < PARAM2) {
39         napi_throw_error(env, NULL, "Expected 2 arguments");
40         return NULL;
41     }
42     obj = argv[PARAM0];
43     propName = argv[PARAM1];
44 
45     napi_valuetype valuetype0;
46 
47     // 确认第一个参数是个对象
48     status = napi_typeof(env, obj, &valuetype0);
49     if (status != napi_ok) {
50         getErrMsg(status, env, extended_error_info, "get obj type", TAG);
51         return NULL;
52     }
53     if (valuetype0 != napi_object) {
54         napi_throw_type_error(env, NULL, "Wrong argument type, expected an object");
55         return NULL;
56     }
57 
58     // 检查属性是否存在
59     bool hasProperty = false;
60     status = napi_has_property(env, obj, propName, &hasProperty);
61     if (status != napi_ok) {
62         getErrMsg(status, env, extended_error_info, "has property", TAG);
63         return NULL;
64     }
65 
66     // 返回属性是否存在的布尔值
67     napi_value result;
68     status = napi_get_boolean(env, hasProperty, &result);
69     if (status != napi_ok) {
70         getErrMsg(status, env, extended_error_info, "get boolean", TAG);
71         return NULL;
72     }
73 
74     return result;
75 }
76