1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:58:13 +00:00

LibJS/Bytecode: Make typeof return "undefined" on unresolvable IDs

Previously it would throw instead of returning "undefined" for
`typeof Identifier` if Identifier does not exist.
This commit is contained in:
Luke Wilde 2022-06-11 23:12:22 +01:00 committed by Ali Mohammad Pur
parent c0fadfb9b7
commit 482a827346
4 changed files with 55 additions and 1 deletions

View file

@ -436,7 +436,9 @@ Bytecode::CodeGenerationErrorOr<void> UnaryExpression::generate_bytecode(Bytecod
if (m_op == UnaryOp::Delete)
return generator.emit_delete_reference(m_lhs);
TRY(m_lhs->generate_bytecode(generator));
// Typeof needs some special handling for when the LHS is an Identifier. Namely, it shouldn't throw on unresolvable references, but instead return "undefined".
if (m_op != UnaryOp::Typeof)
TRY(m_lhs->generate_bytecode(generator));
switch (m_op) {
case UnaryOp::BitwiseNot:
@ -452,6 +454,13 @@ Bytecode::CodeGenerationErrorOr<void> UnaryExpression::generate_bytecode(Bytecod
generator.emit<Bytecode::Op::UnaryMinus>();
break;
case UnaryOp::Typeof:
if (is<Identifier>(*m_lhs)) {
auto& identifier = static_cast<Identifier const&>(*m_lhs);
generator.emit<Bytecode::Op::TypeofVariable>(generator.intern_identifier(identifier.string()));
break;
}
TRY(m_lhs->generate_bytecode(generator));
generator.emit<Bytecode::Op::Typeof>();
break;
case UnaryOp::Void: