Skip to content

Commit

Permalink
- allow Module.config to have assets with buffer instead of url
Browse files Browse the repository at this point in the history
- change sample to show how emscripten callbacks are triggered
- pass config into onConfigLoaded and made it async
- split asset download from copying it into memory
- moved startup sequence to configure_emscripten_startup and documented it there
  • Loading branch information
pavelsavara committed Jan 11, 2022
1 parent fe8422c commit c4d9f40
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 93 deletions.
16 changes: 12 additions & 4 deletions src/mono/sample/wasm/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ async function loadRuntime() {
async function main() {
try {
const createDotnetRuntime = await loadRuntime();
const { MONO, BINDING, Module, RuntimeBuildInfo } = await createDotnetRuntime(() => ({
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
}));
const { MONO, BINDING, Module, RuntimeBuildInfo } = await createDotnetRuntime(() => {
console.log('user code in createDotnetRuntime')
return {
disableDotnet6Compatibility: true,
configSrc: "./mono-config.json",
preInit: () => { console.log('user code Module.preInit') },
preRun: () => { console.log('user code Module.preRun') },
onRuntimeInitialized: () => { console.log('user code Module.onRuntimeInitialized') },
postRun: () => { console.log('user code Module.postRun') },
}
});
console.log('after createDotnetRuntime')

const testMeaning = BINDING.bind_static_method("[Wasm.Browser.CJS.Sample] Sample.Test:TestMeaning");
const ret = testMeaning();
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ declare type AssetEntry = {
culture?: string;
load_remote?: boolean;
is_optional?: boolean;
buffer?: ArrayBuffer;
};
interface AssemblyEntry extends AssetEntry {
name: "assembly";
Expand Down Expand Up @@ -196,7 +197,7 @@ declare type DotnetModuleConfig = {
config?: MonoConfig | MonoConfigError;
configSrc?: string;
scriptDirectory?: string;
onConfigLoaded?: () => void;
onConfigLoaded?: (config: MonoConfig) => Promise<void>;
onDotnetReady?: () => void;
imports?: DotnetModuleConfigImports;
} & Partial<EmscriptenModule>;
Expand Down
51 changes: 4 additions & 47 deletions src/mono/wasm/runtime/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ 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_pre_init,
mono_wasm_runtime_is_initialized,
mono_wasm_on_runtime_initialized
configure_emscripten_startup
} 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 @@ -66,7 +64,7 @@ import {
import { create_weak_ref } from "./weak-ref";
import { fetch_like, readAsync_like } from "./polyfills";
import { EmscriptenModule } from "./types/emscripten";
import { mono_on_abort, mono_run_main, mono_run_main_and_exit } from "./run";
import { mono_run_main, mono_run_main_and_exit } from "./run";

const MONO = {
// current "public" MONO API
Expand Down Expand Up @@ -163,18 +161,6 @@ function initializeImportsAndExports(
module.configSrc = "./mono-config.json";
}

// these could be overriden on DotnetModuleConfig
if (!module.preInit) {
module.preInit = [];
} else if (typeof module.preInit === "function") {
module.preInit = [module.preInit];
}
if (!module.preRun) {
module.preRun = [];
} else if (typeof module.preRun === "function") {
module.preRun = [module.preRun];
}

if (!module.print) {
module.print = console.log.bind(console);
}
Expand Down Expand Up @@ -254,37 +240,6 @@ function initializeImportsAndExports(
warnWrap("removeRunDependency", () => module.removeRunDependency);
}

// this is registration of the runtime pre_init, when user set configSrc
if (module.configSrc) {
module.preInit.push(async () => {
module.addRunDependency("mono_wasm_pre_init");
// execution order == [0] ==
await mono_wasm_pre_init();
module.removeRunDependency("mono_wasm_pre_init");
});
}

// if onRuntimeInitialized is set it's probably Blazor, we let them to do their own init sequence
if (!module.onRuntimeInitialized) {
// note this would keep running in async-parallel with emscripten's `run()` and `postRun()`
// because it's loading files asynchronously and the emscripten is not awaiting onRuntimeInitialized
// execution order == [1] ==
module.onRuntimeInitialized = () => mono_wasm_on_runtime_initialized();

module.ready = module.ready.then(async () => {
// mono_wasm_runtime_is_initialized is set when finalize_startup is done
await mono_wasm_runtime_is_initialized;
// TODO we could take over Module.postRun and call it from here if necessary

// execution order == [2] ==
return exportedAPI;
});
}

if (!module.onAbort) {
module.onAbort = () => mono_on_abort;
}

// this code makes it possible to find dotnet runtime on a page via global namespace, even when there are multiple runtimes at the same time
let list: RuntimeList;
if (!globalThisAny.getDotnetRuntime) {
Expand All @@ -296,6 +251,8 @@ function initializeImportsAndExports(
}
list.registerRuntime(exportedAPI);

configure_emscripten_startup(module, exportedAPI);

return exportedAPI;
}

Expand Down
Loading

0 comments on commit c4d9f40

Please sign in to comment.