Skip to content

Commit

Permalink
[move] Merge CompiledModule and CompiledModuleMut
Browse files Browse the repository at this point in the history
As the previousn commit merged `CompiledScript` and `CompiledScriptMut`,
merge `CompiledModule` and `CompiledModuleMut`, and introduce a type
alias to have them refer to the same type.
  • Loading branch information
modocache authored and bors-libra committed Jul 7, 2021
1 parent 9dab5e8 commit f9b38d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
46 changes: 19 additions & 27 deletions language/move-binary-format/src/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1692,15 +1692,6 @@ impl CompiledScriptMut {
}
}

/// A `CompiledModule` defines the structure of a module which is the unit of published code.
///
/// A `CompiledModule` contains a definition of types (with their fields) and functions.
/// It is a unit of code that can be used by transactions or other modules.
///
/// A module is published as a single entry and it is retrieved as a single blob.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CompiledModule(CompiledModuleMut);

/// A mutable version of `CompiledModule`. Converting to a `CompiledModule` requires this to pass
/// the bounds checker.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
Expand Down Expand Up @@ -1744,6 +1735,14 @@ pub struct CompiledModuleMut {
pub function_defs: Vec<FunctionDefinition>,
}

/// A `CompiledModule` defines the structure of a module which is the unit of published code.
///
/// A `CompiledModule` contains a definition of types (with their fields) and functions.
/// It is a unit of code that can be used by transactions or other modules.
///
/// A module is published as a single entry and it is retrieved as a single blob.
pub type CompiledModule = CompiledModuleMut;

// Need a custom implementation of Arbitrary because as of proptest-derive 0.1.1, the derivation
// doesn't work for structs with more than 10 fields.
#[cfg(any(test, feature = "fuzzing"))]
Expand Down Expand Up @@ -1860,6 +1859,14 @@ impl Arbitrary for CompiledModuleMut {
impl CompiledModuleMut {
/// Returns the count of a specific `IndexKind`
pub fn kind_count(&self, kind: IndexKind) -> usize {
precondition!(!matches!(
kind,
IndexKind::LocalPool
| IndexKind::CodeDefinition
| IndexKind::FieldDefinition
| IndexKind::TypeParameter
| IndexKind::MemberCount
));
match kind {
IndexKind::ModuleHandle => self.module_handles.len(),
IndexKind::StructHandle => self.struct_handles.len(),
Expand Down Expand Up @@ -1888,35 +1895,20 @@ impl CompiledModuleMut {
/// consistency. This includes bounds checks but no others.
pub fn freeze(self) -> PartialVMResult<CompiledModule> {
// Impossible to access self_id for location as it might not be safe due to bounds failing
let module = CompiledModule(self);
let module = self;
BoundsChecker::verify_module(&module)?;
Ok(module)
}
}

impl CompiledModule {
/// Returns a reference to the inner `CompiledModuleMut`.
pub fn as_inner(&self) -> &CompiledModuleMut {
&self.0
self
}

/// Converts this instance into the inner `CompiledModuleMut`. Converting back to a
/// `CompiledModule` would require it to be verified again.
pub fn into_inner(self) -> CompiledModuleMut {
self.0
}

/// Returns the number of items of a specific `IndexKind`.
pub fn kind_count(&self, kind: IndexKind) -> usize {
precondition!(!matches!(
kind,
IndexKind::LocalPool
| IndexKind::CodeDefinition
| IndexKind::FieldDefinition
| IndexKind::TypeParameter
| IndexKind::MemberCount
));
self.as_inner().kind_count(kind)
self
}

/// Returns the code key of `module_handle`
Expand Down
10 changes: 0 additions & 10 deletions language/move-binary-format/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,6 @@ fn serialize_local_index(binary: &mut BinaryData, idx: u8) -> Result<()> {
impl CompiledModule {
/// Serializes a `CompiledModule` into a binary. The mutable `Vec<u8>` will contain the
/// binary blob on return.
pub fn serialize(&self, binary: &mut Vec<u8>) -> Result<()> {
self.as_inner().serialize(binary)
}
}

impl CompiledModuleMut {
/// Serializes this into a binary format.
///
/// This is intended mainly for test code. Production code will typically use
/// [`CompiledModule::serialize`].
pub fn serialize(&self, binary: &mut Vec<u8>) -> Result<()> {
let mut binary_data = BinaryData::from(binary.clone());
let mut ser = ModuleSerializer::new(VERSION_MAX);
Expand Down

0 comments on commit f9b38d0

Please sign in to comment.