const { readFileSync } = require("fs"); const { join } = require("path"); const { cwd } = require("process"); let memory; let instance; const TRUE = 6 << 28; const FALSE = 7 << 28; function is_data(v) { return v >> 28 === 3; } const importObject = { lib: { dump_python_stack() { console.log("python heap", "-".repeat(10)); const python_stack_size = 64; const python_sp = instance.exports.python_sp.value; const python_heap = new Uint32Array( memory.buffer.slice(0, python_stack_size) ); console.log("sp", python_sp); for (var i = 0; i < python_sp / 4; i++) { const v = python_heap[i]; if (is_data(v)) { if (v === TRUE) { console.log(i, i << 2, "true", v); } else if (v === FALSE) { console.log(i, i << 2, "false", v); } else { console.log(i, i << 2, "object", v & 0xfffffff); } } else { console.log(i, i << 2, "byte", v); } } console.log("python heap", "-".repeat(10)); }, print(v) { // { // const heap = new Uint8Array(memory.buffer); // const b = 65558 + 4 + 4 + 4; // for (var i = 0, len = 2 * 4 * 2; i < len;) { // console.log("DICT", b + i, heap[b + i], heap[b + i + 1], heap[b + i + 2], heap[b + i + 3]); // i += 4 // } // } // console.log("raw", v); if (is_data(v)) { const ptr = v & 0xfffffff; const heap = new Uint8Array(memory.buffer); let i = 0; let s = ""; while (true) { const c = heap[ptr + i]; if (c === 0x0) { break; } s += String.fromCharCode(c); i++; } console.log(s); } else if (v === TRUE) { console.log("True"); } else if (v === FALSE) { console.log("False"); } else { console.log(v); } } } }; const m = new WebAssembly.Module(readFileSync(join(cwd(), "./wasm.out"), null)); instance = new WebAssembly.Instance(m, importObject); memory = instance.exports.mem; instance.exports.main();