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;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CompiledModule {
|
||||
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) -> ValidatedModule {
|
||||
fn validate(wat: &str) -> CompiledModule {
|
||||
let wasm = wabt::wat2wasm(wat).unwrap();
|
||||
let module = deserialize_buffer::<Module>(&wasm).unwrap();
|
||||
let validated_module = validate_module(module).unwrap();
|
||||
validated_module
|
||||
let compiled_module = compile_module(module).unwrap();
|
||||
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 mut instructions = Vec::new();
|
||||
let mut pcs = Vec::new();
|
||||
|
|
|
@ -134,7 +134,8 @@ pub fn drive<T: FunctionValidator>(
|
|||
}
|
||||
|
||||
// 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());
|
||||
|
||||
Ok(validator.finish())
|
||||
|
|
|
@ -100,16 +100,19 @@ pub trait FunctionValidator {
|
|||
fn finish(self) -> Self::Output;
|
||||
}
|
||||
|
||||
impl Validation for () {
|
||||
pub struct SimpleValidation;
|
||||
pub struct SimpleFunctionValidator;
|
||||
|
||||
impl Validation for SimpleValidation {
|
||||
type Output = ();
|
||||
type FunctionValidator = ();
|
||||
fn new(module: &Module) -> () {
|
||||
()
|
||||
type FunctionValidator = SimpleFunctionValidator;
|
||||
fn new(_module: &Module) -> SimpleValidation {
|
||||
SimpleValidation
|
||||
}
|
||||
fn on_function_validated(
|
||||
&mut self,
|
||||
index: u32,
|
||||
output: <<Self as Validation>::FunctionValidator as FunctionValidator>::Output,
|
||||
_index: u32,
|
||||
_output: <<Self as Validation>::FunctionValidator as FunctionValidator>::Output,
|
||||
) -> () {
|
||||
()
|
||||
}
|
||||
|
@ -118,11 +121,11 @@ impl Validation for () {
|
|||
}
|
||||
}
|
||||
|
||||
impl FunctionValidator for () {
|
||||
impl FunctionValidator for SimpleFunctionValidator {
|
||||
type Output = ();
|
||||
|
||||
fn new(ctx: &func::FunctionValidationContext) -> () {
|
||||
()
|
||||
fn new(_ctx: &func::FunctionValidationContext) -> SimpleFunctionValidator {
|
||||
SimpleFunctionValidator
|
||||
}
|
||||
|
||||
fn next_instruction(
|
||||
|
@ -130,7 +133,7 @@ impl FunctionValidator for () {
|
|||
ctx: &mut func::FunctionValidationContext,
|
||||
instruction: &Instruction,
|
||||
) -> Result<(), Error> {
|
||||
Ok(())
|
||||
ctx.step(instruction)
|
||||
}
|
||||
|
||||
fn finish(self) -> () {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::Error;
|
||||
use crate::{Error, SimpleValidation};
|
||||
use parity_wasm::builder::module;
|
||||
use parity_wasm::elements::{
|
||||
BlockType, External, GlobalEntry, GlobalType, ImportEntry, InitExpr, Instruction, Instructions,
|
||||
|
@ -6,7 +6,7 @@ use parity_wasm::elements::{
|
|||
};
|
||||
|
||||
fn validate_module(module: &Module) -> Result<(), Error> {
|
||||
super::validate_module::<()>(module)
|
||||
super::validate_module::<SimpleValidation>(module)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in New Issue