diff options
author | Linus Groh <mail@linusgroh.de> | 2020-08-25 12:52:32 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-25 18:30:31 +0200 |
commit | 9ea6ef4ed1450590ad02f6d501c7345eab779439 (patch) | |
tree | cf6827af2c346e2051229b55d49de2f90792b18c /Libraries/LibJS/AST.cpp | |
parent | 98dd034c2926c61b4fae47172a89b4e68a6eb43c (diff) | |
download | serenity-9ea6ef4ed1450590ad02f6d501c7345eab779439.zip |
LibJS: Make Interpreter::throw_exception() a void function
The motivation for this change is twofold:
- Returning a JS::Value is misleading as one would expect it to carry
some meaningful information, like maybe the error object that's being
created, but in fact it is always empty. Supposedly to serve as a
shortcut for the common case of "throw and return empty value", but
that's just leading us to my second point.
- Inconsistent usage / coding style: as of this commit there are 114
uses of throw_exception() discarding its return value and 55 uses
directly returning the call result (in LibJS, not counting LibWeb);
with the first style often having a more explicit empty value (or
nullptr in some cases) return anyway.
One more line to always make the return value obvious is should be
worth it.
So now it's basically always these steps, which is already being used in
the majority of cases (as outlined above):
- Throw an exception. This mutates interpreter state by updating
m_exception and unwinding, but doesn't return anything.
- Let the caller explicitly return an empty value, nullptr or anything
else itself.
Diffstat (limited to 'Libraries/LibJS/AST.cpp')
-rw-r--r-- | Libraries/LibJS/AST.cpp | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 1d440fcf17..70e1992a79 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -147,10 +147,11 @@ Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_obj } else { expression_string = static_cast<const MemberExpression&>(*m_callee).to_string_approximation(); } - return interpreter.throw_exception<TypeError>(ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects().characters(), call_type, expression_string.characters()); + interpreter.throw_exception<TypeError>(ErrorType::IsNotAEvaluatedFrom, callee.to_string_without_side_effects().characters(), call_type, expression_string.characters()); } else { - return interpreter.throw_exception<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects().characters(), call_type); + interpreter.throw_exception<TypeError>(ErrorType::IsNotA, callee.to_string_without_side_effects().characters(), call_type); } + return {}; } auto& function = callee.as_function(); @@ -184,9 +185,10 @@ Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_obj } else if (m_callee->is_super_expression()) { auto* super_constructor = interpreter.current_environment()->current_function()->prototype(); // FIXME: Functions should track their constructor kind. - if (!super_constructor || !super_constructor->is_function()) - return interpreter.throw_exception<TypeError>(ErrorType::NotAConstructor, "Super constructor"); - + if (!super_constructor || !super_constructor->is_function()) { + interpreter.throw_exception<TypeError>(ErrorType::NotAConstructor, "Super constructor"); + return {}; + } result = interpreter.construct(static_cast<Function&>(*super_constructor), function, move(arguments), global_object); if (interpreter.exception()) return {}; @@ -668,9 +670,10 @@ Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_ob super_constructor = m_super_class->execute(interpreter, global_object); if (interpreter.exception()) return {}; - if (!super_constructor.is_function() && !super_constructor.is_null()) - return interpreter.throw_exception<TypeError>(ErrorType::ClassDoesNotExtendAConstructorOrNull, super_constructor.to_string_without_side_effects().characters()); - + if (!super_constructor.is_function() && !super_constructor.is_null()) { + interpreter.throw_exception<TypeError>(ErrorType::ClassDoesNotExtendAConstructorOrNull, super_constructor.to_string_without_side_effects().characters()); + return {}; + } class_constructor->set_constructor_kind(Function::ConstructorKind::Derived); Object* prototype = Object::create_empty(global_object); @@ -695,9 +698,10 @@ Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_ob if (interpreter.exception()) return {}; - if (!class_prototype.is_object()) - return interpreter.throw_exception<TypeError>(ErrorType::NotAnObject, "Class prototype"); - + if (!class_prototype.is_object()) { + interpreter.throw_exception<TypeError>(ErrorType::NotAnObject, "Class prototype"); + return {}; + } for (const auto& method : m_methods) { auto method_value = method.execute(interpreter, global_object); if (interpreter.exception()) @@ -1137,8 +1141,10 @@ void ForOfStatement::dump(int indent) const Value Identifier::execute(Interpreter& interpreter, GlobalObject& global_object) const { auto value = interpreter.get_variable(string(), global_object); - if (value.is_empty()) - return interpreter.throw_exception<ReferenceError>(ErrorType::UnknownIdentifier, string().characters()); + if (value.is_empty()) { + interpreter.throw_exception<ReferenceError>(ErrorType::UnknownIdentifier, string().characters()); + return {}; + } return value; } @@ -1259,9 +1265,10 @@ Value AssignmentExpression::execute(Interpreter& interpreter, GlobalObject& glob if (interpreter.exception()) return {}; - if (reference.is_unresolvable()) - return interpreter.throw_exception<ReferenceError>(ErrorType::InvalidLeftHandAssignment); - + if (reference.is_unresolvable()) { + interpreter.throw_exception<ReferenceError>(ErrorType::InvalidLeftHandAssignment); + return {}; + } update_function_name(rhs_result, get_function_name(interpreter, reference.name().to_value(interpreter))); reference.put(interpreter, global_object, rhs_result); @@ -1797,7 +1804,8 @@ Value ThrowStatement::execute(Interpreter& interpreter, GlobalObject& global_obj auto value = m_argument->execute(interpreter, global_object); if (interpreter.exception()) return {}; - return interpreter.throw_exception(value); + interpreter.throw_exception(value); + return {}; } Value SwitchStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const |