hexicon-vm 0.3.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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! The list built-in class, and the sequence operations it shares with
//! vectors.
//!
//! A list never changes. Every method that "modifies" a list returns a new
//! one.

use std::cmp::Ordering;

use crate::error::{VmError, VmResult, code};
use crate::meta::MetaKind;
use crate::value::{NativeDesc, Value};
use crate::vm::{NativeCall, Vm};

/// Method numbers of the list class.
mod method {
    pub const SUBSET: u16 = 1;
    pub const MAP_ALL: u16 = 2;
    pub const LENGTH: u16 = 3;
    pub const SUBLIST: u16 = 4;
    pub const INTERSECT: u16 = 5;
    pub const INDEX_OF: u16 = 6;
    pub const INDEX_WHICH: u16 = 7;
    pub const FOR_EACH: u16 = 8;
    pub const VAL_WHICH: u16 = 9;
    pub const LAST_INDEX_OF: u16 = 10;
    pub const LAST_INDEX_WHICH: u16 = 11;
    pub const LAST_VAL_WHICH: u16 = 12;
    pub const COUNT_OF: u16 = 13;
    pub const COUNT_WHICH: u16 = 14;
    pub const GET_UNIQUE: u16 = 15;
    pub const APPEND_UNIQUE: u16 = 16;
    pub const APPEND: u16 = 17;
    pub const SORT: u16 = 18;
    pub const PREPEND: u16 = 19;
    pub const INSERT_AT: u16 = 20;
    pub const REMOVE_ELEMENT_AT: u16 = 21;
    pub const REMOVE_RANGE: u16 = 22;
    pub const FOR_EACH_ASSOC: u16 = 23;
    pub const GENERATE: u16 = 24;
    pub const SPLICE: u16 = 25;
    pub const JOIN: u16 = 26;
    pub const INDEX_OF_MIN: u16 = 27;
    pub const MIN_VAL: u16 = 28;
    pub const INDEX_OF_MAX: u16 = 29;
    pub const MAX_VAL: u16 = 30;
}

/// The name and calling interface of each list method, by method number.
const METHODS: &[(&str, NativeDesc)] = &[
    ("", NativeDesc::fixed(0)),
    ("subset", NativeDesc::fixed(1)),
    ("mapAll", NativeDesc::fixed(1)),
    ("length", NativeDesc::fixed(0)),
    ("sublist", NativeDesc::optional(1, 1)),
    ("intersect", NativeDesc::fixed(1)),
    ("indexOf", NativeDesc::fixed(1)),
    ("indexWhich", NativeDesc::fixed(1)),
    ("forEach", NativeDesc::fixed(1)),
    ("valWhich", NativeDesc::fixed(1)),
    ("lastIndexOf", NativeDesc::fixed(1)),
    ("lastIndexWhich", NativeDesc::fixed(1)),
    ("lastValWhich", NativeDesc::fixed(1)),
    ("countOf", NativeDesc::fixed(1)),
    ("countWhich", NativeDesc::fixed(1)),
    ("getUnique", NativeDesc::fixed(0)),
    ("appendUnique", NativeDesc::fixed(1)),
    ("append", NativeDesc::fixed(1)),
    ("sort", NativeDesc::optional(0, 2)),
    ("prepend", NativeDesc::fixed(1)),
    ("insertAt", NativeDesc::variable(2)),
    ("removeElementAt", NativeDesc::fixed(1)),
    ("removeRange", NativeDesc::fixed(2)),
    ("forEachAssoc", NativeDesc::fixed(1)),
    // `generate` is a static method; on an instance it is undefined.
    ("generate", NativeDesc::fixed(2)),
    ("splice", NativeDesc::variable(2)),
    ("join", NativeDesc::optional(0, 1)),
    ("indexOfMin", NativeDesc::optional(0, 1)),
    ("minVal", NativeDesc::optional(0, 1)),
    ("indexOfMax", NativeDesc::optional(0, 1)),
    ("maxVal", NativeDesc::optional(0, 1)),
];

