Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use enums in function signatures within typescript definitions #2242

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,9 @@ impl<'a> ToTokens for DescribeImport<'a> {
impl ToTokens for ast::Enum {
fn to_tokens(&self, into: &mut TokenStream) {
let enum_name = &self.rust_name;
let name_str = self.js_name.to_string();
let name_len = name_str.len() as u32;
let name_chars = name_str.chars().map(|c| c as u32);
let hole = &self.hole;
let cast_clauses = self.variants.iter().map(|variant| {
let variant_name = &variant.name;
Expand Down Expand Up @@ -1205,6 +1208,8 @@ impl ToTokens for ast::Enum {
fn describe() {
use wasm_bindgen::describe::*;
inform(ENUM);
inform(#name_len);
#(inform(#name_chars);)*
inform(#hole);
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/cli-support/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub enum Descriptor {
String,
Externref,
NamedExternref(String),
Enum { hole: u32 },
Enum { name: String, hole: u32 },
RustStruct(String),
Char,
Option(Box<Descriptor>),
Expand Down Expand Up @@ -134,7 +134,11 @@ impl Descriptor {
CACHED_STRING => Descriptor::CachedString,
STRING => Descriptor::String,
EXTERNREF => Descriptor::Externref,
ENUM => Descriptor::Enum { hole: get(data) },
ENUM => {
let name = get_string(data);
let hole = get(data);
Descriptor::Enum { name, hole }
}
RUST_STRUCT => {
let name = get_string(data);
Descriptor::RustStruct(name)
Expand Down
1 change: 1 addition & 0 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,7 @@ fn adapter2ts(ty: &AdapterType, dst: &mut String) {
}
AdapterType::NamedExternref(name) => dst.push_str(name),
AdapterType::Struct(name) => dst.push_str(name),
AdapterType::Enum(name) => dst.push_str(name),
AdapterType::Function => dst.push_str("any"),
}
}
17 changes: 14 additions & 3 deletions crates/cli-support/src/wit/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,18 @@ impl InstructionBuilder<'_, '_> {
self.get(AdapterType::F64);
self.output.push(AdapterType::F64);
}
Descriptor::Enum { .. } => self.number(WitVT::U32, WasmVT::I32),
Descriptor::Enum { name, .. } => {
let std = wit_walrus::Instruction::IntToWasm {
input: WitVT::U32,
output: WasmVT::I32,
trap: false,
};
self.instruction(
&[AdapterType::Enum(name.clone())],
Instruction::Standard(std),
&[AdapterType::I32],
);
}
Descriptor::Ref(d) => self.incoming_ref(false, d)?,
Descriptor::RefMut(d) => self.incoming_ref(true, d)?,
Descriptor::Option(d) => self.incoming_option(d)?,
Expand Down Expand Up @@ -280,9 +291,9 @@ impl InstructionBuilder<'_, '_> {
&[AdapterType::I32],
);
}
Descriptor::Enum { hole } => {
Descriptor::Enum { name, hole } => {
self.instruction(
&[AdapterType::U32.option()],
&[AdapterType::Enum(name.clone()).option()],
Instruction::I32FromOptionEnum { hole: *hole },
&[AdapterType::I32],
);
Expand Down
6 changes: 3 additions & 3 deletions crates/cli-support/src/wit/outgoing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl InstructionBuilder<'_, '_> {
self.get(AdapterType::F64);
self.output.push(AdapterType::F64);
}
Descriptor::Enum { .. } => self.outgoing_i32(AdapterType::U32),
Descriptor::Enum { name, .. } => self.outgoing_i32(AdapterType::Enum(name.clone())),

Descriptor::Char => {
self.instruction(
Expand Down Expand Up @@ -281,11 +281,11 @@ impl InstructionBuilder<'_, '_> {
&[AdapterType::String.option()],
);
}
Descriptor::Enum { hole } => {
Descriptor::Enum { name, hole } => {
self.instruction(
&[AdapterType::I32],
Instruction::OptionEnumFromI32 { hole: *hole },
&[AdapterType::U32.option()],
&[AdapterType::Enum(name.clone()).option()],
);
}
Descriptor::RustStruct(name) => {
Expand Down
2 changes: 2 additions & 0 deletions crates/cli-support/src/wit/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub enum AdapterType {
Vector(VectorKind),
Option(Box<AdapterType>),
Struct(String),
Enum(String),
NamedExternref(String),
Function,
}
Expand Down Expand Up @@ -347,6 +348,7 @@ impl AdapterType {

AdapterType::I32 => wit_walrus::ValType::I32,
AdapterType::I64 => wit_walrus::ValType::I64,
AdapterType::Enum(_) => wit_walrus::ValType::U32,
AdapterType::Option(_)
| AdapterType::Function
| AdapterType::Struct(_)
Expand Down
24 changes: 24 additions & 0 deletions crates/typescript-tests/src/enums.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub enum Shape {
Circle,
Rectangle,
Triangle,
}

#[wasm_bindgen]
pub fn accepts_enum(_: Shape) {}

#[wasm_bindgen]
pub fn take_and_return_enum(s: Shape) -> Shape {
s
}

#[wasm_bindgen]
pub fn accepts_option_enum(_: Option<Shape>) {}

#[wasm_bindgen]
pub fn take_and_return_option_enum(s: Option<Shape>) -> Option<Shape> {
s
}
7 changes: 7 additions & 0 deletions crates/typescript-tests/src/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as wbg from '../pkg/typescript_tests';

const wbg_accepts_enum: (a: wbg.Shape) => void = wbg.accepts_enum;
const wbg_take_and_return_enum: (a: wbg.Shape) => wbg.Shape = wbg.take_and_return_enum;

const wbg_accepts_option_enum: (a: wbg.Shape | undefined) => void = wbg.accepts_option_enum;
const wbg_take_and_return_option_enum: (a: wbg.Shape | undefined) => wbg.Shape | undefined = wbg.take_and_return_option_enum;
1 change: 1 addition & 0 deletions crates/typescript-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod custom_section;
pub mod enums;
pub mod getters_setters;
pub mod omit_definition;
pub mod opt_args_and_ret;
Expand Down