Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathpatchWebAssembly.ts
More file actions
Latest commit
58 lines (55 loc) · 2.02 KB
/
Copy pathpatchWebAssembly.ts
File metadata and controls
58 lines (55 loc) · 2.02 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
exporttypeRegisterModuleCallback=(module: WebAssembly.Module,url: string)=>void;
/**
* Patches the WebAssembly streaming APIs so that every compiled module gets
* registered as a debug image under the URL of the response it was compiled
* from.
*
* @param registerModule callback invoked for every successfully compiled module
*/
exportfunctionpatchWebAssembly(registerModule: RegisterModuleCallback): void{
if('instantiateStreaming'inWebAssembly){
constorigInstantiateStreaming=WebAssembly.instantiateStreamingas(
response: unknown,
...rest: unknown[]
)=>Promise<WebAssembly.WebAssemblyInstantiatedSource>;
WebAssembly.instantiateStreaming=functioninstantiateStreaming(
response: Response|PromiseLike<Response>,
...rest: unknown[]
): Promise<WebAssembly.WebAssemblyInstantiatedSource>{
returnPromise.resolve(response).then(response=>{
returnorigInstantiateStreaming(response, ...rest).then(rv=>{
if(response.url){
registerSafely(registerModule,rv.module,response.url);
}
returnrv;
});
});
};
}
if('compileStreaming'inWebAssembly){
constorigCompileStreaming=WebAssembly.compileStreamingas(
source: unknown,
...rest: unknown[]
)=>Promise<WebAssembly.Module>;
WebAssembly.compileStreaming=functioncompileStreaming(
source: Response|PromiseLike<Response>,
...rest: unknown[]
): Promise<WebAssembly.Module>{
returnPromise.resolve(source).then(response=>{
returnorigCompileStreaming(response, ...rest).then(module=>{
if(response.url){
registerSafely(registerModule,module,response.url);
}
returnmodule;
});
});
};
}
}
functionregisterSafely(registerModule: RegisterModuleCallback,module: WebAssembly.Module,url: string): void{
try{
registerModule(module,url);
}catch{
// a registration failure must never break the user's WebAssembly call
}
}