use crate::bigint::BigInt;
use crate::builtins::{arg, define_method};
use crate::interp::{Interp, JsResult, Signal};
use crate::object::{JsObject, Property};
use crate::value::Value;
use js_gc::Gc;
pub fn install(interp: &mut Interp<'_>, object_proto: &Gc<JsObject>) {
let proto = interp.new_object(Some(object_proto.clone()));
interp.realm.bigint_proto = Some(proto.clone());
let constructor = interp.new_native_constructor("BigInt", 1, constructor);
constructor.define(
"prototype",
Property::with_attributes(Value::Object(proto.clone()), false, false),
);
define_method(&constructor, "asIntN", 2, as_int_n, interp);
define_method(&constructor, "asUintN", 2, as_uint_n, interp);
proto.define(
"constructor",
Property::with_attributes(Value::Object(constructor.clone()), true, false),
);
define_method(&proto, "toString", 0, to_string, interp);
define_method(&proto, "toLocaleString", 0, to_string, interp);
define_method(&proto, "valueOf", 0, value_of, interp);
let global = interp.realm.global.clone();
global.define(
"BigInt",
Property::with_attributes(Value::Object(constructor), true, false),
);
}
fn constructor(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
if interp.is_constructing() {
return Err(interp.not_a_constructor_error());
}
match arg(args, 0) {
Value::BigInt(big) => Ok(Value::BigInt(big)),
Value::Bool(flag) => Ok(interp.bigint(BigInt::from_i64(if flag { 1 } else { 0 }))),
Value::Int(value) => Ok(interp.bigint(BigInt::from_i64(value as i64))),
Value::Float(value) => {
if !value.is_finite() {
return Err(interp.range_error("cannot convert NaN or Infinity to BigInt"));
}
if value.fract() != 0.0 {
return Err(interp.range_error("cannot convert to BigInt: not an integer"));
}
Ok(interp.bigint(BigInt::from_f64_truncated(value)))
}
Value::String(text) => {
let text = text.to_string_lossy();
let big = string_to_bigint(interp, &text)?;
Ok(interp.bigint(big))
}
Value::Symbol(_) => Err(interp.type_error("cannot convert a Symbol to a BigInt")),
Value::Undefined | Value::Null => Err(interp.type_error("cannot convert to BigInt")),
other => {
let primitive = interp.to_primitive(other, crate::interp::Hint::Number)?;
constructor(interp, Value::Undefined, &[primitive])
}
}
}
fn this_bigint(interp: &Interp<'_>, this: &Value) -> JsResult<Gc<BigInt>> {
match this {
Value::BigInt(big) => Ok(big.clone()),
Value::Object(object) => match interp.internal_get(object, "_value") {
Some(Value::BigInt(big)) => Ok(big),
_ => Err(interp.type_error("not a BigInt")),
},
_ => Err(interp.type_error("not a BigInt")),
}
}
fn to_string(interp: &mut Interp<'_>, this: Value, args: &[Value]) -> JsResult<Value> {
let big = this_bigint(interp, &this)?;
let radix = match arg(args, 0) {
Value::Undefined => 10,
other => {
let radix = other.to_number();
if !(2.0..=36.0).contains(&radix) || radix.fract() != 0.0 {
return Err(interp.range_error("radix must be between 2 and 36"));
}
radix as u32
}
};
Ok(interp.string(big.to_radix_string(radix)))
}
fn value_of(interp: &mut Interp<'_>, this: Value, _args: &[Value]) -> JsResult<Value> {
Ok(Value::BigInt(this_bigint(interp, &this)?))
}
fn bits_of(interp: &mut Interp<'_>, args: &[Value]) -> JsResult<u64> {
let bits = arg(args, 0).to_number();
if !bits.is_finite() || bits < 0.0 || bits.fract() != 0.0 {
return Err(interp.range_error("invalid bit count"));
}
Ok(bits as u64)
}
fn as_int_n(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let bits = bits_of(interp, args)?;
let value = to_bigint(interp, &arg(args, 1))?;
Ok(interp.bigint(value.as_int_n(bits)))
}
fn as_uint_n(interp: &mut Interp<'_>, _this: Value, args: &[Value]) -> JsResult<Value> {
let bits = bits_of(interp, args)?;
let value = to_bigint(interp, &arg(args, 1))?;
Ok(interp.bigint(value.as_uint_n(bits)))
}
pub fn to_bigint(interp: &mut Interp<'_>, value: &Value) -> JsResult<BigInt> {
match value {
Value::BigInt(big) => Ok((**big).clone()),
Value::String(text) => string_to_bigint(interp, &text.to_string_lossy()),
Value::Bool(flag) => Ok(BigInt::from_i64(if *flag { 1 } else { 0 })),
Value::Int(_) | Value::Float(_) => Err(interp.type_error("cannot convert to bigint")),
Value::Undefined | Value::Null => Err(interp.type_error("cannot convert to bigint")),
Value::Symbol(_) => Err(interp.type_error("cannot convert to bigint")),
Value::Object(_) => {
let primitive = interp.to_primitive(value.clone(), crate::interp::Hint::Number)?;
to_bigint(interp, &primitive)
}
_ => Err(interp.type_error("cannot convert to bigint")),
}
}
pub fn bigint_element(interp: &mut Interp<'_>, value: &Value) -> JsResult<Gc<BigInt>> {
let big = to_bigint(interp, value)?;
Ok(Gc::new(interp.heap, big))
}
pub fn string_to_bigint(interp: &mut Interp<'_>, text: &str) -> JsResult<BigInt> {
match BigInt::from_string(text) {
Some(big) => Ok(big),
None => Err(Signal::Throw(
interp.make_error("SyntaxError", "invalid bigint literal"),
)),
}
}