-
-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact(wasm): use helper module instead of banner (#607)
* refact(wasm): use helper module instead of banner * test(wasm): test inject helper logic
- Loading branch information
1 parent
274ba1c
commit 1bf5b2c
Showing
5 changed files
with
108 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
export const HELPERS_ID = '\0wasmHelpers.js'; | ||
|
||
export const getHelpersModule = () => ` | ||
function _loadWasmModule (sync, filepath, src, imports) { | ||
function _instantiateOrCompile(source, imports, stream) { | ||
var instantiateFunc = stream ? WebAssembly.instantiateStreaming : WebAssembly.instantiate; | ||
var compileFunc = stream ? WebAssembly.compileStreaming : WebAssembly.compile; | ||
if (imports) { | ||
return instantiateFunc(source, imports) | ||
} else { | ||
return compileFunc(source) | ||
} | ||
} | ||
var buf = null | ||
var isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null | ||
if (filepath && isNode) { | ||
var fs = eval('require("fs")') | ||
var path = eval('require("path")') | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(path.resolve(__dirname, filepath), (error, buffer) => { | ||
if (error != null) { | ||
reject(error) | ||
} | ||
resolve(_instantiateOrCompile(buffer, imports, false)) | ||
}); | ||
}); | ||
} else if (filepath) { | ||
return _instantiateOrCompile(fetch(filepath), imports, true) | ||
} | ||
if (isNode) { | ||
buf = Buffer.from(src, 'base64') | ||
} else { | ||
var raw = globalThis.atob(src) | ||
var rawLength = raw.length | ||
buf = new Uint8Array(new ArrayBuffer(rawLength)) | ||
for(var i = 0; i < rawLength; i++) { | ||
buf[i] = raw.charCodeAt(i) | ||
} | ||
} | ||
if(sync) { | ||
var mod = new WebAssembly.Module(buf) | ||
return imports ? new WebAssembly.Instance(mod, imports) : mod | ||
} else { | ||
return _instantiateOrCompile(buf, imports, false) | ||
} | ||
} | ||
export { _loadWasmModule }; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'foo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import sample from './sample.wasm'; | ||
import foo from './foo'; | ||
|
||
const instance = sample({ env: {} }); | ||
|
||
t.is(instance.exports.main(), 3, 'wasm loaded'); | ||
|
||
t.is(foo, 'foo'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters