File tree Expand file tree Collapse file tree 5 files changed +83
-29
lines changed
Expand file tree Collapse file tree 5 files changed +83
-29
lines changed Original file line number Diff line number Diff line change @@ -128,13 +128,36 @@ export class NgModule {
128128 */
129129 controller ( name : string , ctlFn : ng . Injectable < any > ) : NgModule ;
130130 /**
131- * Register a named worker that will be instantiated via $provide.
131+ * Register a named WebAssembly module that will be instantiated via $provide.
132+ *
133+ * @param {string } name - The injectable name used to access the instantiated WebAssembly module.
134+ *
135+ * @param {string } src - URL of the `.wasm` file to fetch and instantiate.
136+ *
137+ * @param {Object<string, any> } [imports] WebAssembly import object, passed to `WebAssembly.instantiate` or `WebAssembly.instantiateStreaming`.
138+ *
139+ * @param {Object<string, any> } [opts] - Configuration object.
140+ *
141+ * Supported keys:
142+ * - **raw**: `boolean`
143+ * - `false` (default): the injectable resolves to `instance.exports`
144+ * (ideal for plain WASM modules).
145+ * - `true`: the injectable resolves to the full instantiation result:
146+ * `{ instance, exports, module }`
147+ * (required for runtimes such as Go, Emscripten, wasm-bindgen, etc).
132148 *
133- * @param {string } name
134- * @param {string } src
135149 * @returns {NgModule }
136150 */
137- wasm ( name : string , src : string ) : NgModule ;
151+ wasm (
152+ name : string ,
153+ src : string ,
154+ imports ?: {
155+ [ x : string ] : any ;
156+ } ,
157+ opts ?: {
158+ [ x : string ] : any ;
159+ } ,
160+ ) : NgModule ;
138161 /**
139162 * Register a named worker that will be instantiated via $provide.
140163 *
Original file line number Diff line number Diff line change @@ -570,11 +570,16 @@ export function wait(t?: number): Promise<void>;
570570 */
571571export function startsWith ( str : string , search : string ) : boolean ;
572572/**
573- * Loads and instantiates a WebAssembly module
574- *
575- * @param {string } src - URL to the wasm file
576- * @returns {Promise<Object> } - Resolves to wasm exports
577- */
578- export function instantiateWasm ( src : string ) : Promise < any > ;
573+ * Loads and instantiates a WebAssembly module.
574+ * Tries streaming first, then falls back.
575+ */
576+ export function instantiateWasm (
577+ src : any ,
578+ imports ?: { } ,
579+ ) : Promise < {
580+ instance : WebAssembly . Instance ;
581+ exports : WebAssembly . Exports ;
582+ module : WebAssembly . Module ;
583+ } > ;
579584export const isProxySymbol : unique symbol ;
580585export const ngAttrPrefixes : string [ ] ;
Original file line number Diff line number Diff line change @@ -236,21 +236,39 @@ export class NgModule {
236236 }
237237
238238 /**
239- * Register a named worker that will be instantiated via $provide.
239+ * Register a named WebAssembly module that will be instantiated via $provide.
240+ *
241+ * @param {string } name - The injectable name used to access the instantiated WebAssembly module.
242+ *
243+ * @param {string } src - URL of the `.wasm` file to fetch and instantiate.
244+ *
245+ * @param {Object<string, any> } [imports] WebAssembly import object, passed to `WebAssembly.instantiate` or `WebAssembly.instantiateStreaming`.
246+ *
247+ * @param {Object<string, any> } [opts] - Configuration object.
248+ *
249+ * Supported keys:
250+ * - **raw**: `boolean`
251+ * - `false` (default): the injectable resolves to `instance.exports`
252+ * (ideal for plain WASM modules).
253+ * - `true`: the injectable resolves to the full instantiation result:
254+ * `{ instance, exports, module }`
255+ * (required for runtimes such as Go, Emscripten, wasm-bindgen, etc).
240256 *
241- * @param {string } name
242- * @param {string } src
243257 * @returns {NgModule }
244258 */
245- wasm ( name , src ) {
259+ wasm ( name , src , imports = { } , opts = { } ) {
260+ const raw = ! ! opts . raw ;
261+
246262 this . invokeQueue . push ( [
247263 $t . $provide ,
248264 "provider" ,
249265 [
250266 name ,
251267 function ( ) {
252- this . $get = function ( ) {
253- return instantiateWasm ( src ) ;
268+ this . $get = ( ) => {
269+ return instantiateWasm ( src , imports ) . then ( ( result ) =>
270+ raw ? result : result . exports ,
271+ ) ;
254272 } ;
255273 } ,
256274 ] ,
Original file line number Diff line number Diff line change @@ -6,7 +6,9 @@ import { instantiateWasm } from "../../shared/utils.js";
66export function ngWasmDirective ( ) {
77 return {
88 async link ( $scope , _ , $attrs ) {
9- $scope . $target [ $attrs . as || "wasm" ] = await instantiateWasm ( $attrs . src ) ;
9+ $scope . $target [ $attrs . as || "wasm" ] = (
10+ await instantiateWasm ( $attrs . src )
11+ ) . exports ;
1012 } ,
1113 } ;
1214}
Original file line number Diff line number Diff line change @@ -1258,19 +1258,25 @@ export function startsWith(str, search) {
12581258}
12591259
12601260/**
1261- * Loads and instantiates a WebAssembly module
1262- *
1263- * @param {string } src - URL to the wasm file
1264- * @returns {Promise<Object> } - Resolves to wasm exports
1261+ * Loads and instantiates a WebAssembly module.
1262+ * Tries streaming first, then falls back.
12651263 */
1266- export async function instantiateWasm ( src ) {
1264+ export async function instantiateWasm ( src , imports = { } ) {
1265+ const res = await fetch ( src ) ;
1266+ if ( ! res . ok ) throw new Error ( "fetch failed" ) ;
1267+
12671268 try {
1268- const r = await fetch ( src ) ;
1269- if ( ! r . ok ) throw new Error ( ` ${ r } ` ) ;
1270- const bytes = await r . arrayBuffer ( ) ;
1271- const { instance } = await WebAssembly . instantiate ( bytes ) ;
1272- return instance . exports ;
1273- } catch ( e ) {
1274- throw new Error ( e ) ;
1269+ const { instance , module } = await WebAssembly . instantiateStreaming (
1270+ res . clone ( ) ,
1271+ imports ,
1272+ ) ;
1273+ return { instance, exports : instance . exports , module } ;
1274+ } catch {
1275+ /* empty */
12751276 }
1277+
1278+ const bytes = await res . arrayBuffer ( ) ;
1279+ const { instance, module } = await WebAssembly . instantiate ( bytes , imports ) ;
1280+
1281+ return { instance, exports : instance . exports , module } ;
12761282}
You can’t perform that action at this time.
0 commit comments