/// Whether a callback's result counts as true.
pub(crate) fn is_true(value: Value) -> bool {
    !matches!(value, Value::Nil | Value::Int(0))
}

impl Vm {
    /// Describe a native method call for stack traces and error messages.
    /// The method's arguments are still on the stack.
    pub(crate) fn native_call(
        &self,
        name: &'static str,
        self_value: Value,
        method_index: u16,
        argc: u32,
    ) -> NativeCall {
        NativeCall {
            name,
            function: None,
            self_value,
            method_index,
            args: (0..argc as usize).map(|i| self.arg(i)).collect(),
            caller_pc: self.pc,
            arg_top: self.stack.len(),
        }
    }

    /// Call a method of the list class on a list constant or object.
    pub(crate) fn list_method(
        &mut self,
        self_value: Value,
        index: u16,
        argc: Option<&mut u32>,
    ) -> VmResult<Option<Value>> {
        if index == 0 || index == method::GENERATE {
            return Ok(None);
        }
        let Some(&(_, desc)) = METHODS.get(index as usize) else {
            return Ok(None);
        };
        self.native_method(argc, desc, |vm, argc| vm.list_call(self_value, index, argc))
            .map(Some)
    }

    /// The static methods of the list class.
    pub(crate) fn list_static_method(
        &mut self,
        index: u16,
        argc: Option<&mut u32>,
    ) -> VmResult<Option<Value>> {
        if index != method::GENERATE {
            return Ok(None);
        }
        // List.generate(func, n)
        self.native_method(argc, NativeDesc::fixed(2), |vm, argc| {
            let class = Value::obj_or_nil(vm.meta.class_obj(MetaKind::List));
            let native = vm.native_call("List.generate", class, method::GENERATE, argc);
            let items = vm.generate(vm.arg(0), vm.arg(1), &native)?;
            Ok(vm.new_list(items))
        })
        .map(Some)
    }

    /// Build a sequence by calling a function for each index from 1 to a
    /// count, for the `generate` methods.
    pub(crate) fn generate(
        &mut self,
        function: Value,
        count: Value,
        native: &NativeCall,
    ) -> VmResult<Vec<Value>> {
        let Some((min, opt, varargs)) = self.value_interface(function) else {
            return Err(VmError::new(code::BAD_TYPE_BIF));
        };
        if !self.is_function_value(function) {
            return Err(VmError::new(code::BAD_TYPE_BIF));
        }
        let Value::Int(count) = count else {
            return Err(VmError::new(code::NUM_VAL_REQD));
        };
        if count < 0 {
            return Err(VmError::new(code::BAD_VAL_BIF));
        }
        // The function gets the index if it takes an argument.
        let takes_index = varargs || min + opt >= 1;
        let mut items = Vec::with_capacity(count as usize);
        for index in 1..=count {
            let args: &[Value] = if takes_index {
                &[Value::Int(index)]
            } else {
                &[]
            };
            items.push(self.call_value(function, args, native.clone())?);
        }
        Ok(items)
    }

    pub(crate) fn list_items(&self, list: Value) -> VmResult<Vec<Value>> {
        self.as_list(list)
            .map(|items| items.to_vec())
            .ok_or_else(|| VmError::new(code::LIST_VAL_REQD))
    }

