Skip to content

Commit

Permalink
fix(boa): bitwise not operation (#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
neeldug authored Jul 28, 2021
1 parent a7ebfc8 commit 330839d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 5 additions & 0 deletions boa/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ impl JsBigInt {
Self::new(x.as_inner().neg())
}

#[inline]
pub fn not(x: &Self) -> Self {
Self::new(!x.as_inner())
}

#[inline]
pub(crate) fn as_inner(&self) -> &RawBigInt {
&self.inner
Expand Down
6 changes: 6 additions & 0 deletions boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,4 +1094,10 @@ impl Number {
}
(x < y).into()
}

#[inline]
pub(crate) fn not(x: f64) -> i32 {
let x = f64_to_int32(x);
!x
}
}
17 changes: 9 additions & 8 deletions boa/src/syntax/ast/node/operator/unary_op/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::{
exec::Executable,
gc::{Finalize, Trace},
syntax::ast::{node::Node, op},
Context, Result, Value,
Context, JsBigInt, Result, Value,
};
use std::fmt;

use crate::builtins::Number;
use crate::value::Numeric;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -76,13 +78,12 @@ impl Executable for UnaryOp {
}
op::UnaryOp::Not => self.target().run(context)?.not(context)?.into(),
op::UnaryOp::Tilde => {
let num_v_a = self.target().run(context)?.to_number(context)?;
Value::from(if num_v_a.is_nan() {
-1
} else {
// TODO: this is not spec compliant.
!(num_v_a as i32)
})
let expr = self.target().run(context)?;
let old_v = expr.to_numeric(context)?;
match old_v {
Numeric::Number(x) => Value::from(Number::not(x)),
Numeric::BigInt(x) => Value::from(JsBigInt::not(&x)),
}
}
op::UnaryOp::Void => {
self.target().run(context)?;
Expand Down

0 comments on commit 330839d

Please sign in to comment.