marine_module_interface/it_interface/export_it_functions.rs
1/*
2 * Copyright 2021 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use super::IFunctionSignature;
18use super::ITInterfaceError;
19use super::RIResult;
20
21use marine_it_interfaces::MITInterfaces;
22
23use std::sync::Arc;
24
25pub struct ITExportFuncDescriptor<'n> {
26 pub adapter_function_type: u32,
27 pub name: &'n str,
28}
29
30/// Returns all exported IT functions descriptors.
31pub fn get_export_funcs_descriptors<'i>(
32 mit: &'i MITInterfaces<'_>,
33) -> Vec<ITExportFuncDescriptor<'i>> {
34 // An IT function is exported if it lies in export functions and have implementation.
35 // An export IT function without implementation is a hack and used to call core function from
36 // a Wasm module. This hack is needed because there is only one call instruction in the
37 // interface-types crates and it's needed to distinguish somehow between calling export IT or
38 // core functions. This scheme is a kind of mess and it needs to be refactored one day.
39 mit.implementations()
40 .filter_map(|(adapter_function_type, core_function_type)| {
41 mit.exports_by_type(*core_function_type)
42 .map(|export_function_name| (adapter_function_type, export_function_name))
43 })
44 .flat_map(|(&adapter_function_type, export_function_names)| {
45 export_function_names
46 .iter()
47 .map(move |name| ITExportFuncDescriptor {
48 adapter_function_type,
49 name,
50 })
51 })
52 .collect::<Vec<_>>()
53}
54
55/// Returns all exported IT functions.
56pub fn get_export_funcs(mit: &MITInterfaces<'_>) -> RIResult<Vec<IFunctionSignature>> {
57 use marine_it_interfaces::ITAstType;
58
59 let funcs_descriptors = get_export_funcs_descriptors(mit);
60
61 funcs_descriptors
62 .into_iter()
63 .map(|descriptor| {
64 let it_type = mit.type_by_idx_r(descriptor.adapter_function_type)?;
65
66 match it_type {
67 ITAstType::Function {
68 arguments,
69 output_types,
70 } => {
71 let signature = IFunctionSignature {
72 name: Arc::new(descriptor.name.to_string()),
73 arguments: arguments.clone(),
74 outputs: output_types.clone(),
75 adapter_function_type: descriptor.adapter_function_type,
76 };
77 Ok(signature)
78 }
79 _ => Err(ITInterfaceError::ITTypeNotFunction(
80 descriptor.adapter_function_type,
81 )),
82 }
83 })
84 .collect::<RIResult<Vec<IFunctionSignature>>>()
85}