Skip to content

[wasm] pass ExitStatus when calling quit_ #64734

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

Merged
merged 8 commits into from
Feb 4, 2022
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
4 changes: 0 additions & 4 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console-node-ts\Wasm.Console.Node.TS.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\browser-webpack\Wasm.Browser.WebPack.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\browser-nextjs\Wasm.Browser.NextJs.Sample.csproj" />

<!-- https://github.com/dotnet/runtime/issues/64727 -->
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console-node-cjs\Wasm.Console.Node.CJS.Sample.csproj" />
<ProjectExclusions Include="$(MonoProjectRoot)sample\wasm\console-node-es6\Wasm.Console.Node.ES6.Sample.csproj" />
</ItemGroup>

<!-- Wasm aot on !windows -->
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ const DotnetSupportLib = {
$DOTNET__postset: `
let __dotnet_replacements = {readAsync, fetch: globalThis.fetch, require};
let __dotnet_exportedAPI = __dotnet_runtime.__initializeImportsAndExports(
{ isESM:false, isGlobal:ENVIRONMENT_IS_GLOBAL, isNode:ENVIRONMENT_IS_NODE, isShell:ENVIRONMENT_IS_SHELL, isWeb:ENVIRONMENT_IS_WEB, locateFile, quit_, requirePromise:Promise.resolve(require)},
{ isESM:false, isGlobal:ENVIRONMENT_IS_GLOBAL, isNode:ENVIRONMENT_IS_NODE, isShell:ENVIRONMENT_IS_SHELL, isWeb:ENVIRONMENT_IS_WEB, locateFile, quit_, ExitStatus, requirePromise:Promise.resolve(require)},
{ mono:MONO, binding:BINDING, internal:INTERNAL, module:Module },
__dotnet_replacements);
readAsync = __dotnet_replacements.readAsync;
var fetch = __dotnet_replacements.fetch;
require = __dotnet_replacements.requireOut;
var noExitRuntime = __dotnet_replacements.noExitRuntime;
`,
};

Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ if (ENVIRONMENT_IS_NODE) {
}
}
let __dotnet_exportedAPI = __dotnet_runtime.__initializeImportsAndExports(
{ isESM:true, isGlobal:false, isNode:ENVIRONMENT_IS_NODE, isShell:ENVIRONMENT_IS_SHELL, isWeb:ENVIRONMENT_IS_WEB, locateFile, quit_, requirePromise:__dotnet_replacements.requirePromise },
{ isESM:true, isGlobal:false, isNode:ENVIRONMENT_IS_NODE, isShell:ENVIRONMENT_IS_SHELL, isWeb:ENVIRONMENT_IS_WEB, locateFile, quit_, ExitStatus, requirePromise:__dotnet_replacements.requirePromise },
{ mono:MONO, binding:BINDING, internal:INTERNAL, module:Module },
__dotnet_replacements);
readAsync = __dotnet_replacements.readAsync;
var fetch = __dotnet_replacements.fetch;
require = __dotnet_replacements.requireOut;
var noExitRuntime = __dotnet_replacements.noExitRuntime;
`,
};

Expand Down
8 changes: 5 additions & 3 deletions src/mono/wasm/runtime/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
mono_wasm_add_dbg_command_received,
mono_wasm_change_debugger_log_level,
} from "./debug";
import { runtimeHelpers, setImportsAndExports } from "./imports";
import { ENVIRONMENT_IS_WEB, ExitStatusError, runtimeHelpers, setImportsAndExports } from "./imports";
import { DotnetModuleConfigImports, DotnetModule } from "./types";
import {
mono_load_runtime_and_bcl_args, mono_wasm_load_config,
Expand Down Expand Up @@ -132,9 +132,9 @@ let exportedAPI: DotnetPublicAPI;
// it exports methods to global objects MONO, BINDING and Module in backward compatible way
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function initializeImportsAndExports(
imports: { isESM: boolean, isGlobal: boolean, isNode: boolean, isShell: boolean, isWeb: boolean, locateFile: Function, quit_: Function, requirePromise: Promise<Function> },
imports: { isESM: boolean, isGlobal: boolean, isNode: boolean, isShell: boolean, isWeb: boolean, locateFile: Function, quit_: Function, ExitStatus: ExitStatusError, requirePromise: Promise<Function> },
exports: { mono: any, binding: any, internal: any, module: any },
replacements: { fetch: any, readAsync: any, require: any, requireOut: any },
replacements: { fetch: any, readAsync: any, require: any, requireOut: any, noExitRuntime: boolean },
): DotnetPublicAPI {
const module = exports.module as DotnetModule;
const globalThisAny = globalThis as any;
Expand Down Expand Up @@ -192,6 +192,8 @@ function initializeImportsAndExports(
replacements.readAsync = readAsync_like;
replacements.requireOut = module.imports.require;

replacements.noExitRuntime = ENVIRONMENT_IS_WEB;

if (typeof module.disableDotnet6Compatibility === "undefined") {
module.disableDotnet6Compatibility = imports.isESM;
}
Expand Down
8 changes: 7 additions & 1 deletion src/mono/wasm/runtime/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ export let ENVIRONMENT_IS_SHELL: boolean;
export let ENVIRONMENT_IS_WEB: boolean;
export let locateFile: Function;
export let quit: Function;
export let ExitStatus: ExitStatusError;
export let requirePromise: Promise<Function>;

export interface ExitStatusError {
new(status: number): any;
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function setImportsAndExports(
imports: { isESM: boolean, isNode: boolean, isShell: boolean, isWeb: boolean, locateFile: Function, quit_: Function, requirePromise: Promise<Function> },
imports: { isESM: boolean, isNode: boolean, isShell: boolean, isWeb: boolean, locateFile: Function, ExitStatus: ExitStatusError, quit_: Function, requirePromise: Promise<Function> },
exports: { mono: any, binding: any, internal: any, module: any },
): void {
MONO = exports.mono;
Expand All @@ -37,6 +42,7 @@ export function setImportsAndExports(
ENVIRONMENT_IS_WEB = imports.isWeb;
locateFile = imports.locateFile;
quit = imports.quit_;
ExitStatus = imports.ExitStatus;
requirePromise = imports.requirePromise;
}

Expand Down
10 changes: 8 additions & 2 deletions src/mono/wasm/runtime/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module, quit } from "./imports";
import { ExitStatus, Module, quit } from "./imports";
import { mono_call_assembly_entry_point } from "./method-calls";
import { mono_wasm_set_main_args, runtime_is_initialized_reject } from "./startup";

Expand All @@ -8,6 +8,9 @@ export async function mono_run_main_and_exit(main_assembly_name: string, args: s
const result = await mono_run_main(main_assembly_name, args);
set_exit_code(result);
} catch (error) {
if (error instanceof ExitStatus) {
return;
}
set_exit_code(1, error);
}
}
Expand All @@ -24,11 +27,14 @@ export function mono_on_abort(error: any): void {
}

function set_exit_code(exit_code: number, reason?: any) {
if (reason) {
if (reason && !(reason instanceof ExitStatus)) {
Module.printErr(reason.toString());
if (reason.stack) {
Module.printErr(reason.stack);
}
}
else {
reason = new ExitStatus(exit_code);
}
quit(exit_code, reason);
}
8 changes: 4 additions & 4 deletions src/mono/wasm/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ loadDotnet("./dotnet.js").then((createDotnetRuntime) => {
onConfigLoaded: (config) => {
if (!Module.config) {
const err = new Error("Could not find ./mono-config.json. Cancelling run");
set_exit_code(1,);
set_exit_code(1);
throw err;
}
// Have to set env vars here to enable setting MONO_LOG_LEVEL etc.
Expand Down Expand Up @@ -214,7 +214,9 @@ const App = {
const result = await MONO.mono_run_main(main_assembly_name, app_args);
set_exit_code(result);
} catch (error) {
set_exit_code(1, error);
if (error.name != "ExitStatus") {
set_exit_code(1, error);
}
}
} else {
set_exit_code(1, "Unhandled argument: " + processedArguments.applicationArgs[0]);
Expand Down Expand Up @@ -272,8 +274,6 @@ function set_exit_code(exit_code, reason) {
};
stop_when_ws_buffer_empty();

} else if (is_node) {
process.exit(exit_code);
} else if (App && App.INTERNAL) {
App.INTERNAL.mono_wasm_exit(exit_code);
}
Expand Down