@@ -601,15 +601,16 @@ impl<'a> Context<'a> {
601
601
} ,
602
602
wasm_bindgen_shared:: free_function( & name) ,
603
603
) ) ;
604
- ts_dst. push_str ( " free(): void;" ) ;
604
+ ts_dst. push_str ( " free(): void;\n " ) ;
605
605
dst. push_str ( & class. contents ) ;
606
606
ts_dst. push_str ( & class. typescript ) ;
607
607
608
608
let mut fields = class. typescript_fields . keys ( ) . collect :: < Vec < _ > > ( ) ;
609
609
fields. sort ( ) ; // make sure we have deterministic output
610
610
for name in fields {
611
- let ( ty, readonly) = & class. typescript_fields [ name] ;
612
- if * readonly {
611
+ let ( ty, has_setter) = & class. typescript_fields [ name] ;
612
+ ts_dst. push_str ( " " ) ;
613
+ if !has_setter {
613
614
ts_dst. push_str ( "readonly " ) ;
614
615
}
615
616
ts_dst. push_str ( name) ;
@@ -1890,20 +1891,24 @@ impl<'a> Context<'a> {
1890
1891
let ( js, ts, raw_docs) = j2r
1891
1892
. process ( & descriptor, & export. arg_names ) ?
1892
1893
. finish ( "" , & format ! ( "wasm.{}" , wasm_name) ) ;
1893
- let ret_ty = j2r. ret_ty . clone ( ) ;
1894
- let exported = require_class ( & mut self . exported_classes , class) ;
1895
1894
let docs = format_doc_comments ( & export. comments , Some ( raw_docs) ) ;
1896
1895
match export. kind {
1897
1896
AuxExportKind :: Getter { .. } => {
1898
- exported. push_field ( & docs, name, & js, Some ( & ret_ty) , true ) ;
1897
+ let ret_ty = j2r. ret_ty . clone ( ) ;
1898
+ let exported = require_class ( & mut self . exported_classes , class) ;
1899
+ exported. push_getter ( & docs, name, & js, & ret_ty) ;
1899
1900
}
1900
1901
AuxExportKind :: Setter { .. } => {
1901
- exported. push_field ( & docs, name, & js, None , false ) ;
1902
+ let arg_ty = & j2r. js_arguments [ 0 ] . type_ . clone ( ) ;
1903
+ let exported = require_class ( & mut self . exported_classes , class) ;
1904
+ exported. push_setter ( & docs, name, & js, & arg_ty) ;
1902
1905
}
1903
1906
AuxExportKind :: StaticFunction { .. } => {
1907
+ let exported = require_class ( & mut self . exported_classes , class) ;
1904
1908
exported. push ( & docs, name, "static " , & js, & ts) ;
1905
1909
}
1906
1910
_ => {
1911
+ let exported = require_class ( & mut self . exported_classes , class) ;
1907
1912
exported. push ( & docs, name, "" , & js, & ts) ;
1908
1913
}
1909
1914
}
@@ -2155,30 +2160,38 @@ impl ExportedClass {
2155
2160
self . typescript . push_str ( "\n " ) ;
2156
2161
}
2157
2162
2158
- /// Used for adding a field to a class, mainly to ensure that TypeScript
2163
+ /// Used for adding a getter to a class, mainly to ensure that TypeScript
2159
2164
/// generation is handled specially.
2160
- ///
2161
- /// Note that the `ts` is optional and it's expected to just be the field
2162
- /// type, not the full signature. It's currently only available on getters,
2163
- /// but there currently has to always be at least a getter.
2164
- fn push_field ( & mut self , docs : & str , field : & str , js : & str , ts : Option < & str > , getter : bool ) {
2165
+ fn push_getter ( & mut self , docs : & str , field : & str , js : & str , ret_ty : & str ) {
2166
+ self . push_accessor ( docs, field, js, "get " , ret_ty) ;
2167
+ }
2168
+
2169
+ /// Used for adding a setter to a class, mainly to ensure that TypeScript
2170
+ /// generation is handled specially.
2171
+ fn push_setter ( & mut self , docs : & str , field : & str , js : & str , ret_ty : & str ) {
2172
+ let has_setter = self . push_accessor ( docs, field, js, "set " , ret_ty) ;
2173
+ * has_setter = true ;
2174
+ }
2175
+
2176
+ fn push_accessor (
2177
+ & mut self ,
2178
+ docs : & str ,
2179
+ field : & str ,
2180
+ js : & str ,
2181
+ prefix : & str ,
2182
+ ret_ty : & str
2183
+ ) -> & mut bool {
2165
2184
self . contents . push_str ( docs) ;
2166
- if getter {
2167
- self . contents . push_str ( "get " ) ;
2168
- } else {
2169
- self . contents . push_str ( "set " ) ;
2170
- }
2185
+ self . contents . push_str ( prefix) ;
2171
2186
self . contents . push_str ( field) ;
2172
2187
self . contents . push_str ( js) ;
2173
2188
self . contents . push_str ( "\n " ) ;
2174
2189
let ( ty, has_setter) = self
2175
2190
. typescript_fields
2176
2191
. entry ( field. to_string ( ) )
2177
2192
. or_insert_with ( Default :: default) ;
2178
- if let Some ( ts) = ts {
2179
- * ty = ts. to_string ( ) ;
2180
- }
2181
- * has_setter = * has_setter || !getter;
2193
+ * ty = ret_ty. to_string ( ) ;
2194
+ has_setter
2182
2195
}
2183
2196
}
2184
2197
0 commit comments