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です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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関数を呼び出す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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を演習し、24時間対応の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に戻る