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

LibJS: Add a PropertyName class that represents a string or a number

Now that we have two separate storages for Object properties depending
on what kind of index they have, it's nice to have an abstraction that
still allows us to say "here's a property name".

We use PropertyName to always choose the optimal storage path directly
while interpreting the AST. :^)
This commit is contained in:
Andreas Kling 2020-04-06 17:08:23 +02:00
parent 90ba0145f6
commit be019f28ca
5 changed files with 91 additions and 4 deletions

View file

@ -150,6 +150,13 @@ Optional<Value> Object::get(const FlyString& property_name) const
return {};
}
Optional<Value> Object::get(PropertyName property_name) const
{
if (property_name.is_number())
return get_by_index(property_name.as_number());
return get(property_name.as_string());
}
void Object::put_by_index(i32 property_index, Value value)
{
if (property_index < 0)
@ -189,6 +196,13 @@ void Object::put(const FlyString& property_name, Value value)
put_own_property(*this, property_name, value);
}
void Object::put(PropertyName property_name, Value value)
{
if (property_name.is_number())
return put_by_index(property_name.as_number(), value);
return put(property_name.as_string(), value);
}
void Object::put_native_function(const FlyString& property_name, AK::Function<Value(Interpreter&)> native_function, i32 length)
{
auto* function = heap().allocate<NativeFunction>(move(native_function));