mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-14 01:21:19 +00:00
Add export_func macro, prelude, and rename imports stuff
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
error::{LinkError, LinkResult},
|
||||
export::{Context, Export},
|
||||
import::Imports,
|
||||
import::ImportObject,
|
||||
memory::LinearMemory,
|
||||
module::{ImportName, ModuleInner},
|
||||
structures::{BoxedMap, Map, SliceMap, TypedIndex},
|
||||
@ -304,7 +304,7 @@ pub struct ImportBacking {
|
||||
impl ImportBacking {
|
||||
pub fn new(
|
||||
module: &ModuleInner,
|
||||
imports: &mut Imports,
|
||||
imports: &mut ImportObject,
|
||||
vmctx: *mut vm::Ctx,
|
||||
) -> LinkResult<Self> {
|
||||
let mut failed = false;
|
||||
@ -357,7 +357,7 @@ impl ImportBacking {
|
||||
|
||||
fn import_functions(
|
||||
module: &ModuleInner,
|
||||
imports: &mut Imports,
|
||||
imports: &mut ImportObject,
|
||||
vmctx: *mut vm::Ctx,
|
||||
) -> LinkResult<BoxedMap<ImportedFuncIndex, vm::ImportedFunc>> {
|
||||
let mut link_errors = vec![];
|
||||
@ -424,7 +424,7 @@ fn import_functions(
|
||||
|
||||
fn import_memories(
|
||||
module: &ModuleInner,
|
||||
imports: &mut Imports,
|
||||
imports: &mut ImportObject,
|
||||
vmctx: *mut vm::Ctx,
|
||||
) -> LinkResult<BoxedMap<ImportedMemoryIndex, vm::ImportedMemory>> {
|
||||
let mut link_errors = vec![];
|
||||
@ -491,7 +491,7 @@ fn import_memories(
|
||||
|
||||
fn import_tables(
|
||||
module: &ModuleInner,
|
||||
imports: &mut Imports,
|
||||
imports: &mut ImportObject,
|
||||
vmctx: *mut vm::Ctx,
|
||||
) -> LinkResult<BoxedMap<ImportedTableIndex, vm::ImportedTable>> {
|
||||
let mut link_errors = vec![];
|
||||
@ -556,7 +556,7 @@ fn import_tables(
|
||||
|
||||
fn import_globals(
|
||||
module: &ModuleInner,
|
||||
imports: &mut Imports,
|
||||
imports: &mut ImportObject,
|
||||
) -> LinkResult<BoxedMap<ImportedGlobalIndex, vm::ImportedGlobal>> {
|
||||
let mut link_errors = vec![];
|
||||
let mut globals = Map::with_capacity(module.imported_globals.len());
|
||||
|
@ -1,25 +1,25 @@
|
||||
use crate::export::Export;
|
||||
use hashbrown::{hash_map::Entry, HashMap};
|
||||
|
||||
pub trait Namespace {
|
||||
pub trait LikeNamespace {
|
||||
fn get_export(&mut self, name: &str) -> Option<Export>;
|
||||
}
|
||||
|
||||
pub struct Imports {
|
||||
map: HashMap<String, Box<dyn Namespace>>,
|
||||
pub struct ImportObject {
|
||||
map: HashMap<String, Box<dyn LikeNamespace>>,
|
||||
}
|
||||
|
||||
impl Imports {
|
||||
impl ImportObject {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
map: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register<S, N>(&mut self, name: S, namespace: N) -> Option<Box<dyn Namespace>>
|
||||
pub fn register<S, N>(&mut self, name: S, namespace: N) -> Option<Box<dyn LikeNamespace>>
|
||||
where
|
||||
S: Into<String>,
|
||||
N: Namespace + 'static,
|
||||
N: LikeNamespace + 'static,
|
||||
{
|
||||
match self.map.entry(name.into()) {
|
||||
Entry::Vacant(empty) => {
|
||||
@ -30,18 +30,18 @@ impl Imports {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_namespace(&mut self, namespace: &str) -> Option<&mut (dyn Namespace + 'static)> {
|
||||
pub fn get_namespace(&mut self, namespace: &str) -> Option<&mut (dyn LikeNamespace + 'static)> {
|
||||
self.map
|
||||
.get_mut(namespace)
|
||||
.map(|namespace| &mut **namespace)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NamespaceMap {
|
||||
pub struct Namespace {
|
||||
map: HashMap<String, Export>,
|
||||
}
|
||||
|
||||
impl NamespaceMap {
|
||||
impl Namespace {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
map: HashMap::new(),
|
||||
@ -53,7 +53,7 @@ impl NamespaceMap {
|
||||
}
|
||||
}
|
||||
|
||||
impl Namespace for NamespaceMap {
|
||||
impl LikeNamespace for Namespace {
|
||||
fn get_export(&mut self, name: &str) -> Option<Export> {
|
||||
self.map.get(name).cloned()
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use crate::{
|
||||
export::{
|
||||
Context, Export, ExportIter, FuncPointer, GlobalPointer, MemoryPointer, TablePointer,
|
||||
},
|
||||
import::{Imports, Namespace},
|
||||
import::{ImportObject, LikeNamespace},
|
||||
module::{ExportIndex, Module, ModuleInner},
|
||||
types::{
|
||||
FuncIndex, FuncSig, GlobalDesc, GlobalIndex, LocalOrImport, Memory, MemoryIndex, Table,
|
||||
@ -28,11 +28,11 @@ pub struct Instance {
|
||||
pub module: Rc<ModuleInner>,
|
||||
inner: Box<InstanceInner>,
|
||||
#[allow(dead_code)]
|
||||
imports: Box<Imports>,
|
||||
imports: Box<ImportObject>,
|
||||
}
|
||||
|
||||
impl Instance {
|
||||
pub(crate) fn new(module: Rc<ModuleInner>, mut imports: Box<Imports>) -> Result<Instance> {
|
||||
pub(crate) fn new(module: Rc<ModuleInner>, mut imports: Box<ImportObject>) -> Result<Instance> {
|
||||
// We need the backing and import_backing to create a vm::Ctx, but we need
|
||||
// a vm::Ctx to create a backing and an import_backing. The solution is to create an
|
||||
// uninitialized vm::Ctx and then initialize it in-place.
|
||||
@ -325,7 +325,7 @@ impl InstanceInner {
|
||||
}
|
||||
}
|
||||
|
||||
impl Namespace for Instance {
|
||||
impl LikeNamespace for Instance {
|
||||
fn get_export(&mut self, name: &str) -> Option<Export> {
|
||||
let export_index = self.module.exports.get(name)?;
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
extern crate field_offset;
|
||||
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
mod macros;
|
||||
#[doc(hidden)]
|
||||
pub mod backend;
|
||||
mod backing;
|
||||
@ -29,6 +29,17 @@ pub use self::instance::Instance;
|
||||
pub use self::module::Module;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::export_func;
|
||||
pub use crate::import::{ImportObject, Namespace};
|
||||
pub use crate::types::{
|
||||
FuncIndex, GlobalIndex, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex,
|
||||
ImportedTableIndex, LocalFuncIndex, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex,
|
||||
MemoryIndex, TableIndex, Type, Value,
|
||||
};
|
||||
pub use crate::vm;
|
||||
}
|
||||
|
||||
/// Compile a webassembly module using the provided compiler.
|
||||
pub fn compile(wasm: &[u8], compiler: &dyn backend::Compiler) -> CompileResult<module::Module> {
|
||||
let token = backend::Token::generate();
|
||||
|
@ -1,5 +1,23 @@
|
||||
#[macro_export]
|
||||
macro_rules! debug {
|
||||
($fmt:expr) => (if cfg!(any(debug_assertions, feature="debug")) { println!(concat!("wasmer-runtime(:{})::", $fmt), line!()) });
|
||||
($fmt:expr, $($arg:tt)*) => (if cfg!(any(debug_assertions, feature="debug")) { println!(concat!("wasmer-runtime(:{})::", $fmt, "\n"), line!(), $($arg)*) });
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! export_func {
|
||||
($func:ident, [ $( $params:ident ),* ] -> [ $( $returns:ident ),* ]) => {{
|
||||
use wasmer_runtime::{
|
||||
export::{Context, Export, FuncPointer},
|
||||
types::{FuncSig, Type},
|
||||
};
|
||||
|
||||
Export::Function {
|
||||
func: FuncPointer::new($func as _),
|
||||
ctx: Context::Internal,
|
||||
signature: FuncSig {
|
||||
params: vec![$(Type::$params,)*],
|
||||
returns: vec![$(Type::$params,)*],
|
||||
},
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
backend::{FuncResolver, ProtectedCaller},
|
||||
error::Result,
|
||||
import::Imports,
|
||||
import::ImportObject,
|
||||
sig_registry::SigRegistry,
|
||||
structures::Map,
|
||||
types::{
|
||||
@ -50,7 +50,7 @@ impl Module {
|
||||
}
|
||||
|
||||
/// Instantiate a WebAssembly module with the provided imports.
|
||||
pub fn instantiate(&self, imports: Imports) -> Result<Instance> {
|
||||
pub fn instantiate(&self, imports: ImportObject) -> Result<Instance> {
|
||||
Instance::new(Rc::clone(&self.0), Box::new(imports))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user