mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:47:34 +00:00
LibJS: Add Object.{keys,values,entries}()
This commit is contained in:
parent
36a5e0be4b
commit
683a0696f3
7 changed files with 250 additions and 0 deletions
|
@ -108,6 +108,69 @@ Value Object::get_own_property(const Object& this_object, const FlyString& prope
|
|||
return value_here;
|
||||
}
|
||||
|
||||
Value Object::get_enumerable_own_properties(const Object& this_object, GetOwnPropertyMode kind) const
|
||||
{
|
||||
auto* properties_array = Array::create(interpreter().global_object());
|
||||
|
||||
// FIXME: Support generic iterables
|
||||
if (this_object.is_string_object()) {
|
||||
auto str = static_cast<const StringObject&>(this_object).primitive_string().string();
|
||||
|
||||
for (size_t i = 0; i < str.length(); ++i) {
|
||||
if (kind == GetOwnPropertyMode::Key) {
|
||||
properties_array->put_by_index(i, js_string(interpreter(), String::number(i)));
|
||||
} else if (kind == GetOwnPropertyMode::Value) {
|
||||
properties_array->put_by_index(i, js_string(interpreter(), String::format("%c", str[i])));
|
||||
} else {
|
||||
auto* entry_array = Array::create(interpreter().global_object());
|
||||
entry_array->put_by_index(0, js_string(interpreter(), String::number(i)));
|
||||
entry_array->put_by_index(1, js_string(interpreter(), String::format("%c", str[i])));
|
||||
properties_array->put_by_index(i, entry_array);
|
||||
}
|
||||
}
|
||||
|
||||
return properties_array;
|
||||
}
|
||||
|
||||
size_t property_index = 0;
|
||||
for (size_t i = 0; i < m_elements.size(); ++i) {
|
||||
if (m_elements.at(i).is_empty())
|
||||
continue;
|
||||
|
||||
if (kind == GetOwnPropertyMode::Key) {
|
||||
properties_array->put_by_index(property_index, js_string(interpreter(), String::number(i)));
|
||||
} else if (kind == GetOwnPropertyMode::Value) {
|
||||
properties_array->put_by_index(property_index, m_elements.at(i));
|
||||
} else {
|
||||
auto* entry_array = Array::create(interpreter().global_object());
|
||||
entry_array->put_by_index(0, js_string(interpreter(), String::number(i)));
|
||||
entry_array->put_by_index(1, m_elements.at(i));
|
||||
properties_array->put_by_index(property_index, entry_array);
|
||||
}
|
||||
|
||||
++property_index;
|
||||
}
|
||||
|
||||
for (auto& it : this_object.shape().property_table_ordered()) {
|
||||
if (it.value.attributes & Attribute::Enumerable) {
|
||||
size_t offset = it.value.offset + property_index;
|
||||
|
||||
if (kind == GetOwnPropertyMode::Key) {
|
||||
properties_array->put_by_index(offset, js_string(interpreter(), it.key));
|
||||
} else if (kind == GetOwnPropertyMode::Value) {
|
||||
properties_array->put_by_index(offset, this_object.get(it.key));
|
||||
} else {
|
||||
auto* entry_array = Array::create(interpreter().global_object());
|
||||
entry_array->put_by_index(0, js_string(interpreter(), it.key));
|
||||
entry_array->put_by_index(1, this_object.get(it.key));
|
||||
properties_array->put_by_index(offset, entry_array);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return properties_array;
|
||||
}
|
||||
|
||||
void Object::set_shape(Shape& new_shape)
|
||||
{
|
||||
m_storage.resize(new_shape.property_count());
|
||||
|
|
|
@ -61,6 +61,13 @@ public:
|
|||
|
||||
Value get_own_property(const Object& this_object, const FlyString& property_name) const;
|
||||
|
||||
enum class GetOwnPropertyMode {
|
||||
Key,
|
||||
Value,
|
||||
KeyAndValue,
|
||||
};
|
||||
Value get_enumerable_own_properties(const Object& this_object, GetOwnPropertyMode) const;
|
||||
|
||||
enum class PutOwnPropertyMode {
|
||||
Put,
|
||||
DefineProperty,
|
||||
|
|
|
@ -47,6 +47,9 @@ ObjectConstructor::ObjectConstructor()
|
|||
put_native_function("getOwnPropertyNames", get_own_property_names, 1, attr);
|
||||
put_native_function("getPrototypeOf", get_prototype_of, 1, attr);
|
||||
put_native_function("setPrototypeOf", set_prototype_of, 2, attr);
|
||||
put_native_function("keys", keys, 1, attr);
|
||||
put_native_function("values", values, 1, attr);
|
||||
put_native_function("entries", entries, 1, attr);
|
||||
}
|
||||
|
||||
ObjectConstructor::~ObjectConstructor()
|
||||
|
@ -165,4 +168,40 @@ Value ObjectConstructor::is(Interpreter& interpreter)
|
|||
return typed_eq(interpreter, value1, value2);
|
||||
}
|
||||
|
||||
Value ObjectConstructor::keys(Interpreter& interpreter)
|
||||
{
|
||||
if (!interpreter.argument_count())
|
||||
return interpreter.throw_exception<TypeError>("Can't convert undefined to object");
|
||||
|
||||
auto* obj_arg = interpreter.argument(0).to_object(interpreter.heap());
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
return obj_arg->get_enumerable_own_properties(*obj_arg, GetOwnPropertyMode::Key);
|
||||
}
|
||||
|
||||
Value ObjectConstructor::values(Interpreter& interpreter)
|
||||
{
|
||||
if (!interpreter.argument_count())
|
||||
return interpreter.throw_exception<TypeError>("Can't convert undefined to object");
|
||||
|
||||
auto* obj_arg = interpreter.argument(0).to_object(interpreter.heap());
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
return obj_arg->get_enumerable_own_properties(*obj_arg, GetOwnPropertyMode::Value);
|
||||
}
|
||||
|
||||
Value ObjectConstructor::entries(Interpreter& interpreter)
|
||||
{
|
||||
if (!interpreter.argument_count())
|
||||
return interpreter.throw_exception<TypeError>("Can't convert undefined to object");
|
||||
|
||||
auto* obj_arg = interpreter.argument(0).to_object(interpreter.heap());
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
return obj_arg->get_enumerable_own_properties(*obj_arg, GetOwnPropertyMode::KeyAndValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -48,6 +48,9 @@ private:
|
|||
static Value get_own_property_names(Interpreter&);
|
||||
static Value get_prototype_of(Interpreter&);
|
||||
static Value set_prototype_of(Interpreter&);
|
||||
static Value keys(Interpreter&);
|
||||
static Value values(Interpreter&);
|
||||
static Value entries(Interpreter&);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue