LibJS: Support non-base-10 BigInt literals in bytecode VM

Fixes 39 tests in test262 and a handful in test-js. :^)
This commit is contained in:
Andreas Kling 2022-10-19 19:17:58 +02:00
parent 29935fe943
commit 178f0b9971
Notes: sideshowbarker 2024-07-17 05:21:12 +09:00

View file

@ -498,7 +498,19 @@ Bytecode::CodeGenerationErrorOr<void> NullLiteral::generate_bytecode(Bytecode::G
Bytecode::CodeGenerationErrorOr<void> BigIntLiteral::generate_bytecode(Bytecode::Generator& generator) const
{
generator.emit<Bytecode::Op::NewBigInt>(Crypto::SignedBigInteger::from_base(10, m_value.substring(0, m_value.length() - 1)));
// 1. Return the NumericValue of NumericLiteral as defined in 12.8.3.
auto integer = [&] {
if (m_value[0] == '0' && m_value.length() >= 3)
if (m_value[1] == 'x' || m_value[1] == 'X')
return Crypto::SignedBigInteger::from_base(16, m_value.substring(2, m_value.length() - 3));
if (m_value[1] == 'o' || m_value[1] == 'O')
return Crypto::SignedBigInteger::from_base(8, m_value.substring(2, m_value.length() - 3));
if (m_value[1] == 'b' || m_value[1] == 'B')
return Crypto::SignedBigInteger::from_base(2, m_value.substring(2, m_value.length() - 3));
return Crypto::SignedBigInteger::from_base(10, m_value.substring(0, m_value.length() - 1));
}();
generator.emit<Bytecode::Op::NewBigInt>(integer);
return {};
}