Skip to content

Commit

Permalink
feat: allow WASM modules to be imported via a user-land defined loade…
Browse files Browse the repository at this point in the history
…r function microsoft#20876
  • Loading branch information
kyr0 committed Jul 20, 2024
1 parent 11bf309 commit 1f968a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions js/common/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import {env as envImpl} from './env-impl.js';
import type {OrtWasmModule} from 'onnxruntime-web';

export declare namespace Env {
export type WasmPathPrefix = string;
Expand Down Expand Up @@ -80,6 +81,15 @@ export declare namespace Env {
* @defaultValue `false`
*/
proxy?: boolean;

/**
* Set a custom function to import the WebAssembly module from user-land code (Inversion of Control, IoC).
*
* @defaultValue undefined
*/
importWasmModule?: (mjsPathOverride: string, wasmPrefixOverride: string, numThreads: boolean) => Promise<[
string | undefined, EmscriptenModuleFactory<OrtWasmModule>
]>;
}

export interface WebGLFlags {
Expand Down
3 changes: 2 additions & 1 deletion js/web/lib/wasm/wasm-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ export const initializeWebAssembly = async(flags: Env.WebAssemblyFlags): Promise
const mjsPathOverride = (mjsPathOverrideFlag as URL)?.href ?? mjsPathOverrideFlag;
const wasmPathOverrideFlag = (wasmPaths as Env.WasmFilePaths)?.wasm;
const wasmPathOverride = (wasmPathOverrideFlag as URL)?.href ?? wasmPathOverrideFlag;
const importFunction = typeof flags.importWasmModule === "function" ? flags.importWasmModule : importWasmModule;

const [objectUrl, ortWasmFactory] = (await importWasmModule(mjsPathOverride, wasmPrefixOverride, numThreads > 1));
const [objectUrl, ortWasmFactory] = (await importFunction(mjsPathOverride, wasmPrefixOverride, numThreads > 1));

let isTimeout = false;

Expand Down

0 comments on commit 1f968a8

Please sign in to comment.