Browse Source

LibJS/Bytecode: Un-templatize throw_if_needed_for_call()

Andreas Kling 1 year ago
parent
commit
bcf7cdb679

+ 8 - 13
Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp

@@ -202,30 +202,25 @@ ThrowCompletionOr<Value> perform_call(Interpreter& interpreter, Value this_value
     return return_value;
 }
 
-static Completion throw_type_error_for_callee(Bytecode::Interpreter& interpreter, auto& call, StringView callee_type)
+static Completion throw_type_error_for_callee(Bytecode::Interpreter& interpreter, Value callee, StringView callee_type, Optional<StringTableIndex> const& expression_string)
 {
     auto& vm = interpreter.vm();
-    auto callee = interpreter.reg(call.callee());
 
-    if (call.expression_string().has_value())
-        return vm.throw_completion<TypeError>(ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects(), callee_type, interpreter.current_executable().get_string(call.expression_string()->value()));
+    if (expression_string.has_value())
+        return vm.throw_completion<TypeError>(ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects(), callee_type, interpreter.current_executable().get_string(expression_string->value()));
 
     return vm.throw_completion<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects(), callee_type);
 }
 
-template<typename InstructionType>
-ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter& interpreter, InstructionType const& call, Value callee)
+ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter& interpreter, Value callee, Op::CallType call_type, Optional<StringTableIndex> const& expression_string)
 {
-    if (call.call_type() == Op::CallType::Call && !callee.is_function())
-        return throw_type_error_for_callee(interpreter, call, "function"sv);
-    if (call.call_type() == Op::CallType::Construct && !callee.is_constructor())
-        return throw_type_error_for_callee(interpreter, call, "constructor"sv);
+    if (call_type == Op::CallType::Call && !callee.is_function())
+        return throw_type_error_for_callee(interpreter, callee, "function"sv, expression_string);
+    if (call_type == Op::CallType::Construct && !callee.is_constructor())
+        return throw_type_error_for_callee(interpreter, callee, "constructor"sv, expression_string);
     return {};
 }
 
-template ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter&, Op::Call const&, Value);
-template ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter&, Op::CallWithArgumentArray const&, Value);
-
 ThrowCompletionOr<Value> typeof_variable(VM& vm, DeprecatedFlyString const& string)
 {
     // 1. Let val be the result of evaluating UnaryExpression.

+ 1 - 2
Userland/Libraries/LibJS/Bytecode/CommonImplementations.h

@@ -18,8 +18,7 @@ ThrowCompletionOr<Value> get_by_value(Bytecode::Interpreter&, Value base_value,
 ThrowCompletionOr<Value> get_global(Bytecode::Interpreter&, IdentifierTableIndex, u32 cache_index);
 ThrowCompletionOr<void> put_by_property_key(VM&, Value base, Value this_value, Value value, PropertyKey name, Op::PropertyKind kind);
 ThrowCompletionOr<Value> perform_call(Interpreter&, Value this_value, Op::CallType, Value callee, MarkedVector<Value> argument_values);
-template<typename InstructionType>
-ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter&, InstructionType const&, Value callee);
+ThrowCompletionOr<void> throw_if_needed_for_call(Interpreter&, Value callee, Op::CallType, Optional<StringTableIndex> const& expression_string);
 ThrowCompletionOr<Value> typeof_variable(VM&, DeprecatedFlyString const&);
 ThrowCompletionOr<void> set_variable(VM&, DeprecatedFlyString const&, Value, Op::EnvironmentMode, Op::SetVariable::InitializationMode);
 Value new_function(VM&, FunctionExpression const&, Optional<IdentifierTableIndex> const& lhs_name, Optional<Register> const& home_object);

+ 2 - 2
Userland/Libraries/LibJS/Bytecode/Interpreter.cpp

@@ -1131,7 +1131,7 @@ ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) c
     auto& vm = interpreter.vm();
     auto callee = interpreter.reg(m_callee);
 
-    TRY(throw_if_needed_for_call(interpreter, *this, callee));
+    TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
 
     MarkedVector<Value> argument_values(vm.heap());
     argument_values.ensure_capacity(m_argument_count);
@@ -1145,7 +1145,7 @@ ThrowCompletionOr<void> Call::execute_impl(Bytecode::Interpreter& interpreter) c
 ThrowCompletionOr<void> CallWithArgumentArray::execute_impl(Bytecode::Interpreter& interpreter) const
 {
     auto callee = interpreter.reg(m_callee);
-    TRY(throw_if_needed_for_call(interpreter, *this, callee));
+    TRY(throw_if_needed_for_call(interpreter, callee, call_type(), expression_string()));
     auto argument_values = argument_list_evaluation(interpreter);
     interpreter.accumulator() = TRY(perform_call(interpreter, interpreter.reg(m_this_value), call_type(), callee, move(argument_values)));
     return {};