Browse Source

Fix logical expressions evaluation (#999)

* Fix logical expressions evaluation

* Remove redundant clones

Co-authored-by: tofpie <tofpie@users.noreply.github.com>
pull/1001/head
tofpie 4 years ago committed by GitHub
parent
commit
f34e0b58e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      boa/src/syntax/ast/node/operator/bin_op/mod.rs
  2. 26
      boa/src/value/display.rs

30
boa/src/syntax/ast/node/operator/bin_op/mod.rs

@ -150,18 +150,24 @@ impl Executable for BinOp {
} }
})) }))
} }
op::BinOp::Log(op) => { op::BinOp::Log(op) => Ok(match op {
// turn a `Value` into a `bool` LogOp::And => {
let to_bool = |value| bool::from(&value); let left = self.lhs().run(context)?;
Ok(match op { if !left.to_boolean() {
LogOp::And => Value::from( left
to_bool(self.lhs().run(context)?) && to_bool(self.rhs().run(context)?), } else {
), self.rhs().run(context)?
LogOp::Or => Value::from( }
to_bool(self.lhs().run(context)?) || to_bool(self.rhs().run(context)?), }
), LogOp::Or => {
}) let left = self.lhs().run(context)?;
} if left.to_boolean() {
left
} else {
self.rhs().run(context)?
}
}
}),
op::BinOp::Assign(op) => match self.lhs() { op::BinOp::Assign(op) => match self.lhs() {
Node::Identifier(ref name) => { Node::Identifier(ref name) => {
let v_a = context let v_a = context

26
boa/src/value/display.rs

@ -48,18 +48,22 @@ macro_rules! print_obj_value {
}; };
(props of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr, $print_internals:expr) => { (props of $obj:expr, $display_fn:ident, $indent:expr, $encounters:expr, $print_internals:expr) => {
print_obj_value!(impl $obj, |(key, val)| { print_obj_value!(impl $obj, |(key, val)| {
let v = &val if val.is_data_descriptor() {
// FIXME: handle accessor descriptors let v = &val
.as_data_descriptor() .as_data_descriptor()
.unwrap() .unwrap()
.value(); .value();
format!( format!(
"{:>width$}: {}", "{:>width$}: {}",
key, key,
$display_fn(v, $encounters, $indent.wrapping_add(4), $print_internals), $display_fn(v, $encounters, $indent.wrapping_add(4), $print_internals),
width = $indent, width = $indent,
) )
} else {
// FIXME: handle accessor descriptors
"".to_string()
}
}) })
}; };

Loading…
Cancel
Save