Skip to content

Commit

Permalink
[wasm] simplify startup configuration (#61072)
Browse files Browse the repository at this point in the history
* added configSrc - path to MonoConfig json file
* added Module.onConfigLoaded callback
* added Module.onDotNetReady replacing MonoConfig.loaded_cb
* removed loaded_cb, assembly_list, runtime_assets, runtime_asset_sources from MonoConfig
* simplified all sample and test projects
* renamed no_global_exports to disableDotNet6Compatibility
* implement default Module.preInit and Module.onRuntimeInitialized which could be overriden by user code (like Blazor)

* re-enable the interp lane
  • Loading branch information
pavelsavara committed Nov 3, 2021
1 parent 6527f54 commit 3950381
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 316 deletions.
3 changes: 1 addition & 2 deletions eng/pipelines/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ jobs:
jobParameters:
testGroup: innerloop
nameSuffix: AllSubsets_Mono
buildArgs: -s mono+libs+host+packs+libs.tests -c $(_BuildConfig) /p:ArchiveTests=true $(_runSmokeTestsOnlyArg)
buildArgs: -s mono+libs+host+packs+libs.tests -c $(_BuildConfig) /p:ArchiveTests=true
timeoutInMinutes: 180
condition: >-
or(
Expand All @@ -306,7 +306,6 @@ jobs:
extraStepsParameters:
creator: dotnet-bot
testRunNamePrefixSuffix: Mono_$(_BuildConfig)
extraHelixArguments: $(_runSmokeTestsOnlyArg)
scenarios:
- normal
- wasmtestonbrowser
Expand Down
27 changes: 5 additions & 22 deletions src/mono/sample/mbr/browser/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,11 @@
"use strict";
var Module = {
config: null,

preInit: async function () {
await MONO.mono_wasm_load_config("./mono-config.json"); // sets MONO.config implicitly
configSrc: "./mono-config.json",
onConfigLoaded: function () {
MONO.config.environment_variables["DOTNET_MODIFIABLE_ASSEMBLIES"] = "debug";
},

// Called when the runtime is initialized and wasm is ready
onRuntimeInitialized: function () {
if (!MONO.config || MONO.config.error) {
console.log("An error occured while loading the config file");
return;
}

MONO.config.loaded_cb = function () {
App.init();
};
MONO.config.environment_variables = {
"DOTNET_MODIFIABLE_ASSEMBLIES": "debug"
};
MONO.config.fetch_file_cb = function (asset) {
return fetch(asset, { credentials: 'same-origin' });
}

MONO.mono_load_runtime_and_bcl_args(MONO.config);
onDotNetReady: function () {
App.init();
},
};
35 changes: 9 additions & 26 deletions src/mono/sample/wasm/browser-bench/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,25 @@
"use strict";
var Module = {
config: null,

preInit: async function () {
await MONO.mono_wasm_load_config("./mono-config.json"); // sets MONO.config implicitly
},

// Called when the runtime is initialized and wasm is ready
onRuntimeInitialized: function () {
if (!MONO.config || MONO.config.error) {
console.log("An error occured while loading the config file");
return;
}

MONO.config.loaded_cb = function () {
try {
App.init();
} catch (error) {
test_exit(1);
throw (error);
}
};
MONO.config.fetch_file_cb = function (asset) {
return fetch(asset, { credentials: 'same-origin' });
}

configSrc: "./mono-config.json",
onConfigLoaded: function () {
if (MONO.config.enable_profiler) {
MONO.config.aot_profiler_options = {
write_at: "Sample.Test::StopProfile",
send_to: "System.Runtime.InteropServices.JavaScript.Runtime::DumpAotProfileData"
}
}

},
onDotNetReady: function () {
try {
MONO.mono_load_runtime_and_bcl_args(MONO.config);
App.init();
} catch (error) {
test_exit(1);
throw (error);
}
},
onAbort: function (err) {
test_exit(1);

},
};
42 changes: 9 additions & 33 deletions src/mono/sample/wasm/browser-profile/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,26 @@
var Module = {
is_testing: false,
config: null,

preInit: async function () {
await MONO.mono_wasm_load_config("./mono-config.json"); // sets MONO.config implicitly
},

// Called when the runtime is initialized and wasm is ready
onRuntimeInitialized: function () {
if (!MONO.config || MONO.config.error) {
console.log("An error occured while loading the config file");
return;
}

MONO.config.loaded_cb = function () {
try {
Module.init();
} catch (error) {
Module.test_exit(1);
throw (error);
}
};
MONO.config.fetch_file_cb = function (asset) {
return fetch(asset, { credentials: 'same-origin' });
}

configSrc: "./mono-config.json",
onConfigLoaded: function () {
if (MONO.config.enable_profiler) {
MONO.config.aot_profiler_options = {
write_at: "Sample.Test::StopProfile",
send_to: "System.Runtime.InteropServices.JavaScript.Runtime::DumpAotProfileData"
}
}

},
onDotNetReady: function () {
try {
MONO.mono_load_runtime_and_bcl_args(MONO.config);
Module.init();
} catch (error) {
Module.test_exit(1);
test_exit(1);
throw (error);
}
},
onAbort: function (err) {
test_exit(1);
},

init: function () {
console.log("not ready yet")
Expand All @@ -62,12 +44,6 @@ var Module = {
}
},

onLoad: function () {
const url = new URL(decodeURI(window.location));
const args = url.searchParams.getAll('arg');
Module.is_testing = args !== undefined && (args.find(arg => arg == '--testing') !== undefined);
},

test_exit: function (exit_code) {
if (!Module.is_testing) {
console.log(`test_exit: ${exit_code}`);
Expand Down
33 changes: 7 additions & 26 deletions src/mono/sample/wasm/browser/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,18 @@

"use strict";
var Module = {

config: null,

preInit: async function () {
await MONO.mono_wasm_load_config("./mono-config.json"); // sets MONO.config implicitly
},

// Called when the runtime is initialized and wasm is ready
onRuntimeInitialized: function () {
if (!MONO.config || MONO.config.error) {
console.log("No config found");
return;
}

MONO.config.loaded_cb = function () {
try {
App.init();
} catch (error) {
test_exit(1);
throw (error);
}
};
MONO.config.fetch_file_cb = function (asset) {
return fetch(asset, { credentials: 'same-origin' });
}

configSrc: "./mono-config.json",
onDotNetReady: function () {
try {
MONO.mono_load_runtime_and_bcl_args(MONO.config);
App.init();
} catch (error) {
test_exit(1);
throw (error);
}
},
onAbort: function () {
test_exit(1);

},
};
34 changes: 10 additions & 24 deletions src/mono/wasm/debugger/tests/debugger-test/runtime-debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,20 @@

var Module = {
config: null,

preInit: async function () {
await MONO.mono_wasm_load_config("./mono-config.json"); // sets MONO.config implicitly
},

// Called when the runtime is initialized and wasm is ready
onRuntimeInitialized: function () {
if (!MONO.config || MONO.config.error) {
console.log("An error occured while loading the config file");
return;
}

MONO.config.loaded_cb = function () {
App.init();
};
configSrc: "./mono-config.json",
onConfigLoaded: function () {
MONO.config.environment_variables["DOTNET_MODIFIABLE_ASSEMBLIES"] = "debug";
// For custom logging patch the functions below
/*
MONO.config.environment_variables["MONO_LOG_LEVEL"] = "debug";
MONO.config.environment_variables["MONO_LOG_MASK"] = "all";
INTERNAL.logging = {
trace: function (domain, log_level, message, isFatal, dataPtr) {},
debugger: function (level, message) {}
trace: function (domain, log_level, message, isFatal, dataPtr) { },
debugger: function (level, message) { }
};
MONO.mono_wasm_setenv ("MONO_LOG_LEVEL", "debug");
MONO.mono_wasm_setenv ("MONO_LOG_MASK", "all");
*/

MONO.config.environment_variables = {
"DOTNET_MODIFIABLE_ASSEMBLIES": "debug"
};
MONO.mono_load_runtime_and_bcl_args(MONO.config)
},
onDotNetReady: function () {
App.init();
},
};
54 changes: 19 additions & 35 deletions src/mono/wasm/runtime-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,56 +106,40 @@ var Module = {
no_global_exports: true,
mainScriptUrlOrBlob: "dotnet.js",
config: null,
configSrc: "./mono-config.json",
print: console.log,
printErr: console.error,
/** Called before the runtime is loaded and before it is run
* @type {() => Promise<void>}
*/
preInit: async function () {
await MONO.mono_wasm_load_config("./mono-config.json"); // sets Module.config implicitly
},

/** Called after an exception occurs during execution
* @type {(x: string|number=) => void}
* @param {string|number} x error message
*/
onAbort: function (x) {
console.log("ABORT: " + x);
const err = new Error();
console.log("Stacktrace: \n");
console.error(err.stack);
fail_exec(1);
},

/** Called after the runtime is loaded but before it is run mostly prepares runtime and config for the tests
* @type {() => void}
*/
onRuntimeInitialized: function () {
onConfigLoaded: function () {
if (!Module.config) {
console.error("Could not find ./mono-config.json. Cancelling run");
fail_exec(1);
}
// Have to set env vars here to enable setting MONO_LOG_LEVEL etc.
for (let variable in processedArguments.setenv) {
MONO.mono_wasm_setenv(variable, processedArguments.setenv[variable]);
Module.config.environment_variables[variable] = processedArguments.setenv[variable];
}

if (!processedArguments.enable_gc) {
INTERNAL.mono_wasm_enable_on_demand_gc(0);
}
},
onDotNetReady: function () {
let wds = Module.FS.stat(processedArguments.working_dir);
if (wds === undefined || !Module.FS.isDir(wds.mode)) {
fail_exec(1, `Could not find working directory ${processedArguments.working_dir}`);
return;
}

Module.config.loaded_cb = function () {
let wds = Module.FS.stat(processedArguments.working_dir);
if (wds === undefined || !Module.FS.isDir(wds.mode)) {
fail_exec(1, `Could not find working directory ${processedArguments.working_dir}`);
return;
}

Module.FS.chdir(processedArguments.working_dir);
App.init();
};
Module.FS.chdir(processedArguments.working_dir);

MONO.mono_load_runtime_and_bcl_args(Module.config);
App.init();
},
onAbort: function (x) {
console.log("ABORT: " + x);
const err = new Error();
console.log("Stacktrace: \n");
console.error(err.stack);
fail_exec(1);
},
};

Expand Down
27 changes: 17 additions & 10 deletions src/mono/wasm/runtime/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import {
mono_wasm_fire_debugger_agent_message,
} from "./debug";
import { runtimeHelpers, setLegacyModules } from "./modules";
import { MonoArray, MonoConfig, MonoConfigError, MonoObject } from "./types";
import { EmscriptenModuleMono, MonoArray, MonoConfig, MonoConfigError, MonoObject } from "./types";
import {
mono_load_runtime_and_bcl_args, mono_wasm_load_config,
mono_wasm_setenv, mono_wasm_set_runtime_options,
mono_wasm_load_data_archive, mono_wasm_asm_loaded,
mono_wasm_set_main_args
mono_wasm_set_main_args,
mono_wasm_pre_init,
mono_wasm_on_runtime_initialized
} from "./startup";
import { mono_set_timeout, schedule_background_exec } from "./scheduling";
import { mono_wasm_load_icu_data, mono_wasm_get_icudt_name } from "./icu";
Expand Down Expand Up @@ -108,6 +110,8 @@ export const BINDING: BINDING = <any>{
// 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 export_to_emscripten(dotnet: any, mono: any, binding: any, internal: any, module: any): void {
const moduleExt = module as EmscriptenModuleMono;

// we want to have same instance of MONO, BINDING and Module in dotnet iffe
setLegacyModules(dotnet, mono, binding, internal, module);

Expand All @@ -117,17 +121,20 @@ function export_to_emscripten(dotnet: any, mono: any, binding: any, internal: an
Object.assign(binding, BINDING);
Object.assign(internal, INTERNAL);

// backward compatibility, sync with EmscriptenModuleMono
Object.assign(module, {
// https://github.com/search?q=mono_bind_static_method&type=Code
mono_bind_static_method: (fqn: string, signature: ArgsMarshalString): Function => {
// this could be overriden on Module
moduleExt.preInit = mono_wasm_pre_init;
moduleExt.onRuntimeInitialized = mono_wasm_on_runtime_initialized;

if (!moduleExt.disableDotNet6Compatibility) {
// backward compatibility
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
moduleExt.mono_bind_static_method = (fqn: string, signature: ArgsMarshalString): Function => {
console.warn("Module.mono_bind_static_method is obsolete, please use BINDING.bind_static_method instead");
return mono_bind_static_method(fqn, signature);
},
});
};

// here we expose objects used in tests to global namespace
if (!module.no_global_exports) {
// here we expose objects used in tests to global namespace
(<any>globalThis).Module = module;
const warnWrap = (name: string, value: any) => {
if (typeof ((<any>globalThis)[name]) !== "undefined") {
Expand Down
Loading

0 comments on commit 3950381

Please sign in to comment.