Kaynağa Gözat

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.
Linus Groh 4 yıl önce
ebeveyn
işleme
8d7ec28924

+ 8 - 0
Userland/Libraries/LibJS/Runtime/StringObject.cpp

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

+ 1 - 0
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; }

+ 0 - 9
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));

+ 0 - 2
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);

+ 2 - 1
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);
     });
 });