neyuki 0.6.2

A small Lua-like scripting language implemented in Rust
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Hashable table keys. Any value except nil and NaN can be a key.

use std::cell::RefCell;
use std::hash::{Hash, Hasher};
use std::rc::Rc;

use crate::vm::buffer::VmBuffer;
use crate::vm::value::{NativeDef, StrRef, Value, VmClosure, VmTable};

#[derive(Clone, Debug)]
pub enum TableKey {
    Bool(bool),
    Int(i64),
    Float(u64), // normalized f64 bits
    String(StrRef),
    Table(*const RefCell<VmTable>),
    Closure(*const VmClosure),
    Native(*const NativeDef),
    Buffer(*const RefCell<VmBuffer>),
}

impl PartialEq for TableKey {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (TableKey::Bool(a), TableKey::Bool(b)) => a == b,
            (TableKey::Int(a), TableKey::Int(b)) => a == b,
            (TableKey::Float(a), TableKey::Float(b)) => a == b,
            (TableKey::String(a), TableKey::String(b)) => a == b,
            (TableKey::Table(a), TableKey::Table(b)) => std::ptr::eq(*a, *b),
            (TableKey::Closure(a), TableKey::Closure(b)) => std::ptr::eq(*a, *b),
            (TableKey::Native(a), TableKey::Native(b)) => std::ptr::eq(*a, *b),
            (TableKey::Buffer(a), TableKey::Buffer(b)) => std::ptr::eq(*a, *b),
            _ => false,
        }
    }
}

impl Eq for TableKey {}

impl Hash for TableKey {
    fn hash<H: Hasher>(&self, state: &mut H) {
        std::mem::discriminant(self).hash(state);
        match self {
            TableKey::Bool(b) => b.hash(state),
            TableKey::Int(i) => i.hash(state),
            TableKey::Float(bits) => bits.hash(state),
            TableKey::String(s) => s.hash(state),
            TableKey::Table(p) => p.hash(state),
            TableKey::Closure(p) => p.hash(state),
            TableKey::Native(p) => p.hash(state),
            TableKey::Buffer(p) => p.hash(state),
        }
    }
}

impl TableKey {
    pub fn from_value(val: &Value) -> Result<Self, &'static str> {
        match val {
            Value::Nil => Err("table index is nil"),
            Value::Bool(b) => Ok(TableKey::Bool(*b)),
            Value::Int(i) => Ok(TableKey::Int(*i)),
            Value::Float(f) => {
                if f.is_nan() {
                    return Err("table index is NaN");
                }
                // If float is an exact integer, represent as Int to unify lookup
                if f.fract() == 0.0 && *f >= (i64::MIN as f64) && *f <= (i64::MAX as f64) {
                    Ok(TableKey::Int(*f as i64))
                } else {
                    let bits = if *f == 0.0 { 0u64 } else { f.to_bits() };
                    Ok(TableKey::Float(bits))
                }
            }
            Value::BigInt(b) => {
                if let Some(i) = num_traits::ToPrimitive::to_i64(&**b) {
                    Ok(TableKey::Int(i))
                } else {
                    Err("table index too large")
                }
            }
            Value::String(s) => Ok(TableKey::String(s.clone())),
            Value::Table(t) => Ok(TableKey::Table(Rc::as_ptr(t))),
            Value::Closure(c) => Ok(TableKey::Closure(Rc::as_ptr(c))),
            Value::Native(def) => Ok(TableKey::Native(*def as *const _)),
            Value::Buffer(b) => Ok(TableKey::Buffer(Rc::as_ptr(b))),
        }
    }
}

/// Where a non-array key sorts: numbers first (ascending), then strings,
/// booleans, and everything else by identity.
fn key_rank(key: &Value) -> u8 {
    match key {
        Value::Int(_) | Value::BigInt(_) | Value::Float(_) => 0,
        Value::String(_) => 1,
        Value::Bool(_) => 2,
        _ => 3,
    }
}

fn identity(key: &Value) -> usize {
    match key {
        Value::Table(t) => Rc::as_ptr(t) as *const u8 as usize,
        Value::Closure(c) => Rc::as_ptr(c) as *const u8 as usize,
        Value::Native(d) => *d as *const NativeDef as usize,
        Value::Buffer(b) => Rc::as_ptr(b) as *const u8 as usize,
        _ => 0,
    }
}

