• 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 
callFunctionIfTypeIsFunction(napi_env & env,napi_value & obj,napi_value & propValue)20 napi_value callFunctionIfTypeIsFunction(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 
testNapiGetProperty(napi_env env,napi_callback_info info)47 napi_value testNapiGetProperty(napi_env env, napi_callback_info info)
48 {
49     // pages/javascript/jsproperties/napigetproperty
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 
57     // 解析传入的参数
58     status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
59     if (status != napi_ok) {
60         getErrMsg(status, env, extended_error_info, "get cb info", TAG);
61         return NULL;
62     }
63 
64     // 检查参数数量
65     if (argc < PARAM2) {
66         napi_throw_error(env, NULL, "Expected 2 arguments");
67         return NULL;
68     }
69     obj = argv[PARAM0];
70     propName = argv[PARAM1];
71 
72     napi_valuetype valuetype0;
73 
74     // 确认第一个参数是个对象
75     status = napi_typeof(env, obj, &valuetype0);
76     if (status != napi_ok) {
77         getErrMsg(status, env, extended_error_info, "get obj type", TAG);
78         return NULL;
79     }
80     if (valuetype0 != napi_object) {
81         napi_throw_type_error(env, NULL, "Wrong argument type, expected an object");
82         return NULL;
83     }
84 
85     // 读取属性
86     napi_value propValue;
87     status = napi_get_property(env, obj, propName, &propValue);
88     if (status != napi_ok) {
89         getErrMsg(status, env, extended_error_info, "get property", TAG);
90         return NULL;
91     }
92 
93     // 检查 propValue是否是一个函数
94     napi_value result = callFunctionIfTypeIsFunction(env, obj, propValue);
95     if (result != NULL) {
96         return result;
97     }
98 
99     // 返回属性值
100     return propValue;
101 }
102