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');
|
const el = document.getElementById('content');
|
||||||
let wasm_render = null;
|
let wasm_render = null;
|
||||||
|
|
||||||
|
let wasm_exports = null;
|
||||||
|
|
||||||
async function initWasm() {
|
async function initWasm() {
|
||||||
const resp = await fetch('/doc-wasm/pkg/md_wasm_bg.wasm');
|
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 bytes = await resp.arrayBuffer();
|
||||||
const wasm = await WebAssembly.instantiate(bytes, {
|
|
||||||
wbg: { __wbindgen_init_externref_table: function() {} },
|
// Import object: __wbindgen_init_externref_table will be called from __wbindgen_start
|
||||||
'./md_wasm_bg.js': {}
|
// after instantiation, so we close over a variable that will be set later
|
||||||
});
|
let exports = null;
|
||||||
wasm_render = wasm.instance.exports.render;
|
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) {
|
function md2html(md) {
|
||||||
if (!wasm_render) return '<p>WASM not loaded</p>';
|
if (!wasm_exports) return '<p>WASM not loaded</p>';
|
||||||
// Call the WASM render function
|
|
||||||
// It takes (ptr, len) and returns [ptr, len]
|
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
const buf = encoder.encode(md);
|
const buf = encoder.encode(md);
|
||||||
const malloc = wasm.instance.exports.__wbindgen_malloc;
|
const malloc = wasm_exports.__wbindgen_malloc;
|
||||||
const free = wasm.instance.exports.__wbindgen_free;
|
const free = wasm_exports.__wbindgen_free;
|
||||||
const memory = wasm.instance.exports.memory;
|
const memory = wasm_exports.memory;
|
||||||
|
|
||||||
const ptr = malloc(buf.length, 1);
|
const ptr = malloc(buf.length, 1);
|
||||||
const mem = new Uint8Array(memory.buffer);
|
const mem = new Uint8Array(memory.buffer);
|
||||||
mem.set(buf, ptr);
|
mem.set(buf, ptr);
|
||||||
|
|
||||||
const result = wasm_render(ptr, buf.length);
|
const result = wasm_exports.render(ptr, buf.length);
|
||||||
const [rptr, rlen] = result;
|
const rptr = result[0];
|
||||||
|
const rlen = result[1];
|
||||||
const ret = new TextDecoder().decode(new Uint8Array(memory.buffer, rptr, rlen));
|
const ret = new TextDecoder().decode(new Uint8Array(memory.buffer, rptr, rlen));
|
||||||
free(rptr, rlen, 1);
|
free(rptr, rlen, 1);
|
||||||
return ret.replace(/<table>/g, '<table class="table">');
|
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');
|
const resp = await fetch('/doc-wasm/modules/' + name + '.md');
|
||||||
if (!resp.ok) throw new Error('HTTP ' + resp.status);
|
if (!resp.ok) throw new Error('HTTP ' + resp.status);
|
||||||
const md = await resp.text();
|
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);
|
el.innerHTML = md2html(md);
|
||||||
document.querySelectorAll('.sidebar a.module-link').forEach(function(a) { a.classList.remove('active'); });
|
document.querySelectorAll('.sidebar a.module-link').forEach(function(a) { a.classList.remove('active'); });
|
||||||
var link = document.querySelector('.sidebar a[data-module="' + name + '"]');
|
var link = document.querySelector('.sidebar a[data-module="' + name + '"]');
|
||||||
|
|||||||
Reference in New Issue
Block a user