fn compare_keys(a: &Value, b: &Value) -> std::cmp::Ordering {
    use std::cmp::Ordering;
    key_rank(a).cmp(&key_rank(b)).then_with(|| match (a, b) {
        (Value::String(x), Value::String(y)) => x.cmp(y),
        (Value::Bool(x), Value::Bool(y)) => x.cmp(y),
        _ if key_rank(a) == 0 => {
            if crate::vm::ops::eval_lt(a, b).unwrap_or(false) {
                Ordering::Less
            } else if crate::vm::ops::eval_lt(b, a).unwrap_or(false) {
                Ordering::Greater
            } else {
                Ordering::Equal
            }
        }
        _ => identity(a).cmp(&identity(b)),
    })
}

/// The keys outside the array part, in iteration order: numbers ascending,
/// strings sorted, then booleans and the rest. The array part always comes
/// first, so a table iterates the same way every run.
pub fn sorted_keys(table: &VmTable) -> Vec<Value> {
    let mut keys: Vec<Value> = table
        .fields
        .iter()
        .filter(|(_, v)| !matches!(v, Value::Nil))
        .map(|(k, _)| Value::String(k.clone()))
        .collect();
    if let Some(general) = &table.general {
        keys.extend(general.values().map(|(k, _)| k.clone()));
    }
    keys.sort_by(compare_keys);
    keys
}

/// A raw lookup that ignores metatables.
pub fn raw_get(table: &VmTable, key: &Value) -> Value {
    table.raw_get(key)
}

/// The entry after `key` in iteration order, as `next` returns it.
///
/// `key` does not have to be in the table any more: assigning nil to the
/// current key while iterating is allowed, and the entry after it is still
/// the one that follows it in iteration order.
pub fn next_entry(table: &VmTable, key: &Value) -> Result<Option<(Value, Value)>, String> {
    let array_start = match key {
        Value::Nil => Some(0),
        Value::Int(i) if *i >= 1 && (*i as u64) <= table.array.len() as u64 => Some(*i as usize),
        // A key trimmed off the end of the array: the array is done.
        Value::Int(i)
            if *i >= 1
                && (*i as u64) <= table.array_peak as u64
                && table.get_general(&TableKey::Int(*i)).is_none() =>
        {
            Some(table.array.len())
        }
        Value::Float(f) if f.is_nan() => return Err("invalid key to 'next'".to_string()),
        _ => None,
    };
    if let Some(start) = array_start {
        for (i, v) in table.array.iter().enumerate().skip(start) {
            if !matches!(v, Value::Nil) {
                return Ok(Some((Value::from_usize(i + 1), v.clone())));
            }
        }
        if table.fields.is_empty() && table.general.is_none() {
            return Ok(None);
        }
    }
    // The smallest hash key after `key` (or the smallest one when coming
    // from the array) is next, which a scan finds without sorting.
    let after = if array_start.is_some() {
        None
    } else {
        Some(key)
    };
    let mut best: Option<Value> = None;
    for k in table
        .fields
        .iter()
        .filter(|(_, v)| !matches!(v, Value::Nil))
        .map(|(k, _)| Value::String(k.clone()))
        .chain(
            table
                .general
                .iter()
                .flat_map(|g| g.values().map(|(k, _)| k.clone())),
        )
    {
        if after.is_some_and(|a| compare_keys(&k, a) != std::cmp::Ordering::Greater) {
            continue;
        }
        if best
            .as_ref()
            .is_none_or(|b| compare_keys(&k, b) == std::cmp::Ordering::Less)
        {
            best = Some(k);
        }
    }
    Ok(best.map(|k| {
        let v = table.raw_get(&k);
        (k, v)
    }))
}

/// A fast string-keyed map for a table's named fields (`obj.id`, `obj[k]`).
///
/// Implemented as an open-addressing hash table with power-of-two capacity and
/// Knuth backward-shift deletion (zero tombstones). Keys store their precomputed
/// 64-bit hash, eliminating re-hashing across all lookups, mutations, and table growths.
#[derive(Clone, Debug)]
pub struct FieldEntry {
    pub hash: u64,
    pub key: StrRef,
    pub val: Value,
}

#[derive(Clone, Debug, Default)]
pub struct FieldMap {
    entries: Vec<Option<FieldEntry>>,
    count: usize,
}

