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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use crate::collections::HashMap;
use crate::runtime::{
Call, ConstValue, DebugInfo, Inst, Rtti, StaticString, VariantRtti, VmError, VmErrorKind,
};
use crate::Hash;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::sync::Arc;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Unit {
instructions: Vec<Inst>,
functions: HashMap<Hash, UnitFn>,
static_strings: Vec<Arc<StaticString>>,
static_bytes: Vec<Vec<u8>>,
static_object_keys: Vec<Box<[String]>>,
rtti: HashMap<Hash, Arc<Rtti>>,
variant_rtti: HashMap<Hash, Arc<VariantRtti>>,
debug: Option<Box<DebugInfo>>,
constants: HashMap<Hash, ConstValue>,
}
impl Unit {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
instructions: Vec<Inst>,
functions: HashMap<Hash, UnitFn>,
static_strings: Vec<Arc<StaticString>>,
static_bytes: Vec<Vec<u8>>,
static_object_keys: Vec<Box<[String]>>,
rtti: HashMap<Hash, Arc<Rtti>>,
variant_rtti: HashMap<Hash, Arc<VariantRtti>>,
debug: Option<Box<DebugInfo>>,
constants: HashMap<Hash, ConstValue>,
) -> Self {
Self {
instructions,
functions,
static_strings,
static_bytes,
static_object_keys,
rtti,
variant_rtti,
debug,
constants,
}
}
pub fn debug_info(&self) -> Option<&DebugInfo> {
let debug = self.debug.as_ref()?;
Some(&**debug)
}
pub fn instruction_at(&self, ip: usize) -> Option<&Inst> {
self.instructions.get(ip)
}
pub fn iter_static_strings(&self) -> impl Iterator<Item = &Arc<StaticString>> + '_ {
self.static_strings.iter()
}
pub fn iter_constants(&self) -> impl Iterator<Item = (&Hash, &ConstValue)> + '_ {
self.constants.iter()
}
pub fn iter_static_object_keys(&self) -> impl Iterator<Item = (usize, &[String])> + '_ {
let mut it = self.static_object_keys.iter().enumerate();
std::iter::from_fn(move || {
let (n, s) = it.next()?;
Some((n, &s[..]))
})
}
pub fn iter_instructions(&self) -> impl Iterator<Item = Inst> + '_ {
self.instructions.iter().copied()
}
pub fn iter_functions(&self) -> impl Iterator<Item = (Hash, &UnitFn)> + '_ {
self.functions.iter().map(|(h, f)| (*h, f))
}
pub fn lookup_string(&self, slot: usize) -> Result<&Arc<StaticString>, VmError> {
Ok(self
.static_strings
.get(slot)
.ok_or(VmErrorKind::MissingStaticString { slot })?)
}
pub fn lookup_bytes(&self, slot: usize) -> Result<&[u8], VmError> {
Ok(self
.static_bytes
.get(slot)
.ok_or(VmErrorKind::MissingStaticString { slot })?
.as_ref())
}
pub fn lookup_object_keys(&self, slot: usize) -> Option<&[String]> {
self.static_object_keys.get(slot).map(|keys| &keys[..])
}
pub fn lookup_rtti(&self, hash: Hash) -> Option<&Arc<Rtti>> {
self.rtti.get(&hash)
}
pub fn lookup_variant_rtti(&self, hash: Hash) -> Option<&Arc<VariantRtti>> {
self.variant_rtti.get(&hash)
}
pub fn function(&self, hash: Hash) -> Option<UnitFn> {
self.functions.get(&hash).copied()
}
pub fn constant(&self, hash: Hash) -> Option<&ConstValue> {
self.constants.get(&hash)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[non_exhaustive]
pub enum UnitFn {
Offset {
offset: usize,
call: Call,
args: usize,
},
UnitStruct {
hash: Hash,
},
TupleStruct {
hash: Hash,
args: usize,
},
UnitVariant {
hash: Hash,
},
TupleVariant {
hash: Hash,
args: usize,
},
}
impl fmt::Display for UnitFn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Offset { offset, call, args } => {
write!(f, "offset {}, {}, {}", offset, call, args)?;
}
Self::UnitStruct { hash } => {
write!(f, "unit {}", hash)?;
}
Self::TupleStruct { hash, args } => {
write!(f, "tuple {}, {}", hash, args)?;
}
Self::UnitVariant { hash } => {
write!(f, "empty-variant {}", hash)?;
}
Self::TupleVariant { hash, args } => {
write!(f, "tuple-variant {}, {}", hash, args)?;
}
}
Ok(())
}
}
#[cfg(test)]
static_assertions::assert_impl_all!(Unit: Send, Sync);