Chiamare funzioni JavaScript da C/C++
Colmate il divario nella direzione opposta: invocate JavaScript dal vostro codice WASM C/C++ compilato usando gli strumenti di interoperabilità di Emscripten.
Chiamare funzioni JavaScript da C/C++ è una lezione WebAssembly (WASM) for High Performance Apps gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento WebAssembly (WASM) for High Performance Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Domande Frequenti
La lezione «Chiamare funzioni JavaScript da C/C++» è gratuita?
Sì — il testo completo di «Chiamare funzioni JavaScript da C/C++» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso WebAssembly (WASM) for High Performance Apps, passa a CoddyKit PRO. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.
Cosa imparerò in «Chiamare funzioni JavaScript da C/C++»?
Colmate il divario nella direzione opposta: invocate JavaScript dal vostro codice WASM C/C++ compilato usando gli strumenti di interoperabilità di Emscripten. Eserciti WebAssembly (WASM) for High Performance Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare WebAssembly (WASM) for High Performance Apps?
Non è richiesta alcuna esperienza precedente. WebAssembly (WASM) for High Performance Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Chiamare funzioni JavaScript da C/C++»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione WebAssembly (WASM) for High Performance Apps?
Sì. Ogni lezione WebAssembly (WASM) for High Performance Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Compilare C/C++ in WASM con Emscripten
- Caricare ed eseguire WASM in JavaScript
- Scambio di dati di base: primitivi
- Chiamare funzioni JavaScript da C/C++