diff options
author | Linus Groh <mail@linusgroh.de> | 2021-12-29 16:02:44 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-12-29 16:02:44 +0100 |
commit | 87a89e712635a354c6799e10ea0781c7db83149f (patch) | |
tree | 37f905aa66d692d799235a0f385019b96b39e0b2 /Userland | |
parent | 4767be1459546908ec39e781e8aed106adeb4a12 (diff) | |
download | serenity-87a89e712635a354c6799e10ea0781c7db83149f.zip |
LibJS: Convert create_global_function_binding() to ThrowCompletionOr
Diffstat (limited to 'Userland')
4 files changed, 8 insertions, 18 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 6b02e58f4a..6d7fcfce78 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -3725,9 +3725,7 @@ ThrowCompletionOr<void> Program::global_declaration_instantiation(Interpreter& i for (auto& declaration : functions_to_initialize) { auto* function = ECMAScriptFunctionObject::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), &global_environment, private_environment, declaration.kind(), declaration.is_strict_mode(), declaration.might_need_arguments_object(), declaration.contains_direct_call_to_eval()); - global_environment.create_global_function_binding(declaration.name(), function, false); - if (auto* exception = interpreter.exception()) - return throw_completion(exception->value()); + TRY(global_environment.create_global_function_binding(declaration.name(), function, false)); } for (auto& var_name : declared_var_names) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 54ea48a559..52b272327b 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -746,9 +746,7 @@ ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, GlobalObject& glo for (auto& declaration : functions_to_initialize) { auto* function = ECMAScriptFunctionObject::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), lexical_environment, private_environment, declaration.kind(), declaration.is_strict_mode(), declaration.might_need_arguments_object()); if (global_var_environment) { - global_var_environment->create_global_function_binding(declaration.name(), function, true); - if (auto* exception = vm.exception()) - return throw_completion(exception->value()); + TRY(global_var_environment->create_global_function_binding(declaration.name(), function, true)); } else { auto binding_exists = MUST(variable_environment->has_binding(declaration.name())); diff --git a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp index c9249558f6..5031b6342d 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp +++ b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.cpp @@ -274,17 +274,14 @@ ThrowCompletionOr<void> GlobalEnvironment::create_global_var_binding(FlyString c } // 9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D ), https://tc39.es/ecma262/#sec-createglobalfunctionbinding -void GlobalEnvironment::create_global_function_binding(FlyString const& name, Value value, bool can_be_deleted) +ThrowCompletionOr<void> GlobalEnvironment::create_global_function_binding(FlyString const& name, Value value, 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 existingProp be ? globalObject.[[GetOwnProperty]](N). - auto existing_prop_or_error = global_object.internal_get_own_property(name); - if (existing_prop_or_error.is_error()) - return; - auto existing_prop = existing_prop_or_error.release_value(); + auto existing_prop = TRY(global_object.internal_get_own_property(name)); PropertyDescriptor desc; @@ -300,14 +297,10 @@ void GlobalEnvironment::create_global_function_binding(FlyString const& name, Va } // 6. Perform ? DefinePropertyOrThrow(globalObject, N, desc). - auto result_or_error = global_object.define_property_or_throw(name, desc); - if (result_or_error.is_error()) - return; + TRY(global_object.define_property_or_throw(name, desc)); // 7. Perform ? Set(globalObject, N, V, false). - result_or_error = global_object.set(name, value, Object::ShouldThrowExceptions::Yes); - if (result_or_error.is_error()) - return; + TRY(global_object.set(name, value, Object::ShouldThrowExceptions::Yes)); // 8. Let varDeclaredNames be envRec.[[VarNames]]. // 9. If varDeclaredNames does not contain N, then @@ -317,6 +310,7 @@ void GlobalEnvironment::create_global_function_binding(FlyString const& name, Va } // 10. Return NormalCompletion(empty). + return {}; } } diff --git a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h index ce77168a82..51a20f7b2b 100644 --- a/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h +++ b/Userland/Libraries/LibJS/Runtime/GlobalEnvironment.h @@ -37,7 +37,7 @@ public: ThrowCompletionOr<bool> can_declare_global_var(FlyString const& name) const; ThrowCompletionOr<bool> can_declare_global_function(FlyString const& name) const; ThrowCompletionOr<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); + ThrowCompletionOr<void> create_global_function_binding(FlyString const& name, Value, bool can_be_deleted); private: virtual bool is_global_environment() const override { return true; } |