    fn list_call(&mut self, self_value: Value, index: u16, argc: u32) -> VmResult<Value> {
        let items = self.list_items(self_value)?;
        let name = METHODS[index as usize].0;
        // The description of this call that a callback's stack trace shows.
        let native = |vm: &Vm, name: &'static str| vm.native_call(name, self_value, index, argc);
        Ok(match index {
            // These make their result first, as a copy of the list, and
            // fill it in, as the reference does, so the objects the
            // callback makes are numbered after it.
            method::SUBSET => {
                let native = native(self, "List.subset");
                let result = self.new_list(items.clone());
                let kept = self.seq_subset(&items, self.arg(0), &native)?;
                self.objects
                    .replace(result.obj_id(), crate::object::Object::List(kept.into()));
                result
            }
            method::MAP_ALL => {
                let native = native(self, "List.mapAll");
                let result = self.new_list(items.clone());
                let function = self.arg(0);
                let mut mapped = Vec::with_capacity(items.len());
                for &item in &items {
                    mapped.push(self.call_value(function, &[item], native.clone())?);
                }
                self.objects
                    .replace(result.obj_id(), crate::object::Object::List(mapped.into()));
                result
            }
            method::LENGTH => Value::Int(items.len() as i32),
            method::SUBLIST => {
                let old_count = items.len() as i32;
                let mut start = self.int_arg(self.arg(0))?;
                if start < 0 {
                    start += old_count + 1;
                }
                let len = if argc >= 2 {
                    self.int_arg(self.arg(1))?
                } else {
                    old_count
                };
                let in_range = start >= 1 && start <= old_count;
                let mut new_count = if in_range { old_count - (start - 1) } else { 0 };
                if len < 0 {
                    new_count += len;
                } else if len < new_count {
                    new_count = len;
                }
                let new_count = new_count.max(0) as usize;
                let first = if in_range { start as usize - 1 } else { 0 };
                self.new_list(&items[first..first + new_count])
            }
            method::INTERSECT => {
                let common = self.seq_intersect(self_value, self.arg(0))?;
                self.new_list(common)
            }
            method::INDEX_OF => match self.seq_index_of(&items, self.arg(0), true)? {
                Some(found) => Value::Int(found as i32 + 1),
                None => Value::Nil,
            },
            method::INDEX_WHICH | method::LAST_INDEX_WHICH => {
                let forward = index == method::INDEX_WHICH;
                let native = native(
                    self,
                    if forward {
                        "List.indexWhich"
                    } else {
                        "List.lastIndexWhich"
                    },
                );
                match self.seq_index_which(&items, self.arg(0), forward, &native)? {
                    Some(found) => Value::Int(found as i32 + 1),
                    None => Value::Nil,
                }
            }
            method::VAL_WHICH | method::LAST_VAL_WHICH => {
                // These report themselves as the index methods they are
                // built on.
                let forward = index == method::VAL_WHICH;
                let native = self.native_call(
                    if forward {
                        "List.indexWhich"
                    } else {
                        "List.lastIndexWhich"
                    },
                    self_value,
                    if forward {
                        method::INDEX_WHICH
                    } else {
                        method::LAST_INDEX_WHICH
                    },
                    argc,
                );
                match self.seq_index_which(&items, self.arg(0), forward, &native)? {
                    Some(found) => items[found],
                    None => Value::Nil,
                }
            }
            method::FOR_EACH | method::FOR_EACH_ASSOC => {
                let with_index = index == method::FOR_EACH_ASSOC;
                let native = native(
                    self,
                    if with_index {
                        "List.forEachAssoc"
                    } else {
                        "List.forEach"
                    },
                );
                let function = self.arg(0);
                for (position, &item) in items.iter().enumerate() {
                    if with_index {
                        let key = Value::Int(position as i32 + 1);
                        self.call_value(function, &[key, item], native.clone())?;
                    } else {
                        self.call_value(function, &[item], native.clone())?;
                    }
                }
                Value::Nil
            }
            method::LAST_INDEX_OF => match self.seq_index_of(&items, self.arg(0), false)? {
                Some(found) => Value::Int(found as i32 + 1),
                None => Value::Nil,
            },
            method::COUNT_OF => {
                let wanted = self.arg(0);
                let mut count = 0;
                for &item in &items {
                    if self.equals(item, wanted)? {
                        count += 1;
                    }
                }
                Value::Int(count)
            }
            method::COUNT_WHICH => {
                let native = native(self, "List.countWhich");
                let function = self.arg(0);
                let mut count = 0;
                for &item in &items {
                    if is_true(self.call_value(function, &[item], native.clone())?) {
                        count += 1;
                    }
                }
                Value::Int(count)
            }
            method::GET_UNIQUE => {
                let unique = self.seq_unique(&items)?;
                self.new_list(unique)
            }
            method::APPEND_UNIQUE => {
                let other = self.arg(0);
                let Some(len) = self.list_like_len(other)? else {
                    return Err(VmError::new(code::LIST_VAL_REQD));
                };
                let mut all = items;
                for position in 1..=len {
                    all.push(self.list_like_get(other, position)?);
                }
                let unique = self.seq_unique(&all)?;
                self.new_list(unique)
            }
            method::APPEND => {
                let mut items = items;
                items.push(self.arg(0));
                self.new_list(items)
            }
            method::PREPEND => {
                let mut items = items;
                items.insert(0, self.arg(0));
                self.new_list(items)
            }
            method::INSERT_AT => {
                let mut at = self.int_arg(self.arg(0))?;
                if at <= 0 {
                    at += items.len() as i32 + 1;
                }
                at -= 1;
                if at < 0 || at as usize > items.len() {
                    return Err(VmError::new(code::INDEX_OUT_OF_RANGE));
                }
                let mut items = items;
                let new: Vec<Value> = (1..argc as usize).map(|i| self.arg(i)).collect();
                items.splice(at as usize..at as usize, new);
                self.new_list(items)
            }
            method::REMOVE_ELEMENT_AT => {
                let mut at = self.int_arg(self.arg(0))?;
                if at < 0 {
                    at += items.len() as i32 + 1;
                }
                at -= 1;
                if at < 0 || at as usize >= items.len() {
                    return Err(VmError::new(code::INDEX_OUT_OF_RANGE));
                }
                let mut items = items;
                items.remove(at as usize);
                self.new_list(items)
            }
            method::REMOVE_RANGE => {
                let (start, end) = seq_range(
                    self.int_arg(self.arg(0))?,
                    self.int_arg(self.arg(1))?,
                    items.len(),
                )?;
                let mut items = items;
                items.drain(start..=end);
                self.new_list(items)
            }
            method::SORT => {
                let descending = argc >= 1 && !self.arg(0).is_nil();
                let compare = if argc >= 2 { self.arg(1) } else { Value::Nil };
                let native = native(self, "List.sort");
                let mut items = items;
                self.seq_sort(&mut items, compare, descending, &native)?;
                self.new_list(items)
            }
            method::SPLICE => {
                let count = items.len() as i32;
                let mut start = self.int_arg(self.arg(0))?;
                let delete = self.int_arg(self.arg(1))?;
                if start <= 0 {
                    start += count + 1;
                }
                let start = (start - 1).clamp(0, count);
                let delete = delete.clamp(0, count - start);
                let new: Vec<Value> = (2..argc as usize).map(|i| self.arg(i)).collect();
                if delete == 0 && new.is_empty() {
                    // Nothing changes, so the result is the same list.
                    return Ok(self_value);
                }
                let mut items = items;
                items.splice(start as usize..(start + delete) as usize, new);
                self.new_list(items)
            }
            method::JOIN => {
                let separator = match argc >= 1 && !self.arg(0).is_nil() {
                    true => match self.as_str(self.arg(0)) {
                        Some(text) => text.to_vec(),
                        None => return Err(VmError::new(code::BAD_TYPE_BIF)),
                    },
                    false => Vec::new(),
                };
                let text = self.seq_join(&items, &separator)?;
                self.try_new_string(text)?
            }
            method::INDEX_OF_MIN | method::MIN_VAL | method::INDEX_OF_MAX | method::MAX_VAL => {
                let native = native(self, name_for_minmax(index));
                let callback = if argc >= 1 { Some(self.arg(0)) } else { None };
                let wanted = if index == method::INDEX_OF_MIN || index == method::MIN_VAL {
                    Ordering::Less
                } else {
                    Ordering::Greater
                };
                let (position, value) = self.seq_minmax(&items, callback, wanted, &native)?;
                if index == method::INDEX_OF_MIN || index == method::INDEX_OF_MAX {
                    Value::Int(position as i32 + 1)
                } else {
                    value
                }
            }
            _ => {
                return Err(VmError::unimplemented(format!("List.{name}")));
            }
        })
    }

