Skip to content

Instantly share code, notes, and snippets.

@maple3142
Created March 31, 2024 03:47
Show Gist options
  • Save maple3142/afaba030d2d23def9c91ac99c796f961 to your computer and use it in GitHub Desktop.
Save maple3142/afaba030d2d23def9c91ac99c796f961 to your computer and use it in GitHub Desktop.
wasm debug helper functions
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