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
217
218
219
220
221
222
223
224
225
226
227
//! Operator execution.
#[cfg(test)]
mod tests;

use super::{Executable, Interpreter};
use crate::{
    builtins::value::{ResultValue, Value},
    environment::lexical_environment::VariableScope,
    syntax::ast::{
        node::{Assign, BinOp, Node, UnaryOp},
        op::{self, AssignOp, BitOp, CompOp, LogOp, NumOp},
    },
    BoaProfiler,
};

impl Executable for Assign {
    fn run(&self, interpreter: &mut Interpreter) -> ResultValue {
        let _timer = BoaProfiler::global().start_event("Assign", "exec");
        let val = self.rhs().run(interpreter)?;
        match self.lhs() {
            Node::Identifier(ref name) => {
                let environment = &mut interpreter.realm_mut().environment;

                if environment.has_binding(name.as_ref()) {
                    // Binding already exists
                    environment.set_mutable_binding(name.as_ref(), val.clone(), true);
                } else {
                    environment.create_mutable_binding(
                        name.as_ref().to_owned(),
                        true,
                        VariableScope::Function,
                    );
                    environment.initialize_binding(name.as_ref(), val.clone());
                }
            }
            Node::GetConstField(ref get_const_field) => {
                let val_obj = get_const_field.obj().run(interpreter)?;
                val_obj.set_field(get_const_field.field(), val.clone());
            }
            Node::GetField(ref get_field) => {
                let val_obj = get_field.obj().run(interpreter)?;
                let val_field = get_field.field().run(interpreter)?;
                val_obj.set_field(val_field, val.clone());
            }
            _ => (),
        }
        Ok(val)
    }
}

impl Executable for BinOp {
    fn run(&self, interpreter: &mut Interpreter) -> ResultValue {
        match self.op() {
            op::BinOp::Num(op) => {
                let v_a = self.lhs().run(interpreter)?;
                let v_b = self.rhs().run(interpreter)?;
                Ok(match op {
                    NumOp::Add => v_a + v_b,
                    NumOp::Sub => v_a - v_b,
                    NumOp::Mul => v_a * v_b,
                    NumOp::Exp => v_a.as_num_to_power(v_b),
                    NumOp::Div => v_a / v_b,
                    NumOp::Mod => v_a % v_b,
                })
            }
            op::BinOp::Bit(op) => {
                let v_a = self.lhs().run(interpreter)?;
                let v_b = self.rhs().run(interpreter)?;
                Ok(match op {
                    BitOp::And => v_a & v_b,
                    BitOp::Or => v_a | v_b,
                    BitOp::Xor => v_a ^ v_b,
                    BitOp::Shl => v_a << v_b,
                    BitOp::Shr => v_a >> v_b,
                    // TODO Fix
                    BitOp::UShr => v_a >> v_b,
                })
            }
            op::BinOp::Comp(op) => {
                let v_a = self.lhs().run(interpreter)?;
                let v_b = self.rhs().run(interpreter)?;
                Ok(Value::from(match op {
                    CompOp::Equal => v_a.equals(&v_b, interpreter)?,
                    CompOp::NotEqual => !v_a.equals(&v_b, interpreter)?,
                    CompOp::StrictEqual => v_a.strict_equals(&v_b),
                    CompOp::StrictNotEqual => !v_a.strict_equals(&v_b),
                    CompOp::GreaterThan => v_a.to_number() > v_b.to_number(),
                    CompOp::GreaterThanOrEqual => v_a.to_number() >= v_b.to_number(),
                    CompOp::LessThan => v_a.to_number() < v_b.to_number(),
                    CompOp::LessThanOrEqual => v_a.to_number() <= v_b.to_number(),
                    CompOp::In => {
                        if !v_b.is_object() {
                            return interpreter.throw_type_error(format!(
                                "right-hand side of 'in' should be an object, got {}",
                                v_b.get_type().as_str()
                            ));
                        }
                        let key = interpreter.to_property_key(&v_a)?;
                        interpreter.has_property(&v_b, &key)
                    }
                }))
            }
            op::BinOp::Log(op) => {
                // turn a `Value` into a `bool`
                let to_bool = |value| bool::from(&value);
                Ok(match op {
                    LogOp::And => Value::from(
                        to_bool(self.lhs().run(interpreter)?)
                            && to_bool(self.rhs().run(interpreter)?),
                    ),
                    LogOp::Or => Value::from(
                        to_bool(self.lhs().run(interpreter)?)
                            || to_bool(self.rhs().run(interpreter)?),
                    ),
                })
            }
            op::BinOp::Assign(op) => match self.lhs() {
                Node::Identifier(ref name) => {
                    let v_a = interpreter
                        .realm()
                        .environment
                        .get_binding_value(name.as_ref())
                        .ok_or_else(|| interpreter.construct_reference_error(name.as_ref()))?;
                    let v_b = self.rhs().run(interpreter)?;
                    let value = Self::run_assign(op, v_a, v_b);
                    interpreter.realm.environment.set_mutable_binding(
                        name.as_ref(),
                        value.clone(),
                        true,
                    );
                    Ok(value)
                }
                Node::GetConstField(ref get_const_field) => {
                    let v_r_a = get_const_field.obj().run(interpreter)?;
                    let v_a = v_r_a.get_field(get_const_field.field());
                    let v_b = self.rhs().run(interpreter)?;
                    let value = Self::run_assign(op, v_a, v_b);
                    v_r_a.set_field(get_const_field.field(), value.clone());
                    Ok(value)
                }
                _ => Ok(Value::undefined()),
            },
        }
    }
}

