1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:18:12 +00:00

LibJS: Convert create_global_function_binding() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-12-29 16:02:44 +01:00
parent 4767be1459
commit 87a89e7126
4 changed files with 8 additions and 18 deletions

View file

@ -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)

View file

@ -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()));

View file

@ -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 {};
}
}

View file

@ -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; }