-
-
Save maple3142/afaba030d2d23def9c91ac99c796f961 to your computer and use it in GitHub Desktop.
wasm debug helper functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mem = new Uint8Array(Module.asm.memory.buffer) | |
function encode(str) { | |
return new TextEncoder().encode(str) | |
} | |
function decode(arr) { | |
return new TextDecoder().decode(arr) | |
} | |
function indexOfArray(haystack, needle, start = 0) { | |
let hLen = haystack.length | |
let nLen = needle.length | |
if (hLen < nLen) { | |
return -1 | |
} | |
for (let i = start; i <= hLen - nLen; i++) { | |
let match = true | |
for (let j = 0; j < nLen; j++) { | |
if (haystack[i + j] !== needle[j]) { | |
match = false | |
break | |
} | |
} | |
if (match) { | |
return i | |
} | |
} | |
return -1 | |
} | |
function* findmem(mem, target) { | |
let i = -1 | |
while (true) { | |
i = indexOfArray(mem, target, i + 1) | |
if (i === -1) { | |
break | |
} | |
yield i | |
} | |
} | |
function hex(n) { | |
return parseInt(n).toString(16) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment