diff options
author | 2022-11-07 14:57:47 -0800 | |
---|---|---|
committer | 2022-11-07 14:57:47 -0800 | |
commit | 8189f78eed9b904b6441b623116b962b0792ce4b (patch) | |
tree | 7c1bfb69f7da6be8fbef050f4e97eddf751722de | |
parent | 2eb19a96b11b75e65cde00d6ac1c358b30020ef6 (diff) | |
download | bun-8189f78eed9b904b6441b623116b962b0792ce4b.tar.gz bun-8189f78eed9b904b6441b623116b962b0792ce4b.tar.zst bun-8189f78eed9b904b6441b623116b962b0792ce4b.zip |
Maybe fix bug with onExit callback?
-rw-r--r-- | src/bun.js/api/bun/subprocess.zig | 24 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 11 | ||||
-rw-r--r-- | src/c.zig | 23 | ||||
-rw-r--r-- | test/bun.js/spawn.test.ts | 29 |
4 files changed, 50 insertions, 37 deletions
diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig index 4e2a0cc94..9baee4e92 100644 --- a/src/bun.js/api/bun/subprocess.zig +++ b/src/bun.js/api/bun/subprocess.zig @@ -1240,14 +1240,26 @@ pub const Subprocess = struct { this.has_waitpid_task = false; - const callback = this.on_exit_callback.swap(); - if (callback != .zero) { + if (this.on_exit_callback.trySwap()) |callback| { + const exit_value: JSValue = if (this.exit_code) |code| + JSC.JSValue.jsNumber(code) + else + JSC.JSValue.jsNumber(@as(i32, -1)); + + const waitpid_value: JSValue = + if (this.waitpid_err) |err| + err.toJSC(this.globalThis) + else + JSC.JSValue.jsUndefined(); + + const args = [_]JSValue{ + exit_value, + waitpid_value, + }; + const result = callback.call( this.globalThis, - &[_]JSValue{ - if (this.exit_code != null) JSC.JSValue.jsNumber(this.exit_code.?) else JSC.JSValue.jsNumber(@as(i32, -1)), - if (this.waitpid_err != null) this.waitpid_err.?.toJSC(this.globalThis) else JSC.JSValue.jsUndefined(), - }, + args[0 .. @as(usize, @boolToInt(this.exit_code != null)) + @as(usize, @boolToInt(this.waitpid_err != null))], ); if (result.isAnyError(this.globalThis)) { diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 813a222ef..c374ff0ec 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -2961,14 +2961,9 @@ pub const JSValue = enum(JSValueReprInt) { return switch (comptime Number) { JSValue => number, f32, f64 => jsNumberFromDouble(@as(f64, number)), - u8 => jsNumberFromChar(number), - i16, i32, c_int, i8, u16 => jsNumberFromInt32(@intCast(i32, number)), - i64 => jsNumberFromInt64(@intCast(i64, number)), - c_uint => jsNumberFromUint64(@intCast(u64, number)), - u64 => jsNumberFromUint64(@intCast(u64, number)), - u32 => if (number <= std.math.maxInt(i32)) jsNumberFromInt32(@intCast(i32, number)) else jsNumberFromUint64(@as(u64, number)), - u52 => jsNumberFromUint64(@as(u64, number)), - usize => jsNumberFromUint64(@as(u64, number)), + u8, i16, i32, c_int, i8, u16 => jsNumberFromInt32(@intCast(i32, number)), + u32, u52, c_uint, i64 => jsNumberFromInt64(@intCast(i64, number)), + usize, u64 => jsNumberFromUint64(@intCast(u64, number)), comptime_int => switch (number) { 0...std.math.maxInt(i32) => jsNumberFromInt32(@intCast(i32, number)), else => jsNumberFromInt64(@intCast(i64, number)), @@ -396,26 +396,3 @@ pub fn getRelease(buf: []u8) []const u8 { return "unknown"; } } - -// we only want these two symbols from this -const WaitH = struct { - pub usingnamespace @cImport("sys/wait.h"); -}; - -/// Return exit status. -pub const WEXITSTATUS = WaitH.WEXITSTATUS; - -/// True if child exited normally. -pub const WIFEXITED = WaitH.WIFEXITED; - -/// True if child exited due to uncaught signal. -pub const WIFSIGNALED = WaitH.WIFSIGNALED; - -/// True if child is currently stopped. -pub const WIFSTOPPED = WaitH.WIFSTOPPED; - -/// Return signal number that caused process to stop. -pub const WSTOPSIG = WaitH.WSTOPSIG; - -/// Return signal number that caused process to terminate. -pub const WTERMSIG = WaitH.WTERMSIG; diff --git a/test/bun.js/spawn.test.ts b/test/bun.js/spawn.test.ts index 24cf08e35..b3ac6ae5f 100644 --- a/test/bun.js/spawn.test.ts +++ b/test/bun.js/spawn.test.ts @@ -92,6 +92,35 @@ for (let [gcTick, label] of [ expect(exitCode2).toBe(1); }); + it("check exit code from onExit", async () => { + var exitCode1, exitCode2; + await new Promise((resolve) => { + var counter = 0; + spawn({ + cmd: ["ls"], + onExit(code) { + exitCode1 = code; + counter++; + if (counter === 2) { + resolve(); + } + }, + }); + spawn({ + cmd: ["false"], + onExit(code) { + exitCode2 = code; + counter++; + if (counter === 2) { + resolve(); + } + }, + }); + }); + expect(exitCode1).toBe(0); + expect(exitCode2).toBe(1); + }); + it("Blob works as stdin", async () => { rmSync("/tmp/out.123.txt", { force: true }); gcTick(); |