    // ---- operations shared by lists and vectors ----

    /// The elements for which a callback returns true.
    pub(crate) fn seq_subset(
        &mut self,
        items: &[Value],
        function: Value,
        native: &NativeCall,
    ) -> VmResult<Vec<Value>> {
        let mut kept = Vec::new();
        for &item in items {
            if is_true(self.call_value(function, &[item], native.clone())?) {
                kept.push(item);
            }
        }
        Ok(kept)
    }

    /// The index of the first or last element equal to a value.
    pub(crate) fn seq_index_of(
        &mut self,
        items: &[Value],
        wanted: Value,
        forward: bool,
    ) -> VmResult<Option<usize>> {
        let mut order: Box<dyn Iterator<Item = usize>> = if forward {
            Box::new(0..items.len())
        } else {
            Box::new((0..items.len()).rev())
        };
        for index in &mut order {
            if self.equals(items[index], wanted)? {
                return Ok(Some(index));
            }
        }
        Ok(None)
    }

    /// The index of the first or last element for which a callback
    /// returns true.
    pub(crate) fn seq_index_which(
        &mut self,
        items: &[Value],
        function: Value,
        forward: bool,
        native: &NativeCall,
    ) -> VmResult<Option<usize>> {
        let mut order: Box<dyn Iterator<Item = usize>> = if forward {
            Box::new(0..items.len())
        } else {
            Box::new((0..items.len()).rev())
        };
        for index in &mut order {
            if is_true(self.call_value(function, &[items[index]], native.clone())?) {
                return Ok(Some(index));
            }
        }
        Ok(None)
    }

