2017-05-15 19:15:09 +03:00
|
|
|
use parity_wasm::interpreter::{self, ModuleInstance};
|
2017-05-15 17:44:15 +03:00
|
|
|
use runtime::Runtime;
|
2017-05-10 16:19:31 +03:00
|
|
|
|
|
|
|
pub struct Arena {
|
2017-05-15 17:44:15 +03:00
|
|
|
pub runtime: Runtime,
|
2017-05-10 16:19:31 +03:00
|
|
|
}
|
|
|
|
|
2017-05-12 20:50:09 +03:00
|
|
|
#[derive(Debug)]
|
2017-05-10 16:19:31 +03:00
|
|
|
pub struct Error;
|
|
|
|
|
|
|
|
impl Arena {
|
2017-05-15 17:44:15 +03:00
|
|
|
pub fn alloc(&self, size: u32) -> Result<u32, Error> {
|
|
|
|
// todo: maybe use unsafe cell since it has nothing to do with threads
|
|
|
|
let previous_top = self.runtime.env().dynamic_top.get();
|
|
|
|
self.runtime.env().dynamic_top.set(previous_top + size);
|
2017-05-10 16:19:31 +03:00
|
|
|
Ok(previous_top)
|
|
|
|
}
|
2017-05-15 17:44:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl interpreter::UserFunctionInterface for Arena {
|
2017-05-15 19:15:09 +03:00
|
|
|
fn call(&mut self, _module: &ModuleInstance, context: interpreter::CallerContext) -> Result<Option<interpreter::RuntimeValue>, interpreter::Error> {
|
2017-05-15 17:44:15 +03:00
|
|
|
let amount = context.value_stack.pop_as::<i32>()?;
|
|
|
|
self.alloc(amount as u32)
|
|
|
|
.map(|val| Some((val as i32).into()))
|
|
|
|
.map_err(|e| interpreter::Error::Trap(format!("Allocator failure: {}", "todo: format arg")))
|
|
|
|
}
|
2017-05-10 16:19:31 +03:00
|
|
|
}
|