Truly independent web browser
LibJS: Retain resolved bindings during compound assignments and updates
Timothy Flynn5ff5f33ab525
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.Sindex abd772952d1..437f0fe44f9 100644--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S@@ -76,6 +76,7 @@ asm_dispatch_table:.quad asm_handler_DynamicSetVariableBinding.quad asm_handler_ResolveBinding.quad asm_handler_ResolveSuperBase+ .quad asm_handler_GetResolvedBinding.quad asm_handler_SetResolvedBinding.quad asm_handler_TypeofBinding.quad asm_handler_DynamicTypeofBinding@@ -268,7 +269,6 @@ asm_dispatch_table:.quad asm_handler_fallback.quad asm_handler_fallback.quad asm_handler_fallback- .quad asm_handler_fallback.globl CSYM(js_interpreter_debug_dispatch_table)CSYM(js_interpreter_debug_dispatch_table):@@ -785,6 +785,10 @@ CSYM(js_interpreter_handler_ranges):.quad asm_handler_ResolveSuperBase_hot_end.quad asm_handler_ResolveSuperBase_cold.quad asm_handler_ResolveSuperBase_cold_end+ .quad asm_handler_GetResolvedBinding+ .quad asm_handler_GetResolvedBinding_hot_end+ .quad asm_handler_GetResolvedBinding_cold+ .quad asm_handler_GetResolvedBinding_cold_end.quad asm_handler_SetResolvedBinding.quad asm_handler_SetResolvedBinding_hot_end.quad asm_handler_SetResolvedBinding_cold@@ -1553,10 +1557,6 @@ CSYM(js_interpreter_handler_ranges):.quad asm_handler_fallback_hot_end.quad asm_handler_fallback_cold.quad asm_handler_fallback_cold_end- .quad asm_handler_fallback- .quad asm_handler_fallback_hot_end- .quad asm_handler_fallback_cold- .quad asm_handler_fallback_cold_end.text@@ -10838,6 +10838,49 @@ asm_handler_ResolveSuperBase_hot_end:asm_handler_ResolveSuperBase_cold:asm_handler_ResolveSuperBase_cold_end:+.p2align 4+asm_handler_GetResolvedBinding:+ mov x0, x20+ sub w1, w21, w26+ str w1, [x28, #64]+ ldr w9, [x21, #8]+ ldr x10, [x27, x9, lsl #3]+.Lasm_GetResolvedBinding.scalar_value_ready_0:+ mov x3, x10+ mov x0, x20+ sub w1, w21, w26+ mov x2, x21+ bl CSYM(asm_slow_path_get_resolved_binding)+ tbnz x0, #63, .Lasm_GetResolvedBinding.scalar_result_done_1+ tbz x0, #32, .Lasm_GetResolvedBinding.scalar_result_done_1+ ldr w9, [x21, #4]+ str x1, [x27, x9, lsl #3]+ add x21, x26, w0, uxtw+ ldrb w9, [x21]+ ldr x10, [x19, x9, lsl #3]+ br x10+.Lasm_GetResolvedBinding.scalar_result_done_1:+ tbz x0, #63, 1f+ b .Lexit+1:+ tbz x0, #32, .Lasm_GetResolvedBinding.slow_path_transfer_2+ add x21, x26, w0, uxtw+ ldrb w9, [x21]+ ldr x10, [x19, x9, lsl #3]+ br x10+.Lasm_GetResolvedBinding.slow_path_transfer_2:+ ldr x28, [x20]+ ldr x9, [x28, #88]+ ldr x26, [x9, #40]+ add x27, x28, #128+ add x21, x26, w0, uxtw+ ldrb w9, [x21]+ ldr x10, [x19, x9, lsl #3]+ br x10+asm_handler_GetResolvedBinding_hot_end:+asm_handler_GetResolvedBinding_cold:+asm_handler_GetResolvedBinding_cold_end:+.p2align 4asm_handler_SetResolvedBinding:mov x0, x20diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.Sindex 6869cc41b16..893bdf72e4c 100644--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S@@ -77,6 +77,7 @@ asm_dispatch_table:.quad asm_handler_DynamicSetVariableBinding.quad asm_handler_ResolveBinding.quad asm_handler_ResolveSuperBase+ .quad asm_handler_GetResolvedBinding.quad asm_handler_SetResolvedBinding.quad asm_handler_TypeofBinding.quad asm_handler_DynamicTypeofBinding@@ -269,7 +270,6 @@ asm_dispatch_table:.quad asm_handler_fallback.quad asm_handler_fallback.quad asm_handler_fallback- .quad asm_handler_fallback.globl CSYM(js_interpreter_debug_dispatch_table)CSYM(js_interpreter_debug_dispatch_table):@@ -786,6 +786,10 @@ CSYM(js_interpreter_handler_ranges):.quad asm_handler_ResolveSuperBase_hot_end.quad asm_handler_ResolveSuperBase_cold.quad asm_handler_ResolveSuperBase_cold_end+ .quad asm_handler_GetResolvedBinding+ .quad asm_handler_GetResolvedBinding_hot_end+ .quad asm_handler_GetResolvedBinding_cold+ .quad asm_handler_GetResolvedBinding_cold_end.quad asm_handler_SetResolvedBinding.quad asm_handler_SetResolvedBinding_hot_end.quad asm_handler_SetResolvedBinding_cold@@ -1554,10 +1558,6 @@ CSYM(js_interpreter_handler_ranges):.quad asm_handler_fallback_hot_end.quad asm_handler_fallback_cold.quad asm_handler_fallback_cold_end- .quad asm_handler_fallback- .quad asm_handler_fallback_hot_end- .quad asm_handler_fallback_cold- .quad asm_handler_fallback_cold_end.text@@ -10700,6 +10700,49 @@ asm_handler_ResolveSuperBase_hot_end:asm_handler_ResolveSuperBase_cold:asm_handler_ResolveSuperBase_cold_end:+.p2align 4+asm_handler_GetResolvedBinding:+ mov DWORD PTR [rbx + -64], r13d+ lea r11, [r14 + r13]+ mov r10d, DWORD PTR [r11 + 8]+ mov rax, QWORD PTR [rbx + r10 * 8]+.Lasm_GetResolvedBinding.scalar_value_ready_0:+ mov rcx, rax+ mov rdi, QWORD PTR [rbp - 48]+ mov esi, r13d+ lea rdx, [r14 + r13]+ call CSYM(asm_slow_path_get_resolved_binding)+ test rax, rax+ js .Lasm_GetResolvedBinding.scalar_result_done_1+ bt rax, 32+ jnc .Lasm_GetResolvedBinding.scalar_result_done_1+ lea rcx, [r14 + r13]+ mov r10d, DWORD PTR [rcx + 4]+ mov QWORD PTR [rbx + r10 * 8], rdx+ mov r13d, eax+ movzx eax, BYTE PTR [r14 + r13]+ jmp [r12 + rax * 8]+.Lasm_GetResolvedBinding.scalar_result_done_1:+ test rax, rax+ js .Lexit+ bt rax, 32+ jnc .Lasm_GetResolvedBinding.slow_path_transfer_2+ mov r13d, eax+ movzx eax, BYTE PTR [r14 + r13]+ jmp [r12 + rax * 8]+.Lasm_GetResolvedBinding.slow_path_transfer_2:+ mov rcx, QWORD PTR [rbp - 48]+ mov rbx, QWORD PTR [rcx]+ add rbx, 128+ mov rcx, QWORD PTR [rbx + -40]+ mov r14, QWORD PTR [rcx + 40]+ mov r13d, eax+ movzx eax, BYTE PTR [r14 + r13]+ jmp [r12 + rax * 8]+asm_handler_GetResolvedBinding_hot_end:+asm_handler_GetResolvedBinding_cold:+asm_handler_GetResolvedBinding_cold_end:+.p2align 4asm_handler_SetResolvedBinding:mov DWORD PTR [rbx + -64], r13ddiff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flapindex f24cb5ebce6..9e00aa31cab 100644--- a/Libraries/LibJS/Interpreter/interpreter.flap+++ b/Libraries/LibJS/Interpreter/interpreter.flap@@ -2284,6 +2284,12 @@ handler ResolveSuperBase(dst: out Operand) @cold = call_slow_path(resolve_super_base);+handler GetResolvedBinding(+ dst: out Operand,+ environment: Operand,+ identifier: IdentifierTableIndex+) @cold = call_slow_path(get_resolved_binding);+handler SetResolvedBinding(environment: Operand,identifier: IdentifierTableIndex,diff --git a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs b/Libraries/LibJS/JIT/Rust/src/builder/handling.rsindex 04562c907b9..392f47c9a7c 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs@@ -299,6 +299,7 @@ pub fn handling(opcode: OpCode) -> Handling {| O::ResolveBinding| O::ResolveGlobalBinding| O::ResolveSuperBase+ | O::GetResolvedBinding| O::SetResolvedBinding| O::TypeofBinding| O::DynamicTypeofBindingdiff --git a/Libraries/LibJS/Rust/src/bytecode/codegen.rs b/Libraries/LibJS/Rust/src/bytecode/codegen.rsindex 8200c88477d..977ea2da174 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen.rs@@ -3402,6 +3402,28 @@ fn emit_resolve_binding_for_identifier_assignment(})}+fn generate_identifier_with_resolved_binding(+ generator: &mut Generator,+ id: crate::ast::IdentifierId,+ resolved_binding: Option<&ResolvedBinding>,+) -> ScopedOperand {+ if let Some(ResolvedBinding::Environment {+ environment,+ identifier,+ }) = resolved_binding+ {+ let value = generator.allocate_register();+ generator.emit(Instruction::GetResolvedBinding {+ dst: value.operand(),+ environment: environment.operand(),+ identifier: *identifier,+ });+ value+ } else {+ generate_identifier(id, generator, None)+ }+}+fn emit_set_resolved_binding(generator: &mut Generator, resolved_binding: &ResolvedBinding, value: &ScopedOperand) {match resolved_binding {ResolvedBinding::Environment {@@ -4225,9 +4247,10 @@ fn generate_update_expression(// so we can store back without re-evaluating.match &argument.inner {ExpressionKind::Identifier(ident) => {- let value = generate_identifier(*ident, generator, None);+ let resolved_binding = emit_resolve_binding_for_identifier_assignment(generator, *ident);+ let value = generate_identifier_with_resolved_binding(generator, *ident, resolved_binding.as_ref());let result = emit_update_op(generator, op, prefixed, &value);- emit_set_variable(generator, *ident, &value);+ emit_set_variable_or_resolved_binding(generator, *ident, &value, resolved_binding.as_ref());Some(result)}ExpressionKind::Member(data) => {@@ -4376,7 +4399,8 @@ fn generate_assignment_expression(}// Load LHS value first (needed for both compound and logical assignments).- let lhs_val = generate_identifier(id, generator, None);+ let resolved_binding = emit_resolve_binding_for_identifier_assignment(generator, id);+ let lhs_val = generate_identifier_with_resolved_binding(generator, id, resolved_binding.as_ref());let lhs_val = generator.copy_if_needed_to_preserve_evaluation_order(&lhs_val);let is_logical = matches!(@@ -4420,7 +4444,7 @@ fn generate_assignment_expression(choose_dst(generator, preferred_dst)};generator.emit_mov(&dst, &rhs_val);- emit_set_variable(generator, id, &dst);+ emit_set_variable_or_resolved_binding(generator, id, &dst, resolved_binding.as_ref());generator.emit(Instruction::Jump { target: end_block });// LHS block: keep original value.generator.switch_to_basic_block(lhs_block);@@ -4439,7 +4463,7 @@ fn generate_assignment_expression(choose_dst(generator, preferred_dst)};emit_compound_assignment(generator, op, &dst, &lhs_val, &rhs_val);- emit_set_variable(generator, id, &dst);+ emit_set_variable_or_resolved_binding(generator, id, &dst, resolved_binding.as_ref());return Some(dst);}// Member expression LHS (e.g., obj.foo = x, obj[key] = x)diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rsindex 261223ce09f..81325ea8aca 100644--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs@@ -1274,6 +1274,23 @@ pub fn resolve_super_base(SlowPathControl::continue_at(pc + op::ResolveSuperBase::LENGTH)}+pub fn get_resolved_binding(+ vm: &Vm,+ pc: u32,+ instruction: &op::GetResolvedBinding,+ values: &mut op::GetResolvedBindingValues,+) -> SlowPathControl {+ let identifier = get_identifier(vm, instruction.identifier);+ let strict = strict_of(&instruction.header);+ let reference = if values.environment.is_null() {+ Reference::with_base_type(BaseType::Unresolvable, PropertyKey::from(identifier), strict)+ } else {+ Reference::with_base_environment(values.environment.as_environment(), identifier, strict, None)+ };+ values.dst = asm_try!(vm, pc, reference.get_value(vm));+ SlowPathControl::continue_at(pc + op::GetResolvedBinding::LENGTH)+}+pub fn set_resolved_binding(vm: &Vm,pc: u32,diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rsindex f1fb7d250b5..51441e9cc91 100644--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs@@ -1067,6 +1067,15 @@ impl RuntimeFunctions for Runtime {bindings::set_variable_binding(vm, pc, instruction, values)}+ fn get_resolved_binding(+ vm: &Vm,+ pc: u32,+ instruction: &op::GetResolvedBinding,+ values: &mut op::GetResolvedBindingValues,+ ) -> SlowPathControl {+ bindings::get_resolved_binding(vm, pc, instruction, values)+ }+fn set_resolved_binding(vm: &Vm,pc: u32,diff --git a/Tests/LibJS/Runtime/operators/assignment-binding-references.js b/Tests/LibJS/Runtime/operators/assignment-binding-references.jsnew file mode 100644index 00000000000..27a2b8e5e3e--- /dev/null+++ b/Tests/LibJS/Runtime/operators/assignment-binding-references.js@@ -0,0 +1,87 @@+test("compound assignments retain the environment resolved before the getter", () => {+ const cases = [+ ["+=", 6, 2, 8],+ ["-=", 6, 2, 4],+ ["*=", 6, 2, 12],+ ["/=", 6, 2, 3],+ ["%=", 6, 4, 2],+ ["**=", 6, 2, 36],+ ["<<=", 6, 2, 24],+ [">>=", 6, 1, 3],+ [">>>=", 6, 1, 3],+ ["&=", 6, 3, 2],+ ["|=", 6, 3, 7],+ ["^=", 6, 3, 5],+ ["&&=", true, 2, 2],+ ["||=", false, 2, 2],+ ["??=", null, 2, 2],+ ];++ for (const [operator, initial, rhs, expected] of cases) {+ const scope = {+ get x() {+ delete this.x;+ return initial;+ },+ };+ const run = new Function(+ "scope",+ "rhs",+ `var x = 100; var result; with (scope) { result = x ${operator} rhs; } return [result, x];`+ );+ expect(run(scope, rhs)).toEqual([expected, 100]);+ expect(scope.x).toBe(expected);+ }+});++test("compound assignments retain the environment through RHS side effects", () => {+ for (const operator of ["+=", "&&=", "||=", "??="]) {+ const initial = operator === "||=" ? false : operator === "??=" ? null : 1;+ const scope = { x: initial };+ const rhs = () => {+ delete scope.x;+ return 2;+ };+ const run = new Function("scope", "rhs", `var x = 100; with (scope) { x ${operator} rhs(); } return x;`);+ expect(run(scope, rhs)).toBe(100);+ expect(scope.x).toBe(operator === "+=" ? 3 : 2);+ }+});++test("updates retain the environment resolved before the getter", () => {+ for (const [expression, expected, result] of [+ ["++x", 3, 3],+ ["x++", 3, 2],+ ["--x", 1, 1],+ ["x--", 1, 2],+ ]) {+ const scope = {+ get x() {+ delete this.x;+ return 2;+ },+ };+ const run = new Function(+ "scope",+ `var x = 100; var result; with (scope) { result = ${expression}; } return [result, x];`+ );+ expect(run(scope)).toEqual([result, 100]);+ expect(scope.x).toBe(expected);+ }+});++test("updates resolve unscopables only once", () => {+ for (const expression of ["++x", "x++", "--x", "x--"]) {+ let lookups = 0;+ const scope = {+ x: 2,+ get [Symbol.unscopables]() {+ lookups++;+ return { x: false };+ },+ };+ const run = new Function("scope", `with (scope) { return ${expression}; }`);+ run(scope);+ expect(lookups).toBe(1);+ }+});