diff --git a/crates/cli-support/src/descriptor.rs b/crates/cli-support/src/descriptor.rs index d5c4dd7f07a1..13ca2a5211b2 100644 --- a/crates/cli-support/src/descriptor.rs +++ b/crates/cli-support/src/descriptor.rs @@ -86,7 +86,7 @@ pub struct Closure { pub mutable: bool, } -#[derive(Copy, Clone, Debug)] +#[derive(Clone, Debug)] pub enum VectorKind { I8, U8, @@ -101,6 +101,7 @@ pub enum VectorKind { F64, String, Externref, + NamedExternref(String), } impl Descriptor { @@ -193,6 +194,7 @@ impl Descriptor { Descriptor::F32 => Some(VectorKind::F32), Descriptor::F64 => Some(VectorKind::F64), Descriptor::Externref => Some(VectorKind::Externref), + Descriptor::NamedExternref(ref name) => Some(VectorKind::NamedExternref(name.clone())), _ => None, } } @@ -240,21 +242,26 @@ impl Function { } impl VectorKind { - pub fn js_ty(&self) -> &str { + pub fn js_ty(&self) -> String { match *self { - VectorKind::String => "string", - VectorKind::I8 => "Int8Array", - VectorKind::U8 => "Uint8Array", - VectorKind::ClampedU8 => "Uint8ClampedArray", - VectorKind::I16 => "Int16Array", - VectorKind::U16 => "Uint16Array", - VectorKind::I32 => "Int32Array", - VectorKind::U32 => "Uint32Array", - VectorKind::I64 => "BigInt64Array", - VectorKind::U64 => "BigUint64Array", - VectorKind::F32 => "Float32Array", - VectorKind::F64 => "Float64Array", - VectorKind::Externref => "any[]", + VectorKind::String => "string".to_string(), + VectorKind::I8 => "Int8Array".to_string(), + VectorKind::U8 => "Uint8Array".to_string(), + VectorKind::ClampedU8 => "Uint8ClampedArray".to_string(), + VectorKind::I16 => "Int16Array".to_string(), + VectorKind::U16 => "Uint16Array".to_string(), + VectorKind::I32 => "Int32Array".to_string(), + VectorKind::U32 => "Uint32Array".to_string(), + VectorKind::I64 => "BigInt64Array".to_string(), + VectorKind::U64 => "BigUint64Array".to_string(), + VectorKind::F32 => "Float32Array".to_string(), + VectorKind::F64 => "Float64Array".to_string(), + VectorKind::Externref => "any[]".to_string(), + VectorKind::NamedExternref(ref name) => { + let mut type_str = name.clone(); + type_str.push_str("[]"); + type_str + } } } @@ -273,6 +280,7 @@ impl VectorKind { VectorKind::F32 => 4, VectorKind::F64 => 8, VectorKind::Externref => 4, + VectorKind::NamedExternref(_) => 4, } } } diff --git a/crates/cli-support/src/externref.rs b/crates/cli-support/src/externref.rs index 5c8a90e8996a..11127be4a03b 100644 --- a/crates/cli-support/src/externref.rs +++ b/crates/cli-support/src/externref.rs @@ -351,31 +351,31 @@ fn module_needs_externref_metadata(aux: &WasmBindgenAux, section: &NonstandardWi }; instructions.iter().any(|instr| match instr.instr { VectorToMemory { - kind: VectorKind::Externref, + kind: VectorKind::Externref | VectorKind::NamedExternref(_), .. } | MutableSliceToMemory { - kind: VectorKind::Externref, + kind: VectorKind::Externref | VectorKind::NamedExternref(_), .. } | OptionVector { - kind: VectorKind::Externref, + kind: VectorKind::Externref | VectorKind::NamedExternref(_), .. } | VectorLoad { - kind: VectorKind::Externref, + kind: VectorKind::Externref | VectorKind::NamedExternref(_), .. } | OptionVectorLoad { - kind: VectorKind::Externref, + kind: VectorKind::Externref | VectorKind::NamedExternref(_), .. } | View { - kind: VectorKind::Externref, + kind: VectorKind::Externref | VectorKind::NamedExternref(_), .. } | OptionView { - kind: VectorKind::Externref, + kind: VectorKind::Externref | VectorKind::NamedExternref(_), .. } => true, _ => false, diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index 643937635b9b..ea79adf92798 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -762,7 +762,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> Instruction::VectorToMemory { kind, malloc, mem } => { let val = js.pop(); - let func = js.cx.pass_to_wasm_function(*kind, *mem)?; + let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?; let malloc = js.cx.export_name_of(*malloc); let i = js.tmp(); js.prelude(&format!( @@ -805,7 +805,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> } Instruction::OptionVector { kind, mem, malloc } => { - let func = js.cx.pass_to_wasm_function(*kind, *mem)?; + let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?; js.cx.expose_is_like_none(); let i = js.tmp(); let malloc = js.cx.export_name_of(*malloc); @@ -832,7 +832,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> // a length. These two pointer/length values get pushed onto the // value stack. let val = js.pop(); - let func = js.cx.pass_to_wasm_function(*kind, *mem)?; + let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?; let malloc = js.cx.export_name_of(*malloc); let i = js.tmp(); js.prelude(&format!( @@ -850,7 +850,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> // original mutable slice with any modifications, and then free the // Rust-backed memory. let free = js.cx.export_name_of(*free); - let get = js.cx.memview_function(*kind, *mem); + let get = js.cx.memview_function(kind.clone(), *mem); js.finally(&format!( " {val}.set({get}().subarray(ptr{i} / {size}, ptr{i} / {size} + len{i})); @@ -1003,7 +1003,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> Instruction::VectorLoad { kind, mem, free } => { let len = js.pop(); let ptr = js.pop(); - let f = js.cx.expose_get_vector_from_wasm(*kind, *mem)?; + let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?; let i = js.tmp(); let free = js.cx.export_name_of(*free); js.prelude(&format!("var v{} = {}({}, {}).slice();", i, f, ptr, len)); @@ -1020,7 +1020,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> Instruction::OptionVectorLoad { kind, mem, free } => { let len = js.pop(); let ptr = js.pop(); - let f = js.cx.expose_get_vector_from_wasm(*kind, *mem)?; + let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?; let i = js.tmp(); let free = js.cx.export_name_of(*free); js.prelude(&format!("let v{};", i)); @@ -1040,14 +1040,14 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> Instruction::View { kind, mem } => { let len = js.pop(); let ptr = js.pop(); - let f = js.cx.expose_get_vector_from_wasm(*kind, *mem)?; + let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?; js.push(format!("{f}({ptr}, {len})", ptr = ptr, len = len, f = f)); } Instruction::OptionView { kind, mem } => { let len = js.pop(); let ptr = js.pop(); - let f = js.cx.expose_get_vector_from_wasm(*kind, *mem)?; + let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?; js.push(format!( "{ptr} === 0 ? undefined : {f}({ptr}, {len})", ptr = ptr, @@ -1226,7 +1226,7 @@ fn adapter2ts(ty: &AdapterType, dst: &mut String) { AdapterType::String => dst.push_str("string"), AdapterType::Externref => dst.push_str("any"), AdapterType::Bool => dst.push_str("boolean"), - AdapterType::Vector(kind) => dst.push_str(kind.js_ty()), + AdapterType::Vector(kind) => dst.push_str(&kind.js_ty()), AdapterType::Option(ty) => { adapter2ts(ty, dst); dst.push_str(" | undefined"); diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 5b9a1b04e179..a286adc88084 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -1621,6 +1621,7 @@ impl<'a> Context<'a> { VectorKind::F32 => self.expose_f32_memory(memory), VectorKind::F64 => self.expose_f64_memory(memory), VectorKind::Externref => self.expose_uint32_memory(memory), + VectorKind::NamedExternref(_) => self.expose_uint32_memory(memory), } } @@ -1842,6 +1843,7 @@ impl<'a> Context<'a> { VectorKind::F32 => self.expose_pass_array_f32_to_wasm(memory), VectorKind::F64 => self.expose_pass_array_f64_to_wasm(memory), VectorKind::Externref => self.expose_pass_array_jsvalue_to_wasm(memory), + VectorKind::NamedExternref(_) => self.expose_pass_array_jsvalue_to_wasm(memory), } } @@ -1864,6 +1866,7 @@ impl<'a> Context<'a> { VectorKind::F32 => self.expose_get_array_f32_from_wasm(memory), VectorKind::F64 => self.expose_get_array_f64_from_wasm(memory), VectorKind::Externref => self.expose_get_array_js_value_from_wasm(memory)?, + VectorKind::NamedExternref(_) => self.expose_get_array_js_value_from_wasm(memory)?, }) } diff --git a/crates/cli-support/src/wit/incoming.rs b/crates/cli-support/src/wit/incoming.rs index 05a7d757ba44..a911535f4853 100644 --- a/crates/cli-support/src/wit/incoming.rs +++ b/crates/cli-support/src/wit/incoming.rs @@ -121,7 +121,7 @@ impl InstructionBuilder<'_, '_> { format_err!("unsupported argument type for calling Rust function from JS {:?}", arg) })?; self.instruction( - &[AdapterType::Vector(kind)], + &[AdapterType::Vector(kind.clone())], Instruction::VectorToMemory { kind, malloc: self.cx.malloc()?, @@ -198,7 +198,7 @@ impl InstructionBuilder<'_, '_> { })?; if mutable { self.instruction( - &[AdapterType::Vector(kind)], + &[AdapterType::Vector(kind.clone())], Instruction::MutableSliceToMemory { kind, malloc: self.cx.malloc()?, @@ -209,7 +209,7 @@ impl InstructionBuilder<'_, '_> { ); } else { self.instruction( - &[AdapterType::Vector(kind)], + &[AdapterType::Vector(kind.clone())], Instruction::VectorToMemory { kind, malloc: self.cx.malloc()?, @@ -322,7 +322,7 @@ impl InstructionBuilder<'_, '_> { let malloc = self.cx.malloc()?; let mem = self.cx.memory()?; self.instruction( - &[AdapterType::Vector(kind).option()], + &[AdapterType::Vector(kind.clone()).option()], Instruction::OptionVector { kind, malloc, mem }, &[AdapterType::I32, AdapterType::I32], ); diff --git a/crates/cli-support/src/wit/outgoing.rs b/crates/cli-support/src/wit/outgoing.rs index d6b4692c4a0c..bba1fe81d8c2 100644 --- a/crates/cli-support/src/wit/outgoing.rs +++ b/crates/cli-support/src/wit/outgoing.rs @@ -139,7 +139,11 @@ impl InstructionBuilder<'_, '_> { let free = self.cx.free()?; self.instruction( &[AdapterType::I32, AdapterType::I32], - Instruction::VectorLoad { kind, mem, free }, + Instruction::VectorLoad { + kind: kind.clone(), + mem, + free, + }, &[AdapterType::Vector(kind)], ); } @@ -196,7 +200,10 @@ impl InstructionBuilder<'_, '_> { let mem = self.cx.memory()?; self.instruction( &[AdapterType::I32, AdapterType::I32], - Instruction::View { kind, mem }, + Instruction::View { + kind: kind.clone(), + mem, + }, &[AdapterType::Vector(kind)], ); } @@ -313,7 +320,11 @@ impl InstructionBuilder<'_, '_> { let free = self.cx.free()?; self.instruction( &[AdapterType::I32, AdapterType::I32], - Instruction::OptionVectorLoad { kind, mem, free }, + Instruction::OptionVectorLoad { + kind: kind.clone(), + mem, + free, + }, &[AdapterType::Vector(kind).option()], ); } @@ -355,7 +366,10 @@ impl InstructionBuilder<'_, '_> { let mem = self.cx.memory()?; self.instruction( &[AdapterType::I32, AdapterType::I32], - Instruction::OptionView { kind, mem }, + Instruction::OptionView { + kind: kind.clone(), + mem, + }, &[AdapterType::Vector(kind).option()], ); }