Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Sep 18, 2024
1 parent 3501c65 commit be5e818
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 7 additions & 8 deletions quickjs/ckb_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ static JSValue syscall_spawn_cell(JSContext *ctx, JSValueConst this_value, int a
const char *str = JS_ToCString(ctx, elem);
spgs_argc += 1;
spgs_argv[i] = str;
JS_FreeValue(ctx, elem);
}
}
JS_FreeValue(ctx, val);
Expand All @@ -418,6 +419,7 @@ static JSValue syscall_spawn_cell(JSContext *ctx, JSValueConst this_value, int a
err = JS_ToUint32(ctx, &temp, elem);
CHECK(err);
spgs_fds[i] = temp;
JS_FreeValue(ctx, elem);
}
}
JS_FreeValue(ctx, val);
Expand Down Expand Up @@ -445,7 +447,6 @@ static JSValue syscall_pipe(JSContext *ctx, JSValueConst this_value, int argc, J
err = ckb_pipe(fds);
CHECK(err);
JSValue obj = JS_NewArray(ctx);
CHECK2(!JS_IsException(obj), SyscallErrorArgument);
JS_SetPropertyUint32(ctx, obj, 0, JS_NewUint32(ctx, fds[0]));
JS_SetPropertyUint32(ctx, obj, 1, JS_NewUint32(ctx, fds[1]));
exit:
Expand All @@ -458,12 +459,11 @@ static JSValue syscall_pipe(JSContext *ctx, JSValueConst this_value, int argc, J

static JSValue syscall_inherited_fds(JSContext *ctx, JSValueConst this_value, int argc, JSValueConst *argv) {
int err = 0;
uint64_t fds[2];
uint64_t fds[32];
uint64_t length;
err = ckb_inherited_fds(fds, &length);
CHECK(err);
JSValue obj = JS_NewArray(ctx);
CHECK2(!JS_IsException(obj), SyscallErrorArgument);
for (int i = 0; i < length; i++) {
JS_SetPropertyUint32(ctx, obj, i, JS_NewUint32(ctx, (uint32_t)fds[i]));
}
Expand All @@ -478,7 +478,6 @@ static JSValue syscall_inherited_fds(JSContext *ctx, JSValueConst this_value, in
static JSValue syscall_read(JSContext *ctx, JSValueConst this_value, int argc, JSValueConst *argv) {
int err = 0;
uint64_t fd = 0;
void *buffer = {};
size_t length = 0;
uint32_t u32 = 0;
err = JS_ToUint32(ctx, &u32, argv[0]);
Expand All @@ -487,7 +486,7 @@ static JSValue syscall_read(JSContext *ctx, JSValueConst this_value, int argc, J
err = JS_ToUint32(ctx, &u32, argv[1]);
CHECK(err);
length = u32;
CHECK2(length <= 1024, SyscallErrorArgument);
uint8_t *buffer = (uint8_t *)malloc(length);
err = ckb_read(fd, buffer, &length);
CHECK(err);
exit:
Expand Down Expand Up @@ -537,11 +536,11 @@ static JSValue syscall_close(JSContext *ctx, JSValueConst this_value, int argc,

static JSValue syscall_wait(JSContext *ctx, JSValueConst this_value, int argc, JSValueConst *argv) {
int err = 0;
uint32_t fd = 0;
uint32_t pid = 0;
int8_t exit = 0;
err = JS_ToUint32(ctx, &fd, argv[0]);
err = JS_ToUint32(ctx, &pid, argv[0]);
CHECK(err);
err = ckb_wait((uint64_t)fd, &exit);
err = ckb_wait((uint64_t)pid, &exit);
CHECK(err);
exit:
if (err != 0) {
Expand Down
4 changes: 4 additions & 0 deletions tests/ckb_js_tests/test_data/syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ function test_spawn() {
0x18, 0x19, 0xd5, 0x03, 0x11, 0x31, 0xa8, 0x3d, 0x4e, 0xcb, 0xcb, 0x6c, 0xba, 0x07, 0xce, 0x91
]);
let fds = ckb.pipe();
// Unlike the C version, we only need to pass in two parameters: argv and inherited_fds.
// * There is no need to use the argc parameter.
// * There is no need to add 0 to the end of inherited_fds as a terminator.
// * There is no need to pass in the pid address.
let spawn_args = {
argv: ['-e', js_code],
inherited_fds: [fds[1]],
Expand Down

0 comments on commit be5e818

Please sign in to comment.