• 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 
getSecondParamStr(napi_env & env,napi_value & propName)20 char *getSecondParamStr(napi_env &env, napi_value &propName)
21 {
22     napi_status status;
23     const napi_extended_error_info *extended_error_info;
24     size_t str_size = 0;
25     status = napi_get_value_string_utf8(env, propName, NULL, 0, &str_size);
26     if (status != napi_ok) {
27         getErrMsg(status, env, extended_error_info, "get value string", TAG);
28         return NULL;
29     }
30 
31     char *propertyName = new char[str_size + 1];
32     status = napi_get_value_string_utf8(env, propName, propertyName, str_size + 1, &str_size);
33     if (status != napi_ok) {
34         getErrMsg(status, env, extended_error_info, "get value string", TAG);
35         delete[] propertyName;
36         return NULL;
37     }
38     return propertyName;
39 }
40 
testNapiHasNamedProperty(napi_env env,napi_callback_info info)41 napi_value testNapiHasNamedProperty(napi_env env, napi_callback_info info)
42 {
43     // pages/javascript/jsproperties/napihasnamedproperty
44     size_t argc = PARAM2;
45     napi_value argv[PARAM2];
46     napi_status status;
47     napi_value obj;
48     napi_value propName;
49     const napi_extended_error_info *extended_error_info;
50     // 解析传入的参数
51     status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
52     if (status != napi_ok) {
53         getErrMsg(status, env, extended_error_info, "get cb info", TAG);
54         return NULL;
55     }
56     // 检查参数数量
57     if (argc < PARAM2) {
58         napi_throw_error(env, NULL, "Expected 2 arguments");
59         return NULL;
60     }
61     obj = argv[PARAM0];
62     propName = argv[PARAM1];
63     // 判断参数有效性
64     bool resValid = validateObjectProperty(env, obj, propName, TAG);
65     if (resValid == false) {
66         return NULL;
67     }
68     // 将第二个参数从napi_value转换为C字符串
69     char *propertyName = getSecondParamStr(env, propName);
70     // 获取对象上指定名称的属性
71     bool hasProperty = false;
72     status = napi_has_named_property(env, obj, propertyName, &hasProperty);
73     free(propertyName);
74     if (status != napi_ok) {
75         if (status != napi_ok) {
76             getErrMsg(status, env, extended_error_info, "has named property", TAG);
77             return NULL;
78         }
79         return NULL;
80     }
81     // 返回属性是否存在的布尔值
82     napi_value result;
83     status = napi_get_boolean(env, hasProperty, &result);
84     if (status != napi_ok) {
85         getErrMsg(status, env, extended_error_info, "get boolean", TAG);
86         return NULL;
87     }
88     return result;
89 }