Created
March 28, 2020 01:35
-
-
Save binji/c407a4ce6b6a33e456ccc7e9c11f03b5 to your computer and use it in GitHub Desktop.
WebAssembly memory reading demo
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
emcc test.c -o test.js -s EXPORTED_FUNCTIONS='["_get", "_increment"]' |
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
int my_var = 42; | |
int* get(void) { return &my_var; } | |
void increment(void) { my_var++; } |
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
<!doctype html> | |
<script src="test.js"></script> | |
<p>my_var: ???</p> | |
<button>increment</button> | |
<script> | |
let p = document.querySelector('p'); | |
let button = document.querySelector('button'); | |
// Click the button to increment the variable. <p> won't be updated immediately. | |
button.addEventListener('click', () => { _increment(); }); | |
// Wait for emscripten to initialize the module. | |
addOnInit(() => { | |
// Read the address of my_var. | |
let addr = _get(); | |
// Every half second... | |
setInterval(() => { | |
// Read the value of my_var from memory. | |
let value = HEAP32[addr>>2]; | |
// Update the <p> element. | |
p.textContent = `my_var: ${value}`; | |
}, 500); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment