Calling JavaScript Functions from C/C++
Bridge the gap the other way: invoke JavaScript from your compiled C/C++ WASM code using Emscripten's interop tools.
Calling JavaScript Functions from C/C++ is a free WebAssembly (WASM) for High Performance Apps lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebAssembly (WASM) for High Performance Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two-Way Interop
So far you have called C/C++ from JavaScript. But WASM code often needs to reach back into JS, to log, manipulate the DOM, or call browser APIs.
Emscripten provides several ways for your C/C++ code to call JavaScript.
Why Call JS at All?
WASM is sandboxed, it cannot touch the DOM, fetch, or timers directly. Anything outside pure computation must go through JavaScript.
- Update the page
- Read user input
- Use Web APIs like localStorage
EM_JS: Inline JavaScript
The EM_JS macro lets you define a C function whose body is JavaScript. Call it like any C function.
#include <emscripten.h>
EM_JS(void, js_log, (int x), {
console.log('Value from C: ' + x);
});
int main() { js_log(42); return 0; }EM_ASM: Quick Inline Snippets
For one-off JS, EM_ASM runs a JavaScript snippet inline without declaring a separate function.
EM_ASM({
document.title = 'Set from C++';
});Passing Values to JS
EM_ASM_ variants let you pass arguments and receive return values.
int sum = EM_ASM_INT({
return $0 + $1;
}, 10, 20);
// sum == 30Passing Strings
Strings live in WASM memory. Use Emscripten helpers like UTF8ToString to read a C string pointer in JS.
EM_JS(void, show, (const char* msg), {
console.log(UTF8ToString(msg));
});JavaScript Library Files
For larger interop, put JS functions in a library file and link it with --js-library.
// mylib.js
mergeInto(LibraryManager.library, {
beep: function() { console.log('beep'); }
});Declaring Imported Functions in C
Functions from a JS library are declared extern in C so the compiler links them.
extern void beep();
int main() { beep(); return 0; }Calling Exported C Functions Back
JS can hand a C function pointer back, useful for callbacks, via Module.cwrap or function pointers passed across the boundary.
const tick = Module.cwrap('tick', 'number', []);
setInterval(() => console.log(tick()), 1000);Performance Note
Crossing the JS/WASM boundary has overhead. Keep hot loops inside WASM and call JS only at boundaries (I/O, DOM), not inside tight inner loops.
Best Practices Summary
For calling JS from C/C++:
- Use
EM_JSfor named JS functions,EM_ASMfor snippets - Convert pointers with
UTF8ToString - Use a JS library file for substantial interop
- Minimize boundary crossings in hot paths
Quick Check
Which Emscripten macro defines a C-callable function whose body is written in JavaScript?
Recap
You can now call JavaScript from your WASM code:
EM_JSandEM_ASMembed JS in C/C++EM_ASM_INTpasses args and returns values- JS library files handle larger interop
- Keep boundary crossings out of hot loops
Two-way interop unlocks the full power of the browser from compiled code.
Frequently asked questions
Is the “Calling JavaScript Functions from C/C++” lesson free?
Yes — the full text of “Calling JavaScript Functions from C/C++” is free to read here on the web, and the WebAssembly (WASM) for High Performance Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebAssembly (WASM) for High Performance Apps course, upgrade to CoddyKit PRO.
What will I learn in “Calling JavaScript Functions from C/C++”?
Bridge the gap the other way: invoke JavaScript from your compiled C/C++ WASM code using Emscripten's interop tools. You practise WebAssembly (WASM) for High Performance Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start WebAssembly (WASM) for High Performance Apps?
No prior experience is required. WebAssembly (WASM) for High Performance Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Calling JavaScript Functions from C/C++” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this WebAssembly (WASM) for High Performance Apps lesson?
Yes. Every WebAssembly (WASM) for High Performance Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Compiling C/C++ to WASM with Emscripten
- Loading & Running WASM in JavaScript
- Basic Data Exchange: Primitives
- Calling JavaScript Functions from C/C++