Skip to content

Commit

Permalink
Handle nested descriptors
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver T <[email protected]>
  • Loading branch information
snOm3ad committed Aug 25, 2023
1 parent 783fbf7 commit a7d253b
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions crates/cli-support/src/wit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,27 +485,7 @@ impl<'a> Context<'a> {
let class = class.to_string();
match export.method_kind {
decode::MethodKind::Constructor => {
let msg = "Trying to return JS primitive from constructor, return value will be ignored. Use a builder instead (remove `constructor` attribute).";
match descriptor.ret {
Descriptor::I8
| Descriptor::U8
| Descriptor::ClampedU8
| Descriptor::I16
| Descriptor::U16
| Descriptor::I32
| Descriptor::U32
| Descriptor::F32
| Descriptor::F64
| Descriptor::I64
| Descriptor::U64
| Descriptor::Boolean
| Descriptor::Char
| Descriptor::CachedString
| Descriptor::String
| Descriptor::Option(_)
| Descriptor::Unit => bail!(msg),
_ => {}
}
verify_constructor_return(&class, &descriptor.ret)?;
AuxExportKind::Constructor(class)
}
decode::MethodKind::Operation(op) => {
Expand Down Expand Up @@ -1447,6 +1427,36 @@ impl<'a> Context<'a> {
}
}

/// Verifies exported constructor return value is not a JS primitive type
fn verify_constructor_return(class: &str, ret: &Descriptor) -> Result<(), Error> {
match ret {
Descriptor::I8
| Descriptor::U8
| Descriptor::ClampedU8
| Descriptor::I16
| Descriptor::U16
| Descriptor::I32
| Descriptor::U32
| Descriptor::F32
| Descriptor::F64
| Descriptor::I64
| Descriptor::U64
| Descriptor::Boolean
| Descriptor::Char
| Descriptor::CachedString
| Descriptor::String
| Descriptor::Option(_)
| Descriptor::Enum { .. }
| Descriptor::Unit => {
bail!("The constructor for class `{}` tries to return a JS primitive type, which would cause the return value to be ignored. Use a builder instead (remove the `constructor` attribute).", class);
}
Descriptor::Result(ref d) | Descriptor::Ref(ref d) | Descriptor::RefMut(ref d) => {
verify_constructor_return(class, d)
}
_ => Ok(()),
}
}

/// Extract all of the `Program`s encoded in our custom section.
///
/// `program_storage` is used to squirrel away the raw bytes of the custom
Expand Down

0 comments on commit a7d253b

Please sign in to comment.