JavaScript-Funktionen aus C/C++ aufrufen
Überbrücken Sie die Lücke in die andere Richtung: Rufen Sie JavaScript aus Ihrem kompilierten C/C++-WASM-Code mithilfe der Interop-Tools von Emscripten auf.
JavaScript-Funktionen aus C/C++ aufrufen ist eine kostenlose WebAssembly (WASM) for High Performance Apps-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des WebAssembly (WASM) for High Performance Apps-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der WebAssembly (WASM) for High Performance Apps-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „JavaScript-Funktionen aus C/C++ aufrufen“ kostenlos?
Ja — der vollständige Text von „JavaScript-Funktionen aus C/C++ aufrufen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des WebAssembly (WASM) for High Performance Apps-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der WebAssembly (WASM) for High Performance Apps-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „JavaScript-Funktionen aus C/C++ aufrufen“?
Überbrücken Sie die Lücke in die andere Richtung: Rufen Sie JavaScript aus Ihrem kompilierten C/C++-WASM-Code mithilfe der Interop-Tools von Emscripten auf. Du übst WebAssembly (WASM) for High Performance Apps mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um WebAssembly (WASM) for High Performance Apps zu starten?
Keine Vorkenntnisse erforderlich. WebAssembly (WASM) for High Performance Apps auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „JavaScript-Funktionen aus C/C++ aufrufen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser WebAssembly (WASM) for High Performance Apps-Lektion Code schreiben und ausführen?
Ja. Jede WebAssembly (WASM) for High Performance Apps-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- C/C++ mit Emscripten zu WASM kompilieren
- WASM in JavaScript laden und ausführen
- Grundlegender Datenaustausch: Primitive
- JavaScript-Funktionen aus C/C++ aufrufen