diff --git a/wasm-wasi-core/src/common/api.ts b/wasm-wasi-core/src/common/api.ts index db21294d..b88df077 100644 --- a/wasm-wasi-core/src/common/api.ts +++ b/wasm-wasi-core/src/common/api.ts @@ -13,6 +13,7 @@ import { MemoryFileSystem as MemoryFileSystemImpl } from './memoryFileSystemDriv import { WasiProcess as InternalWasiProcess } from './process'; import { ReadableStream, WritableStream, WritableStreamEOT } from './streams'; import { WasmPseudoterminalImpl } from './terminal'; +import { exitcode } from './wasi'; export interface Environment { [key: string]: string; @@ -386,12 +387,12 @@ export interface WasmProcess { /** * Runs the Wasm process. */ - run(): Promise; + run(): Promise; /** * Terminate the Wasm process. */ - terminate(): Promise; + terminate(exitCode?: exitcode): Promise; } export enum Filetype { diff --git a/wasm-wasi-core/src/common/process.ts b/wasm-wasi-core/src/common/process.ts index 4539d041..f2b63e6e 100644 --- a/wasm-wasi-core/src/common/process.ts +++ b/wasm-wasi-core/src/common/process.ts @@ -275,11 +275,11 @@ export abstract class WasiProcess { this._state = 'initialized'; } - public async run(): Promise { + public async run(): Promise { if (this._state !== 'initialized') { throw new Error('WasiProcess is not initialized'); } - return new Promise(async (resolve, reject) => { + return new Promise(async (resolve, reject) => { this.resolveCallback = resolve; const clock: Clock = Clock.create(); const wasiService: WasiService = Object.assign({}, @@ -298,7 +298,7 @@ export abstract class WasiProcess { protected abstract procExit(): Promise; - public abstract terminate(): Promise; + public abstract terminate(exitCode?: exitcode): Promise; protected async destroyStreams(): Promise { if (this._stdin !== undefined) { diff --git a/wasm-wasi-core/src/desktop/process.ts b/wasm-wasi-core/src/desktop/process.ts index 10e33432..1224f3d2 100644 --- a/wasm-wasi-core/src/desktop/process.ts +++ b/wasm-wasi-core/src/desktop/process.ts @@ -10,6 +10,7 @@ import { LogOutputChannel, Uri } from 'vscode'; import { ProcessOptions } from '../common/api'; import { ptr, u32 } from '../common/baseTypes'; +import type { exitcode } from '../common/wasi'; import type { ServiceMessage, StartMainMessage, StartThreadMessage, WorkerMessage } from '../common/connection'; import { WasiProcess } from '../common/process'; import RAL from '../common/ral'; @@ -111,7 +112,7 @@ export class NodeWasiProcess extends WasiProcess { await this.cleanupFileDescriptors(); } - public async terminate(): Promise { + public async terminate(exitCode?: exitcode): Promise { let result = 0; if (this.mainWorker !== undefined) { result = await this.mainWorker.terminate(); @@ -119,7 +120,7 @@ export class NodeWasiProcess extends WasiProcess { await this.cleanUpWorkers(); await this.destroyStreams(); await this.cleanupFileDescriptors(); - return result; + return exitCode ?? result; } private async cleanUpWorkers(): Promise { diff --git a/wasm-wasi-core/src/web/process.ts b/wasm-wasi-core/src/web/process.ts index 88ae970e..6edb3c07 100644 --- a/wasm-wasi-core/src/web/process.ts +++ b/wasm-wasi-core/src/web/process.ts @@ -9,6 +9,7 @@ import RAL from '../common/ral'; import { WasiProcess } from '../common/process'; import { WasiService, ServiceConnection } from '../common/service'; import type { ptr, u32 } from '../common/baseTypes'; +import type { exitcode } from '../common/wasi'; import type { ServiceMessage, StartMainMessage, StartThreadMessage, WorkerMessage } from '../common/connection'; import type { ProcessOptions } from '../common/api'; @@ -68,15 +69,14 @@ export class BrowserWasiProcess extends WasiProcess { await this.cleanupFileDescriptors(); } - public async terminate(): Promise { - const result = 0; + public async terminate(exitCode: exitcode = 0): Promise { await this.procExit(); // when terminated, web workers silently exit, and there are no events // to hook on to know when they are done. To ensure that the run promise resolves, // we call it here so callers awaiting `process.run()` will get a result. - this.resolveRunPromise(result); - return result; + this.resolveRunPromise(exitCode); + return exitCode; } protected async startMain(wasiService: WasiService): Promise { diff --git a/wasm-wasi/src/api/v1.ts b/wasm-wasi/src/api/v1.ts index 00198e0e..0a3824a5 100644 --- a/wasm-wasi/src/api/v1.ts +++ b/wasm-wasi/src/api/v1.ts @@ -10,6 +10,8 @@ import { Event, Extension, ExtensionContext, extensions as Extensions, Pseudoter import semverParse = require('semver/functions/parse'); import semverSatisfies = require('semver/functions/satisfies'); +export type exitcode = number; + export interface Environment { [key: string]: string; } @@ -397,12 +399,12 @@ export interface WasmProcess { /** * Runs the Wasm process. */ - run(): Promise; + run(): Promise; /** * Terminate the Wasm process. */ - terminate(): Promise; + terminate(exitCode?: exitcode): Promise; } export enum Filetype {