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 021a5cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 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(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(name) => {
let 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,
}
}
}
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

0 comments on commit 021a5cc

Please sign in to comment.