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("BigInt", 1, constructor);
constructor.define(
"prototype",
Property::with_attributes(Value::Object(proto.clone()), false, false),
);
constructor.define(
"_not_a_constructor",
Property::with_attributes(Value::Bool(true), 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> {
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() || value.fract() != 0.0 {
return Err(interp.range_error("cannot convert a non-integer to a BigInt"));
}
Ok(interp.bigint(BigInt::from_f64_truncated(value)))
}
Value::String(text) => {
let text = text.to_string_lossy().into_owned();
match BigInt::from_string(&text) {
Some(big) => Ok(interp.bigint(big)),
None => Err(Signal::Throw(
interp.make_error("SyntaxError", "cannot parse BigInt from this string"),
)),
}
}
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()),
_ => 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> {
this_bigint(interp, &this)?;
Ok(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)))
}
fn to_bigint(interp: &mut Interp<'_>, value: &Value) -> JsResult<BigInt> {
match value {
Value::BigInt(big) => Ok((**big).clone()),
other => match constructor(interp, Value::Undefined, std::slice::from_ref(other))? {
Value::BigInt(big) => Ok((*big).clone()),
_ => Err(interp.type_error("cannot convert to BigInt")),
},
}
}