-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
patchWebAssembly.ts
39 lines (37 loc) · 1.31 KB
/
patchWebAssembly.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { registerModule } from './registry';
/**
* Patches the web assembly runtime.
*/
export function patchWebAssembly(): void {
if ('instantiateStreaming' in WebAssembly) {
const origInstantiateStreaming = WebAssembly.instantiateStreaming;
WebAssembly.instantiateStreaming = function instantiateStreaming(
response: Response | PromiseLike<Response>,
importObject: WebAssembly.Imports,
): Promise<WebAssembly.Module> {
return Promise.resolve(response).then(response => {
return origInstantiateStreaming(response, importObject).then(rv => {
if (response.url) {
registerModule(rv.module, response.url);
}
return rv;
});
});
} as typeof WebAssembly.instantiateStreaming;
}
if ('compileStreaming' in WebAssembly) {
const origCompileStreaming = WebAssembly.compileStreaming;
WebAssembly.compileStreaming = function compileStreaming(
source: Response | Promise<Response>,
): Promise<WebAssembly.Module> {
return Promise.resolve(source).then(response => {
return origCompileStreaming(response).then(module => {
if (response.url) {
registerModule(module, response.url);
}
return module;
});
});
} as typeof WebAssembly.compileStreaming;
}
}