62 lines
4.4 KiB
JavaScript
62 lines
4.4 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('AGFzbQEAAAABJAJgGX9+fn5+fn5+fn5+fn5+fn5+fn5+fn5+fn4AYAR/f39/AAIOAQJqcwV0YWJsZQFwAAEDAgEBBQMBAAEHGwIGbWVtb3J5AgAOc2MyNTUxOV9tdWxhZGQAAAqoEQGlEQFUfiABNQIAIQUgATUCBCEGIAE1AgghByABNQIMIQggATUCECEJIAE1AhQhCiABNQIYIQsgATUCHCEMIAE1AiAhDSABNQIkIQ4gATUCKCEPIAE1AiwhECACNQIAIREgAjUCBCESIAI1AgghEyACNQIMIRQgAjUCECEVIAI1AhQhFiACNQIYIRcgAjUCHCEYIAI1AiAhGSACNQIkIRogAjUCKCEbIAI1AiwhHCADNQIAIR0gAzUCBCEeIAM1AgghHyADNQIMISAgAzUCECEhIAM1AhQhIiADNQIYISMgAzUCHCEkIAM1AiAhJSADNQIkISYgAzUCKCEnIAM1AiwhKCAFp6whBSAGp6whBiAHp6whByAIp6whCCAJp6whCSAKp6whCiALp6whCyAMp6whDCANp6whDSAOp6whDiAPp6whDyAQp6whECARp6whESASp6whEiATp6whEyAUp6whFCAVp6whFSAWp6whFiAXp6whFyAYp6whGCAZp6whGSAap6whGiAbp6whGyAcp6whHCAdp6whHSAep6whHiAfp6whHyAgp6whICAhp6whISAip6whIiAjp6whIyAkp6whJCAlp6whJSAmp6whJiAnp6whJyAop6whKCAdIAUgEX58ISkgHiAFIBJ+IAYgEX58fCEqIB8gBSATfiAGIBJ+IAcgEX58fHwhKyAgIAUgFH4gBiATfiAHIBJ+IAggEX58fHx8ISwgISAFIBV+IAYgFH4gByATfiAIIBJ+IAkgEX58fHx8fCEtICIgBSAWfiAGIBV+IAcgFH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gLSAuIC8gMCAxIDIgMyA0IDUgNiA3IDggOSA6IDsgPCA9ID4gPyBAQQARAAAL')
|
|
var ready = null
|
|
|
|
var mod = {
|
|
buffer: wasm,
|
|
memory: null,
|
|
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)
|
|
}
|