From 5f3ae152140e626bc8535da34f3f5e148dd1fa24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Laban?= Date: Tue, 29 Mar 2022 10:57:42 -0400 Subject: [PATCH] feat: Add support for emscripten module exports (#66868) --- src/mono/wasm/runtime/dotnet.d.ts | 1 + src/mono/wasm/runtime/startup.ts | 19 +++++++++++++++++++ src/mono/wasm/runtime/types.ts | 1 + 3 files changed, 21 insertions(+) diff --git a/src/mono/wasm/runtime/dotnet.d.ts b/src/mono/wasm/runtime/dotnet.d.ts index d6a86af0a32..617b572e757 100644 --- a/src/mono/wasm/runtime/dotnet.d.ts +++ b/src/mono/wasm/runtime/dotnet.d.ts @@ -197,6 +197,7 @@ declare type DotnetModuleConfig = { onConfigLoaded?: (config: MonoConfig) => Promise; onDotnetReady?: () => void; imports?: DotnetModuleConfigImports; + exports?: string[]; } & Partial; declare type DotnetModuleConfigImports = { require?: (name: string) => any; diff --git a/src/mono/wasm/runtime/startup.ts b/src/mono/wasm/runtime/startup.ts index 9112a35defb..aa10c272496 100644 --- a/src/mono/wasm/runtime/startup.ts +++ b/src/mono/wasm/runtime/startup.ts @@ -266,6 +266,8 @@ function _apply_configuration_from_args(config: MonoConfig) { } function finalize_startup(config: MonoConfig | MonoConfigError | undefined): void { + const globalThisAny = globalThis as any; + try { if (!config || config.isError) { return; @@ -276,6 +278,23 @@ function finalize_startup(config: MonoConfig | MonoConfigError | undefined): voi const moduleExt = Module as DotnetModule; + if(!Module.disableDotnet6Compatibility && Module.exports){ + // Export emscripten defined in module through EXPORTED_RUNTIME_METHODS + // Useful to export IDBFS or other similar types generally exposed as + // global types when emscripten is not modularized. + for (let i = 0; i < Module.exports.length; ++i) { + const exportName = Module.exports[i]; + const exportValue = (Module)[exportName]; + + if(exportValue) { + globalThisAny[exportName] = exportValue; + } + else{ + console.warn(`MONO_WASM: The exported symbol ${exportName} could not be found in the emscripten module`); + } + } + } + try { _apply_configuration_from_args(config); diff --git a/src/mono/wasm/runtime/types.ts b/src/mono/wasm/runtime/types.ts index cc0d1b24583..aa7481fa076 100644 --- a/src/mono/wasm/runtime/types.ts +++ b/src/mono/wasm/runtime/types.ts @@ -166,6 +166,7 @@ export type DotnetModuleConfig = { onDotnetReady?: () => void; imports?: DotnetModuleConfigImports; + exports?: string[]; } & Partial export type DotnetModuleConfigImports = { -- GitLab