marine/engine/src/module/wit_function.rs

141 lines
4.2 KiB
Rust
Raw Normal View History

2020-06-01 02:45:04 +03:00
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-06-02 17:20:00 +03:00
use super::fce_module::FCEModule;
2020-09-16 01:14:15 +03:00
use super::{IType, IFunctionArg, IValue, WValue};
use super::fce_module::Callable;
2020-07-17 23:07:02 +03:00
use crate::Result;
2020-06-02 17:20:00 +03:00
use wasmer_wit::interpreter::wasm;
use wasmer_core::instance::DynFunc;
2020-06-01 02:45:04 +03:00
use std::sync::Arc;
#[derive(Clone)]
enum WITFunctionInner {
Export {
func: Arc<DynFunc<'static>>,
2020-09-16 01:14:15 +03:00
arguments: Vec<IFunctionArg>,
2020-06-02 17:20:00 +03:00
outputs: Vec<IType>,
2020-06-01 02:45:04 +03:00
},
Import {
2020-06-02 17:20:00 +03:00
// TODO: use dyn Callable here
2020-06-07 22:57:30 +03:00
callable: Arc<Callable>,
2020-10-21 22:21:16 +03:00
arguments: Vec<IFunctionArg>,
outputs: Vec<IType>,
2020-06-01 02:45:04 +03:00
},
}
2020-06-02 17:20:00 +03:00
/// Represents all import and export functions that could be called from WIT context by call-core.
2020-06-01 02:45:04 +03:00
#[derive(Clone)]
2020-06-02 17:20:00 +03:00
pub(super) struct WITFunction {
2020-06-01 02:45:04 +03:00
inner: WITFunctionInner,
}
impl WITFunction {
2020-06-02 17:20:00 +03:00
/// Creates functions from a "usual" (not WIT) module export.
2020-07-17 23:07:02 +03:00
pub(super) fn from_export(dyn_func: DynFunc<'static>) -> Result<Self> {
2020-06-02 17:20:00 +03:00
use super::type_converters::wtype_to_itype;
2020-06-01 02:45:04 +03:00
let signature = dyn_func.signature();
2020-09-16 01:14:15 +03:00
let arguments = signature
2020-06-01 02:45:04 +03:00
.params()
.iter()
2020-09-16 01:14:15 +03:00
.map(|wtype| IFunctionArg {
// here it's considered as an anonymous arguments
name: String::new(),
ty: wtype_to_itype(wtype),
})
2020-06-01 02:45:04 +03:00
.collect::<Vec<_>>();
let outputs = signature
.returns()
.iter()
.map(wtype_to_itype)
.collect::<Vec<_>>();
let inner = WITFunctionInner::Export {
func: Arc::new(dyn_func),
2020-09-16 01:14:15 +03:00
arguments,
2020-06-01 02:45:04 +03:00
outputs,
};
Ok(Self { inner })
}
2020-06-02 17:20:00 +03:00
/// Creates function from a module import.
2020-10-21 22:21:16 +03:00
pub(super) fn from_import(
wit_module: &FCEModule,
function_name: &str,
arguments: Vec<IFunctionArg>,
outputs: Vec<IType>,
) -> Result<Self> {
2020-06-10 14:56:15 +03:00
let callable = wit_module.get_callable(function_name)?;
2020-06-01 02:45:04 +03:00
2020-10-21 22:21:16 +03:00
let inner = WITFunctionInner::Import {
callable,
arguments,
outputs,
};
2020-06-01 02:45:04 +03:00
Ok(Self { inner })
}
}
impl wasm::structures::LocalImport for WITFunction {
fn inputs_cardinality(&self) -> usize {
match &self.inner {
2020-09-16 01:14:15 +03:00
WITFunctionInner::Export { arguments, .. } => arguments.len(),
2020-10-21 22:21:16 +03:00
WITFunctionInner::Import { arguments, .. } => arguments.len(),
2020-06-01 02:45:04 +03:00
}
}
fn outputs_cardinality(&self) -> usize {
match &self.inner {
2020-10-21 22:21:16 +03:00
WITFunctionInner::Export { outputs, .. } => outputs.len(),
WITFunctionInner::Import { outputs, .. } => outputs.len(),
2020-06-01 02:45:04 +03:00
}
}
2020-09-16 01:14:15 +03:00
fn arguments(&self) -> &[IFunctionArg] {
2020-06-01 02:45:04 +03:00
match &self.inner {
2020-10-21 22:21:16 +03:00
WITFunctionInner::Export { arguments, .. } => arguments,
WITFunctionInner::Import { arguments, .. } => arguments,
2020-06-01 02:45:04 +03:00
}
}
2020-06-02 17:20:00 +03:00
fn outputs(&self) -> &[IType] {
2020-06-01 02:45:04 +03:00
match &self.inner {
2020-10-21 22:21:16 +03:00
WITFunctionInner::Export { outputs, .. } => outputs,
WITFunctionInner::Import { outputs, .. } => outputs,
2020-06-01 02:45:04 +03:00
}
}
2020-07-17 23:07:02 +03:00
fn call(&self, arguments: &[IValue]) -> std::result::Result<Vec<IValue>, ()> {
2020-06-02 17:20:00 +03:00
use super::type_converters::{ival_to_wval, wval_to_ival};
2020-06-01 02:45:04 +03:00
match &self.inner {
2020-06-02 00:12:23 +03:00
WITFunctionInner::Export { func, .. } => func
.as_ref()
2020-06-02 17:20:00 +03:00
.call(&arguments.iter().map(ival_to_wval).collect::<Vec<WValue>>())
.map(|result| result.iter().map(wval_to_ival).collect())
2020-06-02 00:12:23 +03:00
.map_err(|_| ()),
2020-10-21 22:21:16 +03:00
WITFunctionInner::Import { callable, .. } => Arc::make_mut(&mut callable.clone())
2020-06-10 14:56:15 +03:00
.call(arguments)
.map_err(|_| ()),
2020-06-01 02:45:04 +03:00
}
}
}