0Pricing
WebAssembly (WASM) for High Performance Apps · 课时

从 C/C++ 调用 JavaScript 函数

通过另一个方向连接两者:使用 Emscripten 的互操作工具,在编译后的 C/C++ WASM 代码中调用 JavaScript。

从 C/C++ 调用 JavaScript 函数 是 CoddyKit 上的免费 WebAssembly (WASM) for High Performance Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 == 30

Passing 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_JS for named JS functions, EM_ASM for 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_JS and EM_ASM embed JS in C/C++
  • EM_ASM_INT passes 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 函数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebAssembly (WASM) for High Performance Apps 课程的其余内容,请升级到 CoddyKit PRO。 WebAssembly (WASM) for High Performance Apps 课程共包含 4 节课。

「从 C/C++ 调用 JavaScript 函数」这节课中我会学到什么?

通过另一个方向连接两者:使用 Emscripten 的互操作工具,在编译后的 C/C++ WASM 代码中调用 JavaScript。 你通过在浏览器中直接运行的动手代码来练习 WebAssembly (WASM) for High Performance Apps,全天候 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 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 Emscripten 将 C/C++ 编译为 WASM
  2. 在 JavaScript 中加载并运行 WASM
  3. 基本数据交换:原始类型
  4. 从 C/C++ 调用 JavaScript 函数
← 返回 WebAssembly (WASM) for High Performance Apps