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

use caml_ffi_types::{Cint, Clong, Value};

extern {
    pub fn long_val(l: Value) -> Clong;
    pub fn int_val(i: Value) -> Cint;
    pub fn val_long(l: Clong) -> Value;
    pub fn val_int(i: Cint) -> Value;
    pub fn val_true() -> Value;
    pub fn val_false() -> Value;
    pub fn val_unit() -> Value;
}

// Primitives

pub fn caml_unit() -> Value {
    unsafe { val_unit() }
}

pub fn caml_false() -> Value {
    unsafe { val_false() }
}

pub fn caml_true() -> Value {
    unsafe { val_true() }
}

pub fn caml_to_int(i: Value) -> Cint {
    unsafe { int_val(i) }
}

pub fn caml_of_int(i: Cint) -> Value {
    unsafe { val_int(i) }
}

pub fn caml_to_long(l: Value) -> Clong {
    unsafe { long_val(l) }
}

pub fn caml_of_long(l: Clong) -> Value {
    unsafe { val_long(l) }
}

// Helpers

fn is_block(x: Value) -> bool {
    (x & 1) == 0
}

fn hd_val(x: Value) -> usize {
    assert!(is_block(x));
    unsafe {
	*(x as *const usize).offset(-1)
    }
}

pub fn tag_of_value(x: Value) -> u8 {
    assert!(is_block(x));
    (hd_val(x) & 0xff) as u8
}