aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/bun-types/bun.d.ts33
-rw-r--r--src/bun.js/api/bun/subprocess.zig12
2 files changed, 40 insertions, 5 deletions
diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts
index ccdcf2c2c..c9ea2fb66 100644
--- a/packages/bun-types/bun.d.ts
+++ b/packages/bun-types/bun.d.ts
@@ -1,3 +1,5 @@
+import { SignalConstants } from "os";
+
interface VoidFunction {
(): void;
}
@@ -2617,13 +2619,21 @@ declare module "bun" {
* ```ts
* const subprocess = spawn({
* cmd: ["echo", "hello"],
- * onExit: (code) => {
+ * onExit: (subprocess, code) => {
* console.log(`Process exited with code ${code}`);
* },
* });
* ```
*/
- onExit?: (exitCode: number) => void | Promise<void>;
+ onExit?(
+ subprocess: Subprocess,
+ exitCode: number | null,
+ signalCode: number | null,
+ /**
+ * If an error occured in the call to waitpid2, this will be the error.
+ */
+ error?: Errorlike,
+ ): void | Promise<void>;
}
}
@@ -2661,6 +2671,25 @@ declare module "bun" {
readonly exited: Promise<number>;
/**
+ * Synchronously get the exit code of the process
+ *
+ * If the process hasn't exited yet, this will return `null`
+ */
+ readonly exitCode: number | null;
+
+ /**
+ * Synchronously get the signal code of the process
+ *
+ * If the process never sent a signal code, this will return `null`
+ *
+ * To receive signal code changes, use the `onExit` callback.
+ *
+ * If the signal code is unknown, it will return the original signal code
+ * number, but that case should essentially never happen.
+ */
+ readonly signalCode: Signals | null;
+
+ /**
* Has the process exited?
*/
readonly killed: boolean;
diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig
index fe09746eb..53f0da03e 100644
--- a/src/bun.js/api/bun/subprocess.zig
+++ b/src/bun.js/api/bun/subprocess.zig
@@ -1571,14 +1571,20 @@ pub const Subprocess = struct {
else
JSC.JSValue.jsUndefined();
+ const this_value = if (this_jsvalue.isEmptyOrUndefinedOrNull()) JSC.JSValue.jsUndefined() else this_jsvalue;
+ this_value.ensureStillAlive();
+
const args = [_]JSValue{
- exit_value,
+ this_value,
+ this.getExitCode(globalThis),
+ this.getSignalCode(globalThis),
waitpid_value,
};
- const result = callback.call(
+ const result = callback.callWithThis(
globalThis,
- args[0 .. @as(usize, @boolToInt(this.exit_code != null)) + @as(usize, @boolToInt(this.waitpid_err != null))],
+ this_value,
+ &args,
);
if (result.isAnyError(globalThis)) {