1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use uptown_funk::{Executor, HostFunctions};

use crate::api::channel::ChannelReceiver;
use crate::module::LunaticModule;

use crate::api::{channel, networking, process, wasi};
pub struct DefaultApi {
    context_receiver: Option<ChannelReceiver>,
    module: LunaticModule,
}

impl DefaultApi {
    pub fn new(context_receiver: Option<ChannelReceiver>, module: LunaticModule) -> Self {
        Self {
            context_receiver,
            module,
        }
    }
}

impl HostFunctions for DefaultApi {
    type Return = ();

    #[cfg(feature = "vm-wasmtime")]
    fn add_to_linker<E>(self, executor: E, linker: &mut wasmtime::Linker)
    where
        E: Executor + Clone + 'static,
    {
        let channel_state = channel::api::ChannelState::new(self.context_receiver);
        let process_state = process::api::ProcessState::new(self.module, channel_state.clone());
        let networking_state = networking::TcpState::new(channel_state.clone());
        let wasi_state = wasi::api::WasiState::new();

        channel_state.add_to_linker(executor.clone(), linker);
        process_state.add_to_linker(executor.clone(), linker);
        networking_state.add_to_linker(executor.clone(), linker);
        wasi_state.add_to_linker(executor, linker);
    }

    #[cfg(feature = "vm-wasmer")]
    fn add_to_wasmer_linker<E>(
        self,
        executor: E,
        linker: &mut uptown_funk::wasmer::WasmerLinker,
        store: &wasmer::Store,
    ) -> ()
    where
        E: Executor + Clone + 'static,
    {
        let channel_state = channel::api::ChannelState::new(self.context_receiver);
        let process_state = process::api::ProcessState::new(self.module, channel_state.clone());
        let networking_state = networking::TcpState::new(channel_state.clone());
        let wasi_state = wasi::api::WasiState::new();

        channel_state.add_to_wasmer_linker(executor.clone(), linker, store);
        process_state.add_to_wasmer_linker(executor.clone(), linker, store);
        networking_state.add_to_wasmer_linker(executor.clone(), linker, store);
        wasi_state.add_to_wasmer_linker(executor, linker, store);
    }
}