1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-17 08:47:35 +00:00

LibJS: Add the Object.getOwnPropertySymbols method

This commit is contained in:
Idan Horowitz 2021-06-12 17:41:13 +03:00 committed by Linus Groh
parent a2da3f97ef
commit bd9e20ef79
5 changed files with 44 additions and 15 deletions

View file

@ -137,6 +137,7 @@ namespace JS {
P(getMonth) \ P(getMonth) \
P(getOwnPropertyDescriptor) \ P(getOwnPropertyDescriptor) \
P(getOwnPropertyNames) \ P(getOwnPropertyNames) \
P(getOwnPropertySymbols) \
P(getPrototypeOf) \ P(getPrototypeOf) \
P(getSeconds) \ P(getSeconds) \
P(getTime) \ P(getTime) \

View file

@ -307,23 +307,25 @@ MarkedValueList Object::get_own_properties(PropertyKind kind, bool only_enumerab
return properties; return properties;
} }
for (auto& entry : m_indexed_properties) { if (return_type != GetOwnPropertyReturnType::SymbolOnly) {
auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(this)); for (auto& entry : m_indexed_properties) {
if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable()) auto value_and_attributes = entry.value_and_attributes(const_cast<Object*>(this));
continue; if (only_enumerable_properties && !value_and_attributes.attributes.is_enumerable())
continue;
if (kind == PropertyKind::Key) { if (kind == PropertyKind::Key) {
properties.append(js_string(vm(), String::number(entry.index()))); properties.append(js_string(vm(), String::number(entry.index())));
} else if (kind == PropertyKind::Value) { } else if (kind == PropertyKind::Value) {
properties.append(value_and_attributes.value); properties.append(value_and_attributes.value);
} else { } else {
auto* entry_array = Array::create(global_object()); auto* entry_array = Array::create(global_object());
entry_array->define_property(0, js_string(vm(), String::number(entry.index()))); entry_array->define_property(0, js_string(vm(), String::number(entry.index())));
entry_array->define_property(1, value_and_attributes.value); entry_array->define_property(1, value_and_attributes.value);
properties.append(entry_array); properties.append(entry_array);
}
if (vm().exception())
return MarkedValueList { heap() };
} }
if (vm().exception())
return MarkedValueList { heap() };
} }
auto add_property_to_results = [&](auto& property) { auto add_property_to_results = [&](auto& property) {

View file

@ -33,6 +33,7 @@ void ObjectConstructor::initialize(GlobalObject& global_object)
define_native_function(vm.names.is, is, 2, attr); define_native_function(vm.names.is, is, 2, attr);
define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr); define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
define_native_function(vm.names.getOwnPropertyNames, get_own_property_names, 1, attr); define_native_function(vm.names.getOwnPropertyNames, get_own_property_names, 1, attr);
define_native_function(vm.names.getOwnPropertySymbols, get_own_property_symbols, 1, attr);
define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr); define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr); define_native_function(vm.names.setPrototypeOf, set_prototype_of, 2, attr);
define_native_function(vm.names.isExtensible, is_extensible, 1, attr); define_native_function(vm.names.isExtensible, is_extensible, 1, attr);
@ -74,6 +75,14 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_names)
return Array::create_from(global_object, object->get_own_properties(PropertyKind::Key, false, GetOwnPropertyReturnType::StringOnly)); return Array::create_from(global_object, object->get_own_properties(PropertyKind::Key, false, GetOwnPropertyReturnType::StringOnly));
} }
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_symbols)
{
auto* object = vm.argument(0).to_object(global_object);
if (vm.exception())
return {};
return Array::create_from(global_object, object->get_own_properties(PropertyKind::Key, false, GetOwnPropertyReturnType::SymbolOnly));
}
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of) JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
{ {
auto* object = vm.argument(0).to_object(global_object); auto* object = vm.argument(0).to_object(global_object);

View file

@ -30,6 +30,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(is); JS_DECLARE_NATIVE_FUNCTION(is);
JS_DECLARE_NATIVE_FUNCTION(get_own_property_descriptor); JS_DECLARE_NATIVE_FUNCTION(get_own_property_descriptor);
JS_DECLARE_NATIVE_FUNCTION(get_own_property_names); JS_DECLARE_NATIVE_FUNCTION(get_own_property_names);
JS_DECLARE_NATIVE_FUNCTION(get_own_property_symbols);
JS_DECLARE_NATIVE_FUNCTION(get_prototype_of); JS_DECLARE_NATIVE_FUNCTION(get_prototype_of);
JS_DECLARE_NATIVE_FUNCTION(set_prototype_of); JS_DECLARE_NATIVE_FUNCTION(set_prototype_of);
JS_DECLARE_NATIVE_FUNCTION(is_extensible); JS_DECLARE_NATIVE_FUNCTION(is_extensible);

View file

@ -0,0 +1,16 @@
test("use with array", () => {
let names = Object.getOwnPropertySymbols([1, 2, 3]);
console.log(names);
expect(names).toEqual([]);
});
test("use with object", () => {
let names = Object.getOwnPropertySymbols({ [Symbol.iterator]: 1, [Symbol.species]: 2 });
expect(names).toEqual([Symbol.iterator, Symbol.species]);
});
test("use with object with string keys", () => {
let symbol = Symbol("bar");
let names = Object.getOwnPropertySymbols({ foo: 1, [symbol]: 2, baz: 3 });
expect(names).toEqual([symbol]);
});