Skip to content

Truly independent web browser

LibJS: Allow a non-object this value when setting a super property

Shannon Boothd62fe662b96f
diff --git a/Libraries/LibJS/Rust/src/bytecode/property_access.rs b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
index 71c91338d04..28136eac905 100644
--- a/Libraries/LibJS/Rust/src/bytecode/property_access.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
@@ -545,8 +545,15 @@ pub fn put_by_property_key(
}
}
PutKind::Normal => {
- let this_value_object = this_value.to_object(vm).must();
- let from_shape = this_value_object.shape();
+ // NB: The this value of a super property reference need not be an object, e.g. for `super.x = v` in a method
+ // called with a null this. [[Set]] handles a non-object receiver by failing to create or change a data
+ // property (https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor, step 2.b), but the caches below
+ // assume an object one, so don't use them for such a receiver.
+ let (caches, from_shape) = if this_value.is_object() {
+ (caches, this_value.as_object().shape())
+ } else {
+ (None, object.shape())
+ };
let from_shape_dictionary_generation = from_shape.dictionary_generation();
if let Some(caches) = caches {
for cache in caches.entries_for_shape(object.shape(), cache_key).as_slice() {
diff --git a/Tests/LibJS/Runtime/regress/super-property-set-with-non-object-this.js b/Tests/LibJS/Runtime/regress/super-property-set-with-non-object-this.js
new file mode 100644
index 00000000000..c69e9ab58e5
--- /dev/null
+++ b/Tests/LibJS/Runtime/regress/super-property-set-with-non-object-this.js
@@ -0,0 +1,70 @@
+// https://tc39.es/ecma262/#sec-putvalue
+// https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor
+describe("setting a super property with a non-object this value", () => {
+ test("throws a TypeError for a data property", () => {
+ class A {
+ setNamed() {
+ super.x = 1;
+ }
+
+ setComputed(key) {
+ super[key] = 1;
+ }
+ }
+
+ for (const thisValue of [null, undefined, 1, "foo", Symbol(), 1n]) {
+ expect(() => A.prototype.setNamed.call(thisValue)).toThrowWithMessage(
+ TypeError,
+ "Cannot set property 'x' of"
+ );
+ expect(() => A.prototype.setComputed.call(thisValue, "y")).toThrowWithMessage(
+ TypeError,
+ "Cannot set property 'y' of"
+ );
+ }
+ });
+
+ test("throws a TypeError after the caches have seen an object this value", () => {
+ class A {
+ set() {
+ super.x = 1;
+ }
+ }
+
+ for (let i = 0; i < 10; ++i) {
+ const object = new A();
+ A.prototype.set.call(object);
+ expect(object.x).toBe(1);
+ }
+
+ expect(() => A.prototype.set.call(null)).toThrowWithMessage(TypeError, "Cannot set property 'x' of");
+ expect(Object.hasOwn(A.prototype, "x")).toBeFalse();
+ });
+
+ test("calls a setter with the non-object this value", () => {
+ const receivers = [];
+
+ class A {
+ set x(value) {
+ receivers.push(this);
+ }
+ }
+
+ class B extends A {
+ set() {
+ super.x = 1;
+ }
+ }
+
+ for (let i = 0; i < 10; ++i) {
+ B.prototype.set.call(new B());
+ }
+ receivers.length = 0;
+
+ B.prototype.set.call(null);
+ B.prototype.set.call(undefined);
+ B.prototype.set.call(42);
+
+ expect(receivers).toEqual([null, undefined, 42]);
+ });
+});