Skip to content

Commit

Permalink
LibJS: Support non-base-10 BigInt literals in bytecode VM
Browse files Browse the repository at this point in the history
Fixes 39 tests in test262 and a handful in test-js. :^)
  • Loading branch information
awesomekling committed Oct 19, 2022
1 parent 29935fe commit 178f0b9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}

Expand Down

0 comments on commit 178f0b9

Please sign in to comment.