Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeryAntopol committed Sep 30, 2022
1 parent 6834c3f commit d9d6b39
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
4 changes: 2 additions & 2 deletions core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct MModuleConfig<WB: WasmBackend> {
pub max_heap_pages_count: u32,

/// Import object that will be used in module instantiation process.
pub raw_imports: <WB as WasmBackend>::IO,
pub raw_imports: ImportObject<WB>,

/// Imports from the host side that will be used in module instantiation process.
pub host_imports: HashMap<String, HostImportDescriptor<WB>>,
Expand All @@ -77,7 +77,7 @@ impl<WB: WasmBackend> Default for MModuleConfig<WB> {
// some reasonable defaults
Self {
max_heap_pages_count: DEFAULT_HEAP_PAGES_COUNT,
raw_imports: <WB as WasmBackend>::IO::new(),
raw_imports: ImportObject::<WB>::default(),
host_imports: HashMap::new(),
wasi_version: WasiVersion::Latest,
wasi_envs: HashMap::new(),
Expand Down
20 changes: 12 additions & 8 deletions core/src/module/marine_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ pub(crate) struct MModule<WB: WasmBackend> {
// import_object is needed because ImportObject::extend doesn't really deep copy
// imports, so we need to store imports of this module to prevent their removing.
#[allow(unused)]
it_import_object: <WB as WasmBackend>::IO,
it_import_object: ImportObject<WB>,

// host_import_object is needed because ImportObject::extend doesn't really deep copy
// imports, so we need to store imports of this module to prevent their removing.
#[allow(unused)]
host_import_object: <WB as WasmBackend>::IO,
host_import_object: ImportObject<WB>,

// host_closures_import_object is needed because ImportObject::extend doesn't really deep copy
// imports, so we need to store imports of this module to prevent their removing.
#[allow(unused)]
host_closures_import_object: <WB as WasmBackend>::IO,
host_closures_import_object: ImportObject<WB>,

// TODO: replace with dyn Trait
export_funcs: ExportFunctions<WB>,
Expand Down Expand Up @@ -241,8 +241,8 @@ impl<WB: WasmBackend> MModule<WB> {
fn create_import_objects(
config: MModuleConfig<WB>,
mit: &MITInterfaces<'_>,
wit_import_object: <WB as WasmBackend>::IO,
) -> MResult<(<WB as WasmBackend>::IO, <WB as WasmBackend>::IO)> {
wit_import_object: ImportObject<WB>,
) -> MResult<(ImportObject<WB>, ImportObject<WB>)> {
use crate::host_imports::create_host_import_func;

let wasi_envs = config
Expand Down Expand Up @@ -277,7 +277,7 @@ impl<WB: WasmBackend> MModule<WB> {
let host_import = create_host_import_func::<WB>(descriptor, record_types.clone());
host_closures_namespace.insert(import_name, host_import);
}
let mut host_closures_import_object = <WB as WasmBackend>::IO::new();
let mut host_closures_import_object = ImportObject::<WB>::default();
host_closures_import_object.register("host", host_closures_namespace);

wasi_import_object.extend_with_self(wit_import_object);
Expand Down Expand Up @@ -323,7 +323,7 @@ impl<WB: WasmBackend> MModule<WB> {
fn adjust_wit_imports(
wit: &MITInterfaces<'_>,
wit_instance: Arc<MaybeUninit<ITInstance<WB>>>,
) -> MResult<<WB as WasmBackend>::IO> {
) -> MResult<ImportObject<WB>> {
use marine_it_interfaces::ITAstType;
//use wasmer_core::typed_func::DynamicFunc;
//use wasmer_core::vm::Ctx;
Expand Down Expand Up @@ -442,7 +442,7 @@ impl<WB: WasmBackend> MModule<WB> {
})
.collect::<MResult<multimap::MultiMap<_, _>>>()?;

let mut import_object = <WB as WasmBackend>::IO::new();
let mut import_object = ImportObject::<WB>::defailt();

// TODO: refactor this
for (namespace_name, funcs) in wit_import_funcs.into_iter() {
Expand All @@ -456,3 +456,7 @@ impl<WB: WasmBackend> MModule<WB> {
Ok(import_object)
}
}


mut: [x:; @x];
mut: [isConst] [0 .ERROR_CAN_ONLY_HANDLE_MUTABLE_OBJECTS] pfunc;
2 changes: 1 addition & 1 deletion core/src/module/wit_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::MResult;
use marine_wasm_backend_traits::WasmBackend;
//use marine_wasm_backend_traits::Module;
use marine_wasm_backend_traits::Instance;
use marine_wasm_backend_traits::ImportObject;
//use marine_wasm_backend_traits::ImportObject;
use marine_wasm_backend_traits::Exports;
use marine_wasm_backend_traits::Memory as WBMemory;

Expand Down
2 changes: 2 additions & 0 deletions crates/wasm-backend-traits/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum Export<M: MemoryExport, F: FunctionExport> {
Other,
}

pub type ExportShort<WB: WasmBackend> = Export<<WB as WasmBackend>::MemoryExport, <WB as WasmBackend>::FunctionExport>;

pub trait ExportedDynFunc<WB: WasmBackend> {
fn signature(&self) -> &FuncSig;

Expand Down
27 changes: 22 additions & 5 deletions crates/wasm-backend-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod wasi;
pub mod wtype;

use std::borrow::Cow;
use std::collections::HashMap;

use it_memory_traits::{MemoryView};

Expand All @@ -12,8 +13,22 @@ pub use exports::*;
pub use wasi::*;
pub use wtype::*;


/*
pub trait WasmBackendNew: Clone {
type Module: ModuleNew;
type Store: StoreNew;
type Instance: InstanceNew;
type Func: FuncNew;
type Memory: MemoryNew;
type Table: TableNew;
type Global: GlobalNew;
type Engine: EngineNew;
type Exporty: ExportNew;
}
*/
pub trait WasmBackend: Clone + 'static {
type IO: ImportObject<Self>;
//type IO: ImportObject<Self>;
type Exports: Exports<Self>;
type MemoryExport: MemoryExport;
type WITMemory: Memory<Self> + it_memory_traits::Memory<Self::WITMemoryView> + Clone + 'static;
Expand All @@ -33,11 +48,13 @@ pub trait WasmBackend: Clone + 'static {
fn compile(wasm: &[u8]) -> WasmBackendResult<Self::M>;
}

pub type ImportObject<WB: WasmBackend> = HashMap<String, Vec<(String, ExportShort<WB>)>>;

pub trait Module<WB: WasmBackend> {
fn custom_sections(&self, key: &str) -> Option<&[Vec<u8>]>;
fn instantiate(
&self,
imports: &<WB as WasmBackend>::IO,
imports: &ImportObject<WB>,
) -> WasmBackendResult<<WB as WasmBackend>::I>;
}

Expand All @@ -53,10 +70,10 @@ pub trait Instance<WB: WasmBackend> {
> + 'a,
>;
fn exports(&self) -> &<WB as WasmBackend>::Exports;
fn import_object(&self) -> &<WB as WasmBackend>::IO;
fn import_object(&self) -> &ImportObject<WB>;
fn memory(&self, memory_index: u32) -> <WB as WasmBackend>::WITMemory;
}

/*
pub trait ImportObject<WB: WasmBackend>:
Clone
+ Extend<(
Expand All @@ -80,7 +97,7 @@ pub trait ImportObject<WB: WasmBackend>:
&self,
) -> Option<Export<<WB as WasmBackend>::MemoryExport, <WB as WasmBackend>::FunctionExport>>;
}

*/
pub trait DynamicFunc<'a, WB: WasmBackend> {
fn new<'c, F>(sig: FuncSig, func: F) -> Self
where
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-backend-traits/src/wasi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::WasmBackend;
use crate::{ImportObject, WasmBackend};

use std::path::PathBuf;

Expand All @@ -9,7 +9,7 @@ pub trait WasiImplementation<WB: WasmBackend> {
envs: Vec<Vec<u8>>,
preopened_files: Vec<PathBuf>,
mapped_dirs: Vec<(String, PathBuf)>,
) -> Result<<WB as WasmBackend>::IO, String>;
) -> Result<ImportObject<WB>, String>;

fn get_wasi_state<'s>(instance: &'s mut <WB as WasmBackend>::I) -> Box<dyn WasiState + 's>;
}
Expand Down
5 changes: 4 additions & 1 deletion crates/wasmer-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl WasmBackend for WasmerBackend /*<'b>*/ {
type FunctionExport = WasmerFunctionExport;
type M = WasmerModule;
type I = WasmerInstance;
type IO = WasmerImportObject;
//type IO = WasmerImportObject;
//type SR = WasmerSequentialReader<'b>;
//type SW = WasmerSequentialWriter<'b>;
type WITMemory = WITMemory;
Expand Down Expand Up @@ -123,6 +123,8 @@ impl Instance<WasmerBackend> for WasmerInstance {
}
}

pub type WasmerImportObject = ImportObject<WasmerBackend>;
/*
#[derive(Clone)]
pub struct WasmerImportObject {
pub import_object: wasmer_runtime::ImportObject,
Expand Down Expand Up @@ -203,6 +205,7 @@ impl ImportObject<WasmerBackend> for WasmerImportObject {
self.import_object.maybe_with_namespace(namespace, f)
}*/
}
*/

pub struct WasmerFunctionExport {
func: wasmer_core::export::FuncPointer,
Expand Down

0 comments on commit d9d6b39

Please sign in to comment.