hexicon-vm 0.2.0

The Hexicon virtual machine: runs the games Hexicon builds
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Program-defined objects: superclass lists, property tables, and the
//! inheritance search.

use std::cell::RefCell;
use std::rc::Rc;

use crate::object::{Object, ObjectTable};
use crate::value::{ObjId, PropId, Value};

/// The object was defined with `class`.
pub const OBJ_FLAG_CLASS: u16 = 0x0001;

/// One property of an object.
#[derive(Debug, Clone, Copy)]
pub struct PropEntry {
    /// The property ID.
    pub prop: PropId,
    /// The property's value.
    pub value: Value,
    /// The value has changed since the object was loaded.
    pub modified: bool,
    /// Next entry in the same hash bucket, plus one; zero ends the chain.
    next: u32,
}

/// A program-defined object.
///
/// Properties keep the order they were added in, which programs can see
/// through `getPropList`.
#[derive(Debug, Clone, Default)]
pub struct ProgramObject {
    /// The direct superclasses, in declaration order.
    pub superclasses: Vec<ObjId>,
    /// Flags from the image; see [`OBJ_FLAG_CLASS`].
    pub load_flags: u16,
    /// The object has changed since it was loaded.
    pub modified: bool,
    entries: Vec<PropEntry>,
    /// Hash bucket heads: an index into `entries` plus one, or zero.
    buckets: Vec<u32>,
    /// The order in which to search superclasses when there is more than
    /// one, computed on first use.
    search_path: RefCell<Option<Rc<[ObjId]>>>,
    /// The reference's hash table size and the number of properties it
    /// has room for, which decide how many bytes it allocates. See
    /// [`crate::heap`].
    ref_hash: usize,
    ref_room: usize,
}

/// The properties a new object has room for unless it says otherwise, in
/// the reference.
const PROP_ROOM: usize = 16;

/// The reference's hash table size for an object with room asked for so
/// many properties, and the room it gives it.
fn ref_table(count: usize) -> (usize, usize) {
    let hash = count.next_power_of_two().clamp(2, 256);
    (hash, count.max(hash))
}

impl ProgramObject {
    /// An object with the given superclasses and no properties.
    pub fn new(superclasses: Vec<ObjId>) -> Self {
        Self::with_room(superclasses, PROP_ROOM)
    }

    /// An object with the given superclasses and no properties, that the
    /// reference would give room for so many.
    pub fn with_room(superclasses: Vec<ObjId>, props: usize) -> Self {
        let (ref_hash, ref_room) = ref_table(props);
        Self {
            superclasses,
            ref_hash,
            ref_room,
            ..Self::default()
        }
    }

    /// The bytes the reference's header for this object takes.
    pub(crate) fn heap_bytes(&self) -> i64 {
        crate::heap::program_object_bytes(self.superclasses.len(), self.ref_hash, self.ref_room)
    }

    /// Grow the reference's header to so many superclasses and room for
    /// so many properties, counting what it allocates.
    fn ref_expand(&mut self, superclasses: usize, props: usize) {
        (self.ref_hash, self.ref_room) = ref_table(props);
        crate::heap::count(
            crate::heap::program_object_bytes(superclasses, self.ref_hash, self.ref_room),
            "hexicon-object",
        );
    }

    /// Read an object from its image data: a superclass count, a property
    /// count, flags, the superclass IDs, and the properties.
    pub fn from_image(data: &[u8]) -> Option<Self> {
        let u16_at = |at: usize| Some(u16::from_le_bytes([*data.get(at)?, *data.get(at + 1)?]));
        let superclass_count = u16_at(0)? as usize;
        let prop_count = u16_at(2)? as usize;
        let (ref_hash, ref_room) = ref_table(prop_count);
        let mut object = Self {
            load_flags: u16_at(4)?,
            ref_hash,
            ref_room,
            ..Self::default()
        };
        let mut pos = 6;
        for _ in 0..superclass_count {
            let b = data.get(pos..pos + 4)?;
            object
                .superclasses
                .push(ObjId(u32::from_le_bytes([b[0], b[1], b[2], b[3]])));
            pos += 4;
        }
        object.entries.reserve(prop_count);
        for _ in 0..prop_count {
            let prop = u16_at(pos)?;
            let holder = data.get(pos + 2..pos + 2 + Value::HOLDER_SIZE)?;
            object.insert(prop, Value::from_holder(holder));
            pos += 2 + Value::HOLDER_SIZE;
        }
        Some(object)
    }

    /// Whether the object was defined as a class.
    pub fn is_class(&self) -> bool {
        self.load_flags & OBJ_FLAG_CLASS != 0
    }

