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
use super::*;

pub struct Frame {
    stack: Vec<Value>,
    locals: Vec<Value>,
    upvals: Vec<Rc<RefCell<Value>>>,
    pc: usize,
}

impl Frame {
    pub(super) fn new(
        nlocals: usize,
        bindings: Vec<Rc<RefCell<Value>>>,
        nownedvars: usize,
    ) -> Self {
        Self {
            stack: vec![],
            locals: {
                let mut vec = Vec::new();
                vec.resize_with(nlocals, || Value::Invalid);
                vec
            },
            upvals: {
                let mut vec = bindings;
                vec.resize_with(vec.len() + nownedvars, || {
                    Rc::new(RefCell::new(Value::Invalid))
                });
                vec
            },
            pc: 0,
        }
    }
    #[inline(always)]
    pub(super) fn len(&mut self) -> usize {
        self.stack.len()
    }
    #[inline(always)]
    pub(super) fn pull(&mut self, pos: usize) {
        let len = self.stack.len();
        let x = self.stack.remove(len - 1 - pos);
        self.stack.push(x);
    }
    #[inline(always)]
    pub(super) fn unpull(&mut self, pos: usize) {
        // aka, rotate
        // reposition the top of stack and shift all other elements
        // so that it ends up in the new given position
        let x = self.stack.pop().unwrap();
        let len = self.stack.len();
        self.stack.insert(len - pos, x);
    }
    #[inline(always)]
    pub(super) fn swap(&mut self, a: usize, b: usize) {
        let len = self.stack.len();
        self.stack.swap(len - 1 - a, len - 1 - b);
    }
    #[inline(always)]
    pub(super) fn push(&mut self, value: Value) {
        self.stack.push(value);
    }
    #[inline(always)]
    pub(super) fn pop(&mut self) -> Value {
        self.stack.pop().unwrap()
    }
    #[inline(always)]
    pub(super) fn peek(&self) -> &Value {
        self.stack.last().unwrap()
    }
    #[inline(always)]
    pub(super) fn peekn(&self, n: usize) -> Vec<Value> {
        let len = self.stack.len();
        self.stack[len - n..].to_vec()
    }
    #[inline(always)]
    pub(super) fn popn(&mut self, n: usize) -> Vec<Value> {
        let len = self.stack.len();
        self.stack.drain(len - n..).collect()
    }
    #[inline(always)]
    pub(super) fn popn_iter<'a>(&'a mut self, n: usize) -> impl Iterator<Item = Value> + 'a {
        let len = self.stack.len();
        self.stack.drain(len - n..)
    }
    #[inline(always)]
    pub(super) fn pushn<I: IntoIterator<Item = Value>>(&mut self, vec: I) {
        self.stack.extend(vec);
    }
    #[inline(always)]
    pub(super) fn setvar(&mut self, var: &Variable, value: Value) {
        match var.type_() {
            VariableType::Local => self.locals[var.slot()] = value,
            VariableType::Upval => {
                self.upvals[var.slot()].replace(value);
            }
        }
    }
    #[inline(always)]
    pub(super) fn getcell(&self, slot: usize) -> Rc<RefCell<Value>> {
        self.upvals[slot].clone()
    }
    #[inline(always)]
    pub(super) fn getcells(&self) -> &Vec<Rc<RefCell<Value>>> {
        &self.upvals
    }
    #[inline(always)]
    pub(super) fn getcells_mut(&mut self) -> &mut Vec<Rc<RefCell<Value>>> {
        &mut self.upvals
    }
    #[inline(always)]
    pub(super) fn getvar(&self, var: &Variable) -> Result<Value> {
        let value = match var.type_() {
            VariableType::Local => self.locals[var.slot()].clone(),
            VariableType::Upval => self.upvals[var.slot()].borrow().clone(),
        };
        if let Value::Invalid = value {
            Err(rterr!("Variable {:?} used before being set", var.name()))
        } else {
            Ok(value)
        }
    }
    #[inline(always)]
    pub(super) fn delvar(&mut self, var: &Variable) -> Result<Value> {
        let value = match var.type_() {
            VariableType::Local => std::mem::replace(&mut self.locals[var.slot()], Value::Invalid),
            VariableType::Upval => self.upvals[var.slot()].replace(Value::Invalid),
        };
        if let Value::Invalid = value {
            Err(rterr!(
                "Variable {:?} used before being set (for del)",
                var.name()
            ))
        } else {
            Ok(value)
        }
    }
    #[inline(always)]
    pub(super) fn setargs(&mut self, params: &Vec<Variable>, args: Vec<Value>) {
        assert_eq!(params.len(), args.len());
        for (var, arg) in params.iter().zip(args) {
            self.setvar(var, arg);
        }
    }
    #[inline(always)]
    pub(super) fn fetch<'a>(&mut self, code: &'a Code) -> (usize, Option<&'a Opcode>) {
        let pc = self.pc;
        let opc = code.ops().get(pc);
        self.pc += 1;
        (pc, opc)
    }
    #[inline(always)]
    pub(super) fn jump(&mut self, pc: usize) {
        self.pc = pc;
    }
}