mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-16 22:41:24 +00:00
Allow web-sys to emit correct typescript declarations from webidl (#1998)
* Update to emit typescript names * Update to use NamedAnyref * Update incoming / outgoing * Remove added space * Remove comment * Add basic typescript tests for web-sys
This commit is contained in:
@ -31,6 +31,7 @@ tys! {
|
||||
SLICE
|
||||
VECTOR
|
||||
ANYREF
|
||||
NAMED_ANYREF
|
||||
ENUM
|
||||
RUST_STRUCT
|
||||
CHAR
|
||||
@ -62,6 +63,7 @@ pub enum Descriptor {
|
||||
CachedString,
|
||||
String,
|
||||
Anyref,
|
||||
NamedAnyref(String),
|
||||
Enum { hole: u32 },
|
||||
RustStruct(String),
|
||||
Char,
|
||||
@ -134,11 +136,13 @@ impl Descriptor {
|
||||
ANYREF => Descriptor::Anyref,
|
||||
ENUM => Descriptor::Enum { hole: get(data) },
|
||||
RUST_STRUCT => {
|
||||
let name = (0..get(data))
|
||||
.map(|_| char::from_u32(get(data)).unwrap())
|
||||
.collect();
|
||||
let name = get_string(data);
|
||||
Descriptor::RustStruct(name)
|
||||
}
|
||||
NAMED_ANYREF => {
|
||||
let name = get_string(data);
|
||||
Descriptor::NamedAnyref(name)
|
||||
}
|
||||
CHAR => Descriptor::Char,
|
||||
UNIT => Descriptor::Unit,
|
||||
CLAMPED => Descriptor::_decode(data, true),
|
||||
@ -200,6 +204,12 @@ fn get(a: &mut &[u32]) -> u32 {
|
||||
ret
|
||||
}
|
||||
|
||||
fn get_string(data: &mut &[u32]) -> String {
|
||||
(0..get(data))
|
||||
.map(|_| char::from_u32(get(data)).unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
impl Closure {
|
||||
fn decode(data: &mut &[u32]) -> Closure {
|
||||
let shim_idx = get(data);
|
||||
|
@ -1238,6 +1238,7 @@ fn adapter2ts(ty: &AdapterType, dst: &mut String) {
|
||||
adapter2ts(ty, dst);
|
||||
dst.push_str(" | undefined");
|
||||
}
|
||||
AdapterType::NamedAnyref(name) => dst.push_str(name),
|
||||
AdapterType::Struct(name) => dst.push_str(name),
|
||||
AdapterType::Function => dst.push_str("any"),
|
||||
}
|
||||
|
@ -67,6 +67,13 @@ impl InstructionBuilder<'_, '_> {
|
||||
&[AdapterType::I32],
|
||||
);
|
||||
}
|
||||
Descriptor::NamedAnyref(name) => {
|
||||
self.instruction(
|
||||
&[AdapterType::NamedAnyref(name.clone())],
|
||||
Instruction::I32FromAnyrefOwned,
|
||||
&[AdapterType::I32]
|
||||
)
|
||||
}
|
||||
Descriptor::RustStruct(class) => {
|
||||
self.instruction(
|
||||
&[AdapterType::Struct(class.clone())],
|
||||
@ -161,6 +168,13 @@ impl InstructionBuilder<'_, '_> {
|
||||
&[AdapterType::I32],
|
||||
);
|
||||
}
|
||||
Descriptor::NamedAnyref(name) => {
|
||||
self.instruction(
|
||||
&[AdapterType::NamedAnyref(name.clone())],
|
||||
Instruction::I32FromAnyrefBorrow,
|
||||
&[AdapterType::I32],
|
||||
);
|
||||
}
|
||||
Descriptor::String | Descriptor::CachedString => {
|
||||
// This allocation is cleaned up once it's received in Rust.
|
||||
self.instruction(
|
||||
@ -224,6 +238,15 @@ impl InstructionBuilder<'_, '_> {
|
||||
&[AdapterType::I32],
|
||||
);
|
||||
}
|
||||
Descriptor::NamedAnyref(name) => {
|
||||
self.instruction(
|
||||
&[AdapterType::NamedAnyref(name.clone()).option()],
|
||||
Instruction::I32FromOptionAnyref {
|
||||
table_and_alloc: None,
|
||||
},
|
||||
&[AdapterType::I32],
|
||||
);
|
||||
}
|
||||
Descriptor::I8 => self.in_option_sentinel(AdapterType::S8),
|
||||
Descriptor::U8 => self.in_option_sentinel(AdapterType::U8),
|
||||
Descriptor::I16 => self.in_option_sentinel(AdapterType::S16),
|
||||
|
@ -39,6 +39,13 @@ impl InstructionBuilder<'_, '_> {
|
||||
&[AdapterType::Anyref],
|
||||
);
|
||||
}
|
||||
Descriptor::NamedAnyref(name) => {
|
||||
self.instruction(
|
||||
&[AdapterType::I32],
|
||||
Instruction::AnyrefLoadOwned,
|
||||
&[AdapterType::NamedAnyref(name.clone())],
|
||||
);
|
||||
}
|
||||
Descriptor::I8 => self.outgoing_i32(AdapterType::S8),
|
||||
Descriptor::U8 => self.outgoing_i32(AdapterType::U8),
|
||||
Descriptor::I16 => self.outgoing_i32(AdapterType::S16),
|
||||
@ -162,6 +169,13 @@ impl InstructionBuilder<'_, '_> {
|
||||
&[AdapterType::Anyref],
|
||||
);
|
||||
}
|
||||
Descriptor::NamedAnyref(name) => {
|
||||
self.instruction(
|
||||
&[AdapterType::I32],
|
||||
Instruction::TableGet,
|
||||
&[AdapterType::NamedAnyref(name.clone())],
|
||||
);
|
||||
}
|
||||
Descriptor::CachedString => self.cached_string(false, false)?,
|
||||
|
||||
Descriptor::String => {
|
||||
@ -227,6 +241,13 @@ impl InstructionBuilder<'_, '_> {
|
||||
&[AdapterType::Anyref.option()],
|
||||
);
|
||||
}
|
||||
Descriptor::NamedAnyref(name) => {
|
||||
self.instruction(
|
||||
&[AdapterType::I32],
|
||||
Instruction::AnyrefLoadOwned,
|
||||
&[AdapterType::NamedAnyref(name.clone()).option()],
|
||||
);
|
||||
}
|
||||
Descriptor::I8 => self.out_option_sentinel(AdapterType::S8),
|
||||
Descriptor::U8 => self.out_option_sentinel(AdapterType::U8),
|
||||
Descriptor::I16 => self.out_option_sentinel(AdapterType::S16),
|
||||
@ -316,6 +337,13 @@ impl InstructionBuilder<'_, '_> {
|
||||
&[AdapterType::Anyref.option()],
|
||||
);
|
||||
}
|
||||
Descriptor::NamedAnyref(name) => {
|
||||
self.instruction(
|
||||
&[AdapterType::I32],
|
||||
Instruction::TableGet,
|
||||
&[AdapterType::NamedAnyref(name.clone()).option()],
|
||||
);
|
||||
}
|
||||
Descriptor::CachedString => self.cached_string(true, false)?,
|
||||
Descriptor::String | Descriptor::Slice(_) => {
|
||||
let kind = arg.vector_kind().ok_or_else(|| {
|
||||
|
@ -84,6 +84,7 @@ pub enum AdapterType {
|
||||
Vector(VectorKind),
|
||||
Option(Box<AdapterType>),
|
||||
Struct(String),
|
||||
NamedAnyref(String),
|
||||
Function,
|
||||
}
|
||||
|
||||
@ -322,7 +323,7 @@ impl AdapterType {
|
||||
AdapterType::I64 => walrus::ValType::I64,
|
||||
AdapterType::F32 => walrus::ValType::F32,
|
||||
AdapterType::F64 => walrus::ValType::F64,
|
||||
AdapterType::Anyref => walrus::ValType::Anyref,
|
||||
AdapterType::Anyref | AdapterType::NamedAnyref(_) => walrus::ValType::Anyref,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@ -340,7 +341,7 @@ impl AdapterType {
|
||||
AdapterType::F32 => wit_walrus::ValType::F32,
|
||||
AdapterType::F64 => wit_walrus::ValType::F64,
|
||||
AdapterType::String => wit_walrus::ValType::String,
|
||||
AdapterType::Anyref => wit_walrus::ValType::Anyref,
|
||||
AdapterType::Anyref | AdapterType::NamedAnyref(_) => wit_walrus::ValType::Anyref,
|
||||
|
||||
AdapterType::I32 => wit_walrus::ValType::I32,
|
||||
AdapterType::I64 => wit_walrus::ValType::I64,
|
||||
|
Reference in New Issue
Block a user