impl BinOp {
    /// Runs the assignment operators.
    fn run_assign(op: AssignOp, v_a: Value, v_b: Value) -> Value {
        match op {
            AssignOp::Add => v_a + v_b,
            AssignOp::Sub => v_a - v_b,
            AssignOp::Mul => v_a * v_b,
            AssignOp::Exp => v_a.as_num_to_power(v_b),
            AssignOp::Div => v_a / v_b,
            AssignOp::Mod => v_a % v_b,
            AssignOp::And => v_a & v_b,
            AssignOp::Or => v_a | v_b,
            AssignOp::Xor => v_a ^ v_b,
            AssignOp::Shl => v_a << v_b,
            AssignOp::Shr => v_a << v_b,
        }
    }
}

impl Executable for UnaryOp {
    fn run(&self, interpreter: &mut Interpreter) -> ResultValue {
        let v_a = self.target().run(interpreter)?;

        Ok(match self.op() {
            op::UnaryOp::Minus => -v_a,
            op::UnaryOp::Plus => Value::from(v_a.to_number()),
            op::UnaryOp::IncrementPost => {
                let ret = v_a.clone();
                interpreter.set_value(self.target(), Value::from(v_a.to_number() + 1.0))?;
                ret
            }
            op::UnaryOp::IncrementPre => {
                interpreter.set_value(self.target(), Value::from(v_a.to_number() + 1.0))?
            }
            op::UnaryOp::DecrementPost => {
                let ret = v_a.clone();
                interpreter.set_value(self.target(), Value::from(v_a.to_number() - 1.0))?;
                ret
            }
            op::UnaryOp::DecrementPre => {
                interpreter.set_value(self.target(), Value::from(v_a.to_number() - 1.0))?
            }
            op::UnaryOp::Not => !v_a,
            op::UnaryOp::Tilde => {
                let num_v_a = v_a.to_number();
                // NOTE: possible UB: https://github.com/rust-lang/rust/issues/10184
                Value::from(if num_v_a.is_nan() {
                    -1
                } else {
                    !(num_v_a as i32)
                })
            }
            op::UnaryOp::Void => Value::undefined(),
            op::UnaryOp::Delete => match *self.target() {
                Node::GetConstField(ref get_const_field) => Value::boolean(
                    get_const_field
                        .obj()
                        .run(interpreter)?
                        .remove_property(get_const_field.field()),
                ),
                Node::GetField(ref get_field) => Value::boolean(
                    get_field
                        .obj()
                        .run(interpreter)?
                        .remove_property(&get_field.field().run(interpreter)?.to_string()),
                ),
                Node::Identifier(_) => Value::boolean(false),
                Node::ArrayDecl(_)
                | Node::Block(_)
                | Node::Const(_)
                | Node::FunctionDecl(_)
                | Node::FunctionExpr(_)
                | Node::New(_)
                | Node::Object(_)
                | Node::UnaryOp(_) => Value::boolean(true),
                _ => panic!("SyntaxError: wrong delete argument {}", self),
            },
            op::UnaryOp::TypeOf => Value::from(v_a.get_type().as_str()),
        })
    }
}