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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! Values: what a stack slot, local variable, or property can hold.
use std::fmt;
/// An object ID: an index into the VM's object table.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct ObjId(pub u32);
impl ObjId {
/// The ID that refers to no object.
pub const INVALID: ObjId = ObjId(0);
/// Whether this ID can refer to an object.
pub const fn is_valid(self) -> bool {
self.0 != 0
}
}
impl fmt::Display for ObjId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
/// A property ID.
pub type PropId = u16;
/// The property ID that refers to no property.
pub const INVALID_PROP: PropId = 0;
/// The calling interface of a method implemented by the VM itself.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NativeDesc {
/// Number of required arguments.
pub min_args: u8,
/// Number of optional arguments after the required ones.
pub opt_args: u8,
/// Whether any number of further arguments is accepted.
pub varargs: bool,
}
impl NativeDesc {
/// A method taking exactly `min_args` arguments.
pub const fn fixed(min_args: u8) -> Self {
Self {
min_args,
opt_args: 0,
varargs: false,
}
}
/// A method taking `min_args` to `min_args + opt_args` arguments.
pub const fn optional(min_args: u8, opt_args: u8) -> Self {
Self {
min_args,
opt_args,
varargs: false,
}
}
/// A method taking `min_args` or more arguments.
pub const fn variable(min_args: u8) -> Self {
Self {
min_args,
opt_args: 0,
varargs: true,
}
}
/// Whether a call with `argc` arguments fits this interface.
pub const fn accepts(self, argc: u32) -> bool {
argc >= self.min_args as u32
&& (self.varargs || argc <= self.min_args as u32 + self.opt_args as u32)
}
}
/// A value, as the virtual machine holds one.
///
/// The variants mirror the reference VM's value types. Strings and lists
/// appear twice: as constants (an offset into the image's constant pool)
/// and as objects (an [`ObjId`] of a string or list object).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Value {
/// `nil`: false, and the absence of a value.
Nil,
/// `true`.
True,
/// No value at all. Marks unused slots; programs never see it.
Empty,
/// A 32-bit integer.
Int(i32),
/// An object reference.
Obj(ObjId),
/// A property ID.
Prop(PropId),
/// A constant string, by constant pool offset.
SString(u32),
/// A constant string that displays itself when evaluated as a property.
DString(u32),
/// A constant list, by constant pool offset.
List(u32),
/// Method code, by code pool offset. Only property values have this
/// type; evaluating the property calls the code.
CodeOfs(u32),
/// A function pointer, by code pool offset.
FuncPtr(u32),
/// An enumerator.
Enum(u32),
/// A pointer to a function the VM implements.
BifPtr {
/// Index of the function set in the image's dependency list.
set: u16,
/// Index of the function within the set.
index: u16,
},
/// An object to invoke when the property holding it is evaluated.
ObjX(ObjId),
/// A VM function to call when the property holding it is evaluated.
BifPtrX {
/// Index of the function set in the image's dependency list.
set: u16,
/// Index of the function within the set.
index: u16,
},
/// A method the VM implements, as found by a property lookup that asked
/// not to call it.
NativeCode(NativeDesc),
}
/// Type codes as programs see them through `dataType()` and as images
/// store them in data holders.
pub mod type_code {
#![allow(missing_docs)]
pub const NIL: i32 = 1;
pub const TRUE: i32 = 2;
pub const OBJ: i32 = 5;
pub const PROP: i32 = 6;
pub const INT: i32 = 7;
pub const SSTRING: i32 = 8;
pub const DSTRING: i32 = 9;
pub const LIST: i32 = 10;
pub const CODEOFS: i32 = 11;
pub const FUNCPTR: i32 = 12;
pub const EMPTY: i32 = 13;
pub const NATIVE_CODE: i32 = 14;
pub const ENUM: i32 = 15;
pub const BIFPTR: i32 = 16;
pub const OBJX: i32 = 17;
pub const BIFPTRX: i32 = 18;
}
impl Value {
/// Size of a value as images encode it: a type byte and four data bytes.
pub const HOLDER_SIZE: usize = 5;
/// `true` or `nil`.
pub const fn logical(condition: bool) -> Self {
if condition { Self::True } else { Self::Nil }
}
/// An object reference, or `nil` for the invalid ID.
pub const fn obj_or_nil(id: ObjId) -> Self {
if id.is_valid() {
Self::Obj(id)
} else {
Self::Nil
}
}
/// The object ID, if this is an object reference.
pub const fn as_obj(self) -> Option<ObjId> {
match self {
Self::Obj(id) => Some(id),
_ => None,
}
}
/// The object ID, or the invalid ID for anything but an object.
pub const fn obj_id(self) -> ObjId {
match self {
Self::Obj(id) => id,
_ => ObjId::INVALID,
}
}
/// Whether this is `nil`.
pub const fn is_nil(self) -> bool {
matches!(self, Self::Nil)
}
/// Whether evaluating a property with this value runs code.
pub const fn is_invocable_prop(self) -> bool {
matches!(
self,
Self::CodeOfs(_) | Self::ObjX(_) | Self::BifPtrX { .. }
)
}
/// The type code of this value.
pub const fn type_code(self) -> i32 {
match self {
Self::Nil => type_code::NIL,
Self::True => type_code::TRUE,
Self::Empty => type_code::EMPTY,
Self::Int(_) => type_code::INT,
Self::Obj(_) => type_code::OBJ,
Self::Prop(_) => type_code::PROP,
Self::SString(_) => type_code::SSTRING,
Self::DString(_) => type_code::DSTRING,
Self::List(_) => type_code::LIST,
Self::CodeOfs(_) => type_code::CODEOFS,
Self::FuncPtr(_) => type_code::FUNCPTR,
Self::Enum(_) => type_code::ENUM,
Self::BifPtr { .. } => type_code::BIFPTR,
Self::ObjX(_) => type_code::OBJX,
Self::BifPtrX { .. } => type_code::BIFPTRX,
Self::NativeCode(_) => type_code::NATIVE_CODE,
}
}
/// Encode a value in the five-byte form images and saved states use.
pub fn to_holder(self) -> [u8; Self::HOLDER_SIZE] {
let mut b = [0u8; Self::HOLDER_SIZE];
b[0] = self.type_code() as u8;
let data: u32 = match self {
Self::Obj(id) | Self::ObjX(id) => id.0,
Self::Int(n) => n as u32,
Self::Prop(prop) => prop.into(),
Self::SString(at)
| Self::DString(at)
| Self::List(at)
| Self::CodeOfs(at)
| Self::FuncPtr(at)
| Self::Enum(at) => at,
Self::BifPtr { set, index } | Self::BifPtrX { set, index } => {
u32::from(index) | u32::from(set) << 16
}
_ => 0,
};
b[1..].copy_from_slice(&data.to_le_bytes());
b
}
/// Decode a value from the five-byte form images use.
///
/// Type codes that cannot appear in an image decode as `nil`.
pub fn from_holder(b: &[u8]) -> Self {
let u32_at = || u32::from_le_bytes([b[1], b[2], b[3], b[4]]);
let u16_at = |at: usize| u16::from_le_bytes([b[at], b[at + 1]]);
match i32::from(b[0]) {
type_code::TRUE => Self::True,
type_code::OBJ => Self::obj_or_nil(ObjId(u32_at())),
type_code::PROP => Self::Prop(u16_at(1)),
type_code::INT => Self::Int(u32_at() as i32),
type_code::SSTRING => Self::SString(u32_at()),
type_code::DSTRING => Self::DString(u32_at()),
type_code::LIST => Self::List(u32_at()),
type_code::CODEOFS => Self::CodeOfs(u32_at()),
type_code::FUNCPTR => Self::FuncPtr(u32_at()),
type_code::EMPTY => Self::Empty,
type_code::ENUM => Self::Enum(u32_at()),
type_code::BIFPTR => Self::BifPtr {
index: u16_at(1),
set: u16_at(3),
},
type_code::OBJX => Self::ObjX(ObjId(u32_at())),
type_code::BIFPTRX => Self::BifPtrX {
index: u16_at(1),
set: u16_at(3),
},
_ => Self::Nil,
}
}
}