diff --git a/Cargo.lock b/Cargo.lock index 3ae1d22a..294264ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1610,6 +1610,7 @@ dependencies = [ "itertools 0.10.3", "marine-it-interfaces 0.5.0", "marine-module-interface 0.2.0", + "marine-wasm-backend-traits", "nom", "semver 0.11.0", "serde", @@ -1658,6 +1659,7 @@ dependencies = [ "anyhow", "chrono", "marine-rs-sdk-main", + "marine-wasm-backend-traits", "semver 0.11.0", "serde", "thiserror", @@ -1799,6 +1801,8 @@ dependencies = [ "marine-module-info-parser 0.2.2", "marine-module-interface 0.2.0", "marine-utils 0.4.0", + "marine-wasm-backend-traits", + "marine-wasmer-backend", "multimap", "once_cell", "parity-wasm 0.42.2", @@ -1875,6 +1879,26 @@ checksum = "8dc5838acba84ce4d802d672afd0814fae0ae7098021ae5b06d975e70d09f812" name = "marine-utils" version = "0.4.0" +[[package]] +name = "marine-wasm-backend-traits" +version = "0.1.0" +dependencies = [ + "thiserror", + "wasmer-interface-types-fl 0.21.1", + "wasmer-runtime-core-fl", + "wasmer-runtime-fl", +] + +[[package]] +name = "marine-wasmer-backend" +version = "0.1.0" +dependencies = [ + "marine-wasm-backend-traits", + "wasmer-interface-types-fl 0.21.1", + "wasmer-runtime-core-fl", + "wasmer-runtime-fl", +] + [[package]] name = "marine-web-runtime" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 018ff55b..07a83d54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ members = [ "crates/min-it-version", "crates/module-info-parser", "crates/module-interface", + "crates/wasm-backend-traits", + "crates/wasmer-backend", "crates/utils", "examples/call_parameters", "examples/greeting", diff --git a/crates/it-parser/Cargo.toml b/crates/it-parser/Cargo.toml index d9735e92..1d86a526 100644 --- a/crates/it-parser/Cargo.toml +++ b/crates/it-parser/Cargo.toml @@ -13,6 +13,7 @@ path = "src/lib.rs" [dependencies] marine-it-interfaces = { path = "../it-interfaces", version = "0.5.0" } marine-module-interface = { path = "../module-interface", version = "0.2.0" } +marine-wasm-backend-traits = { path = "../wasm-backend-traits", version = "0.1.0"} anyhow = "1.0.31" walrus = "0.18.0" diff --git a/crates/it-parser/src/extractor/it.rs b/crates/it-parser/src/extractor/it.rs index 5b63118b..db6e8138 100644 --- a/crates/it-parser/src/extractor/it.rs +++ b/crates/it-parser/src/extractor/it.rs @@ -20,7 +20,8 @@ use crate::ParserResult; use walrus::IdsToIndices; use wasmer_it::ast::Interfaces; -use wasmer_core::Module as WasmerModule; +//use wasmer_core::Module as WasmerModule; +use marine_wasm_backend_traits::Module as ModuleTrait; use std::borrow::Cow; use std::path::Path; @@ -42,7 +43,7 @@ where } /// Extracts IT section of provided Wasm binary and converts it to a MITInterfaces. -pub fn extract_it_from_module(wasmer_module: &WasmerModule) -> ParserResult> { +pub fn extract_it_from_module(wasmer_module: &M) -> ParserResult> { let wit_sections = wasmer_module .custom_sections(IT_SECTION_NAME) .ok_or(ITParserError::NoITSection)?; diff --git a/crates/module-info-parser/Cargo.toml b/crates/module-info-parser/Cargo.toml index d4528720..43578a3f 100644 --- a/crates/module-info-parser/Cargo.toml +++ b/crates/module-info-parser/Cargo.toml @@ -14,6 +14,7 @@ path = "src/lib.rs" marine-rs-sdk-main = "0.6.13" wasmer-core = { package = "wasmer-runtime-core-fl", version = "=0.17.1" } +marine-wasm-backend-traits = { path = "../wasm-backend-traits", version = "0.1.0"} anyhow = "1.0.31" chrono = "0.4.19" diff --git a/crates/module-info-parser/src/sdk_version/version_extractor.rs b/crates/module-info-parser/src/sdk_version/version_extractor.rs index 89a66887..1f50ae48 100644 --- a/crates/module-info-parser/src/sdk_version/version_extractor.rs +++ b/crates/module-info-parser/src/sdk_version/version_extractor.rs @@ -20,7 +20,8 @@ use super::SDKVersionError; use crate::extract_custom_sections_by_name; use crate::try_as_one_section; -use wasmer_core::Module as WasmerModule; +//use wasmer_core::Module as WasmerModule; +use marine_wasm_backend_traits::Module as ModuleTrait; use marine_rs_sdk_main::VERSION_SECTION_NAME; use walrus::ModuleConfig; use walrus::Module; @@ -56,8 +57,8 @@ pub fn extract_from_module(wasm_module: &Module) -> ModuleInfoResult( + wasmer_module: &M, ) -> ModuleInfoResult> { let sections = wasmer_module.custom_sections(VERSION_SECTION_NAME); diff --git a/crates/wasm-backend-traits/Cargo.toml b/crates/wasm-backend-traits/Cargo.toml new file mode 100644 index 00000000..aa583a45 --- /dev/null +++ b/crates/wasm-backend-traits/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "marine-wasm-backend-traits" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +thiserror = "1.0.24" + +wasmer-it = { package = "wasmer-interface-types-fl", version = "0.21.1" } +wasmer-runtime = { package = "wasmer-runtime-fl", version = "=0.17.1" } +# dynamicfunc-fat-closures allows using state inside DynamicFunc +wasmer-core = { package = "wasmer-runtime-core-fl", version = "=0.17.1", features = ["dynamicfunc-fat-closures"] } \ No newline at end of file diff --git a/crates/wasm-backend-traits/src/lib.rs b/crates/wasm-backend-traits/src/lib.rs new file mode 100644 index 00000000..c61b178d --- /dev/null +++ b/crates/wasm-backend-traits/src/lib.rs @@ -0,0 +1,35 @@ +use thiserror::Error; + +pub struct Value {} + +#[derive(Debug, Error)] +pub enum WasmBackendError { + #[error("Some error")] + SomeError, +} + +pub type WasmBackendResult = Result; + +pub trait WasmBackend: Clone + 'static { + type M: Module; + + fn compile(wasm: &[u8]) -> WasmBackendResult; +} + +pub trait Module { + type I: Instance; + + fn custom_sections(&self, key: &str) -> Option<&[Vec]>; + fn instantiate(&self, imports: &wasmer_runtime::ImportObject) -> WasmBackendResult; +} + +pub trait Instance { + + fn export_iter<'a>(&'a self) -> Box + 'a>; + fn exports(&self) -> &wasmer_core::instance::Exports; + fn import_object(&self) -> &wasmer_runtime::ImportObject; + + // maybe hide them inside impl + fn context(&self) -> &wasmer_core::vm::Ctx; + fn context_mut(&mut self) -> &mut wasmer_core::vm::Ctx; +} diff --git a/crates/wasmer-backend/Cargo.toml b/crates/wasmer-backend/Cargo.toml new file mode 100644 index 00000000..b0ea17d3 --- /dev/null +++ b/crates/wasmer-backend/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "marine-wasmer-backend" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +marine-wasm-backend-traits = {path = "../wasm-backend-traits", version = "0.1.0"} +wasmer-runtime = { package = "wasmer-runtime-fl", version = "=0.17.1" } +# dynamicfunc-fat-closures allows using state inside DynamicFunc +wasmer-core = { package = "wasmer-runtime-core-fl", version = "=0.17.1", features = ["dynamicfunc-fat-closures"] } +wasmer-it = { package = "wasmer-interface-types-fl", version = "0.21.1" } \ No newline at end of file diff --git a/crates/wasmer-backend/src/lib.rs b/crates/wasmer-backend/src/lib.rs new file mode 100644 index 00000000..b719c6d6 --- /dev/null +++ b/crates/wasmer-backend/src/lib.rs @@ -0,0 +1,75 @@ +use marine_wasm_backend_traits::WasmBackend; +use marine_wasm_backend_traits::WasmBackendResult; +use marine_wasm_backend_traits::WasmBackendError; +use marine_wasm_backend_traits::Module; +use marine_wasm_backend_traits::Instance; + +#[derive(Clone)] +pub struct WasmerBackend {} + +impl WasmBackend for WasmerBackend { + type M = WasmerModule; + + fn compile(wasm: &[u8]) -> WasmBackendResult { + wasmer_runtime::compile(wasm).map_err(|_| { + WasmBackendError::SomeError + }).map(|module| { + WasmerModule { module } + }) + } +} + +pub struct WasmerModule { + module: wasmer_core::Module, +} + +impl Module for WasmerModule { + type I = WasmerInstance; + + fn custom_sections(&self, name: &str) -> Option<&[Vec]> { + self.module.custom_sections(name) + } + + fn instantiate(&self, imports: &wasmer_runtime::ImportObject) -> WasmBackendResult { + self.module + .instantiate(&imports) + .map_err(|_| { + WasmBackendError::SomeError + }) + .map(|instance| {WasmerInstance{instance}}) + } +} + +pub struct WasmerInstance { + instance: wasmer_core::Instance, +} + +impl Instance for WasmerInstance { + fn export_iter<'a>(&'a self) -> Box + 'a> { + let exports = self.instance.exports(); + Box::new(exports) + } + + fn exports(&self) -> &wasmer_core::instance::Exports { + &self.instance.exports + } + + fn import_object(&self) -> &wasmer_runtime::ImportObject { + &self.instance.import_object + } + + fn context(&self) -> &wasmer_core::vm::Ctx { self.instance.context() } + fn context_mut(&mut self) -> &mut wasmer_core::vm::Ctx { self.instance.context_mut()} +} +/* +pub struct WasmerExportIter { + export_iter: Box + 'a> +} + +impl<'a> Iterator for WasmerExportIter<'a> { + type Item = (String, wasmer_runtime::Export); + + fn next(&mut self) -> Option { + self.export_iter.as_mut().next() + } +}*/ \ No newline at end of file diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index e4c3301b..f72deafa 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -18,6 +18,7 @@ marine-it-generator = { path = "../crates/it-generator", version = "0.6.0" } marine-module-interface = { path = "../crates/module-interface", version = "0.2.0" } marine-utils = { path = "../crates/utils", version = "0.4.0" } marine-min-it-version = { path = "../crates/min-it-version", version = "0.1.0"} +marine-wasm-backend-traits = { path = "../crates/wasm-backend-traits", version = "0.1.0"} wasmer-runtime = { package = "wasmer-runtime-fl", version = "=0.17.1" } # dynamicfunc-fat-closures allows using state inside DynamicFunc @@ -47,3 +48,4 @@ reqwest = "0.10.4" bytes = "0.5.4" tokio = { version = "0.2.20", features = ["blocking", "macros"] } once_cell = "1.4.0" +marine-wasmer-backend = { path = "../crates/wasmer-backend", version = "0.1.0"} diff --git a/runtime/src/engine.rs b/runtime/src/engine.rs index edfd6605..32ee2a8a 100644 --- a/runtime/src/engine.rs +++ b/runtime/src/engine.rs @@ -18,6 +18,8 @@ use super::*; use crate::module::MModule; use crate::module::MRecordTypes; +use marine_wasm_backend_traits::WasmBackend; + use serde::Serialize; use std::collections::hash_map::Entry; @@ -32,12 +34,12 @@ pub struct MModuleInterface<'a> { } /// The base struct of Marine, the Fluence compute runtime. -pub struct Marine { +pub struct Marine { // set of modules registered inside Marine - modules: HashMap, + modules: HashMap>, } -impl Marine { +impl Marine { pub fn new() -> Self { Self { modules: HashMap::new(), @@ -149,7 +151,7 @@ impl Marine { records.into() } - fn get_module_interface(module: &MModule) -> MModuleInterface<'_> { + fn get_module_interface(module: &MModule) -> MModuleInterface<'_> { let record_types = module.export_record_types(); let function_signatures = module.get_exports_signatures().collect::>(); @@ -161,7 +163,7 @@ impl Marine { } } -impl Default for Marine { +impl Default for Marine { fn default() -> Self { Self::new() } diff --git a/runtime/src/errors.rs b/runtime/src/errors.rs index 1ad6e0b2..09fdb572 100644 --- a/runtime/src/errors.rs +++ b/runtime/src/errors.rs @@ -20,7 +20,7 @@ use crate::misc::PrepareError; use marine_it_interfaces::MITInterfacesError; use marine_it_parser::ITParserError; use marine_module_interface::it_interface::ITInterfaceError; - +use marine_wasm_backend_traits::WasmBackendError; use wasmer_runtime::error as wasmer_error; use thiserror::Error as ThisError; @@ -101,6 +101,9 @@ pub enum MError { /// Incorrect IT section. #[error("{0}")] IncorrectWIT(String), + + #[error("WASM BACKEND ERROR: {0}")] + WasmBackendError(WasmBackendError) } impl From for MError { @@ -132,3 +135,9 @@ impl From<()> for MError { MError::IncorrectWIT("failed to parse instructions for adapter type".to_string()) } } + +impl From for MError { + fn from(err: WasmBackendError) -> Self { + MError::WasmBackendError(err) + } +} diff --git a/runtime/src/misc/version_checker.rs b/runtime/src/misc/version_checker.rs index 2c3018f6..87ff3e12 100644 --- a/runtime/src/misc/version_checker.rs +++ b/runtime/src/misc/version_checker.rs @@ -21,11 +21,13 @@ use marine_module_info_parser::sdk_version; use marine_min_it_version::min_sdk_version; use marine_min_it_version::min_it_version; -use wasmer_core::Module; +use marine_wasm_backend_traits::Module; -pub(crate) fn check_sdk_version( +//use wasmer_core::Module; + +pub(crate) fn check_sdk_version( name: impl Into, - wasmer_module: &Module, + wasmer_module: &M, ) -> PrepareResult<()> { let module_version = sdk_version::extract_from_wasmer_module(wasmer_module)?; let module_version = match module_version { diff --git a/runtime/src/module/marine_module.rs b/runtime/src/module/marine_module.rs index 0f783819..29ede59e 100644 --- a/runtime/src/module/marine_module.rs +++ b/runtime/src/module/marine_module.rs @@ -21,12 +21,16 @@ use super::{IType, IRecordType, IFunctionArg, IValue, WValue}; use crate::MResult; use crate::MModuleConfig; +use marine_wasm_backend_traits::WasmBackend; +use marine_wasm_backend_traits::Module; +use marine_wasm_backend_traits::Instance; + use marine_it_interfaces::MITInterfaces; use marine_it_parser::extract_it_from_module; use marine_utils::SharedString; -use wasmer_core::Instance as WasmerInstance; +//use wasmer_core::Instance as WasmerInstance; use wasmer_core::import::Namespace; -use wasmer_runtime::compile; +//use wasmer_runtime::compile; use wasmer_runtime::ImportObject; use wasmer_it::interpreter::Interpreter; @@ -36,23 +40,23 @@ use std::mem::MaybeUninit; use std::sync::Arc; use std::rc::Rc; -type ITInterpreter = - Interpreter>; +type ITInterpreter = + Interpreter, ITExport, WITFunction, WITMemory, WITMemoryView<'static>>; #[derive(Clone)] -pub(super) struct ITModuleFunc { - interpreter: Arc, +pub(super) struct ITModuleFunc { + interpreter: Arc>, pub(super) arguments: Rc>, pub(super) output_types: Rc>, } #[derive(Clone)] -pub(super) struct Callable { - pub(super) it_instance: Arc, - pub(super) it_module_func: ITModuleFunc, +pub(super) struct Callable { + pub(super) it_instance: Arc>, + pub(super) it_module_func: ITModuleFunc, } -impl Callable { +impl Callable { pub fn call(&mut self, args: &[IValue]) -> MResult> { use wasmer_it::interpreter::stack::Stackable; @@ -67,13 +71,13 @@ impl Callable { } } -type ExportFunctions = HashMap>; +type ExportFunctions = HashMap>>; -pub(crate) struct MModule { +pub(crate) struct MModule { // wasmer_instance is needed because WITInstance contains dynamic functions // that internally keep pointer to it. #[allow(unused)] - wasmer_instance: Box, + wasmer_instance: Box<<::M as Module>::I>, // 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. @@ -91,21 +95,21 @@ pub(crate) struct MModule { host_closures_import_object: ImportObject, // TODO: replace with dyn Trait - export_funcs: ExportFunctions, + export_funcs: ExportFunctions, // TODO: save refs instead copying of a record types HashMap. /// Record types used in exported functions as arguments or return values. export_record_types: MRecordTypes, } -impl MModule { +impl MModule { pub(crate) fn new( name: &str, wasm_bytes: &[u8], config: MModuleConfig, - modules: &HashMap, + modules: &HashMap>, ) -> MResult { - let wasmer_module = compile(wasm_bytes)?; + let wasmer_module = WB::compile(wasm_bytes)?; crate::misc::check_sdk_version(name, &wasmer_module)?; let it = extract_it_from_module(&wasmer_module)?; @@ -125,14 +129,14 @@ impl MModule { // it and the environment is single-threaded *Arc::get_mut_unchecked(&mut wit_instance) = MaybeUninit::new(ITInstance::new(&wasmer_instance, name, &mit, modules)?); - std::mem::transmute::<_, Arc>(wit_instance) + std::mem::transmute::<_, Arc>>(wit_instance) }; let (export_funcs, export_record_types) = Self::instantiate_exports(&it_instance, &mit)?; // call _start to populate the WASI state of the module #[rustfmt::skip] - if let Ok(start_func) = wasmer_instance.exports.get::>("_start") { + if let Ok(start_func) = wasmer_instance.exports().get::>("_start") { start_func.call()?; } @@ -199,7 +203,7 @@ impl MModule { &self, module_name: &str, function_name: &str, - ) -> MResult> { + ) -> MResult>> { match self.export_funcs.get(function_name) { Some(func) => Ok(func.clone()), None => Err(MError::NoSuchFunction( @@ -259,9 +263,9 @@ impl MModule { } fn instantiate_exports( - it_instance: &Arc, + it_instance: &Arc>, mit: &MITInterfaces<'_>, - ) -> MResult<(ExportFunctions, MRecordTypes)> { + ) -> MResult<(ExportFunctions, MRecordTypes)> { let module_interface = marine_module_interface::it_interface::get_interface(mit)?; let export_funcs = module_interface @@ -270,7 +274,7 @@ impl MModule { .map(|sign| { let adapter_instructions = mit.adapter_by_type_r(sign.adapter_function_type)?; - let interpreter: ITInterpreter = adapter_instructions.clone().try_into()?; + let interpreter: ITInterpreter = adapter_instructions.clone().try_into()?; let it_module_func = ITModuleFunc { interpreter: Arc::new(interpreter), arguments: sign.arguments.clone(), @@ -285,7 +289,7 @@ impl MModule { Ok((shared_string, callable)) }) - .collect::>()?; + .collect::>>()?; Ok((export_funcs, module_interface.export_record_types)) } @@ -293,7 +297,7 @@ impl MModule { // this function deals only with import functions that have an adaptor implementation fn adjust_wit_imports( wit: &MITInterfaces<'_>, - wit_instance: Arc>, + wit_instance: Arc>>, ) -> MResult { use marine_it_interfaces::ITAstType; use wasmer_core::typed_func::DynamicFunc; @@ -317,9 +321,9 @@ impl MModule { } // creates a closure that is represent a IT module import - fn create_raw_import( - wit_instance: Arc>, - interpreter: ITInterpreter, + fn create_raw_import( + wit_instance: Arc>>, + interpreter: ITInterpreter, import_namespace: String, import_name: String, ) -> impl Fn(&mut Ctx, &[WValue]) -> Vec + 'static { @@ -383,7 +387,7 @@ impl MModule { arguments, output_types, } => { - let interpreter: ITInterpreter = adapter_instructions.clone().try_into()?; + let interpreter: ITInterpreter = adapter_instructions.clone().try_into()?; let raw_import = create_raw_import( wit_instance.clone(), diff --git a/runtime/src/module/wit_function.rs b/runtime/src/module/wit_function.rs index ffb59f1b..1b342b94 100644 --- a/runtime/src/module/wit_function.rs +++ b/runtime/src/module/wit_function.rs @@ -19,6 +19,8 @@ use super::{IType, IFunctionArg, IValue, WValue}; use super::marine_module::Callable; use crate::MResult; +use marine_wasm_backend_traits::WasmBackend; + use wasmer_it::interpreter::wasm; use wasmer_core::instance::DynFunc; @@ -26,26 +28,26 @@ use wasmer_core::instance::DynFunc; use std::rc::Rc; #[derive(Clone)] -enum WITFunctionInner { +enum WITFunctionInner { Export { func: Rc>, }, Import { // TODO: use dyn Callable here - callable: Rc, + callable: Rc>, }, } /// Represents all import and export functions that could be called from IT context by call-core. #[derive(Clone)] -pub(super) struct WITFunction { +pub(super) struct WITFunction { name: String, arguments: Rc>, outputs: Rc>, - inner: WITFunctionInner, + inner: WITFunctionInner, } -impl WITFunction { +impl WITFunction { /// Creates functions from a "usual" (not IT) module export. pub(super) fn from_export(dyn_func: DynFunc<'static>, name: String) -> MResult { use super::type_converters::wtype_to_itype; @@ -83,7 +85,7 @@ impl WITFunction { /// Creates function from a module import. pub(super) fn from_import( - wit_module: &MModule, + wit_module: &MModule, module_name: &str, function_name: &str, arguments: Rc>, @@ -104,7 +106,7 @@ impl WITFunction { } } -impl wasm::structures::LocalImport for WITFunction { +impl wasm::structures::LocalImport for WITFunction { fn name(&self) -> &str { self.name.as_str() } diff --git a/runtime/src/module/wit_instance.rs b/runtime/src/module/wit_instance.rs index 843ceafa..f50651e5 100644 --- a/runtime/src/module/wit_instance.rs +++ b/runtime/src/module/wit_instance.rs @@ -19,11 +19,16 @@ use super::marine_module::MModule; use super::IRecordType; use crate::MResult; +use marine_wasm_backend_traits::WasmBackend; +use marine_wasm_backend_traits::Module; +use marine_wasm_backend_traits::Instance; + use marine_it_interfaces::MITInterfaces; use marine_it_interfaces::ITAstType; + use wasmer_it::interpreter::wasm; use wasmer_it::interpreter::wasm::structures::{LocalImportIndex, Memory, TypedIndex}; -use wasmer_core::Instance as WasmerInstance; +//use wasmer_core::Instance as WasmerInstance; use std::collections::HashMap; use std::rc::Rc; @@ -32,9 +37,9 @@ pub type MRecordTypes = HashMap>; /// Contains all import and export functions that could be called from IT context by call-core. #[derive(Clone)] -pub(super) struct ITInstance { +pub(super) struct ITInstance { /// IT functions indexed by id. - funcs: HashMap, + funcs: HashMap>, /// IT memories. memories: Vec, @@ -43,12 +48,12 @@ pub(super) struct ITInstance { record_types_by_id: MRecordTypes, } -impl ITInstance { +impl ITInstance { pub(super) fn new( - wasmer_instance: &WasmerInstance, + wasmer_instance: &<::M as Module>::I, module_name: &str, wit: &MITInterfaces<'_>, - modules: &HashMap, + modules: &HashMap>, ) -> MResult { let mut exports = Self::extract_raw_exports(wasmer_instance, wit)?; let imports = Self::extract_imports(module_name, modules, wit, exports.len())?; @@ -66,13 +71,13 @@ impl ITInstance { }) } - fn extract_raw_exports( - wasmer_instance: &WasmerInstance, + fn extract_raw_exports( + wasmer_instance: &I, it: &MITInterfaces<'_>, - ) -> MResult> { + ) -> MResult>> { use wasmer_core::DynFunc; - let module_exports = &wasmer_instance.exports; + let module_exports = &wasmer_instance.exports(); it.exports() .enumerate() @@ -95,10 +100,10 @@ impl ITInstance { /// Extracts only those imports that don't have implementations. fn extract_imports( module_name: &str, - modules: &HashMap, + modules: &HashMap>, wit: &MITInterfaces<'_>, start_index: usize, - ) -> MResult> { + ) -> MResult>> { wit.imports() .filter(|import| // filter out imports that have implementations @@ -136,11 +141,11 @@ impl ITInstance { .collect::>>() } - fn extract_memories(wasmer_instance: &WasmerInstance) -> Vec { + fn extract_memories(wasmer_instance: &I) -> Vec { use wasmer_core::export::Export::Memory; let mut memories = wasmer_instance - .exports() + .export_iter() .filter_map(|(_, export)| match export { Memory(memory) => Some(WITMemory(memory)), _ => None, @@ -148,7 +153,7 @@ impl ITInstance { .collect::>(); if let Some(Memory(memory)) = wasmer_instance - .import_object + .import_object() .maybe_with_namespace("env", |env| env.get_export("memory")) { memories.push(WITMemory(memory)); @@ -175,15 +180,15 @@ impl ITInstance { } } -impl<'v> wasm::structures::Instance> - for ITInstance +impl<'v, WB: WasmBackend> wasm::structures::Instance, WITMemory, WITMemoryView<'v>> + for ITInstance { fn export(&self, _export_name: &str) -> Option<&ITExport> { // exports aren't used in this version of IT None } - fn local_or_import(&self, index: I) -> Option<&WITFunction> { + fn local_or_import(&self, index: I) -> Option<&WITFunction> { self.funcs.get(&index.index()) } diff --git a/runtime/tests/greeting.rs b/runtime/tests/greeting.rs index 94761416..af960d91 100644 --- a/runtime/tests/greeting.rs +++ b/runtime/tests/greeting.rs @@ -24,9 +24,11 @@ static GREETING_WASM_BYTES: Lazy> = Lazy::new(|| { .expect("../examples/greeting/artifacts/greeting.wasm should presence") }); +use marine_wasmer_backend::WasmerBackend; + #[test] pub fn greeting_basic() { - let mut marine = Marine::new(); + let mut marine = Marine::::new(); marine .load_module("greeting", &*GREETING_WASM_BYTES, <_>::default()) .unwrap_or_else(|e| panic!("can't load a module into Marine: {:?}", e)); @@ -50,7 +52,7 @@ pub fn greeting_basic() { #[test] // test loading module with the same name twice pub fn non_unique_module_name() { - let mut marine = Marine::new(); + let mut marine = Marine::::new(); let module_name = String::from("greeting"); marine .load_module(&module_name, &*GREETING_WASM_BYTES, <_>::default()) @@ -68,7 +70,7 @@ pub fn non_unique_module_name() { #[allow(unused_variables)] // test calling Marine with non-exist module and function names pub fn non_exist_module_func() { - let mut marine = Marine::new(); + let mut marine = Marine::::new(); marine .load_module("greeting", &*GREETING_WASM_BYTES, <_>::default()) .unwrap_or_else(|e| panic!("can't load a module into Marine: {:?}", e)); diff --git a/runtime/tests/records.rs b/runtime/tests/records.rs index 8307982b..da948038 100644 --- a/runtime/tests/records.rs +++ b/runtime/tests/records.rs @@ -17,6 +17,8 @@ use marine::Marine; use marine::IValue; +use marine_wasmer_backend::WasmerBackend; + #[test] pub fn records() { let effector_wasm_bytes = std::fs::read("../examples/records/artifacts/records_effector.wasm") @@ -25,7 +27,7 @@ pub fn records() { let pure_wasm_bytes = std::fs::read("../examples/records/artifacts/records_pure.wasm") .expect("../examples/records/artifacts/records_pure.wasm should presence"); - let mut marine = Marine::new(); + let mut marine = Marine::::new(); let load_result = marine.load_module("pure", &pure_wasm_bytes, <_>::default()); assert!(load_result.is_err()); diff --git a/runtime/tests/redis_sqlite.rs b/runtime/tests/redis_sqlite.rs index 648970c5..b70435f3 100644 --- a/runtime/tests/redis_sqlite.rs +++ b/runtime/tests/redis_sqlite.rs @@ -16,6 +16,7 @@ use marine::Marine; use marine::IValue; +use marine_wasmer_backend::WasmerBackend; const REDIS_DOWNLOAD_URL: &str = "https://github.com/fluencelabs/redis/releases/download/v0.14.0_w/redis.wasm"; @@ -35,7 +36,7 @@ pub async fn download(url: &str) -> bytes::Bytes { async fn redis() { let wasm_bytes = download(REDIS_DOWNLOAD_URL).await; - let mut marine = Marine::new(); + let mut marine = Marine::::new(); let module_name = "redis"; let config = <_>::default(); @@ -95,7 +96,7 @@ async fn redis() { async fn sqlite() { let wasm_bytes = download(SQLITE_DOWNLOAD_URL).await; - let mut marine = Marine::new(); + let mut marine = Marine::::new(); let module_name = "sqlite"; let config = <_>::default(); diff --git a/web-runtime/src/engine.rs b/web-runtime/src/engine.rs index f2478afe..ecda49a3 100644 --- a/web-runtime/src/engine.rs +++ b/web-runtime/src/engine.rs @@ -68,13 +68,13 @@ impl Marine { pub fn load_module>( &mut self, name: S, - wit_section_bytes: &[u8], + wasm_bytes: &[u8], ) -> MResult<()> { - self.load_module_(name.into(), wit_section_bytes) + self.load_module_(name.into(), wasm_bytes) } - fn load_module_(&mut self, name: String, wit_section_bytes: &[u8]) -> MResult<()> { - let module = MModule::new(&name, wit_section_bytes)?; + fn load_module_(&mut self, name: String, wasm_bytes: &[u8]) -> MResult<()> { + let module = MModule::new(&name, wasm_bytes)?; match self.modules.entry(name) { Entry::Vacant(entry) => { diff --git a/web-runtime/src/module/marine_module.rs b/web-runtime/src/module/marine_module.rs index 79829df3..62430f26 100644 --- a/web-runtime/src/module/marine_module.rs +++ b/web-runtime/src/module/marine_module.rs @@ -86,8 +86,11 @@ pub(crate) fn extract_it_from_bytes(wit_section_bytes: &[u8]) -> Result MResult { - let it = extract_it_from_bytes(wit_section_bytes)?; + pub(crate) fn new(name: &str, wasm_bytes: &[u8]) -> MResult { + // TODO: compile module + // TODO: extract it section + // TODO: extract sdk version + let it = extract_it_from_bytes(wasm_bytes)?; crate::misc::check_it_version(name, &it.version)?; let mit = MITInterfaces::new(it);