Move code under prepare

This commit is contained in:
Sergey Pepyakin 2019-04-08 16:20:21 +02:00
parent fc3d21a17a
commit b7a94855d8
5 changed files with 1306 additions and 1290 deletions

View File

@ -389,6 +389,7 @@ mod isa;
mod memory; mod memory;
mod module; mod module;
pub mod nan_preserving_float; pub mod nan_preserving_float;
mod prepare;
mod runner; mod runner;
mod table; mod table;
mod types; mod types;
@ -454,8 +455,7 @@ impl Module {
/// } /// }
/// ``` /// ```
pub fn from_parity_wasm_module(module: parity_wasm::elements::Module) -> Result<Module, Error> { pub fn from_parity_wasm_module(module: parity_wasm::elements::Module) -> Result<Module, Error> {
use validation::{validate_module, ValidatedModule}; let prepare::CompiledModule { code_map, module } = prepare::compile_module(module)?;
let ValidatedModule { code_map, module } = validate_module(module)?;
Ok(Module { code_map, module }) Ok(Module { code_map, module })
} }

1239
src/prepare/compile.rs Normal file

File diff suppressed because it is too large Load Diff

37
src/prepare/mod.rs Normal file
View File

@ -0,0 +1,37 @@
use crate::{isa, validation::{Error, validate_module2, Validation}};
use parity_wasm::elements::Module;
mod compile;
#[derive(Clone)]
pub struct CompiledModule {
pub code_map: Vec<isa::Instructions>,
pub module: Module,
}
pub struct WasmiValidation {
code_map: Vec<isa::Instructions>,
}
impl Validation for WasmiValidation {
type Output = Vec<isa::Instructions>;
type FunctionValidator = compile::Compiler;
fn new(_module: &Module) -> Self {
WasmiValidation {
// TODO: with capacity?
code_map: Vec::new(),
}
}
fn on_function_validated(&mut self, _index: u32, output: isa::Instructions) {
self.code_map.push(output);
}
fn finish(self) -> Vec<isa::Instructions> {
self.code_map
}
}
/// Validate a module and compile it to the internal representation.
pub fn compile_module(module: Module) -> Result<CompiledModule, Error> {
let code_map = validate_module2::<WasmiValidation>(&module)?;
Ok(CompiledModule { module, code_map })
}

File diff suppressed because it is too large Load Diff

View File

@ -18,15 +18,18 @@ use parity_wasm::elements::{
Module, ResizableLimits, TableType, Type, ValueType, Module, ResizableLimits, TableType, Type, ValueType,
}; };
mod context; pub mod context;
mod func; pub mod func;
mod util; pub mod util;
#[cfg(test)] // TODO: Uncomment
mod tests; // #[cfg(test)]
// mod tests;
// TODO: Consider using a type other than String, because
// of formatting machinary is not welcomed in substrate runtimes.
#[derive(Debug)] #[derive(Debug)]
pub struct Error(String); pub struct Error(pub String);
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -197,31 +200,6 @@ pub trait FunctionValidator {
fn finish(self) -> Self::Output; fn finish(self) -> Self::Output;
} }
pub struct WasmiValidation {
code_map: Vec<isa::Instructions>,
}
impl Validation for WasmiValidation {
type Output = Vec<isa::Instructions>;
type FunctionValidator = func::Compiler;
fn new(_module: &Module) -> Self {
WasmiValidation {
// TODO: with capacity?
code_map: Vec::new(),
}
}
fn on_function_validated(
&mut self,
_index: u32,
output: isa::Instructions,
) {
self.code_map.push(output);
}
fn finish(self) -> Vec<isa::Instructions> {
self.code_map
}
}
// TODO: Rename to validate_module // TODO: Rename to validate_module
pub fn validate_module2<V: Validation>(module: &Module) -> Result<V::Output, Error> { pub fn validate_module2<V: Validation>(module: &Module) -> Result<V::Output, Error> {
let mut context_builder = ModuleContextBuilder::new(); let mut context_builder = ModuleContextBuilder::new();
@ -314,14 +292,13 @@ pub fn validate_module2<V: Validation>(module: &Module) -> Result<V::Output, Err
.get(index as usize) .get(index as usize)
.ok_or(Error(format!("Missing body for function {}", index)))?; .ok_or(Error(format!("Missing body for function {}", index)))?;
let output = func::drive::<V::FunctionValidator>(&context, function, function_body).map_err( let output = func::drive::<V::FunctionValidator>(&context, function, function_body)
|Error(ref msg)| { .map_err(|Error(ref msg)| {
Error(format!( Error(format!(
"Function #{} reading/validation error: {}", "Function #{} reading/validation error: {}",
index, msg index, msg
)) ))
}, })?;
)?;
validation.on_function_validated(index as u32, output); validation.on_function_validated(index as u32, output);
} }
} }
@ -433,11 +410,6 @@ pub fn validate_module2<V: Validation>(module: &Module) -> Result<V::Output, Err
Ok(validation.finish()) Ok(validation.finish())
} }
pub fn validate_module(module: Module) -> Result<ValidatedModule, Error> {
let code_map = validate_module2::<WasmiValidation>(&module)?;
Ok(ValidatedModule { module, code_map })
}
fn validate_limits(limits: &ResizableLimits) -> Result<(), Error> { fn validate_limits(limits: &ResizableLimits) -> Result<(), Error> {
if let Some(maximum) = limits.maximum() { if let Some(maximum) = limits.maximum() {
if limits.initial() > maximum { if limits.initial() > maximum {