marine/engine/src/module/wit_function.rs

142 lines
3.8 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;
2021-02-24 12:25:55 +03:00
use crate::FCEResult;
2020-06-02 17:20:00 +03:00
use wasmer_wit::interpreter::wasm;
use wasmer_core::instance::DynFunc;
2020-11-05 15:18:48 +03:00
// use std::sync::Arc;
use std::rc::Rc;
2020-06-01 02:45:04 +03:00
#[derive(Clone)]
enum WITFunctionInner {
Export {
2020-11-05 15:18:48 +03:00
func: Rc<DynFunc<'static>>,
2020-06-01 02:45:04 +03:00
},
Import {
2020-06-02 17:20:00 +03:00
// TODO: use dyn Callable here
2020-11-05 15:18:48 +03:00
callable: Rc<Callable>,
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-11-07 00:08:09 +03:00
name: String,
arguments: Rc<Vec<IFunctionArg>>,
outputs: Rc<Vec<IType>>,
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.
2021-02-24 12:25:55 +03:00
pub(super) fn from_export(dyn_func: DynFunc<'static>, name: String) -> FCEResult<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 {
2020-11-05 15:18:48 +03:00
func: Rc::new(dyn_func),
2020-06-01 02:45:04 +03:00
};
2020-11-07 00:08:09 +03:00
let arguments = Rc::new(arguments);
let outputs = Rc::new(outputs);
Ok(Self {
name,
arguments,
outputs,
inner,
})
2020-06-01 02:45:04 +03:00
}
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,
2020-11-05 15:18:48 +03:00
arguments: Rc<Vec<IFunctionArg>>,
outputs: Rc<Vec<IType>>,
2021-02-24 12:25:55 +03:00
) -> FCEResult<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-11-07 00:08:09 +03:00
let inner = WITFunctionInner::Import { callable };
let name = function_name.to_string();
Ok(Self {
name,
2020-10-21 22:21:16 +03:00
arguments,
outputs,
2020-11-07 00:08:09 +03:00
inner,
})
2020-06-01 02:45:04 +03:00
}
}
impl wasm::structures::LocalImport for WITFunction {
2020-11-07 00:08:09 +03:00
fn name(&self) -> &str {
self.name.as_str()
}
2020-06-01 02:45:04 +03:00
fn inputs_cardinality(&self) -> usize {
2020-11-07 00:08:09 +03:00
self.arguments.len()
2020-06-01 02:45:04 +03:00
}
fn outputs_cardinality(&self) -> usize {
2020-11-07 00:08:09 +03:00
self.outputs.len()
2020-06-01 02:45:04 +03:00
}
2020-09-16 01:14:15 +03:00
fn arguments(&self) -> &[IFunctionArg] {
2020-11-07 00:08:09 +03:00
&self.arguments
2020-06-01 02:45:04 +03:00
}
2020-06-02 17:20:00 +03:00
fn outputs(&self) -> &[IType] {
2020-11-07 00:08:09 +03:00
&self.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-11-05 15:18:48 +03:00
WITFunctionInner::Import { callable, .. } => Rc::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
}
}
}