crossbind
GitHub

Browser

In the browser your C++ runs as WebAssembly behind a JavaScript loader. A bundler plugin turns each header you import into a module and ships the wasm with your bundle; without a bundler, the CLI builds both for a <script> tag.

Setup

A browser project needs the plugin for its bundler, a crossbind.config.js at the root and a header imported from app code. The scaffolder sets up all three for React, Vue or Svelte on Vite, React on Rspack, or a page with no bundler:

shell
npm create crossbind@beta my-app Web React Vite

In an existing app, add crossbind() to your bundler (Bundler plugins) and this config:

crossbind.config.js
export default {
paths: {
config: import.meta.url,
},
};

C++ lives in src/native. Import a header and call initNative() once:

src/main.js
import { initNative, Native } from './native/native.h';
 
initNative().then(async () => {
console.log(await Native.sample());
});

Calls return promises because the module runs in a Web Worker by default; see Workers and storage.

No bundler

A static page builds with the CLI and loads the result with a <script> tag. Point paths.output at the folder you serve and build for the browser:

crossbind.config.js
export default {
general: { name: 'my-app' },
paths: {
config: import.meta.url,
output: 'dist',
},
};
shell
npm install -D crossbind@beta
npx crossbind build -p wasm -a wasm32 -r st -e browser -b release

That writes dist/my-app-wasm-wasm32-st-release.browser.js and its .wasm. Load the script with a classic <script> tag: it defines a global initNative. path says where the files are, relative to the page.

index.html
<script src="./dist/my-app-wasm-wasm32-st-release.browser.js"></script>
<script>
initNative({ path: './dist' }).then(async ({ Native }) => {
document.querySelector('#out').textContent = await Native.sample();
});
</script>

Without a bundler, the build binds every .h file in src/native, and a library reaches the page through a header of your own. There is no dev server and no rebuild on save.

Workers and storage

In a browser, initNative() starts the module in a Web Worker. The page never blocks on C++, and every call returns a promise, construction included (await new X()). Set dts: 'promise' in crossbind.config.js so the generated types say the same.

Pass useWorker: false to keep the module on the page thread, where calls return their values directly and a long call blocks the page:

src/main.js
initNative({ useWorker: false }).then(() => {
console.log(Native.sample());
});

Files under /opfs/<app>/ survive a reload; OPFS needs the worker and is on by default only in mt builds. Files under /memfs/<app>/ last for the session. More in Files. useWorker and runtime: 'mt' are independent choices (Threads and workers).

Development

The first dev start compiles in Docker (or on a remote runner) and takes a while; later starts reuse the cache in .crossbind/. Dev servers build debug, production builds release.

Saving a header, a C++ source or a .rs file in src/native rebuilds and reloads the page. The Vite and webpack dev servers, and vite preview, send the COOP/COEP headers, so a multithreaded build runs locally.

Production build

A production build (vite build, rspack build or webpack) writes crossbind.js, crossbind.wasm and, with data files, crossbind.data.txt to the output folder. Deploy them with the bundle. They load from Vite's base or webpack's output.publicPath, so an app on a sub-path finds them.

A single-threaded build needs no special headers. A runtime: 'mt' build needs SharedArrayBuffer, which browsers enable only on a cross-origin isolated page, so the production host has to send:

response headers
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

Without them the module fails to start; crossOriginIsolated must be true in the console of the deployed page. Threads and workers shows how each host sends them. GitHub Pages cannot, so a multithreaded build does not run there.

Limits

  • Next.js has no example or test in the repository, and the webpack plugin does not run under Turbopack.
  • COEP in development. The dev servers send Cross-Origin-Embedder-Policy: require-corp for every build, single-threaded too, so a cross-origin image or script without a Cross-Origin-Resource-Policy header fails to load in dev but not in production.
  • Synchronous callbacks into page code do not cross the worker: design them as promise round-trips, or set useWorker: false.

Complete apps, each also a scaffolder template: web-react-vite, web-vue-vite, web-svelte-vite, web-react-rspack and web-vanilla.

Type to search every guide page and section.
↑↓ navigate↵ openesc close