-
Notifications
You must be signed in to change notification settings - Fork 22
/
addon.cc
284 lines (239 loc) · 6.79 KB
/
addon.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright 2024 MNX Cloud, Inc.
*/
/*
* This file contains all of the C++ support code required to interface node
* with kstat.c.
*/
#include "addon.h"
#include <iostream>
#include <napi.h>
class KStat : public Napi::ObjectWrap<KStat> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
KStat(const Napi::CallbackInfo& info);
private:
Napi::Value Read(const Napi::CallbackInfo& info);
Napi::Value Close(const Napi::CallbackInfo& info);
nvlist_t * parse_options(const Napi::CallbackInfo& info);
Napi::Value nvpair_to_value(Napi::Env, nvpair_t *);
Napi::Object create_and_populate(Napi::Env, nvlist_t *);
kstatjs_t *ksj;
};
Napi::Object KStat::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, "KStat", {
InstanceMethod("read", &KStat::Read),
InstanceMethod("close", &KStat::Close)
});
exports.Set("Reader", func);
return (exports);
}
/*
* Parse an optional Object passed as the only argument to Reader() or read(),
* and return a populated nvlist_t with valid entries added.
*
* Any invalid arguments throw a TypeError and return NULL.
*/
nvlist_t *
KStat::parse_options(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
Napi::Object args;
nvlist_t *nvl;
/*
* The only valid options are none, or a single object.
*/
if (info.Length() > 1) {
Napi::Error::New(env,
"illegal kstat specifier (spurious argument)")
.ThrowAsJavaScriptException();
return (NULL);
}
if (info.Length() == 1 && !info[0].IsObject()) {
Napi::Error::New(env,
"illegal kstat specifier (expected object)")
.ThrowAsJavaScriptException();
return (NULL);
}
nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
/*
* If no object was provided just return the empty nvlist.
*/
if (!info[0].IsObject())
return (nvl);
args = info[0].ToObject();
if (args.Has("class")) {
if (!(args.Get("class").IsString())) {
Napi::TypeError::New(env,
"illegal kstat specifier (expected string)")
.ThrowAsJavaScriptException();
nvlist_free(nvl);
return (NULL);
}
nvlist_add_string(nvl, "class",
args.Get("class").ToString().Utf8Value().c_str());
}
if (args.Has("name")) {
if (!(args.Get("name").IsString())) {
Napi::TypeError::New(env,
"illegal kstat specifier (expected string)")
.ThrowAsJavaScriptException();
nvlist_free(nvl);
return (NULL);
}
nvlist_add_string(nvl, "name",
args.Get("name").ToString().Utf8Value().c_str());
}
if (args.Has("module")) {
if (!(args.Get("module").IsString())) {
Napi::TypeError::New(env,
"illegal kstat specifier (expected string)")
.ThrowAsJavaScriptException();
nvlist_free(nvl);
return (NULL);
}
nvlist_add_string(nvl, "module",
args.Get("module").ToString().Utf8Value().c_str());
}
if (args.Has("instance")) {
if (!(args.Get("instance").IsNumber())) {
Napi::TypeError::New(env,
"illegal kstat specifier (expected double)")
.ThrowAsJavaScriptException();
nvlist_free(nvl);
return (NULL);
}
nvlist_add_double(nvl, "instance",
args.Get("instance").ToNumber());
}
return (nvl);
}
/*
* Initialise our Reader class, parsing an optional object that provides a
* specific class/name/module/instance to look at.
*/
KStat::KStat(const Napi::CallbackInfo& info) : Napi::ObjectWrap<KStat>(info) {
Napi::Env env = info.Env();
nvlist_t *nvl = NULL;
if ((nvl = KStat::parse_options(info)) == NULL)
return;
if ((this->ksj = kstatjs_init(nvl)) == NULL) {
Napi::Error::New(env, kstatjs_errmsg())
.ThrowAsJavaScriptException();
return;
}
}
/*
* Based on similar functions in v8plus.
*/
/*
* Given an nvlist, create an Object and populate it with each nvpair. Note
* that this can end up being called recursively if the nvlist contains another
* nvlist, resulting in an Object containing an Object.
*/
Napi::Object
KStat::create_and_populate(Napi::Env env, nvlist_t *nvl)
{
nvpair_t *pp = NULL;
Napi::Object obj = Napi::Object::New(env);
while ((pp = nvlist_next_nvpair(nvl, pp)) != NULL) {
obj.Set(nvpair_name(pp), KStat::nvpair_to_value(env, pp));
}
return (obj);
}
#define RETURN_JS(_env, _p, _jt, _ct, _xt, _pt) \
do { \
_ct _v; \
(void) nvpair_value_##_pt(const_cast<nvpair_t *>(_p), &_v); \
return (Napi::_jt::New(_env, (_xt)_v)); \
} while (0)
/*
* Convert an nvpair value to a Napi Value.
*/
Napi::Value
KStat::nvpair_to_value(Napi::Env env, nvpair_t *pp)
{
nvlist_t *lp;
switch (nvpair_type(pp)) {
case DATA_TYPE_BOOLEAN:
return (env.Undefined());
case DATA_TYPE_BOOLEAN_VALUE:
RETURN_JS(env, pp, Boolean, boolean_t, bool, boolean_value);
case DATA_TYPE_BYTE:
return (env.Null());
case DATA_TYPE_INT8:
RETURN_JS(env, pp, Number, int8_t, double, int8);
case DATA_TYPE_UINT8:
RETURN_JS(env, pp, Number, uint8_t, double, uint8);
case DATA_TYPE_INT16:
RETURN_JS(env, pp, Number, int16_t, double, int16);
case DATA_TYPE_UINT16:
RETURN_JS(env, pp, Number, uint16_t, double, uint16);
case DATA_TYPE_INT32:
RETURN_JS(env, pp, Number, int32_t, double, int32);
case DATA_TYPE_UINT32:
RETURN_JS(env, pp, Number, uint32_t, double, uint32);
case DATA_TYPE_INT64:
RETURN_JS(env, pp, Number, int64_t, double, int64);
case DATA_TYPE_UINT64:
RETURN_JS(env, pp, Number, uint64_t, double, uint64);
case DATA_TYPE_DOUBLE:
RETURN_JS(env, pp, Number, double, double, double);
case DATA_TYPE_STRING:
RETURN_JS(env, pp, String, char *, const char *, string);
case DATA_TYPE_NVLIST:
nvpair_value_nvlist(pp, &lp);
return (KStat::create_and_populate(env, lp));
default: {
char errmsg[256];
(void) snprintf(errmsg, sizeof (errmsg),
"invalid nvpair data type %d", nvpair_type(pp));
Napi::Error::New(env, errmsg).ThrowAsJavaScriptException();
}
}
/*NOTREACHED*/
return (env.Undefined());
}
#undef RETURN_JS
Napi::Value
KStat::Read(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
Napi::Array arr;
nvlist_t *ret, *nvl = NULL;
nvpair_t *pp = NULL;
int i = 0, len = 0;
if ((nvl = KStat::parse_options(info)) == NULL)
return (env.Undefined());
if ((ret = kstatjs_read(this->ksj, nvl)) == NULL) {
Napi::Error::New(env, kstatjs_errmsg())
.ThrowAsJavaScriptException();
return (env.Undefined());
}
while ((pp = nvlist_next_nvpair(ret, pp)) != NULL)
len++;
arr = Napi::Array::New(env, len);
while ((pp = nvlist_next_nvpair(ret, pp)) != NULL) {
arr.Set(i++, KStat::nvpair_to_value(env, pp));
}
kstatjs_free(ret);
return (arr);
}
Napi::Value
KStat::Close(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
if ((kstatjs_close(this->ksj)) != 0) {
Napi::Error::New(env, kstatjs_errmsg())
.ThrowAsJavaScriptException();
return (env.Undefined());
}
return (env.Undefined());
}
Napi::Object
Init(Napi::Env env, Napi::Object exports)
{
KStat::Init(env, exports);
return (exports);
}
NODE_API_MODULE(kstat, Init)