Skip to content

Commit

Permalink
[move-cm][closures] Refactor: Move type conversions out of Loader i…
Browse files Browse the repository at this point in the history
…nto a trait

Type conversions from runtime types to `MoveTypeLayout` and `TypeTag` currently are associated with the `Loader` type. However, they are needed for the `FunctionValueExtension` trait which needs to be constructed in contexts where no loader but only `ModuleStorage` exists.

This PR moves the conversion functions into a new trait `TypeConverter`. The trait is then implemented two times based on `ModuleStorage` only and based on the existing `Loader`, for downwards compatibility.
  • Loading branch information
wrwg committed Jan 5, 2025
1 parent ee49138 commit 9c0814c
Show file tree
Hide file tree
Showing 5 changed files with 641 additions and 504 deletions.
31 changes: 28 additions & 3 deletions third_party/move/move-core/types/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

use crate::{
account_address::AccountAddress,
identifier::Identifier,
language_storage::{StructTag, TypeTag},
ident_str,
identifier::{IdentStr, Identifier},
language_storage::{ModuleId, StructTag, TypeTag},
u256,
};
use anyhow::{anyhow, bail, Result as AResult};
Expand Down Expand Up @@ -182,6 +183,30 @@ pub enum IdentifierMappingKind {
DerivedString,
}

impl IdentifierMappingKind {
/// If the struct identifier has a special mapping, return it.
pub fn from_ident(
module_id: &ModuleId,
struct_id: &Identifier,
) -> Option<IdentifierMappingKind> {
let ident_str_to_kind = |ident_str: &IdentStr| -> Option<IdentifierMappingKind> {
if ident_str.eq(ident_str!("Aggregator")) {
Some(IdentifierMappingKind::Aggregator)
} else if ident_str.eq(ident_str!("AggregatorSnapshot")) {
Some(IdentifierMappingKind::Snapshot)
} else if ident_str.eq(ident_str!("DerivedStringSnapshot")) {
Some(IdentifierMappingKind::DerivedString)
} else {
None
}
};
(module_id.address().eq(&AccountAddress::ONE)
&& module_id.name().eq(ident_str!("aggregator_v2")))
.then(|| ident_str_to_kind(struct_id.as_ident_str()))
.flatten()
}
}

#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(
any(test, feature = "fuzzing"),
Expand Down Expand Up @@ -822,7 +847,7 @@ impl fmt::Display for MoveTypeLayout {
U256 => write!(f, "u256"),
Address => write!(f, "address"),
Vector(typ) => write!(f, "vector<{}>", typ),
Struct(s) => write!(f, "{}", s),
Struct(s) => fmt::Display::fmt(s, f),
Signer => write!(f, "signer"),
// TODO[agg_v2](cleanup): consider printing the tag as well.
Native(_, typ) => write!(f, "native<{}>", typ),
Expand Down
1 change: 1 addition & 0 deletions third_party/move/move-vm/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ pub use storage::{
},
module_storage::{ambassador_impl_ModuleStorage, AsFunctionValueExtension, ModuleStorage},
publishing::{StagingModuleStorage, VerifiedModuleBundle},
type_converter::{ModuleStorageTypeConverter, TypeConverter},
};
Loading

0 comments on commit 9c0814c

Please sign in to comment.