diff --git a/Userland/Libraries/LibJS/Runtime/StringObject.cpp b/Userland/Libraries/LibJS/Runtime/StringObject.cpp index 00795dfc95..d73d8bad43 100644 --- a/Userland/Libraries/LibJS/Runtime/StringObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringObject.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2021, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -25,6 +26,13 @@ StringObject::~StringObject() { } +void StringObject::initialize(GlobalObject& global_object) +{ + auto& vm = this->vm(); + Object::initialize(global_object); + define_property(vm.names.length, Value(m_string.string().length()), 0); +} + void StringObject::visit_edges(Cell::Visitor& visitor) { Object::visit_edges(visitor); diff --git a/Userland/Libraries/LibJS/Runtime/StringObject.h b/Userland/Libraries/LibJS/Runtime/StringObject.h index af27e19eff..8c7ee5cc94 100644 --- a/Userland/Libraries/LibJS/Runtime/StringObject.h +++ b/Userland/Libraries/LibJS/Runtime/StringObject.h @@ -17,6 +17,7 @@ public: static StringObject* create(GlobalObject&, PrimitiveString&); StringObject(PrimitiveString&, Object& prototype); + virtual void initialize(GlobalObject&) override; virtual ~StringObject() override; const PrimitiveString& primitive_string() const { return m_string; } diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index 1af489e334..e4d33b3933 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -52,7 +52,6 @@ void StringPrototype::initialize(GlobalObject& global_object) StringObject::initialize(global_object); u8 attr = Attribute::Writable | Attribute::Configurable; - define_native_property(vm.names.length, length_getter, nullptr, 0); define_native_function(vm.names.charAt, char_at, 1, attr); define_native_function(vm.names.charCodeAt, char_code_at, 1, attr); define_native_function(vm.names.repeat, repeat, 1, attr); @@ -274,14 +273,6 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_uppercase) return js_string(vm, string.to_uppercase()); } -JS_DEFINE_NATIVE_FUNCTION(StringPrototype::length_getter) -{ - auto string_value = this_string_value(global_object, vm.this_value(global_object)); - if (vm.exception()) - return {}; - return Value((i32)string_value.as_string().string().length()); -} - JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_string) { return this_string_value(global_object, vm.this_value(global_object)); diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.h b/Userland/Libraries/LibJS/Runtime/StringPrototype.h index d9a051e4a3..95da408160 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.h @@ -19,8 +19,6 @@ public: virtual ~StringPrototype() override; private: - JS_DECLARE_NATIVE_GETTER(length_getter); - JS_DECLARE_NATIVE_FUNCTION(char_at); JS_DECLARE_NATIVE_FUNCTION(char_code_at); JS_DECLARE_NATIVE_FUNCTION(repeat); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Reflect/Reflect.get.js b/Userland/Libraries/LibJS/Tests/builtins/Reflect/Reflect.get.js index fc9b5f172e..b26e9fe888 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Reflect/Reflect.get.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Reflect/Reflect.get.js @@ -62,6 +62,7 @@ describe("normal behavior", () => { }); test("native getter function", () => { - expect(Reflect.get(String.prototype, "length", "foo")).toBe(3); + const typedArray = new Uint8Array(3); + expect(Reflect.get(Uint8Array.prototype, "length", typedArray)).toBe(3); }); });