marine_core/
config.rs

1/*
2 * Copyright 2020 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::IValue;
18use super::IType;
19use crate::HostImportError;
20
21use marine_wasm_backend_traits::WasiParameters;
22use marine_wasm_backend_traits::WasmBackend;
23
24use std::path::PathBuf;
25use std::collections::HashMap;
26use std::sync::Arc;
27
28pub type ErrorHandler =
29    Option<Box<dyn Fn(&HostImportError) -> Option<IValue> + Sync + Send + 'static>>;
30pub type HostExportedFunc<WB> = Box<
31    dyn for<'c> Fn(&mut <WB as WasmBackend>::ImportCallContext<'c>, Vec<IValue>) -> Option<IValue>
32        + Sync
33        + Send
34        + 'static,
35>;
36
37pub type RawImportCreator<WB> = Arc<
38    dyn Fn(<WB as WasmBackend>::ContextMut<'_>) -> <WB as WasmBackend>::HostFunction + Send + Sync,
39>;
40
41pub struct HostImportDescriptor<WB: WasmBackend> {
42    /// This closure will be invoked for corresponding import.
43    pub host_exported_func: HostExportedFunc<WB>,
44
45    /// Type of the closure arguments.
46    pub argument_types: Vec<IType>,
47
48    /// Types of output of the closure.
49    pub output_type: Option<IType>,
50
51    /// If Some, this closure is called with error when errors is encountered while lifting.
52    /// If None, panic will occur.
53    pub error_handler: ErrorHandler,
54}
55
56#[derive(Hash, Ord, PartialOrd, Eq, PartialEq)]
57pub enum HostAPIVersion {
58    V0,
59    V1,
60    V2,
61    V3,
62}
63
64impl HostAPIVersion {
65    pub fn namespace(&self) -> &'static str {
66        // TODO: create a common place for these consts to use in both marine and marine-rs-sdk to use in both marine and marine-rs-sdk
67        match self {
68            Self::V0 => "host",
69            Self::V1 => "__marine_host_api_v1",
70            Self::V2 => "__marine_host_api_v2",
71            Self::V3 => "__marine_host_api_v3",
72        }
73    }
74}
75
76pub struct MModuleConfig<WB: WasmBackend> {
77    /// Import object that will be used in module instantiation process.
78    pub raw_imports: HashMap<HostAPIVersion, HashMap<String, RawImportCreator<WB>>>,
79
80    /// Imports from the host side that will be used in module instantiation process.
81    pub host_imports: HashMap<HostAPIVersion, HashMap<String, HostImportDescriptor<WB>>>,
82
83    /// WASI parameters: env variables, mapped dirs, and args
84    pub wasi_parameters: WasiParameters,
85}
86
87impl<WB: WasmBackend> Default for MModuleConfig<WB> {
88    fn default() -> Self {
89        // some reasonable defaults
90        Self {
91            raw_imports: HashMap::new(),
92            host_imports: HashMap::new(),
93            wasi_parameters: WasiParameters::default(),
94        }
95    }
96}
97
98// TODO: implement debug for MModuleConfig
99
100#[allow(dead_code)]
101impl<WB: WasmBackend> MModuleConfig<WB> {
102    pub fn with_wasi_envs(mut self, envs: HashMap<String, String>) -> Self {
103        self.wasi_parameters.envs = envs;
104        self
105    }
106
107    pub fn with_wasi_mapped_dirs(mut self, mapped_dirs: HashMap<String, PathBuf>) -> Self {
108        self.wasi_parameters.mapped_dirs = mapped_dirs;
109        self
110    }
111}
112
113pub struct MarineCoreConfig<WB: WasmBackend> {
114    pub(crate) total_memory_limit: u64,
115    pub(crate) wasm_backend: WB,
116}
117
118pub const INFINITE_MEMORY_LIMIT: u64 = u64::MAX;
119
120impl<WB: WasmBackend> MarineCoreConfig<WB> {
121    pub fn new(wasm_backend: WB, total_memory_limit: Option<u64>) -> Self {
122        Self {
123            total_memory_limit: total_memory_limit.unwrap_or(INFINITE_MEMORY_LIMIT),
124            wasm_backend,
125        }
126    }
127}