Virtual_machine/
Runtime.rs

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
use core::ffi::c_void;

use wamr_rust_sdk::{
    runtime::{Runtime, RuntimeBuilder},
    sys::{wasm_runtime_destroy_thread_env, wasm_runtime_init_thread_env},
};

use crate::{Registrable_trait, Result_type};

pub struct Runtime_builder_type(RuntimeBuilder);

impl Runtime_builder_type {
    pub fn New() -> Self {
        let Runtime_builder = Runtime::builder().use_system_allocator();

        Self(Runtime_builder)
    }

    pub fn Register_function(self, Name: &str, Function_pointer: *mut c_void) -> Self {
        Self(self.0.register_host_function(Name, Function_pointer))
    }

    pub fn Register(mut self, Registrable: &dyn Registrable_trait) -> Self {
        for Function_descriptor in Registrable.Get_functions() {
            self = self.Register_function(Function_descriptor.Name, Function_descriptor.Pointer);
        }

        self
    }

    pub fn Build(self) -> Result_type<Runtime_type> {
        Ok(Runtime_type(self.0.build()?))
    }
}

pub struct Runtime_type(Runtime);

impl Runtime_type {
    pub fn Builder() -> Runtime_builder_type {
        Runtime_builder_type::New()
    }

    pub(crate) fn Get_inner_reference(&self) -> &Runtime {
        &self.0
    }

    pub fn Initialize_thread_environment() -> Option<()> {
        if unsafe { wasm_runtime_init_thread_env() } {
            Some(())
        } else {
            None
        }
    }

    pub fn Deinitialize_thread_environment() {
        unsafe { wasm_runtime_destroy_thread_env() }
    }
}