Rename LoadedModule to Module
This commit is contained in:
parent
aa4c8fe3bb
commit
fa9e040778
|
@ -4,14 +4,14 @@ extern crate wasmi;
|
||||||
|
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::fs::File;
|
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::*;
|
use std::io::prelude::*;
|
||||||
let mut file = File::open(filename).unwrap();
|
let mut file = File::open(filename).unwrap();
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
file.read_to_end(&mut buf).unwrap();
|
file.read_to_end(&mut buf).unwrap();
|
||||||
load_from_buffer(buf).unwrap()
|
Module::from_buffer(buf).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -4,7 +4,7 @@ extern crate wasmi;
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
|
|
||||||
use parity_wasm::elements::{Internal, External, Type, FunctionType, ValueType};
|
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() {
|
fn main() {
|
||||||
|
@ -69,7 +69,7 @@ fn main() {
|
||||||
}).collect::<Vec<RuntimeValue>>()
|
}).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:
|
// Intialize deserialized module. It adds module into It expects 3 parameters:
|
||||||
// - a name for the module
|
// - a name for the module
|
||||||
|
|
|
@ -195,7 +195,7 @@ fn instantiate(path: &str) -> Result<ModuleRef, Error> {
|
||||||
let mut file = File::open(path).unwrap();
|
let mut file = File::open(path).unwrap();
|
||||||
let mut wasm_buf = Vec::new();
|
let mut wasm_buf = Vec::new();
|
||||||
file.read_to_end(&mut wasm_buf).unwrap();
|
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();
|
let mut imports = ImportsBuilder::new();
|
||||||
|
|
|
@ -13,7 +13,7 @@ use wasmi::{
|
||||||
GlobalInstance, GlobalRef, ImportResolver, ImportsBuilder,
|
GlobalInstance, GlobalRef, ImportResolver, ImportsBuilder,
|
||||||
MemoryInstance, MemoryRef, ModuleImportResolver, ModuleInstance,
|
MemoryInstance, MemoryRef, ModuleImportResolver, ModuleInstance,
|
||||||
ModuleRef, RuntimeValue, TableInstance, TableRef, ValueType,
|
ModuleRef, RuntimeValue, TableInstance, TableRef, ValueType,
|
||||||
load_from_buffer, LoadedModule, Signature, MemoryDescriptor,
|
Module, Signature, MemoryDescriptor,
|
||||||
TableDescriptor, GlobalDescriptor, FuncInstance, RuntimeArgs,
|
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::*;
|
use std::io::prelude::*;
|
||||||
|
|
||||||
let mut wasm_path = PathBuf::from(base_dir.clone());
|
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 file = File::open(wasm_path).unwrap();
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
file.read_to_end(&mut buf).unwrap();
|
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(
|
fn try_load(
|
||||||
|
|
40
src/lib.rs
40
src/lib.rs
|
@ -105,7 +105,6 @@ extern crate byteorder;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use parity_wasm::elements::Module;
|
|
||||||
|
|
||||||
/// Internal interpreter error.
|
/// Internal interpreter error.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -231,40 +230,37 @@ pub use self::global::{GlobalInstance, GlobalRef};
|
||||||
pub use self::func::{FuncInstance, FuncRef};
|
pub use self::func::{FuncInstance, FuncRef};
|
||||||
pub use self::types::{Signature, ValueType, GlobalDescriptor, TableDescriptor, MemoryDescriptor};
|
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>>,
|
labels: HashMap<usize, HashMap<usize, usize>>,
|
||||||
module: Module,
|
module: parity_wasm::elements::Module,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LoadedModule {
|
impl Module {
|
||||||
pub(crate) fn module(&self) -> &Module {
|
pub fn from_module(module: parity_wasm::elements::Module) -> Result<Module, Error> {
|
||||||
&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};
|
use validation::{validate_module, ValidatedModule};
|
||||||
let ValidatedModule {
|
let ValidatedModule {
|
||||||
labels,
|
labels,
|
||||||
module,
|
module,
|
||||||
} = validate_module(module)?;
|
} = validate_module(module)?;
|
||||||
|
|
||||||
Ok(LoadedModule {
|
Ok(Module {
|
||||||
labels,
|
labels,
|
||||||
module,
|
module,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_from_buffer<B: AsRef<[u8]>>(buffer: B) -> Result<LoadedModule, Error> {
|
pub fn from_buffer<B: AsRef<[u8]>>(buffer: B) -> Result<Module, Error> {
|
||||||
let module = parity_wasm::elements::deserialize_buffer(buffer.as_ref())
|
let module = parity_wasm::elements::deserialize_buffer(buffer.as_ref())
|
||||||
.map_err(|e: parity_wasm::elements::Error| Error::Validation(e.to_string()))?;
|
.map_err(|e: parity_wasm::elements::Error| Error::Validation(e.to_string()))?;
|
||||||
load_from_module(module)
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::cell::RefCell;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use parity_wasm::elements::{External, InitExpr, Internal, Opcode, ResizableLimits, Type};
|
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 imports::ImportResolver;
|
||||||
use global::{GlobalInstance, GlobalRef};
|
use global::{GlobalInstance, GlobalRef};
|
||||||
use func::{FuncRef, FuncBody, FuncInstance};
|
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:
|
/// of all entities that are imported or defined by the module, namely:
|
||||||
///
|
///
|
||||||
/// - [functions][`FuncInstance`],
|
/// - [functions][`FuncInstance`],
|
||||||
|
@ -115,7 +115,7 @@ impl ExternVal {
|
||||||
///
|
///
|
||||||
/// After module is instantiated you can start invoking it's exported functions with [`invoke_export`].
|
/// 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
|
/// [`FuncInstance`]: struct.FuncInstance.html
|
||||||
/// [`MemoryInstance`]: struct.MemoryInstance.html
|
/// [`MemoryInstance`]: struct.MemoryInstance.html
|
||||||
/// [`TableInstance`]: struct.TableInstance.html
|
/// [`TableInstance`]: struct.TableInstance.html
|
||||||
|
@ -188,7 +188,7 @@ impl ModuleInstance {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn alloc_module(
|
fn alloc_module(
|
||||||
loaded_module: &LoadedModule,
|
loaded_module: &Module,
|
||||||
extern_vals: &[ExternVal]
|
extern_vals: &[ExternVal]
|
||||||
) -> Result<ModuleRef, Error> {
|
) -> Result<ModuleRef, Error> {
|
||||||
let module = loaded_module.module();
|
let module = loaded_module.module();
|
||||||
|
@ -357,7 +357,7 @@ impl ModuleInstance {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn instantiate_with_externvals(
|
fn instantiate_with_externvals(
|
||||||
loaded_module: &LoadedModule,
|
loaded_module: &Module,
|
||||||
extern_vals: &[ExternVal],
|
extern_vals: &[ExternVal],
|
||||||
) -> Result<ModuleRef, Error> {
|
) -> Result<ModuleRef, Error> {
|
||||||
let module = loaded_module.module();
|
let module = loaded_module.module();
|
||||||
|
@ -400,7 +400,7 @@ impl ModuleInstance {
|
||||||
Ok(module_ref)
|
Ok(module_ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Instantiate a [module][`LoadedModule`].
|
/// Instantiate a [module][`Module`].
|
||||||
///
|
///
|
||||||
/// Note that in case of successful instantiation this function returns a reference to
|
/// Note that in case of successful instantiation this function returns a reference to
|
||||||
/// a module which `start` function is not called.
|
/// 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
|
/// [`ImportResolver`]: trait.ImportResolver.html
|
||||||
/// [`assert_no_start`]: struct.NotStartedModuleRef.html#method.assert_no_start
|
/// [`assert_no_start`]: struct.NotStartedModuleRef.html#method.assert_no_start
|
||||||
pub fn new<'m, I: ImportResolver>(
|
pub fn new<'m, I: ImportResolver>(
|
||||||
loaded_module: &'m LoadedModule,
|
loaded_module: &'m Module,
|
||||||
imports: &I,
|
imports: &I,
|
||||||
) -> Result<NotStartedModuleRef<'m>, Error> {
|
) -> Result<NotStartedModuleRef<'m>, Error> {
|
||||||
let module = loaded_module.module();
|
let module = loaded_module.module();
|
||||||
|
@ -584,7 +584,7 @@ impl ModuleInstance {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct NotStartedModuleRef<'a> {
|
pub struct NotStartedModuleRef<'a> {
|
||||||
loaded_module: &'a LoadedModule,
|
loaded_module: &'a Module,
|
||||||
instance: ModuleRef,
|
instance: ModuleRef,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use {
|
use {
|
||||||
Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder,
|
Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder,
|
||||||
MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef,
|
MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef,
|
||||||
RuntimeValue, RuntimeArgs, LoadedModule, load_from_buffer, TableDescriptor, MemoryDescriptor,
|
RuntimeValue, RuntimeArgs, Module, TableDescriptor, MemoryDescriptor,
|
||||||
};
|
};
|
||||||
use types::ValueType;
|
use types::ValueType;
|
||||||
use wabt::wat2wasm;
|
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");
|
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]
|
#[test]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use {
|
use {
|
||||||
Error, Signature, FuncRef, GlobalInstance, GlobalRef, ImportsBuilder, MemoryInstance,
|
Error, Signature, FuncRef, GlobalInstance, GlobalRef, ImportsBuilder, MemoryInstance,
|
||||||
MemoryRef, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeValue,
|
MemoryRef, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeValue,
|
||||||
TableInstance, TableRef, LoadedModule, load_from_buffer, GlobalDescriptor, TableDescriptor, MemoryDescriptor,
|
TableInstance, TableRef, Module, GlobalDescriptor, TableDescriptor, MemoryDescriptor,
|
||||||
};
|
};
|
||||||
use std::fs::File;
|
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::*;
|
use std::io::prelude::*;
|
||||||
let mut file = File::open(filename).unwrap();
|
let mut file = File::open(filename).unwrap();
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
file.read_to_end(&mut buf).unwrap();
|
file.read_to_end(&mut buf).unwrap();
|
||||||
load_from_buffer(buf).unwrap()
|
Module::from_buffer(buf).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue