Skip to content
Open
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
5 changes: 3 additions & 2 deletions wasm-wasi-core/src/common/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -386,12 +387,12 @@ export interface WasmProcess {
/**
* Runs the Wasm process.
*/
run(): Promise<number>;
run(): Promise<exitcode>;

/**
* Terminate the Wasm process.
*/
terminate(): Promise<number>;
terminate(exitCode?: exitcode): Promise<exitcode>;
}

export enum Filetype {
Expand Down
6 changes: 3 additions & 3 deletions wasm-wasi-core/src/common/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ export abstract class WasiProcess {
this._state = 'initialized';
}

public async run(): Promise<number> {
public async run(): Promise<exitcode> {
if (this._state !== 'initialized') {
throw new Error('WasiProcess is not initialized');
}
return new Promise<number>(async (resolve, reject) => {
return new Promise<exitcode>(async (resolve, reject) => {
this.resolveCallback = resolve;
const clock: Clock = Clock.create();
const wasiService: WasiService = Object.assign({},
Expand All @@ -298,7 +298,7 @@ export abstract class WasiProcess {

protected abstract procExit(): Promise<void>;

public abstract terminate(): Promise<number>;
public abstract terminate(exitCode?: exitcode): Promise<exitcode>;

protected async destroyStreams(): Promise<void> {
if (this._stdin !== undefined) {
Expand Down
5 changes: 3 additions & 2 deletions wasm-wasi-core/src/desktop/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -111,15 +112,15 @@ export class NodeWasiProcess extends WasiProcess {
await this.cleanupFileDescriptors();
}

public async terminate(): Promise<number> {
public async terminate(exitCode?: exitcode): Promise<exitcode> {
let result = 0;
if (this.mainWorker !== undefined) {
result = await this.mainWorker.terminate();
}
await this.cleanUpWorkers();
await this.destroyStreams();
await this.cleanupFileDescriptors();
return result;
return exitCode ?? result;
}

private async cleanUpWorkers(): Promise<void> {
Expand Down
8 changes: 4 additions & 4 deletions wasm-wasi-core/src/web/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -68,15 +69,14 @@ export class BrowserWasiProcess extends WasiProcess {
await this.cleanupFileDescriptors();
}

public async terminate(): Promise<number> {
const result = 0;
public async terminate(exitCode: exitcode = 0): Promise<exitcode> {
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<void> {
Expand Down
6 changes: 4 additions & 2 deletions wasm-wasi/src/api/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -397,12 +399,12 @@ export interface WasmProcess {
/**
* Runs the Wasm process.
*/
run(): Promise<number>;
run(): Promise<exitcode>;

/**
* Terminate the Wasm process.
*/
terminate(): Promise<number>;
terminate(exitCode?: exitcode): Promise<exitcode>;
}

export enum Filetype {
Expand Down