Skip to content

Commit 5a3cfa4

Browse files
Add enum types for function parameters and return types
1 parent 277b20f commit 5a3cfa4

File tree

8 files changed

+50
-8
lines changed

8 files changed

+50
-8
lines changed

crates/backend/src/codegen.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,9 @@ impl<'a> ToTokens for DescribeImport<'a> {
13821382
impl ToTokens for ast::Enum {
13831383
fn to_tokens(&self, into: &mut TokenStream) {
13841384
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<_>>();
13851388
let hole = &self.hole;
13861389
let cast_clauses = self.variants.iter().map(|variant| {
13871390
let variant_name = &variant.name;
@@ -1433,6 +1436,8 @@ impl ToTokens for ast::Enum {
14331436
fn describe() {
14341437
use #wasm_bindgen::describe::*;
14351438
inform(ENUM);
1439+
inform(#name_len);
1440+
#(inform(#name_chars);)*
14361441
inform(#hole);
14371442
}
14381443
}

crates/cli-support/src/descriptor.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub enum Descriptor {
6666
String,
6767
Externref,
6868
NamedExternref(String),
69-
Enum { hole: u32 },
69+
Enum { name: String, hole: u32 },
7070
RustStruct(String),
7171
Char,
7272
Option(Box<Descriptor>),
@@ -149,7 +149,11 @@ impl Descriptor {
149149
CACHED_STRING => Descriptor::CachedString,
150150
STRING => Descriptor::String,
151151
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+
}
153157
RUST_STRUCT => {
154158
let name = get_string(data);
155159
Descriptor::RustStruct(name)

crates/cli-support/src/js/binding.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ fn adapter2ts(ty: &AdapterType, dst: &mut String) {
13361336
}
13371337
AdapterType::NamedExternref(name) => dst.push_str(name),
13381338
AdapterType::Struct(name) => dst.push_str(name),
1339+
AdapterType::Enum(name) => dst.push_str(name),
13391340
AdapterType::Function => dst.push_str("any"),
13401341
}
13411342
}

crates/cli-support/src/wit/incoming.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,16 @@ impl InstructionBuilder<'_, '_> {
9898
self.get(AdapterType::F64);
9999
self.output.push(AdapterType::F64);
100100
}
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+
},
102111
Descriptor::Ref(d) => self.incoming_ref(false, d)?,
103112
Descriptor::RefMut(d) => self.incoming_ref(true, d)?,
104113
Descriptor::Option(d) => self.incoming_option(d)?,
@@ -274,9 +283,9 @@ impl InstructionBuilder<'_, '_> {
274283
&[AdapterType::I32],
275284
);
276285
}
277-
Descriptor::Enum { hole } => {
286+
Descriptor::Enum { name, hole } => {
278287
self.instruction(
279-
&[AdapterType::U32.option()],
288+
&[AdapterType::Enum(name.clone()).option()],
280289
Instruction::I32FromOptionEnum { hole: *hole },
281290
&[AdapterType::I32],
282291
);

crates/cli-support/src/wit/outgoing.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl InstructionBuilder<'_, '_> {
7373
self.get(AdapterType::F64);
7474
self.output.push(AdapterType::F64);
7575
}
76-
Descriptor::Enum { .. } => self.outgoing_i32(AdapterType::U32),
76+
Descriptor::Enum { name, .. } => self.outgoing_i32(AdapterType::Enum(name.clone())),
7777

7878
Descriptor::Char => {
7979
self.instruction(
@@ -278,11 +278,11 @@ impl InstructionBuilder<'_, '_> {
278278
&[AdapterType::String.option()],
279279
);
280280
}
281-
Descriptor::Enum { hole } => {
281+
Descriptor::Enum { name, hole } => {
282282
self.instruction(
283283
&[AdapterType::I32],
284284
Instruction::OptionEnumFromI32 { hole: *hole },
285-
&[AdapterType::U32.option()],
285+
&[AdapterType::Enum(name.clone()).option()],
286286
);
287287
}
288288
Descriptor::RustStruct(name) => {

crates/cli-support/src/wit/standard.rs

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub enum AdapterType {
8585
Vector(VectorKind),
8686
Option(Box<AdapterType>),
8787
Struct(String),
88+
Enum(String),
8889
NamedExternref(String),
8990
Function,
9091
}
@@ -327,6 +328,7 @@ impl AdapterType {
327328
AdapterType::I64 => walrus::ValType::I64,
328329
AdapterType::F32 => walrus::ValType::F32,
329330
AdapterType::F64 => walrus::ValType::F64,
331+
AdapterType::Enum(_) => walrus::ValType::I32,
330332
AdapterType::Externref | AdapterType::NamedExternref(_) => walrus::ValType::Externref,
331333
_ => return None,
332334
})

crates/typescript-tests/src/enums.rs

+16
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,19 @@ pub enum Foo {
55
A = 1,
66
B = 3,
77
}
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+
}

crates/typescript-tests/src/enums.ts

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ const a3: wbg.Foo.A = 1;
66
const b1: wbg.Foo = wbg.Foo.B;
77
const b2: wbg.Foo.B = wbg.Foo.B;
88
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;

0 commit comments

Comments
 (0)