syscall-napi 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/lib/index.js +13 -1
- package/native/syscall.c +52 -2
- package/package.json +1 -1
- package/test/basic.js +6 -2
package/README.md
CHANGED
|
@@ -34,6 +34,10 @@ Supported argument types:
|
|
|
34
34
|
In case of an error the promise is rejected with an error object.
|
|
35
35
|
|
|
36
36
|
For params see `man 2 syscall`.
|
|
37
|
+
|
|
38
|
+
### `sys.syscall.sync(...params)` => `BigInt`
|
|
39
|
+
|
|
40
|
+
Same as above, but synchronous.
|
|
37
41
|
|
|
38
42
|
### `sys.__NR_xxx`
|
|
39
43
|
This module provides syscall numbers (e.g. `__NR_getpid`) that are defined in `uapi/asm-generic/unistd.h` in the linux kernel.
|
package/lib/index.js
CHANGED
|
@@ -9,4 +9,16 @@ try {
|
|
|
9
9
|
native = require("../build/Debug/syscall.node");
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
const { syscall_async, syscall_sync, ...other } = native;
|
|
13
|
+
|
|
14
|
+
const syscall = (...args) => {
|
|
15
|
+
return syscall_async(...args);
|
|
16
|
+
};
|
|
17
|
+
syscall.sync = (...args) => {
|
|
18
|
+
return syscall_sync(...args);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default {
|
|
22
|
+
syscall,
|
|
23
|
+
...other
|
|
24
|
+
};
|
package/native/syscall.c
CHANGED
|
@@ -63,7 +63,7 @@ static napi_status syscall_done(napi_env env, void* opaque, napi_deferred deferr
|
|
|
63
63
|
return napi_ok;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
static napi_status
|
|
66
|
+
static napi_status syscall_async_entry(napi_env env, napi_value* args, int arg_count, napi_value* result) {
|
|
67
67
|
int i;
|
|
68
68
|
struct syscall_ctx* ctx = (struct syscall_ctx*) malloc(sizeof(*ctx));
|
|
69
69
|
memset(ctx, 0, sizeof(*ctx));
|
|
@@ -107,6 +107,55 @@ static napi_status syscall_entry(napi_env env, napi_value* args, int arg_count,
|
|
|
107
107
|
return napi_ok;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
static napi_status syscall_sync_entry(napi_env env, napi_value* args, int arg_count, napi_value* result) {
|
|
111
|
+
int i;
|
|
112
|
+
struct syscall_ctx ctx;
|
|
113
|
+
memset(&ctx, 0, sizeof(ctx));
|
|
114
|
+
|
|
115
|
+
if(arg_count < 1) {
|
|
116
|
+
return napi_throw_error(env, NULL, "minimum required argument count for syscall() is 1");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if(arg_count > 8) {
|
|
120
|
+
return napi_throw_error(env, NULL, "maximum supported argument count for syscall() is 8");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
NAPILIB_CHECK(napilib_require_bigint_int64(env, args[0], &ctx.num));
|
|
124
|
+
|
|
125
|
+
for(i = 1; i < arg_count; i += 1) {
|
|
126
|
+
int arg_idx = i - 1;
|
|
127
|
+
bool is_buffer = 0;
|
|
128
|
+
|
|
129
|
+
NAPILIB_CHECK(napi_is_buffer(env, args[i], &is_buffer));
|
|
130
|
+
if(is_buffer) {
|
|
131
|
+
void* data;
|
|
132
|
+
size_t length;
|
|
133
|
+
|
|
134
|
+
NAPILIB_CHECK(napi_get_buffer_info(env, args[i], &data, &length));
|
|
135
|
+
ctx.native_args[arg_idx] = (long) data;
|
|
136
|
+
} else {
|
|
137
|
+
NAPILIB_CHECK(napilib_require_bigint_int64(env, args[i], &ctx.native_args[arg_idx]));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
ctx.res = syscall(ctx.num,
|
|
142
|
+
ctx.native_args[0],
|
|
143
|
+
ctx.native_args[1],
|
|
144
|
+
ctx.native_args[2],
|
|
145
|
+
ctx.native_args[3],
|
|
146
|
+
ctx.native_args[4],
|
|
147
|
+
ctx.native_args[5],
|
|
148
|
+
ctx.native_args[6]);
|
|
149
|
+
|
|
150
|
+
if(ctx.res < 0) {
|
|
151
|
+
NAPILIB_CHECK(napilib_create_error_by_errno(env, errno, result));
|
|
152
|
+
} else {
|
|
153
|
+
NAPILIB_CHECK(napi_create_bigint_int64(env, ctx.res, result));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return napi_ok;
|
|
157
|
+
}
|
|
158
|
+
|
|
110
159
|
#define DEF_SYS_CONSTANT(env, obj, name) NAPILIB_CHECK(napilib_set_named_bigint_int64_property(env, obj, #name, name))
|
|
111
160
|
|
|
112
161
|
static napi_status add_syscall_constants_to(napi_env env, napi_value target) {
|
|
@@ -385,7 +434,8 @@ static napi_status create_module_instance(napi_env env, napi_value* res) {
|
|
|
385
434
|
NAPILIB_CHECK(napi_create_object(env, &exports));
|
|
386
435
|
|
|
387
436
|
NAPILIB_CHECK(add_syscall_constants_to(env, exports));
|
|
388
|
-
NAPILIB_CHECK(napilib_set_named_simple_function_property(env, exports, "
|
|
437
|
+
NAPILIB_CHECK(napilib_set_named_simple_function_property(env, exports, "syscall_async", syscall_async_entry));
|
|
438
|
+
NAPILIB_CHECK(napilib_set_named_simple_function_property(env, exports, "syscall_sync", syscall_sync_entry));
|
|
389
439
|
|
|
390
440
|
*res = exports;
|
|
391
441
|
|
package/package.json
CHANGED
package/test/basic.js
CHANGED
|
@@ -5,9 +5,13 @@ import assert from "assert";
|
|
|
5
5
|
import sys from "../lib/index.js";
|
|
6
6
|
|
|
7
7
|
describe("basic", () => {
|
|
8
|
-
it("should run getpid() correctly", async () => {
|
|
9
|
-
// console.log("sys =", sys);
|
|
8
|
+
it("should run getpid() correctly [async]", async () => {
|
|
10
9
|
const pid = await sys.syscall(sys.__NR_getpid);
|
|
11
10
|
assert.equal(pid, process.pid);
|
|
12
11
|
});
|
|
12
|
+
|
|
13
|
+
it("should run getpid() correctly [sync]", () => {
|
|
14
|
+
const pid = sys.syscall.sync(sys.__NR_getpid);
|
|
15
|
+
assert.equal(pid, process.pid);
|
|
16
|
+
});
|
|
13
17
|
});
|