    /// The elements with later duplicates removed.
    pub(crate) fn seq_unique(&mut self, items: &[Value]) -> VmResult<Vec<Value>> {
        let mut unique: Vec<Value> = Vec::with_capacity(items.len());
        for &item in items {
            let mut found = false;
            for &kept in &unique {
                if self.equals(item, kept)? {
                    found = true;
                    break;
                }
            }
            if !found {
                unique.push(item);
            }
        }
        Ok(unique)
    }

    /// The elements two list-like values have in common. The result
    /// follows the order of the shorter of the two.
    pub(crate) fn seq_intersect(&mut self, a: Value, b: Value) -> VmResult<Vec<Value>> {
        let len_a = self.list_like_len(a)?.unwrap_or(0);
        let len_b = self.list_like_len(b)?.unwrap_or(0);
        let (short, short_len, long, long_len) = if len_a > len_b {
            (b, len_b, a, len_a)
        } else {
            (a, len_a, b, len_b)
        };
        let mut long_items = Vec::with_capacity(long_len);
        for index in 1..=long_len {
            long_items.push(self.list_like_get(long, index)?);
        }
        let mut common = Vec::new();
        for index in 1..=short_len {
            let item = self.list_like_get(short, index)?;
            for &other in &long_items {
                if self.equals(other, item)? {
                    common.push(item);
                    break;
                }
            }
        }
        Ok(common)
    }

    /// Join the text of the elements with a separator.
    pub(crate) fn seq_join(&mut self, items: &[Value], separator: &[u8]) -> VmResult<Vec<u8>> {
        let mut text = Vec::new();
        for (index, &item) in items.iter().enumerate() {
            if index > 0 {
                text.extend_from_slice(separator);
            }
            match item {
                Value::Nil => {}
                Value::Int(n) => text.extend_from_slice(n.to_string().as_bytes()),
                _ => match self.cast_to_text(item) {
                    Ok(piece) => text.extend_from_slice(&piece),
                    Err(VmError::Runtime(error)) if error.code == code::NO_STR_CONV => {
                        return Err(VmError::new(code::NO_STR_CONV));
                    }
                    Err(error) => return Err(error),
                },
            }
        }
        Ok(text)
    }

