From 391b0a405ae5df1c769cdde518da9a8de5e17665 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Wed, 6 Jan 2021 18:29:55 +0800 Subject: [PATCH] BREAKING(wasi): return exit code from start (denoland/deno#9022) This returns the exit code directly from the start entry point instead of throwing it and letting the user handle it. As a result the exit status is an implementation detail and has been made internal. --- wasi/snapshot_preview1.ts | 16 +++++++++++++--- wasi/snapshot_preview1_test.ts | 24 ++++++++++++++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/wasi/snapshot_preview1.ts b/wasi/snapshot_preview1.ts index 1a17d1a3d961..d4caa97bce6e 100644 --- a/wasi/snapshot_preview1.ts +++ b/wasi/snapshot_preview1.ts @@ -270,7 +270,7 @@ interface FileDescriptor { entries?: Deno.DirEntry[]; } -export class ExitStatus { +class ExitStatus { code: number; constructor(code: number) { @@ -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"); } @@ -1683,7 +1683,17 @@ export default class Context { ); } - _start(); + try { + _start(); + } catch (err) { + if (err instanceof ExitStatus) { + return err.code; + } + + throw err; + } + + return null; } /** diff --git a/wasi/snapshot_preview1_test.ts b/wasi/snapshot_preview1_test.ts index 1fe94a4cc766..073c30aab20b 100644 --- a/wasi/snapshot_preview1_test.ts +++ b/wasi/snapshot_preview1_test.ts @@ -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"; @@ -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; @@ -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(