crossbind
GitHub

Functions and classes

crossbind reads your C++ header and binds the functions, classes and enums it declares. There is no glue code and no binding file: the header is the interface.

native.h declares a function, greet, and a class, Counter; native.cpp defines them. Press Run: main.js imports both from the header.

A std::string crosses as a JavaScript string and an int as a number. The class arrives as a JavaScript class: new Counter(41) runs the C++ constructor, and its public methods are ordinary methods.

The C++ object goes when the garbage collector reclaims counter. counter.delete() frees it at once, which suits an object holding a lot of memory, or many made in a loop.

await initNative() loads the module once, and every imported name works after it. A project imports the header the same way; the quick start sets that up.

The playground runs the module on the page's own thread, so a call returns its result. In a browser app, initNative() starts it in a Web Worker by default, where every call returns a promise to await; initNative({ useWorker: false }) runs it the way these steps do.

Try it
Add int add(int a, int b); to native.h, define it in native.cpp, import add in main.js and call add(2, 3). Changing the C++ or the imports makes Run compile on the playground server; until then, a change to the rest of main.js runs at once.
Open in playgroundRuns without a compile until the C++, Rust, packages or imports change.
Console
Press Run, or Ctrl+Enter, to run main.js.
Type to search every guide page and section.
↑↓ navigate↵ openesc close