Skip to content

Commit

Permalink
Add proper TypeScript types for NamedExternref Vecs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jules-Bertholet committed Jul 10, 2021
1 parent afdbda2 commit fcb0491
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 39 deletions.
38 changes: 23 additions & 15 deletions crates/cli-support/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct Closure {
pub mutable: bool,
}

#[derive(Copy, Clone, Debug)]
#[derive(Clone, Debug)]
pub enum VectorKind {
I8,
U8,
Expand All @@ -101,6 +101,7 @@ pub enum VectorKind {
F64,
String,
Externref,
NamedExternref(String),
}

impl Descriptor {
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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
}
}
}

Expand All @@ -273,6 +280,7 @@ impl VectorKind {
VectorKind::F32 => 4,
VectorKind::F64 => 8,
VectorKind::Externref => 4,
VectorKind::NamedExternref(_) => 4,
}
}
}
14 changes: 7 additions & 7 deletions crates/cli-support/src/externref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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);
Expand All @@ -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!(
Expand All @@ -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}));
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 3 additions & 0 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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),
}
}

Expand All @@ -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)?,
})
}

Expand Down
8 changes: 4 additions & 4 deletions crates/cli-support/src/wit/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?,
Expand Down Expand Up @@ -198,7 +198,7 @@ impl InstructionBuilder<'_, '_> {
})?;
if mutable {
self.instruction(
&[AdapterType::Vector(kind)],
&[AdapterType::Vector(kind.clone())],
Instruction::MutableSliceToMemory {
kind,
malloc: self.cx.malloc()?,
Expand All @@ -209,7 +209,7 @@ impl InstructionBuilder<'_, '_> {
);
} else {
self.instruction(
&[AdapterType::Vector(kind)],
&[AdapterType::Vector(kind.clone())],
Instruction::VectorToMemory {
kind,
malloc: self.cx.malloc()?,
Expand Down Expand Up @@ -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],
);
Expand Down
22 changes: 18 additions & 4 deletions crates/cli-support/src/wit/outgoing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
);
}
Expand Down Expand Up @@ -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)],
);
}
Expand Down Expand Up @@ -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()],
);
}
Expand Down Expand Up @@ -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()],
);
}
Expand Down

0 comments on commit fcb0491

Please sign in to comment.