mirror of
https://github.com/fluencelabs/wasmer
synced 2025-05-29 01:51:18 +00:00
Add FuncResolver and ImportResolver traits
This commit is contained in:
parent
8a19b042f8
commit
faf41e295c
@ -1,7 +1,16 @@
|
|||||||
use crate::runtime::module::Module;
|
use crate::runtime::{
|
||||||
|
module::Module,
|
||||||
|
types::FuncIndex,
|
||||||
|
vm,
|
||||||
|
};
|
||||||
|
use std::ptr::NonNull;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub trait Compiler {
|
pub trait Compiler {
|
||||||
/// Compiles a `Module` from WebAssembly binary format
|
/// Compiles a `Module` from WebAssembly binary format
|
||||||
fn compile(&self, wasm: &[u8]) -> Result<Arc<Module>, String>;
|
fn compile(&self, wasm: &[u8]) -> Result<Arc<Module>, String>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait FuncResolver {
|
||||||
|
fn get(&self, module: &Module, index: FuncIndex) -> Option<NonNull<vm::Func>>;
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
use crate::runtime::{
|
use crate::runtime::{
|
||||||
instance::{Import, Imports},
|
instance::{ImportResolver, Import},
|
||||||
memory::LinearMemory,
|
memory::LinearMemory,
|
||||||
module::{ImportName, Module},
|
module::{ImportName, Module},
|
||||||
sig_registry::SigRegistry,
|
sig_registry::SigRegistry,
|
||||||
@ -114,7 +114,7 @@ impl LocalBacking {
|
|||||||
imports.functions[func_index.index()].clone()
|
imports.functions[func_index.index()].clone()
|
||||||
} else {
|
} else {
|
||||||
vm::ImportedFunc {
|
vm::ImportedFunc {
|
||||||
func: (module.function_resolver)(module, func_index)
|
func: module.func_resolver.get(module, func_index)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_ptr(),
|
.as_ptr(),
|
||||||
}
|
}
|
||||||
@ -237,7 +237,7 @@ pub struct ImportBacking {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ImportBacking {
|
impl ImportBacking {
|
||||||
pub fn new(module: &Module, imports: &Imports) -> Result<Self, String> {
|
pub fn new(module: &Module, imports: &dyn ImportResolver) -> Result<Self, String> {
|
||||||
assert!(
|
assert!(
|
||||||
module.imported_memories.len() == 0,
|
module.imported_memories.len() == 0,
|
||||||
"imported memories not yet supported"
|
"imported memories not yet supported"
|
||||||
@ -260,9 +260,9 @@ impl ImportBacking {
|
|||||||
let expected_sig = &module.signatures[expected_sig_index];
|
let expected_sig = &module.signatures[expected_sig_index];
|
||||||
let import = imports.get(mod_name, item_name);
|
let import = imports.get(mod_name, item_name);
|
||||||
if let Some(Import::Func(func, signature)) = import {
|
if let Some(Import::Func(func, signature)) = import {
|
||||||
if expected_sig == signature {
|
if expected_sig == &signature {
|
||||||
functions.push(vm::ImportedFunc {
|
functions.push(vm::ImportedFunc {
|
||||||
func: *func,
|
func: func,
|
||||||
// vmctx: ptr::null_mut(),
|
// vmctx: ptr::null_mut(),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@ -294,10 +294,10 @@ impl ImportBacking {
|
|||||||
globals.push(vm::ImportedGlobal {
|
globals.push(vm::ImportedGlobal {
|
||||||
global: vm::LocalGlobal {
|
global: vm::LocalGlobal {
|
||||||
data: match val {
|
data: match val {
|
||||||
Val::I32(n) => *n as u64,
|
Val::I32(n) => n as u64,
|
||||||
Val::I64(n) => *n as u64,
|
Val::I64(n) => n as u64,
|
||||||
Val::F32(n) => *n as u64,
|
Val::F32(n) => n as u64,
|
||||||
Val::F64(n) => *n,
|
Val::F64(n) => n,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -8,7 +8,6 @@ use crate::runtime::{
|
|||||||
types::{FuncIndex, FuncSig, Memory, Table, Type, Val},
|
types::{FuncIndex, FuncSig, Memory, Table, Type, Val},
|
||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
use hashbrown::HashMap;
|
|
||||||
use libffi::high::{arg as libffi_arg, call as libffi_call, CodePtr};
|
use libffi::high::{arg as libffi_arg, call as libffi_call, CodePtr};
|
||||||
use std::iter;
|
use std::iter;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -21,7 +20,7 @@ pub struct Instance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
pub fn new(module: Arc<Module>, imports: &Imports) -> Result<Box<Instance>, String> {
|
pub(in crate::runtime) fn new(module: Arc<Module>, imports: &dyn ImportResolver) -> Result<Box<Instance>, String> {
|
||||||
let sig_registry = SigRegistry::new(&*module);
|
let sig_registry = SigRegistry::new(&*module);
|
||||||
|
|
||||||
let import_backing = ImportBacking::new(&*module, imports)?;
|
let import_backing = ImportBacking::new(&*module, imports)?;
|
||||||
@ -109,7 +108,7 @@ impl Instance {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let func_ptr = CodePtr::from_ptr(
|
let func_ptr = CodePtr::from_ptr(
|
||||||
(self.module.function_resolver)(&*self.module, func_index)
|
self.module.func_resolver.get(&*self.module, func_index)
|
||||||
.expect("broken invariant, func resolver not synced with module.exports")
|
.expect("broken invariant, func resolver not synced with module.exports")
|
||||||
.cast()
|
.cast()
|
||||||
.as_ptr(),
|
.as_ptr(),
|
||||||
@ -144,25 +143,6 @@ pub enum Import {
|
|||||||
Global(Val),
|
Global(Val),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Imports {
|
pub trait ImportResolver {
|
||||||
map: HashMap<String, HashMap<String, Import>>,
|
fn get(&self, module: &str, name: &str) -> Option<Import>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Imports {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
map: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add(&mut self, module: String, name: String, import: Import) {
|
|
||||||
self.map
|
|
||||||
.entry(module)
|
|
||||||
.or_insert(HashMap::new())
|
|
||||||
.insert(name, import);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get(&self, module: &str, name: &str) -> Option<&Import> {
|
|
||||||
self.map.get(module).and_then(|m| m.get(name))
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,15 +9,15 @@ pub mod types;
|
|||||||
pub mod vm;
|
pub mod vm;
|
||||||
pub mod vmcalls;
|
pub mod vmcalls;
|
||||||
|
|
||||||
pub use self::backend::Compiler;
|
pub use self::backend::{Compiler, FuncResolver};
|
||||||
pub use self::instance::{Import, Imports, Instance};
|
pub use self::instance::{Import, ImportResolver, Instance};
|
||||||
pub use self::module::Module;
|
pub use self::module::Module;
|
||||||
|
|
||||||
/// Compile a webassembly module using the provided compiler and linked with the provided imports.
|
/// Compile a webassembly module using the provided compiler and linked with the provided imports.
|
||||||
pub fn compile(
|
pub fn instantiate(
|
||||||
compiler: &dyn Compiler,
|
|
||||||
wasm: &[u8],
|
wasm: &[u8],
|
||||||
imports: &Imports,
|
compiler: &dyn Compiler,
|
||||||
|
imports: &dyn ImportResolver,
|
||||||
) -> Result<Box<Instance>, String> {
|
) -> Result<Box<Instance>, String> {
|
||||||
let module = compiler.compile(wasm)?;
|
let module = compiler.compile(wasm)?;
|
||||||
Instance::new(module, imports)
|
Instance::new(module, imports)
|
||||||
|
@ -3,14 +3,13 @@ use crate::runtime::{
|
|||||||
FuncIndex, FuncSig, Global, GlobalDesc, GlobalIndex, Map, MapIndex, Memory, MemoryIndex,
|
FuncIndex, FuncSig, Global, GlobalDesc, GlobalIndex, Map, MapIndex, Memory, MemoryIndex,
|
||||||
SigIndex, Table, TableIndex,
|
SigIndex, Table, TableIndex,
|
||||||
},
|
},
|
||||||
vm,
|
backend::FuncResolver,
|
||||||
};
|
};
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use std::ptr::NonNull;
|
|
||||||
|
|
||||||
/// This is used to instantiate a new webassembly module.
|
/// This is used to instantiate a new webassembly module.
|
||||||
pub struct Module {
|
pub struct Module {
|
||||||
pub function_resolver: Box<dyn Fn(&Module, FuncIndex) -> Option<NonNull<vm::Func>>>,
|
pub func_resolver: Box<dyn FuncResolver>,
|
||||||
pub memories: Map<MemoryIndex, Memory>,
|
pub memories: Map<MemoryIndex, Memory>,
|
||||||
pub globals: Map<GlobalIndex, Global>,
|
pub globals: Map<GlobalIndex, Global>,
|
||||||
pub tables: Map<TableIndex, Table>,
|
pub tables: Map<TableIndex, Table>,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user