From 7af1d2c17056417d520c3e94db3151bf45bc82ed Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 20 Apr 2021 18:53:07 +0200 Subject: [PATCH] LibJS: Make Object.getOwnPropertyDescriptor() work with string indexed property E.g. for "0" we have to contruct a PropertyName with Type::Number so it looks in the indexed properties as expected. --- Userland/Libraries/LibJS/Runtime/Object.cpp | 5 +++++ .../Object/Object.getOwnPropertyDescriptor.js | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/Object.cpp b/Userland/Libraries/LibJS/Runtime/Object.cpp index 79e303618b5..af47b795cc5 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.cpp +++ b/Userland/Libraries/LibJS/Runtime/Object.cpp @@ -388,6 +388,11 @@ Optional Object::get_own_property_descriptor(const PropertyN value = existing_value.value().value; attributes = existing_value.value().attributes; } else { + if (property_name.is_string()) { + i32 property_index = property_name.as_string().to_int().value_or(-1); + if (property_index >= 0) + return get_own_property_descriptor(property_index); + } auto metadata = shape().lookup(property_name.to_string_or_symbol()); if (!metadata.has_value()) return {}; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyDescriptor.js b/Userland/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyDescriptor.js index d854c8c75d5..ee1fcef2fbd 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyDescriptor.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Object/Object.getOwnPropertyDescriptor.js @@ -43,6 +43,24 @@ test("setter property", () => { expect(o).toHaveSetterProperty("foo"); }); +test("indexed property", () => { + let o = { 0: "foo" }; + + expect(o).toHaveConfigurableProperty(0); + expect(o).toHaveEnumerableProperty(0); + expect(o).toHaveWritableProperty(0); + expect(o).toHaveValueProperty(0, "foo"); + expect(o).not.toHaveGetterProperty(0); + expect(o).not.toHaveSetterProperty(0); + + expect(o).toHaveConfigurableProperty("0"); + expect(o).toHaveEnumerableProperty("0"); + expect(o).toHaveWritableProperty("0"); + expect(o).toHaveValueProperty("0", "foo"); + expect(o).not.toHaveGetterProperty("0"); + expect(o).not.toHaveSetterProperty("0"); +}); + test("defined property", () => { let o = {};