crossbind
GitHub

Callbacks

C++ can call a JavaScript function you pass it: a function pointer parameter takes one.

countdown takes a Visitor, a pointer to a function, and calls it once for each number; applyTwice uses the value its callback returns. A function passed to C++ holds a slot in the module until releaseCallback frees it.

A JavaScript function cannot cross into a Web Worker. An app that passes callbacks keeps the module on the page thread, which is what useWorker: false asks for; the playground runs there in any case.

Try it
Pass applyTwice a function that squares its value, and count down from 5.
Open in playground
Runs without a compile until the C++, Rust, packages or imports change.
Press Run, or Ctrl+Enter, to run main.js.
Type to search every guide page and section.
↑↓ navigate↵ openesc close