-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathunary.rs
More file actions
70 lines (68 loc) · 3.01 KB
/
unary.rs
File metadata and controls
70 lines (68 loc) · 3.01 KB
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
use crate::bytecompiler::{Access, BindingAccessOpcode, ByteCompiler, Register, ToJsString};
use boa_ast::Expression;
use boa_ast::expression::literal::Number;
use boa_ast::expression::operator::{Unary, unary::UnaryOp};
impl ByteCompiler<'_> {
pub(crate) fn compile_unary(&mut self, unary: &Unary, dst: &Register) {
match unary.op() {
UnaryOp::Delete => {
let mut compiler = self.position_guard(unary);
if let Some(access) = Access::from_expression(unary.target()) {
compiler.access_delete(access, dst);
} else if let Expression::Optional(opt) = unary.target() {
compiler.compile_optional_delete(opt, dst);
} else {
compiler.compile_expr(unary.target(), dst);
compiler.bytecode.emit_store_true(dst.variable());
}
}
UnaryOp::Minus => {
if let Expression::Literal(literal) = unary.target().flatten()
&& let Some(number) = literal.kind().as_number()
{
match number {
// Handles special case -0
Number::Int(0) => self.emit_store_rational(-0.0, dst),
Number::Int(value) => self.emit_store_integer(-value, dst),
Number::Num(value) => self.emit_store_rational(-value, dst),
}
} else {
self.compile_expr(unary.target(), dst);
self.bytecode.emit_neg(dst.variable());
}
}
UnaryOp::Plus => {
self.compile_expr(unary.target(), dst);
self.bytecode.emit_pos(dst.variable());
}
UnaryOp::Not => {
self.compile_expr(unary.target(), dst);
self.bytecode.emit_logical_not(dst.variable());
}
UnaryOp::Tilde => {
self.compile_expr(unary.target(), dst);
self.bytecode.emit_bit_not(dst.variable());
}
UnaryOp::TypeOf => {
match unary.target().flatten() {
Expression::Identifier(identifier) => {
let identifier = identifier.to_js_string(self.interner());
let binding = self.lexical_scope.get_identifier_reference(identifier);
let index = self.get_binding(&binding);
self.emit_binding_access(
BindingAccessOpcode::GetNameOrUndefined,
&index,
dst,
);
}
expr => self.compile_expr(expr, dst),
}
self.bytecode.emit_type_of(dst.variable());
}
UnaryOp::Void => {
self.compile_expr(unary.target(), dst);
self.bytecode.emit_store_undefined(dst.variable());
}
}
}
}