Skip to content

Commit 021a5cc

Browse files
Add proper TypeScript types for NamedExternref Vecs
1 parent afdbda2 commit 021a5cc

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

crates/cli-support/src/descriptor.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub struct Closure {
8686
pub mutable: bool,
8787
}
8888

89-
#[derive(Copy, Clone, Debug)]
89+
#[derive(Clone, Debug)]
9090
pub enum VectorKind {
9191
I8,
9292
U8,
@@ -101,6 +101,7 @@ pub enum VectorKind {
101101
F64,
102102
String,
103103
Externref,
104+
NamedExternref(String),
104105
}
105106

106107
impl Descriptor {
@@ -193,6 +194,7 @@ impl Descriptor {
193194
Descriptor::F32 => Some(VectorKind::F32),
194195
Descriptor::F64 => Some(VectorKind::F64),
195196
Descriptor::Externref => Some(VectorKind::Externref),
197+
Descriptor::NamedExternref(name) => Some(VectorKind::NamedExternref(name.clone())),
196198
_ => None,
197199
}
198200
}
@@ -240,21 +242,26 @@ impl Function {
240242
}
241243

242244
impl VectorKind {
243-
pub fn js_ty(&self) -> &str {
245+
pub fn js_ty(&self) -> String {
244246
match *self {
245-
VectorKind::String => "string",
246-
VectorKind::I8 => "Int8Array",
247-
VectorKind::U8 => "Uint8Array",
248-
VectorKind::ClampedU8 => "Uint8ClampedArray",
249-
VectorKind::I16 => "Int16Array",
250-
VectorKind::U16 => "Uint16Array",
251-
VectorKind::I32 => "Int32Array",
252-
VectorKind::U32 => "Uint32Array",
253-
VectorKind::I64 => "BigInt64Array",
254-
VectorKind::U64 => "BigUint64Array",
255-
VectorKind::F32 => "Float32Array",
256-
VectorKind::F64 => "Float64Array",
257-
VectorKind::Externref => "any[]",
247+
VectorKind::String => "string".to_string(),
248+
VectorKind::I8 => "Int8Array".to_string(),
249+
VectorKind::U8 => "Uint8Array".to_string(),
250+
VectorKind::ClampedU8 => "Uint8ClampedArray".to_string(),
251+
VectorKind::I16 => "Int16Array".to_string(),
252+
VectorKind::U16 => "Uint16Array".to_string(),
253+
VectorKind::I32 => "Int32Array".to_string(),
254+
VectorKind::U32 => "Uint32Array".to_string(),
255+
VectorKind::I64 => "BigInt64Array".to_string(),
256+
VectorKind::U64 => "BigUint64Array".to_string(),
257+
VectorKind::F32 => "Float32Array".to_string(),
258+
VectorKind::F64 => "Float64Array".to_string(),
259+
VectorKind::Externref => "any[]".to_string(),
260+
VectorKind::NamedExternref(name) => {
261+
let type_str = name.clone();
262+
type_str.push_str("[]");
263+
type_str
264+
}
258265
}
259266
}
260267

@@ -273,6 +280,7 @@ impl VectorKind {
273280
VectorKind::F32 => 4,
274281
VectorKind::F64 => 8,
275282
VectorKind::Externref => 4,
283+
VectorKind::NamedExternref(_) => 4,
276284
}
277285
}
278286
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
762762

763763
Instruction::VectorToMemory { kind, malloc, mem } => {
764764
let val = js.pop();
765-
let func = js.cx.pass_to_wasm_function(*kind, *mem)?;
765+
let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?;
766766
let malloc = js.cx.export_name_of(*malloc);
767767
let i = js.tmp();
768768
js.prelude(&format!(
@@ -805,7 +805,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
805805
}
806806

807807
Instruction::OptionVector { kind, mem, malloc } => {
808-
let func = js.cx.pass_to_wasm_function(*kind, *mem)?;
808+
let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?;
809809
js.cx.expose_is_like_none();
810810
let i = js.tmp();
811811
let malloc = js.cx.export_name_of(*malloc);
@@ -832,7 +832,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
832832
// a length. These two pointer/length values get pushed onto the
833833
// value stack.
834834
let val = js.pop();
835-
let func = js.cx.pass_to_wasm_function(*kind, *mem)?;
835+
let func = js.cx.pass_to_wasm_function(kind.clone(), *mem)?;
836836
let malloc = js.cx.export_name_of(*malloc);
837837
let i = js.tmp();
838838
js.prelude(&format!(
@@ -850,7 +850,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
850850
// original mutable slice with any modifications, and then free the
851851
// Rust-backed memory.
852852
let free = js.cx.export_name_of(*free);
853-
let get = js.cx.memview_function(*kind, *mem);
853+
let get = js.cx.memview_function(kind.clone(), *mem);
854854
js.finally(&format!(
855855
"
856856
{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) ->
10031003
Instruction::VectorLoad { kind, mem, free } => {
10041004
let len = js.pop();
10051005
let ptr = js.pop();
1006-
let f = js.cx.expose_get_vector_from_wasm(*kind, *mem)?;
1006+
let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?;
10071007
let i = js.tmp();
10081008
let free = js.cx.export_name_of(*free);
10091009
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) ->
10201020
Instruction::OptionVectorLoad { kind, mem, free } => {
10211021
let len = js.pop();
10221022
let ptr = js.pop();
1023-
let f = js.cx.expose_get_vector_from_wasm(*kind, *mem)?;
1023+
let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?;
10241024
let i = js.tmp();
10251025
let free = js.cx.export_name_of(*free);
10261026
js.prelude(&format!("let v{};", i));
@@ -1040,14 +1040,14 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
10401040
Instruction::View { kind, mem } => {
10411041
let len = js.pop();
10421042
let ptr = js.pop();
1043-
let f = js.cx.expose_get_vector_from_wasm(*kind, *mem)?;
1043+
let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?;
10441044
js.push(format!("{f}({ptr}, {len})", ptr = ptr, len = len, f = f));
10451045
}
10461046

10471047
Instruction::OptionView { kind, mem } => {
10481048
let len = js.pop();
10491049
let ptr = js.pop();
1050-
let f = js.cx.expose_get_vector_from_wasm(*kind, *mem)?;
1050+
let f = js.cx.expose_get_vector_from_wasm(kind.clone(), *mem)?;
10511051
js.push(format!(
10521052
"{ptr} === 0 ? undefined : {f}({ptr}, {len})",
10531053
ptr = ptr,
@@ -1226,7 +1226,7 @@ fn adapter2ts(ty: &AdapterType, dst: &mut String) {
12261226
AdapterType::String => dst.push_str("string"),
12271227
AdapterType::Externref => dst.push_str("any"),
12281228
AdapterType::Bool => dst.push_str("boolean"),
1229-
AdapterType::Vector(kind) => dst.push_str(kind.js_ty()),
1229+
AdapterType::Vector(kind) => dst.push_str(&kind.js_ty()),
12301230
AdapterType::Option(ty) => {
12311231
adapter2ts(ty, dst);
12321232
dst.push_str(" | undefined");

0 commit comments

Comments
 (0)