Skip to content

Commit e1560d1

Browse files
committed
feat: add wasm instance
1 parent 1ed8850 commit e1560d1

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ export * as extensions from './extensions';
1010
export * as tools from './tools';
1111

1212
export * as types from './types';
13+
14+
export * as wasm from './wasm';

src/wasm/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './wasm-instance';

src/wasm/wasm-instance.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { EventEmitter } from '../core';
2+
3+
export type WasmInstanceEvents = 'instantiated';
4+
5+
/**
6+
* Events:
7+
* - instantiated: Emitted when the wasm instance is instantiated
8+
*/
9+
class WasmInstance extends EventEmitter<WasmInstanceEvents> {
10+
#instance: WebAssembly.Instance | null = null;
11+
#module: WebAssembly.Module | null = null;
12+
#imports: WebAssembly.ModuleImportDescriptor[] | null = null;
13+
#exports: WebAssembly.ModuleExportDescriptor[] | null = null;
14+
15+
get instance() {
16+
return this.#instance;
17+
}
18+
19+
get module() {
20+
return this.#module;
21+
}
22+
23+
get imports() {
24+
return this.#imports;
25+
}
26+
27+
get exports() {
28+
return this.#exports;
29+
}
30+
31+
constructor() {
32+
super();
33+
WebAssembly.instantiateStreaming = new Proxy(WebAssembly.instantiateStreaming, {
34+
apply: async (target, thisArg, args) => {
35+
const result = (await Reflect.apply(
36+
target,
37+
thisArg,
38+
args,
39+
)) as WebAssembly.WebAssemblyInstantiatedSource;
40+
41+
this.#instance = result.instance;
42+
this.#module = result.module;
43+
44+
this.#imports = WebAssembly.Module.imports(this.#module);
45+
this.#exports = WebAssembly.Module.exports(this.#module);
46+
47+
this.emit('instantiated');
48+
49+
return result;
50+
},
51+
});
52+
}
53+
}
54+
55+
export const wasmInstance = new WasmInstance();

0 commit comments

Comments
 (0)