62 lines
5.5 KiB
JavaScript
62 lines
5.5 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('AGFzbQEAAAABYAxgFX5+fn5+fn5+fn5+fn5+fn5+fn5+fwBgDX5+fn5+fn5+fn5/f38AYAF/AGABfwF/YAJ/fwBgAX0AYAF9AX1gAXwAYAF8AXxgAX4AYAF+AX5gCn9/f39/f39/f38BfwJuCAJqcwV0YWJsZQFwAAMFZGVidWcDbG9nAAIFZGVidWcHbG9nX3RlZQADBWRlYnVnA2xvZwAEBWRlYnVnA2xvZwAFBWRlYnVnB2xvZ190ZWUABgVkZWJ1ZwNsb2cABwVkZWJ1Zwdsb2dfdGVlAAgDBAMJCgsFAwEAAQcXAgZtZW1vcnkCAApzY2FsYXJtdWx0AAkK8RYDDQAgAEIgh6cgAKcQAgsJACAAEAcgAA8L1hYCB39MfiAFKQMAIRMgBSkDCCEUIAUpAxAhFSAFKQMYIRYgADUCACEXIAA1AgQhGCAANQIIIRkgADUCDCEaIAA1AhAhGyAANQIUIRwgADUCGCEdIAA1AhwhHiAANQIgIR8gADUCJCEgIAE1AgAhISABNQIEISIgATUCCCEjIAE1AgwhJCABNQIQISUgATUCFCEmIAE1AhghJyABNQIcISggATUCICEpIAE1AiQhKiACNQIAITUgAjUCBCE2IAI1AgghNyACNQIMITggAjUCECE5IAI1AhQhOiACNQIYITsgAjUCHCE8IAI1AiAhPSACNQIkIT4gAzUCACErIAM1AgQhLCADNQIIIS0gAzUCDCEuIAM1AhAhLyADNQIUITAgAzUCGCExIAM1AhwhMiADNQIgITMgAzUCJCE0IAQ1AgAhPyAENQIEIUAgBDUCCCFBIAQ1AgwhQiAENQIQIUMgBDUCFCFEIAQ1AhghRSAENQIcIUYgBDUCICFHIAQ1AiQhSEH+ASEMA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IEE+AgggCSBCPgIMIAkgQz4CECAJIEQ+AhQgCSBFPgIYIAkgRj4CHCAJIEc+AiAgCSBIPgIkIAoL')
|
|
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)
|
|
}
|