Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING(std/wasi): return exit code from start #9022

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions std/wasi/snapshot_preview1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ interface FileDescriptor {
entries?: Deno.DirEntry[];
}

export class ExitStatus {
class ExitStatus {
code: number;

constructor(code: number) {
Expand Down Expand Up @@ -1656,7 +1656,7 @@ export default class Context {
* which will be used as the address space, if it does not an error will be
* thrown.
*/
start(instance: WebAssembly.Instance) {
start(instance: WebAssembly.Instance): null | number | never {
if (this.#started) {
throw new Error("WebAssembly.Instance has already started");
}
Expand All @@ -1683,7 +1683,17 @@ export default class Context {
);
}

_start();
try {
_start();
} catch (err) {
if (err instanceof ExitStatus) {
return err.code;
}

throw err;
}

return null;
}

/**
Expand Down
24 changes: 18 additions & 6 deletions std/wasi/snapshot_preview1_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import Context, { ExitStatus } from "./snapshot_preview1.ts";
import Context from "./snapshot_preview1.ts";
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
import { copy } from "../fs/mod.ts";
import * as path from "../path/mod.ts";
Expand Down Expand Up @@ -181,11 +181,25 @@ Deno.test("context_start", function () {
"export _start must be a function",
);

try {
{
const context = new Context({
exitOnReturn: false,
});
context.start({
const exitCode = context.start({
exports: {
_start() {
},
memory: new WebAssembly.Memory({ initial: 1 }),
},
});
assertEquals(exitCode, null);
}

{
const context = new Context({
exitOnReturn: false,
});
const exitCode = context.start({
exports: {
_start() {
const exit = context.exports["proc_exit"] as CallableFunction;
Expand All @@ -194,9 +208,7 @@ Deno.test("context_start", function () {
memory: new WebAssembly.Memory({ initial: 1 }),
},
});
} catch (err) {
assert(err instanceof ExitStatus);
assertEquals(err.code, 0);
assertEquals(exitCode, 0);
}

assertThrows(
Expand Down