Skip to content

Instantly share code, notes, and snippets.

@kyorohiro
Created December 30, 2017 18:43
Show Gist options
  • Save kyorohiro/2d52b67328f9c7cf16612a55af88c6e7 to your computer and use it in GitHub Desktop.
Save kyorohiro/2d52b67328f9c7cf16612a55af88c6e7 to your computer and use it in GitHub Desktop.
$ emacs libtest1.c
void add01(int i, char output[]) {
output[0] = i+i;
}
void add02(int i, char* output) {
output[0] = i+i;
}
$ emcc libtest1.c -o libtest1.js -s EXPORTED_FUNCTIONS='["_add01","_add02"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]'
$ emacs main.html
<html>
<head>
<title></title>
</head>
<body>
<script src="libtest1.js"></script>
<script>
add01 = Module.cwrap('add01', '', ['number','array']);
add02 = Module.cwrap('add02', '', ['number','string']);
output = new Uint8Array(1);
add01(1, output);
console.log("#"+output.toString('hex', 0, output));
output = "a";
add02(1, output);
console.log("#"+output.charAt(0));
output = new Uint8Array(1);
add02(1, output);
console.log("#"+output.toString('hex', 0, output));
</script>
</body>
</html>
$ python -m SimpleHTTPServer
$ open http://0.0.0.0:8000/
main.html:13 #0
main.html:17 #a
libtest1.js:829 Uncaught TypeError: str.charCodeAt is not a function
at stringToUTF8Array (libtest1.js:829)
at stringToUTF8 (libtest1.js:879)
at stringToC (libtest1.js:503)
at ccall (libtest1.js:522)
at libtest1.js:547
at main.html:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment