diff options
author | Linus Groh <mail@linusgroh.de> | 2021-12-29 15:54:44 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-12-29 15:54:44 +0100 |
commit | 215a56b0e4ae8d720dce08a2ab4ee1c6ec104cc4 (patch) | |
tree | cd58ed612b022935f5211825fc3ba68ed8088cdb /Userland | |
parent | 1817c1f83ca3df98120d1e06d6906d579be8a214 (diff) | |
download | serenity-215a56b0e4ae8d720dce08a2ab4ee1c6ec104cc4.zip |
LibJS: Convert can_declare_global_var() to ThrowCompletionOr
Diffstat (limited to 'Userland')
4 files changed, 15 insertions, 10 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index d72ec95e73..7aa96cc4f1 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -3650,9 +3650,10 @@ ThrowCompletionOr<void> Program::global_declaration_instantiation(Interpreter& i if (declared_function_names.contains(name)) return IterationDecision::Continue; - auto var_definable = global_environment.can_declare_global_var(name); - if (interpreter.exception()) + auto var_definable_or_error = global_environment.can_declare_global_var(name); + if (var_definable_or_error.is_error()) return IterationDecision::Break; + auto var_definable = var_definable_or_error.release_value(); if (!var_definable) { interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::CannotDeclareGlobalVariable, name); diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 769cf84c40..8650edb5de 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -660,9 +660,12 @@ ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, GlobalObject& glo if (global_var_environment) { if (global_var_environment->has_lexical_declaration(function_name)) return IterationDecision::Continue; - auto var_definable = global_var_environment->can_declare_global_var(function_name); - if (vm.exception()) + + auto var_definable_or_error = global_var_environment->can_declare_global_var(function_name); + if (var_definable_or_error.is_error()) return IterationDecision::Break; + auto var_definable = var_definable_or_error.release_value(); + if (!var_definable) return IterationDecision::Continue; } @@ -697,9 +700,10 @@ ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, GlobalObject& glo declaration.for_each_bound_name([&](auto const& name) { if (!declared_function_names.contains(name)) { if (global_var_environment) { - auto variable_definable = global_var_environment->can_declare_global_var(name); - if (vm.exception()) + auto variable_definable_or_error = global_var_environment->can_declare_global_var(name); + if (variable_definable_or_error.is_error()) return IterationDecision::Break; + auto variable_definable = variable_definable_or_error.release_value(); if (!variable_definable) { vm.throw_exception<TypeError>(global_object, ErrorType::CannotDeclareGlobalVariable, name); return IterationDecision::Break; diff --git a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp index 709e90a345..12d5fa2605 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp @@ -197,21 +197,21 @@ ThrowCompletionOr<bool> GlobalEnvironment::has_restricted_global_property(FlyStr } // 9.1.1.4.15 CanDeclareGlobalVar ( N ), https://tc39.es/ecma262/#sec-candeclareglobalvar -bool GlobalEnvironment::can_declare_global_var(FlyString const& name) const +ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_var(FlyString const& name) const { // 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). - bool has_property = TRY_OR_DISCARD(global_object.has_own_property(name)); + bool has_property = TRY(global_object.has_own_property(name)); // 4. If hasProperty is true, return true. if (has_property) return true; // 5. Return ? IsExtensible(globalObject). - return TRY_OR_DISCARD(global_object.is_extensible()); + return global_object.is_extensible(); } // 9.1.1.4.16 CanDeclareGlobalFunction ( N ), https://tc39.es/ecma262/#sec-candeclareglobalfunction diff --git a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h index 1abe82948f..1a6a00cae3 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h +++ b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h @@ -34,7 +34,7 @@ public: bool has_var_declaration(FlyString const& name) const; bool has_lexical_declaration(FlyString const& name) const; ThrowCompletionOr<bool> has_restricted_global_property(FlyString const& name) const; - bool can_declare_global_var(FlyString const& name) const; + ThrowCompletionOr<bool> can_declare_global_var(FlyString const& name) const; bool can_declare_global_function(FlyString const& name) const; void create_global_var_binding(FlyString const& name, bool can_be_deleted); void create_global_function_binding(FlyString const& name, Value, bool can_be_deleted); |