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
use std::fmt;
use parking_lot::RwLock;
use elements::{GlobalType, ValueType, TableElementType};
use interpreter::Error;
use interpreter::value::RuntimeValue;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VariableType {
AnyFunc,
I32,
I64,
F32,
F64,
}
pub trait ExternalVariableValue {
fn get(&self) -> RuntimeValue;
fn set(&mut self, value: RuntimeValue) -> Result<(), Error>;
}
#[derive(Debug)]
pub struct VariableInstance {
is_mutable: bool,
variable_type: VariableType,
value: RwLock<VariableValue>,
}
enum VariableValue {
Internal(RuntimeValue),
External(Box<ExternalVariableValue>),
}
impl VariableInstance {
pub fn new(is_mutable: bool, variable_type: VariableType, value: RuntimeValue) -> Result<Self, Error> {
if !value.is_null() && value.variable_type() != Some(variable_type) {
return Err(Error::Variable(format!("trying to initialize variable of type {:?} with value of type {:?}", variable_type, value.variable_type())));
}
Ok(VariableInstance {
is_mutable: is_mutable,
variable_type: variable_type,
value: RwLock::new(VariableValue::Internal(value)),
})
}
pub fn new_global(global_type: &GlobalType, value: RuntimeValue) -> Result<Self, Error> {
Self::new(global_type.is_mutable(), global_type.content_type().into(), value)
}
pub fn new_external_global(is_mutable: bool, variable_type: VariableType, value: Box<ExternalVariableValue>) -> Result<Self, Error> {
let current_value = value.get();
if !current_value.is_null() && current_value.variable_type() != Some(variable_type) {
return Err(Error::Variable(format!("trying to initialize variable of type {:?} with value of type {:?}", variable_type, current_value.variable_type())));
}
Ok(VariableInstance {
is_mutable: is_mutable,
variable_type: variable_type,
value: RwLock::new(VariableValue::External(value)),
})
}
pub fn is_mutable(&self) -> bool {
self.is_mutable
}
pub fn variable_type(&self) -> VariableType {
self.variable_type
}
pub fn get(&self) -> RuntimeValue {
self.value.read().get()
}
pub fn set(&self, value: RuntimeValue) -> Result<(), Error> {
if !self.is_mutable {
return Err(Error::Variable("trying to update immutable variable".into()));
}
if value.variable_type() != Some(self.variable_type) {
return Err(Error::Variable(format!("trying to update variable of type {:?} with value of type {:?}", self.variable_type, value.variable_type())));
}
self.value.write().set(value)
}
}
impl VariableValue {
fn get(&self) -> RuntimeValue {
match *self {
VariableValue::Internal(ref value) => value.clone(),
VariableValue::External(ref value) => value.get(),
}
}
fn set(&mut self, new_value: RuntimeValue) -> Result<(), Error> {
match *self {
VariableValue::Internal(ref mut value) => {
*value = new_value;
Ok(())
},
VariableValue::External(ref mut value) => value.set(new_value),
}
}
}
impl From<ValueType> for VariableType {
fn from(vt: ValueType) -> VariableType {
match vt {
ValueType::I32 => VariableType::I32,
ValueType::I64 => VariableType::I64,
ValueType::F32 => VariableType::F32,
ValueType::F64 => VariableType::F64,
}
}
}
impl From<TableElementType> for VariableType {
fn from(tt: TableElementType) -> VariableType {
match tt {
TableElementType::AnyFunc => VariableType::AnyFunc,
}
}
}
impl fmt::Debug for VariableValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
VariableValue::Internal(ref value) => write!(f, "Variable.Internal({:?})", value),
VariableValue::External(ref value) => write!(f, "Variable.External({:?})", value.get()),
}
}
}