Skip to content

Commit

Permalink
Added specialization for BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed May 26, 2020
1 parent a1fa22e commit 5c4dc6f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
13 changes: 4 additions & 9 deletions boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,11 @@ impl BigInt {
///
/// [spec]: https://tc39.es/ecma262/#sec-bigint-objects
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/BigInt
pub(crate) fn make_bigint(
_this: &mut Value,
args: &[Value],
ctx: &mut Interpreter,
) -> ResultValue {
pub(crate) fn make_bigint(_: &mut Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue {
let data = match args.get(0) {
Some(ref value) => {
if let Some(bigint) = value.to_bigint() {
Value::from(bigint)
bigint
} else {
return Err(RangeError::run_new(
format!(
Expand All @@ -59,9 +55,9 @@ impl BigInt {
)?);
}
}
None => Value::from(AstBigInt::from(0)),
None => AstBigInt::from(0),
};
Ok(data)
Ok(Value::from(data))
}

/// `BigInt.prototype.toString( [radix] )`
Expand Down Expand Up @@ -119,7 +115,6 @@ impl BigInt {
/// Create a new `Number` object
pub(crate) fn create(global: &Value) -> Value {
let prototype = Value::new_object(Some(global));
prototype.set_internal_slot("BigIntData", Value::from(AstBigInt::from(0)));

make_builtin_fn(Self::to_string, "toString", &prototype, 1);
make_builtin_fn(Self::value_of, "valueOf", &prototype, 0);
Expand Down
19 changes: 8 additions & 11 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
value::{same_value, ResultValue, Value, ValueData},
},
exec::Interpreter,
syntax::ast::bigint::BigInt as AstBigInt,
};
use gc::{Finalize, Trace};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -404,19 +405,15 @@ impl Object {
}

/// Return a new `BigInt` object whose `[[BigIntData]]` internal slot is set to argument.
pub fn bigint(argument: &Value) -> Self {
let mut obj = Self {
data: ObjectData::BigInt,
pub fn bigint(value: AstBigInt) -> Self {
Self {
data: ObjectData::BigInt(value),
internal_slots: FxHashMap::default(),
properties: FxHashMap::default(),
sym_properties: FxHashMap::default(),
state: None,
func: None,
};

obj.internal_slots
.insert("BigIntData".to_string(), argument.clone());
obj
}
}

/// Converts the `Value` to an `Object` type.
Expand All @@ -431,7 +428,7 @@ impl Object {
ValueData::Rational(a) => Ok(Self::number(a)),
ValueData::Integer(a) => Ok(Self::number(f64::from(a))),
ValueData::String(ref a) => Ok(Self::string(a.clone())),
ValueData::BigInt(_) => Ok(Self::bigint(value)),
ValueData::BigInt(ref bigint) => Ok(Self::bigint(bigint.clone())),
ValueData::Object(ref obj) => Ok((*obj).deref().borrow().clone()),
_ => Err(()),
}
Expand Down Expand Up @@ -474,7 +471,7 @@ pub enum ObjectData {
Error,
Boolean(bool),
Number(f64),
BigInt,
BigInt(AstBigInt),
Ordinary,
}

Expand All @@ -492,7 +489,7 @@ impl Display for ObjectData {
Self::Ordinary => "Ordinary",
Self::Boolean(_) => "Boolean",
Self::Number(_) => "Number",
Self::BigInt => "BigInt",
Self::BigInt(_) => "BigInt",
}
)
}
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ impl ValueData {
ValueData::BigInt(b) => Some(b.clone()),
ValueData::Object(ref o) => {
let object = (o).deref().borrow();
if object.data == ObjectData::BigInt {
object.get_internal_slot("BigIntData").to_bigint()
if let ObjectData::BigInt(ref bigint) = object.data {
Some(bigint.clone())
} else {
None
}
Expand Down
6 changes: 3 additions & 3 deletions boa/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,14 @@ impl Interpreter {
))
}
ValueData::Object(_) | ValueData::Symbol(_) => Ok(value.clone()),
ValueData::BigInt(_) => {
ValueData::BigInt(ref bigint) => {
let proto = self
.realm
.environment
.get_binding_value("BigInt")
.get_field(PROTOTYPE);
let bigint_obj = Value::new_object_from_prototype(proto, ObjectData::BigInt);
bigint_obj.set_internal_slot("BigIntData", value.clone());
let bigint_obj =
Value::new_object_from_prototype(proto, ObjectData::BigInt(bigint.clone()));
Ok(bigint_obj)
}
}
Expand Down

0 comments on commit 5c4dc6f

Please sign in to comment.