forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm_component_runtime.rs
More file actions
126 lines (110 loc) · 4.01 KB
/
Copy pathwasm_component_runtime.rs
File metadata and controls
126 lines (110 loc) · 4.01 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use crate::types::{NuError, NuResult, Span};
#[cfg(feature = "wasm-backend")]
use wasmtime::component::*;
#[cfg(feature = "wasm-backend")]
use wasmtime::*;
#[cfg(feature = "wasm-backend")]
bindgen!({
world: "actor",
path: "wit/actor.wit",
});
#[cfg(feature = "wasm-backend")]
pub fn component_config() -> Config {
let mut config = Config::new();
config.wasm_component_model(true);
config.memory_reservation(4 << 30);
config.memory_guard_size(128 << 20);
config.cranelift_opt_level(OptLevel::Speed);
config.wasm_simd(true);
config
}
#[cfg(feature = "wasm-backend")]
pub struct HostState {}
#[cfg(feature = "wasm-backend")]
pub struct ComponentRuntime {
_engine: Engine,
component: Component,
config: Config,
}
#[cfg(feature = "wasm-backend")]
impl ComponentRuntime {
pub fn new(wasm_bytes: &[u8]) -> NuResult<Self> {
let config = component_config();
let engine = Engine::new(&config).map_err(|e| NuError::VMError {
msg: format!("wasmtime engine: {}", e),
span: Span::default(),
})?;
let component = Component::new(&engine, wasm_bytes).map_err(|e| NuError::VMError {
msg: format!("wasmtime component: {}", e),
span: Span::default(),
})?;
let config = component_config();
let engine = Engine::new(&config).map_err(|e| NuError::VMError {
msg: format!("wasmtime engine: {}", e),
span: Span::default(),
})?;
Ok(ComponentRuntime {
_engine: engine,
component,
config,
})
}
pub fn init(&self) -> NuResult<i64> {
let engine = Engine::new(&self.config).map_err(|e| NuError::VMError {
msg: format!("wasmtime engine: {}", e),
span: Span::default(),
})?;
let mut store = Store::new(&engine, HostState {});
let linker = wasmtime::component::Linker::<HostState>::new(&engine);
let actor = Actor::instantiate(&mut store, &self.component, &linker).map_err(|e| {
NuError::VMError {
msg: format!("wasmtime instantiate: {}", e),
span: Span::default(),
}
})?;
actor.call_init(&mut store).map_err(|e| NuError::VMError {
msg: format!("wasmtime call_init: {}", e),
span: Span::default(),
})
}
pub fn handle_message(&self, msg: &[u8]) -> NuResult<i64> {
let engine = Engine::new(&self.config).map_err(|e| NuError::VMError {
msg: format!("wasmtime engine: {}", e),
span: Span::default(),
})?;
let mut store = Store::new(&engine, HostState {});
let linker = wasmtime::component::Linker::<HostState>::new(&engine);
let actor = Actor::instantiate(&mut store, &self.component, &linker).map_err(|e| {
NuError::VMError {
msg: format!("wasmtime instantiate: {}", e),
span: Span::default(),
}
})?;
actor
.call_handle_message(&mut store, msg)
.map_err(|e| NuError::VMError {
msg: format!("wasmtime call_handle_message: {}", e),
span: Span::default(),
})
}
pub fn checkpoint(&self) -> NuResult<Vec<u8>> {
let engine = Engine::new(&self.config).map_err(|e| NuError::VMError {
msg: format!("wasmtime engine: {}", e),
span: Span::default(),
})?;
let mut store = Store::new(&engine, HostState {});
let linker = wasmtime::component::Linker::<HostState>::new(&engine);
let actor = Actor::instantiate(&mut store, &self.component, &linker).map_err(|e| {
NuError::VMError {
msg: format!("wasmtime instantiate: {}", e),
span: Span::default(),
}
})?;
actor
.call_checkpoint(&mut store)
.map_err(|e| NuError::VMError {
msg: format!("wasmtime call_checkpoint: {}", e),
span: Span::default(),
})
}
}