mirror of
https://github.com/fluencelabs/marine.git
synced 2024-12-12 14:55:32 +00:00
Merge remote-tracking branch 'origin/master' into readme-docs
This commit is contained in:
commit
7e81c84580
@ -54,12 +54,12 @@ jobs:
|
|||||||
cd examples/ipfs_node/wasm/ipfs_node
|
cd examples/ipfs_node/wasm/ipfs_node
|
||||||
cargo fmt --all -- --check --color always
|
cargo fmt --all -- --check --color always
|
||||||
cargo wasi build
|
cargo wasi build
|
||||||
cargo clippy -v
|
cargo clippy -v --target wasm32-wasi
|
||||||
|
|
||||||
cd ../ipfs_rpc
|
cd ../ipfs_rpc
|
||||||
cargo fmt --all -- --check --color always
|
cargo fmt --all -- --check --color always
|
||||||
cargo wasi build
|
cargo wasi build
|
||||||
cargo clippy -v
|
cargo clippy -v --target wasm32-wasi
|
||||||
|
|
||||||
# cd ../../../../tools/wit_embedder
|
# cd ../../../../tools/wit_embedder
|
||||||
# cargo fmt --all -- --check --color always
|
# cargo fmt --all -- --check --color always
|
||||||
|
@ -26,8 +26,15 @@ use fluence_sdk_wit::ParsedType;
|
|||||||
use wasmer_wit::interpreter::Instruction;
|
use wasmer_wit::interpreter::Instruction;
|
||||||
use crate::instructions_generator::utils::wtype_to_itype;
|
use crate::instructions_generator::utils::wtype_to_itype;
|
||||||
|
|
||||||
|
const HOST_NAMESPACE_NAME: &str = "host";
|
||||||
|
|
||||||
impl WITGenerator for AstExternModItem {
|
impl WITGenerator for AstExternModItem {
|
||||||
fn generate_wit<'a>(&'a self, interfaces: &mut Interfaces<'a>) {
|
fn generate_wit<'a>(&'a self, interfaces: &mut Interfaces<'a>) {
|
||||||
|
// host imports should be left as is
|
||||||
|
if self.namespace == HOST_NAMESPACE_NAME {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for import in &self.imports {
|
for import in &self.imports {
|
||||||
generate_wit_for_import(import, &self.namespace, interfaces);
|
generate_wit_for_import(import, &self.namespace, interfaces);
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ pub(crate) fn ptype_to_itype(pty: &ParsedType) -> IType {
|
|||||||
ParsedType::F64 => IType::F64,
|
ParsedType::F64 => IType::F64,
|
||||||
ParsedType::Boolean => IType::I32,
|
ParsedType::Boolean => IType::I32,
|
||||||
ParsedType::Utf8String => IType::String,
|
ParsedType::Utf8String => IType::String,
|
||||||
ParsedType::ByteVector => IType::String,
|
ParsedType::ByteVector => IType::ByteArray,
|
||||||
ParsedType::Record(_) => unimplemented!(),
|
ParsedType::Record(_) => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,29 +47,44 @@ impl FCE {
|
|||||||
module_name: MN,
|
module_name: MN,
|
||||||
func_name: FN,
|
func_name: FN,
|
||||||
argument: &[IValue],
|
argument: &[IValue],
|
||||||
) -> Result<Vec<IValue>, FCEError> {
|
) -> Result<Vec<IValue>> {
|
||||||
match self.modules.get_mut(module_name.as_ref()) {
|
self.call_(module_name.as_ref(), func_name.as_ref(), argument)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_(
|
||||||
|
&mut self,
|
||||||
|
module_name: &str,
|
||||||
|
func_name: &str,
|
||||||
|
argument: &[IValue],
|
||||||
|
) -> Result<Vec<IValue>> {
|
||||||
|
match self.modules.get_mut(module_name) {
|
||||||
// TODO: refactor errors
|
// TODO: refactor errors
|
||||||
Some(module) => module.call(func_name.as_ref(), argument),
|
Some(module) => module.call(func_name.as_ref(), argument),
|
||||||
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())),
|
None => Err(FCEError::NoSuchModule(module_name.to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load a new module inside FCE.
|
/// Load a new module inside FCE.
|
||||||
pub fn load_module<S>(
|
pub fn load_module<S: Into<String>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module_name: S,
|
module_name: S,
|
||||||
wasm_bytes: &[u8],
|
wasm_bytes: &[u8],
|
||||||
config: FCEModuleConfig,
|
config: FCEModuleConfig,
|
||||||
) -> Result<(), FCEError>
|
) -> Result<()> {
|
||||||
where
|
self.load_module_(module_name.into(), wasm_bytes, config)
|
||||||
S: Into<String>,
|
}
|
||||||
{
|
|
||||||
|
pub fn load_module_(
|
||||||
|
&mut self,
|
||||||
|
module_name: String,
|
||||||
|
wasm_bytes: &[u8],
|
||||||
|
config: FCEModuleConfig,
|
||||||
|
) -> Result<()> {
|
||||||
let _prepared_wasm_bytes = crate::misc::prepare_module(wasm_bytes, config.mem_pages_count)?;
|
let _prepared_wasm_bytes = crate::misc::prepare_module(wasm_bytes, config.mem_pages_count)?;
|
||||||
|
|
||||||
let module = FCEModule::new(&wasm_bytes, config, &self.modules)?;
|
let module = FCEModule::new(&wasm_bytes, config, &self.modules)?;
|
||||||
|
|
||||||
match self.modules.entry(module_name.into()) {
|
match self.modules.entry(module_name) {
|
||||||
Entry::Vacant(entry) => {
|
Entry::Vacant(entry) => {
|
||||||
entry.insert(module);
|
entry.insert(module);
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -79,10 +94,14 @@ impl FCE {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Unload previously loaded module.
|
/// Unload previously loaded module.
|
||||||
pub fn unload_module<S: AsRef<str>>(&mut self, module_name: S) -> Result<(), FCEError> {
|
pub fn unload_module<S: AsRef<str>>(&mut self, module_name: S) -> Result<()> {
|
||||||
match self.modules.remove(module_name.as_ref()) {
|
self.unload_module_(module_name.as_ref())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unload_module_(&mut self, module_name: &str) -> Result<()> {
|
||||||
|
match self.modules.remove(module_name) {
|
||||||
Some(_) => Ok(()),
|
Some(_) => Ok(()),
|
||||||
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())),
|
None => Err(FCEError::NoSuchModule(module_name.to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +119,7 @@ impl FCE {
|
|||||||
pub fn module_interface<S: AsRef<str>>(
|
pub fn module_interface<S: AsRef<str>>(
|
||||||
&self,
|
&self,
|
||||||
module_name: S,
|
module_name: S,
|
||||||
) -> Result<Vec<FCEFunctionSignature<'_>>, FCEError> {
|
) -> Result<Vec<FCEFunctionSignature<'_>>> {
|
||||||
match self.modules.get(module_name.as_ref()) {
|
match self.modules.get(module_name.as_ref()) {
|
||||||
Some(module) => Ok(Self::get_module_function_signatures(module)),
|
Some(module) => Ok(Self::get_module_function_signatures(module)),
|
||||||
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())),
|
None => Err(FCEError::NoSuchModule(module_name.as_ref().to_string())),
|
||||||
|
@ -39,4 +39,7 @@ pub use engine::FCEFunctionSignature;
|
|||||||
pub use errors::FCEError;
|
pub use errors::FCEError;
|
||||||
pub use module::IValue;
|
pub use module::IValue;
|
||||||
pub use module::IType;
|
pub use module::IType;
|
||||||
pub use module::{to_interface_value, from_interface_values};
|
pub use module::from_interface_values;
|
||||||
|
pub use module::to_interface_value;
|
||||||
|
|
||||||
|
pub(crate) type Result<T> = std::result::Result<T, FCEError>;
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
// https://github.com/paritytech/substrate/blob/master/srml/contracts/src/wasm/prepare.rs
|
// https://github.com/paritytech/substrate/blob/master/srml/contracts/src/wasm/prepare.rs
|
||||||
// https://github.com/nearprotocol/nearcore/blob/master/runtime/near-vm-runner/src/prepare.rs
|
// https://github.com/nearprotocol/nearcore/blob/master/runtime/near-vm-runner/src/prepare.rs
|
||||||
|
|
||||||
use crate::FCEError;
|
use crate::Result;
|
||||||
|
|
||||||
use parity_wasm::{
|
use parity_wasm::{
|
||||||
builder, elements,
|
builder, elements,
|
||||||
@ -30,7 +30,7 @@ struct ModuleBootstrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ModuleBootstrapper {
|
impl<'a> ModuleBootstrapper {
|
||||||
fn init(module_code: &[u8]) -> Result<Self, FCEError> {
|
fn init(module_code: &[u8]) -> Result<Self> {
|
||||||
let module = elements::deserialize_buffer(module_code)?;
|
let module = elements::deserialize_buffer(module_code)?;
|
||||||
|
|
||||||
Ok(Self { module })
|
Ok(Self { module })
|
||||||
@ -66,14 +66,14 @@ impl<'a> ModuleBootstrapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn into_wasm(self) -> Result<Vec<u8>, FCEError> {
|
fn into_wasm(self) -> Result<Vec<u8>> {
|
||||||
elements::serialize(self.module).map_err(Into::into)
|
elements::serialize(self.module).map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prepares a Wasm module:
|
/// Prepares a Wasm module:
|
||||||
/// - set memory page count
|
/// - set memory page count
|
||||||
pub(crate) fn prepare_module(module: &[u8], mem_pages_count: u32) -> Result<Vec<u8>, FCEError> {
|
pub(crate) fn prepare_module(module: &[u8], mem_pages_count: u32) -> Result<Vec<u8>> {
|
||||||
ModuleBootstrapper::init(module)?
|
ModuleBootstrapper::init(module)?
|
||||||
.set_mem_pages_count(mem_pages_count)
|
.set_mem_pages_count(mem_pages_count)
|
||||||
.into_wasm()
|
.into_wasm()
|
||||||
|
@ -14,7 +14,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use super::{IValue, IType};
|
use super::IValue;
|
||||||
|
use super::IType;
|
||||||
use wasmer_wit::interpreter::wasm;
|
use wasmer_wit::interpreter::wasm;
|
||||||
|
|
||||||
// In current implementation export simply does nothing, because there is no more
|
// In current implementation export simply does nothing, because there is no more
|
||||||
|
@ -16,13 +16,15 @@
|
|||||||
|
|
||||||
use super::wit_prelude::*;
|
use super::wit_prelude::*;
|
||||||
use super::{IType, IValue, WValue};
|
use super::{IType, IValue, WValue};
|
||||||
|
use crate::Result;
|
||||||
use crate::FCEModuleConfig;
|
use crate::FCEModuleConfig;
|
||||||
|
|
||||||
use fce_wit_interfaces::FCEWITInterfaces;
|
use fce_wit_interfaces::FCEWITInterfaces;
|
||||||
use wasmer_wit::interpreter::Interpreter;
|
|
||||||
use wasmer_runtime::{compile, ImportObject};
|
|
||||||
use wasmer_core::Instance as WasmerInstance;
|
use wasmer_core::Instance as WasmerInstance;
|
||||||
use wasmer_core::import::Namespace;
|
use wasmer_core::import::Namespace;
|
||||||
|
use wasmer_runtime::compile;
|
||||||
|
use wasmer_runtime::ImportObject;
|
||||||
|
use wasmer_wit::interpreter::Interpreter;
|
||||||
use wit_parser::extract_wit;
|
use wit_parser::extract_wit;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@ -47,7 +49,7 @@ pub(super) struct Callable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Callable {
|
impl Callable {
|
||||||
pub fn call(&mut self, args: &[IValue]) -> Result<Vec<IValue>, FCEError> {
|
pub fn call(&mut self, args: &[IValue]) -> Result<Vec<IValue>> {
|
||||||
use wasmer_wit::interpreter::stack::Stackable;
|
use wasmer_wit::interpreter::stack::Stackable;
|
||||||
|
|
||||||
let result = self
|
let result = self
|
||||||
@ -86,7 +88,7 @@ impl FCEModule {
|
|||||||
wasm_bytes: &[u8],
|
wasm_bytes: &[u8],
|
||||||
fce_module_config: FCEModuleConfig,
|
fce_module_config: FCEModuleConfig,
|
||||||
modules: &HashMap<String, FCEModule>,
|
modules: &HashMap<String, FCEModule>,
|
||||||
) -> Result<Self, FCEError> {
|
) -> Result<Self> {
|
||||||
let wasmer_module = compile(&wasm_bytes)?;
|
let wasmer_module = compile(&wasm_bytes)?;
|
||||||
let wit = extract_wit(&wasmer_module)?;
|
let wit = extract_wit(&wasmer_module)?;
|
||||||
let fce_wit = FCEWITInterfaces::new(wit);
|
let fce_wit = FCEWITInterfaces::new(wit);
|
||||||
@ -130,11 +132,7 @@ impl FCEModule {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn call(
|
pub(crate) fn call(&mut self, function_name: &str, args: &[IValue]) -> Result<Vec<IValue>> {
|
||||||
&mut self,
|
|
||||||
function_name: &str,
|
|
||||||
args: &[IValue],
|
|
||||||
) -> Result<Vec<IValue>, FCEError> {
|
|
||||||
match self.exports_funcs.get_mut(function_name) {
|
match self.exports_funcs.get_mut(function_name) {
|
||||||
Some(func) => Arc::make_mut(func).call(args),
|
Some(func) => Arc::make_mut(func).call(args),
|
||||||
None => Err(FCEError::NoSuchFunction(format!(
|
None => Err(FCEError::NoSuchFunction(format!(
|
||||||
@ -157,7 +155,7 @@ impl FCEModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: change the cloning Callable behaviour after changes of Wasmer API
|
// TODO: change the cloning Callable behaviour after changes of Wasmer API
|
||||||
pub(super) fn get_callable(&self, function_name: &str) -> Result<Arc<Callable>, FCEError> {
|
pub(super) fn get_callable(&self, function_name: &str) -> Result<Arc<Callable>> {
|
||||||
match self.exports_funcs.get(function_name) {
|
match self.exports_funcs.get(function_name) {
|
||||||
Some(func) => Ok(func.clone()),
|
Some(func) => Ok(func.clone()),
|
||||||
None => Err(FCEError::NoSuchFunction(format!(
|
None => Err(FCEError::NoSuchFunction(format!(
|
||||||
@ -170,7 +168,7 @@ impl FCEModule {
|
|||||||
fn instantiate_wit_exports(
|
fn instantiate_wit_exports(
|
||||||
wit_instance: Arc<WITInstance>,
|
wit_instance: Arc<WITInstance>,
|
||||||
wit: &FCEWITInterfaces<'_>,
|
wit: &FCEWITInterfaces<'_>,
|
||||||
) -> Result<HashMap<String, Arc<Callable>>, FCEError> {
|
) -> Result<HashMap<String, Arc<Callable>>> {
|
||||||
use fce_wit_interfaces::WITAstType;
|
use fce_wit_interfaces::WITAstType;
|
||||||
|
|
||||||
wit.implementations()
|
wit.implementations()
|
||||||
@ -218,14 +216,14 @@ impl FCEModule {
|
|||||||
))),
|
))),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Result<HashMap<String, Arc<Callable>>, FCEError>>()
|
.collect::<Result<HashMap<String, Arc<Callable>>>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
// this function deals only with import functions that have an adaptor implementation
|
// this function deals only with import functions that have an adaptor implementation
|
||||||
fn adjust_wit_imports(
|
fn adjust_wit_imports(
|
||||||
wit: &FCEWITInterfaces<'_>,
|
wit: &FCEWITInterfaces<'_>,
|
||||||
wit_instance: Arc<MaybeUninit<WITInstance>>,
|
wit_instance: Arc<MaybeUninit<WITInstance>>,
|
||||||
) -> Result<ImportObject, FCEError> {
|
) -> Result<ImportObject> {
|
||||||
use fce_wit_interfaces::WITAstType;
|
use fce_wit_interfaces::WITAstType;
|
||||||
use wasmer_core::typed_func::DynamicFunc;
|
use wasmer_core::typed_func::DynamicFunc;
|
||||||
use wasmer_core::vm::Ctx;
|
use wasmer_core::vm::Ctx;
|
||||||
@ -327,7 +325,7 @@ impl FCEModule {
|
|||||||
))),
|
))),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Result<multimap::MultiMap<_, _>, FCEError>>()?;
|
.collect::<Result<multimap::MultiMap<_, _>>>()?;
|
||||||
|
|
||||||
let mut import_object = ImportObject::new();
|
let mut import_object = ImportObject::new();
|
||||||
|
|
||||||
|
@ -21,11 +21,12 @@ mod wit_instance;
|
|||||||
mod type_converters;
|
mod type_converters;
|
||||||
mod fce_module;
|
mod fce_module;
|
||||||
|
|
||||||
pub(crate) use fce_module::FCEModule;
|
|
||||||
|
|
||||||
pub use wasmer_wit::types::InterfaceType as IType;
|
pub use wasmer_wit::types::InterfaceType as IType;
|
||||||
pub use wasmer_wit::values::InterfaceValue as IValue;
|
pub use wasmer_wit::values::InterfaceValue as IValue;
|
||||||
pub use wasmer_wit::values::{to_interface_value, from_interface_values};
|
pub use wasmer_wit::values::from_interface_values;
|
||||||
|
pub use wasmer_wit::values::to_interface_value;
|
||||||
|
|
||||||
|
pub(crate) use fce_module::FCEModule;
|
||||||
pub(self) use wasmer_core::types::Type as WType;
|
pub(self) use wasmer_core::types::Type as WType;
|
||||||
pub(self) use wasmer_core::types::Value as WValue;
|
pub(self) use wasmer_core::types::Value as WValue;
|
||||||
|
|
||||||
|
@ -14,10 +14,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use super::wit_prelude::FCEError;
|
|
||||||
use super::fce_module::FCEModule;
|
use super::fce_module::FCEModule;
|
||||||
use super::{IType, IValue, WValue};
|
use super::{IType, IValue, WValue};
|
||||||
use super::fce_module::Callable;
|
use super::fce_module::Callable;
|
||||||
|
use crate::Result;
|
||||||
|
|
||||||
use wasmer_wit::interpreter::wasm;
|
use wasmer_wit::interpreter::wasm;
|
||||||
use wasmer_core::instance::DynFunc;
|
use wasmer_core::instance::DynFunc;
|
||||||
@ -45,7 +45,7 @@ pub(super) struct WITFunction {
|
|||||||
|
|
||||||
impl WITFunction {
|
impl WITFunction {
|
||||||
/// Creates functions from a "usual" (not WIT) module export.
|
/// Creates functions from a "usual" (not WIT) module export.
|
||||||
pub(super) fn from_export(dyn_func: DynFunc<'static>) -> Result<Self, FCEError> {
|
pub(super) fn from_export(dyn_func: DynFunc<'static>) -> Result<Self> {
|
||||||
use super::type_converters::wtype_to_itype;
|
use super::type_converters::wtype_to_itype;
|
||||||
|
|
||||||
let signature = dyn_func.signature();
|
let signature = dyn_func.signature();
|
||||||
@ -70,10 +70,7 @@ impl WITFunction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates function from a module import.
|
/// Creates function from a module import.
|
||||||
pub(super) fn from_import(
|
pub(super) fn from_import(wit_module: &FCEModule, function_name: &str) -> Result<Self> {
|
||||||
wit_module: &FCEModule,
|
|
||||||
function_name: &str,
|
|
||||||
) -> Result<Self, FCEError> {
|
|
||||||
let callable = wit_module.get_callable(function_name)?;
|
let callable = wit_module.get_callable(function_name)?;
|
||||||
|
|
||||||
let inner = WITFunctionInner::Import { callable };
|
let inner = WITFunctionInner::Import { callable };
|
||||||
@ -111,7 +108,7 @@ impl wasm::structures::LocalImport for WITFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&self, arguments: &[IValue]) -> Result<Vec<IValue>, ()> {
|
fn call(&self, arguments: &[IValue]) -> std::result::Result<Vec<IValue>, ()> {
|
||||||
use super::type_converters::{ival_to_wval, wval_to_ival};
|
use super::type_converters::{ival_to_wval, wval_to_ival};
|
||||||
|
|
||||||
match &self.inner {
|
match &self.inner {
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
use super::wit_prelude::*;
|
use super::wit_prelude::*;
|
||||||
use super::fce_module::FCEModule;
|
use super::fce_module::FCEModule;
|
||||||
|
use crate::Result;
|
||||||
|
|
||||||
use fce_wit_interfaces::FCEWITInterfaces;
|
use fce_wit_interfaces::FCEWITInterfaces;
|
||||||
use fce_wit_interfaces::WITAstType;
|
use fce_wit_interfaces::WITAstType;
|
||||||
@ -37,7 +38,7 @@ impl WITInstance {
|
|||||||
wasmer_instance: &WasmerInstance,
|
wasmer_instance: &WasmerInstance,
|
||||||
wit: &FCEWITInterfaces<'_>,
|
wit: &FCEWITInterfaces<'_>,
|
||||||
modules: &HashMap<String, FCEModule>,
|
modules: &HashMap<String, FCEModule>,
|
||||||
) -> Result<Self, FCEError> {
|
) -> Result<Self> {
|
||||||
let mut exports = Self::extract_raw_exports(&wasmer_instance, wit)?;
|
let mut exports = Self::extract_raw_exports(&wasmer_instance, wit)?;
|
||||||
let imports = Self::extract_imports(modules, wit, exports.len())?;
|
let imports = Self::extract_imports(modules, wit, exports.len())?;
|
||||||
let memories = Self::extract_memories(&wasmer_instance);
|
let memories = Self::extract_memories(&wasmer_instance);
|
||||||
@ -51,7 +52,7 @@ impl WITInstance {
|
|||||||
fn extract_raw_exports(
|
fn extract_raw_exports(
|
||||||
wasmer_instance: &WasmerInstance,
|
wasmer_instance: &WasmerInstance,
|
||||||
wit: &FCEWITInterfaces<'_>,
|
wit: &FCEWITInterfaces<'_>,
|
||||||
) -> Result<HashMap<usize, WITFunction>, FCEError> {
|
) -> Result<HashMap<usize, WITFunction>> {
|
||||||
use wasmer_core::DynFunc;
|
use wasmer_core::DynFunc;
|
||||||
|
|
||||||
let module_exports = &wasmer_instance.exports;
|
let module_exports = &wasmer_instance.exports;
|
||||||
@ -76,7 +77,7 @@ impl WITInstance {
|
|||||||
modules: &HashMap<String, FCEModule>,
|
modules: &HashMap<String, FCEModule>,
|
||||||
wit: &FCEWITInterfaces<'_>,
|
wit: &FCEWITInterfaces<'_>,
|
||||||
start_index: usize,
|
start_index: usize,
|
||||||
) -> Result<HashMap<usize, WITFunction>, FCEError> {
|
) -> Result<HashMap<usize, WITFunction>> {
|
||||||
wit.imports()
|
wit.imports()
|
||||||
.filter(|import|
|
.filter(|import|
|
||||||
// filter out imports that have implementations
|
// filter out imports that have implementations
|
||||||
@ -89,7 +90,7 @@ impl WITInstance {
|
|||||||
}
|
}
|
||||||
None => Err(FCEError::NoSuchModule(import.namespace.to_string())),
|
None => Err(FCEError::NoSuchModule(import.namespace.to_string())),
|
||||||
})
|
})
|
||||||
.collect::<Result<HashMap<_, _>, _>>()
|
.collect::<Result<HashMap<_, _>>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract_memories(wasmer_instance: &WasmerInstance) -> Vec<WITMemory> {
|
fn extract_memories(wasmer_instance: &WasmerInstance) -> Vec<WITMemory> {
|
||||||
|
Binary file not shown.
Binary file not shown.
@ -39,7 +39,7 @@ pub fn put(file_path: String) -> String {
|
|||||||
let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string());
|
let timeout = std::env::var(TIMEOUT_ENV_NAME).unwrap_or_else(|_| "1s".to_string());
|
||||||
let cmd = format!("add --timeout {} -Q {}", timeout, file_path);
|
let cmd = format!("add --timeout {} -Q {}", timeout, file_path);
|
||||||
|
|
||||||
call_ipfs(cmd)
|
ipfs(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get file by provided hash from IPFS, saves it to a temporary file and returns a path to it.
|
/// Get file by provided hash from IPFS, saves it to a temporary file and returns a path to it.
|
||||||
@ -55,7 +55,7 @@ pub fn get(hash: String) -> String {
|
|||||||
timeout, result_file_path, hash
|
timeout, result_file_path, hash
|
||||||
);
|
);
|
||||||
|
|
||||||
call_ipfs(cmd);
|
ipfs(cmd);
|
||||||
|
|
||||||
RESULT_FILE_PATH.to_string()
|
RESULT_FILE_PATH.to_string()
|
||||||
}
|
}
|
||||||
@ -71,22 +71,9 @@ pub fn get_address() -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call_ipfs(cmd: String) -> String {
|
#[fce]
|
||||||
unsafe {
|
|
||||||
// TODO: better error handling
|
|
||||||
match ipfs(cmd.as_ptr() as _, cmd.len() as _) {
|
|
||||||
0 => String::from_raw_parts(
|
|
||||||
fluence::internal::get_result_ptr() as _,
|
|
||||||
fluence::internal::get_result_size(),
|
|
||||||
fluence::internal::get_result_size(),
|
|
||||||
),
|
|
||||||
_ => "host ipfs call failed".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[link(wasm_import_module = "host")]
|
#[link(wasm_import_module = "host")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
/// Put a file to ipfs, returns ipfs hash of the file.
|
/// Execute provided cmd as a parameters of ipfs cli, return result.
|
||||||
fn ipfs(ptr: i32, size: i32) -> i32;
|
pub fn ipfs(cmd: String) -> String;
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,16 @@ impl FluenceFaaS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Executes provided Wasm code in the internal environment (with access to module exports).
|
/// Executes provided Wasm code in the internal environment (with access to module exports).
|
||||||
pub fn call_code(
|
pub fn call_code<S: AsRef<str>>(
|
||||||
|
&mut self,
|
||||||
|
wasm: &[u8],
|
||||||
|
func_name: S,
|
||||||
|
args: &[IValue],
|
||||||
|
) -> Result<Vec<IValue>> {
|
||||||
|
self.call_code_(wasm, func_name.as_ref(), args)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_code_(
|
||||||
&mut self,
|
&mut self,
|
||||||
wasm: &[u8],
|
wasm: &[u8],
|
||||||
func_name: &str,
|
func_name: &str,
|
||||||
@ -125,10 +134,10 @@ impl FluenceFaaS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Call a specified function of loaded on a startup module by its name.
|
/// Call a specified function of loaded on a startup module by its name.
|
||||||
pub fn call_module(
|
pub fn call_module<MN: AsRef<str>, FN: AsRef<str>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
module_name: &str,
|
module_name: MN,
|
||||||
func_name: &str,
|
func_name: FN,
|
||||||
args: &[IValue],
|
args: &[IValue],
|
||||||
) -> Result<Vec<IValue>> {
|
) -> Result<Vec<IValue>> {
|
||||||
self.fce
|
self.fce
|
||||||
|
@ -93,7 +93,7 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
DynamicFunc::new(
|
DynamicFunc::new(
|
||||||
std::sync::Arc::new(FuncSig::new(vec![Type::I32, Type::I32], vec![Type::I32])),
|
std::sync::Arc::new(FuncSig::new(vec![Type::I32, Type::I32], vec![])),
|
||||||
func,
|
func,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -14,20 +14,19 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use wasmer_core::vm::Ctx;
|
|
||||||
use crate::FaaSError;
|
|
||||||
use super::config::ModuleConfig;
|
use super::config::ModuleConfig;
|
||||||
|
|
||||||
use fce::FCEModuleConfig;
|
use fce::FCEModuleConfig;
|
||||||
|
|
||||||
|
use wasmer_core::backend::SigRegistry;
|
||||||
|
use wasmer_core::module::ExportIndex;
|
||||||
|
use wasmer_core::vm::Ctx;
|
||||||
use wasmer_core::import::ImportObject;
|
use wasmer_core::import::ImportObject;
|
||||||
use wasmer_runtime::func;
|
|
||||||
use wasmer_core::typed_func::WasmTypeList;
|
use wasmer_core::typed_func::WasmTypeList;
|
||||||
|
use wasmer_runtime::func;
|
||||||
use wasmer_runtime::Func;
|
use wasmer_runtime::Func;
|
||||||
use wasmer_runtime::error::ResolveError;
|
use wasmer_runtime::error::ResolveError;
|
||||||
use wasmer_core::backend::SigRegistry;
|
|
||||||
use wasmer_runtime::types::LocalOrImport;
|
use wasmer_runtime::types::LocalOrImport;
|
||||||
use wasmer_core::module::ExportIndex;
|
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
@ -103,7 +102,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Make FCE config based on parsed raw config.
|
/// Make FCE config based on parsed raw config.
|
||||||
pub(crate) fn make_fce_config(config: Option<ModuleConfig>) -> Result<FCEModuleConfig, FaaSError> {
|
pub(crate) fn make_fce_config(config: Option<ModuleConfig>) -> crate::Result<FCEModuleConfig> {
|
||||||
use super::imports::create_host_import_func;
|
use super::imports::create_host_import_func;
|
||||||
use super::imports::log_utf8_string;
|
use super::imports::log_utf8_string;
|
||||||
use wasmer_core::import::Namespace;
|
use wasmer_core::import::Namespace;
|
||||||
|
@ -25,15 +25,15 @@ pub const IN_WASM_PATH: &str = "in-wasm-path";
|
|||||||
pub fn build<'a, 'b>() -> App<'a, 'b> {
|
pub fn build<'a, 'b>() -> App<'a, 'b> {
|
||||||
SubCommand::with_name("build")
|
SubCommand::with_name("build")
|
||||||
.about("build provided Rust project to Wasm")
|
.about("build provided Rust project to Wasm")
|
||||||
.args(&[Arg::with_name(IN_WASM_PATH)
|
.setting(clap::AppSettings::TrailingVarArg)
|
||||||
.takes_value(true)
|
.setting(clap::AppSettings::AllowLeadingHyphen)
|
||||||
.short("i")
|
.arg(Arg::from_usage("[optional]... 'cargo build arguments'").multiple(true))
|
||||||
.help("path to a Cargo.toml file")])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn show_wit<'a, 'b>() -> App<'a, 'b> {
|
pub fn show_wit<'a, 'b>() -> App<'a, 'b> {
|
||||||
SubCommand::with_name("show")
|
SubCommand::with_name("show")
|
||||||
.about("show WIT in provided Wasm file")
|
.about("show WIT in provided Wasm file")
|
||||||
|
.setting(clap::AppSettings::ArgRequiredElseHelp)
|
||||||
.args(&[Arg::with_name(IN_WASM_PATH)
|
.args(&[Arg::with_name(IN_WASM_PATH)
|
||||||
.required(true)
|
.required(true)
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
use crate::Result;
|
use crate::Result;
|
||||||
use crate::errors::CLIError;
|
use crate::errors::CLIError;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
@ -29,15 +28,13 @@ enum DiagnosticMessage {
|
|||||||
RunWithArgs,
|
RunWithArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn build(manifest_path: Option<PathBuf>) -> Result<()> {
|
pub(crate) fn build(trailing_args: Vec<&str>) -> Result<()> {
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
let mut cargo = Command::new("cargo");
|
let mut cargo = Command::new("cargo");
|
||||||
cargo.arg("build").arg("--target").arg("wasm32-wasi");
|
cargo.arg("build").arg("--target").arg("wasm32-wasi");
|
||||||
cargo.arg("--message-format").arg("json-render-diagnostics");
|
cargo.arg("--message-format").arg("json-render-diagnostics");
|
||||||
if let Some(wasm_path) = manifest_path {
|
cargo.args(trailing_args);
|
||||||
cargo.arg("--manifest-path").arg(wasm_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut process = cargo.stdout(std::process::Stdio::piped()).spawn()?;
|
let mut process = cargo.stdout(std::process::Stdio::piped()).spawn()?;
|
||||||
|
|
||||||
|
@ -29,27 +29,22 @@ mod args;
|
|||||||
mod build;
|
mod build;
|
||||||
mod errors;
|
mod errors;
|
||||||
|
|
||||||
use clap::App;
|
|
||||||
use clap::AppSettings;
|
|
||||||
|
|
||||||
pub(crate) type Result<T> = std::result::Result<T, crate::errors::CLIError>;
|
pub(crate) type Result<T> = std::result::Result<T, crate::errors::CLIError>;
|
||||||
|
|
||||||
pub fn main() -> Result<()> {
|
pub fn main() -> Result<()> {
|
||||||
let app = App::new("CLI tool for embedding WIT to provided Wasm file")
|
let app = clap::App::new("CLI tool for embedding WIT to provided Wasm file")
|
||||||
.version(args::VERSION)
|
.version(args::VERSION)
|
||||||
.author(args::AUTHORS)
|
.author(args::AUTHORS)
|
||||||
.about(args::DESCRIPTION)
|
.about(args::DESCRIPTION)
|
||||||
.setting(AppSettings::ArgRequiredElseHelp)
|
|
||||||
.subcommand(args::build())
|
.subcommand(args::build())
|
||||||
.subcommand(args::show_wit());
|
.subcommand(args::show_wit());
|
||||||
|
let arg_matches = app.get_matches();
|
||||||
|
|
||||||
match app.get_matches().subcommand() {
|
match arg_matches.subcommand() {
|
||||||
("build", Some(arg)) => {
|
("build", Some(args)) => {
|
||||||
let manifest_path = arg
|
let trailing_args: Vec<&str> = args.values_of("optional").unwrap_or_default().collect();
|
||||||
.value_of(args::IN_WASM_PATH)
|
|
||||||
.map(std::path::PathBuf::from);
|
|
||||||
|
|
||||||
crate::build::build(manifest_path)
|
crate::build::build(trailing_args)
|
||||||
}
|
}
|
||||||
("show", Some(arg)) => {
|
("show", Some(arg)) => {
|
||||||
let wasm_path = arg.value_of(args::IN_WASM_PATH).unwrap();
|
let wasm_path = arg.value_of(args::IN_WASM_PATH).unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user