wasmer/src/runtime/instance.rs

63 lines
1.5 KiB
Rust
Raw Normal View History

2018-12-24 17:25:17 -05:00
use crate::runtime::{
vm,
backing::{LocalBacking, ImportBacking},
2018-12-24 23:05:04 -05:00
module::{ModuleName, ItemName},
types::{Val, Memory, Table, Global, FuncSig},
table::TableBacking,
memory::LinearMemory,
2018-12-24 17:25:17 -05:00
};
use std::sync::Arc;
2018-12-24 23:05:04 -05:00
use hashbrown::{HashMap, Entry};
2018-12-24 17:25:17 -05:00
pub struct Instance {
pub vmctx: vm::Ctx,
2018-12-24 23:05:04 -05:00
backing: LocalBacking,
imports: ImportBacking,
2018-12-24 17:25:17 -05:00
pub module: Arc<Module>,
}
impl Instance {
2018-12-24 23:05:04 -05:00
pub fn new(module: Arc<Module>, imports: &Imports) -> Result<Box<Instance>, String> {
let mut import_backing = ImportBacking::new(&*module, imports)?;
let mut backing = LocalBacking::new(&*module, &import_backing);
let vmctx = vm::Ctx::new(&mut backing, &mut imports);
2018-12-24 17:25:17 -05:00
2018-12-24 23:05:04 -05:00
Ok(Box::new(Instance {
2018-12-24 17:25:17 -05:00
vmctx,
2018-12-24 23:05:04 -05:00
backing,
import_backing,
module,
}))
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum Import {
Func(*const vm::Func, FuncSig),
Table(Arc<TableBacking>, Table),
Memory(Arc<LinearMemory>, Memory),
Global(Val),
}
pub struct Imports {
map: HashMap<ModuleName, HashMap<ItemName, Import>>,
}
impl Imports {
pub fn new() -> Self {
Self {
map: HashMap::new(),
}
}
pub fn add(&mut self, module: ModuleName, name: ItemName, import: Import) {
self.map.entry(module).or_insert(HashMap::new()).insert(name, import)
}
pub fn get(&self, module: ModuleName, name: ItemName) -> Option<&Import> {
self.map.get().and_then(|m| m.get(name))
2018-12-24 17:25:17 -05:00
}
}