1 /*
2 * Copyright (c) 2021 Huawei Device 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 "napi/native_api.h"
17 #include "hidebug/hidebug.h"
18 #include "hidebug/hidebug_type.h"
19
Add(napi_env env,napi_callback_info info)20 static napi_value Add(napi_env env, napi_callback_info info)
21 {
22 size_t argc = 2;
23 napi_value args[2] = {nullptr};
24
25 napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
26
27 napi_valuetype valuetype0;
28 napi_typeof(env, args[0], &valuetype0);
29
30 napi_valuetype valuetype1;
31 napi_typeof(env, args[1], &valuetype1);
32
33 double value0;
34 napi_get_value_double(env, args[0], &value0);
35
36 double value1;
37 napi_get_value_double(env, args[1], &value1);
38
39 napi_value sum;
40 napi_create_double(env, value0 + value1, &sum);
41
42 return sum;
43
44 }
45
GetNativeMemInfoTrue(napi_env env,napi_callback_info info)46 static napi_value GetNativeMemInfoTrue(napi_env env, napi_callback_info info)
47 {
48 HiDebug_NativeMemInfo nativeMemInfo;
49 OH_HiDebug_GetAppNativeMemInfoWithCache(&nativeMemInfo, true);
50 napi_value memInfo;
51 napi_create_object(env, &memInfo);
52
53 napi_value pss;
54 napi_create_bigint_uint64(env, nativeMemInfo.pss, &pss);
55 napi_set_named_property(env, memInfo, "pss", pss);
56
57 napi_value rss;
58 napi_create_bigint_uint64(env, nativeMemInfo.rss, &rss);
59 napi_set_named_property(env, memInfo, "rss", rss);
60
61 napi_value sharedDirty;
62 napi_create_bigint_uint64(env, nativeMemInfo.sharedDirty, &sharedDirty);
63 napi_set_named_property(env, memInfo, "sharedDirty", sharedDirty);
64
65 napi_value privateDirty;
66 napi_create_bigint_uint64(env, nativeMemInfo.privateDirty, &privateDirty);
67 napi_set_named_property(env, memInfo, "privateDirty", privateDirty);
68
69 napi_value sharedClean;
70 napi_create_bigint_uint64(env, nativeMemInfo.sharedClean, &sharedClean);
71 napi_set_named_property(env, memInfo, "sharedClean", sharedClean);
72
73 napi_value privateClean;
74 napi_create_bigint_uint64(env, nativeMemInfo.privateClean, &privateClean);
75 napi_set_named_property(env, memInfo, "privateClean", privateClean);
76
77 napi_value vss;
78 napi_create_bigint_uint64(env, nativeMemInfo.vss, &vss);
79 napi_set_named_property(env, memInfo, "vss", vss);
80 return memInfo;
81 }
82
GetNativeMemInfoFalse(napi_env env,napi_callback_info info)83 static napi_value GetNativeMemInfoFalse(napi_env env, napi_callback_info info)
84 {
85 HiDebug_NativeMemInfo nativeMemInfo;
86 OH_HiDebug_GetAppNativeMemInfoWithCache(&nativeMemInfo, false);
87 napi_value memInfo;
88 napi_create_object(env, &memInfo);
89
90 napi_value pss;
91 napi_create_bigint_uint64(env, nativeMemInfo.pss, &pss);
92 napi_set_named_property(env, memInfo, "pss", pss);
93
94 napi_value rss;
95 napi_create_bigint_uint64(env, nativeMemInfo.rss, &rss);
96 napi_set_named_property(env, memInfo, "rss", rss);
97
98 napi_value sharedDirty;
99 napi_create_bigint_uint64(env, nativeMemInfo.sharedDirty, &sharedDirty);
100 napi_set_named_property(env, memInfo, "sharedDirty", sharedDirty);
101
102 napi_value privateDirty;
103 napi_create_bigint_uint64(env, nativeMemInfo.privateDirty, &privateDirty);
104 napi_set_named_property(env, memInfo, "privateDirty", privateDirty);
105
106 napi_value sharedClean;
107 napi_create_bigint_uint64(env, nativeMemInfo.sharedClean, &sharedClean);
108 napi_set_named_property(env, memInfo, "sharedClean", sharedClean);
109
110 napi_value privateClean;
111 napi_create_bigint_uint64(env, nativeMemInfo.privateClean, &privateClean);
112 napi_set_named_property(env, memInfo, "privateClean", privateClean);
113
114 napi_value vss;
115 napi_create_bigint_uint64(env, nativeMemInfo.vss, &vss);
116 napi_set_named_property(env, memInfo, "vss", vss);
117 return memInfo;
118 }
119
120 EXTERN_C_START
Init(napi_env env,napi_value exports)121 static napi_value Init(napi_env env, napi_value exports)
122 {
123 napi_property_descriptor desc[] = {
124 { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
125 { "getNativeMemInfoTrue", nullptr, GetNativeMemInfoTrue, nullptr, nullptr, nullptr, napi_default, nullptr },
126 { "getNativeMemInfoFalse", nullptr, GetNativeMemInfoFalse, nullptr, nullptr, nullptr, napi_default, nullptr },
127 };
128 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
129 return exports;
130 }
131 EXTERN_C_END
132
133 static napi_module demoModule = {
134 .nm_version = 1,
135 .nm_flags = 0,
136 .nm_filename = nullptr,
137 .nm_register_func = Init,
138 .nm_modname = "entry",
139 .nm_priv = ((void*)0),
140 .reserved = { 0 },
141 };
142
RegisterEntryModule(void)143 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
144 {
145 napi_module_register(&demoModule);
146 }
147