1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 13:27:36 +00:00

LibCrypto: Replace from_base{2,8,10,16}() & to_base10 with from_base(N)

This allows us to support parsing and serializing BigIntegers to and
from any base N (such that 2 <= N <= 36).
This commit is contained in:
Idan Horowitz 2021-06-29 17:51:52 +03:00 committed by Linus Groh
parent a768131720
commit 005d75656e
14 changed files with 56 additions and 132 deletions

View file

@ -1850,14 +1850,14 @@ Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
Crypto::SignedBigInteger integer;
if (m_value[0] == '0' && m_value.length() >= 3) {
if (m_value[1] == 'x' || m_value[1] == 'X') {
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base16(m_value.substring(2, m_value.length() - 3)));
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base(16, m_value.substring(2, m_value.length() - 3)));
} else if (m_value[1] == 'o' || m_value[1] == 'O') {
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base8(m_value.substring(2, m_value.length() - 3)));
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base(8, m_value.substring(2, m_value.length() - 3)));
} else if (m_value[1] == 'b' || m_value[1] == 'B') {
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base2(m_value.substring(2, m_value.length() - 3)));
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base(2, m_value.substring(2, m_value.length() - 3)));
}
}
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base(10, m_value.substring(0, m_value.length() - 1)));
}
Value BooleanLiteral::execute(Interpreter& interpreter, GlobalObject&) const

View file

@ -250,7 +250,7 @@ void NullLiteral::generate_bytecode(Bytecode::Generator& generator) const
void BigIntLiteral::generate_bytecode(Bytecode::Generator& generator) const
{
generator.emit<Bytecode::Op::NewBigInt>(Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
generator.emit<Bytecode::Op::NewBigInt>(Crypto::SignedBigInteger::from_base(10, m_value.substring(0, m_value.length() - 1)));
}
void StringLiteral::generate_bytecode(Bytecode::Generator& generator) const

View file

@ -469,7 +469,7 @@ String Store::to_string_impl(Bytecode::Executable const&) const
String NewBigInt::to_string_impl(Bytecode::Executable const&) const
{
return String::formatted("NewBigInt \"{}\"", m_bigint.to_base10());
return String::formatted("NewBigInt \"{}\"", m_bigint.to_base(10));
}
String NewArray::to_string_impl(Bytecode::Executable const&) const

View file

@ -17,7 +17,7 @@ public:
virtual ~BigInt();
const Crypto::SignedBigInteger& big_integer() const { return m_big_integer; }
const String to_string() const { return String::formatted("{}n", m_big_integer.to_base10()); }
const String to_string() const { return String::formatted("{}n", m_big_integer.to_base(10)); }
private:
virtual const char* class_name() const override { return "BigInt"; }

View file

@ -53,7 +53,7 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntPrototype::to_string)
if (vm.exception())
return {};
// FIXME: Support radix argument
return js_string(vm, bigint_value.as_bigint().big_integer().to_base10());
return js_string(vm, bigint_value.as_bigint().big_integer().to_base(10));
}
// 21.2.3.2 BigInt.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-bigint.prototype.tolocalestring

View file

@ -355,7 +355,7 @@ String Value::to_string(GlobalObject& global_object, bool legacy_null_to_empty_s
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "string");
return {};
case Type::BigInt:
return m_value.as_bigint->big_integer().to_base10();
return m_value.as_bigint->big_integer().to_base(10);
case Type::Object: {
auto primitive_value = to_primitive(global_object, PreferredType::String);
if (global_object.vm().exception())
@ -547,7 +547,7 @@ BigInt* Value::to_bigint(GlobalObject& global_object) const
vm.throw_exception<SyntaxError>(global_object, ErrorType::BigIntInvalidValue, string);
return {};
}
return js_bigint(vm.heap(), Crypto::SignedBigInteger::from_base10(string.trim_whitespace()));
return js_bigint(vm.heap(), Crypto::SignedBigInteger::from_base(10, string.trim_whitespace()));
}
case Type::Symbol:
vm.throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "BigInt");
@ -1398,7 +1398,7 @@ bool abstract_eq(GlobalObject& global_object, Value lhs, Value rhs)
auto& rhs_string = rhs.as_string().string();
if (!is_valid_bigint_value(rhs_string))
return false;
return abstract_eq(global_object, lhs, js_bigint(global_object.heap(), Crypto::SignedBigInteger::from_base10(rhs_string)));
return abstract_eq(global_object, lhs, js_bigint(global_object.heap(), Crypto::SignedBigInteger::from_base(10, rhs_string)));
}
if (lhs.is_string() && rhs.is_bigint())
@ -1490,7 +1490,7 @@ TriState abstract_relation(GlobalObject& global_object, bool left_first, Value l
auto& y_string = y_primitive.as_string().string();
if (!is_valid_bigint_value(y_string))
return TriState::Unknown;
if (x_primitive.as_bigint().big_integer() < Crypto::SignedBigInteger::from_base10(y_string))
if (x_primitive.as_bigint().big_integer() < Crypto::SignedBigInteger::from_base(10, y_string))
return TriState::True;
else
return TriState::False;
@ -1500,7 +1500,7 @@ TriState abstract_relation(GlobalObject& global_object, bool left_first, Value l
auto& x_string = x_primitive.as_string().string();
if (!is_valid_bigint_value(x_string))
return TriState::Unknown;
if (Crypto::SignedBigInteger::from_base10(x_string) < y_primitive.as_bigint().big_integer())
if (Crypto::SignedBigInteger::from_base(10, x_string) < y_primitive.as_bigint().big_integer())
return TriState::True;
else
return TriState::False;