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
//! The `std::vec` module.

use crate::runtime::{Function, Protocol, Value, Vec};
use crate::{ContextError, Module};

/// Construct the `std::vec` module.
pub fn module() -> Result<Module, ContextError> {
    let mut module = Module::with_crate_item("std", &["vec"]);

    module.ty::<Vec>()?;

    module.function(&["Vec", "new"], Vec::new)?;
    module.inst_fn("clear", Vec::clear)?;
    module.inst_fn("clone", Vec::clone)?;
    module.inst_fn("extend", Vec::extend)?;
    module.inst_fn("get", vec_get)?;
    module.inst_fn("iter", Vec::into_iterator)?;
    module.inst_fn("len", Vec::len)?;
    module.inst_fn("pop", Vec::pop)?;
    module.inst_fn("push", Vec::push)?;
    module.inst_fn("remove", Vec::remove)?;
    module.inst_fn("sort_by", sort_by)?;
    module.inst_fn("insert", Vec::insert)?;
    module.inst_fn(Protocol::INTO_ITER, Vec::into_iterator)?;
    module.inst_fn(Protocol::INDEX_SET, Vec::set)?;

    // TODO: parameterize with generics.
    module.inst_fn("sort_int", sort_int)?;

    Ok(module)
}

/// Sort a vector of integers.
fn sort_int(vec: &mut Vec) {
    vec.sort_by(|a, b| match (a, b) {
        (Value::Integer(a), Value::Integer(b)) => a.cmp(b),
        // NB: fall back to sorting by address.
        _ => (a as *const _ as usize).cmp(&(b as *const _ as usize)),
    });
}

fn vec_get(vec: &Vec, index: usize) -> Option<Value> {
    vec.get(index).cloned()
}

fn sort_by(vec: &mut Vec, comparator: &Function) {
    vec.sort_by(|a, b| {
        comparator
            .call::<_, std::cmp::Ordering>((a, b))
            .expect("an ordering")
    })
}