Truly independent web browser
LibJS: Don't call a missing setter of a cached global accessor
Shannon Booth13638ac0e957
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rsindex 87a2db5c7fc..9ddde235bde 100644--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs@@ -987,11 +987,21 @@ pub fn set_global(vm: &Vm, pc: u32, instruction: &op::SetGlobal, values: &mut op{let value = binding_object.get_direct(entry.property_offset);if value.is_accessor() {- let setter = value.as_accessor().setter().map_or(Value::NULL, Value::from_object);- asm_try!(vm, pc, call(vm, setter, Value::from_object(binding_object), &[src]));- return SlowPathControl::continue_at(pc + op::SetGlobal::LENGTH);- }- if entry.writes_data_property {+ // 10.1.9.2 OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ), https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor+ // NB: Steps 4 to 7, for the accessor that the cache found.+ // 4. Let setter be ownDesc.[[Set]].+ // 5. If setter is undefined, return false.+ // NB: The path with no cache does that.+ if let Some(setter) = value.as_accessor().setter() {+ let setter = Value::from_object(setter);++ // 6. Perform ? Call(setter, Receiver, « V »).+ asm_try!(vm, pc, call(vm, setter, Value::from_object(binding_object), &[src]));++ // 7. Return true.+ return SlowPathControl::continue_at(pc + op::SetGlobal::LENGTH);+ }+ } else if entry.writes_data_property {binding_object.put_direct_from_cache(entry.property_offset, src);return SlowPathControl::continue_at(pc + op::SetGlobal::LENGTH);}diff --git a/Tests/LibJS/Runtime/global-accessor-setter-removed.js b/Tests/LibJS/Runtime/global-accessor-setter-removed.jsnew file mode 100644index 00000000000..b4fec4e6113--- /dev/null+++ b/Tests/LibJS/Runtime/global-accessor-setter-removed.js@@ -0,0 +1,65 @@+function defineAccessor(name, setter) {+ Object.defineProperty(globalThis, name, {+ get() {+ return 1;+ },+ set: setter,+ configurable: true,+ });+}++test("a sloppy store to a global accessor whose setter was removed does nothing", () => {+ defineAccessor("accessorForAssignment", () => {});+ function assign() {+ accessorForAssignment = 2;+ }+ assign();+ assign();+ defineAccessor("accessorForAssignment", undefined);+ expect(assign).not.toThrow();+ expect(accessorForAssignment).toBe(1);++ defineAccessor("accessorForUpdate", () => {});+ function update() {+ accessorForUpdate++;+ accessorForUpdate += 2;+ [accessorForUpdate] = [3];+ }+ update();+ update();+ defineAccessor("accessorForUpdate", undefined);+ expect(update).not.toThrow();+ delete globalThis.accessorForAssignment;+ delete globalThis.accessorForUpdate;+});++test("a strict store to a global accessor whose setter was removed throws", () => {+ defineAccessor("accessorForStrictAssignment", () => {});+ function assign() {+ "use strict";+ accessorForStrictAssignment = 2;+ }+ assign();+ assign();+ defineAccessor("accessorForStrictAssignment", undefined);+ expect(assign).toThrowWithMessage(TypeError, "Object's [[Set]] method returned false");+ delete globalThis.accessorForStrictAssignment;+});++test("a store to a global accessor whose setter was replaced calls the setter that is there", () => {+ const calls = [];+ defineAccessor("accessorWithReplacedSetter", value => calls.push(["first", value]));+ function assign(value) {+ accessorWithReplacedSetter = value;+ }+ assign(1);+ assign(2);+ defineAccessor("accessorWithReplacedSetter", value => calls.push(["second", value]));+ assign(3);+ expect(calls).toEqual([+ ["first", 1],+ ["first", 2],+ ["second", 3],+ ]);+ delete globalThis.accessorWithReplacedSetter;+});