Skip to content

Commit

Permalink
Sync wasm loading
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Feb 27, 2024
1 parent dbfb45e commit 3da47b9
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions hysnappy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
*/
export async function snappyUncompress(input, output) {
// Load the WASM module
const snappyModule = await instantiateWasm()
const wasm = instantiateWasm()
// const { memory, uncompress } = snappyModule.instance.exports
/** @type {WebAssembly.Memory} */
// @ts-ignore
// eslint-disable-next-line prefer-destructuring
const memory = snappyModule.instance.exports.memory
const memory = wasm.exports.memory
/** @type {Function} */
// @ts-ignore
// eslint-disable-next-line prefer-destructuring
const uncompress = snappyModule.instance.exports.uncompress
const uncompress = wasm.exports.uncompress

// Input data is passed into wasm memory at inputStart
// Output data is expected to be written to wasm memory at outputStart
Expand Down Expand Up @@ -58,15 +58,17 @@ export async function snappyUncompress(input, output) {
/**
* Instantiate WASM module from a base64 string.
*
* @returns {Promise<WebAssembly.WebAssemblyInstantiatedSource>}
* @returns {WebAssembly.Instance}
*/
function instantiateWasm() {
const binaryString = atob(wasm64)
const byteArray = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i += 1) {
byteArray[i] = binaryString.charCodeAt(i)
}
return WebAssembly.instantiate(byteArray)
// only works for payload less than 4kb:
const mod = new WebAssembly.Module(byteArray)
return new WebAssembly.Instance(mod)
}

// Base64 encoded hysnappy.wasm
Expand Down

0 comments on commit 3da47b9

Please sign in to comment.