Skip to main content

Crate js_export_macro

Crate js_export_macro 

Source
Expand description

Proc macro that generates dual wasm_bindgen/napi annotations from a single attribute.

§Usage

#[js_export]                          // bare — applies to struct/enum/impl
#[js_export(constructor)]             // forwarded to both wasm_bindgen & napi
#[js_export(js_name = "foo")]         // forwarded to both
#[js_export(getter, js_name = "x")]   // forwarded to both

When a method signature contains [JsU64], the macro automatically splits it into browser (u64) and Node.js (::napi::bindgen_prelude::BigInt) variants. Both map to JavaScript BigInt, preserving full u64 precision on both platforms.

§Expansion example (JsU64 split)

A method with JsU64 in its signature:

#[js_export]
impl Felt {
    #[js_export(constructor)]
    pub fn new(value: JsU64) -> Felt { ... }
}

expands to one impl block per platform, with JsU64 replaced by the concrete type:

#[cfg(feature = "browser")]
#[::wasm_bindgen::prelude::wasm_bindgen]
impl Felt {
    #[::wasm_bindgen::prelude::wasm_bindgen(constructor)]
    pub fn new(value: u64) -> Felt { ... }
}

#[cfg(feature = "nodejs")]
#[::napi_derive::napi]
impl Felt {
    #[::napi_derive::napi(constructor)]
    pub fn new(value: ::napi::bindgen_prelude::BigInt) -> Felt { ... }
}

Methods without JsU64 stay in a single impl block with #[cfg_attr] forwarding the platform-specific macro.

Attribute Macros§

js_export