Last active
March 24, 2022 15:34
-
-
Save alpgul/73246bd2ac4c082a6cbe5d61214f1abb to your computer and use it in GitHub Desktop.
WebAssembly Set/Get String - https://wasdk.github.io/WasmFiddle/
This file contains 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
char a[20]; | |
void setA(char b){ | |
*a=b; | |
} | |
char getA(){ | |
return *a; | |
} | |
int main() { | |
setA("atest"); | |
} |
This file contains 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
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