1 /*
2 * Copyright (c) 2023 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 "basesample.h"
17 
Add(napi_env env, napi_callback_info info)18 static napi_value Add(napi_env env, napi_callback_info info)
19 {
20     size_t requireArgc = 2;
21     size_t argc = 2;
22     napi_value args[2] = {nullptr};
23 
24     OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, "basesample", "Add in");
25 
26     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
27 
28     napi_valuetype valuetype0;
29     napi_typeof(env, args[0], &valuetype0);
30 
31     napi_valuetype valuetype1;
32     napi_typeof(env, args[1], &valuetype1);
33 
34     double value0;
35     napi_get_value_double(env, args[0], &value0);
36 
37     double value1;
38     napi_get_value_double(env, args[1], &value1);
39 
40     napi_value sum;
41     napi_create_double(env, value0 + value1, &sum);
42 
43     return sum;
44 }
45 
createTctBaseInstance(napi_env env)46 napi_value createTctBaseInstance(napi_env env)
47 {
48     napi_status status;
49     napi_value instance;
50 
51     OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, "basesample", "Add in");
52 
53     status = napi_create_object(env, &instance);
54     if (status != napi_ok) {
55         napi_throw_error(env, nullptr, "Failed to create object");
56         return nullptr;
57     }
58 
59     napi_value pname;
60     status = napi_create_string_utf8(env, "tct_base", PARAM10, &pname);
61     if (status != napi_ok) {
62         napi_throw_error(env, nullptr, "Invalid argument: name must be a string");
63         return nullptr;
64     }
65 
66     status = napi_set_named_property(env, instance, "name", pname);
67     if (status != napi_ok) {
68         napi_throw_error(env, nullptr, "Failed to set property");
69         return nullptr;
70     }
71 
72     napi_property_descriptor properties[] = {
73         {"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr},
74     };
75 
76     status = napi_define_properties(env, instance, sizeof(properties) / sizeof(napi_property_descriptor), properties);
77     if (status != napi_ok) {
78         napi_throw_error(env, nullptr, "Failed to define properties");
79         return nullptr;
80     }
81     return instance;
82 }
83