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
use executor::{ExecuteResult, ExecuteError};
use module::ValType;
use fp_ops;

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum Value {
    Undef,
    I32(i32),
    I64(i64),
    F32(f32),
    F64(f64)
}

impl Default for Value {
    fn default() -> Value {
        Value::Undef
    }
}

impl Value {
    pub fn get_i32(&self) -> ExecuteResult<i32> {
        match *self {
            Value::Undef => Ok(0),
            Value::I32(v) => Ok(v),
            _ => {
                //panic!();
                Err(ExecuteError::ValueTypeMismatch)
            }
        }
    }

    pub fn get_i64(&self) -> ExecuteResult<i64> {
        match *self {
            Value::Undef => Ok(0),
            Value::I64(v) => Ok(v),
            _ => {
                //panic!();
                Err(ExecuteError::ValueTypeMismatch)
            }
        }
    }

    pub fn cast_to_i64(&self) -> i64 {
        match *self {
            Value::Undef => 0,
            Value::I32(v) => v as i64,
            Value::I64(v) => v,
            Value::F32(v) => v as i64,
            Value::F64(v) => v as i64
        }
    }

    pub fn reinterpret_as_i64(&self) -> i64 {
        unsafe {
            match *self {
                Value::Undef => 0,
                Value::I32(v) => v as u32 as u64 as i64,
                Value::I64(v) => v,
                Value::F32(v) => ::prelude::mem::transmute::<f32, u32>(v) as u64 as i64,
                Value::F64(v) => ::prelude::mem::transmute(v)
            }
        }
    }

    pub fn reinterpret_from_i64(v: i64, ty: ValType) -> Value {
        unsafe {
            match ty {
                ValType::I32 => Value::I32(v as i32),
                ValType::I64 => Value::I64(v),
                ValType::F32 => Value::F32(::std::mem::transmute(v as u64 as u32)),
                ValType::F64 => Value::F64(::std::mem::transmute(v))
            }
        }
    }
}