Commit 5abd092
feat(macro): export generic structs via instantiate()
Adds a `#[wasm_bindgen(instantiate(...))]` attribute that monomorphizes a
generic struct into one concrete JS class per listed instantiation, written as
`Type<Args> as JsName`:
#[wasm_bindgen(instantiate(Pair<f64> as PairF64, Pair<u32> as PairU32))]
pub struct Pair<T> { pub first: T, pub second: T }
Each instantiation is emitted as an independent `ast::Struct` with the type
parameter substituted throughout its fields, producing a distinct exported
class with its own ABI conversions and public-field accessors. Because the
struct macro is a single expansion that emits both the class and its field
accessors, the monomorphized field types cannot diverge from the class they
belong to.
Scope is deliberately limited to the struct surface: instances cross the
boundary via free functions, and public fields are read/written directly from
JS. Exporting impl methods/constructors on an instantiation, and propagating
TS-level type parameters, are follow-up work.
Validation rejects non-type (lifetime/const) parameters, arity mismatches,
duplicate or JS-keyword class names, `js_name` combined with `instantiate`,
and instantiations that don't name the struct.
Tests:
- crates/macro/ui-tests/instantiate-struct: compile-fail coverage of every
validation path (messages are wasm-bindgen diagnostics, toolchain-stable).
- tests/wasm/instantiate: runtime round-trips over two instantiations,
field read/write from JS, and class-distinctness (`instanceof`) checks.
Also documents the attribute under the guide's on-rust-exports reference and
updates the invalid-generics/invalid-items stderr expectations.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 4f17390 commit 5abd092
13 files changed
Lines changed: 564 additions & 44 deletions
File tree
- crates
- macro-support/src
- macro/ui-tests
- guide/src
- reference/attributes/on-rust-exports
- tests/wasm
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
507 | 507 | | |
508 | 508 | | |
509 | 509 | | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
510 | 514 | | |
511 | 515 | | |
512 | 516 | | |
| |||
555 | 559 | | |
556 | 560 | | |
557 | 561 | | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
558 | 565 | | |
559 | 566 | | |
560 | 567 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
218 | 218 | | |
219 | 219 | | |
220 | 220 | | |
221 | | - | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
222 | 226 | | |
223 | 227 | | |
224 | 228 | | |
| |||
548 | 552 | | |
549 | 553 | | |
550 | 554 | | |
551 | | - | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
552 | 558 | | |
553 | 559 | | |
554 | 560 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
228 | 228 | | |
229 | 229 | | |
230 | 230 | | |
231 | | - | |
| 231 | + | |
| 232 | + | |
232 | 233 | | |
233 | 234 | | |
234 | 235 | | |
| |||
0 commit comments