From 853fab352d379bc1b93b68b24a79c6d8ad99deb4 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Tue, 2 Nov 2021 19:27:29 +0200 Subject: [PATCH] LibJS: Convert the InitializeReferencedBinding AO to ThrowCompletionOr --- Userland/Libraries/LibJS/AST.cpp | 18 +++++------------- .../LibJS/Runtime/ECMAScriptFunctionObject.cpp | 7 ++----- Userland/Libraries/LibJS/Runtime/Reference.h | 5 +++-- Userland/Libraries/LibJS/Runtime/VM.cpp | 18 +++++------------- 4 files changed, 15 insertions(+), 33 deletions(-) diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index bee4db9217..f4387762af 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -693,12 +693,9 @@ struct ForInOfHeadState { if (!destructuring) { VERIFY(lhs_reference.has_value()); if (lhs_kind == LexicalBinding) - lhs_reference->initialize_referenced_binding(global_object, next_value); + return lhs_reference->initialize_referenced_binding(global_object, next_value); else - TRY(lhs_reference->put_value(global_object, next_value)); - if (auto* exception = interpreter.exception()) - return throw_completion(exception->value()); - return {}; + return lhs_reference->put_value(global_object, next_value); } // j. Else, @@ -2388,12 +2385,9 @@ Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& globa VERIFY(!initializer_result.is_empty()); if (m_declaration_kind == DeclarationKind::Var) - TRY(reference.put_value(global_object, initializer_result)); + return reference.put_value(global_object, initializer_result); else - reference.initialize_referenced_binding(global_object, initializer_result); - if (auto* exception = interpreter.exception()) - return throw_completion(exception->value()); - return {}; + return reference.initialize_referenced_binding(global_object, initializer_result); }, [&](NonnullRefPtr const& pattern) -> ThrowCompletionOr { auto initializer_result = init->execute(interpreter, global_object); @@ -2408,9 +2402,7 @@ Value VariableDeclaration::execute(Interpreter& interpreter, GlobalObject& globa VERIFY(declarator.target().has>()); auto& identifier = declarator.target().get>(); auto reference = identifier->to_reference(interpreter, global_object); - reference.initialize_referenced_binding(global_object, js_undefined()); - if (interpreter.exception()) - return {}; + TRY_OR_DISCARD(reference.initialize_referenced_binding(global_object, js_undefined())); } } return {}; diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp index f855ddc77a..54529bb984 100644 --- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp @@ -426,12 +426,9 @@ ThrowCompletionOr ECMAScriptFunctionObject::function_declaration_instantia return throw_completion(exception->value()); // Here the difference from hasDuplicates is important if (has_duplicates) - TRY(reference.put_value(global_object(), argument_value)); + return reference.put_value(global_object(), argument_value); else - reference.initialize_referenced_binding(global_object(), argument_value); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); - return {}; + return reference.initialize_referenced_binding(global_object(), argument_value); } else if (IsSame const&, decltype(param)>) { // Here the difference from hasDuplicates is important return vm.binding_initialization(param, argument_value, used_environment, global_object()); diff --git a/Userland/Libraries/LibJS/Runtime/Reference.h b/Userland/Libraries/LibJS/Runtime/Reference.h index e4a21b43a7..b022dafc58 100644 --- a/Userland/Libraries/LibJS/Runtime/Reference.h +++ b/Userland/Libraries/LibJS/Runtime/Reference.h @@ -122,11 +122,12 @@ public: return m_base_type == BaseType::Environment; } - void initialize_referenced_binding(GlobalObject& global_object, Value value) const + // 6.2.4.8 InitializeReferencedBinding ( V, W ), https://tc39.es/ecma262/#sec-object.prototype.hasownproperty + ThrowCompletionOr initialize_referenced_binding(GlobalObject& global_object, Value value) const { VERIFY(!is_unresolvable()); VERIFY(m_base_type == BaseType::Environment); - (void)m_base_environment->initialize_binding(global_object, m_name.as_string(), value); + return m_base_environment->initialize_binding(global_object, m_name.as_string(), value); } ThrowCompletionOr put_value(GlobalObject&, Value); diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index 52f21683da..6be945d8c5 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -239,11 +239,9 @@ ThrowCompletionOr VM::property_binding_initialization(BindingPattern const TRY(rest_object->copy_data_properties(object, seen_names, global_object)); if (!environment) - TRY(assignment_target.put_value(global_object, rest_object)); + return assignment_target.put_value(global_object, rest_object); else - assignment_target.initialize_referenced_binding(global_object, rest_object); - - break; + return assignment_target.initialize_referenced_binding(global_object, rest_object); } PropertyKey name; @@ -283,7 +281,7 @@ ThrowCompletionOr VM::property_binding_initialization(BindingPattern const if (!environment) TRY(reference.put_value(global_object, value_to_assign)); else - reference.initialize_referenced_binding(global_object, value_to_assign); + TRY(reference.initialize_referenced_binding(global_object, value_to_assign)); continue; } @@ -320,10 +318,7 @@ ThrowCompletionOr VM::property_binding_initialization(BindingPattern const if (!environment) TRY(reference_to_assign_to->put_value(global_object, value_to_assign)); else - reference_to_assign_to->initialize_referenced_binding(global_object, value_to_assign); - - if (auto* thrown_exception = exception()) - return JS::throw_completion(thrown_exception->value()); + TRY(reference_to_assign_to->initialize_referenced_binding(global_object, value_to_assign)); } } @@ -418,10 +413,7 @@ ThrowCompletionOr VM::iterator_binding_initialization(BindingPattern const if (!environment) TRY(assignment_target->put_value(global_object, value)); else - assignment_target->initialize_referenced_binding(global_object, value); - - if (auto* thrown_exception = exception()) - return JS::throw_completion(thrown_exception->value()); + TRY(assignment_target->initialize_referenced_binding(global_object, value)); } }