AIGC: ContentProducer: '001191110102MAD55U9H0F10002' ContentPropagator: '001191110102MAD55U9H0F10002' Label: '1' ProduceID: '5b2dcfba-4a51-4e0c-b315-5acc97faee8d' PropagateID: '5b2dcfba-4a51-4e0c-b315-5acc97faee8d' ReservedCode1: '30c0d065-a0a3-493e-8118-41afb0e2a373' ReservedCode2: '30c0d065-a0a3-493e-8118-41afb0e2a373'
js2rust-bridge-macro
Compile-time bridge macros for
js2rust — wire translated JavaScript
into your Rust crate at macro-expansion time.
Dependency direction is strictly one-way: this crate depends on js2rust;
js2rust never depends on this crate. User projects depend on both.
bridge! — project-level wiring (offline codegen output)
bridge!
Reads your js2rust.toml ([output] section), validates the codegen
contract (manifest.json: schema version, unique handler names), and
expands into the gen module tree:
const _: & = include_bytes!;
The include_bytes! sentinel makes rustc track the manifest — rerunning
js2rust codegen automatically triggers a rebuild (the classic
"proc-macro can't see external files" gap, closed).
#[func] / #[route] — single-file translation (no offline step)
async
The stub fn is discarded (its name and docs are kept). At expansion time
the macro loads the config, runs the same Rust→Rhai translation tiers as
the offline pipeline, and emits a private module containing the
regenerated handler, its per-script host-fn register fn, and
include_bytes! sentinels tracking the JS source / config / GraphQL
schema — editing any of them rebuilds the handler.
Tier outcomes:
- Rust tier — the translated handler fn is emitted directly.
- Rhai tier — the translated script is embedded as a string constant
and executed through
js2rust::rhai_translator::rt(requires therhai-rtfeature ofjs2rust, on by default). - JS fallback — not supported in single-file mode; the macro raises a
compile error pointing at the project-level
bridge!instead.
MIT licensed. See the workspace repository for the full example corpus and end-to-end verification.