virtual_machine/
runtime.rs1use core::ffi::c_void;
7
8use wamr_rust_sdk::{
9 runtime,
10 sys::{wasm_runtime_destroy_thread_env, wasm_runtime_init_thread_env},
11};
12
13use crate::{Registrable, Result};
14
15pub struct RuntimeBuilder(runtime::RuntimeBuilder);
20
21impl Default for RuntimeBuilder {
22 fn default() -> Self {
23 Self::new()
24 }
25}
26
27impl RuntimeBuilder {
28 pub fn new() -> Self {
29 let runtime_builder = runtime::Runtime::builder().use_system_allocator();
30
31 Self(runtime_builder)
32 }
33
34 pub fn register_function(self, name: &str, function_pointer: *mut c_void) -> Self {
35 Self(self.0.register_host_function(name, function_pointer))
36 }
37
38 pub fn register(mut self, registrable: &dyn Registrable) -> Self {
39 for function_descriptor in registrable.get_functions() {
40 self = self.register_function(function_descriptor.name, function_descriptor.pointer);
41 }
42
43 self
44 }
45
46 pub fn build(self) -> Result<Runtime> {
47 Ok(Runtime(self.0.build()?))
48 }
49}
50
51pub struct Runtime(runtime::Runtime);
52
53impl Runtime {
54 pub fn builder() -> RuntimeBuilder {
55 RuntimeBuilder::new()
56 }
57
58 pub(crate) fn get_inner_reference(&self) -> &runtime::Runtime {
59 &self.0
60 }
61
62 pub fn initialize_thread_environment() -> Option<()> {
63 if unsafe { wasm_runtime_init_thread_env() } {
64 Some(())
65 } else {
66 None
67 }
68 }
69
70 pub fn deinitialize_thread_environment() {
71 unsafe { wasm_runtime_destroy_thread_env() }
72 }
73}