    /// The smallest or largest element, or the element for which a
    /// callback returns the smallest or largest value. Returns its index
    /// and the winning value.
    pub(crate) fn seq_minmax(
        &mut self,
        items: &[Value],
        callback: Option<Value>,
        wanted: Ordering,
        native: &NativeCall,
    ) -> VmResult<(usize, Value)> {
        if let Some(callback) = callback
            && !self.is_function_value(callback)
        {
            return Err(VmError::new(code::BAD_TYPE_BIF));
        }
        if items.is_empty() {
            return Err(VmError::new(code::BAD_VAL_BIF));
        }
        let mut winner = (0, Value::Nil);
        for (index, &item) in items.iter().enumerate() {
            let value = match callback {
                Some(callback) => self.call_value(callback, &[item], native.clone())?,
                None => item,
            };
            if index == 0 || self.compare(value, winner.1)? == wanted {
                winner = (index, value);
            }
        }
        Ok(winner)
    }

    /// Sort values in place.
    ///
    /// The algorithm is the reference VM's quicksort, step for step: it
    /// is not stable, so equal elements end up in an order that depends on
    /// exactly how the sort proceeds, and programs can see that order.
    pub(crate) fn seq_sort(
        &mut self,
        items: &mut [Value],
        compare: Value,
        descending: bool,
        native: &NativeCall,
    ) -> VmResult<()> {
        if items.len() > 1 {
            self.quicksort(items, 0, items.len() - 1, compare, descending, native)?;
        }
        Ok(())
    }

    fn sort_compare(
        &mut self,
        a: Value,
        b: Value,
        compare: Value,
        descending: bool,
        native: &NativeCall,
    ) -> VmResult<Ordering> {
        let order = if compare.is_nil() {
            self.compare(a, b)?
        } else {
            match self.call_value(compare, &[a, b], native.clone())? {
                Value::Int(n) => n.cmp(&0),
                _ => return Err(VmError::new(code::NUM_VAL_REQD)),
            }
        };
        Ok(if descending { order.reverse() } else { order })
    }

    fn quicksort(
        &mut self,
        items: &mut [Value],
        left: usize,
        right: usize,
        compare: Value,
        descending: bool,
        native: &NativeCall,
    ) -> VmResult<()> {
        if right <= left {
            return Ok(());
        }
        // The pivot starts as the last element and is tracked as the
        // exchanges move it. Indexes step outside the range by one, as in
        // the original, so the arithmetic wraps.
        let mut pivot = right;
        let mut i = left.wrapping_sub(1);
        let mut j = right;
        loop {
            loop {
                i = i.wrapping_add(1);
                if i == right
                    || self.sort_compare(items[i], items[pivot], compare, descending, native)?
                        != Ordering::Less
                {
                    break;
                }
            }
            loop {
                j = j.wrapping_sub(1);
                if j == left
                    || self.sort_compare(items[j], items[pivot], compare, descending, native)?
                        != Ordering::Greater
                {
                    break;
                }
            }
            items.swap(i, j);
            if pivot == i {
                pivot = j;
            } else if pivot == j {
                pivot = i;
            }
            if j <= i {
                break;
            }
        }
        items.swap(i, j);
        items.swap(i, right);
        if i > left {
            self.quicksort(items, left, i - 1, compare, descending, native)?;
        }
        if i < right {
            self.quicksort(items, i + 1, right, compare, descending, native)?;
        }
        Ok(())
    }
}

fn name_for_minmax(index: u16) -> &'static str {
    match index {
        method::INDEX_OF_MIN => "List.indexOfMin",
        method::MIN_VAL => "List.minVal",
        method::INDEX_OF_MAX => "List.indexOfMax",
        _ => "List.maxVal",
    }
}

/// Convert the one-based, possibly negative start and end indexes of a
/// range to zero-based ones, checking them against a length.
pub(crate) fn seq_range(start: i32, end: i32, len: usize) -> VmResult<(usize, usize)> {
    let len = len as i32;
    let start = if start < 0 { start + len + 1 } else { start } - 1;
    let end = if end < 0 { end + len + 1 } else { end } - 1;
    if start < 0 || start >= len || end < 0 || end >= len || end < start {
        return Err(VmError::new(code::INDEX_OUT_OF_RANGE));
    }
    Ok((start as usize, end as usize))
}