Code loader framework.

This commit is contained in:
losfair
2019-05-03 00:23:41 +08:00
parent 99c101f312
commit 14bfd75ac9
7 changed files with 242 additions and 1 deletions

View File

@ -12,6 +12,8 @@ use crate::{
typed_func::{Func, Wasm, WasmTrapInfo, WasmTypeList},
types::{FuncIndex, FuncSig, GlobalIndex, LocalOrImport, MemoryIndex, TableIndex, Type, Value},
vm,
loader::{self, Loader, Instance as _},
structures::TypedIndex,
};
use smallvec::{smallvec, SmallVec};
use std::{mem, ptr::NonNull, sync::Arc};
@ -38,7 +40,7 @@ impl Drop for InstanceInner {
///
/// [`ImportObject`]: struct.ImportObject.html
pub struct Instance {
module: Arc<ModuleInner>,
pub module: Arc<ModuleInner>,
inner: Box<InstanceInner>,
#[allow(dead_code)]
import_object: ImportObject,
@ -127,6 +129,10 @@ impl Instance {
Ok(instance)
}
pub fn load<T: Loader>(&self, loader: T) -> ::std::result::Result<T::Instance, T::Error> {
loader.load(&*self.module.runnable_module, &self.module.info, unsafe { &(*self.inner.vmctx).internal })
}
/// Through generic magic and the awe-inspiring power of traits, we bring you...
///
/// # "Func"
@ -214,6 +220,32 @@ impl Instance {
}
}
pub fn resolve_local_func(&self, name: &str) -> ResolveResult<usize> {
let export_index =
self.module
.info
.exports
.get(name)
.ok_or_else(|| ResolveError::ExportNotFound {
name: name.to_string(),
})?;
if let ExportIndex::Func(func_index) = export_index {
match func_index.local_or_import(&self.module.info) {
LocalOrImport::Local(x) => Ok(x.index()),
LocalOrImport::Import(x) => Err(ResolveError::ExportWrongType {
name: name.to_string(),
}
.into())
}
} else {
Err(ResolveError::ExportWrongType {
name: name.to_string(),
}
.into())
}
}
/// This returns the representation of a function that can be called
/// safely.
///