File tree 8 files changed +50
-8
lines changed
8 files changed +50
-8
lines changed Original file line number Diff line number Diff line change @@ -1382,6 +1382,9 @@ impl<'a> ToTokens for DescribeImport<'a> {
1382
1382
impl ToTokens for ast:: Enum {
1383
1383
fn to_tokens ( & self , into : & mut TokenStream ) {
1384
1384
let enum_name = & self . rust_name ;
1385
+ let name_str = self . js_name . to_string ( ) ;
1386
+ let name_len = name_str. len ( ) as u32 ;
1387
+ let name_chars = name_str. chars ( ) . map ( |c| c as u32 ) . collect :: < Vec < _ > > ( ) ;
1385
1388
let hole = & self . hole ;
1386
1389
let cast_clauses = self . variants . iter ( ) . map ( |variant| {
1387
1390
let variant_name = & variant. name ;
@@ -1433,6 +1436,8 @@ impl ToTokens for ast::Enum {
1433
1436
fn describe( ) {
1434
1437
use #wasm_bindgen:: describe:: * ;
1435
1438
inform( ENUM ) ;
1439
+ inform( #name_len) ;
1440
+ #( inform( #name_chars) ; ) *
1436
1441
inform( #hole) ;
1437
1442
}
1438
1443
}
Original file line number Diff line number Diff line change @@ -66,7 +66,7 @@ pub enum Descriptor {
66
66
String ,
67
67
Externref ,
68
68
NamedExternref ( String ) ,
69
- Enum { hole : u32 } ,
69
+ Enum { name : String , hole : u32 } ,
70
70
RustStruct ( String ) ,
71
71
Char ,
72
72
Option ( Box < Descriptor > ) ,
@@ -149,7 +149,11 @@ impl Descriptor {
149
149
CACHED_STRING => Descriptor :: CachedString ,
150
150
STRING => Descriptor :: String ,
151
151
EXTERNREF => Descriptor :: Externref ,
152
- ENUM => Descriptor :: Enum { hole : get ( data) } ,
152
+ ENUM => {
153
+ let name = get_string ( data) ;
154
+ let hole = get ( data) ;
155
+ Descriptor :: Enum { name, hole }
156
+ }
153
157
RUST_STRUCT => {
154
158
let name = get_string ( data) ;
155
159
Descriptor :: RustStruct ( name)
Original file line number Diff line number Diff line change @@ -1336,6 +1336,7 @@ fn adapter2ts(ty: &AdapterType, dst: &mut String) {
1336
1336
}
1337
1337
AdapterType :: NamedExternref ( name) => dst. push_str ( name) ,
1338
1338
AdapterType :: Struct ( name) => dst. push_str ( name) ,
1339
+ AdapterType :: Enum ( name) => dst. push_str ( name) ,
1339
1340
AdapterType :: Function => dst. push_str ( "any" ) ,
1340
1341
}
1341
1342
}
Original file line number Diff line number Diff line change @@ -98,7 +98,16 @@ impl InstructionBuilder<'_, '_> {
98
98
self . get ( AdapterType :: F64 ) ;
99
99
self . output . push ( AdapterType :: F64 ) ;
100
100
}
101
- Descriptor :: Enum { .. } => self . number ( AdapterType :: U32 , WasmVT :: I32 ) ,
101
+ Descriptor :: Enum { name, .. } => {
102
+ self . instruction (
103
+ & [ AdapterType :: Enum ( name. clone ( ) ) ] ,
104
+ Instruction :: IntToWasm {
105
+ input : AdapterType :: U32 ,
106
+ output : ValType :: I32 ,
107
+ } ,
108
+ & [ AdapterType :: I32 ] ,
109
+ ) ;
110
+ } ,
102
111
Descriptor :: Ref ( d) => self . incoming_ref ( false , d) ?,
103
112
Descriptor :: RefMut ( d) => self . incoming_ref ( true , d) ?,
104
113
Descriptor :: Option ( d) => self . incoming_option ( d) ?,
@@ -274,9 +283,9 @@ impl InstructionBuilder<'_, '_> {
274
283
& [ AdapterType :: I32 ] ,
275
284
) ;
276
285
}
277
- Descriptor :: Enum { hole } => {
286
+ Descriptor :: Enum { name , hole } => {
278
287
self . instruction (
279
- & [ AdapterType :: U32 . option ( ) ] ,
288
+ & [ AdapterType :: Enum ( name . clone ( ) ) . option ( ) ] ,
280
289
Instruction :: I32FromOptionEnum { hole : * hole } ,
281
290
& [ AdapterType :: I32 ] ,
282
291
) ;
Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ impl InstructionBuilder<'_, '_> {
73
73
self . get ( AdapterType :: F64 ) ;
74
74
self . output . push ( AdapterType :: F64 ) ;
75
75
}
76
- Descriptor :: Enum { .. } => self . outgoing_i32 ( AdapterType :: U32 ) ,
76
+ Descriptor :: Enum { name , .. } => self . outgoing_i32 ( AdapterType :: Enum ( name . clone ( ) ) ) ,
77
77
78
78
Descriptor :: Char => {
79
79
self . instruction (
@@ -278,11 +278,11 @@ impl InstructionBuilder<'_, '_> {
278
278
& [ AdapterType :: String . option ( ) ] ,
279
279
) ;
280
280
}
281
- Descriptor :: Enum { hole } => {
281
+ Descriptor :: Enum { name , hole } => {
282
282
self . instruction (
283
283
& [ AdapterType :: I32 ] ,
284
284
Instruction :: OptionEnumFromI32 { hole : * hole } ,
285
- & [ AdapterType :: U32 . option ( ) ] ,
285
+ & [ AdapterType :: Enum ( name . clone ( ) ) . option ( ) ] ,
286
286
) ;
287
287
}
288
288
Descriptor :: RustStruct ( name) => {
Original file line number Diff line number Diff line change @@ -85,6 +85,7 @@ pub enum AdapterType {
85
85
Vector ( VectorKind ) ,
86
86
Option ( Box < AdapterType > ) ,
87
87
Struct ( String ) ,
88
+ Enum ( String ) ,
88
89
NamedExternref ( String ) ,
89
90
Function ,
90
91
}
@@ -327,6 +328,7 @@ impl AdapterType {
327
328
AdapterType :: I64 => walrus:: ValType :: I64 ,
328
329
AdapterType :: F32 => walrus:: ValType :: F32 ,
329
330
AdapterType :: F64 => walrus:: ValType :: F64 ,
331
+ AdapterType :: Enum ( _) => walrus:: ValType :: I32 ,
330
332
AdapterType :: Externref | AdapterType :: NamedExternref ( _) => walrus:: ValType :: Externref ,
331
333
_ => return None ,
332
334
} )
Original file line number Diff line number Diff line change @@ -5,3 +5,19 @@ pub enum Foo {
5
5
A = 1 ,
6
6
B = 3 ,
7
7
}
8
+
9
+ #[ wasm_bindgen]
10
+ pub fn fn_expects_enum ( _: Foo ) { }
11
+
12
+ #[ wasm_bindgen]
13
+ pub fn fn_returns_enum ( ) -> Foo {
14
+ Foo :: A
15
+ }
16
+
17
+ #[ wasm_bindgen]
18
+ pub fn fn_expects_option_enum ( _: Option < Foo > ) { }
19
+
20
+ #[ wasm_bindgen]
21
+ pub fn fn_returns_option_enum ( ) -> Option < Foo > {
22
+ Some ( Foo :: A )
23
+ }
Original file line number Diff line number Diff line change @@ -6,3 +6,8 @@ const a3: wbg.Foo.A = 1;
6
6
const b1 : wbg . Foo = wbg . Foo . B ;
7
7
const b2 : wbg . Foo . B = wbg . Foo . B ;
8
8
const b3 : wbg . Foo . B = 3 ;
9
+
10
+ const fn_expects_enum : ( _ : wbg . Foo ) => void = wbg . fn_expects_enum ;
11
+ const fn_returns_enum : ( ) => wbg . Foo = wbg . fn_returns_enum ;
12
+ const fn_expects_option_enum : ( _ ?: wbg . Foo | undefined ) => void = wbg . fn_expects_option_enum ;
13
+ const fn_returns_option_enum : ( ) => wbg . Foo | undefined = wbg . fn_returns_option_enum ;
You can’t perform that action at this time.
0 commit comments