Truly independent web browser
LibJS: Recognize string class keys whose value is constructor
Shannon Booth0ad42b741bca
diff --git a/Libraries/LibJS/Rust/src/parser/stmt.rs b/Libraries/LibJS/Rust/src/parser/stmt.rsindex 7869648a7c3..324cee40b75 100644--- a/Libraries/LibJS/Rust/src/parser/stmt.rs+++ b/Libraries/LibJS/Rust/src/parser/stmt.rs@@ -1447,16 +1447,17 @@ impl<U: Unit> Parser<'_, U> {is_name_like(self.tok) || matches!(self.tok, T::PrivateName | T::Num | T::BigInt | T::Str | T::LBracket)}- fn key_is(&self, key: u32, computed: bool, word: u32, text: &[u8]) -> bool {+ fn key_is(&self, key: u32, computed: bool, word: u32, text: &str) -> bool {if computed {return false;}match self.ast.tag[key as usize] {tag::IDENT | tag::IDENT_KEY => self.ast.a[key as usize] == word,tag::STR | tag::STR_KEY => {- let start = self.ast.pos[key as usize] as usize;- self.ast.a[key as usize] as usize - start == text.len() + 2- && (0..text.len()).all(|i| self.at(start + 1 + i) == text[i] as u32)+ // NB: The name of the property is the value of the string, which escape sequences spell out as well.+ let start = self.ast.pos[key as usize] as usize + 1;+ let units = self.source_view(start, self.ast.a[key as usize] as usize - 1);+ super::literals::string_literal_text(units).view() == text}_ => false,}@@ -1526,7 +1527,7 @@ impl<U: Unit> Parser<'_, U> {if self.tok == T::LParen || accessor != 0 || is_generator || is_async {let is_constructor =- !is_static && private_name == NONE && self.key_is(key, computed, N_CONSTRUCTOR, b"constructor");+ !is_static && private_name == NONE && self.key_is(key, computed, N_CONSTRUCTOR, "constructor");if is_constructor {if accessor != 0 {self.fail(key_start, "Constructor can't have a get or set modifier");@@ -1538,7 +1539,7 @@ impl<U: Unit> Parser<'_, U> {self.fail(key_start, "Duplicate constructor in the same class");}*had_constructor = true;- } else if is_static && private_name == NONE && self.key_is(key, computed, N_PROTOTYPE, b"prototype") {+ } else if is_static && private_name == NONE && self.key_is(key, computed, N_PROTOTYPE, "prototype") {self.fail(key_start, "Classes may not have a static property named 'prototype'");}if private_name != NONE {@@ -1577,9 +1578,9 @@ impl<U: Unit> Parser<'_, U> {};}- if self.key_is(key, computed, N_CONSTRUCTOR, b"constructor") {+ if self.key_is(key, computed, N_CONSTRUCTOR, "constructor") {self.fail(key_start, "Classes can't have a field named 'constructor'");- } else if is_static && self.key_is(key, computed, N_PROTOTYPE, b"prototype") {+ } else if is_static && self.key_is(key, computed, N_PROTOTYPE, "prototype") {self.fail(key_start, "Classes can't have a static field named 'prototype'");}if private_name != NONE {diff --git a/Tests/LibJS/Runtime/classes/class-constructor.js b/Tests/LibJS/Runtime/classes/class-constructor.jsindex 98d70081c40..e1075419b5a 100644--- a/Tests/LibJS/Runtime/classes/class-constructor.js+++ b/Tests/LibJS/Runtime/classes/class-constructor.js@@ -72,3 +72,28 @@ test("can call constructor without parentheses", () => {// prettier-ignoreexpect(new A).toBeInstanceOf(A);});++test("a string literal key whose value is constructor names the constructor", () => {+ class A {+ "construct\u006fr"() {+ this.x = 1;+ }+ }+ expect(new A().x).toBe(1);+ expect(A.prototype.constructor).toBe(A);++ class B extends A {+ "\u0063onstructor"() {+ super();+ this.y = 2;+ }+ }+ const b = new B();+ expect([b.x, b.y]).toEqual([1, 2]);++ expect(`class C { "construct\\u006fr"() {} constructor() {} }`).not.toEval();+ expect(`class C { get "construct\\u006fr"() {} }`).not.toEval();+ expect(`class C { "construct\\u006fr" = 1; }`).not.toEval();+ expect(`class C { static "prot\\u006ftype"() {} }`).not.toEval();+ expect(`class C { static "prot\\u006ftype" = 1; }`).not.toEval();+});