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
callFunctionIfPropertyTypeIsFunction(napi_env & env,napi_value & obj,napi_value & propValue)20 napi_value callFunctionIfPropertyTypeIsFunction(napi_env &env, napi_value &obj, napi_value &propValue)
21 {
22 napi_status status;
23 const napi_extended_error_info *extended_error_info;
24 napi_valuetype valuetype;
25
26 status = napi_typeof(env, propValue, &valuetype);
27 if (status != napi_ok) {
28 getErrMsg(status, env, extended_error_info, "get type", TAG);
29 return NULL;
30 }
31
32 // propValue 是一个函数,我们可以尝试调用它
33 if (valuetype == napi_function) {
34 napi_value result;
35 status = napi_call_function(env, obj, propValue, 0, NULL, &result);
36 if (status != napi_ok) {
37 getErrMsg(status, env, extended_error_info, "call function", TAG);
38 return NULL;
39 }
40 // 函数被调用,其结果存储在 result 中,返回函数调用结果
41 return result;
42 }
43
44 return propValue;
45 }
46
testNapiGetNamedProperty(napi_env env,napi_callback_info info)47 napi_value testNapiGetNamedProperty(napi_env env, napi_callback_info info)
48 {
49 // pages/javascript/jsproperties/napigetnamedproperty
50 size_t argc = PARAM2;
51 napi_value argv[PARAM2];
52 napi_status status;
53 napi_value obj;
54 napi_value propName;
55 const napi_extended_error_info *extended_error_info;
56 status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); // 解析传入的参数
57 if (status != napi_ok) {
58 getErrMsg(status, env, extended_error_info, "get cb info", TAG);
59 return NULL;
60 }
61 if (argc < PARAM2) { // 检查参数数量
62 napi_throw_error(env, NULL, "Expected 2 arguments");
63 return NULL;
64 }
65 obj = argv[PARAM0];
66 propName = argv[PARAM1];
67 // 判断参数有效性
68 bool resValid = validateObjectProperty(env, obj, propName, TAG);
69 if (resValid == false) {
70 return NULL;
71 }
72 size_t str_size = 0; // 将第二个参数从napi_value转换为C字符串
73 status = napi_get_value_string_utf8(env, propName, NULL, 0, &str_size);
74 if (status != napi_ok) {
75 getErrMsg(status, env, extended_error_info, "get value string", TAG);
76 return NULL;
77 }
78 char *propertyName = new char[str_size + 1];
79 status = napi_get_value_string_utf8(env, propName, propertyName, str_size + 1, &str_size);
80 if (status != napi_ok) {
81 getErrMsg(status, env, extended_error_info, "get value string", TAG);
82 delete[] propertyName;
83 return NULL;
84 }
85 napi_value propValue;
86 status = napi_get_named_property(env, obj, propertyName, &propValue); // 读取属性
87 if (status != napi_ok) {
88 getErrMsg(status, env, extended_error_info, "get named property", TAG);
89 delete[] propertyName;
90 return NULL;
91 }
92 delete[] propertyName;
93 // 检查 propValue是否是一个函数
94 napi_value result = callFunctionIfPropertyTypeIsFunction(env, obj, propValue);
95 if (result != NULL) {
96 return result;
97 }
98 return propValue; // 返回属性值
99 }