diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 8234fb83c3..6b02e58f4a 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -3688,8 +3688,8 @@ ThrowCompletionOr Program::global_declaration_instantiation(Interpreter& i } if (!declared_function_names.contains(function_name) && !declared_var_names.contains(function_name)) { - global_environment.create_global_var_binding(function_name, false); - if (interpreter.exception()) + auto result = global_environment.create_global_var_binding(function_name, false); + if (result.is_error()) return IterationDecision::Break; declared_function_names.set(function_name); } @@ -3730,11 +3730,8 @@ ThrowCompletionOr Program::global_declaration_instantiation(Interpreter& i return throw_completion(exception->value()); } - for (auto& var_name : declared_var_names) { - global_environment.create_global_var_binding(var_name, false); - if (auto* exception = interpreter.exception()) - return throw_completion(exception->value()); - } + for (auto& var_name : declared_var_names) + TRY(global_environment.create_global_var_binding(var_name, false)); return {}; } diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 52b784658c..54ea48a559 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -674,8 +674,8 @@ ThrowCompletionOr eval_declaration_instantiation(VM& vm, GlobalObject& glo if (!declared_function_names.contains(function_name) && !hoisted_functions.contains(function_name)) { if (global_var_environment) { - global_var_environment->create_global_var_binding(function_name, true); - if (vm.exception()) + auto result = global_var_environment->create_global_var_binding(function_name, true); + if (result.is_error()) return IterationDecision::Break; } else { if (!MUST(variable_environment->has_binding(function_name))) { @@ -763,9 +763,7 @@ ThrowCompletionOr eval_declaration_instantiation(VM& vm, GlobalObject& glo for (auto& var_name : declared_var_names) { if (global_var_environment) { - global_var_environment->create_global_var_binding(var_name, true); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + TRY(global_var_environment->create_global_var_binding(var_name, true)); } else { auto binding_exists = MUST(variable_environment->has_binding(var_name)); diff --git a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp index 5f9ab7dea0..c9249558f6 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp @@ -241,35 +241,25 @@ ThrowCompletionOr GlobalEnvironment::can_declare_global_function(FlyString } // 9.1.1.4.17 CreateGlobalVarBinding ( N, D ), https://tc39.es/ecma262/#sec-createglobalvarbinding -void GlobalEnvironment::create_global_var_binding(FlyString const& name, bool can_be_deleted) +ThrowCompletionOr GlobalEnvironment::create_global_var_binding(FlyString const& name, bool can_be_deleted) { // 1. Let ObjRec be envRec.[[ObjectRecord]]. // 2. Let globalObject be ObjRec.[[BindingObject]]. auto& global_object = m_object_record->binding_object(); // 3. Let hasProperty be ? HasOwnProperty(globalObject, N). - auto has_property_or_error = global_object.has_own_property(name); - if (has_property_or_error.is_error()) - return; - auto has_property = has_property_or_error.release_value(); + auto has_property = TRY(global_object.has_own_property(name)); // 4. Let extensible be ? IsExtensible(globalObject). - auto extensible_or_error = global_object.is_extensible(); - if (extensible_or_error.is_error()) - return; - auto extensible = extensible_or_error.release_value(); + auto extensible = TRY(global_object.is_extensible()); // 5. If hasProperty is false and extensible is true, then if (!has_property && extensible) { // a. Perform ? ObjRec.CreateMutableBinding(N, D). - auto result = m_object_record->create_mutable_binding(m_object_record->global_object(), name, can_be_deleted); - if (result.is_error()) - return; + TRY(m_object_record->create_mutable_binding(m_object_record->global_object(), name, can_be_deleted)); // b. Perform ? ObjRec.InitializeBinding(N, undefined). - result = m_object_record->initialize_binding(m_object_record->global_object(), name, js_undefined()); - if (result.is_error()) - return; + TRY(m_object_record->initialize_binding(m_object_record->global_object(), name, js_undefined())); } // 6. Let varDeclaredNames be envRec.[[VarNames]]. @@ -280,6 +270,7 @@ void GlobalEnvironment::create_global_var_binding(FlyString const& name, bool ca } // 8. Return NormalCompletion(empty). + return {}; } // 9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D ), https://tc39.es/ecma262/#sec-createglobalfunctionbinding diff --git a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h index e82debaf57..ce77168a82 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h +++ b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h @@ -36,7 +36,7 @@ public: ThrowCompletionOr has_restricted_global_property(FlyString const& name) const; ThrowCompletionOr can_declare_global_var(FlyString const& name) const; ThrowCompletionOr can_declare_global_function(FlyString const& name) const; - void create_global_var_binding(FlyString const& name, bool can_be_deleted); + ThrowCompletionOr create_global_var_binding(FlyString const& name, bool can_be_deleted); void create_global_function_binding(FlyString const& name, Value, bool can_be_deleted); private: