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

LibJS: Convert can_declare_global_function() to ThrowCompletionOr

This commit is contained in:
Linus Groh 2021-12-29 15:56:53 +01:00
parent 215a56b0e4
commit 8296d3fbd2
4 changed files with 14 additions and 12 deletions

View file

@ -3626,10 +3626,10 @@ ThrowCompletionOr<void> Program::global_declaration_instantiation(Interpreter& i
if (declared_function_names.set(function.name()) != AK::HashSetResult::InsertedNewEntry)
return IterationDecision::Continue;
auto function_definable = global_environment.can_declare_global_function(function.name());
if (interpreter.exception())
auto function_definable_or_error = global_environment.can_declare_global_function(function.name());
if (function_definable_or_error.is_error())
return IterationDecision::Break;
auto function_definable = function_definable_or_error.release_value();
if (!function_definable) {
interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::CannotDeclareGlobalFunction, function.name());
@ -3677,10 +3677,10 @@ ThrowCompletionOr<void> Program::global_declaration_instantiation(Interpreter& i
if (global_environment.has_lexical_declaration(function_name))
return IterationDecision::Continue;
auto function_definable = global_environment.can_declare_global_function(function_name);
if (interpreter.exception())
auto function_definable_or_error = global_environment.can_declare_global_function(function_name);
if (function_definable_or_error.is_error())
return IterationDecision::Break;
auto function_definable = function_definable_or_error.release_value();
if (!function_definable) {
interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::CannotDeclareGlobalFunction, function_name);

View file

@ -627,9 +627,11 @@ ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, GlobalObject& glo
return IterationDecision::Continue;
if (global_var_environment) {
auto function_definable = global_var_environment->can_declare_global_function(function.name());
if (vm.exception())
auto function_definable_or_error = global_var_environment->can_declare_global_function(function.name());
if (function_definable_or_error.is_error())
return IterationDecision::Break;
auto function_definable = function_definable_or_error.release_value();
if (!function_definable) {
vm.throw_exception<TypeError>(global_object, ErrorType::CannotDeclareGlobalFunction, function.name());
return IterationDecision::Break;

View file

@ -215,18 +215,18 @@ ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_var(FlyString cons
}
// 9.1.1.4.16 CanDeclareGlobalFunction ( N ), https://tc39.es/ecma262/#sec-candeclareglobalfunction
bool GlobalEnvironment::can_declare_global_function(FlyString const& name) const
ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_function(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 existingProp be ? globalObject.[[GetOwnProperty]](N).
auto existing_prop = TRY_OR_DISCARD(global_object.internal_get_own_property(name));
auto existing_prop = TRY(global_object.internal_get_own_property(name));
// 4. If existingProp is undefined, return ? IsExtensible(globalObject).
if (!existing_prop.has_value())
return TRY_OR_DISCARD(global_object.is_extensible());
return TRY(global_object.is_extensible());
// 5. If existingProp.[[Configurable]] is true, return true.
if (*existing_prop->configurable)

View file

@ -35,7 +35,7 @@ public:
bool has_lexical_declaration(FlyString const& name) const;
ThrowCompletionOr<bool> has_restricted_global_property(FlyString const& name) const;
ThrowCompletionOr<bool> can_declare_global_var(FlyString const& name) const;
bool can_declare_global_function(FlyString const& name) const;
ThrowCompletionOr<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);