Skip to content

Commit

Permalink
simplify & document
Browse files Browse the repository at this point in the history
  • Loading branch information
neeldug committed Jul 25, 2021
1 parent 41c7752 commit 1a5266f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
5 changes: 4 additions & 1 deletion boa/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
use std::{
convert::TryFrom,
fmt::{self, Display},
ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub},
ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub},
rc::Rc,
};

Expand Down Expand Up @@ -260,6 +260,9 @@ impl JsBigInt {
Self::new(x.as_inner().neg())
}

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

#[inline]
pub(crate) fn as_inner(&self) -> &RawBigInt {
&self.inner
Expand Down
26 changes: 9 additions & 17 deletions boa/src/syntax/ast/node/operator/unary_op/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use crate::{
exec::Executable,
gc::{Finalize, Trace},
syntax::ast::{node::Node, op},
Context, Result, Value,
};
use crate::{exec::Executable, gc::{Finalize, Trace}, syntax::ast::{node::Node, op}, Context, Result, Value, JsBigInt};
use std::fmt;

#[cfg(feature = "deser")]
Expand Down Expand Up @@ -77,18 +72,15 @@ 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_numeric_number(context)?;
if num_v_a.is_nan() {
Value::from(-1)
} else if num_v_a.is_infinite() {
Value::from(-1)
if num_v_a.is_nan() || num_v_a.is_infinite() {
Value::from(-1) // special case for inf or nan
} else if let Some(num_bigint) = self.target.run(context)?.as_bigint() {

Value::from(JsBigInt::not(num_bigint))
// add bigint support
} else {
if self.target.run(context)?.is_bigint() {
Value::from(Value::from(-num_v_a-1f64).to_bigint(context)?)
}
else {
let temp = (num_v_a as i64) & 0x00000000ffffffff;
Value::from(!(temp as i32))
}
let masked = (num_v_a as i64) & 0x00000000ffffffff; // converts float to i32 following spec to ignore MSB using mask
Value::from(!(masked as i32)) // Nots i32 conversion and creates value from this
}
}
op::UnaryOp::Void => {
Expand Down

0 comments on commit 1a5266f

Please sign in to comment.