diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp index 50388890fe..38a5add979 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp @@ -226,4 +226,22 @@ ThrowCompletionOr throw_if_needed_for_call(Interpreter& interpreter, Instr template ThrowCompletionOr throw_if_needed_for_call(Interpreter&, Op::Call const&, Value); template ThrowCompletionOr throw_if_needed_for_call(Interpreter&, Op::CallWithArgumentArray const&, Value); +ThrowCompletionOr typeof_variable(VM& vm, DeprecatedFlyString const& string) +{ + // 1. Let val be the result of evaluating UnaryExpression. + auto reference = TRY(vm.resolve_binding(string)); + + // 2. If val is a Reference Record, then + // a. If IsUnresolvableReference(val) is true, return "undefined". + if (reference.is_unresolvable()) + return PrimitiveString::create(vm, "undefined"_string); + + // 3. Set val to ? GetValue(val). + auto value = TRY(reference.get_value(vm)); + + // 4. NOTE: This step is replaced in section B.3.6.3. + // 5. Return a String according to Table 41. + return PrimitiveString::create(vm, value.typeof()); +} + } diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h index 979951d1e7..6524271f17 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h @@ -20,5 +20,6 @@ ThrowCompletionOr put_by_property_key(VM&, Value base, Value this_value, V ThrowCompletionOr perform_call(Interpreter&, Value this_value, Op::CallType, Value callee, MarkedVector argument_values); template ThrowCompletionOr throw_if_needed_for_call(Interpreter&, InstructionType const&, Value callee); +ThrowCompletionOr typeof_variable(VM&, DeprecatedFlyString const&); } diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 90566a84ac..7c2151e3f1 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -1639,24 +1639,7 @@ ThrowCompletionOr NewClass::execute_impl(Bytecode::Interpreter& interprete ThrowCompletionOr TypeofVariable::execute_impl(Bytecode::Interpreter& interpreter) const { auto& vm = interpreter.vm(); - - // 1. Let val be the result of evaluating UnaryExpression. - auto const& string = interpreter.current_executable().get_identifier(m_identifier); - auto reference = TRY(vm.resolve_binding(string)); - - // 2. If val is a Reference Record, then - // a. If IsUnresolvableReference(val) is true, return "undefined". - if (reference.is_unresolvable()) { - interpreter.accumulator() = PrimitiveString::create(vm, "undefined"_string); - return {}; - } - - // 3. Set val to ? GetValue(val). - auto value = TRY(reference.get_value(vm)); - - // 4. NOTE: This step is replaced in section B.3.6.3. - // 5. Return a String according to Table 41. - interpreter.accumulator() = PrimitiveString::create(vm, value.typeof()); + interpreter.accumulator() = TRY(typeof_variable(vm, interpreter.current_executable().get_identifier(m_identifier))); return {}; }