fix: WASM import module path was wrong (wbg -> ./md_wasm_bg.js)
This commit is contained in:
@@ -62,32 +62,55 @@ const MODULES = [
|
||||
const el = document.getElementById('content');
|
||||
let wasm_render = null;
|
||||
|
||||
let wasm_exports = null;
|
||||
|
||||
async function initWasm() {
|
||||
const resp = await fetch('/doc-wasm/pkg/md_wasm_bg.wasm');
|
||||
if (!resp.ok) throw new Error('WASM fetch failed: ' + resp.status);
|
||||
const bytes = await resp.arrayBuffer();
|
||||
const wasm = await WebAssembly.instantiate(bytes, {
|
||||
wbg: { __wbindgen_init_externref_table: function() {} },
|
||||
'./md_wasm_bg.js': {}
|
||||
});
|
||||
wasm_render = wasm.instance.exports.render;
|
||||
|
||||
// Import object: __wbindgen_init_externref_table will be called from __wbindgen_start
|
||||
// after instantiation, so we close over a variable that will be set later
|
||||
let exports = null;
|
||||
const importObj = {
|
||||
'./md_wasm_bg.js': {
|
||||
__wbindgen_init_externref_table: function() {
|
||||
const table = exports.__wbindgen_externrefs;
|
||||
const offset = table.grow(4);
|
||||
table.set(0, undefined);
|
||||
table.set(offset + 0, undefined);
|
||||
table.set(offset + 1, null);
|
||||
table.set(offset + 2, true);
|
||||
table.set(offset + 3, false);
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
const wasm = await WebAssembly.instantiate(bytes, importObj);
|
||||
exports = wasm.instance.exports;
|
||||
wasm_exports = exports;
|
||||
|
||||
// Call start function to initialize (triggers __wbindgen_init_externref_table)
|
||||
if (exports.__wbindgen_start) {
|
||||
exports.__wbindgen_start();
|
||||
}
|
||||
}
|
||||
|
||||
function md2html(md) {
|
||||
if (!wasm_render) return '<p>WASM not loaded</p>';
|
||||
// Call the WASM render function
|
||||
// It takes (ptr, len) and returns [ptr, len]
|
||||
if (!wasm_exports) return '<p>WASM not loaded</p>';
|
||||
const encoder = new TextEncoder();
|
||||
const buf = encoder.encode(md);
|
||||
const malloc = wasm.instance.exports.__wbindgen_malloc;
|
||||
const free = wasm.instance.exports.__wbindgen_free;
|
||||
const memory = wasm.instance.exports.memory;
|
||||
const malloc = wasm_exports.__wbindgen_malloc;
|
||||
const free = wasm_exports.__wbindgen_free;
|
||||
const memory = wasm_exports.memory;
|
||||
|
||||
const ptr = malloc(buf.length, 1);
|
||||
const mem = new Uint8Array(memory.buffer);
|
||||
mem.set(buf, ptr);
|
||||
|
||||
const result = wasm_render(ptr, buf.length);
|
||||
const [rptr, rlen] = result;
|
||||
const result = wasm_exports.render(ptr, buf.length);
|
||||
const rptr = result[0];
|
||||
const rlen = result[1];
|
||||
const ret = new TextDecoder().decode(new Uint8Array(memory.buffer, rptr, rlen));
|
||||
free(rptr, rlen, 1);
|
||||
return ret.replace(/<table>/g, '<table class="table">');
|
||||
@@ -99,7 +122,7 @@ async function loadDoc(name) {
|
||||
const resp = await fetch('/doc-wasm/modules/' + name + '.md');
|
||||
if (!resp.ok) throw new Error('HTTP ' + resp.status);
|
||||
const md = await resp.text();
|
||||
if (!wasm_render) throw new Error('WASM not loaded');
|
||||
if (!wasm_exports) throw new Error('WASM not loaded');
|
||||
el.innerHTML = md2html(md);
|
||||
document.querySelectorAll('.sidebar a.module-link').forEach(function(a) { a.classList.remove('active'); });
|
||||
var link = document.querySelector('.sidebar a[data-module="' + name + '"]');
|
||||
|
||||
Reference in New Issue
Block a user