Skip to content

Instantly share code, notes, and snippets.

@juliandavidmr
Created November 25, 2019 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliandavidmr/85ef89a8e4048cf74502989f7073e5a4 to your computer and use it in GitHub Desktop.
Save juliandavidmr/85ef89a8e4048cf74502989f7073e5a4 to your computer and use it in GitHub Desktop.
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