我们可以在 wasm 二进制文件中嵌入自定义部分,并从 JavaScript 中访问它们。
控制台输入:
WebAssembly.compileStreaming(fetch("./pkg/wasm_test_bg.wasm"))
.then(mod => {
let flag = "";
for (let i = 42; i >= 0; i--) {
const sections = WebAssembly.Module.customSections(mod, i.toString());
const decoder = new TextDecoder();
const text = decoder.decode(sections[0]);
flag += text;
}
console.log(flag);
});

参考文档:https://rustwasm.github.io/docs/book/reference/js-ffi.html#custom-sections