Skip to content

Commit

Permalink
[vm_runtime] remove duplicate code to convert CompiledScript to Compi…
Browse files Browse the repository at this point in the history
…ledModule

This code already exists in `vm`.
  • Loading branch information
sunshowers authored and calibra-opensource committed Jun 18, 2019
1 parent 887730f commit a0341ce
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 51 deletions.
3 changes: 3 additions & 0 deletions language/vm/src/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,9 @@ pub struct CompiledScript {
}

impl CompiledScript {
/// Returns the index of `main` in case a script is converted to a module.
pub const MAIN_INDEX: FunctionDefinitionIndex = FunctionDefinitionIndex(0);

/// Converts a `CompiledScript` to a `CompiledModule` for code that wants a uniform view of
/// both.
pub fn into_module(self) -> CompiledModule {
Expand Down
26 changes: 2 additions & 24 deletions language/vm/vm_runtime/src/code_cache/module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use vm::{
access::{BaseAccess, ModuleAccess},
errors::*,
file_format::{
CompiledModule, CompiledScript, FunctionDefinitionIndex, FunctionHandleIndex,
SignatureToken, StructDefinitionIndex, StructHandleIndex,
CompiledModule, FunctionHandleIndex, SignatureToken, StructDefinitionIndex,
StructHandleIndex,
},
views::{FunctionHandleView, StructHandleView},
};
Expand Down Expand Up @@ -120,28 +120,6 @@ pub struct VMModuleCache<'alloc> {
map: CacheRefMap<'alloc, CodeKey, LoadedModule>,
}

/// Convert a CompiledScript into a CompiledModule.
pub fn create_fake_module(script: CompiledScript) -> (CompiledModule, FunctionDefinitionIndex) {
(
CompiledModule {
module_handles: script.module_handles,
struct_handles: script.struct_handles,
function_handles: script.function_handles,

struct_defs: vec![],
field_defs: vec![],
function_defs: vec![script.main],
type_signatures: script.type_signatures,
function_signatures: script.function_signatures,
locals_signatures: script.locals_signatures,
string_pool: script.string_pool,
byte_array_pool: script.byte_array_pool,
address_pool: script.address_pool,
},
FunctionDefinitionIndex::new(0),
)
}

impl<'alloc> VMModuleCache<'alloc> {
/// In order
/// to get a cleaner lifetime, the loaded program trait will take an input parameter of Arena
Expand Down
13 changes: 5 additions & 8 deletions language/vm/vm_runtime/src/code_cache/script_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
//! Cache for commonly executed scripts

use crate::{
code_cache::module_cache::create_fake_module,
loaded_data::{
function::{FunctionRef, FunctionReference},
loaded_module::LoadedModule,
},
use crate::loaded_data::{
function::{FunctionRef, FunctionReference},
loaded_module::LoadedModule,
};
use logger::prelude::*;
use tiny_keccak::Keccak;
Expand Down Expand Up @@ -46,13 +43,13 @@ impl<'alloc> ScriptCache<'alloc> {
Ok(Ok(f))
} else {
trace!("[VM] Script cache miss");
let (fake_module, idx) = create_fake_module(script);
let fake_module = script.into_module();
let loaded_module = LoadedModule::new(fake_module)?;
self.map
.or_insert_with_try_transform(
hash,
move || loaded_module,
|module_ref| FunctionRef::new(module_ref, idx),
|module_ref| FunctionRef::new(module_ref, CompiledScript::MAIN_INDEX),
)
.map(Ok)
}
Expand Down
6 changes: 3 additions & 3 deletions language/vm/vm_runtime/src/txn_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Processor for a single transaction.

use crate::{
code_cache::module_cache::{create_fake_module, ModuleCache, VMModuleCache},
code_cache::module_cache::{ModuleCache, VMModuleCache},
data_cache::{RemoteCache, TransactionDataCache},
execution_stack::ExecutionStack,
gas_meter::GasMeter,
Expand Down Expand Up @@ -818,9 +818,9 @@ pub fn execute_function(
) -> VMResult<()> {
let allocator = Arena::new();
let module_cache = VMModuleCache::new(&allocator);
let (main_module, entry_idx) = create_fake_module(caller_script);
let main_module = caller_script.into_module();
let loaded_main = LoadedModule::new(main_module)?;
let entry_func = FunctionRef::new(&loaded_main, entry_idx)?;
let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX)?;
for m in modules {
module_cache.cache_module(m)?;
}
Expand Down
17 changes: 7 additions & 10 deletions language/vm/vm_runtime/src/unit_tests/module_cache_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

use super::*;
use crate::{
code_cache::{
module_adapter::FakeFetcher,
module_cache::{create_fake_module, ModuleCache},
},
code_cache::{module_adapter::FakeFetcher, module_cache::ModuleCache},
loaded_data::function::{FunctionRef, FunctionReference},
};
use ::compiler::{compiler, parser::parse_program};
Expand Down Expand Up @@ -178,9 +175,9 @@ fn test_loader_cross_modules() {
let loaded_program = VMModuleCache::new(&allocator);
loaded_program.cache_module(module).unwrap();

let (owned_entry_module, entry_idx) = create_fake_module(script);
let owned_entry_module = script.into_module();
let loaded_main = LoadedModule::new(owned_entry_module).unwrap();
let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap();
let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap();
let entry_module = entry_func.module();
let func1 = loaded_program
.resolve_function_ref(entry_module, FunctionHandleIndex::new(0))
Expand Down Expand Up @@ -209,9 +206,9 @@ fn test_loader_cross_modules() {
fn test_cache_with_storage() {
let allocator = Arena::new();

let (owned_entry_module, entry_idx) = create_fake_module(test_script());
let owned_entry_module = test_script().into_module();
let loaded_main = LoadedModule::new(owned_entry_module).unwrap();
let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap();
let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap();
let entry_module = entry_func.module();

let vm_cache = VMModuleCache::new(&allocator);
Expand Down Expand Up @@ -375,9 +372,9 @@ fn test_multi_level_cache_write_back() {
address_pool: vec![AccountAddress::default()],
};

let (owned_entry_module, entry_idx) = create_fake_module(script);
let owned_entry_module = script.into_module();
let loaded_main = LoadedModule::new(owned_entry_module).unwrap();
let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap();
let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap();
let entry_module = entry_func.module();

{
Expand Down
12 changes: 6 additions & 6 deletions language/vm/vm_runtime/src/unit_tests/runtime_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ fn test_binop_instruction_overflow<'alloc, 'txn>(
fn test_simple_instruction_transition() {
let allocator = Arena::new();
let module_cache = VMModuleCache::new(&allocator);
let (main_module, entry_idx) = create_fake_module(fake_script());
let main_module = fake_script().into_module();
let loaded_main = LoadedModule::new(main_module).unwrap();
let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap();
let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap();
let data_cache = FakeDataCache::new();
let mut vm =
TransactionExecutor::new(module_cache, &data_cache, TransactionMetadata::default());
Expand Down Expand Up @@ -343,9 +343,9 @@ fn test_simple_instruction_transition() {
fn test_arith_instructions() {
let allocator = Arena::new();
let module_cache = VMModuleCache::new(&allocator);
let (main_module, entry_idx) = create_fake_module(fake_script());
let main_module = fake_script().into_module();
let loaded_main = LoadedModule::new(main_module).unwrap();
let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap();
let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap();
let data_cache = FakeDataCache::new();

let mut vm =
Expand Down Expand Up @@ -683,9 +683,9 @@ fn test_call() {
fn test_transaction_info() {
let allocator = Arena::new();
let module_cache = VMModuleCache::new(&allocator);
let (main_module, entry_idx) = create_fake_module(fake_script());
let main_module = fake_script().into_module();
let loaded_main = LoadedModule::new(main_module).unwrap();
let entry_func = FunctionRef::new(&loaded_main, entry_idx).unwrap();
let entry_func = FunctionRef::new(&loaded_main, CompiledScript::MAIN_INDEX).unwrap();

let txn_info = {
let (_, public_key) = crypto::signing::generate_genesis_keypair();
Expand Down

0 comments on commit a0341ce

Please sign in to comment.