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
use crate::data::Value;
use crate::data::exception::UncheckedException;
use crate::data::value_typed::{
    BOOL_TYPE_TAG,
    CHAR_TYPE_TAG,
    INT_TYPE_TAG,
    VALUE_TYPE_TAG_MASK,
    FLOAT_TYPE_TAG
};

#[inline(never)] pub unsafe fn cast_any_int(
    src: Value, dest: &mut Value
) -> Result<(), UncheckedException> {
    if !src.is_value() {
        return Err(UncheckedException::InvalidCastOp { dest_type: "int", src });
    }

    match src.vt_data.tag & (VALUE_TYPE_TAG_MASK as usize) {
        INT_TYPE_TAG => *dest = src,
        CHAR_TYPE_TAG => *dest = Value::new_int(src.vt_data.inner.char_value as u32 as i64),
        FLOAT_TYPE_TAG => *dest = Value::new_int(src.vt_data.inner.float_value as i64),
        BOOL_TYPE_TAG => *dest = Value::new_int(if src.vt_data.inner.bool_value { 1 } else { 0 }),
        _ => unreachable!()
    }

    Ok(())
}

#[inline(never)] pub unsafe fn cast_any_float(
    src: Value, dest: &mut Value
) -> Result<(), UncheckedException> {
    if !src.is_value() {
        return Err(UncheckedException::InvalidCastOp { dest_type: "float", src });
    }

    match src.vt_data.tag & (VALUE_TYPE_TAG_MASK as usize) {
        FLOAT_TYPE_TAG => *dest = src,
        INT_TYPE_TAG => *dest = Value::new_float(src.vt_data.inner.int_value as f64),
        _ => return Err(UncheckedException::InvalidCastOp { dest_type: "float", src })
    }

    Ok(())
}

#[inline(never)] pub unsafe fn cast_any_char(
    src: Value, dest: &mut Value
) -> Result<(), UncheckedException> {
    match src.vt_data.tag & (VALUE_TYPE_TAG_MASK as usize) {
        CHAR_TYPE_TAG => *dest = src,
        // should we even implement this?
        // INT_TYPE_TAG => *dest = Value::new_char(src.vt_data.inner.int_value as u32 as char),
        _ => return Err(UncheckedException::InvalidCastOp { dest_type: "char", src })
    }

    Ok(())
}

#[inline(never)] pub unsafe fn cast_any_bool(
    src: Value, dest: &mut Value
) -> Result<(), UncheckedException> {
    match src.vt_data.tag & (VALUE_TYPE_TAG_MASK as usize) {
        BOOL_TYPE_TAG => *dest = src,
        INT_TYPE_TAG => *dest = Value::new_bool(src.vt_data.inner.int_value != 0),
        _ => return Err(UncheckedException::InvalidCastOp { dest_type: "bool", src })
    }

    Ok(())
}