impl FieldMap {
    #[inline]
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
            count: 0,
        }
    }

    #[inline]
    pub fn with_capacity(cap: usize) -> Self {
        if cap == 0 {
            Self::new()
        } else {
            let n = (cap * 4 / 3 + 1).next_power_of_two().max(4);
            let mut entries = Vec::with_capacity(n);
            entries.resize_with(n, || None);
            Self { entries, count: 0 }
        }
    }

    #[inline(always)]
    pub fn len(&self) -> usize {
        self.count
    }

    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.count == 0
    }

    #[inline]
    pub fn find_index_by_strref(&self, key: &StrRef) -> Option<usize> {
        if self.count == 0 {
            return None;
        }
        let mask = self.entries.len() - 1;
        let hash = key.0.hash;
        let mut idx = (hash as usize) & mask;
        loop {
            let entry = self.entries[idx].as_ref()?;
            if entry.hash == hash
                && (Rc::ptr_eq(&entry.key.0, &key.0) || entry.key.0.str == key.0.str)
            {
                return Some(idx);
            }
            idx = (idx + 1) & mask;
        }
    }

    #[inline]
    pub fn find_index(&self, key: &str) -> Option<usize> {
        if self.count == 0 {
            return None;
        }
        let mask = self.entries.len() - 1;
        let hash = crate::vm::hash::fx_hash_bytes(key.as_bytes());
        let mut idx = (hash as usize) & mask;
        loop {
            let entry = self.entries[idx].as_ref()?;
            if entry.hash == hash && entry.key.0.str == key {
                return Some(idx);
            }
            idx = (idx + 1) & mask;
        }
    }

    #[inline]
    pub fn get_by_strref(&self, key: &StrRef) -> Option<&Value> {
        let idx = self.find_index_by_strref(key)?;
        self.entries[idx].as_ref().map(|entry| &entry.val)
    }

    #[inline]
    pub fn get_mut_by_strref(&mut self, key: &StrRef) -> Option<&mut Value> {
        let idx = self.find_index_by_strref(key)?;
        self.entries[idx].as_mut().map(|entry| &mut entry.val)
    }

    #[inline]
    pub fn get(&self, key: &str) -> Option<&Value> {
        let idx = self.find_index(key)?;
        self.entries[idx].as_ref().map(|entry| &entry.val)
    }

    #[inline]
    pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
        let idx = self.find_index(key)?;
        self.entries[idx].as_mut().map(|entry| &mut entry.val)
    }

    #[inline]
    pub fn contains_key(&self, key: &str) -> bool {
        self.get(key).is_some()
    }

    pub fn insert(&mut self, key: StrRef, val: Value) -> Option<Value> {
        if self.entries.is_empty() || (self.count + 1) * 4 > self.entries.len() * 3 {
            self.grow();
        }
        let mask = self.entries.len() - 1;
        let hash = key.0.hash;
        let mut idx = (hash as usize) & mask;
        loop {
            match &mut self.entries[idx] {
                None => {
                    self.entries[idx] = Some(FieldEntry { hash, key, val });
                    self.count += 1;
                    return None;
                }
                Some(entry) => {
                    if entry.hash == hash
                        && (Rc::ptr_eq(&entry.key.0, &key.0) || entry.key.0.str == key.0.str)
                    {
                        return Some(std::mem::replace(&mut entry.val, val));
                    }
                    idx = (idx + 1) & mask;
                }
            }
        }
    }

    fn grow(&mut self) {
        let new_cap = (self.entries.len() * 2).max(4);
        let mut new_entries = Vec::with_capacity(new_cap);
        new_entries.resize_with(new_cap, || None);
        let new_mask = new_cap - 1;
        for entry in self.entries.drain(..).flatten() {
            let mut idx = (entry.hash as usize) & new_mask;
            while new_entries[idx].is_some() {
                idx = (idx + 1) & new_mask;
            }
            new_entries[idx] = Some(entry);
        }
        self.entries = new_entries;
    }

    pub fn remove(&mut self, key: &str) -> Option<Value> {
        if self.count == 0 {
            return None;
        }
        let mask = self.entries.len() - 1;
        let hash = crate::vm::hash::fx_hash_bytes(key.as_bytes());
        let mut idx = (hash as usize) & mask;
        let found = loop {
            match &self.entries[idx] {
                None => return None,
                Some(entry) if entry.hash == hash && entry.key.0.str == key => {
                    break idx;
                }
                _ => {
                    idx = (idx + 1) & mask;
                }
            }
        };
        let removed = self.entries[found].take().map(|e| e.val);
        self.count -= 1;
        let mut i = found;
        let mut j = (found + 1) & mask;
        while let Some(entry) = &self.entries[j] {
            let nat = (entry.hash as usize) & mask;
            let dist_to_nat = (j + mask + 1 - nat) & mask;
            let dist_to_i = (j + mask + 1 - i) & mask;
            if dist_to_i <= dist_to_nat {
                self.entries[i] = self.entries[j].take();
                i = j;
            }
            j = (j + 1) & mask;
        }
        removed
    }

    pub fn remove_by_strref(&mut self, key: &StrRef) -> Option<Value> {
        if self.count == 0 {
            return None;
        }
        let mask = self.entries.len() - 1;
        let hash = key.0.hash;
        let mut idx = (hash as usize) & mask;
        let found = loop {
            match &self.entries[idx] {
                None => return None,
                Some(entry)
                    if entry.hash == hash
                        && (Rc::ptr_eq(&entry.key.0, &key.0) || entry.key.0.str == key.0.str) =>
                {
                    break idx;
                }
                _ => {
                    idx = (idx + 1) & mask;
                }
            }
        };
        let removed = self.entries[found].take().map(|e| e.val);
        self.count -= 1;
        let mut i = found;
        let mut j = (found + 1) & mask;
        while let Some(entry) = &self.entries[j] {
            let nat = (entry.hash as usize) & mask;
            let dist_to_nat = (j + mask + 1 - nat) & mask;
            let dist_to_i = (j + mask + 1 - i) & mask;
            if dist_to_i <= dist_to_nat {
                self.entries[i] = self.entries[j].take();
                i = j;
            }
            j = (j + 1) & mask;
        }
        removed
    }

    #[inline]
    pub fn clear(&mut self) {
        self.entries.clear();
        self.count = 0;
    }

    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = (&StrRef, &Value)> {
        self.entries
            .iter()
            .filter_map(|opt| opt.as_ref().map(|e| (&e.key, &e.val)))
    }

    #[inline]
    pub fn values(&self) -> impl Iterator<Item = &Value> {
        self.entries
            .iter()
            .filter_map(|opt| opt.as_ref().map(|e| &e.val))
    }

    #[inline]
    pub fn values_mut(&mut self) -> impl Iterator<Item = &mut Value> {
        self.entries
            .iter_mut()
            .filter_map(|opt| opt.as_mut().map(|e| &mut e.val))
    }

    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&StrRef, &mut Value) -> bool,
    {
        if self.count == 0 {
            return;
        }
        let old_entries = std::mem::take(&mut self.entries);
        let cap = old_entries.len();
        self.entries = Vec::with_capacity(cap);
        self.entries.resize_with(cap, || None);
        self.count = 0;
        let mask = cap - 1;
        for mut entry in old_entries.into_iter().flatten() {
            if f(&entry.key, &mut entry.val) {
                let mut idx = (entry.hash as usize) & mask;
                while self.entries[idx].is_some() {
                    idx = (idx + 1) & mask;
                }
                self.entries[idx] = Some(entry);
                self.count += 1;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_table_key_conversions() {
        assert_eq!(
            TableKey::from_value(&Value::Bool(true)),
            Ok(TableKey::Bool(true))
        );
        assert_eq!(
            TableKey::from_value(&Value::Int(-42)),
            Ok(TableKey::Int(-42))
        );
        assert_eq!(
            TableKey::from_value(&Value::Float(10.0)),
            Ok(TableKey::Int(10))
        );
        assert!(TableKey::from_value(&Value::Nil).is_err());
        assert!(TableKey::from_value(&Value::Float(f64::NAN)).is_err());
    }

    #[test]
    fn test_field_map_crud_and_backward_shift() {
        let mut map = FieldMap::new();
        assert!(map.is_empty());
        assert_eq!(map.len(), 0);

        // Insert keys
        for i in 0..50 {
            let key = StrRef::from(format!("field_{i}"));
            assert!(map.insert(key, Value::Int(i * 10)).is_none());
        }
        assert_eq!(map.len(), 50);

        // Read back
        for i in 0..50 {
            let key_str = format!("field_{i}");
            let key = StrRef::from(key_str.clone());
            assert_eq!(map.get_by_strref(&key), Some(&Value::Int(i * 10)));
            assert_eq!(map.get(&key_str), Some(&Value::Int(i * 10)));
        }

        // Overwrite
        let k0 = StrRef::from("field_0");
        assert_eq!(map.insert(k0.clone(), Value::Int(999)), Some(Value::Int(0)));
        assert_eq!(map.get_by_strref(&k0), Some(&Value::Int(999)));

        // Remove every second key
        for i in (0..50).step_by(2) {
            let key_str = format!("field_{i}");
            assert!(map.remove(&key_str).is_some());
            assert_eq!(map.get(&key_str), None);
        }
        assert_eq!(map.len(), 25);

        // Verify remaining odd keys are still reachable
        for i in (1..50).step_by(2) {
            let key_str = format!("field_{i}");
            let key = StrRef::from(key_str.clone());
            assert_eq!(map.get_by_strref(&key), Some(&Value::Int(i * 10)));
        }

        // Clear
        map.clear();
        assert!(map.is_empty());
        assert_eq!(map.len(), 0);
    }
}