Fix basics tests

This commit is contained in:
Sergey Pepyakin
2017-12-12 15:18:35 +01:00
parent 74559a49b0
commit 1fc65ca54f
8 changed files with 357 additions and 370 deletions

View File

@ -4,8 +4,6 @@ extern crate parity_wasm;
use std::env::args;
use parity_wasm::ModuleInstanceInterface;
fn main() {
let args: Vec<_> = args().collect();
if args.len() != 3 {
@ -15,7 +13,7 @@ fn main() {
}
// Intrepreter initialization.
let program = parity_wasm::ProgramInstance::new();
let mut program = parity_wasm::ProgramInstance::new();
// Here we load module using dedicated for this purpose
// `deserialize_file` function (which works only with modules)
@ -26,11 +24,11 @@ fn main() {
// - a module declaration
// - "main" module doesn't import native module(s) this is why we don't need to provide external native modules here
// This test shows how to implement native module https://github.com/NikVolf/parity-wasm/blob/master/src/interpreter/tests/basics.rs#L197
let module = program.add_module("main", module, None).expect("Failed to initialize module");
let module = program.add_module("main", module, &mut ()).expect("Failed to initialize module");
// The argument should be parsable as a valid integer
let argument: i32 = args[2].parse().expect("Integer argument required");
// "_call" export of function to be executed with an i32 argument and prints the result of execution
println!("Result: {:?}", module.execute_export("_call", vec![parity_wasm::RuntimeValue::I32(argument)].into()));
println!("Result: {:?}", program.invoke_export("main", "_call", vec![parity_wasm::RuntimeValue::I32(argument)], &mut ()));
}

View File

@ -2,7 +2,7 @@ extern crate parity_wasm;
use std::env::args;
use parity_wasm::{interpreter, ModuleInstanceInterface, RuntimeValue};
use parity_wasm::{interpreter, RuntimeValue};
use parity_wasm::elements::{Internal, External, Type, FunctionType, ValueType};
@ -16,12 +16,12 @@ fn main() {
let (_, program_args) = args.split_at(3);
// Intrepreter initialization.
let program = parity_wasm::ProgramInstance::new();
let mut program = parity_wasm::ProgramInstance::new();
let module = parity_wasm::deserialize_file(&args[1]).expect("File to be deserialized");
// Extracts call arguments from command-line arguments
let execution_params = {
let args = {
// Export section has an entry with a func_name with an index inside a module
let export_section = module.export_section().expect("No export section found");
// It's a section with function declarations (which are references to the type section entries)
@ -63,14 +63,12 @@ fn main() {
};
// Parses arguments and constructs runtime values in correspondence of their types
let args: Vec<RuntimeValue> = function_type.params().iter().enumerate().map(|(i, value)| match value {
function_type.params().iter().enumerate().map(|(i, value)| match value {
&ValueType::I32 => RuntimeValue::I32(program_args[i].parse::<i32>().expect(&format!("Can't parse arg #{} as i32", program_args[i]))),
&ValueType::I64 => RuntimeValue::I64(program_args[i].parse::<i64>().expect(&format!("Can't parse arg #{} as i64", program_args[i]))),
&ValueType::F32 => RuntimeValue::F32(program_args[i].parse::<f32>().expect(&format!("Can't parse arg #{} as f32", program_args[i]))),
&ValueType::F64 => RuntimeValue::F64(program_args[i].parse::<f64>().expect(&format!("Can't parse arg #{} as f64", program_args[i]))),
}).collect();
interpreter::ExecutionParams::from(args)
}).collect::<Vec<RuntimeValue>>()
};
// Intialize deserialized module. It adds module into It expects 3 parameters:
@ -78,7 +76,7 @@ fn main() {
// - a module declaration
// - "main" module doesn't import native module(s) this is why we don't need to provide external native modules here
// This test shows how to implement native module https://github.com/NikVolf/parity-wasm/blob/master/src/interpreter/tests/basics.rs#L197
let module = program.add_module("main", module, None).expect("Failed to initialize module");
let module = program.add_module("main", module, &mut ()).expect("Failed to initialize module");
println!("Result: {:?}", module.execute_export(func_name, execution_params).expect(""));
println!("Result: {:?}", program.invoke_export("main", func_name, args, &mut ()).expect(""));
}

View File

@ -1,4 +1,4 @@
use std::any::Any;
use std::any::{Any, TypeId};
use std::sync::Arc;
use std::marker::PhantomData;
use std::collections::HashMap;
@ -25,6 +25,10 @@ enum HostItem {
Table {
name: String,
table_type: TableType,
},
ExternVal {
name: String,
extern_val: ExternVal,
}
}
@ -42,87 +46,97 @@ impl<St: 'static> HostModuleBuilder<St> {
}
pub fn with_func0<
Cl: Fn(&mut St) -> Result<Option<Ret>, Error> + 'static,
Cl: Fn(&mut Store, &mut St) -> Result<Option<Ret>, Error> + 'static,
Ret: AsReturnVal + 'static,
F: Into<Func0<Cl, St, Ret>>,
N: Into<String>,
>(
&mut self,
name: &str,
name: N,
f: F,
) {
let func_type = Func0::<Cl, St, Ret>::derive_func_type();
let host_func = Arc::new(f.into()) as Arc<AnyFunc>;
self.items.push(HostItem::Func {
name: name.to_owned(),
name: name.into(),
func_type,
host_func,
});
}
pub fn with_func1<
Cl: Fn(&mut St, P1) -> Result<Option<Ret>, Error> + 'static,
Cl: Fn(&mut Store, &mut St, P1) -> Result<Option<Ret>, Error> + 'static,
Ret: AsReturnVal + 'static,
P1: FromArg + 'static,
F: Into<Func1<Cl, St, Ret, P1>>,
N: Into<String>,
>(
&mut self,
name: &str,
name: N,
f: F,
) {
let func_type = Func1::<Cl, St, Ret, P1>::derive_func_type();
let host_func = Arc::new(f.into()) as Arc<AnyFunc>;
self.items.push(HostItem::Func {
name: name.to_owned(),
name: name.into(),
func_type,
host_func,
});
}
pub fn with_func2<
Cl: Fn(&mut St, P1, P2) -> Result<Option<Ret>, Error> + 'static,
Cl: Fn(&mut Store, &mut St, P1, P2) -> Result<Option<Ret>, Error> + 'static,
Ret: AsReturnVal + 'static,
P1: FromArg + 'static,
P2: FromArg + 'static,
F: Into<Func2<Cl, St, Ret, P1, P2>>,
N: Into<String>,
>(
&mut self,
name: &str,
name: N,
f: F,
) {
let func_type = Func2::<Cl, St, Ret, P1, P2>::derive_func_type();
let host_func = Arc::new(f.into()) as Arc<AnyFunc>;
self.items.push(HostItem::Func {
name: name.to_owned(),
name: name.into(),
func_type,
host_func,
});
}
pub fn with_global(&mut self, name: &str, global_type: GlobalType, init_val: RuntimeValue) {
pub fn with_global<N: Into<String>>(&mut self, name: N, global_type: GlobalType, init_val: RuntimeValue) {
self.items.push(HostItem::Global {
name: name.to_owned(),
name: name.into(),
global_type,
init_val,
});
}
pub fn with_memory(&mut self, name: &str, memory_type: MemoryType) {
pub fn with_memory<N: Into<String>>(&mut self, name: N, memory_type: MemoryType) {
self.items.push(HostItem::Memory {
name: name.to_owned(),
name: name.into(),
memory_type,
});
}
pub fn with_table(&mut self, name: &str, table_type: TableType) {
pub fn with_table<N: Into<String>>(&mut self, name: N, table_type: TableType) {
self.items.push(HostItem::Table {
name: name.to_owned(),
name: name.into(),
table_type,
});
}
pub fn with_extern_val<N: Into<String>>(&mut self, name: N, extern_val: ExternVal) {
self.items.push(HostItem::ExternVal {
name: name.into(),
extern_val,
});
}
pub fn build(self) -> HostModule {
HostModule {
items: self.items
@ -157,6 +171,9 @@ impl HostModule {
let table_id = store.alloc_table(&table_type)?;
exports.insert(name, ExternVal::Table(table_id));
}
HostItem::ExternVal { name, extern_val } => {
exports.insert(name, extern_val);
}
}
}
@ -170,6 +187,7 @@ impl HostModule {
pub trait AnyFunc {
fn call_as_any(
&self,
store: &mut Store,
state: &mut Any,
args: &[RuntimeValue],
) -> Result<Option<RuntimeValue>, Error>;
@ -218,7 +236,7 @@ impl AsReturnVal for () {
}
}
pub struct Func0<Cl: Fn(&mut St) -> Result<Option<Ret>, Error>, St, Ret: AsReturnVal> {
pub struct Func0<Cl: Fn(&mut Store, &mut St) -> Result<Option<Ret>, Error>, St, Ret: AsReturnVal> {
closure: Cl,
_marker: PhantomData<(St, Ret)>,
}
@ -226,20 +244,21 @@ pub struct Func0<Cl: Fn(&mut St) -> Result<Option<Ret>, Error>, St, Ret: AsRetur
impl<
St: 'static,
Ret: AsReturnVal,
Cl: Fn(&mut St) -> Result<Option<Ret>, Error>,
Cl: Fn(&mut Store, &mut St) -> Result<Option<Ret>, Error>,
> AnyFunc for Func0<Cl, St, Ret> {
fn call_as_any(
&self,
store: &mut Store,
state: &mut Any,
args: &[RuntimeValue],
_args: &[RuntimeValue],
) -> Result<Option<RuntimeValue>, Error> {
let state = state.downcast_mut::<St>().unwrap();
let result = (self.closure)(state);
let result = (self.closure)(store, state);
result.map(|r| r.and_then(|r| r.as_return_val()))
}
}
impl<St: 'static, Ret: AsReturnVal, Cl: Fn(&mut St) -> Result<Option<Ret>, Error>> From<Cl>
impl<St: 'static, Ret: AsReturnVal, Cl: Fn(&mut Store, &mut St) -> Result<Option<Ret>, Error>> From<Cl>
for Func0<Cl, St, Ret> {
fn from(cl: Cl) -> Self {
Func0 {
@ -252,14 +271,14 @@ impl<St: 'static, Ret: AsReturnVal, Cl: Fn(&mut St) -> Result<Option<Ret>, Error
impl<
St: 'static,
Ret: AsReturnVal,
Cl: Fn(&mut St) -> Result<Option<Ret>, Error>,
Cl: Fn(&mut Store, &mut St) -> Result<Option<Ret>, Error>,
> Func0<Cl, St, Ret> {
fn derive_func_type() -> FunctionType {
FunctionType::new(vec![], Ret::value_type())
}
}
pub struct Func1<Cl: Fn(&mut St, P1) -> Result<Option<Ret>, Error>, St, Ret: AsReturnVal, P1: FromArg> {
pub struct Func1<Cl: Fn(&mut Store, &mut St, P1) -> Result<Option<Ret>, Error>, St, Ret: AsReturnVal, P1: FromArg> {
closure: Cl,
_marker: PhantomData<(St, Ret, P1)>,
}
@ -268,21 +287,22 @@ impl<
St: 'static,
Ret: AsReturnVal,
P1: FromArg,
Cl: Fn(&mut St, P1) -> Result<Option<Ret>, Error>,
Cl: Fn(&mut Store, &mut St, P1) -> Result<Option<Ret>, Error>,
> AnyFunc for Func1<Cl, St, Ret, P1> {
fn call_as_any(
&self,
store: &mut Store,
state: &mut Any,
args: &[RuntimeValue],
) -> Result<Option<RuntimeValue>, Error> {
let state = state.downcast_mut::<St>().unwrap();
let p1 = P1::from_arg(&args[0]);
let result = (self.closure)(state, p1);
let result = (self.closure)(store, state, p1);
result.map(|r| r.and_then(|r| r.as_return_val()))
}
}
impl<St: 'static, Ret: AsReturnVal, P1: FromArg, Cl: Fn(&mut St, P1) -> Result<Option<Ret>, Error>> From<Cl>
impl<St: 'static, Ret: AsReturnVal, P1: FromArg, Cl: Fn(&mut Store, &mut St, P1) -> Result<Option<Ret>, Error>> From<Cl>
for Func1<Cl, St, Ret, P1> {
fn from(cl: Cl) -> Self {
Func1 {
@ -296,14 +316,14 @@ impl<
St: 'static,
Ret: AsReturnVal,
P1: FromArg,
Cl: Fn(&mut St, P1) -> Result<Option<Ret>, Error>,
Cl: Fn(&mut Store, &mut St, P1) -> Result<Option<Ret>, Error>,
> Func1<Cl, St, Ret, P1> {
fn derive_func_type() -> FunctionType {
FunctionType::new(vec![P1::value_type()], Ret::value_type())
}
}
pub struct Func2<Cl: Fn(&mut St, P1, P2) -> Result<Option<Ret>, Error>, St, Ret: AsReturnVal, P1: FromArg, P2: FromArg> {
pub struct Func2<Cl: Fn(&mut Store, &mut St, P1, P2) -> Result<Option<Ret>, Error>, St, Ret: AsReturnVal, P1: FromArg, P2: FromArg> {
closure: Cl,
_marker: PhantomData<(St, Ret, P1, P2)>,
}
@ -313,22 +333,23 @@ impl<
Ret: AsReturnVal,
P1: FromArg,
P2: FromArg,
Cl: Fn(&mut St, P1, P2) -> Result<Option<Ret>, Error>,
Cl: Fn(&mut Store, &mut St, P1, P2) -> Result<Option<Ret>, Error>,
> AnyFunc for Func2<Cl, St, Ret, P1, P2> {
fn call_as_any(
&self,
store: &mut Store,
state: &mut Any,
args: &[RuntimeValue],
) -> Result<Option<RuntimeValue>, Error> {
let state = state.downcast_mut::<St>().unwrap();
let p1 = P1::from_arg(&args[0]);
let p2 = P2::from_arg(&args[1]);
let result = (self.closure)(state, p1, p2);
let result = (self.closure)(store, state, p1, p2);
result.map(|r| r.and_then(|r| r.as_return_val()))
}
}
impl<St: 'static, Ret: AsReturnVal, P1: FromArg, P2: FromArg, Cl: Fn(&mut St, P1, P2) -> Result<Option<Ret>, Error>> From<Cl>
impl<St: 'static, Ret: AsReturnVal, P1: FromArg, P2: FromArg, Cl: Fn(&mut Store, &mut St, P1, P2) -> Result<Option<Ret>, Error>> From<Cl>
for Func2<Cl, St, Ret, P1, P2> {
fn from(cl: Cl) -> Self {
Func2 {
@ -343,9 +364,65 @@ impl<
Ret: AsReturnVal,
P1: FromArg,
P2: FromArg,
Cl: Fn(&mut St, P1, P2) -> Result<Option<Ret>, Error>,
Cl: Fn(&mut Store, &mut St, P1, P2) -> Result<Option<Ret>, Error>,
> Func2<Cl, St, Ret, P1, P2> {
fn derive_func_type() -> FunctionType {
FunctionType::new(vec![P1::value_type(), P2::value_type()], Ret::value_type())
}
}
use interpreter::UserError;
use interpreter::store::MemoryId;
// custom user error
#[derive(Debug, Clone, PartialEq)]
struct UserErrorWithCode {
error_code: i32,
}
impl ::std::fmt::Display for UserErrorWithCode {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
write!(f, "{}", self.error_code)
}
}
impl UserError for UserErrorWithCode {}
// TODO: Rename to state
// user function executor
struct FunctionExecutor {
pub memory: MemoryId,
pub values: Vec<i32>,
}
// TODO: Remove this stuff
fn build_env_module() -> HostModule {
let mut builder = HostModuleBuilder::<FunctionExecutor>::new();
builder.with_func2("add", |store: &mut Store, state: &mut FunctionExecutor, arg: i32, unused: i32| {
let memory_value = state.memory.resolve(store).get(0, 1).unwrap()[0];
let fn_argument_unused = unused as u8;
let fn_argument = arg as u8;
assert_eq!(fn_argument_unused, 0);
let sum = memory_value + fn_argument;
state.memory.resolve(store).set(0, &vec![sum]).unwrap();
state.values.push(sum as i32);
Ok(Some(sum as i32))
});
builder.with_func2("sub", |store: &mut Store, state: &mut FunctionExecutor, arg: i32, unused: i32| {
let memory_value = state.memory.resolve(store).get(0, 1).unwrap()[0];
let fn_argument_unused = unused as u8;
let fn_argument = arg as u8;
assert_eq!(fn_argument_unused, 0);
let diff = memory_value - fn_argument;
state.memory.resolve(store).set(0, &vec![diff]).unwrap();
state.values.push(diff as i32);
Ok(Some(diff as i32))
});
builder.with_func0("err", |store: &mut Store, state: &mut FunctionExecutor| -> Result<Option<i32>, Error> {
Err(Error::User(Box::new(UserErrorWithCode { error_code: 777 })))
});
builder.with_memory("memory", MemoryType::new(256, None));
builder.build()
}

View File

@ -109,4 +109,8 @@ impl ProgramInstance {
self.store.invoke(func_id, args, state)
}
pub fn store(&self) -> &Store {
&self.store
}
}

View File

@ -59,7 +59,7 @@ enum RunResult {
/// Function has returned (optional) value.
Return(Option<RuntimeValue>),
/// Function is calling other function.
NestedCall(FunctionContext),
NestedCall(FuncId),
}
impl<'a, St: 'static> Interpreter<'a, St> {
@ -107,9 +107,23 @@ impl<'a, St: 'static> Interpreter<'a, St> {
None => return Ok(return_value),
}
},
RunResult::NestedCall(nested_context) => {
function_stack.push_back(function_context);
function_stack.push_back(nested_context);
RunResult::NestedCall(nested_func) => {
let func = nested_func.resolve(self.store).clone();
match func {
FuncInstance::Internal { .. } => {
let nested_context = function_context.nested(self.store, nested_func)?;
function_stack.push_back(function_context);
function_stack.push_back(nested_context);
},
FuncInstance::Host { func_type, .. } => {
let args = prepare_function_args(func_type.resolve(self.store), &mut function_context.value_stack)?;
let return_val = self.store.invoke(nested_func, args, self.state)?;
if let Some(return_val) = return_val {
function_context.value_stack_mut().push(return_val)?;
}
function_stack.push_back(function_context);
}
}
},
}
}
@ -136,7 +150,7 @@ impl<'a, St: 'static> Interpreter<'a, St> {
},
InstructionOutcome::ExecuteCall(func_ref) => {
function_context.position += 1;
return Ok(RunResult::NestedCall(function_context.nested(self.store, func_ref)?));
return Ok(RunResult::NestedCall(func_ref));
},
InstructionOutcome::End => {
if function_context.frame_stack().is_empty() {

View File

@ -4,8 +4,8 @@
use std::sync::Arc;
use std::any::Any;
use std::collections::HashMap;
use elements::{FunctionType, GlobalEntry, GlobalType, InitExpr, Internal, Local, MemoryType,
Module, Opcode, Opcodes, TableType, Type};
use elements::{FunctionType, GlobalEntry, GlobalType, InitExpr, Internal, External, Local, MemoryType,
Module, Opcode, Opcodes, TableType, Type, ResizableLimits};
use interpreter::{Error, ExecutionParams, MemoryInstance,
RuntimeValue, TableInstance};
use interpreter::runner::{prepare_function_args, FunctionContext, Interpreter};
@ -44,7 +44,6 @@ impl ModuleId {
.cloned()
}
pub fn global_by_index(&self, store: &Store, idx: u32) -> Option<GlobalId> {
store.resolve_module(*self)
.globals
@ -124,6 +123,37 @@ pub enum ExternVal {
Global(GlobalId),
}
impl ExternVal {
pub fn as_func(&self) -> Option<FuncId> {
match *self {
ExternVal::Func(func) => Some(func),
_ => None,
}
}
pub fn as_table(&self) -> Option<TableId> {
match *self {
ExternVal::Table(table) => Some(table),
_ => None,
}
}
pub fn as_memory(&self) -> Option<MemoryId> {
match *self {
ExternVal::Memory(memory) => Some(memory),
_ => None,
}
}
pub fn as_global(&self) -> Option<GlobalId> {
match *self {
ExternVal::Global(global) => Some(global),
_ => None,
}
}
}
#[derive(Clone)]
pub enum FuncInstance {
Internal {
func_type: TypeId,
@ -280,15 +310,6 @@ impl Store {
) -> Result<(), Error> {
let mut aux_data = validate_module(module)?;
for extern_val in extern_vals {
match *extern_val {
ExternVal::Func(func) => instance.funcs.push(func),
ExternVal::Table(table) => instance.tables.push(table),
ExternVal::Memory(memory) => instance.memories.push(memory),
ExternVal::Global(global) => instance.globals.push(global),
}
}
for type_ in module
.type_section()
.map(|ts| ts.types())
@ -403,9 +424,44 @@ impl Store {
let mut instance = ModuleInstance::new();
// Reserve the index of the module, but not yet push the module.
let module_id = ModuleId((self.modules.len()) as u32);
self.alloc_module_internal(module, extern_vals, &mut instance, module_id)?;
// TODO: assert module is valid with extern_vals.
{
let imports = module.import_section().map(|is| is.entries()).unwrap_or(&[]);
if imports.len() != extern_vals.len() {
return Err(Error::Initialization(format!("extern_vals length is not equal to import section entries")));
}
for (import, extern_val) in Iterator::zip(imports.into_iter(), extern_vals.into_iter())
{
match (import.external(), *extern_val) {
(&External::Function(ref f), ExternVal::Func(func)) => {
// TODO: check func types
instance.funcs.push(func)
}
(&External::Table(ref tt), ExternVal::Table(table)) => {
match_limits(table.resolve(self).limits(), tt.limits())?;
instance.tables.push(table);
}
(&External::Memory(ref mt), ExternVal::Memory(memory)) => {
match_limits(memory.resolve(self).limits(), mt.limits())?;
instance.memories.push(memory);
}
(&External::Global(ref gl), ExternVal::Global(global)) => {
// TODO: check globals
instance.globals.push(global)
}
(expected_import, actual_extern_val) => {
return Err(Error::Initialization(format!(
"Expected {:?} type, but provided {:?} extern_val",
expected_import,
actual_extern_val
)));
}
}
}
}
self.alloc_module_internal(module, extern_vals, &mut instance, module_id)?;
for element_segment in module
.elements_section()
@ -484,7 +540,7 @@ impl Store {
) -> Result<Option<RuntimeValue>, Error> {
enum InvokeKind {
Internal(FunctionContext),
Host(Arc<AnyFunc>),
Host(Arc<AnyFunc>, Vec<RuntimeValue>),
}
let result = match *func.resolve(self) {
@ -502,7 +558,7 @@ impl Store {
);
InvokeKind::Internal(context)
}
FuncInstance::Host { ref host_func, .. } => InvokeKind::Host(Arc::clone(host_func)),
FuncInstance::Host { ref host_func, .. } => InvokeKind::Host(Arc::clone(host_func), args),
};
match result {
@ -510,9 +566,8 @@ impl Store {
let mut interpreter = Interpreter::new(self, state);
interpreter.run_function(ctx)
}
InvokeKind::Host(host_func) => {
// host_func.call_as_any();
panic!()
InvokeKind::Host(host_func, args) => {
host_func.call_as_any(self, state as &mut Any, &args)
}
}
}
@ -562,3 +617,27 @@ fn eval_init_expr(init_expr: &InitExpr, module: &ModuleInstance, store: &Store)
_ => panic!("Due to validation init should be a const expr"),
}
}
fn match_limits(l1: &ResizableLimits, l2: &ResizableLimits) -> Result<(), Error> {
if l1.initial() < l2.initial() {
return Err(Error::Initialization(format!(
"trying to import with limits l1.initial={} and l2.initial={}",
l1.initial(),
l2.initial()
)));
}
match (l1.maximum(), l2.maximum()) {
(_, None) => (),
(Some(m1), Some(m2)) if m1 <= m2 => (),
_ => {
return Err(Error::Initialization(format!(
"trying to import with limits l1.max={:?} and l2.max={:?}",
l1.maximum(),
l2.maximum()
)))
}
}
Ok(())
}

View File

@ -7,11 +7,11 @@ use builder::module;
use elements::{ExportEntry, Internal, ImportEntry, External, GlobalEntry, GlobalType,
InitExpr, ValueType, Opcodes, Opcode, FunctionType, TableType, MemoryType};
use interpreter::{Error, UserError, ProgramInstance};
use interpreter::native::{native_module, UserDefinedElements, UserFunctionExecutor, UserFunctionDescriptor};
use interpreter::memory::MemoryInstance;
use interpreter::module::{ModuleInstanceInterface, CallerContext, ItemIndex, ExecutionParams, ExportEntryType, FunctionSignature};
use interpreter::value::{RuntimeValue, TryInto};
use interpreter::variable::{VariableInstance, ExternalVariableValue, VariableType};
use interpreter::host::{HostModuleBuilder, HostModule};
use interpreter::store::{Store, MemoryId};
use super::utils::program_with_default_env;
#[test]
@ -40,12 +40,12 @@ fn import_function() {
.build()
.build();
let program = ProgramInstance::new();
let external_module = program.add_module("external_module", module1, None).unwrap();
let main_module = program.add_module("main", module2, None).unwrap();
let mut program = ProgramInstance::new();
program.add_module("external_module", module1, &mut ()).unwrap();
program.add_module("main", module2, &mut ()).unwrap();
assert_eq!(external_module.execute_index(0, vec![].into()).unwrap().unwrap(), RuntimeValue::I32(3));
assert_eq!(main_module.execute_index(1, vec![].into()).unwrap().unwrap(), RuntimeValue::I32(10));
assert_eq!(program.invoke_index("external_module", 0, vec![], &mut ()).unwrap().unwrap(), RuntimeValue::I32(3));
assert_eq!(program.invoke_index("main", 1, vec![], &mut ()).unwrap().unwrap(), RuntimeValue::I32(10));
}
#[test]
@ -74,9 +74,9 @@ fn wrong_import() {
.build()
.build();
let program = ProgramInstance::new();
let _side_module_instance = program.add_module("side_module", side_module, None).unwrap();
assert!(program.add_module("main", module, None).is_err());
let mut program = ProgramInstance::new();
let _side_module_instance = program.add_module("side_module", side_module, &mut ()).unwrap();
assert!(program.add_module("main", module, &mut ()).is_err());
}
#[test]
@ -96,48 +96,10 @@ fn global_get_set() {
.build()
.build();
let program = ProgramInstance::new();
let module = program.add_module("main", module, None).unwrap();
assert_eq!(module.execute_index(0, vec![].into()).unwrap().unwrap(), RuntimeValue::I32(50));
assert_eq!(module.execute_index(0, vec![].into()).unwrap().unwrap(), RuntimeValue::I32(58));
}
const SIGNATURE_I32_I32: &'static [ValueType] = &[ValueType::I32, ValueType::I32];
const SIGNATURES: &'static [UserFunctionDescriptor] = &[
UserFunctionDescriptor::Static(
"add",
SIGNATURE_I32_I32,
Some(ValueType::I32),
),
UserFunctionDescriptor::Static(
"sub",
SIGNATURE_I32_I32,
Some(ValueType::I32),
),
UserFunctionDescriptor::Static(
"err",
SIGNATURE_I32_I32,
Some(ValueType::I32),
),
];
const NO_SIGNATURES: &'static [UserFunctionDescriptor] = &[];
// 'external' variable
struct MeasuredVariable {
pub val: i32,
}
impl ExternalVariableValue for MeasuredVariable {
fn get(&self) -> RuntimeValue {
RuntimeValue::I32(self.val)
}
fn set(&mut self, val: RuntimeValue) -> Result<(), Error> {
self.val = val.try_into()?;
Ok(())
}
let mut program = ProgramInstance::new();
program.add_module("main", module, &mut ()).unwrap();
assert_eq!(program.invoke_index("main", 0, vec![], &mut ()).unwrap().unwrap(), RuntimeValue::I32(50));
assert_eq!(program.invoke_index("main", 0, vec![], &mut ()).unwrap().unwrap(), RuntimeValue::I32(58));
}
// custom user error
@ -154,68 +116,56 @@ impl ::std::fmt::Display for UserErrorWithCode {
impl UserError for UserErrorWithCode {}
// TODO: Rename to state
// user function executor
struct FunctionExecutor {
pub memory: Arc<MemoryInstance>,
pub memory: MemoryId,
pub values: Vec<i32>,
}
impl<'a> UserFunctionExecutor for &'a mut FunctionExecutor {
fn execute(&mut self, name: &str, context: CallerContext) -> Result<Option<RuntimeValue>, Error> {
match name {
"add" => {
let memory_value = self.memory.get(0, 1).unwrap()[0];
let fn_argument_unused = context.value_stack.pop_as::<u32>().unwrap() as u8;
let fn_argument = context.value_stack.pop_as::<u32>().unwrap() as u8;
assert_eq!(fn_argument_unused, 0);
fn build_env_module() -> HostModule {
let mut builder = HostModuleBuilder::<FunctionExecutor>::new();
builder.with_func2("add", |store: &mut Store, state: &mut FunctionExecutor, arg: i32, unused: i32| {
let memory_value = state.memory.resolve(store).get(0, 1).unwrap()[0];
let fn_argument_unused = unused as u8;
let fn_argument = arg as u8;
assert_eq!(fn_argument_unused, 0);
let sum = memory_value + fn_argument;
self.memory.set(0, &vec![sum]).unwrap();
self.values.push(sum as i32);
Ok(Some(RuntimeValue::I32(sum as i32)))
},
"sub" => {
let memory_value = self.memory.get(0, 1).unwrap()[0];
let fn_argument_unused = context.value_stack.pop_as::<u32>().unwrap() as u8;
let fn_argument = context.value_stack.pop_as::<u32>().unwrap() as u8;
assert_eq!(fn_argument_unused, 0);
let sum = memory_value + fn_argument;
state.memory.resolve(store).set(0, &vec![sum]).unwrap();
state.values.push(sum as i32);
Ok(Some(sum as i32))
});
builder.with_func2("sub", |store: &mut Store, state: &mut FunctionExecutor, arg: i32, unused: i32| {
let memory_value = state.memory.resolve(store).get(0, 1).unwrap()[0];
let fn_argument_unused = unused as u8;
let fn_argument = arg as u8;
assert_eq!(fn_argument_unused, 0);
let diff = memory_value - fn_argument;
self.memory.set(0, &vec![diff]).unwrap();
self.values.push(diff as i32);
Ok(Some(RuntimeValue::I32(diff as i32)))
},
"err" => {
Err(Error::User(Box::new(UserErrorWithCode { error_code: 777 })))
},
_ => Err(Error::Trap("not implemented".into()).into()),
}
}
let diff = memory_value - fn_argument;
state.memory.resolve(store).set(0, &vec![diff]).unwrap();
state.values.push(diff as i32);
Ok(Some(diff as i32))
});
builder.with_func0("err", |_: &mut Store, _: &mut FunctionExecutor| -> Result<Option<i32>, Error> {
Err(Error::User(Box::new(UserErrorWithCode { error_code: 777 })))
});
builder.with_memory("memory", MemoryType::new(256, None));
builder.build()
}
#[test]
fn native_env_function() {
// create new program
let program = program_with_default_env();
// => env module is created
let env_instance = program.module("env").unwrap();
// => linear memory is created
let env_memory = env_instance.memory(ItemIndex::Internal(0)).unwrap();
let mut program = program_with_default_env();
let env_host_module = build_env_module();
let env_module = program.add_host_module("env", env_host_module).unwrap();
let env_memory = env_module.export_by_name(program.store(), "memory").unwrap().as_memory().unwrap();
// create native env module executor
let mut executor = FunctionExecutor {
memory: env_memory.clone(),
let mut state = FunctionExecutor {
memory: env_memory,
values: Vec::new(),
};
{
let functions = UserDefinedElements {
executor: Some(&mut executor),
globals: HashMap::new(),
functions: ::std::borrow::Cow::from(SIGNATURES),
};
let native_env_instance = native_module(env_instance, functions).unwrap();
let params = ExecutionParams::with_external("env".into(), native_env_instance);
let module = module()
.with_import(ImportEntry::new("env".into(), "add".into(), External::Function(0)))
.with_import(ImportEntry::new("env".into(), "sub".into(), External::Function(0)))
@ -240,104 +190,40 @@ fn native_env_function() {
.build();
// load module
let module_instance = program.add_module("main", module, Some(&params.externals)).unwrap();
program.add_module("main", module, &mut state).unwrap();
{
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(7)).add_argument(RuntimeValue::I32(0))).unwrap().unwrap(), RuntimeValue::I32(7));
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(50)).add_argument(RuntimeValue::I32(0))).unwrap().unwrap(), RuntimeValue::I32(57));
assert_eq!(module_instance.execute_index(3, params.clone().add_argument(RuntimeValue::I32(15)).add_argument(RuntimeValue::I32(0))).unwrap().unwrap(), RuntimeValue::I32(42));
assert_eq!(
program.invoke_index("main", 2, vec![RuntimeValue::I32(7), RuntimeValue::I32(0)], &mut state)
.unwrap()
.unwrap(),
RuntimeValue::I32(7)
);
assert_eq!(
program.invoke_index("main", 2, vec![RuntimeValue::I32(50), RuntimeValue::I32(0)], &mut state)
.unwrap()
.unwrap(),
RuntimeValue::I32(57)
);
assert_eq!(
program.invoke_index("main", 3, vec![RuntimeValue::I32(15), RuntimeValue::I32(0)], &mut state)
.unwrap()
.unwrap(),
RuntimeValue::I32(42)
);
}
}
assert_eq!(executor.memory.get(0, 1).unwrap()[0], 42);
assert_eq!(executor.values, vec![7, 57, 42]);
}
#[test]
fn native_env_function_own_memory() {
// create program + env module is auto instantiated + env module memory is instantiated (we do not need this)
let program = program_with_default_env();
struct OwnMemoryReference {
pub memory: RefCell<Option<Arc<MemoryInstance>>>,
}
struct OwnMemoryExecutor {
pub memory_ref: Arc<OwnMemoryReference>,
}
impl<'a> UserFunctionExecutor for &'a mut OwnMemoryExecutor {
fn execute(&mut self, name: &str, context: CallerContext) -> Result<Option<RuntimeValue>, Error> {
match name {
"add" => {
let memory = self.memory_ref.memory.borrow_mut().as_ref().expect("initialized before execution; qed").clone();
let memory_value = memory.get(0, 1).unwrap()[0];
let fn_argument_unused = context.value_stack.pop_as::<u32>().unwrap() as u8;
let fn_argument = context.value_stack.pop_as::<u32>().unwrap() as u8;
assert_eq!(fn_argument_unused, 0);
let sum = memory_value + fn_argument;
memory.set(0, &vec![sum]).unwrap();
Ok(Some(RuntimeValue::I32(sum as i32)))
},
_ => Err(Error::Trap("not implemented".into()).into()),
}
}
}
let env_instance = program.module("env").unwrap();
let memory_ref = Arc::new(OwnMemoryReference { memory: RefCell::new(None) });
let mut executor = OwnMemoryExecutor { memory_ref: memory_ref.clone() };
let native_env_instance = native_module(env_instance, UserDefinedElements {
executor: Some(&mut executor),
globals: HashMap::new(),
functions: ::std::borrow::Cow::from(SIGNATURES),
}).unwrap();
let params = ExecutionParams::with_external("env".into(), native_env_instance);
// create module definition with its own memory
// => since we do not import env' memory, all instructions from this module will access this memory
let module = module()
.memory().build() // new memory is created
.with_import(ImportEntry::new("env".into(), "add".into(), External::Function(0))) // import 'native' function
.function() // add simple wasm function
.signature().param().i32().param().i32().return_type().i32().build()
.body().with_opcodes(Opcodes::new(vec![
Opcode::GetLocal(0),
Opcode::GetLocal(1),
Opcode::Call(0),
Opcode::End,
])).build()
.build()
.build();
// instantiate module
let module_instance = program.add_module("main", module, Some(&params.externals)).unwrap();
// now get memory reference
let module_memory = module_instance.memory(ItemIndex::Internal(0)).unwrap();
// post-initialize our executor with memory reference
*memory_ref.memory.borrow_mut() = Some(module_memory);
// now execute function => executor updates memory
assert_eq!(module_instance.execute_index(1, params.clone().add_argument(RuntimeValue::I32(7)).add_argument(RuntimeValue::I32(0))).unwrap().unwrap(),
RuntimeValue::I32(7));
assert_eq!(state.memory.resolve(program.store()).get(0, 1).unwrap()[0], 42);
assert_eq!(state.values, vec![7, 57, 42]);
}
#[test]
fn native_env_global() {
struct DummyExecutor;
impl UserFunctionExecutor for DummyExecutor {
fn execute(&mut self, _name: &str, _context: CallerContext) -> Result<Option<RuntimeValue>, Error> {
// this code should be unreachable, because we actually doesn't call any
// native functions in this test.
unreachable!();
}
}
struct State;
let module_constructor = |elements: UserDefinedElements<DummyExecutor>| {
let program = program_with_default_env();
let env_instance = program.module("env").unwrap();
let native_env_instance = native_module(env_instance, elements).unwrap();
let params = ExecutionParams::with_external("env".into(), native_env_instance);
let module_constructor = |host_module: HostModule| {
let mut program = ProgramInstance::new();
program.add_host_module("env", host_module)?;
let module = module()
.with_import(ImportEntry::new("env".into(), "ext_global".into(), External::Global(GlobalType::new(ValueType::I32, false))))
@ -349,52 +235,35 @@ fn native_env_global() {
])).build()
.build()
.build();
program.add_module("main", module, Some(&params.externals))?
.execute_index(0, params.clone())
program.add_module("main", module, &mut State)?;
program.invoke_index("main", 0, vec![], &mut State)
};
// try to add module, exporting non-existant env' variable => error
{
assert!(module_constructor(UserDefinedElements {
executor: None,
globals: HashMap::new(),
functions: ::std::borrow::Cow::from(NO_SIGNATURES),
}).is_err());
let mut host_module_builder = HostModuleBuilder::<State>::new();
assert!(module_constructor(host_module_builder.build()).is_err());
}
// now add simple variable natively => ok
{
assert_eq!(module_constructor(UserDefinedElements {
executor: None,
globals: vec![("ext_global".into(), Arc::new(VariableInstance::new(false, VariableType::I32, RuntimeValue::I32(777)).unwrap()))].into_iter().collect(),
functions: ::std::borrow::Cow::from(NO_SIGNATURES),
}).unwrap().unwrap(), RuntimeValue::I32(777));
}
// now add 'getter+setter' variable natively => ok
{
assert_eq!(module_constructor(UserDefinedElements {
executor: None,
globals: vec![("ext_global".into(), Arc::new(VariableInstance::new_external_global(false, VariableType::I32, Box::new(MeasuredVariable { val: 345 })).unwrap()))].into_iter().collect(),
functions: ::std::borrow::Cow::from(NO_SIGNATURES),
}).unwrap().unwrap(), RuntimeValue::I32(345));
let mut host_module_builder = HostModuleBuilder::<State>::new();
host_module_builder.with_global("ext_global", GlobalType::new(ValueType::I32, false), RuntimeValue::I32(777));
assert_eq!(module_constructor(host_module_builder.build()).unwrap().unwrap(), RuntimeValue::I32(777));
}
}
#[test]
fn native_custom_error() {
let program = program_with_default_env();
let env_instance = program.module("env").unwrap();
let env_memory = env_instance.memory(ItemIndex::Internal(0)).unwrap();
let mut program = program_with_default_env();
let env_host_module = build_env_module();
let env_module = program.add_host_module("env", env_host_module).unwrap();
let env_memory = env_module.export_by_name(program.store(), "memory").unwrap().as_memory().unwrap();
let mut executor = FunctionExecutor { memory: env_memory.clone(), values: Vec::new() };
let functions = UserDefinedElements {
executor: Some(&mut executor),
globals: HashMap::new(),
functions: ::std::borrow::Cow::from(SIGNATURES),
let mut state = FunctionExecutor {
memory: env_memory,
values: Vec::new(),
};
let native_env_instance = native_module(env_instance, functions).unwrap();
let params = ExecutionParams::with_external("env".into(), native_env_instance);
let module = module()
.with_import(ImportEntry::new("env".into(), "err".into(), External::Function(0)))
@ -409,66 +278,17 @@ fn native_custom_error() {
.build()
.build();
let module_instance = program.add_module("main", module, Some(&params.externals)).unwrap();
let user_error1 = match module_instance.execute_index(
let module_instance = program.add_module("main", module, &mut state).unwrap();
let user_error = match program.invoke_index(
"main",
0,
params
.clone()
.add_argument(RuntimeValue::I32(7))
.add_argument(RuntimeValue::I32(0)),
vec![RuntimeValue::I32(7), RuntimeValue::I32(0)],
&mut state
) {
Err(Error::User(user_error)) => user_error,
result => panic!("Unexpected result {:?}", result),
};
assert_eq!(user_error1.downcast_ref::<UserErrorWithCode>().unwrap(), &UserErrorWithCode { error_code: 777 });
let user_error2 = match module_instance.execute_index(
0,
params
.clone()
.add_argument(RuntimeValue::I32(7))
.add_argument(RuntimeValue::I32(0)),
) {
Err(Error::User(user_error)) => user_error,
result => panic!("Unexpected result {:?}", result),
};
assert_eq!(user_error2.downcast_ref::<UserErrorWithCode>().unwrap(), &UserErrorWithCode { error_code: 777 });
}
#[test]
fn env_native_export_entry_type_check() {
let program = program_with_default_env();
let env_instance = program.module("env").unwrap();
let env_memory = env_instance.memory(ItemIndex::Internal(0)).unwrap();
let mut function_executor = FunctionExecutor {
memory: env_memory,
values: Vec::new(),
};
let native_env_instance = native_module(env_instance, UserDefinedElements {
executor: Some(&mut function_executor),
globals: vec![("ext_global".into(), Arc::new(VariableInstance::new(false, VariableType::I32, RuntimeValue::I32(1312)).unwrap()))].into_iter().collect(),
functions: ::std::borrow::Cow::from(SIGNATURES),
}).unwrap();
assert!(native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32, ValueType::I32], Some(ValueType::I32))))).is_ok());
match native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![], Some(ValueType::I32))))) {
Err(Error::Validation(_)) => { },
result => panic!("Unexpected result {:?}", result),
}
match native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32, ValueType::I32], None)))) {
Err(Error::Validation(_)) => { },
result => panic!("Unexpected result {:?}", result),
}
match native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32, ValueType::I32], Some(ValueType::I64))))) {
Err(Error::Validation(_)) => { },
result => panic!("Unexpected result {:?}", result),
}
assert!(native_env_instance.export_entry("ext_global", &ExportEntryType::Global(VariableType::I32)).is_ok());
match native_env_instance.export_entry("ext_global", &ExportEntryType::Global(VariableType::F32)) {
Err(Error::Validation(_)) => { },
result => panic!("Unexpected result {:?}", result),
}
assert_eq!(user_error.downcast_ref::<UserErrorWithCode>().unwrap(), &UserErrorWithCode { error_code: 777 });
}
#[test]
@ -478,8 +298,8 @@ fn memory_import_limits_initial() {
.with_export(ExportEntry::new("memory".into(), Internal::Memory(0)))
.build();
let program = ProgramInstance::new();
program.add_module("core", core_module, None).unwrap();
let mut program = ProgramInstance::new();
program.add_module("core", core_module, &mut ()).unwrap();
let test_cases = vec![
(9, false),
@ -492,10 +312,9 @@ fn memory_import_limits_initial() {
let client_module = module()
.with_import(ImportEntry::new("core".into(), "memory".into(), External::Memory(MemoryType::new(import_initial, None))))
.build();
match program.add_module("client", client_module, None).map(|_| ()) {
match program.add_module("client", client_module, &mut ()).map(|_| ()) {
Ok(_) if !is_error => (),
Err(Error::Validation(ref actual_error))
if is_error && actual_error == &format!("trying to import memory with initial=10 and import.initial={}", import_initial) => (),
Err(Error::Initialization(_)) if is_error => (),
x @ _ => panic!("unexpected result for test_case {:?}: {:?}", test_case, x),
}
}
@ -507,7 +326,6 @@ fn memory_import_limits_maximum() {
enum MaximumError { ValueMismatch, Ok };
let test_cases = vec![
(None, Some(100), MaximumError::Ok),
(Some(100), None, MaximumError::Ok),
(Some(100), Some(98), MaximumError::ValueMismatch),
(Some(100), Some(100), MaximumError::Ok),
@ -515,7 +333,7 @@ fn memory_import_limits_maximum() {
(None, None, MaximumError::Ok),
];
let program = ProgramInstance::new();
let mut program = ProgramInstance::new();
for test_case in test_cases {
let (core_maximum, client_maximum, expected_err) = test_case;
let core_module = module()
@ -526,11 +344,11 @@ fn memory_import_limits_maximum() {
.with_import(ImportEntry::new("core".into(), "memory".into(), External::Memory(MemoryType::new(10, client_maximum))))
.build();
program.add_module("core", core_module, None).unwrap();
match program.add_module("client", client_module, None).map(|_| ()) {
Err(Error::Validation(actual_err)) => match expected_err {
program.add_module("core", core_module, &mut ()).unwrap();
match program.add_module("client", client_module, &mut ()).map(|_| ()) {
Err(Error::Initialization(actual_err)) => match expected_err {
MaximumError::ValueMismatch
if actual_err == format!("trying to import memory with maximum={} and import.maximum={}", core_maximum.unwrap_or_default(), client_maximum.unwrap_or_default()) => (),
if actual_err == format!("trying to import with limits l1.max={:?} and l2.max={:?}", core_maximum, client_maximum) => (),
_ => panic!("unexpected validation error for test_case {:?}: {}", test_case, actual_err),
},
Ok(_) if expected_err == MaximumError::Ok => (),
@ -546,8 +364,8 @@ fn table_import_limits_initial() {
.with_export(ExportEntry::new("table".into(), Internal::Table(0)))
.build();
let program = ProgramInstance::new();
program.add_module("core", core_module, None).unwrap();
let mut program = ProgramInstance::new();
program.add_module("core", core_module, &mut ()).unwrap();
let test_cases = vec![
(9, false),
@ -560,10 +378,10 @@ fn table_import_limits_initial() {
let client_module = module()
.with_import(ImportEntry::new("core".into(), "table".into(), External::Table(TableType::new(import_initial, None))))
.build();
match program.add_module("client", client_module, None).map(|_| ()) {
match program.add_module("client", client_module, &mut ()).map(|_| ()) {
Ok(_) if !is_error => (),
Err(Error::Validation(ref actual_error))
if is_error && actual_error == &format!("trying to import table with initial=10 and import.initial={}", import_initial) => (),
Err(Error::Initialization(ref actual_error))
if is_error && actual_error == &format!("trying to import with limits l1.initial=10 and l2.initial={}", import_initial) => (),
x @ _ => panic!("unexpected result for test_case {:?}: {:?}", test_case, x),
}
}
@ -575,7 +393,6 @@ fn table_import_limits_maximum() {
enum MaximumError { ValueMismatch, Ok };
let test_cases = vec![
(None, Some(100), MaximumError::Ok),
(Some(100), None, MaximumError::Ok),
(Some(100), Some(98), MaximumError::ValueMismatch),
(Some(100), Some(100), MaximumError::Ok),
@ -583,7 +400,7 @@ fn table_import_limits_maximum() {
(None, None, MaximumError::Ok),
];
let program = ProgramInstance::new();
let mut program = ProgramInstance::new();
for test_case in test_cases {
let (core_maximum, client_maximum, expected_err) = test_case;
let core_module = module()
@ -594,11 +411,11 @@ fn table_import_limits_maximum() {
.with_import(ImportEntry::new("core".into(), "table".into(), External::Table(TableType::new(10, client_maximum))))
.build();
program.add_module("core", core_module, None).unwrap();
match program.add_module("client", client_module, None).map(|_| ()) {
Err(Error::Validation(actual_err)) => match expected_err {
program.add_module("core", core_module, &mut ()).unwrap();
match program.add_module("client", client_module, &mut ()).map(|_| ()) {
Err(Error::Initialization(actual_err)) => match expected_err {
MaximumError::ValueMismatch
if actual_err == format!("trying to import table with maximum={} and import.maximum={}", core_maximum.unwrap_or_default(), client_maximum.unwrap_or_default()) => (),
if actual_err == format!("trying to import with limits l1.max={:?} and l2.max={:?}", core_maximum, client_maximum) => (),
_ => panic!("unexpected validation error for test_case {:?}: {}", test_case, actual_err),
},
Ok(_) if expected_err == MaximumError::Ok => (),

View File

@ -1,6 +1,6 @@
mod basics;
mod wabt;
mod wasm;
// mod wabt;
// mod wasm;
mod utils {
use elements::{Internal, ExportEntry, InitExpr, Opcode, ValueType, GlobalType, GlobalEntry};
@ -8,7 +8,7 @@ mod utils {
use builder::module;
pub fn program_with_default_env() -> ProgramInstance {
let program = ProgramInstance::new();
let mut program = ProgramInstance::new();
let env_module = module()
.memory()
.with_min(256) // 256 pages. 256 * 64K = 16MB
@ -23,7 +23,7 @@ mod utils {
.with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, false), InitExpr::new(vec![Opcode::I32Const(0), Opcode::End])))
.with_export(ExportEntry::new("memoryBase".into(), Internal::Global(1)))
.build();
program.add_module("env", env_module, None).unwrap();
program.add_module("env", env_module, &mut ()).unwrap();
program
}
}