Commit 9fa62f2
committed
Iterable / iterator support and generic type parameter propagation
Adds first-class handling for the TS iterability protocol and bare
generic type parameters in method signatures, so APIs like
`SyncKvStorage.put<T>(key: string, value: T)` and
`SyncKvStorage.list<T>(): Iterable<[string, T]>` round-trip into
typed wasm-bindgen externs without erasing the generics.
## Iterators
`Iterator<T>` and `IterableIterator<T>` map directly to
`js_sys::Iterator<T>`; `AsyncIterator<T>` and
`AsyncIterableIterator<T>` map to `js_sys::AsyncIterator<T>`. These
are added as new IR variants (`TypeRef::Iterator` and
`TypeRef::AsyncIterator`) and emit through the existing generic
container machinery alongside `Promise<T>`.
## Iterable wrapper synthesis
`Iterable<T>` describes the protocol — an object exposing
`[Symbol.iterator](): Iterator<T>` — distinct from `Iterator<T>`
itself. To preserve that at the binding layer, top-level
`Iterable<T>` returns now synthesize a wrapper extern type with a
single `[Symbol.iterator]()` method:
```ts
interface SyncKvStorage {
list<T>(): Iterable<[string, T]>;
}
```
becomes:
```rust
pub type SyncKvStorageList<T: ::wasm_bindgen::JsGeneric>;
#[wasm_bindgen(method, js_name = "Symbol.iterator")]
pub fn iterator<T: ::wasm_bindgen::JsGeneric>(
this: &SyncKvStorageList<T>,
) -> Iterator<ArrayTuple<(JsString, T)>>;
#[wasm_bindgen(method)]
pub fn list<T: ::wasm_bindgen::JsGeneric>(this: &SyncKvStorage) -> SyncKvStorageList<T>;
```
The wrapper's name follows the existing `<Parent><Member>`
convention with dedup, mirroring anonymous-interface parameter
synthesis. `AsyncIterable<T>` synthesizes the analogous wrapper
keyed on `Symbol.asyncIterator`. Nested occurrences inside unions
or arrays are not synthesized — they erase to `JsValue`, matching
the existing parameter-synthesis limitation.
## Generic type parameter propagation
Bare TS generics on methods now survive codegen as
`<T: ::wasm_bindgen::JsGeneric>` declarations rather than being
erased to `JsValue`. Implementation:
* New `TypeRef::TypeParam(String)` IR variant for in-scope generic
type parameter references.
* `convert_ts_type_scoped` returns `TypeParam(name)` instead of
`Any` for names in the method's type-parameter scope.
* `convert_formal_params_with_synthesis` now threads scope through
to parameter type conversion (previously called `convert_ts_type`,
which ignored scope).
* Method codegen collects every `TypeParam` reachable from the
signature and emits a generic decl bounded by
`::wasm_bindgen::JsGeneric`. Synthesized iterable wrappers
propagate any `TypeParam` from their item type onto the wrapper
type itself, so `SyncKvStorageList` is `SyncKvStorageList<T>`.
* `pub type X<T, …>` declarations carry their generics so type
aliases that mention generic parameters compile.
## External non-generic type protection
`GenericInstantiation` codegen now consults the codegen context's
recorded type-parameter arity per local type. References like
`ReadableStream<Uint8Array>` whose target accepts zero generics
(non-generic web-sys / external types) drop the args and emit the
bare base type, preventing `E0107`.
## Symbol-keyed methods
Symbol JS names like `Symbol.iterator` previously snake_cased to
`symboliterator`. `base_rust_name` now strips the `Symbol.`
prefix when computing the Rust name, so the synthesized iterator
methods read as `iterator` / `async_iterator` while `js_name`
keeps the full `Symbol.iterator` for wasm-bindgen.
## Header comment
Generated files now begin with `// Generated by ts-gen. Do not
edit.` (prepended after rustfmt — `quote!` doesn't preserve line
comments through token streams).
## Tests
* New `tests/fixtures/iterables.d.ts` exercises every iterability
variant plus generic propagation through `put<T>` / `get<T>`.
* New `tests/snapshots/iterables.rs` blesses the expected output.
* Four unit tests in `parse::members::tests` cover happy path
(sync), async, non-iterable rejection, and dedup.
* Existing snapshots updated: workers-types now produces typed
`Iterator<...>`, `AsyncIterator<...>`, and per-method generics
on `put` / `get` style methods that previously aliased `T` to
`JsValue`.
## CONVENTIONS
Two new sections (slotted next to `Promise<T>` since all three
share the generic-container shape):
* `Iterator<T>` / `IterableIterator<T>` map to `js_sys::Iterator<T>`
* `Iterable<T>` returns synthesize a wrapper with `[Symbol.iterator]()`
`AGENTS.md` updated to clarify that CONVENTIONS sections are not
numbered and are ordered from simplest to most obscure.1 parent 1f3ca2d commit 9fa62f2
20 files changed
Lines changed: 2200 additions & 893 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
62 | 66 | | |
63 | 67 | | |
64 | 68 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
689 | 689 | | |
690 | 690 | | |
691 | 691 | | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
692 | 762 | | |
693 | 763 | | |
694 | 764 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
58 | 62 | | |
59 | 63 | | |
60 | 64 | | |
| |||
85 | 89 | | |
86 | 90 | | |
87 | 91 | | |
| 92 | + | |
88 | 93 | | |
89 | 94 | | |
90 | 95 | | |
| |||
117 | 122 | | |
118 | 123 | | |
119 | 124 | | |
| 125 | + | |
120 | 126 | | |
121 | 127 | | |
122 | 128 | | |
123 | 129 | | |
124 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
125 | 160 | | |
126 | 161 | | |
127 | 162 | | |
| |||
976 | 1011 | | |
977 | 1012 | | |
978 | 1013 | | |
| 1014 | + | |
| 1015 | + | |
979 | 1016 | | |
980 | 1017 | | |
981 | 1018 | | |
982 | | - | |
| 1019 | + | |
983 | 1020 | | |
984 | 1021 | | |
985 | 1022 | | |
| |||
1069 | 1106 | | |
1070 | 1107 | | |
1071 | 1108 | | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
1072 | 1116 | | |
1073 | 1117 | | |
1074 | 1118 | | |
1075 | | - | |
| 1119 | + | |
1076 | 1120 | | |
1077 | 1121 | | |
1078 | 1122 | | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
1079 | 1150 | | |
1080 | 1151 | | |
1081 | 1152 | | |
| |||
1121 | 1192 | | |
1122 | 1193 | | |
1123 | 1194 | | |
| 1195 | + | |
| 1196 | + | |
1124 | 1197 | | |
1125 | 1198 | | |
1126 | 1199 | | |
1127 | | - | |
| 1200 | + | |
1128 | 1201 | | |
1129 | 1202 | | |
1130 | 1203 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
75 | 80 | | |
76 | 81 | | |
77 | 82 | | |
| |||
120 | 125 | | |
121 | 126 | | |
122 | 127 | | |
123 | | - | |
124 | | - | |
125 | 128 | | |
126 | 129 | | |
127 | 130 | | |
| |||
300 | 303 | | |
301 | 304 | | |
302 | 305 | | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
303 | 322 | | |
304 | 323 | | |
305 | | - | |
| 324 | + | |
306 | 325 | | |
307 | 326 | | |
308 | 327 | | |
| |||
0 commit comments