Rename LoadedModule to Module

This commit is contained in:
Sergey Pepyakin 2018-01-26 17:31:34 +03:00
parent aa4c8fe3bb
commit fa9e040778
8 changed files with 49 additions and 53 deletions

View File

@ -4,14 +4,14 @@ extern crate wasmi;
use std::env::args;
use std::fs::File;
use wasmi::{ModuleInstance, NopExternals, RuntimeValue, ImportsBuilder, load_from_buffer, LoadedModule};
use wasmi::{ModuleInstance, NopExternals, RuntimeValue, ImportsBuilder, Module};
fn load_from_file(filename: &str) -> LoadedModule {
fn load_from_file(filename: &str) -> Module {
use std::io::prelude::*;
let mut file = File::open(filename).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
load_from_buffer(buf).unwrap()
Module::from_buffer(buf).unwrap()
}
fn main() {

View File

@ -4,7 +4,7 @@ extern crate wasmi;
use std::env::args;
use parity_wasm::elements::{Internal, External, Type, FunctionType, ValueType};
use wasmi::{RuntimeValue, ModuleInstance, NopExternals, ImportsBuilder, load_from_module};
use wasmi::{RuntimeValue, ModuleInstance, NopExternals, ImportsBuilder};
fn main() {
@ -69,7 +69,7 @@ fn main() {
}).collect::<Vec<RuntimeValue>>()
};
let loaded_module = load_from_module(module).expect("Module to be valid");
let loaded_module = wasmi::Module::from_module(module).expect("Module to be valid");
// Intialize deserialized module. It adds module into It expects 3 parameters:
// - a name for the module

View File

@ -195,7 +195,7 @@ fn instantiate(path: &str) -> Result<ModuleRef, Error> {
let mut file = File::open(path).unwrap();
let mut wasm_buf = Vec::new();
file.read_to_end(&mut wasm_buf).unwrap();
wasmi::load_from_buffer(&wasm_buf)?
wasmi::Module::from_buffer(&wasm_buf)?
};
let mut imports = ImportsBuilder::new();

View File

@ -13,7 +13,7 @@ use wasmi::{
GlobalInstance, GlobalRef, ImportResolver, ImportsBuilder,
MemoryInstance, MemoryRef, ModuleImportResolver, ModuleInstance,
ModuleRef, RuntimeValue, TableInstance, TableRef, ValueType,
load_from_buffer, LoadedModule, Signature, MemoryDescriptor,
Module, Signature, MemoryDescriptor,
TableDescriptor, GlobalDescriptor, FuncInstance, RuntimeArgs,
};
@ -225,7 +225,7 @@ impl ImportResolver for SpecDriver {
}
}
fn try_load_module(base_dir: &Path, module_path: &str) -> Result<LoadedModule, Error> {
fn try_load_module(base_dir: &Path, module_path: &str) -> Result<Module, Error> {
use std::io::prelude::*;
let mut wasm_path = PathBuf::from(base_dir.clone());
@ -233,7 +233,7 @@ fn try_load_module(base_dir: &Path, module_path: &str) -> Result<LoadedModule, E
let mut file = File::open(wasm_path).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
load_from_buffer(buf).map_err(|e| Error::Load(e.to_string()))
Module::from_buffer(buf).map_err(|e| Error::Load(e.to_string()))
}
fn try_load(

View File

@ -105,7 +105,6 @@ extern crate byteorder;
use std::fmt;
use std::error;
use std::collections::HashMap;
use parity_wasm::elements::Module;
/// Internal interpreter error.
#[derive(Debug)]
@ -231,40 +230,37 @@ pub use self::global::{GlobalInstance, GlobalRef};
pub use self::func::{FuncInstance, FuncRef};
pub use self::types::{Signature, ValueType, GlobalDescriptor, TableDescriptor, MemoryDescriptor};
pub struct LoadedModule {
/// Deserialized module prepared for instantiation.
pub struct Module {
labels: HashMap<usize, HashMap<usize, usize>>,
module: Module,
module: parity_wasm::elements::Module,
}
impl LoadedModule {
pub(crate) fn module(&self) -> &Module {
impl Module {
pub fn from_module(module: parity_wasm::elements::Module) -> Result<Module, Error> {
use validation::{validate_module, ValidatedModule};
let ValidatedModule {
labels,
module,
} = validate_module(module)?;
Ok(Module {
labels,
module,
})
}
pub fn from_buffer<B: AsRef<[u8]>>(buffer: B) -> Result<Module, Error> {
let module = parity_wasm::elements::deserialize_buffer(buffer.as_ref())
.map_err(|e: parity_wasm::elements::Error| Error::Validation(e.to_string()))?;
Module::from_module(module)
}
pub(crate) fn module(&self) -> &parity_wasm::elements::Module {
&self.module
}
pub(crate) fn labels(&self) -> &HashMap<usize, HashMap<usize, usize>> {
&self.labels
}
pub fn into_module(self) -> Module {
self.module
}
}
pub fn load_from_module(module: Module) -> Result<LoadedModule, Error> {
use validation::{validate_module, ValidatedModule};
let ValidatedModule {
labels,
module,
} = validate_module(module)?;
Ok(LoadedModule {
labels,
module,
})
}
pub fn load_from_buffer<B: AsRef<[u8]>>(buffer: B) -> Result<LoadedModule, Error> {
let module = parity_wasm::elements::deserialize_buffer(buffer.as_ref())
.map_err(|e: parity_wasm::elements::Error| Error::Validation(e.to_string()))?;
load_from_module(module)
}

View File

@ -3,7 +3,7 @@ use std::cell::RefCell;
use std::fmt;
use std::collections::HashMap;
use parity_wasm::elements::{External, InitExpr, Internal, Opcode, ResizableLimits, Type};
use {LoadedModule, Error, Signature, MemoryInstance, RuntimeValue, TableInstance};
use {Module, Error, Signature, MemoryInstance, RuntimeValue, TableInstance};
use imports::ImportResolver;
use global::{GlobalInstance, GlobalRef};
use func::{FuncRef, FuncBody, FuncInstance};
@ -100,9 +100,9 @@ impl ExternVal {
}
}
/// A module instance is the runtime representation of a [module][`LoadedModule`].
/// A module instance is the runtime representation of a [module][`Module`].
///
/// It is created by instantiating a [module][`LoadedModule`], and collects runtime representations
/// It is created by instantiating a [module][`Module`], and collects runtime representations
/// of all entities that are imported or defined by the module, namely:
///
/// - [functions][`FuncInstance`],
@ -115,7 +115,7 @@ impl ExternVal {
///
/// After module is instantiated you can start invoking it's exported functions with [`invoke_export`].
///
/// [`LoadedModule`]: struct.LoadedModule.html
/// [`Module`]: struct.Module.html
/// [`FuncInstance`]: struct.FuncInstance.html
/// [`MemoryInstance`]: struct.MemoryInstance.html
/// [`TableInstance`]: struct.TableInstance.html
@ -188,7 +188,7 @@ impl ModuleInstance {
}
fn alloc_module(
loaded_module: &LoadedModule,
loaded_module: &Module,
extern_vals: &[ExternVal]
) -> Result<ModuleRef, Error> {
let module = loaded_module.module();
@ -357,7 +357,7 @@ impl ModuleInstance {
}
fn instantiate_with_externvals(
loaded_module: &LoadedModule,
loaded_module: &Module,
extern_vals: &[ExternVal],
) -> Result<ModuleRef, Error> {
let module = loaded_module.module();
@ -400,7 +400,7 @@ impl ModuleInstance {
Ok(module_ref)
}
/// Instantiate a [module][`LoadedModule`].
/// Instantiate a [module][`Module`].
///
/// Note that in case of successful instantiation this function returns a reference to
/// a module which `start` function is not called.
@ -454,11 +454,11 @@ impl ModuleInstance {
/// # }
/// ```
///
/// [`LoadedModule`]: struct.LoadedModule.html
/// [`Module`]: struct.Module.html
/// [`ImportResolver`]: trait.ImportResolver.html
/// [`assert_no_start`]: struct.NotStartedModuleRef.html#method.assert_no_start
pub fn new<'m, I: ImportResolver>(
loaded_module: &'m LoadedModule,
loaded_module: &'m Module,
imports: &I,
) -> Result<NotStartedModuleRef<'m>, Error> {
let module = loaded_module.module();
@ -584,7 +584,7 @@ impl ModuleInstance {
}
pub struct NotStartedModuleRef<'a> {
loaded_module: &'a LoadedModule,
loaded_module: &'a Module,
instance: ModuleRef,
}

View File

@ -1,7 +1,7 @@
use {
Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder,
MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef,
RuntimeValue, RuntimeArgs, LoadedModule, load_from_buffer, TableDescriptor, MemoryDescriptor,
RuntimeValue, RuntimeArgs, Module, TableDescriptor, MemoryDescriptor,
};
use types::ValueType;
use wabt::wat2wasm;
@ -200,9 +200,9 @@ impl ModuleImportResolver for TestHost {
}
}
fn parse_wat(source: &str) -> LoadedModule {
fn parse_wat(source: &str) -> Module {
let wasm_binary = wat2wasm(source).expect("Failed to parse wat source");
load_from_buffer(wasm_binary).expect("Failed to load parsed module")
Module::from_buffer(wasm_binary).expect("Failed to load parsed module")
}
#[test]

View File

@ -1,7 +1,7 @@
use {
Error, Signature, FuncRef, GlobalInstance, GlobalRef, ImportsBuilder, MemoryInstance,
MemoryRef, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeValue,
TableInstance, TableRef, LoadedModule, load_from_buffer, GlobalDescriptor, TableDescriptor, MemoryDescriptor,
TableInstance, TableRef, Module, GlobalDescriptor, TableDescriptor, MemoryDescriptor,
};
use std::fs::File;
@ -69,12 +69,12 @@ impl ModuleImportResolver for Env {
}
}
fn load_from_file(filename: &str) -> LoadedModule {
fn load_from_file(filename: &str) -> Module {
use std::io::prelude::*;
let mut file = File::open(filename).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
load_from_buffer(buf).unwrap()
Module::from_buffer(buf).unwrap()
}
#[test]