Make wasmi compilation tests work
This commit is contained in:
parent
584b1fd2e9
commit
0b11d665aa
|
@ -9,6 +9,9 @@ use parity_wasm::elements::Module;
|
||||||
|
|
||||||
mod compile;
|
mod compile;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct CompiledModule {
|
pub struct CompiledModule {
|
||||||
pub code_map: Vec<isa::Instructions>,
|
pub code_map: Vec<isa::Instructions>,
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
|
use super::{compile_module, CompiledModule};
|
||||||
|
use parity_wasm::{deserialize_buffer, elements::Module};
|
||||||
|
|
||||||
|
use isa;
|
||||||
|
use wabt;
|
||||||
|
|
||||||
|
fn validate(wat: &str) -> CompiledModule {
|
||||||
fn validate(wat: &str) -> ValidatedModule {
|
|
||||||
let wasm = wabt::wat2wasm(wat).unwrap();
|
let wasm = wabt::wat2wasm(wat).unwrap();
|
||||||
let module = deserialize_buffer::<Module>(&wasm).unwrap();
|
let module = deserialize_buffer::<Module>(&wasm).unwrap();
|
||||||
let validated_module = validate_module(module).unwrap();
|
let compiled_module = compile_module(module).unwrap();
|
||||||
validated_module
|
compiled_module
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile(module: &ValidatedModule) -> (Vec<isa::Instruction>, Vec<u32>) {
|
fn compile(module: &CompiledModule) -> (Vec<isa::Instruction>, Vec<u32>) {
|
||||||
let code = &module.code_map[0];
|
let code = &module.code_map[0];
|
||||||
let mut instructions = Vec::new();
|
let mut instructions = Vec::new();
|
||||||
let mut pcs = Vec::new();
|
let mut pcs = Vec::new();
|
||||||
|
|
|
@ -134,7 +134,8 @@ pub fn drive<T: FunctionValidator>(
|
||||||
}
|
}
|
||||||
|
|
||||||
// The last `end` opcode should pop last instruction.
|
// The last `end` opcode should pop last instruction.
|
||||||
// TODO: This looks like it should be returned as an error?
|
// parity-wasm ensures that there is always `End` opcode at
|
||||||
|
// the end of the function body.
|
||||||
assert!(context.frame_stack.is_empty());
|
assert!(context.frame_stack.is_empty());
|
||||||
|
|
||||||
Ok(validator.finish())
|
Ok(validator.finish())
|
||||||
|
|
|
@ -100,16 +100,19 @@ pub trait FunctionValidator {
|
||||||
fn finish(self) -> Self::Output;
|
fn finish(self) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Validation for () {
|
pub struct SimpleValidation;
|
||||||
|
pub struct SimpleFunctionValidator;
|
||||||
|
|
||||||
|
impl Validation for SimpleValidation {
|
||||||
type Output = ();
|
type Output = ();
|
||||||
type FunctionValidator = ();
|
type FunctionValidator = SimpleFunctionValidator;
|
||||||
fn new(module: &Module) -> () {
|
fn new(_module: &Module) -> SimpleValidation {
|
||||||
()
|
SimpleValidation
|
||||||
}
|
}
|
||||||
fn on_function_validated(
|
fn on_function_validated(
|
||||||
&mut self,
|
&mut self,
|
||||||
index: u32,
|
_index: u32,
|
||||||
output: <<Self as Validation>::FunctionValidator as FunctionValidator>::Output,
|
_output: <<Self as Validation>::FunctionValidator as FunctionValidator>::Output,
|
||||||
) -> () {
|
) -> () {
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
|
@ -118,11 +121,11 @@ impl Validation for () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FunctionValidator for () {
|
impl FunctionValidator for SimpleFunctionValidator {
|
||||||
type Output = ();
|
type Output = ();
|
||||||
|
|
||||||
fn new(ctx: &func::FunctionValidationContext) -> () {
|
fn new(_ctx: &func::FunctionValidationContext) -> SimpleFunctionValidator {
|
||||||
()
|
SimpleFunctionValidator
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_instruction(
|
fn next_instruction(
|
||||||
|
@ -130,7 +133,7 @@ impl FunctionValidator for () {
|
||||||
ctx: &mut func::FunctionValidationContext,
|
ctx: &mut func::FunctionValidationContext,
|
||||||
instruction: &Instruction,
|
instruction: &Instruction,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
Ok(())
|
ctx.step(instruction)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish(self) -> () {
|
fn finish(self) -> () {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::Error;
|
use crate::{Error, SimpleValidation};
|
||||||
use parity_wasm::builder::module;
|
use parity_wasm::builder::module;
|
||||||
use parity_wasm::elements::{
|
use parity_wasm::elements::{
|
||||||
BlockType, External, GlobalEntry, GlobalType, ImportEntry, InitExpr, Instruction, Instructions,
|
BlockType, External, GlobalEntry, GlobalType, ImportEntry, InitExpr, Instruction, Instructions,
|
||||||
|
@ -6,7 +6,7 @@ use parity_wasm::elements::{
|
||||||
};
|
};
|
||||||
|
|
||||||
fn validate_module(module: &Module) -> Result<(), Error> {
|
fn validate_module(module: &Module) -> Result<(), Error> {
|
||||||
super::validate_module::<()>(module)
|
super::validate_module::<SimpleValidation>(module)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue