Created
November 25, 2019 01:01
-
-
Save juliandavidmr/85ef89a8e4048cf74502989f7073e5a4 to your computer and use it in GitHub Desktop.
C + WASM + Linux, Link: https://dev.to/juliandavidmr/c-wasm-linux-jg0
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 main.c -O3 -o index.js -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']" |
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
module.exports = { | |
mime: { | |
'application/wasm': ['wasm'] | |
} | |
}; |
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
#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 |
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> | |
<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