62 lines
6.7 KiB
JavaScript
62 lines
6.7 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('AGFzbQEAAAABQQpgAX8AYAF/AX9gAn9/AGABfQBgAX0BfWABfABgAXwBfGABfgBgAX4BfmAUfn5+fn5+fn5+fn5+fn5+fn5+fn4AAmEHBWRlYnVnA2xvZwAABWRlYnVnB2xvZ190ZWUAAQVkZWJ1ZwNsb2cAAgVkZWJ1ZwNsb2cAAwVkZWJ1Zwdsb2dfdGVlAAQFZGVidWcDbG9nAAUFZGVidWcHbG9nX3RlZQAGAwUEBwgJAgUDAQABBjMKfgFCAAt+AUIAC34BQgALfgFCAAt+AUIAC34BQgALfgFCAAt+AUIAC34BQgALfgFCAAsHGwIGbWVtb3J5AgAOZmUyNTUxOV9pbnZlcnQACgrDHQQNACAAQiCHpyAApxACCwkAIAAQByAADwuzDwF8fiAAp6whACABp6whASACp6whAiADp6whAyAEp6whBCAFp6whBSAGp6whBiAHp6whByAIp6whCCAJp6whCSAKp6whCiALp6whCyAMp6whDCANp6whDSAOp6whDiAPp6whDyAQp6whECARp6whESASp6whEiATp6whE0ITIAt+IAtCgICAgAiDQgGGQhN+fSEeQhMgDH4gDEKAgICACINCAYZCE359IR9CEyANfiANQoCAgIAIg0IBhkITfn0hIEITIA5+IA5CgICAgAiDQgGGQhN+fSEhQhMgD34gD0KAgICACINCAYZCE359ISJCEyAQfiAQQoCAgIAIg0IBhkITfn0hI0ITIBF+IBFCgICAgAiDQgGGQhN+fSEkQhMgEn4gEkKAgICACINCAYZCE359ISVCEyATfiATQoCAgIAIg0IBhkITfn0hJkICIAF+IAFCgICAgAiDQgGGQgJ+fSEnQgIgA34gA0KAgICACINCAYZCAn59IShCAiAFfiAFQoCAgIAIg0IBhkICfn0hKUICIAd+IAdCgICAgAiDQgGGQgJ+fSEqQgIgCX4gCUKAgICACINCAYZCAn59ISsgHqesIR4gH6esIR8gIKesISAgIaesISEgIqesISIgI6esISMgJKesISQgJaesISUgJqesISYgJ6esIScgKKesISggKaesISkgKqesISogK6esISsgACAKfiEsIAAgC34hLSAAIAx+IS4gACANfiEvIAAgDn4hMCAAIA9+ITEgACAQfiEyIAAgEX4hMyAAIBJ+ITQgACATfiE1IAEgCn4hNiAnIAt+ITcgASAMfiE4ICcgDX4hOSABIA5+ITogJyAPfiE7IAEgEH4hPCAnIBF+IT0gASASfiE+ICcgJn4hPyACIAp+IUAgAiALfiFBIAIgDH4hQiACIA1+IUMgAiAOfiFEIAIgD34hRSACIBB+IUYgAiARfiFHIAIgJX4hSCACICZ+IUkgAyAKfiFKICggC34hSyADIAx+IUwgKCANfiFNIAMgDn4hTiAoIA9+IU8gAyAQfiFQICggJH4hUSADICV+IVIgKCAmfiFTIAQgCn4hVCAEIAt+IVUgBCAMfiFWIAQgDX4hVyAEIA5+IVggBCAPfiFZIAQgI34hWiAEICR+IVsgBCAlfiFcIAQgJn4hXSAFIAp+IV4gKSALfiFfIAUgDH4hYCApIA1+IWEgBSAOfiFiICkgIn4hYyAFICN+IWQgKSAkfiFlIAUgJX4hZiApICZ+IWcgBiAKfiFoIAYgC34haSAGIAx+IWogBiANfiFrIAYgIX4hbCAGICJ+IW0gBiAjfiFuIAYgJH4hbyAGICV+IXAgBiAmfiFxIAcgCn4hciAqIAt+IXMgByAMfiF0ICogIH4hdSAHICF+IXYgKiAifiF3IAcgI34heCAqICR+IXkgByAlfiF6ICogJn4heyAIIAp+IXwgCCALfiF9IAggH34hfiAIICB+IX8gCCAhfiGAASAIICJ+IYEBIAggI34hggEgCCAkfiGDASAIICV+IYQBIAggJn4hhQEgCSAKfiGGASArIB5+IYcBIAkgH34hiAEgKyAgfiGJASAJICF+IYoBICsgIn4hiwEgCSAjfiGMASArICR+IY0BIA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AhQhByABNQIYIQggATUCHCEJIAE1AiAhCiABNQIkIQsgAqesIQIgA6esIQMgBKesIQQgBaesIQUgBqesIQYgB6esIQcgCKesIQggCaesIQkgCqesIQogC6esIQsgAiADIAQgBSAGIAcgCCAJIAogCyACIAMgBCAFIAYgByAIIAkgCiALEAkjACENIwEhECMCIRMjAyEWIwQhGSMFIRwjBiEfIwchIiMIISUjCSEoIwAjASMCIwMjBCMFIwYjByMIIwkjACMBIwIjAyMEIwUjBiMHIwgjCRAJIwAjASMCIwMjBCMFIwYjByMIIwkjACMBIwIjAyMEIwUjBiMHIwgjCRAJIAIgAyAEIAUgBiAHIAggCSAKIAsjACMBIwIjAyMEIwUjBiMHIwgjCRAJIwAhDiMBIREjAiEUIwMhFyMEIRojBSEdIwYhICMHISMjCCEmIwkhKSAOIBEgFCAXIBogHSAgICMgJiApIA0gECATIBYgGSAcIB8gIiAlICgQCSMAIQ0jASEQIwIhEyMDIRYjBCEZIwUhHCMGIR8jByEiIwghJSMJISgjACMBIwIjAyMEIwUjBiMHIwgjCSMAIwEjAiMDIwQjBSMGIwcjCCMJEAkjACEPIwEhEiMCIRUjAyEYIwQhGyMFIR4jBiEhIwchJCMIIScjCSEqIA4gESAUIBcgGiAdICAgIyAmICkjACMBIwIjAyMEIwUjBiMHIwgjCRAJIwAhDiMBIREjAiEUIwMhFyMEIRojBSEdIwYhICMHISMjCCEmIwkhKQJAA0BBBSAMRg0BIAxBAWohDCMAIwEjAiMDIwQjBSMGIwcjCCMJIwAjASMCIwMjBCMFIwYjByMIIwkQCQwACwtBACEMIwAhDyMBIRIjAiEVIwMhGCMEIRsjBSEeIwYhISMHISQjCCEnIwkhKiAOIBEgFCAXIBogHSAgICMgJiApIwAjASMCIwMjBCMFIwYjByMIIwkQ
|
||
|
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)
|
||
|
}
|