63 lines
4.6 KiB
JavaScript
63 lines
4.6 KiB
JavaScript
|
|
module.exports = loadWebAssembly
|
|
|
|
loadWebAssembly.supported = typeof WebAssembly !== 'undefined'
|
|
|
|
function loadWebAssembly (opts) {
|
|
if (!loadWebAssembly.supported) return null
|
|
|
|
var imp = opts && opts.imports
|
|
console.log(imp)
|
|
var wasm = toUint8Array('AGFzbQEAAAABMApgAX8AYAF/AX9gAn9/AGABfQBgAX0BfWABfABgAXwBfGABfgBgAX4BfmADf39/AAJhBwVkZWJ1ZwNsb2cAAAVkZWJ1Zwdsb2dfdGVlAAEFZGVidWcDbG9nAAIFZGVidWcDbG9nAAMFZGVidWcHbG9nX3RlZQAEBWRlYnVnA2xvZwAFBWRlYnVnB2xvZ190ZWUABgMEAwcICQUDAQABBxgCBm1lbW9yeQIAC2ZlMjU1MTlfbXVsAAkKuxEDDQAgAEIgh6cgAKcQAgsJACAAEAcgAA8LoBEBmgF+IAE1AgAhAyABNQIEIQQgATUCCCEFIAE1AgwhBiABNQIQIQcgATUCFCEIIAE1AhghCSABNQIcIQogATUCICELIAE1AiQhDCACNQIAIQ0gAjUCBCEOIAI1AgghDyACNQIMIRAgAjUCECERIAI1AhQhEiACNQIYIRMgAjUCHCEUIAI1AiAhFSACNQIkIRYgA6esIQMgBKesIQQgBaesIQUgBqesIQYgB6esIQcgCKesIQggCaesIQkgCqesIQogC6esIQsgDKesIQwgDaesIQ0gDqesIQ4gD6esIQ8gEKesIRAgEaesIREgEqesIRIgE6esIRMgFKesIRQgFaesIRUgFqesIRZCEyAOfiAOQoCAgIAIg0IBhkITfn0hK0ITIA9+IA9CgICAgAiDQgGGQhN+fSEsQhMgEH4gEEKAgICACINCAYZCE359IS1CEyARfiARQoCAgIAIg0IBhkITfn0hLkITIBJ+IBJCgICAgAiDQgGGQhN+fSEvQhMgE34gE0KAgICACINCAYZCE359ITBCEyAUfiAUQoCAgIAIg0IBhkITfn0hMUITIBV+IBVCgICAgAiDQgGGQhN+fSEyQhMgFn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fSEfICBCAUIYhnxCGYchKiAXICpCE358IRcgICAqQgFCGYZ+fSEgIBdCAUIZhnxCGochISAYICF8IRggFyAhQgFCGoZ+fSEXIAAgFz4CACAAIBg+AgQgACAZPgIIIAAgGj4CDCAAIBs+AhAgACAcPgIUIAAgHT4CGCAAIB4+AhwgACAfPgIgIAAgID4CJAs=')
|
|
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)
|
|
}
|