    fn bucket(&self, prop: PropId) -> usize {
        prop as usize & (self.buckets.len() - 1)
    }

    fn rehash(&mut self, size: usize) {
        self.buckets = vec![0; size];
        for index in 0..self.entries.len() {
            let bucket = self.bucket(self.entries[index].prop);
            self.entries[index].next = self.buckets[bucket];
            self.buckets[bucket] = index as u32 + 1;
        }
    }

    /// Append a property that is known not to be present.
    fn insert(&mut self, prop: PropId, value: Value) -> &mut PropEntry {
        if self.entries.len() >= self.ref_room {
            // The reference doubles the room, or adds half once it is
            // large.
            let room = self.ref_room;
            let props = if room <= 128 {
                room * 2
            } else {
                room + room / 2
            };
            self.ref_expand(self.superclasses.len(), props.max(1));
        }
        if self.entries.len() >= self.buckets.len() {
            self.rehash((self.buckets.len() * 2).max(4));
        }
        let bucket = self.bucket(prop);
        self.entries.push(PropEntry {
            prop,
            value,
            modified: false,
            next: self.buckets[bucket],
        });
        self.buckets[bucket] = self.entries.len() as u32;
        self.entries.last_mut().expect("just pushed")
    }

    fn index_of(&self, prop: PropId) -> Option<usize> {
        if self.buckets.is_empty() {
            return None;
        }
        let mut link = self.buckets[self.bucket(prop)];
        while link != 0 {
            let entry = &self.entries[link as usize - 1];
            if entry.prop == prop {
                return Some(link as usize - 1);
            }
            link = entry.next;
        }
        None
    }

    /// The value of a property this object defines directly.
    pub fn get(&self, prop: PropId) -> Option<Value> {
        self.index_of(prop).map(|index| self.entries[index].value)
    }

    /// Set a property on this object, adding it if it is not defined here.
    /// Returns the value it replaced.
    pub fn set(&mut self, prop: PropId, value: Value) -> Option<Value> {
        self.modified = true;
        match self.index_of(prop) {
            Some(index) => {
                let entry = &mut self.entries[index];
                entry.modified = true;
                Some(std::mem::replace(&mut entry.value, value))
            }
            None => {
                self.insert(prop, value).modified = true;
                None
            }
        }
    }

    /// Give a property this object defines a new value, as it is when
    /// the image is written: no change the program made.
    pub fn set_value_in_place(&mut self, prop: PropId, value: Value) {
        if let Some(index) = self.index_of(prop) {
            self.entries[index].value = value;
        }
    }

    /// The properties this object defines directly, in the order they were
    /// added.
    pub fn props(&self) -> &[PropEntry] {
        &self.entries
    }

    /// Replace the superclass list.
    pub fn set_superclasses(&mut self, superclasses: Vec<ObjId>) {
        if superclasses.len() > self.superclasses.len() {
            self.ref_expand(superclasses.len(), self.ref_room);
        }
        self.superclasses = superclasses;
        self.invalidate_search_path();
    }

    /// Forget the computed superclass search order.
    pub fn invalidate_search_path(&self) {
        *self.search_path.borrow_mut() = None;
    }

    /// Whether the computed search order mentions an object.
    pub fn search_path_contains(&self, id: ObjId) -> bool {
        self.search_path
            .borrow()
            .as_ref()
            .is_some_and(|path| path.contains(&id))
    }

    /// The order in which to search this object's superclasses, direct and
    /// indirect, for an inherited property.
    ///
    /// The order is breadth-first by declaration, except that a class
    /// reachable by more than one route takes its last position, so a
    /// shared base class comes after everything derived from it.
    fn search_path(&self, table: &ObjectTable) -> Rc<[ObjId]> {
        if let Some(path) = self.search_path.borrow().as_ref() {
            return path.clone();
        }
        const NONE: usize = usize::MAX;
        // A singly linked queue. Removing an element leaves a tombstone.
        let mut nodes: Vec<(ObjId, usize)> = Vec::new();
        let mut head = NONE;
        let mut position = NONE;
        let mut current = self;
        'build: loop {
            let mut insert_after = position;
            for &superclass in &current.superclasses {
                if !matches!(table.get(superclass), Some(Object::Program(_))) {
                    continue;
                }
                let mut node = head;
                while node != NONE {
                    if nodes[node].0 == superclass {
                        nodes[node].0 = ObjId::INVALID;
                        break;
                    }
                    node = nodes[node].1;
                }
                let new = nodes.len();
                if insert_after == NONE {
                    nodes.push((superclass, head));
                    head = new;
                } else {
                    nodes.push((superclass, nodes[insert_after].1));
                    nodes[insert_after].1 = new;
                }
                insert_after = new;
            }
            loop {
                position = if position == NONE {
                    head
                } else {
                    nodes[position].1
                };
                if position == NONE {
                    break 'build;
                }
                if nodes[position].0.is_valid() {
                    break;
                }
            }
            match table.program_object(nodes[position].0) {
                Some(object) => current = object,
                None => break,
            }
        }
        let mut path = Vec::new();
        let mut node = head;
        while node != NONE {
            if nodes[node].0.is_valid() {
                path.push(nodes[node].0);
            }
            node = nodes[node].1;
        }
        let path: Rc<[ObjId]> = path.into();
        *self.search_path.borrow_mut() = Some(path.clone());
        path
    }
}

