Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
emcc main.c -O3 -o index.js -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']"
module.exports = {
mime: {
'application/wasm': ['wasm']
}
};
#include <stdio.h>
#include <stdlib.h>
#include <emscripten/emscripten.h>
int main(int argc, char ** argv) {
printf("WebAssembly module loadedn");
return 0;
}
#ifdef __cplusplus
extern "C" {
#endif
int EMSCRIPTEN_KEEPALIVE myFunction(int argc, char ** argv) {
printf("MyFunction Called");
return argc * 2;
}
#ifdef __cplusplus
}
#endif
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>WebAssembly Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<button id="myFunction">Call myFunction</button>
<!-- Include the JavaScript glue code. -->
<!-- This will load the WebAssembly module and run its main. -->
<script async src="index.js"></script>
<script>
document.getElementById('myFunction').addEventListener('click', function(ev) {
var result = Module.ccall(
'myFunction', // name of C function
'number', // return type
['number'], // argument types
[42]); // arguments
console.log('Result:', result);
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment