61 lines
24 KiB
JavaScript
61 lines
24 KiB
JavaScript
|
|
||
|
module.exports = loadWebAssembly
|
||
|
|
||
|
loadWebAssembly.supported = typeof WebAssembly !== 'undefined'
|
||
|
|
||
|
function loadWebAssembly (opts) {
|
||
|
if (!loadWebAssembly.supported) return null
|
||
|
|
||
|
var imp = opts && opts.imports
|
||
|
var wasm = toUint8Array('AGFzbQEAAAABNwtgAX8AYAF/AX9gAn9/AGABfQBgAX0BfWABfABgAXwBfGABfgBgAX4BfmADf39/AGAEf39/fwACYQcFZGVidWcDbG9nAAAFZGVidWcHbG9nX3RlZQABBWRlYnVnA2xvZwACBWRlYnVnA2xvZwADBWRlYnVnB2xvZ190ZWUABAVkZWJ1ZwNsb2cABQVkZWJ1Zwdsb2dfdGVlAAYDCQgHCAgJCQkKAAUDAQABB0UGBm1lbW9yeQIAA211bAAKAnNxAAsLc2MyNTUxOV9tdWwADA5zYzI1NTE5X211bGFkZAANDnNjMjU1MTlfcmVkdWNlAA4K1YIBCA0AIABCIIenIACnEAILCQAgABAHIAAPCzcBAX4gAEIfiEIghiIBIAFCAYaEIgEgAUIChoQiASABQgSGhCIBIAFCCIaEIgEgAUIQhoQgAIQLohEBmwF+IAE1AgAhAyABNQIEIQQgATUCCCEFIAE1AgwhBiABNQIQIQcgATUCFCEIIAE1AhghCSABNQIcIQogATUCICELIAE1AiQhDCACNQIAIQ0gAjUCBCEOIAI1AgghDyACNQIMIRAgAjUCECERIAI1AhQhEiACNQIYIRMgAjUCHCEUIAI1AiAhFSACNQIkIRYgAxAJIQMgBBAJIQQgBRAJIQUgBhAJIQYgBxAJIQcgCBAJIQggCRAJIQkgChAJIQogCxAJIQsgDBAJIQwgDRAJIQ0gDhAJIQ4gDxAJIQ8gEBAJIRAgERAJIREgEhAJIRIgExAJIRMgFBAJIRQgFRAJIRUgFhAJIRZCEyAOfiAOQoCAgIAIg0IBhkITfn0hLEITIA9+IA9CgICAgAiDQgGGQhN+fSEtQhMgEH4gEEKAgICACINCAY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|
exports: null,
|
||
|
realloc: realloc,
|
||
|
onload: onload
|
||
|
}
|
||
|
|
||
|
onload(function () {})
|
||
|
return mod
|
||
|
|
||
|
function realloc (size) {
|
||
|
mod.exports.memory.grow(Math.max(0, Math.ceil(Math.abs(size - mod.memory.length) / 65536)))
|
||
|
mod.memory = new Uint8Array(mod.exports.memory.buffer)
|
||
|
}
|
||
|
|
||
|
function onload (cb) {
|
||
|
if (mod.exports) return cb()
|
||
|
|
||
|
if (ready) {
|
||
|
ready.then(cb.bind(null, null)).catch(cb)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
if (opts && opts.async) throw new Error('async')
|
||
|
setup({instance: new WebAssembly.Instance(new WebAssembly.Module(wasm), imp)})
|
||
|
} catch (err) {
|
||
|
ready = WebAssembly.instantiate(wasm, imp).then(setup)
|
||
|
}
|
||
|
|
||
|
onload(cb)
|
||
|
}
|
||
|
|
||
|
function setup (w) {
|
||
|
mod.exports = w.instance.exports
|
||
|
mod.memory = mod.exports.memory && mod.exports.memory.buffer && new Uint8Array(mod.exports.memory.buffer)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function toUint8Array (s) {
|
||
|
if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt))
|
||
|
return (require('buf' + 'fer').Buffer).from(s, 'base64')
|
||
|
}
|
||
|
|
||
|
function charCodeAt (c) {
|
||
|
return c.charCodeAt(0)
|
||
|
}
|