/// A walk from an object up through its superclasses in inheritance order.
pub struct Search<'a> {
    table: &'a ObjectTable,
    /// The object the walk is at.
    pub current: ObjId,
    object: Option<&'a ProgramObject>,
    path: Option<(Rc<[ObjId]>, usize)>,
}

impl<'a> Search<'a> {
    /// Start a walk at an object.
    pub fn new(table: &'a ObjectTable, start: ObjId) -> Self {
        Self {
            table,
            current: start,
            object: table.program_object(start),
            path: None,
        }
    }

    /// Move to the next object in inheritance order. Returns false at the
    /// end.
    pub fn advance(&mut self) -> bool {
        loop {
            if let Some((path, position)) = &mut self.path {
                let Some(&next) = path.get(*position) else {
                    return false;
                };
                *position += 1;
                self.current = next;
                self.object = self.table.program_object(next);
                return true;
            }
            let Some(object) = self.object else {
                return false;
            };
            match object.superclasses.len() {
                0 => return false,
                // With one superclass there is nothing to order. This is
                // by far the most common case.
                1 => {
                    self.current = object.superclasses[0];
                    self.object = self.table.program_object(self.current);
                    return self.object.is_some();
                }
                _ => self.path = Some((object.search_path(self.table), 0)),
            }
        }
    }

    /// Find the nearest definition of a property at or after the current
    /// position. Returns its value and the object that defines it.
    pub fn find(&mut self, prop: PropId) -> Option<(Value, ObjId)> {
        loop {
            if let Some(value) = self.object.and_then(|object| object.get(prop)) {
                return Some((value, self.current));
            }
            if !self.advance() {
                return None;
            }
        }
    }

    /// Move forward to a given object. Returns false if the walk ends
    /// without reaching it.
    pub fn skip_to(&mut self, target: ObjId) -> bool {
        while self.current != target {
            if !self.advance() {
                return false;
            }
        }
        true
    }
}

/// Find a property on an object or its superclasses.
///
/// For an object with one superclass, by far the most common case, what
/// the superclass gives is looked up once and remembered: see
/// [`ObjectTable::cached_lookup`].
pub fn find_prop(table: &ObjectTable, object: ObjId, prop: PropId) -> Option<(Value, ObjId)> {
    let obj = table.program_object(object)?;
    if let Some(value) = obj.get(prop) {
        return Some((value, object));
    }
    match obj.superclasses.as_slice() {
        [] => None,
        &[superclass] => {
            table.program_object(superclass)?;
            table.cached_lookup(superclass, prop, || find_prop(table, superclass, prop))
        }
        _ => Search::new(table, object).find(prop),
    }
}

/// The objects a lookup from an object depends on: it and everything it
/// inherits from.
pub(crate) fn lookup_deps(table: &ObjectTable, object: ObjId) -> Vec<ObjId> {
    let mut deps = Vec::new();
    let mut search = Search::new(table, object);
    loop {
        deps.push(search.current);
        if !search.advance() {
            return deps;
        }
    }
}

/// Find the definition of a property that the one in `definer` overrides,
/// in the inheritance order of `target`. With an invalid `definer`, find
/// the first definition.
pub fn find_inherited_prop(
    table: &ObjectTable,
    target: ObjId,
    definer: ObjId,
    prop: PropId,
) -> Option<(Value, ObjId)> {
    let mut search = Search::new(table, target);
    if definer.is_valid() && (!search.skip_to(definer) || !search.advance()) {
        return None;
    }
    search.find(prop)
}

/// Whether an object inherits, directly or indirectly, from another
/// program-defined object.
pub fn inherits_from(table: &ObjectTable, object: ObjId, ancestor: ObjId) -> bool {
    let mut search = Search::new(table, object);
    while search.advance() {
        if search.current == ancestor {
            return true;
        }
    }
    false
}