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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::cell::Cell;
use std::collections::HashSet;
use smallvec::SmallVec;
use errors;
use value::Value;
use opcode::StackMapPattern;
use object_pool::ObjectPool;



fixed_array!(FixedArray32, 32);

pub struct CallStack {
    frames: Vec<Frame>,
    n_frames: usize,
    limit: Option<usize>
}

pub type FrameHandle = Frame;

// [unsafe]
// These fields are guaranteed to be accessed properly (as an implementation detail).
pub struct Frame {
    this: Cell<Value>,
    arguments: FixedArray32<Value>,
    locals: FixedArray32<Value>,
    pub(crate) exec_stack: FixedArray32<Value>
}

impl CallStack {
    pub fn new(len: usize) -> CallStack {
        let mut frames = Vec::with_capacity(len);
        for _ in 0..len {
            frames.push(Frame::new());
        }

        CallStack {
            frames: frames,
            n_frames: 1, // one 'initial' frame
            limit: None
        }
    }

    pub fn set_limit(&mut self, limit: usize) {
        self.limit = Some(limit);
    }

    pub fn push(&mut self) {
        if self.n_frames >= self.frames.len() {
            panic!(errors::VMError::from(errors::RuntimeError::new("Virtual stack overflow")));
        }
        if let Some(limit) = self.limit {
            if self.n_frames >= limit {
                panic!(errors::VMError::from(errors::RuntimeError::new("Maximum stack depth exceeded")));
            }
        }
        self.n_frames += 1;
    }

    pub fn pop(&mut self) {
        if self.n_frames <= 0 {
            panic!(errors::VMError::from(errors::RuntimeError::new("Virtual stack underflow")));
        }
        self.frames[self.n_frames - 1].reset();
        self.n_frames -= 1;
    }

    pub fn top(&self) -> &Frame {
        if self.n_frames <= 0 {
            panic!(errors::VMError::from(errors::RuntimeError::new("Virtual stack underflow")));
        }
        &self.frames[self.n_frames - 1]
    }

    pub fn collect_objects(&self) -> Vec<usize> {
        let mut objs = HashSet::new();
        for i in 0..self.n_frames {
            let frame = &self.frames[i];
            if let Value::Object(id) = frame.this.get() {
                objs.insert(id);
            }
            for i in 0..frame.arguments.len() {
                let v = frame.arguments.get(i).unwrap();
                if let Value::Object(id) = v {
                    objs.insert(id);
                }
            }
            for i in 0..frame.locals.len() {
                let v = frame.locals.get(i).unwrap();
                if let Value::Object(id) = v {
                    objs.insert(id);
                }
            }
            for i in 0..frame.exec_stack.len() {
                let v = frame.exec_stack.get(i).unwrap();
                if let Value::Object(id) = v {
                    objs.insert(id);
                }
            }
        }
        objs.into_iter().collect()
    }
}

impl Frame {
    pub fn new() -> Frame {
        Frame {
            this: Cell::new(Value::Null),
            arguments: FixedArray32::new(Value::Null),
            locals: FixedArray32::new(Value::Null),
            exec_stack: FixedArray32::new(Value::Null)
        }
    }

    fn reset(&self) {
        self.this.set(Value::Null);
        self.arguments.clear();
        self.locals.clear();
        self.exec_stack.clear();
    }

    pub fn init_with_arguments(&self, this: Value, args: &[Value]) {
        self.this.set(this);
        for arg in args {
            self.arguments.push(*arg);
        }
    }

    #[inline]
    pub fn push_exec(&self, obj: Value) {
        self.exec_stack.push(obj);
    }

    #[inline]
    pub fn pop_exec(&self) -> Value {
        self.exec_stack.pop()
    }

    #[inline]
    pub fn dup_exec(&self) {
        self.exec_stack.push(self.exec_stack.top());
    }

    pub fn map_exec(&self, p: &StackMapPattern, pool: &mut ObjectPool) {
        let mut new_values: SmallVec<[Value; 4]> = SmallVec::with_capacity(p.map.len());
        for loc in &p.map {
            new_values.push(loc.extract(self, pool));
        }

        if p.end_state < 0 {
            for _ in 0..(-p.end_state) {
                self.exec_stack.pop();
            }
        } else {
            for _ in 0..p.end_state {
                self.exec_stack.push(Value::Null);
            }
        }

        for i in 0..new_values.len() {
            let sv_id = self.exec_stack.len() - 1 - i;
            let nv_id = new_values.len() - 1 - i;
            self.exec_stack.set(sv_id, new_values[nv_id]);
        }
    }

    pub fn bulk_load(&self, values: &[Value]) {
        for v in values {
            self.exec_stack.push(*v);
        }
    }

    pub fn reset_locals(&self, n_slots: usize) {
        self.locals.clear();
        for _ in 0..n_slots {
            self.locals.push(Value::Null);
        }
    }

    #[inline]
    pub fn get_local(&self, ind: usize) -> Value {
        self.locals.get(ind).unwrap()
    }

    #[inline]
    pub fn set_local(&self, ind: usize, obj: Value) {
        self.locals.set(ind, obj);
    }

    #[inline]
    pub fn get_argument(&self, id: usize) -> Option<Value> {
        self.arguments.get(id)
    }

    #[inline]
    pub fn must_get_argument(&self, id: usize) -> Value {
        self.get_argument(id).unwrap_or_else(|| {
            panic!(errors::VMError::from(errors::RuntimeError::new("Argument index out of bound")))
        })
    }

    #[inline]
    pub fn get_n_arguments(&self) -> usize {
        self.arguments.len()
    }

    #[inline]
    pub fn get_this(&self) -> Value {
        self.this.get()
    }

    #[inline]
    pub fn set_this(&self, this: Value) {
        self.this.set(this);
    }
}