• 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 
testNapiSetProperty(napi_env env,napi_callback_info info)20 napi_value testNapiSetProperty(napi_env env, napi_callback_info info)
21 {
22     // pages/javascript/jsproperties/napisetproperty
23     size_t argc = PARAM3;
24     napi_value argv[PARAM3];
25     napi_status status;
26     napi_value obj;
27     napi_value propName;
28     napi_value propValue;
29     const napi_extended_error_info *extended_error_info;
30 
31     // 解析传入的参数
32     status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
33     if (status != napi_ok) {
34         getErrMsg(status, env, extended_error_info, "get cb info", TAG);
35         return NULL;
36     }
37 
38     // 检查参数数量
39     if (argc < PARAM3) {
40         napi_throw_error(env, NULL, "Expected 3 arguments");
41         return NULL;
42     }
43     obj = argv[PARAM0];
44     propName = argv[PARAM1];
45     propValue = argv[PARAM2];
46 
47     napi_valuetype valuetype0;
48 
49     // 确认第一个参数是个对象
50     status = napi_typeof(env, obj, &valuetype0);
51     if (status != napi_ok) {
52         getErrMsg(status, env, extended_error_info, "get obj type", TAG);
53         return NULL;
54     }
55     if (valuetype0 != napi_object) {
56         napi_throw_type_error(env, NULL, "Wrong argument type, expected an object");
57         return NULL;
58     }
59 
60     // 设置对象的属性
61     status = napi_set_property(env, obj, propName, propValue);
62     if (status != napi_ok) {
63         getErrMsg(status, env, extended_error_info, "set property", TAG);
64         return NULL;
65     }
66 
67     // 可以返回新设置对象
68     return obj;
69 }
70