Skip to content

Truly independent web browser

LibJS: Throw for a static prototype method before later class keys

Shannon Booth5951787487d7
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
index 2dac5651302..2e3f7539e7e 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
@@ -309,7 +309,14 @@ impl Generator<'_> {
};
element_keys.push(match key {
Some(key) if private_name_of(key).is_none() => {
- Some(self.sweep_class_element_key(key, first_element_row)?)
+ let operand = self.sweep_class_element_key(key, first_element_row)?;
+ if let ClassElementData::Method { is_static: true, .. } = element_data
+ && let ClassElementKey::Expression(expression) = key
+ && !matches!(tree.node_tag(expression), tag::STR_KEY | tag::NUM_KEY)
+ {
+ self.throw_if_key_of_static_method_is_prototype(operand);
+ }
+ Some(operand)
}
_ => None,
});
@@ -397,6 +404,33 @@ impl Generator<'_> {
self.bind_label(end_label);
}
+ /// A static method gets defined on the class before the key of the next element gets evaluated, which fails for the
+ /// non-configurable `prototype` of the class. `NewClass` defines them all at once, so it has to fail here instead.
+ fn throw_if_key_of_static_method_is_prototype(&mut self, key: Operand) {
+ let prototype = self.add_constant_string_view(crate::utf16::Utf16View::Ascii(b"prototype"));
+ let first_temporary = self.next_temporary();
+ let is_prototype = self.temporary();
+ let arith_feedback = self.next_arith_feedback();
+ self.emit(op::StrictlyEquals {
+ arith_feedback,
+ dst: is_prototype,
+ lhs: key,
+ rhs: prototype,
+ });
+ let not_prototype = self.new_label();
+ self.jump_unless(is_prototype, not_prototype);
+ let error_string = self.intern_string_text(crate::utf16::Utf16View::Ascii(
+ b"Object's [[DefineOwnProperty]] method returned false",
+ ));
+ self.put(op::NewTypeError {
+ dst: is_prototype,
+ error_string,
+ });
+ self.put(op::Throw { src: is_prototype });
+ self.bind_label(not_prototype);
+ self.release_temporaries_from(first_temporary);
+ }
+
/// The key of an element of a class, which `NewClass` turns into a property key.
fn sweep_class_element_key(&mut self, key: ClassElementKey, first_row: usize) -> Result<Operand, OutOfStack> {
let tree = self.tree;
diff --git a/Tests/LibJS/Runtime/classes/class-definition-evaluation-order.js b/Tests/LibJS/Runtime/classes/class-definition-evaluation-order.js
index f28f9762b7d..a5a7dcba868 100644
--- a/Tests/LibJS/Runtime/classes/class-definition-evaluation-order.js
+++ b/Tests/LibJS/Runtime/classes/class-definition-evaluation-order.js
@@ -93,4 +93,44 @@ describe("class definition evaluation order", () => {
expect(object.named.name).toBe("named");
expect(object[symbol].seenName).toBe("[description]");
});
+
+ test("a static method named prototype throws before the next key is evaluated", () => {
+ const log = [];
+ expect(() => {
+ class C {
+ static ["prototype"]() {}
+ [(log.push("method key"), "x")]() {}
+ }
+ }).toThrow(TypeError);
+ expect(() => {
+ class C {
+ static set ["prototype"](value) {}
+ static [(log.push("static key"), "y")] = 1;
+ }
+ }).toThrow(TypeError);
+ expect(() => {
+ class C {
+ static get ["prototype"]() {}
+ [(() => {
+ throw new RangeError();
+ })()]() {}
+ }
+ }).toThrow(TypeError);
+ expect(log).toEqual([]);
+
+ function* generator() {
+ class C {
+ static ["prototype"]() {}
+ [yield "reached"]() {}
+ }
+ }
+ expect(() => generator().next()).toThrow(TypeError);
+
+ class D {
+ ["prototype"]() {
+ return "instance";
+ }
+ }
+ expect(D.prototype.prototype()).toBe("instance");
+ });
});