1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 04:07:34 +00:00

LibJS: Remove String.prototype.length

A string's length property is supposed to be a regular non-writable,
non-enumerable, non-configurable property on the StringObject instead.
This commit is contained in:
Linus Groh 2021-06-06 16:46:14 +01:00 committed by Andreas Kling
parent fc2673d111
commit 8d7ec28924
5 changed files with 11 additions and 12 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* 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);

View file

@ -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; }

View file

@ -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));

View file

@ -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);

View file

@ -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);
});
});