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 19:16:15 +03:00
|
|
|
use super::wit_prelude::FCEError;
|
2020-06-02 17:20:00 +03:00
|
|
|
use super::fce_module::FCEModule;
|
|
|
|
use super::{IType, IValue, WValue};
|
2020-06-07 22:57:30 +03:00
|
|
|
use crate::vm::module::fce_module::Callable;
|
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-06-02 17:20:00 +03:00
|
|
|
inputs: Vec<IType>,
|
|
|
|
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-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,
|
|
|
|
}
|
|
|
|
|
2020-06-07 12:33:26 +03:00
|
|
|
impl Drop for WITFunction {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
match &self.inner {
|
|
|
|
WITFunctionInner::Export { func, .. } => {
|
2020-06-07 22:57:30 +03:00
|
|
|
println!("WITFunction export dropped: {:?}", func.signature());
|
2020-06-07 12:33:26 +03:00
|
|
|
}
|
2020-06-07 22:57:30 +03:00
|
|
|
WITFunctionInner::Import { callable } => {
|
|
|
|
println!("WITFunction import dropped: {:?}", callable.wit_module_func.inputs);
|
2020-06-07 12:33:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 02:45:04 +03:00
|
|
|
impl WITFunction {
|
2020-06-02 17:20:00 +03:00
|
|
|
/// Creates functions from a "usual" (not WIT) module export.
|
2020-06-02 19:16:15 +03:00
|
|
|
pub(super) fn from_export(dyn_func: DynFunc<'static>) -> Result<Self, FCEError> {
|
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();
|
|
|
|
let inputs = signature
|
|
|
|
.params()
|
|
|
|
.iter()
|
|
|
|
.map(wtype_to_itype)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let outputs = signature
|
|
|
|
.returns()
|
|
|
|
.iter()
|
|
|
|
.map(wtype_to_itype)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let inner = WITFunctionInner::Export {
|
|
|
|
func: Arc::new(dyn_func),
|
|
|
|
inputs,
|
|
|
|
outputs,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Self { inner })
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:20:00 +03:00
|
|
|
/// Creates function from a module import.
|
2020-06-03 23:08:18 +03:00
|
|
|
pub(super) fn from_import(
|
2020-06-07 22:57:30 +03:00
|
|
|
wit_module: &FCEModule,
|
2020-06-03 23:08:18 +03:00
|
|
|
func_name: String,
|
|
|
|
) -> Result<Self, FCEError> {
|
2020-06-07 22:57:30 +03:00
|
|
|
let callable = wit_module.exports_funcs.get(&func_name).unwrap().clone();
|
2020-06-01 02:45:04 +03:00
|
|
|
|
|
|
|
let inner = WITFunctionInner::Import {
|
2020-06-07 22:57:30 +03:00
|
|
|
callable
|
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 {
|
|
|
|
WITFunctionInner::Export { ref inputs, .. } => inputs.len(),
|
2020-06-07 22:57:30 +03:00
|
|
|
WITFunctionInner::Import { ref callable, .. } => callable.wit_module_func.inputs.len(),
|
2020-06-01 02:45:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn outputs_cardinality(&self) -> usize {
|
|
|
|
match &self.inner {
|
|
|
|
WITFunctionInner::Export { ref outputs, .. } => outputs.len(),
|
2020-06-07 22:57:30 +03:00
|
|
|
WITFunctionInner::Import { ref callable, .. } => callable.wit_module_func.outputs.len(),
|
2020-06-01 02:45:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:20:00 +03:00
|
|
|
fn inputs(&self) -> &[IType] {
|
2020-06-01 02:45:04 +03:00
|
|
|
match &self.inner {
|
|
|
|
WITFunctionInner::Export { ref inputs, .. } => inputs,
|
2020-06-07 22:57:30 +03:00
|
|
|
WITFunctionInner::Import { ref callable, .. } => &callable.wit_module_func.inputs,
|
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 {
|
|
|
|
WITFunctionInner::Export { ref outputs, .. } => outputs,
|
2020-06-07 22:57:30 +03:00
|
|
|
WITFunctionInner::Import { ref callable, .. } => &callable.wit_module_func.outputs,
|
2020-06-01 02:45:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:20:00 +03:00
|
|
|
fn call(&self, arguments: &[IValue]) -> Result<Vec<IValue>, ()> {
|
|
|
|
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-06-01 02:45:04 +03:00
|
|
|
WITFunctionInner::Import {
|
2020-06-07 22:57:30 +03:00
|
|
|
callable
|
2020-06-01 02:45:04 +03:00
|
|
|
} => {
|
2020-06-07 22:57:30 +03:00
|
|
|
Arc::make_mut(&mut callable.clone()).call(arguments).map_err(|_| ())
|
2020-06-01 02:45:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|