Truly independent web browser
LibJS: Load super properties from the cache and inline new.target
Andreas Klinge89c9bfdf583
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.Sindex 47181a22d76..146a8833609 100644--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S@@ -5427,6 +5427,56 @@ asm_handler_GetById:br x10asm_handler_GetById_hot_end:+.p2align 4+asm_handler_GetByIdWithThis:+ ldrb w0, [x21, #2]+ ldr x2, [x27, x0, lsl #3]+ lsr x9, x2, #48+ mov w10, #65529+ cmp x9, x10+ b.ne .Lasm_GetByIdWithThis.ssa_block_1+ and x2, x2, #0x7ffffffff+ add x2, x2, x24+ ldr w1, [x2, #20]+ cbz x1, .Lasm_GetByIdWithThis.assert_failure_0+ add x1, x24, w1, uxtw #3+ ldr x0, [x28, #88]+ cbz x0, .Lasm_GetByIdWithThis.assert_predicate_failure_32+ ldr x0, [x0, #152]+ cbz x0, .Lasm_GetByIdWithThis.assert_failure_1+ ldrh w3, [x21, #6]+ ldr x0, [x0, x3, lsl #3]+ cbz x0, .Lasm_GetByIdWithThis.ssa_block_1+ and x0, x0, #0xfffffffffffffffc+ cbz x0, .Lasm_GetByIdWithThis.assert_predicate_failure_43+ ldp x4, x3, [x0, #24]+ cmp x4, x1+ b.ne .Lasm_GetByIdWithThis.ssa_block_1+ ldr w4, [x0]+ cmp w4, #6+ b.eq .Lasm_GetByIdWithThis.ssa_block_1+ ldr w4, [x0, #8]+ ldr w9, [x1, #40]+ cmp w9, w4+ b.ne .Lasm_GetByIdWithThis.ssa_block_1+ cbnz x3, .Lasm_GetByIdWithThis.ssa_block_3+.Lasm_GetByIdWithThis.ssa_block_2:+ ldr x1, [x2, #24]+ cbz x1, .Lasm_GetByIdWithThis.assert_failure_2+ ldr w0, [x0, #4]+ ldr x0, [x1, x0, lsl #3]+ lsr x9, x0, #48+ mov w10, #65532+ cmp x9, x10+ b.eq .Lasm_GetByIdWithThis.ssa_block_1+ ldrb w9, [x21, #1]+ str x0, [x27, x9, lsl #3]+ ldrb w9, [x21, #8]+ add x21, x21, #8+ ldr x10, [x19, x9, lsl #3]+ br x10+asm_handler_GetByIdWithThis_hot_end:+.p2align 4asm_handler_PutById:ldrb w0, [x21, #1]@@ -14680,41 +14730,55 @@ asm_handler_GetById_cold:.Lasm_GetById.ssa_block_7:b .Lexitasm_handler_GetById_cold_end:-.p2align 4-asm_handler_GetByIdWithThis:+asm_handler_GetByIdWithThis_cold:+// Cold paths for GetByIdWithThis+.Lasm_GetByIdWithThis.ssa_block_3:+ ldr x1, [x0, #40]+ cbz x1, .Lasm_GetByIdWithThis.ssa_block_1+ ldrb w9, [x1, #11]+ cbz w9, .Lasm_GetByIdWithThis.ssa_block_1+ mov x2, x3+ b .Lasm_GetByIdWithThis.ssa_block_2+.Lasm_GetByIdWithThis.ssa_block_1:mov x0, x20sub w1, w21, w26str w1, [x28, #64]ldrb w9, [x21, #2]ldr x10, [x27, x9, lsl #3]-.Lasm_GetByIdWithThis.scalar_value_ready_0:+.Lasm_GetByIdWithThis.scalar_value_ready_3:mov x3, x10ldrb w9, [x21, #5]ldr x10, [x27, x9, lsl #3]-.Lasm_GetByIdWithThis.scalar_value_ready_1:+.Lasm_GetByIdWithThis.scalar_value_ready_4:mov x4, x10mov x0, x20sub w1, w21, w26mov x2, x21bl CSYM(asm_slow_path_get_by_id_with_this)- tbnz x0, #63, .Lasm_GetByIdWithThis.scalar_result_done_2- tbz x0, #32, .Lasm_GetByIdWithThis.scalar_result_done_2+ tbz x0, #63, 1f+ b .Lasm_GetByIdWithThis.scalar_result_done_5+1:+ tbnz x0, #32, 1f+ b .Lasm_GetByIdWithThis.scalar_result_done_5+1:ldrb w9, [x21, #1]str x1, [x27, x9, lsl #3]add x21, x26, w0, uxtwldrb w9, [x21]ldr x10, [x19, x9, lsl #3]br x10-.Lasm_GetByIdWithThis.scalar_result_done_2:+.Lasm_GetByIdWithThis.scalar_result_done_5:tbz x0, #63, 1fb .Lexit1:- tbz x0, #32, .Lasm_GetByIdWithThis.slow_path_transfer_3+ tbnz x0, #32, 1f+ b .Lasm_GetByIdWithThis.slow_path_transfer_6+1:add x21, x26, w0, uxtwldrb w9, [x21]ldr x10, [x19, x9, lsl #3]br x10-.Lasm_GetByIdWithThis.slow_path_transfer_3:+.Lasm_GetByIdWithThis.slow_path_transfer_6:ldr x28, [x20]ldr x9, [x28, #88]ldr x26, [x9, #40]@@ -14723,10 +14787,7 @@ asm_handler_GetByIdWithThis:ldrb w9, [x21]ldr x10, [x19, x9, lsl #3]br x10-asm_handler_GetByIdWithThis_hot_end:-asm_handler_GetByIdWithThis_cold:asm_handler_GetByIdWithThis_cold_end:-asm_handler_PutById_cold:// Cold paths for PutById.Lasm_PutById.ssa_block_2:@@ -21341,6 +21402,16 @@ asm_assertion_failure_traps:brk #0.Lasm_GetById.assert_failure_21:brk #0+.Lasm_GetByIdWithThis.assert_failure_0:+ brk #0+.Lasm_GetByIdWithThis.assert_predicate_failure_32:+ brk #0+.Lasm_GetByIdWithThis.assert_failure_1:+ brk #0+.Lasm_GetByIdWithThis.assert_predicate_failure_43:+ brk #0+.Lasm_GetByIdWithThis.assert_failure_2:+ brk #0.Lasm_PutById.assert_failure_0:brk #0.Lasm_PutById.assert_predicate_failure_14:diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.Sindex 62b0942cade..c6b96dcf946 100644--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S@@ -5423,6 +5423,63 @@ asm_handler_GetById:jmp [r12 + rax * 8]asm_handler_GetById_hot_end:+.p2align 6+asm_handler_GetByIdWithThis:+ movzx eax, BYTE PTR [r14 + r13 + 2]+ mov rdx, QWORD PTR [rbx + rax * 8]+ mov r11, rdx+ shr r11, 48+ cmp r11w, 65529+ jne .Lasm_GetByIdWithThis.ssa_block_1+ movabs r11, 34359738367+ and rdx, r11+ add rdx, r15+ mov ecx, DWORD PTR [rdx + 20]+ test rcx, rcx+ jz .Lasm_GetByIdWithThis.assert_failure_0+ lea rcx, [r15 + rcx * 8]+ mov rax, QWORD PTR [rbx - 48]+ test rax, rax+ jz .Lasm_GetByIdWithThis.assert_predicate_failure_32+ mov rax, QWORD PTR [rax + 152]+ test rax, rax+ jz .Lasm_GetByIdWithThis.assert_failure_1+ movzx esi, WORD PTR [r14 + r13 + 6]+ mov rax, QWORD PTR [rax + rsi * 8]+ test rax, rax+ jz .Lasm_GetByIdWithThis.ssa_block_1+ and rax, -4+ test rax, rax+ jz .Lasm_GetByIdWithThis.assert_predicate_failure_43+ mov rdi, QWORD PTR [rax + 24]+ mov rsi, QWORD PTR [rax + 32]+ cmp rdi, rcx+ jne .Lasm_GetByIdWithThis.ssa_block_1+ mov edi, DWORD PTR [rax]+ cmp edi, 6+ je .Lasm_GetByIdWithThis.ssa_block_1+ mov edi, DWORD PTR [rax + 8]+ cmp DWORD PTR [rcx + 40], edi+ jne .Lasm_GetByIdWithThis.ssa_block_1+ test rsi, rsi+ jnz .Lasm_GetByIdWithThis.ssa_block_3+.Lasm_GetByIdWithThis.ssa_block_2:+ mov rcx, QWORD PTR [rdx + 24]+ test rcx, rcx+ jz .Lasm_GetByIdWithThis.assert_failure_2+ mov eax, DWORD PTR [rax + 4]+ mov rax, QWORD PTR [rcx + rax * 8]+ mov r11, rax+ shr r11, 48+ cmp r11w, 65532+ je .Lasm_GetByIdWithThis.ssa_block_1+ movzx r11d, BYTE PTR [r14 + r13 + 1]+ mov QWORD PTR [rbx + r11 * 8], rax+ add r13d, 8+ movzx eax, BYTE PTR [r14 + r13]+ jmp [r12 + rax * 8]+asm_handler_GetByIdWithThis_hot_end:+.p2align 6asm_handler_PutById:movzx eax, BYTE PTR [r14 + r13 + 1]@@ -14298,41 +14355,50 @@ asm_handler_GetById_cold:.Lasm_GetById.ssa_block_7:jmp .Lexitasm_handler_GetById_cold_end:-.p2align 4-asm_handler_GetByIdWithThis:+asm_handler_GetByIdWithThis_cold:+# Cold paths for GetByIdWithThis+.Lasm_GetByIdWithThis.ssa_block_3:+ mov rcx, QWORD PTR [rax + 40]+ test rcx, rcx+ jz .Lasm_GetByIdWithThis.ssa_block_1+ cmp BYTE PTR [rcx + 11], 0+ jz .Lasm_GetByIdWithThis.ssa_block_1+ mov rdx, rsi+ jmp .Lasm_GetByIdWithThis.ssa_block_2+.Lasm_GetByIdWithThis.ssa_block_1:mov DWORD PTR [rbx + -72], r13dlea r11, [r14 + r13]movzx r10d, BYTE PTR [r11 + 2]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_GetByIdWithThis.scalar_value_ready_0:+.Lasm_GetByIdWithThis.scalar_value_ready_3:mov rcx, raxmovzx r10d, BYTE PTR [r11 + 5]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_GetByIdWithThis.scalar_value_ready_1:+.Lasm_GetByIdWithThis.scalar_value_ready_4:mov r8, raxmov rdi, QWORD PTR [rbp - 48]mov esi, r13dlea rdx, [r14 + r13]call CSYM(asm_slow_path_get_by_id_with_this)test rax, rax- js .Lasm_GetByIdWithThis.scalar_result_done_2+ js .Lasm_GetByIdWithThis.scalar_result_done_5bt rax, 32- jnc .Lasm_GetByIdWithThis.scalar_result_done_2+ jnc .Lasm_GetByIdWithThis.scalar_result_done_5lea rcx, [r14 + r13]movzx r10d, BYTE PTR [rcx + 1]mov QWORD PTR [rbx + r10 * 8], rdxmov r13d, eaxmovzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]-.Lasm_GetByIdWithThis.scalar_result_done_2:+.Lasm_GetByIdWithThis.scalar_result_done_5:test rax, raxjs .Lexitbt rax, 32- jnc .Lasm_GetByIdWithThis.slow_path_transfer_3+ jnc .Lasm_GetByIdWithThis.slow_path_transfer_6mov r13d, eaxmovzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]-.Lasm_GetByIdWithThis.slow_path_transfer_3:+.Lasm_GetByIdWithThis.slow_path_transfer_6:mov rcx, QWORD PTR [rbp - 48]mov rbx, QWORD PTR [rcx]add rbx, 136@@ -14341,10 +14407,7 @@ asm_handler_GetByIdWithThis:mov r13d, eaxmovzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]-asm_handler_GetByIdWithThis_hot_end:-asm_handler_GetByIdWithThis_cold:asm_handler_GetByIdWithThis_cold_end:-asm_handler_PutById_cold:# Cold paths for PutById.Lasm_PutById.ssa_block_2:@@ -20730,6 +20793,16 @@ asm_assertion_failure_traps:ud2.Lasm_GetById.assert_failure_21:ud2+.Lasm_GetByIdWithThis.assert_failure_0:+ ud2+.Lasm_GetByIdWithThis.assert_predicate_failure_32:+ ud2+.Lasm_GetByIdWithThis.assert_failure_1:+ ud2+.Lasm_GetByIdWithThis.assert_predicate_failure_43:+ ud2+.Lasm_GetByIdWithThis.assert_failure_2:+ ud2.Lasm_PutById.assert_failure_0:ud2.Lasm_PutById.assert_predicate_failure_14:diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flapindex b521a7f11f5..b19e89020b2 100644--- a/Libraries/LibJS/Interpreter/interpreter.flap+++ b/Libraries/LibJS/Interpreter/interpreter.flap@@ -3435,11 +3435,40 @@ handler GetById(handler GetByIdWithThis(dst: out Operand,- base: Operand,+ base: in Operand,property: PropertyKeyTableIndex,this_value: Operand,cache: ProfiledPropertyLookupCacheIndex-) @cold = call_slow_path(get_by_id_with_this);+) {+ let slow = || @cold {+ call_slow_path(get_by_id_with_this);+ };++ # Data properties found through the cache of the base, whose value does not depend on the receiver. Getters run in+ # the slow path with the this value.+ let load_cached_value = |holder: Object, property_cache: PropertyLookupCache| {+ let value = holder.named_properties[property_cache.property_offset];+ guard value is not Value<Accessor> else slow;+ record_cache_value_feedback(cache, value);+ store(dst, value);+ dispatch_next;+ };++ guard let Value<Object>(object) = load(base) else slow;+ let shape = object.shape;+ guard let property_cache = load_property_lookup_cache(cache) else slow;+ guard property_cache.shape == shape else slow;+ guard property_cache.entry_type != PROPERTY_LOOKUP_CACHE_ENTRY_TYPE_GET_MISSING_PROPERTY else slow;+ guard property_cache.shape_dictionary_generation == shape.dictionary_generation else slow;+ let cached_prototype = property_cache.prototype;+ guard cached_prototype == 0 else @cold {+ let prototype_chain_validity = property_cache.prototype_chain_validity;+ guard prototype_chain_validity != 0 else slow;+ guard prototype_chain_validity.valid != 0 else slow;+ goto load_cached_value(cached_prototype, property_cache);+ };+ goto load_cached_value(object, property_cache);+}handler PutById(base: in Operand,diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rsindex a1e246a5095..cd218871251 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs@@ -150,13 +150,18 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool) -> bool {// NB: These read the function, the this value or the new target of the running frame, which is the// caller's while an inlined callee runs (see `function_with_frame_as_this_environment()` in the// runtime).- | OpCode::GetNewTarget| OpCode::GetSuperConstructor| OpCode::SuperCallWithArgumentArray| OpCode::SuperCall| OpCode::SuperCallWithForwardedArguments| OpCode::BindThisFromSuperCall);+ // NB: The new target of a callee with its own new.target is known (see `try_build_new_target()`).+ let unknown_new_target = opcode == OpCode::GetNewTarget+ && !callee+ .executable+ .function+ .is_some_and(|function| function.has_own_new_target);// NB: The base of `super` property access is built from the callee's home object (see// `try_build_resolve_super_base()`).let unknown_super_base = opcode == OpCode::ResolveSuperBase@@ -164,7 +169,10 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool) -> bool {.executable.function.is_none_or(|function| function.super_base.is_none());- !reads_frame_fields && !unknown_super_base && !matches!(handling(opcode), Handling::Unsupported(_))+ !reads_frame_fields+ && !unknown_super_base+ && !unknown_new_target+ && !matches!(handling(opcode), Handling::Unsupported(_))})}@@ -1398,6 +1406,32 @@ impl GraphBuilder<'_> {Ok(true)}+ /// `GetNewTarget` in an inlined callee with its own new.target: undefined for a call, and for a construct the+ /// constructor, which is the new target of the constructs JIT code inlines. The interpreter's slow path reads the+ /// running frame, which is the caller's here.+ pub(super) fn try_build_new_target(&mut self, dst: Operand) -> Result<bool, CompileFailure> {+ let Some(call) = self.function.inline.as_ref() else {+ return Ok(false);+ };+ let value = if call.construct {+ let function = self+ .function+ .executable+ .function+ .ok_or(CompileFailure::InvalidBytecode {+ pc: self.function.pc,+ reason: "inlined construct without a known function",+ })?;+ self.embed(function.function);+ self.boxed_object(function.function)+ } else {+ value::UNDEFINED+ };+ let value = self.constant(value);+ self.write(dst, value)?;+ Ok(true)+ }+/// `this` of an inlined construct of `function`: the object its/// construct creates, an empty plain object of the prototype in its/// "prototype", which must still be the one it was at compile time.diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rsindex 8cf6f71b7b1..7e04b5ce84b 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs@@ -1642,6 +1642,11 @@ impl<'a> GraphBuilder<'a> {return Ok(None);}}+ Instruction::GetNewTarget { dst } => {+ if !self.try_build_new_target(dst)? {+ return Ok(None);+ }+ }Instruction::ResolveSuperBase { dst } => {if !self.try_build_resolve_super_base(dst)? {return Ok(None);@@ -1766,6 +1771,13 @@ impl<'a> GraphBuilder<'a> {return Ok(None);}}+ // NB: Only data properties, whose value does not depend on the receiver, are loaded here. Getters run in+ // the slow path with the this value.+ Instruction::GetByIdWithThis { dst, base, cache, .. } => {+ if !self.try_build_get_by_id(dst, base, cache)? {+ return Ok(None);+ }+ }Instruction::PutById { base, src, cache, .. } => {if let Some(flow) = self.try_inline_setter(base, src, cache)? {return Ok(Some(flow));diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rsindex 56fa148bfc3..9a73ae9eecc 100644--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs@@ -1628,6 +1628,7 @@ pub(crate) mod inlining {pub(crate) fn function(strict: bool, uses_this: bool) -> InlinedFunctionSnapshot {InlinedFunctionSnapshot {super_base: None,+ has_own_new_target: true,function: CellId(CALLEE_FUNCTION),formal_parameter_count: 1,strict,diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rsindex 21955215c0c..e8852d05d67 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs@@ -3515,6 +3515,7 @@ mod direct_calls {DirectCallTarget {function: InlinedFunctionSnapshot {super_base: None,+ has_own_new_target: true,function: CellId(self.function.as_ptr() as u64),formal_parameter_count: 3,strict,@@ -4532,6 +4533,7 @@ fn aarch64_direct_calls_disassemble() {let target = DirectCallTarget {function: InlinedFunctionSnapshot {super_base: None,+ has_own_new_target: true,function: CellId(0x5000),formal_parameter_count: 2,strict: false,diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rsindex d670b95ac00..08c9ce917c7 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -295,6 +295,9 @@ pub struct InlinedFunctionSnapshot {/// What `super` property access in the function resolves its base to,/// if JIT code can tell (see `SuperBaseSnapshot`).pub super_base: Option<SuperBaseSnapshot>,+ /// Whether the function has a new.target of its own, which an arrow+ /// function does not have.+ pub has_own_new_target: bool,}/// The [[HomeObject]] of a method, an ordinary object with a shape that isdiff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rsindex 30008d3beaa..3954d4e86cc 100644--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs@@ -661,6 +661,8 @@ impl SnapshotBuilder<'_> {realm: self.cell(realm),shared_data: self.cell(function.shared_data()),super_base: self.super_base(function),+ // NB: Arrow functions resolve new.target through their environments.+ has_own_new_target: function.this_mode() != ThisMode::Lexical,}}@@ -690,6 +692,7 @@ impl SnapshotBuilder<'_> {realm: self.cell(realm),shared_data: self.cell(shared_data),super_base: None,+ has_own_new_target: true,}}diff --git a/Tests/LibJS/Runtime/classes/class-super-call-paths.js b/Tests/LibJS/Runtime/classes/class-super-call-paths.jsindex e9dd0493fb4..dfec98121a1 100644--- a/Tests/LibJS/Runtime/classes/class-super-call-paths.js+++ b/Tests/LibJS/Runtime/classes/class-super-call-paths.js@@ -594,3 +594,59 @@ describe("prototypes of new objects", () => {});});});++describe("super property loads and new.target in hot code", () => {+ test("a super method that becomes a getter or changes", () => {+ class A {+ value() {+ return 1;+ }+ }+ class B extends A {+ read() {+ return super.value;+ }+ call() {+ return super.value();+ }+ }+ const b = new B();+ repeat(i => {+ if (i === ITERATIONS / 2) A.prototype.value = () => 2;+ expect(b.call()).toBe(i < ITERATIONS / 2 ? 1 : 2);+ });+ Object.defineProperty(A.prototype, "value", {+ get() {+ return this;+ },+ configurable: true,+ });+ expect(b.read()).toBe(b);+ Object.setPrototypeOf(B.prototype, { value: "other" });+ expect(b.read()).toBe("other");+ });++ test("new.target of called and constructed functions", () => {+ function target() {+ return new.target;+ }+ function Constructed() {+ this.target = new.target;+ }+ class Base {+ constructor() {+ this.target = new.target;+ }+ }+ function outer() {+ return (() => new.target)();+ }+ repeat(() => {+ expect(target()).toBeUndefined();+ expect(new Constructed().target).toBe(Constructed);+ expect(new Base().target).toBe(Base);+ expect(outer()).toBeUndefined();+ expect(new outer()).toBe(outer);+ });+ });+});