Skip to content

Instantly share code, notes, and snippets.

@alpgul
Last active March 24, 2022 15:34
Show Gist options
  • Save alpgul/73246bd2ac4c082a6cbe5d61214f1abb to your computer and use it in GitHub Desktop.
Save alpgul/73246bd2ac4c082a6cbe5d61214f1abb to your computer and use it in GitHub Desktop.
WebAssembly Set/Get String - https://wasdk.github.io/WasmFiddle/
char a[20];
void setA(char b){
*a=b;
}
char getA(){
return *a;
}
int main() {
setA("atest");
}
function setMemory(bytes, ptr, len) {
const buffer = new Uint8Array(exports.memory.buffer, ptr, len)
buffer.set(bytes);
}
var wasmModule = new WebAssembly.Module(wasmCode);
var wasmInstance = new WebAssembly.Instance(wasmModule, wasmImports);
var exports = wasmInstance.exports;
exports.main();
//Set
const encoder = new TextEncoder();
const bytes = encoder.encode("Hello World!");
const ptr = exports.getA();
setMemory(bytes, ptr, bytes.byteLength);
//Set
exports.setA(ptr + 1);
//Get
const array = new Uint8Array(exports.memory.buffer, exports.getA(), 20)
const decoder = new TextDecoder()
const string = decoder.decode(array)
//Get
log(string) console.log(exports.memory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment