api/sys: Replace ImportObject with new type Imports

This commit is contained in:
Manos Pitsidianakis
2022-04-14 19:29:23 +03:00
parent 308d8a0a4b
commit 8aa225a192
26 changed files with 720 additions and 696 deletions

View File

@ -25,5 +25,4 @@
)]
pub mod error;
mod ordered_resolver;
pub mod wasm_c_api;

View File

@ -1,34 +0,0 @@
//! Ordered Resolvers are a custom kind of [`Resolver`] that retrieves
//! `EngineExport`s based on the index of the import, and not the module or name.
//!
//! This resolver is used in the Wasm-C-API as the imports are provided
//! by index and not by module and name.
use std::iter::FromIterator;
use wasmer_api::{Export, Exportable, Extern, Resolver};
/// An `OrderedResolver` stores all the `externs` provided to an Instance
/// in a Vec, so we can retrieve them later based on index.
#[derive(Clone)]
pub struct OrderedResolver {
/// The externs to be resolved by index
externs: Vec<Extern>,
}
impl Resolver for OrderedResolver {
fn resolve(&self, index: u32, _module: &str, _name: &str) -> Option<Export> {
self.externs
.get(index as usize)
.map(|extern_| extern_.to_export())
}
}
impl FromIterator<Extern> for OrderedResolver {
fn from_iter<I: IntoIterator<Item = Extern>>(iter: I) -> Self {
let mut externs = Vec::new();
for extern_ in iter {
externs.push(extern_);
}
OrderedResolver { externs }
}
}

View File

@ -2,7 +2,6 @@ use super::externals::wasm_extern_vec_t;
use super::module::wasm_module_t;
use super::store::wasm_store_t;
use super::trap::wasm_trap_t;
use crate::ordered_resolver::OrderedResolver;
use std::sync::Arc;
use wasmer_api::{Extern, Instance, InstantiationError};
@ -48,14 +47,14 @@ pub unsafe extern "C" fn wasm_instance_new(
let wasm_module = &module.inner;
let module_imports = wasm_module.imports();
let module_import_count = module_imports.len();
let resolver: OrderedResolver = imports
let externs = imports
.as_slice()
.iter()
.map(|imp| Extern::from(imp.as_ref().unwrap().as_ref().clone()))
.take(module_import_count)
.collect();
.collect::<Vec<Extern>>();
let instance = match Instance::new(wasm_module, &resolver) {
let instance = match Instance::new_by_index(wasm_module, &externs) {
Ok(instance) => Arc::new(instance),
Err(InstantiationError::Link(link_error)) => {

View File

@ -5,7 +5,7 @@ use super::super::{
externals::wasm_extern_t, module::wasm_module_t, store::wasm_store_t, types::wasm_name_t,
wasi::wasi_env_t,
};
use wasmer_api::Extern;
use wasmer_api::{Exportable, Extern};
use wasmer_wasi::{generate_import_object_from_env, get_wasi_version};
/// Unstable non-standard type wrapping `wasm_extern_t` with the
@ -175,10 +175,10 @@ fn wasi_get_unordered_imports_inner(
imports.set_buffer(
import_object
.into_iter()
.map(|((module, name), export)| {
.map(|((module, name), extern_)| {
let module = module.into();
let name = name.into();
let extern_inner = Extern::from_vm_export(store, export);
let extern_inner = Extern::from_vm_export(store, extern_.to_export());
Some(Box::new(wasmer_named_extern_t {
module,

View File

@ -17,7 +17,7 @@ use std::convert::TryFrom;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::slice;
use wasmer_api::{Extern, NamedResolver};
use wasmer_api::{Exportable, Extern};
use wasmer_wasi::{
generate_import_object_from_env, get_wasi_version, WasiEnv, WasiFile, WasiState,
WasiStateBuilder, WasiVersion,
@ -351,8 +351,8 @@ fn wasi_get_imports_inner(
.inner
.imports()
.map(|import_type| {
let export = import_object
.resolve_by_name(import_type.module(), import_type.name())
let ext = import_object
.get_export(import_type.module(), import_type.name())
.ok_or_else(|| {
format!(
"Failed to resolve import \"{}\" \"{}\"",
@ -360,7 +360,7 @@ fn wasi_get_imports_inner(
import_type.name()
)
})?;
let inner = Extern::from_vm_export(store, export);
let inner = Extern::from_vm_export(store, ext.to_export());
Ok(Some(Box::new(inner.into())))
})