mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 05:38:11 +00:00
LibJS: Add Object::define_accessor()
This is a helper function based on the getter/setter definition logic from ObjectExpression::execute() to look up an Accessor property if it already exists, define a new Accessor property if it doesn't exist, and set the getter or setter function on the Accessor.
This commit is contained in:
parent
949bffdc93
commit
a535d58cac
3 changed files with 29 additions and 17 deletions
|
@ -427,6 +427,31 @@ bool Object::define_property(PropertyName property_name, Value value, PropertyAt
|
|||
return put_own_property(*this, property_name.as_string(), value, attributes, PutOwnPropertyMode::DefineProperty, throw_exceptions);
|
||||
}
|
||||
|
||||
bool Object::define_accessor(PropertyName property_name, Function& getter_or_setter, bool is_getter, PropertyAttributes attributes, bool throw_exceptions)
|
||||
{
|
||||
Accessor* accessor { nullptr };
|
||||
auto property_metadata = shape().lookup(property_name.as_string());
|
||||
if (property_metadata.has_value()) {
|
||||
auto existing_property = get_direct(property_metadata.value().offset);
|
||||
if (existing_property.is_accessor())
|
||||
accessor = &existing_property.as_accessor();
|
||||
}
|
||||
if (!accessor) {
|
||||
accessor = Accessor::create(interpreter(), nullptr, nullptr);
|
||||
bool definition_success = define_property(property_name, accessor, attributes, throw_exceptions);
|
||||
if (interpreter().exception())
|
||||
return {};
|
||||
if (!definition_success)
|
||||
return false;
|
||||
}
|
||||
if (is_getter)
|
||||
accessor->set_getter(&getter_or_setter);
|
||||
else
|
||||
accessor->set_setter(&getter_or_setter);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Object::put_own_property(Object& this_object, const FlyString& property_name, Value value, PropertyAttributes attributes, PutOwnPropertyMode mode, bool throw_exceptions)
|
||||
{
|
||||
ASSERT(!(mode == PutOwnPropertyMode::Put && value.is_accessor()));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue