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
testNapiGetPropertyNames(napi_env env,napi_callback_info info)20 napi_value testNapiGetPropertyNames(napi_env env, napi_callback_info info)
21 {
22 // pages/javascript/jsproperties/napigetpropertynames
23 size_t argc = PARAM1;
24 napi_value argv[PARAM1];
25 napi_status status;
26 napi_value obj;
27 const napi_extended_error_info *extended_error_info;
28
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 // 检查参数数量
37 if (argc < PARAM1) {
38 napi_throw_error(env, NULL, "Expected 1 arguments");
39 return NULL;
40 }
41
42 obj = argv[PARAM0];
43 napi_value propertyNames;
44 status = napi_get_property_names(env, obj, &propertyNames);
45 if (status != napi_ok) {
46 getErrMsg(status, env, extended_error_info, "get property names", TAG);
47 return NULL;
48 }
49
50 uint32_t length = 0;
51 status = napi_get_array_length(env, propertyNames, &length);
52 if (status != napi_ok) {
53 getErrMsg(status, env, extended_error_info, "get array length", TAG);
54 return NULL;
55 }
56 // 打印属性个数
57 OH_LOG_INFO(LOG_APP, "napi_get_array_length success! propertyNames length: %i", length);
58
59 return propertyNames;
60 }
61