LibJS: Update Object::define_accessor() to take both getter and setter

This replaces the current 'function plus boolean indicating the type'
API, which makes it easier to set both getter and setter at once.
This was already possible before but required two calls of this
function, which wasn't intuitive:

    define_accessor(name, getter, true, ...);
    define_accessor(name, setter, false, ...);

Which now becomes:

    define_accessor(name, getter, setter, ...);
This commit is contained in:
Linus Groh 2021-04-10 18:35:29 +02:00 committed by Andreas Kling
commit 275da6fcc9
Notes: sideshowbarker 2024-07-18 20:35:07 +09:00
3 changed files with 36 additions and 29 deletions

View file

@ -536,7 +536,7 @@ bool Object::define_property(const PropertyName& property_name, Value value, Pro
return put_own_property(property_name.to_string_or_symbol(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
}
bool Object::define_accessor(const PropertyName& property_name, Function& getter_or_setter, bool is_getter, PropertyAttributes attributes, bool throw_exceptions)
bool Object::define_accessor(const PropertyName& property_name, Function* getter, Function* setter, PropertyAttributes attributes, bool throw_exceptions)
{
VERIFY(property_name.is_valid());
@ -548,18 +548,18 @@ bool Object::define_accessor(const PropertyName& property_name, Function& getter
accessor = &existing_property.as_accessor();
}
if (!accessor) {
accessor = Accessor::create(vm(), nullptr, nullptr);
accessor = Accessor::create(vm(), getter, setter);
bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
if (vm().exception())
return {};
if (!definition_success)
return false;
} else {
if (getter)
accessor->set_getter(getter);
if (setter)
accessor->set_setter(setter);
}
if (is_getter)
accessor->set_getter(&getter_or_setter);
else
accessor->set_setter(&getter_or_setter);
return true;
}