Skip to content

Commit

Permalink
fix: handle trait references correctly everywhere
Browse files Browse the repository at this point in the history
Fixes #229
  • Loading branch information
obycode committed Jan 2, 2024
1 parent 35b4e27 commit 7f64b0f
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions clarity/src/vm/clarity_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,9 @@ pub fn get_type_in_memory_size(ty: &TypeSignature, include_repr: bool) -> i32 {
}
size
}
TypeSignature::PrincipalType | TypeSignature::CallableType(_) => {
TypeSignature::PrincipalType
| TypeSignature::CallableType(_)
| TypeSignature::TraitReferenceType(_) => {
// Standard principal is a 1 byte version and a 20 byte Hash160.
// Then there is an int32 for the contract name length, followed by
// the contract name, which has a max length of 128.
Expand Down Expand Up @@ -683,8 +685,7 @@ pub fn get_type_in_memory_size(ty: &TypeSignature, include_repr: bool) -> i32 {
4 + get_type_in_memory_size(&res_types.0, true)
+ get_type_in_memory_size(&res_types.1, true)
}
TypeSignature::ListUnionType(_) => todo!(),
TypeSignature::TraitReferenceType(_) => todo!(),
TypeSignature::ListUnionType(_) => unreachable!("not a value type"),
}
}

Expand All @@ -699,10 +700,11 @@ pub fn is_in_memory_type(ty: &TypeSignature) -> bool {
| TypeSignature::TupleType(_)
| TypeSignature::OptionalType(_)
| TypeSignature::ResponseType(_) => false,
TypeSignature::SequenceType(_) | TypeSignature::PrincipalType => true,
TypeSignature::CallableType(_)
| TypeSignature::ListUnionType(_)
| TypeSignature::TraitReferenceType(_) => unreachable!("not a value type"),
TypeSignature::SequenceType(_)
| TypeSignature::PrincipalType
| TypeSignature::CallableType(_)
| TypeSignature::TraitReferenceType(_) => true,
TypeSignature::ListUnionType(_) => unreachable!("not a value type"),
}
}

Expand Down Expand Up @@ -861,7 +863,9 @@ fn read_from_wasm(
.map_err(|e| Error::Wasm(WasmError::Runtime(e.into())))?;
Value::string_utf8_from_unicode_scalars(buffer)
}
TypeSignature::PrincipalType => {
TypeSignature::PrincipalType
| TypeSignature::CallableType(_)
| TypeSignature::TraitReferenceType(_) => {
debug_assert!(
length >= STANDARD_PRINCIPAL_BYTES as i32 && length <= PRINCIPAL_BYTES_MAX as i32
);
Expand Down Expand Up @@ -1007,9 +1011,7 @@ fn read_from_wasm(
}
}
TypeSignature::NoType => todo!("type not yet implemented: {:?}", ty),
TypeSignature::CallableType(_subtype) => todo!("type not yet implemented: {:?}", ty),
TypeSignature::ListUnionType(_subtypes) => todo!("type not yet implemented: {:?}", ty),
TypeSignature::TraitReferenceType(_trait_id) => todo!("type not yet implemented: {:?}", ty),
}
}

Expand Down Expand Up @@ -1320,7 +1322,9 @@ fn write_to_wasm(
}
Ok((written, in_mem_written))
}
TypeSignature::PrincipalType => {
TypeSignature::PrincipalType
| TypeSignature::CallableType(_)
| TypeSignature::TraitReferenceType(_trait_id) => {
let principal = value_as_principal(&value)?;
let (standard, contract_name) = match principal {
PrincipalData::Standard(s) => (s, ""),
Expand Down Expand Up @@ -1417,8 +1421,7 @@ fn write_to_wasm(

Ok((written, in_mem_written))
}
TypeSignature::TraitReferenceType(_trait_id) => todo!("type not yet implemented: {:?}", ty),
TypeSignature::CallableType(_) | TypeSignature::ListUnionType(_) => {
TypeSignature::ListUnionType(_) => {
unreachable!("not a value type")
}
}
Expand Down Expand Up @@ -1566,7 +1569,8 @@ fn reserve_space_for_return<T>(
TypeSignature::NoType => Ok((vec![Val::I32(0)], offset)),
TypeSignature::SequenceType(_)
| TypeSignature::PrincipalType
| TypeSignature::CallableType(_) => {
| TypeSignature::CallableType(_)
| TypeSignature::TraitReferenceType(_) => {
// All in-memory types return an offset and length.˝
let length = get_type_in_memory_size(return_type, false);

Expand All @@ -1583,7 +1587,7 @@ fn reserve_space_for_return<T>(
}
Ok((vals, adjusted))
}
TypeSignature::ListUnionType(_) | TypeSignature::TraitReferenceType(_) => {
TypeSignature::ListUnionType(_) => {
unreachable!("not a valid return type");
}
}
Expand Down Expand Up @@ -1759,7 +1763,9 @@ fn wasm_to_clarity_value(
let value = read_from_wasm(memory, store, type_sig, offset, length, epoch)?;
Ok((Some(value), 2))
}
TypeSignature::PrincipalType | TypeSignature::CallableType(_) => {
TypeSignature::PrincipalType
| TypeSignature::CallableType(_)
| TypeSignature::TraitReferenceType(_t) => {
let offset = buffer[value_index]
.i32()
.ok_or(Error::Wasm(WasmError::ValueTypeMismatch))?;
Expand Down Expand Up @@ -1818,9 +1824,6 @@ fn wasm_to_clarity_value(
let tuple = TupleData::from_data(data_map)?;
Ok((Some(tuple.into()), index - value_index))
}
TypeSignature::TraitReferenceType(_t) => {
todo!("Wasm value type not implemented: {:?}", type_sig)
}
TypeSignature::ListUnionType(_lu) => {
todo!("Wasm value type not implemented: {:?}", type_sig)
}
Expand Down

0 comments on commit 7f64b0f

Please sign in to comment.