github wasm-bindgen/wasm-bindgen 0.2.121

7 hours ago

Added

  • Added the slice_to_array attribute for imported JS functions,
    which makes a &[T] (or Option<&[T]>) argument arrive on the JS
    side as a plain Array rather than a typed array — without
    changing the Rust-side &[T] signature. Useful when binding JS
    APIs that take T[] rather than TypedArray<T>. For primitive
    element kinds the wire is the same zero-copy borrow used by plain
    &[T], with the JS-side shim wrapping the view in Array.from(...)
    to materialise the Array — no extra allocation. For String,
    JsValue, and JS-imported element types the Rust side builds a
    fresh [u32] index buffer that JS reads and frees, with per-element
    &T -> JsValue (refcount bump for handle-shaped types). No T: Clone bound is required. The attribute can be set per-fn
    (#[wasm_bindgen(slice_to_array)] fn ...) or per-block on an
    extern "C" { ... } declaration to apply to every imported function
    in that block. &[ExportedRustStruct] remains unsupported (use
    owned Vec<T> for that). Has no effect on exported functions;
    default &[T] (typed-array view / memory borrow) and owned
    Vec<T> semantics are unchanged for callers that didn't opt in.
    See the
    slice_to_array guide page.
    #5145

  • Added js_sys::AggregateError bindings (constructor, errors getter, and
    new_with_message / new_with_options overloads). AggregateError represents
    multiple unrelated errors wrapped in a single error, e.g. as thrown by
    Promise.any when all input promises reject, along with js_sys::ErrorOptions,
    accepted by built-in error constructors. ErrorOptions::new(cause)
    constructs an instance pre-populated with cause, and get_cause /
    set_cause provide typed access to the property. All standard error
    constructors that previously took only a message (EvalError,
    RangeError, ReferenceError, SyntaxError, TypeError, URIError,
    WebAssembly.CompileError, WebAssembly.LinkError,
    WebAssembly.RuntimeError) now expose a new_with_options(message, &ErrorOptions) overload, and Error gains
    new_with_error_options(message, &ErrorOptions) alongside the existing
    untyped new_with_options. AggregateError::new_with_options also takes
    &ErrorOptions.
    #5139

  • Added inheritance for Rust-exported types: an exported struct may
    declare #[wasm_bindgen(extends = Parent)] to inherit from another
    exported #[wasm_bindgen] struct. The macro injects a hidden
    parent: wasm_bindgen::Parent<Parent> field (a refcounted cell around
    the parent value) and emits class Child extends Parent in the
    generated JS / .d.ts. The child gets an AsRef<Parent<Parent>> impl
    for the direct parent, and threads per-class pointer slots through
    the wasm ABI so that instanceof Parent is true and parent methods
    dispatch soundly via the JS prototype chain. From inside child
    methods, parent data is reached via self.parent.borrow() /
    self.parent.borrow_mut(). See the new
    extends guide page.
    #5120

  • Added js_sys::FinalizationRegistry bindings (constructor, register,
    register_with_token, and unregister). The cleanup callback parameter
    is typed as &Function<fn(JsValue) -> Undefined>, so closures created via
    Closure::new can be passed using Function::from_closure (for owned
    closures retained by JS) or Function::closure_ref (for borrowed scoped
    closures). Pairs with the existing js_sys::WeakRef bindings.
    #5140

  • Added support for well-known symbols in js_name, getter, and
    setter via the explicit bracket-string form
    "[Symbol.<name>]". This works for imported and exported methods,
    fields, getters, and setters. For example,
    #[wasm_bindgen(js_name = "[Symbol.iterator]")] on an exported method
    generates [Symbol.iterator]() { ... } on the generated JS class, and
    the same syntax works for getter / setter and for imported items.
    #4230

  • Added level 2 bindings for ViewTransition to web-sys.
    #5138

  • Add support for dynamic unions: a #[wasm_bindgen] enum that mixes string-literal
    variants with single-field tuple variants is now exported as an untagged TypeScript
    union and dispatched dynamically at the JS↔Rust boundary. The new enum-level
    #[wasm_bindgen(fallback)] attribute makes the last tuple variant an
    unconditional catch-all, supporting unions whose trailing variant has no
    runtime check (e.g., interface-only imports). String enums and dynamic
    unions now emit export type (was bare type) so the alias is a named
    export, and both honour the private flag to suppress the keyword.
    #4734
    #2153
    #2088

Fixed

  • From<Promise<T>> for JsFuture<T> and IntoFuture for Promise<T> now
    accept any T: FromWasmAbi (rather than T: JsGeneric), letting
    imported async fns return dynamic-union enums.

  • TryFromJsValue for C-style enums no longer accepts non-numeric values
    via JS unary + coercion. Previously calling dyn_into::<MyEnum>() on
    a string would silently coerce it via +"foo" (yielding NaN, then
    NaN as u32 = 0) and could match a discriminant by accident; the
    conversion now returns None for any value that is not a JS number.
    #4734

  • Fix compilation failure with no_std + release
    #5134

  • Raw identifiers (r#name) on enums, enum variants, extern types, statics,
    and impl blocks no longer leak the r# prefix into generated JS / TS
    output and shim names. The Rust-side identifier and the JS-side name are
    now tracked separately for enum variants, and all known identifier
    fallback paths apply Ident::unraw() so e.g.
    pub enum r#Enum { r#A } generates Enum.A instead of producing
    syntactically invalid JS.
    #4323

  • Using the -C panic=unwind option when building for the bundler target
    would produce invalid JS.
    #5142

Changed

  • js_sys::DataView now implements the js_sys::TypedArray trait. A
    FIXME notes that the trait should be renamed to ArrayBufferView in
    the next major release to better reflect the WebIDL spec name covering
    both DataView and the typed-array types.
    #5135

Don't miss a new wasm-bindgen release

NewReleases is sending notifications on new releases.