Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
csgui committed Oct 3, 2024
1 parent 97b229c commit 4586738
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 52 deletions.
19 changes: 10 additions & 9 deletions clar2wasm/src/wasm_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ impl WasmGenerator {

let (val_offset, _) = self.create_call_stack_local(builder, &ty, false, true);
self.write_to_memory(builder, val_offset, 0, &ty)?;
let serialized_ty = Self::type_for_serialization(&ty).to_string();
let serialized_ty = self.type_for_serialization(&ty).to_string();

// Check, at compile time, if the type can be deserialized.
signature_from_string(
Expand Down Expand Up @@ -688,7 +688,10 @@ impl WasmGenerator {
Ok(())
}

fn type_for_serialization(ty: &TypeSignature) -> TypeSignature {
/// Try to change `ty` for serialization/deserialization (as stringified signature)
/// In case of failure, clones the input `ty`
#[allow(clippy::only_used_in_recursion)]
pub fn type_for_serialization(&self, ty: &TypeSignature) -> TypeSignature {
use clarity::vm::types::signatures::TypeSignature::*;
match ty {
// NoType and BoolType have the same size (both type and inner)
Expand All @@ -697,16 +700,14 @@ impl WasmGenerator {
CallableType(CallableSubtype::Trait(_)) => PrincipalType,
// Recursive types
ResponseType(types) => ResponseType(Box::new((
Self::type_for_serialization(&types.0),
Self::type_for_serialization(&types.1),
self.type_for_serialization(&types.0),
self.type_for_serialization(&types.1),
))),
OptionalType(value_ty) => {
OptionalType(Box::new(Self::type_for_serialization(value_ty)))
}
OptionalType(value_ty) => OptionalType(Box::new(self.type_for_serialization(value_ty))),
SequenceType(SequenceSubtype::ListType(list_ty)) => {
SequenceType(SequenceSubtype::ListType(
ListTypeData::new_list(
Self::type_for_serialization(list_ty.get_list_item_type()),
self.type_for_serialization(list_ty.get_list_item_type()),
list_ty.get_max_len(),
)
.unwrap_or_else(|_| list_ty.clone()),
Expand All @@ -717,7 +718,7 @@ impl WasmGenerator {
tuple_ty
.get_type_map()
.iter()
.map(|(k, v)| (k.clone(), Self::type_for_serialization(v)))
.map(|(k, v)| (k.clone(), self.type_for_serialization(v)))
.collect::<Vec<_>>(),
)
.unwrap_or_else(|_| tuple_ty.clone()),
Expand Down
45 changes: 2 additions & 43 deletions clar2wasm/src/words/print.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use clarity::vm::types::signatures::CallableSubtype;
use clarity::vm::types::{
ASCIIData, CharType, ListTypeData, SequenceSubtype, TupleTypeSignature, TypeSignature,
};
use clarity::vm::types::{ASCIIData, CharType};
use clarity::vm::{ClarityName, SymbolicExpression};

use super::ComplexWord;
Expand All @@ -11,44 +8,6 @@ use crate::wasm_utils::signature_from_string;
#[derive(Debug)]
pub struct Print;

/// Try to change `ty` for serialization/deserialization (as stringified signature)
/// In case of failure, clones the input `ty`
fn type_for_serialization(ty: &TypeSignature) -> TypeSignature {
use clarity::vm::types::signatures::TypeSignature::*;
match ty {
// NoType and BoolType have the same size (both type and inner)
NoType => BoolType,
// Avoid serialization like `(list 2 <S1G2081040G2081040G2081040G208105NK8PE5.my-trait.my-trait>)`
CallableType(CallableSubtype::Trait(_)) => PrincipalType,
// Recursive types
ResponseType(types) => ResponseType(Box::new((
type_for_serialization(&types.0),
type_for_serialization(&types.1),
))),
OptionalType(value_ty) => OptionalType(Box::new(type_for_serialization(value_ty))),
SequenceType(SequenceSubtype::ListType(list_ty)) => {
SequenceType(SequenceSubtype::ListType(
ListTypeData::new_list(
type_for_serialization(list_ty.get_list_item_type()),
list_ty.get_max_len(),
)
.unwrap_or_else(|_| list_ty.clone()),
))
}
TupleType(tuple_ty) => TupleType(
TupleTypeSignature::try_from(
tuple_ty
.get_type_map()
.iter()
.map(|(k, v)| (k.clone(), type_for_serialization(v)))
.collect::<Vec<_>>(),
)
.unwrap_or_else(|_| tuple_ty.clone()),
),
t => t.clone(),
}
}

impl ComplexWord for Print {
fn name(&self) -> ClarityName {
"print".into()
Expand All @@ -75,7 +34,7 @@ impl ComplexWord for Print {
.clone();
let val_locals = generator.save_to_locals(builder, &ty, true);

let ty_for_serde = type_for_serialization(&ty);
let ty_for_serde = generator.type_for_serialization(&ty);
let serialized_ty = ty_for_serde.to_string();
// Ensure (at compile time) type can be reconstructed
signature_from_string(
Expand Down

0 comments on commit 4586738

Please sign in to comment.