C/C++에서 JavaScript 함수 호출하기
반대 방향으로도 연결해 보세요. 엠스크립튼의 상호 운용 도구를 사용하여 컴파일된 C/C++ WASM 코드에서 JavaScript를 호출합니다.
C/C++에서 JavaScript 함수 호출하기은(는) CoddyKit의 무료 WebAssembly (WASM) for High Performance Apps 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 WebAssembly (WASM) for High Performance Apps 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. WebAssembly (WASM) for High Performance Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
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.
자주 묻는 질문
“C/C++에서 JavaScript 함수 호출하기” 강의는 무료인가요?
네 — “C/C++에서 JavaScript 함수 호출하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 WebAssembly (WASM) for High Performance Apps 강의 전체를 잠금 해제할 수 있습니다. WebAssembly (WASM) for High Performance Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
“C/C++에서 JavaScript 함수 호출하기”에서 뭘 배우나요?
반대 방향으로도 연결해 보세요. 엠스크립튼의 상호 운용 도구를 사용하여 컴파일된 C/C++ WASM 코드에서 JavaScript를 호출합니다. 브라우저에서 직접 실행하는 실습 코드로 WebAssembly (WASM) for High Performance Apps을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
WebAssembly (WASM) for High Performance Apps을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 WebAssembly (WASM) for High Performance Apps은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“C/C++에서 JavaScript 함수 호출하기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 WebAssembly (WASM) for High Performance Apps 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 WebAssembly (WASM) for High Performance Apps 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Emscripten으로 C/C++를 WASM으로 컴파일하기
- JavaScript에서 WASM 불러오고 실행하기
- 기본 데이터 교환: 원시 타입
- C/C++에서 JavaScript 함수 호출하기