mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-20 10:16:49 +00:00
env memory
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use builder::module;
|
use builder::module;
|
||||||
use elements::{Module, FunctionType};
|
use elements::{Module, FunctionType, ExportEntry, Internal, MemoryType};
|
||||||
use interpreter::Error;
|
use interpreter::Error;
|
||||||
use interpreter::module::{ModuleInstanceInterface, ItemIndex, CallerContext};
|
use interpreter::module::{ModuleInstanceInterface, ItemIndex, CallerContext};
|
||||||
use interpreter::memory::MemoryInstance;
|
use interpreter::memory::MemoryInstance;
|
||||||
@ -8,14 +8,18 @@ use interpreter::table::TableInstance;
|
|||||||
use interpreter::value::RuntimeValue;
|
use interpreter::value::RuntimeValue;
|
||||||
use interpreter::variable::VariableInstance;
|
use interpreter::variable::VariableInstance;
|
||||||
|
|
||||||
|
const MEMORY_LIMIT_MIN: u32 = 1;
|
||||||
|
|
||||||
pub struct EnvModuleInstance {
|
pub struct EnvModuleInstance {
|
||||||
module: Module,
|
module: Module,
|
||||||
|
memory: Arc<MemoryInstance>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EnvModuleInstance {
|
impl EnvModuleInstance {
|
||||||
pub fn new(module: Module) -> Result<Self, Error> {
|
pub fn new(module: Module) -> Result<Self, Error> {
|
||||||
Ok(EnvModuleInstance {
|
Ok(EnvModuleInstance {
|
||||||
module: module,
|
module: module,
|
||||||
|
memory: MemoryInstance::new(&MemoryType::new(MEMORY_LIMIT_MIN, None))?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -37,8 +41,11 @@ impl ModuleInstanceInterface for EnvModuleInstance {
|
|||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn memory(&self, _index: ItemIndex) -> Result<Arc<MemoryInstance>, Error> {
|
fn memory(&self, index: ItemIndex) -> Result<Arc<MemoryInstance>, Error> {
|
||||||
unimplemented!()
|
match &index {
|
||||||
|
&ItemIndex::Internal(0) => Ok(self.memory.clone()),
|
||||||
|
_ => Err(Error::Env(format!("trying to get memory with index {:?}", index))),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn global(&self, _index: ItemIndex) -> Result<Arc<VariableInstance>, Error> {
|
fn global(&self, _index: ItemIndex) -> Result<Arc<VariableInstance>, Error> {
|
||||||
@ -60,7 +67,10 @@ impl ModuleInstanceInterface for EnvModuleInstance {
|
|||||||
|
|
||||||
pub fn env_module() -> Result<EnvModuleInstance, Error> {
|
pub fn env_module() -> Result<EnvModuleInstance, Error> {
|
||||||
let module = module()
|
let module = module()
|
||||||
.memory().build() // TODO: limits
|
.memory()
|
||||||
|
.with_min(MEMORY_LIMIT_MIN)
|
||||||
|
.build()
|
||||||
|
.with_export(ExportEntry::new("memory".into(), Internal::Memory(0)))
|
||||||
.build();
|
.build();
|
||||||
EnvModuleInstance::new(module)
|
EnvModuleInstance::new(module)
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,8 @@ pub enum Error {
|
|||||||
Value(String),
|
Value(String),
|
||||||
/// Interpreter (code) error.
|
/// Interpreter (code) error.
|
||||||
Interpreter(String),
|
Interpreter(String),
|
||||||
|
/// Env module error.
|
||||||
|
Env(String),
|
||||||
/// Trap.
|
/// Trap.
|
||||||
Trap(String),
|
Trap(String),
|
||||||
}
|
}
|
||||||
@ -43,6 +45,7 @@ impl Into<String> for Error {
|
|||||||
Error::Stack(s) => s,
|
Error::Stack(s) => s,
|
||||||
Error::Interpreter(s) => s,
|
Error::Interpreter(s) => s,
|
||||||
Error::Value(s) => s,
|
Error::Value(s) => s,
|
||||||
|
Error::Env(s) => s,
|
||||||
Error::Trap(s) => format!("trap: {}", s),
|
Error::Trap(s) => format!("trap: {}", s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user