Skip to content

Commit

Permalink
feat: Return exit code from WASI.start()
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Dec 5, 2023
1 parent 9289c4c commit f3c5567
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ export interface Options {
debug?: boolean;
}

/**
* An exception that is thrown when the process exits.
**/
export class WASIProcExit extends Error {
constructor(public readonly code: number) {
super("exit with exit code " + code);
}
}

export default class WASI {
args: Array<string> = [];
env: Array<string> = [];
Expand All @@ -19,7 +28,15 @@ export default class WASI {
exports: { memory: WebAssembly.Memory; _start: () => unknown };
}) {
this.inst = instance;
instance.exports._start();
try {
instance.exports._start();
} catch (e) {
if (e instanceof WASIProcExit) {
return e.code;
} else {
throw e;
}
}
}

/// Initialize a WASI reactor
Expand Down Expand Up @@ -695,7 +712,7 @@ export default class WASI {
throw "async io not supported";
},
proc_exit(exit_code: number) {
throw "exit with exit code " + exit_code;
throw new WASIProcExit(exit_code);
},
proc_raise(sig: number) {
throw "raised signal " + sig;
Expand Down

0 comments on commit f3c5567

Please sign in to comment.