Truly independent web browser
LibJS: Define the length and name of functions lazily
Andreas Kling05e20bcc7c4b
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.Sindex c98a6278bfc..511f5dfeaf6 100644--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S@@ -6010,13 +6010,13 @@ asm_handler_Call:ldr w4, [x21, #24]mov w3, w0cmp x3, x4- b.hs .Lasm_Call.ssa_block_27+ b.hs .Lasm_Call.ssa_block_29mov x3, x4-.Lasm_Call.ssa_block_27:- cbz x2, .Lasm_Call.assert_predicate_failure_280+.Lasm_Call.ssa_block_29:+ cbz x2, .Lasm_Call.assert_predicate_failure_352ldp w0, w4, [x2, #12]cmp x4, x0- b.lo .Lasm_Call.assert_predicate_failure_286+ b.lo .Lasm_Call.assert_predicate_failure_358movn w0, #0subs x0, x0, x4cmp x0, x3@@ -6031,7 +6031,7 @@ asm_handler_Call:add x5, x5, #128adds x5, x5, x0cmp x5, x0- b.lo .Lasm_Call.assert_predicate_failure_301+ b.lo .Lasm_Call.assert_predicate_failure_373cmp x5, x8b.hi .Lasm_Call.ssa_block_1mov x8, x20@@ -6050,26 +6050,35 @@ asm_handler_Call:add x9, x24, w5, uxtw #3cmp w5, #0csel x5, x9, xzr, ne- ldr w7, [x1, #76]- add x9, x24, w7, uxtw #3- cmp w7, #0- csel x7, x9, xzr, ne- cbz x5, .Lasm_Call.assert_predicate_failure_329+ cbz x5, .Lasm_Call.assert_predicate_failure_399stp x5, x5, [x0, #32]- str x7, [x0, #48]+ str xzr, [x0, #48]+ ldrb w9, [x1, #15]+ mov w10, #8+ tst x9, x10+ b.eq .Lasm_Call.ssa_block_16+ ldr w5, [x1, #76]+ add x9, x24, w5, uxtw #3+ cmp w5, #0+ csel x5, x9, xzr, ne+ str x5, [x0, #48]+.Lasm_Call.ssa_block_16:stp x6, x2, [x0, #80]movz x2, #0x7ffb, lsl #48stp x2, x2, [x0, #128]str x6, [x0, #144]stp x2, x2, [x0, #152]+ ldr w1, [x1, #68]+ cbz x1, .Lasm_Call.assert_failure_8+ add x1, x24, w1, uxtw #3add x2, x0, #16- add x1, x1, #88+ add x1, x1, #40ldp x1, x5, [x1, #0]stp x1, x5, [x2, #0]str wzr, [x0, #64]mov x2, x20ldr x1, [x2, #32]- cbz x1, .Lasm_Call.assert_failure_8+ cbz x1, .Lasm_Call.assert_failure_9str x1, [x0, #56]add x1, x1, #1str x1, [x2, #32]@@ -6090,35 +6099,35 @@ asm_handler_Call:stp w2, w5, [x0, #108]ldr w2, [x21, #24]cmp x3, x2- b.lo .Lasm_Call.assert_predicate_failure_398- add x5, x0, #128+ b.lo .Lasm_Call.assert_predicate_failure_265add x6, x28, #128add x7, x21, #32+ add x5, x0, #128mov x1, #0-.Lasm_Call.ssa_block_17:+.Lasm_Call.ssa_block_19:cmp x1, x2- b.hs .Lasm_Call.ssa_block_16+ b.hs .Lasm_Call.ssa_block_18ldr w8, [x7, x1, lsl #2]ldr x8, [x6, x8, lsl #3]str x8, [x5, x4, lsl #3]add x1, x1, #1add x4, x4, #1- b .Lasm_Call.ssa_block_17-.Lasm_Call.ssa_block_16:+ b .Lasm_Call.ssa_block_19+.Lasm_Call.ssa_block_18:movz x1, #0x7ffe, lsl #48adds x3, x3, x4subs x3, x3, x2-.Lasm_Call.ssa_block_20:+.Lasm_Call.ssa_block_22:cmp x4, x3- b.hs .Lasm_Call.ssa_block_19+ b.hs .Lasm_Call.ssa_block_21str x1, [x5, x4, lsl #3]add x4, x4, #1- b .Lasm_Call.ssa_block_20-.Lasm_Call.ssa_block_19:+ b .Lasm_Call.ssa_block_22+.Lasm_Call.ssa_block_21:ldr x1, [x0, #88]- cbz x1, .Lasm_Call.assert_predicate_failure_216+ cbz x1, .Lasm_Call.assert_predicate_failure_288ldr x26, [x1, #40]- cbz x26, .Lasm_Call.assert_failure_9+ cbz x26, .Lasm_Call.assert_failure_10mov x1, x20str x0, [x1]mov x28, x0@@ -6135,8 +6144,8 @@ asm_handler_Call:ldr w3, [x21, #24]mov x0, x20ldp x0, x4, [x0, #16]- cbz x0, .Lasm_Call.assert_failure_10- cbz x4, .Lasm_Call.assert_failure_11+ cbz x0, .Lasm_Call.assert_failure_11+ cbz x4, .Lasm_Call.assert_failure_12lsl x2, x3, #3add x2, x2, #128adds x2, x2, x0@@ -6157,10 +6166,10 @@ asm_handler_Call:stp w3, w3, [x0, #116]str w3, [x0, #104]ldr w2, [x1, #20]- cbz x2, .Lasm_Call.assert_failure_12+ cbz x2, .Lasm_Call.assert_failure_13add x2, x24, w2, uxtw #3ldr w2, [x2, #12]- cbz x2, .Lasm_Call.assert_failure_13+ cbz x2, .Lasm_Call.assert_failure_14add x2, x24, w2, uxtw #3stp x1, x2, [x0, #0]ldp x2, x4, [x28, #32]@@ -6179,7 +6188,7 @@ asm_handler_Call:str wzr, [x0, #64]mov x4, x20ldr x2, [x4, #32]- cbz x2, .Lasm_Call.assert_failure_14+ cbz x2, .Lasm_Call.assert_failure_15str x2, [x0, #56]add x2, x2, #1str x2, [x4, #32]@@ -6202,39 +6211,39 @@ asm_handler_Call:add x5, x21, #32add x6, x0, #128mov x2, #0-.Lasm_Call.ssa_block_23:+.Lasm_Call.ssa_block_25:cmp x2, x3- b.hs .Lasm_Call.ssa_block_22+ b.hs .Lasm_Call.ssa_block_24ldr w7, [x5, x2, lsl #2]ldr x7, [x4, x7, lsl #3]str x7, [x6, x2, lsl #3]add x2, x2, #1- b .Lasm_Call.ssa_block_23-.Lasm_Call.ssa_block_22:+ b .Lasm_Call.ssa_block_25+.Lasm_Call.ssa_block_24:mov x2, x20str x0, [x2]mov x28, x0add x27, x28, #128ldr w0, [x1, #92]ldr x1, [x2, #80]- cbz x1, .Lasm_Call.assert_failure_15+ cbz x1, .Lasm_Call.assert_failure_16and w0, w0, #0xfffflsl x0, x0, #4adds x0, x0, x1ldr w1, [x0, #8]cmp x1, #1- b.hs .Lasm_Call.assert_predicate_failure_251+ b.hs .Lasm_Call.assert_predicate_failure_323ldr x2, [x0]- cbz x2, .Lasm_Call.assert_failure_16+ cbz x2, .Lasm_Call.assert_failure_17mov x9, x2mov x0, x20blr x9and x1, x1, #0xffcmp x1, #2- b.hs .Lasm_Call.assert_predicate_failure_258+ b.hs .Lasm_Call.assert_predicate_failure_330cbnz x1, .Lasm_Call.ssa_block_4ldr x1, [x28, #96]- cbz x1, .Lasm_Call.assert_predicate_failure_264+ cbz x1, .Lasm_Call.assert_predicate_failure_336mov x2, x20str x1, [x2]str x28, [x2, #16]@@ -6258,42 +6267,42 @@ asm_handler_Call:mov w13, #32768add x12, x12, x13cmp x14, x12- b.hs .Lasm_Call.values_allocate_18+ b.hs .Lasm_Call.values_allocate_19mov w0, #1- b .Lasm_Call.values_call_done_17-.Lasm_Call.values_allocate_18:-.Lasm_Call.values_probe_19:+ b .Lasm_Call.values_call_done_18+.Lasm_Call.values_allocate_19:+.Lasm_Call.values_probe_20:sub x10, sp, #1, lsl #12cmp x10, x14- b.ls .Lasm_Call.values_allocated_20+ b.ls .Lasm_Call.values_allocated_21add sp, x10, #0str xzr, [sp]- b .Lasm_Call.values_probe_19-.Lasm_Call.values_allocated_20:+ b .Lasm_Call.values_probe_20+.Lasm_Call.values_allocated_21:add sp, x14, #0str x9, [sp]ldr w12, [x21, #16]ldr x11, [x27, x12, lsl #3]-.Lasm_Call.value_ready_21:+.Lasm_Call.value_ready_22:str x11, [sp, #24]ldr w12, [x21, #20]ldr x11, [x27, x12, lsl #3]-.Lasm_Call.value_ready_22:+.Lasm_Call.value_ready_23:str x11, [sp, #32]mov w9, #0ldr w10, [x21, #24]add x14, sp, #40add x3, x21, #32-.Lasm_Call.array_value_next_23:+.Lasm_Call.array_value_next_24:cmp x9, x10- b.hs .Lasm_Call.array_values_end_25+ b.hs .Lasm_Call.array_values_end_26ldr w12, [x3, x9, lsl #2]ldr x11, [x27, x12, lsl #3]-.Lasm_Call.array_value_ready_24:+.Lasm_Call.array_value_ready_25:str x11, [x14, x9, lsl #3]add x9, x9, #1- b .Lasm_Call.array_value_next_23-.Lasm_Call.array_values_end_25:+ b .Lasm_Call.array_value_next_24+.Lasm_Call.array_values_end_26:mov x0, x20sub w1, w21, w26mov x2, x21@@ -6301,18 +6310,18 @@ asm_handler_Call:bl CSYM(asm_try_inline_call)ldr x9, [x20]cmp x9, x28- b.ne .Lasm_Call.values_restore_stack_26- cbnz x0, .Lasm_Call.values_exceptional_outputs_27+ b.ne .Lasm_Call.values_restore_stack_27+ cbnz x0, .Lasm_Call.values_exceptional_outputs_28ldr w12, [x21, #12]ldr x11, [sp, #16]str x11, [x27, x12, lsl #3]-.Lasm_Call.skip_output_value_28:- b .Lasm_Call.values_restore_stack_26-.Lasm_Call.values_exceptional_outputs_27:-.Lasm_Call.values_restore_stack_26:+.Lasm_Call.skip_output_value_29:+ b .Lasm_Call.values_restore_stack_27+.Lasm_Call.values_exceptional_outputs_28:+.Lasm_Call.values_restore_stack_27:ldr x9, [sp]add sp, x9, #0-.Lasm_Call.values_call_done_17:+.Lasm_Call.values_call_done_18:cbnz w0, .Lasm_Call.ssa_block_1mov x0, x20ldr x28, [x0]@@ -6321,7 +6330,7 @@ asm_handler_Call:ldr x0, [x28, #88]cbz x0, .Lasm_Call.assert_predicate_failure_17ldr x26, [x0, #40]- cbz x26, .Lasm_Call.assert_failure_29+ cbz x26, .Lasm_Call.assert_failure_30mov w25, #0add x21, x26, w25, uxtwldrb w9, [x21]@@ -15393,7 +15402,7 @@ asm_handler_Call_cold:ldr x0, [x28, #88]cbz x0, .Lasm_Call.assert_predicate_failure_148ldr x26, [x0, #40]- cbz x26, .Lasm_Call.assert_failure_30+ cbz x26, .Lasm_Call.assert_failure_31ldr w25, [x28, #64]add x21, x26, w25, uxtwldrb w9, [x21]@@ -15413,81 +15422,81 @@ asm_handler_Call_cold:mov w13, #32768add x12, x12, x13cmp x14, x12- b.hs .Lasm_Call.values_allocate_32+ b.hs .Lasm_Call.values_allocate_33mov x0, x20sub w1, w21, w26bl CSYM(asm_slow_path_stack_overflow)- b .Lasm_Call.values_call_done_31-.Lasm_Call.values_allocate_32:-.Lasm_Call.values_probe_33:+ b .Lasm_Call.values_call_done_32+.Lasm_Call.values_allocate_33:+.Lasm_Call.values_probe_34:sub x10, sp, #1, lsl #12cmp x10, x14- b.ls .Lasm_Call.values_allocated_34+ b.ls .Lasm_Call.values_allocated_35add sp, x10, #0str xzr, [sp]- b .Lasm_Call.values_probe_33-.Lasm_Call.values_allocated_34:+ b .Lasm_Call.values_probe_34+.Lasm_Call.values_allocated_35:add sp, x14, #0str x9, [sp]ldr w12, [x21, #16]ldr x11, [x27, x12, lsl #3]-.Lasm_Call.value_ready_35:+.Lasm_Call.value_ready_36:str x11, [sp, #24]ldr w12, [x21, #20]ldr x11, [x27, x12, lsl #3]-.Lasm_Call.value_ready_36:+.Lasm_Call.value_ready_37:str x11, [sp, #32]mov w9, #0ldr w10, [x21, #24]add x14, sp, #40add x3, x21, #32-.Lasm_Call.array_value_next_37:+.Lasm_Call.array_value_next_38:cmp x9, x10- b.hs .Lasm_Call.array_values_end_39+ b.hs .Lasm_Call.array_values_end_40ldr w12, [x3, x9, lsl #2]ldr x11, [x27, x12, lsl #3]-.Lasm_Call.array_value_ready_38:+.Lasm_Call.array_value_ready_39:str x11, [x14, x9, lsl #3]add x9, x9, #1- b .Lasm_Call.array_value_next_37-.Lasm_Call.array_values_end_39:+ b .Lasm_Call.array_value_next_38+.Lasm_Call.array_values_end_40:mov x0, x20sub w1, w21, w26mov x2, x21add x3, sp, #16bl CSYM(asm_slow_path_call)tbz x0, #63, 1f- b .Lasm_Call.values_exceptional_outputs_41+ b .Lasm_Call.values_exceptional_outputs_421:tbnz x0, #32, 1f- b .Lasm_Call.values_exceptional_outputs_41+ b .Lasm_Call.values_exceptional_outputs_421:ldr w12, [x21, #12]ldr x11, [sp, #16]str x11, [x27, x12, lsl #3]-.Lasm_Call.skip_output_value_42:+.Lasm_Call.skip_output_value_43:ldr x9, [sp]add sp, x9, #0add x21, x26, w0, uxtwldrb w9, [x21]ldr x10, [x19, x9, lsl #3]br x10-.Lasm_Call.values_exceptional_outputs_41:-.Lasm_Call.values_restore_stack_40:+.Lasm_Call.values_exceptional_outputs_42:+.Lasm_Call.values_restore_stack_41:ldr x9, [sp]add sp, x9, #0-.Lasm_Call.values_call_done_31:+.Lasm_Call.values_call_done_32:tbz x0, #63, 1fb .Lexit1:tbnz x0, #32, 1f- b .Lasm_Call.slow_path_transfer_43+ b .Lasm_Call.slow_path_transfer_441:add x21, x26, w0, uxtwldrb w9, [x21]ldr x10, [x19, x9, lsl #3]br x10-.Lasm_Call.slow_path_transfer_43:+.Lasm_Call.slow_path_transfer_44:ldr x28, [x20]ldr x9, [x28, #88]ldr x26, [x9, #40]@@ -20879,69 +20888,71 @@ asm_assertion_failure_traps:brk #0.Lasm_Call.assert_failure_3:brk #0-.Lasm_Call.assert_predicate_failure_280:+.Lasm_Call.assert_predicate_failure_352:brk #0-.Lasm_Call.assert_predicate_failure_286:+.Lasm_Call.assert_predicate_failure_358:brk #0.Lasm_Call.assert_failure_4:brk #0.Lasm_Call.assert_failure_5:brk #0-.Lasm_Call.assert_predicate_failure_301:+.Lasm_Call.assert_predicate_failure_373:brk #0.Lasm_Call.assert_failure_6:brk #0.Lasm_Call.assert_failure_7:brk #0-.Lasm_Call.assert_predicate_failure_329:+.Lasm_Call.assert_predicate_failure_399:brk #0.Lasm_Call.assert_failure_8:brk #0-.Lasm_Call.assert_predicate_failure_398:+.Lasm_Call.assert_failure_9:brk #0-.Lasm_Call.assert_predicate_failure_216:+.Lasm_Call.assert_predicate_failure_265:brk #0-.Lasm_Call.assert_failure_9:+.Lasm_Call.assert_predicate_failure_288:brk #0.Lasm_Call.assert_failure_10:brk #0.Lasm_Call.assert_failure_11:brk #0+.Lasm_Call.assert_failure_12:+ brk #0.Lasm_Call.assert_predicate_failure_35:brk #0.Lasm_Call.assert_predicate_failure_43:brk #0-.Lasm_Call.assert_failure_12:- brk #0.Lasm_Call.assert_failure_13:brk #0+.Lasm_Call.assert_failure_14:+ brk #0.Lasm_Call.assert_predicate_failure_68:brk #0.Lasm_Call.assert_predicate_failure_70:brk #0-.Lasm_Call.assert_failure_14:- brk #0.Lasm_Call.assert_failure_15:brk #0-.Lasm_Call.assert_predicate_failure_251:- brk #0.Lasm_Call.assert_failure_16:brk #0-.Lasm_Call.assert_predicate_failure_258:+.Lasm_Call.assert_predicate_failure_323:+ brk #0+.Lasm_Call.assert_failure_17:+ brk #0+.Lasm_Call.assert_predicate_failure_330:brk #0-.Lasm_Call.assert_predicate_failure_264:+.Lasm_Call.assert_predicate_failure_336:brk #0.Lasm_Call.assert_predicate_failure_11:brk #0.Lasm_Call.assert_predicate_failure_17:brk #0-.Lasm_Call.assert_failure_29:+.Lasm_Call.assert_failure_30:brk #0.Lasm_Call.assert_predicate_failure_142:brk #0.Lasm_Call.assert_predicate_failure_148:brk #0-.Lasm_Call.assert_failure_30:+.Lasm_Call.assert_failure_31:brk #0.Lasm_ObjectPropertyIteratorNext.assert_predicate_failure_9:brk #0diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.Sindex 874106cf1fa..235743c57fe 100644--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S@@ -6101,15 +6101,15 @@ asm_handler_Call:mov edi, DWORD PTR [r14 + r13 + 24]mov esi, eaxcmp rsi, rdi- jae .Lasm_Call.ssa_block_27+ jae .Lasm_Call.ssa_block_29mov rsi, rdi-.Lasm_Call.ssa_block_27:+.Lasm_Call.ssa_block_29:test rdx, rdx- jz .Lasm_Call.assert_predicate_failure_280+ jz .Lasm_Call.assert_predicate_failure_352mov eax, DWORD PTR [rdx + 12]mov edi, DWORD PTR [rdx + 16]cmp rdi, rax- jb .Lasm_Call.assert_predicate_failure_286+ jb .Lasm_Call.assert_predicate_failure_358mov eax, 4294967295sub rax, rdicmp rax, rsi@@ -6128,7 +6128,7 @@ asm_handler_Call:lea r8, [r8 + 128]add r8, raxcmp r8, rax- jb .Lasm_Call.assert_predicate_failure_301+ jb .Lasm_Call.assert_predicate_failure_373cmp r8, r11ja .Lasm_Call.ssa_block_1mov r11, QWORD PTR [rbp - 48]@@ -6151,15 +6151,19 @@ asm_handler_Call:lea r11, [r15 + r8 * 8]test r8d, r8dcmovnz r8, r11- mov r10d, DWORD PTR [rcx + 76]- lea r11, [r15 + r10 * 8]- test r10d, r10d- cmovnz r10, r11test r8, r8- jz .Lasm_Call.assert_predicate_failure_329+ jz .Lasm_Call.assert_predicate_failure_399mov QWORD PTR [rax + 32], r8mov QWORD PTR [rax + 40], r8- mov QWORD PTR [rax + 48], r10+ mov QWORD PTR [rax + 48], 0+ test BYTE PTR [rcx + 15], 8+ jz .Lasm_Call.ssa_block_16+ mov r8d, DWORD PTR [rcx + 76]+ lea r11, [r15 + r8 * 8]+ test r8d, r8d+ cmovnz r8, r11+ mov QWORD PTR [rax + 48], r8+.Lasm_Call.ssa_block_16:mov QWORD PTR [rax + 80], r9mov QWORD PTR [rax + 88], rdxmovabs rdx, 9221964661971222528@@ -6168,8 +6172,12 @@ asm_handler_Call:mov QWORD PTR [rax + 144], r9mov QWORD PTR [rax + 152], rdxmov QWORD PTR [rax + 160], rdx+ mov ecx, DWORD PTR [rcx + 68]+ test rcx, rcx+ jz .Lasm_Call.assert_failure_8+ lea rcx, [r15 + rcx * 8]lea rdx, [rax + 16]- lea rcx, [rcx + 88]+ lea rcx, [rcx + 40]mov r8, QWORD PTR [rcx + 8]mov rcx, QWORD PTR [rcx]mov QWORD PTR [rdx], rcx@@ -6178,7 +6186,7 @@ asm_handler_Call:mov rdx, QWORD PTR [rbp - 48]mov rcx, QWORD PTR [rdx + 32]test rcx, rcx- jz .Lasm_Call.assert_failure_8+ jz .Lasm_Call.assert_failure_9mov QWORD PTR [rax + 56], rcxlea rcx, [rcx + 1]mov QWORD PTR [rdx + 32], rcx@@ -6201,37 +6209,37 @@ asm_handler_Call:mov DWORD PTR [rax + 112], r8dmov edx, DWORD PTR [r14 + r13 + 24]cmp rsi, rdx- jb .Lasm_Call.assert_predicate_failure_398- lea r8, [rax + 128]+ jb .Lasm_Call.assert_predicate_failure_265lea r9, [rbx]lea r10, [r14 + r13 + 32]+ lea r8, [rax + 128]xor rcx, rcx-.Lasm_Call.ssa_block_17:+.Lasm_Call.ssa_block_19:cmp rcx, rdx- jae .Lasm_Call.ssa_block_16+ jae .Lasm_Call.ssa_block_18mov r11d, DWORD PTR [r10 + rcx * 4]mov r11, QWORD PTR [r9 + r11 * 8]mov QWORD PTR [r8 + rdi * 8], r11lea rcx, [rcx + 1]lea rdi, [rdi + 1]- jmp .Lasm_Call.ssa_block_17-.Lasm_Call.ssa_block_16:+ jmp .Lasm_Call.ssa_block_19+.Lasm_Call.ssa_block_18:movabs rcx, 9222809086901354496add rsi, rdisub rsi, rdx-.Lasm_Call.ssa_block_20:+.Lasm_Call.ssa_block_22:cmp rdi, rsi- jae .Lasm_Call.ssa_block_19+ jae .Lasm_Call.ssa_block_21mov QWORD PTR [r8 + rdi * 8], rcxlea rdi, [rdi + 1]- jmp .Lasm_Call.ssa_block_20-.Lasm_Call.ssa_block_19:+ jmp .Lasm_Call.ssa_block_22+.Lasm_Call.ssa_block_21:mov rcx, QWORD PTR [rax + 88]test rcx, rcx- jz .Lasm_Call.assert_predicate_failure_216+ jz .Lasm_Call.assert_predicate_failure_288mov r14, QWORD PTR [rcx + 40]test r14, r14- jz .Lasm_Call.assert_failure_9+ jz .Lasm_Call.assert_failure_10mov rcx, QWORD PTR [rbp - 48]mov QWORD PTR [rcx], raxlea rbx, [rax + 128]@@ -6246,9 +6254,9 @@ asm_handler_Call:mov rdi, QWORD PTR [rax + 24]mov rax, QWORD PTR [rax + 16]test rax, rax- jz .Lasm_Call.assert_failure_10- test rdi, rdijz .Lasm_Call.assert_failure_11+ test rdi, rdi+ jz .Lasm_Call.assert_failure_12mov rdx, rsishl rdx, 3lea rdx, [rdx + 128]@@ -6272,11 +6280,11 @@ asm_handler_Call:mov DWORD PTR [rax + 104], esimov edx, DWORD PTR [rcx + 20]test rdx, rdx- jz .Lasm_Call.assert_failure_12+ jz .Lasm_Call.assert_failure_13lea rdx, [r15 + rdx * 8]mov edx, DWORD PTR [rdx + 12]test rdx, rdx- jz .Lasm_Call.assert_failure_13+ jz .Lasm_Call.assert_failure_14lea rdx, [r15 + rdx * 8]mov QWORD PTR [rax], rcxmov QWORD PTR [rax + 8], rdx@@ -6302,7 +6310,7 @@ asm_handler_Call:mov rdi, QWORD PTR [rbp - 48]mov rdx, QWORD PTR [rdi + 32]test rdx, rdx- jz .Lasm_Call.assert_failure_14+ jz .Lasm_Call.assert_failure_15mov QWORD PTR [rax + 56], rdxlea rdx, [rdx + 1]mov QWORD PTR [rdi + 32], rdx@@ -6327,43 +6335,43 @@ asm_handler_Call:lea r8, [r14 + r13 + 32]lea r9, [rax + 128]xor rdx, rdx-.Lasm_Call.ssa_block_23:+.Lasm_Call.ssa_block_25:cmp rdx, rsi- jae .Lasm_Call.ssa_block_22+ jae .Lasm_Call.ssa_block_24mov r10d, DWORD PTR [r8 + rdx * 4]mov r10, QWORD PTR [rdi + r10 * 8]mov QWORD PTR [r9 + rdx * 8], r10lea rdx, [rdx + 1]- jmp .Lasm_Call.ssa_block_23-.Lasm_Call.ssa_block_22:+ jmp .Lasm_Call.ssa_block_25+.Lasm_Call.ssa_block_24:mov rdx, QWORD PTR [rbp - 48]mov QWORD PTR [rdx], raxlea rbx, [rax + 128]mov eax, DWORD PTR [rcx + 92]mov rcx, QWORD PTR [rdx + 80]test rcx, rcx- jz .Lasm_Call.assert_failure_15+ jz .Lasm_Call.assert_failure_16and eax, 65535shl rax, 4add rax, rcxmov ecx, DWORD PTR [rax + 8]cmp rcx, 1- jae .Lasm_Call.assert_predicate_failure_251+ jae .Lasm_Call.assert_predicate_failure_323mov rdx, QWORD PTR [rax]test rdx, rdx- jz .Lasm_Call.assert_failure_16+ jz .Lasm_Call.assert_failure_17mov r11, rdxmov rdi, QWORD PTR [rbp - 48]call r11mov rcx, rdxand rcx, 255cmp rcx, 2- jae .Lasm_Call.assert_predicate_failure_258+ jae .Lasm_Call.assert_predicate_failure_330test rcx, rcxjnz .Lasm_Call.ssa_block_4mov rcx, QWORD PTR [rbx - 32]test rcx, rcx- jz .Lasm_Call.assert_predicate_failure_264+ jz .Lasm_Call.assert_predicate_failure_336mov rdx, QWORD PTR [rbp - 48]mov QWORD PTR [rdx], rcxlea r11, [rbx - 128]@@ -6388,40 +6396,40 @@ asm_handler_Call:mov rdx, QWORD PTR [rdx + 40]add rdx, 32768cmp rax, rdx- jae .Lasm_Call.values_allocate_18+ jae .Lasm_Call.values_allocate_19mov eax, 1- jmp .Lasm_Call.values_call_done_17-.Lasm_Call.values_allocate_18:-.Lasm_Call.values_probe_19:+ jmp .Lasm_Call.values_call_done_18+.Lasm_Call.values_allocate_19:+.Lasm_Call.values_probe_20:lea r10, [rsp - 4096]cmp r10, rax- jbe .Lasm_Call.values_allocated_20+ jbe .Lasm_Call.values_allocated_21mov rsp, r10mov QWORD PTR [rsp], 0- jmp .Lasm_Call.values_probe_19-.Lasm_Call.values_allocated_20:+ jmp .Lasm_Call.values_probe_20+.Lasm_Call.values_allocated_21:mov rsp, raxmov QWORD PTR [rsp], r11mov r10d, DWORD PTR [rcx + 16]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_Call.value_ready_21:+.Lasm_Call.value_ready_22:mov QWORD PTR [rsp + 24], raxmov r10d, DWORD PTR [rcx + 20]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_Call.value_ready_22:+.Lasm_Call.value_ready_23:mov QWORD PTR [rsp + 32], raxmov r11d, 0mov r10d, DWORD PTR [rcx + 24]-.Lasm_Call.array_value_next_23:+.Lasm_Call.array_value_next_24:cmp r11, r10- jae .Lasm_Call.array_values_end_25+ jae .Lasm_Call.array_values_end_26mov edx, DWORD PTR [rcx + r11 * 4 + 32]mov rax, QWORD PTR [rbx + rdx * 8]-.Lasm_Call.array_value_ready_24:+.Lasm_Call.array_value_ready_25:mov QWORD PTR [rsp + r11 * 8 + 40], raxadd r11, 1- jmp .Lasm_Call.array_value_next_23-.Lasm_Call.array_values_end_25:+ jmp .Lasm_Call.array_value_next_24+.Lasm_Call.array_values_end_26:mov rdi, QWORD PTR [rbp - 48]mov esi, r13dlea rdx, [r14 + r13]@@ -6431,19 +6439,19 @@ asm_handler_Call:mov r10, QWORD PTR [r10]add r10, 128cmp r10, rbx- jne .Lasm_Call.values_restore_stack_26+ jne .Lasm_Call.values_restore_stack_27test rax, rax- jne .Lasm_Call.values_exceptional_outputs_27+ jne .Lasm_Call.values_exceptional_outputs_28lea rcx, [r14 + r13]mov r10d, DWORD PTR [rcx + 12]mov r11, QWORD PTR [rsp + 16]mov QWORD PTR [rbx + r10 * 8], r11-.Lasm_Call.skip_output_value_28:- jmp .Lasm_Call.values_restore_stack_26-.Lasm_Call.values_exceptional_outputs_27:-.Lasm_Call.values_restore_stack_26:+.Lasm_Call.skip_output_value_29:+ jmp .Lasm_Call.values_restore_stack_27+.Lasm_Call.values_exceptional_outputs_28:+.Lasm_Call.values_restore_stack_27:mov rsp, QWORD PTR [rsp]-.Lasm_Call.values_call_done_17:+.Lasm_Call.values_call_done_18:test eax, eaxjnz .Lasm_Call.ssa_block_1mov rax, QWORD PTR [rbp - 48]@@ -6456,7 +6464,7 @@ asm_handler_Call:jz .Lasm_Call.assert_predicate_failure_17mov r14, QWORD PTR [rax + 40]test r14, r14- jz .Lasm_Call.assert_failure_29+ jz .Lasm_Call.assert_failure_30mov r13d, 0movzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]@@ -15027,7 +15035,7 @@ asm_handler_Call_cold:jz .Lasm_Call.assert_predicate_failure_148mov r14, QWORD PTR [rax + 40]test r14, r14- jz .Lasm_Call.assert_failure_30+ jz .Lasm_Call.assert_failure_31mov r13d, DWORD PTR [rbx - 64]movzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]@@ -15045,73 +15053,73 @@ asm_handler_Call_cold:mov rdx, QWORD PTR [rdx + 40]add rdx, 32768cmp rax, rdx- jae .Lasm_Call.values_allocate_32+ jae .Lasm_Call.values_allocate_33mov rdi, QWORD PTR [rbp - 48]mov esi, r13dlea rdx, [r14 + r13]call CSYM(asm_slow_path_stack_overflow)- jmp .Lasm_Call.values_call_done_31-.Lasm_Call.values_allocate_32:-.Lasm_Call.values_probe_33:+ jmp .Lasm_Call.values_call_done_32+.Lasm_Call.values_allocate_33:+.Lasm_Call.values_probe_34:lea r10, [rsp - 4096]cmp r10, rax- jbe .Lasm_Call.values_allocated_34+ jbe .Lasm_Call.values_allocated_35mov rsp, r10mov QWORD PTR [rsp], 0- jmp .Lasm_Call.values_probe_33-.Lasm_Call.values_allocated_34:+ jmp .Lasm_Call.values_probe_34+.Lasm_Call.values_allocated_35:mov rsp, raxmov QWORD PTR [rsp], r11mov r10d, DWORD PTR [rcx + 16]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_Call.value_ready_35:+.Lasm_Call.value_ready_36:mov QWORD PTR [rsp + 24], raxmov r10d, DWORD PTR [rcx + 20]mov rax, QWORD PTR [rbx + r10 * 8]-.Lasm_Call.value_ready_36:+.Lasm_Call.value_ready_37:mov QWORD PTR [rsp + 32], raxmov r11d, 0mov r10d, DWORD PTR [rcx + 24]-.Lasm_Call.array_value_next_37:+.Lasm_Call.array_value_next_38:cmp r11, r10- jae .Lasm_Call.array_values_end_39+ jae .Lasm_Call.array_values_end_40mov edx, DWORD PTR [rcx + r11 * 4 + 32]mov rax, QWORD PTR [rbx + rdx * 8]-.Lasm_Call.array_value_ready_38:+.Lasm_Call.array_value_ready_39:mov QWORD PTR [rsp + r11 * 8 + 40], raxadd r11, 1- jmp .Lasm_Call.array_value_next_37-.Lasm_Call.array_values_end_39:+ jmp .Lasm_Call.array_value_next_38+.Lasm_Call.array_values_end_40:mov rdi, QWORD PTR [rbp - 48]mov esi, r13dlea rdx, [r14 + r13]lea rcx, [rsp + 16]call CSYM(asm_slow_path_call)test rax, rax- js .Lasm_Call.values_exceptional_outputs_41+ js .Lasm_Call.values_exceptional_outputs_42bt rax, 32- jnc .Lasm_Call.values_exceptional_outputs_41+ jnc .Lasm_Call.values_exceptional_outputs_42lea rcx, [r14 + r13]mov r10d, DWORD PTR [rcx + 12]mov r11, QWORD PTR [rsp + 16]mov QWORD PTR [rbx + r10 * 8], r11-.Lasm_Call.skip_output_value_42:+.Lasm_Call.skip_output_value_43:mov rsp, QWORD PTR [rsp]mov r13d, eaxmovzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]-.Lasm_Call.values_exceptional_outputs_41:-.Lasm_Call.values_restore_stack_40:+.Lasm_Call.values_exceptional_outputs_42:+.Lasm_Call.values_restore_stack_41:mov rsp, QWORD PTR [rsp]-.Lasm_Call.values_call_done_31:+.Lasm_Call.values_call_done_32:test rax, raxjs .Lexitbt rax, 32- jnc .Lasm_Call.slow_path_transfer_43+ jnc .Lasm_Call.slow_path_transfer_44mov r13d, eaxmovzx eax, BYTE PTR [r14 + r13]jmp [r12 + rax * 8]-.Lasm_Call.slow_path_transfer_43:+.Lasm_Call.slow_path_transfer_44:mov rcx, QWORD PTR [rbp - 48]mov rbx, QWORD PTR [rcx]add rbx, 128@@ -20326,69 +20334,71 @@ asm_assertion_failure_traps:ud2.Lasm_Call.assert_failure_3:ud2-.Lasm_Call.assert_predicate_failure_280:+.Lasm_Call.assert_predicate_failure_352:ud2-.Lasm_Call.assert_predicate_failure_286:+.Lasm_Call.assert_predicate_failure_358:ud2.Lasm_Call.assert_failure_4:ud2.Lasm_Call.assert_failure_5:ud2-.Lasm_Call.assert_predicate_failure_301:+.Lasm_Call.assert_predicate_failure_373:ud2.Lasm_Call.assert_failure_6:ud2.Lasm_Call.assert_failure_7:ud2-.Lasm_Call.assert_predicate_failure_329:+.Lasm_Call.assert_predicate_failure_399:ud2.Lasm_Call.assert_failure_8:ud2-.Lasm_Call.assert_predicate_failure_398:+.Lasm_Call.assert_failure_9:ud2-.Lasm_Call.assert_predicate_failure_216:+.Lasm_Call.assert_predicate_failure_265:ud2-.Lasm_Call.assert_failure_9:+.Lasm_Call.assert_predicate_failure_288:ud2.Lasm_Call.assert_failure_10:ud2.Lasm_Call.assert_failure_11:ud2+.Lasm_Call.assert_failure_12:+ ud2.Lasm_Call.assert_predicate_failure_35:ud2.Lasm_Call.assert_predicate_failure_43:ud2-.Lasm_Call.assert_failure_12:- ud2.Lasm_Call.assert_failure_13:ud2+.Lasm_Call.assert_failure_14:+ ud2.Lasm_Call.assert_predicate_failure_68:ud2.Lasm_Call.assert_predicate_failure_70:ud2-.Lasm_Call.assert_failure_14:- ud2.Lasm_Call.assert_failure_15:ud2-.Lasm_Call.assert_predicate_failure_251:- ud2.Lasm_Call.assert_failure_16:ud2-.Lasm_Call.assert_predicate_failure_258:+.Lasm_Call.assert_predicate_failure_323:+ ud2+.Lasm_Call.assert_failure_17:ud2-.Lasm_Call.assert_predicate_failure_264:+.Lasm_Call.assert_predicate_failure_330:+ ud2+.Lasm_Call.assert_predicate_failure_336:ud2.Lasm_Call.assert_predicate_failure_11:ud2.Lasm_Call.assert_predicate_failure_17:ud2-.Lasm_Call.assert_failure_29:+.Lasm_Call.assert_failure_30:ud2.Lasm_Call.assert_predicate_failure_142:ud2.Lasm_Call.assert_predicate_failure_148:ud2-.Lasm_Call.assert_failure_30:+.Lasm_Call.assert_failure_31:ud2.Lasm_ObjectPropertyIteratorNext.assert_predicate_failure_9:ud2diff --git a/Libraries/LibJS/Flap/tests/interpreter-layout.conf b/Libraries/LibJS/Flap/tests/interpreter-layout.confindex b8f7cbb63a1..03e82576bdd 100644--- a/Libraries/LibJS/Flap/tests/interpreter-layout.conf+++ b/Libraries/LibJS/Flap/tests/interpreter-layout.conf@@ -20,6 +20,8 @@ field Accessor.getter FunctionObject ACCESSOR_GETTER nullable compressed# Object flagsconst OBJECT_FLAGS = 12field Object.flags u16 OBJECT_FLAGS nullable scalar+const OBJECT_FUNCTION_BITS = 15+field Object.function_bits u8 OBJECT_FUNCTION_BITS nullable scalarconst OBJECT_FLAG_HAS_MAGICAL_LENGTH = 4const OBJECT_FLAG_MAY_INTERFERE = 16const OBJECT_FLAG_IS_TYPED_ARRAY = 8@@ -376,10 +378,11 @@ const ECMASCRIPT_FUNCTION_OBJECT_ENVIRONMENT = 72field ECMAScriptFunctionObject.environment Environment ECMASCRIPT_FUNCTION_OBJECT_ENVIRONMENT nullable compressedconst ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT = 76field ECMAScriptFunctionObject.private_environment PrivateEnvironment ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT nullable compressed-const ECMASCRIPT_FUNCTION_OBJECT_SCRIPT_OR_MODULE = 88-field ECMAScriptFunctionObject.script_or_module ScriptOrModule ECMASCRIPT_FUNCTION_OBJECT_SCRIPT_OR_MODULE nullable scalar+const ECMASCRIPT_FUNCTION_FLAG_HAS_RARE_DATA = 8# SharedFunctionInstanceData layout+const SHARED_FUNCTION_INSTANCE_DATA_SCRIPT_OR_MODULE = 40+field SharedFunctionInstanceData.script_or_module ScriptOrModule SHARED_FUNCTION_INSTANCE_DATA_SCRIPT_OR_MODULE nullable scalarconst SHARED_FUNCTION_INSTANCE_DATA_EXECUTABLE = 12field SharedFunctionInstanceData.executable Executable SHARED_FUNCTION_INSTANCE_DATA_EXECUTABLE nullable compressedconst SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA = 16diff --git a/Libraries/LibJS/HostObjectABI.h b/Libraries/LibJS/HostObjectABI.hindex dac181dd5a7..5468d47d503 100644--- a/Libraries/LibJS/HostObjectABI.h+++ b/Libraries/LibJS/HostObjectABI.h@@ -32,9 +32,9 @@ enum {// Layout of every host object of kind JS_HOST_CLASS_OBJECT. Direct getter functions read the wrappable slot without// checking what it holds, so it only ever holds the embedder's wrapped implementation object. Any other per-object state-// belongs in the host data slot. The host object's own fields follow the engine's object, which is 48 bytes.+// belongs in the host data slot. The host object's own fields follow the engine's object, which is 32 bytes.enum {- JS_HOST_OBJECT_HOST_CLASS_OFFSET = 48,+ JS_HOST_OBJECT_HOST_CLASS_OFFSET = 32,JS_HOST_OBJECT_WRAPPABLE_OFFSET = JS_HOST_OBJECT_HOST_CLASS_OFFSET + 8,JS_HOST_OBJECT_HOST_DATA_OFFSET = JS_HOST_OBJECT_WRAPPABLE_OFFSET + 8,JS_HOST_OBJECT_SIZE = JS_HOST_OBJECT_HOST_DATA_OFFSET + 8,diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flapindex a4bf49ceb94..a0c39505ced 100644--- a/Libraries/LibJS/Interpreter/interpreter.flap+++ b/Libraries/LibJS/Interpreter/interpreter.flap@@ -3904,11 +3904,14 @@ handler Call(frame.function = function;frame.realm = realm;let lexical_environment = ecmascript_function.environment;- let private_environment = ecmascript_function.private_environment;assert(lexical_environment != 0);frame.lexical_environment = lexical_environment;frame.variable_environment = lexical_environment;- frame.private_environment = private_environment;+ frame.private_environment = null;+ let function_bits = function.function_bits;+ if function_bits has ECMASCRIPT_FUNCTION_FLAG_HAS_RARE_DATA {+ frame.private_environment = ecmascript_function.private_environment;+ }frame.this_value = receiver;frame.executable = executable_pointer;let empty_tag: Value = Value<Empty>;@@ -3917,7 +3920,8 @@ handler Call(frame.this_value_register = receiver;frame.return_value = empty_tag;frame.saved_lexical_environment = empty_tag;- frame.script_or_module = ecmascript_function.script_or_module;+ let frame_shared_data = ecmascript_function.shared_data;+ frame.script_or_module = frame_shared_data.script_or_module;frame.program_counter = 0;let frame_id_vm = current_vm();let frame_id = frame_id_vm.interpreter_stack_next_frame_id;diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs b/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rsindex ccc4f924077..4292237fa1d 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/allocation.rs@@ -619,7 +619,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {let stored = [(objects.named_properties_offset, 8),(offsets.ecmascript_function_environment, 4),- (offsets.ecmascript_function_private_environment, 4),];let template_bytes = 8 * closure.words.len() as u64;let fits = info.allocator != 0@@ -637,6 +636,9 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {let (function, temp) = (self.allocation_temp(node, 0), self.allocation_temp(node, 1));let at = |base: Gpr, offset: u32| Address::new(base, offset as i32);+ // NB: Functions with a private environment have rare data, unlike the template.+ self.masm+ .branch_test64(Condition::NonZero, private_environment, u64::MAX, entry);self.emit_check_heap_threshold(info.cell_size, function, temp, entry);self.emit_pop_free_list(info.allocator, function, temp, entry);self.emit_count_allocation(info.cell_size, info.cell_size, temp, false);@@ -651,11 +653,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {environment,temp,);- self.emit_store_compressed_cell(- &at(function, offsets.ecmascript_function_private_environment),- private_environment,- temp,- );self.box_cell_with_tag(output, function, value::OBJECT_TAG);self.masm.bind(resume);Ok(())diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call.rsindex fc4cc96c52d..ad020f26666 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs@@ -688,10 +688,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {.store64(&field(offsets.execution_context_lexical_environment), temp);self.masm.store64(&field(offsets.execution_context_variable_environment), temp);- self.emit_load_nullable_compressed_cell(- temp,- &Address::new(aux, offsets.ecmascript_function_private_environment as i32),- );+ self.emit_load_function_private_environment(temp, aux);self.masm.store64(&field(offsets.execution_context_private_environment), temp);}@@ -1622,10 +1619,12 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.store64(&field(offsets.execution_context_function), function_register);let function_field = |offset: u32| Address::new(function_register, offset as i32);+ let shared_data_field = function_field(self.runtime.dynamic_calls.ecmascript_function_shared_data);for word in [0, 8] {+ self.emit_load_compressed_cell(temp, &shared_data_field);self.masm.load64(temp,- &function_field(offsets.ecmascript_function_script_or_module + word),+ &Address::new(temp, (offsets.shared_function_script_or_module + word) as i32),);self.masm.store64(&field(offsets.execution_context_script_or_module + word), temp);@@ -1636,7 +1635,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {.store64(&field(offsets.execution_context_lexical_environment), temp);self.masm.store64(&field(offsets.execution_context_variable_environment), temp);- self.emit_load_nullable_compressed_cell(temp, &function_field(offsets.ecmascript_function_private_environment));+ self.emit_load_function_private_environment(temp, function_register);self.masm.store64(&field(offsets.execution_context_private_environment), temp);// NB: Frames get their id when the debugger first needs it.diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rsindex 635054b8928..0d69a839343 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs@@ -1392,8 +1392,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {let (output, function) = (self.output(node_id), self.input(node_id, 0));let offsets = self.runtime.offsets;if *private {- let address = Address::new(function, offsets.ecmascript_function_private_environment as i32);- self.emit_load_nullable_compressed_cell(output, &address);+ // NB: The output may be in the register of the function.+ let scratch = self.pinned.scratch;+ self.emit_load_function_private_environment(scratch, function);+ self.masm.move64(output, scratch);} else {// NB: ECMAScript functions always have an environment.let address = Address::new(function, offsets.ecmascript_function_environment as i32);@@ -2154,6 +2156,24 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.bind(null);}+ /// Loads the private environment of the ECMAScript function that+ /// `function` holds into `dst`, another register, as a cell pointer, or 0+ /// if it has none.+ fn emit_load_function_private_environment(&mut self, dst: Gpr, function: Gpr) {+ let offsets = self.runtime.offsets;+ let done = self.masm.new_label();+ self.masm+ .load8(dst, &Address::new(function, offsets.object_function_bits as i32));+ self.masm+ .and32_imm(dst, dst, offsets.ecmascript_function_flag_has_rare_data);+ self.masm.branch_test32(Condition::Zero, dst, u32::MAX, done);+ self.emit_load_nullable_compressed_cell(+ dst,+ &Address::new(function, offsets.ecmascript_function_private_environment as i32),+ );+ self.masm.bind(done);+ }+/// Loads the compressed cell pointer field at `address` into `dst` as a/// cell pointer, or branches to `null` if it is null.fn emit_load_compressed_cell_or_branch(&mut self, dst: Gpr, address: &Address, null: Label) {diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/strings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/strings.rsindex 1bc9a175e20..244bf4e6c7d 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/strings.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/strings.rs@@ -194,14 +194,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {u32::from(self.runtime.object_flag_is_function),no,);- self.masm.load8(- scratch,- &Address::new(function, layout.function_object_has_builtin as i32),- );- self.masm.branch_test32(Condition::Zero, scratch, 0xFF, no);self.masm.load8(scratch, &Address::new(function, layout.function_object_builtin as i32));- self.masm.branch32_imm(Condition::Equal, scratch, i32::from(id), yes);+ self.masm+ .branch32_imm(Condition::Equal, scratch, i32::from(id) + 1, yes);self.masm.jump(no);}diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rsindex cf8fa3c224d..73e06cf2aa9 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs@@ -71,7 +71,9 @@ fn offsets() -> RuntimeOffsets {execution_context_runs_jit_code: 157,ecmascript_function_environment: 0,ecmascript_function_private_environment: 8,- ecmascript_function_script_or_module: 16,+ shared_function_script_or_module: 24,+ object_function_bits: 40,+ ecmascript_function_flag_has_rare_data: 8,vm_execution_generation: 8 * VM_EXECUTION_GENERATION_WORD as u32,}}@@ -534,6 +536,17 @@ fn write_frame_states(vm: *mut u64, frame: *mut u64, exit_index: u32, dump: *con/// the offsets of their data from it.const PRIMITIVE_STORAGE_CAGE_BASE: u64 = 0x1000;+/// The words of a fake ECMAScript function (see `offsets()`): `words` are the+/// environment, the private environment and the script or module, of which the+/// last two also are in its fake shared data, followed by the compressed shared+/// data and flags saying it has rare data.+fn fake_function_words(words: [u64; 4]) -> Vec<u64> {+ let shared_data = fake_heap::cell(&[0, 0, 0, words[2], words[3]]);+ let mut function = words.to_vec();+ function.extend([fake_heap::compressed(shared_data.as_ptr() as u64), 8]);+ function+}+fn runtime() -> RuntimeInfo {let mut slow_paths = vec![0; NUM_OPCODES as usize];let mut set = |opcode: OpCode, address: u64| slow_paths[opcode as usize] = address;@@ -2854,13 +2867,19 @@ mod inlining {configure: impl FnOnce(&mut Snapshot),prepare: impl FnOnce(&mut Machine),) -> (JitResult, Machine, CompiledCode) {- let function_object = FUNCTION_WORDS.to_vec();+ let function_object = fake_function_words(FUNCTION_WORDS);let mut function = function(true, false);function.function = CellId(function_object.as_ptr() as u64);let mut snapshot = snapshot_with_callee(caller, callee, function);snapshot.runtime = runtime();for executable in &mut snapshot.executables {executable.feedback.arith = vec![NUMBERS_AND_STRINGS_FEEDBACK];+ // NB: The words of a function its frames start with are constants in code.+ executable.function_fields = Some(FunctionFrameFields {+ script_or_module: [FUNCTION_WORDS[2], FUNCTION_WORDS[3]],+ environment: FUNCTION_WORDS[0],+ private_environment: FUNCTION_WORDS[1],+ });}configure(&mut snapshot);let compiled = compile_for::<crate::asm::MacroAssembler>(&snapshot, &|_| true).unwrap();@@ -3295,7 +3314,7 @@ mod direct_calls {impl Callee {fn new() -> Self {let mut callee = Self {- function: FUNCTION_WORDS.to_vec(),+ function: fake_function_words(FUNCTION_WORDS),executable: fake_heap::cell(&[0; 7]),entry_table: vec![0; 2 * ENTRY_TABLE_OWNERS_WORD],interpreter_stack: vec![0xdead_beef_dead_beef; 512],@@ -3748,10 +3767,13 @@ mod direct_calls {fake_heap::compressed(callee.executable.as_ptr() as u64),formal_parameter_count | flags,0,+ FUNCTION_WORDS[2],+ FUNCTION_WORDS[3],]);let mut function = vec![fake_heap::compressed(shape.as_ptr() as u64),- u64::from(ECMASCRIPT_FUNCTION_FLAG),+ // NB: The top byte holds the function's flags.+ u64::from(ECMASCRIPT_FUNCTION_FLAG) | 8 << 56,fake_heap::compressed(shared_data.as_ptr() as u64),];function.extend(FUNCTION_WORDS);@@ -3780,7 +3802,7 @@ mod direct_calls {fake_heap::use_in(runtime);runtime.offsets.ecmascript_function_environment = 24;runtime.offsets.ecmascript_function_private_environment = 32;- runtime.offsets.ecmascript_function_script_or_module = 40;+ runtime.offsets.object_function_bits = 15;runtime.dynamic_calls = crate::snapshot::DynamicCallLayout {object_flag_is_ecmascript_function: ECMASCRIPT_FUNCTION_FLAG,ecmascript_function_shared_data: 16,diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rsindex dd1d304e1ff..4ef3303ce77 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs@@ -1231,8 +1231,7 @@ fn fake_object_layout() -> RuntimeLayout {utf16_string_data_has_utf16_storage: 1,utf16_string_data_storage: 24,single_ascii_character_strings: 0,- function_object_builtin: 24,- function_object_has_builtin: 25,+ function_object_builtin: 15,builtin_string_prototype_char_code_at: 7,builtin_string_prototype_char_at: 8,builtin_math_abs: 9,@@ -3490,12 +3489,11 @@ fn saw_builtin(snapshot: &mut Snapshot, builtin: u8) {}];}-/// Fake function memory: its flags at byte 8, and the builtin it is (if-/// any) at bytes 24 and 25, see `function_object_builtin` in the test-/// layout.+/// Fake function memory: its flags at byte 8, and the builtin it is plus one+/// (if any) at byte 15, see `function_object_builtin` in the test layout.fn fake_function(builtin: Option<u8>) -> u64 {- let builtin = builtin.map_or(0, |builtin| u64::from(builtin) | 1 << 8);- let memory: &'static mut [u64; 4] = Box::leak(Box::new([0, u64::from(FUNCTION_FLAG), 0, builtin]));+ let builtin = builtin.map_or(0, |builtin| u64::from(builtin) + 1);+ let memory: &'static mut [u64; 2] = Box::leak(Box::new([0, u64::from(FUNCTION_FLAG) | builtin << 56]));u64::from(value::OBJECT_TAG) << 48 | memory.as_ptr() as u64}diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rsindex 1953e0e28ce..ca0fcc854ec 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -1543,10 +1543,16 @@ pub struct RuntimeOffsets {pub execution_context_runs_jit_code: u32,/// `EcmascriptFunctionObject::environment` (cell pointer).pub ecmascript_function_environment: u32,- /// `EcmascriptFunctionObject::private_environment` (cell pointer).+ /// `EcmascriptFunctionObjectWithRareData::private_environment` (cell+ /// pointer), of functions with the flag for it.pub ecmascript_function_private_environment: u32,- /// `EcmascriptFunctionObject::script_or_module` (16 bytes, as in `ExecutionContext`).- pub ecmascript_function_script_or_module: u32,+ /// `SharedFunctionInstanceData::script_or_module` (16 bytes, as in `ExecutionContext`).+ pub shared_function_script_or_module: u32,+ /// `Object::function_bits` (u8).+ pub object_function_bits: u32,+ /// The bit of `Object::function_bits` of an ECMAScript function that has+ /// a private environment field.+ pub ecmascript_function_flag_has_rare_data: u32,/// `VmHead::execution_generation` (u32).pub vm_execution_generation: u32,}@@ -1745,10 +1751,9 @@ pub struct RuntimeLayout {/// The address of the VM's array of the strings of the 128 ASCII/// characters (cell pointers), or 0 for code without one.pub single_ascii_character_strings: u64,- /// `FunctionObject::builtin` (u8), the builtin a function is, if- /// `FunctionObject::has_builtin` (bool).+ /// `Object::function_bits` (u8): the builtin a function is plus one, if+ /// it is one.pub function_object_builtin: u32,- pub function_object_has_builtin: u32,/// `Builtin` values.pub builtin_string_prototype_char_code_at: u8,pub builtin_string_prototype_char_at: u8,diff --git a/Libraries/LibJS/Rust/generate/layout_table.rs b/Libraries/LibJS/Rust/generate/layout_table.rsindex 9a9d5395ee8..4ac1fb18543 100644--- a/Libraries/LibJS/Rust/generate/layout_table.rs+++ b/Libraries/LibJS/Rust/generate/layout_table.rs@@ -186,6 +186,7 @@ pub fn generate(configuration: &LayoutConfiguration) -> LayoutWriter {w.section("Object flags");field!(w, "OBJECT_FLAGS", "Object.flags", "u16", Object, flags, 2, "nullable", "scalar");+ field!(w, "OBJECT_FUNCTION_BITS", "Object.function_bits", "u8", Object, function_bits, 1, "nullable", "scalar");w.constant("OBJECT_FLAG_HAS_MAGICAL_LENGTH", object_flag::HAS_MAGICAL_LENGTH_PROPERTY);w.constant("OBJECT_FLAG_MAY_INTERFERE", object_flag::MAY_INTERFERE_WITH_INDEXED_PROPERTY_ACCESS);w.constant("OBJECT_FLAG_IS_TYPED_ARRAY", object_flag::IS_TYPED_ARRAY);@@ -491,12 +492,13 @@ pub fn generate(configuration: &LayoutConfiguration) -> LayoutWriter {field!(w, "DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_IS_COMPRESSED", "DirectGetterFunction.implementation_value_is_compressed", "u32", DirectGetterFunction, implementation_value_is_compressed, 4, "nullable", "scalar");w.section("ECMAScriptFunctionObject layout");- field!(w, "ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA", "ECMAScriptFunctionObject.shared_data", "SharedFunctionInstanceData", FunctionObject, ecmascript_shared_data, 4, "nonnull", "compressed");+ field!(w, "ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA", "ECMAScriptFunctionObject.shared_data", "SharedFunctionInstanceData", EcmascriptFunctionObject, shared_data, 4, "nonnull", "compressed");field!(w, "ECMASCRIPT_FUNCTION_OBJECT_ENVIRONMENT", "ECMAScriptFunctionObject.environment", "Environment", EcmascriptFunctionObject, environment, 4, "nullable", "compressed");- field!(w, "ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT", "ECMAScriptFunctionObject.private_environment", "PrivateEnvironment", EcmascriptFunctionObject, private_environment, 4, "nullable", "compressed");- field!(w, "ECMASCRIPT_FUNCTION_OBJECT_SCRIPT_OR_MODULE", "ECMAScriptFunctionObject.script_or_module", "ScriptOrModule", EcmascriptFunctionObject, script_or_module, 16, "nullable", "scalar");+ field!(w, "ECMASCRIPT_FUNCTION_OBJECT_PRIVATE_ENVIRONMENT", "ECMAScriptFunctionObject.private_environment", "PrivateEnvironment", EcmascriptFunctionObjectWithRareData, private_environment, 4, "nullable", "compressed");+ w.constant("ECMASCRIPT_FUNCTION_FLAG_HAS_RARE_DATA", ecmascript_function_flags::HAS_RARE_DATA);w.section("SharedFunctionInstanceData layout");+ field!(w, "SHARED_FUNCTION_INSTANCE_DATA_SCRIPT_OR_MODULE", "SharedFunctionInstanceData.script_or_module", "ScriptOrModule", SharedFunctionInstanceData, script_or_module, 16, "nullable", "scalar");field!(w, "SHARED_FUNCTION_INSTANCE_DATA_EXECUTABLE", "SharedFunctionInstanceData.executable", "Executable", SharedFunctionInstanceData, executable, 4, "nullable", "compressed");field!(w, "SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA", "SharedFunctionInstanceData.asm_call_metadata", "u64", SharedFunctionInstanceData, asm_call_metadata, 8, "nullable", "scalar");offset!(w, "SHARED_FUNCTION_INSTANCE_DATA_FORMAL_PARAMETER_COUNT", SharedFunctionInstanceData, formal_parameter_count);diff --git a/Libraries/LibJS/Rust/src/gc/class_id.rs b/Libraries/LibJS/Rust/src/gc/class_id.rsindex 69e51db5c06..bbeda4afb6d 100644--- a/Libraries/LibJS/Rust/src/gc/class_id.rs+++ b/Libraries/LibJS/Rust/src/gc/class_id.rs@@ -51,6 +51,7 @@ define_class_ids! {DirectGetterFunction,CapturingNativeFunction,EcmascriptFunctionObject,+ EcmascriptFunctionObjectWithRareData,SharedFunctionInstanceData,CallEnvironmentTemplate,BoundFunction,diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rsindex d05f864a86f..b1d9d667ed5 100644--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs@@ -956,19 +956,26 @@ pub fn new_function(FunctionKind::AsyncGenerator => realm.async_generator_function_prototype(),};- let function = EcmascriptFunctionObject::create_from_function_data_with_prototype(- vm,- realm,- shared_data,- vm.lexical_environment(),- running_execution_context(vm).private_environment.get(),- prototype,- );-- if instruction.home_object.get().is_some() {- let home_object_value = values.home_object;- function.make_method(home_object_value.as_object());- }+ let function = if instruction.home_object.get().is_some() {+ EcmascriptFunctionObject::create_method(+ vm,+ realm,+ shared_data,+ vm.lexical_environment(),+ running_execution_context(vm).private_environment.get(),+ values.home_object.as_object(),+ prototype,+ )+ } else {+ EcmascriptFunctionObject::create_from_function_data_with_prototype(+ vm,+ realm,+ shared_data,+ vm.lexical_environment(),+ running_execution_context(vm).private_environment.get(),+ prototype,+ )+ };values.dst = Value::from_object(function);SlowPathControl::continue_at(pc + op::NewFunction::LENGTH)diff --git a/Libraries/LibJS/Rust/src/jit/allocation.rs b/Libraries/LibJS/Rust/src/jit/allocation.rsindex eb9339720df..d355314a8bb 100644--- a/Libraries/LibJS/Rust/src/jit/allocation.rs+++ b/Libraries/LibJS/Rust/src/jit/allocation.rs@@ -29,7 +29,7 @@ use crate::layout::cell::{CellHeader, Gc};use crate::layout::environment::{DeclarativeEnvironment, Environment, PrivateEnvironment};use crate::layout::function_object::{CallEnvironmentTemplate, EcmascriptFunctionObject, FUNCTION_ENVIRONMENT_WORDS};use crate::layout::member::NullableMember;-use crate::layout::object::{INLINE_NAMED_STORAGE_CAPACITY, Object};+use crate::layout::object::Object;use crate::layout::primitive_string::PrimitiveString;use crate::layout::realm::Realm;use crate::layout::shape::Shape;@@ -118,15 +118,10 @@ pub fn allocation_infos<'d>(vm: &Vm, realm: Gc<Realm>, defer_gc: &'d DeferGc<'_>..ObjectAllocationInfo::default()};// NB: Free lists are only popped inline where the heap gives their addresses (not in AddressSanitizer builds).- if let Some(local_free_list) = heap.local_free_list_of(Object::CLASS) {- object.size_classes.push(ObjectSizeClass {- allocator: local_free_list.as_ptr() as u64,- cell_size: size(size_of::<Object>()),- inline_capacity: INLINE_NAMED_STORAGE_CAPACITY as u32,- });+ if heap.local_free_list_of(Object::CLASS).is_some() {for (size_class, inline_capacity) in plain_object_size_classes(heap).iter()- .zip(&PLAIN_OBJECT_INLINE_CAPACITIES[1..])+ .zip(&PLAIN_OBJECT_INLINE_CAPACITIES){object.size_classes.push(ObjectSizeClass {allocator: address(size_class.local_free_list()),@@ -492,8 +487,6 @@ fn closure_template(None,realm.function_prototype(),);- // NB: The closures of the function's frames belong to its script or module, the active one in them.- sample.script_or_module.set(function.script_or_module.get());if !sample.can_be_copied() {return None;}@@ -570,6 +563,5 @@ pub extern "C" fn libjs_jit_clone_function(private_environment,realm.function_prototype(),);- closure.script_or_module.set(sample.script_or_module.get());closure.as_ptr()}diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rsindex f894b8fddde..1b868264a02 100644--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs@@ -42,9 +42,9 @@ use crate::layout::environment::{use crate::layout::executable::ExecutableHead;use crate::layout::execution_context::ExecutionContext;use crate::layout::function_object::{- CallEnvironmentTemplate, EcmascriptFunctionObject, FUNCTION_ENVIRONMENT_WORDS, FunctionObject,- NATIVE_FUNCTION_TABLE_INDEX_MASK, NativeFunction, NativeFunctionTableEntry, SharedFunctionInstanceData,- asm_call_metadata,+ CallEnvironmentTemplate, EcmascriptFunctionObject, EcmascriptFunctionObjectWithRareData,+ FUNCTION_ENVIRONMENT_WORDS, NATIVE_FUNCTION_TABLE_INDEX_MASK, NativeFunction, NativeFunctionTableEntry,+ SharedFunctionInstanceData, asm_call_metadata, ecmascript_function_flags,};use crate::layout::object::{INDEXED_ELEMENTS_HEADER_SIZE, IndexedStorageKind, Object, TYPED_ARRAY_CACHED_DATA_OFFSET_INVALID, TypedArrayBase,@@ -164,8 +164,13 @@ fn runtime_offsets() -> RuntimeOffsets {execution_context_returns_to_native_caller: offset(offset_of!(ExecutionContext, returns_to_native_caller)),execution_context_runs_jit_code: offset(offset_of!(ExecutionContext, runs_jit_code)),ecmascript_function_environment: offset(offset_of!(EcmascriptFunctionObject, environment)),- ecmascript_function_private_environment: offset(offset_of!(EcmascriptFunctionObject, private_environment)),- ecmascript_function_script_or_module: offset(offset_of!(EcmascriptFunctionObject, script_or_module)),+ ecmascript_function_private_environment: offset(offset_of!(+ EcmascriptFunctionObjectWithRareData,+ private_environment+ )),+ shared_function_script_or_module: offset(offset_of!(SharedFunctionInstanceData, script_or_module)),+ object_function_bits: offset(offset_of!(Object, function_bits)),+ ecmascript_function_flag_has_rare_data: ecmascript_function_flags::HAS_RARE_DATA.into(),vm_execution_generation: offset(offset_of!(VmHead, execution_generation)),}}@@ -226,8 +231,7 @@ fn runtime_layout() -> RuntimeLayout {utf16_string_data_storage: offset(size_of::<ak::Utf16StringDataHeader>()),// NB: Filled in by runtime_info().single_ascii_character_strings: 0,- function_object_builtin: offset(offset_of!(FunctionObject, builtin)),- function_object_has_builtin: offset(offset_of!(FunctionObject, has_builtin)),+ function_object_builtin: offset(offset_of!(Object, function_bits)),builtin_string_prototype_char_code_at: Builtin::StringPrototypeCharCodeAt as u8,builtin_string_prototype_char_at: Builtin::StringPrototypeCharAt as u8,builtin_math_abs: Builtin::MathAbs as u8,@@ -567,7 +571,7 @@ fn dynamic_call_layout(vm: &Vm) -> DynamicCallLayout {.expect("the VM has a JIT entry table while the JIT is on");DynamicCallLayout {object_flag_is_ecmascript_function: object_flag::IS_ECMASCRIPT_FUNCTION_OBJECT,- ecmascript_function_shared_data: offset(offset_of!(FunctionObject, ecmascript_shared_data)),+ ecmascript_function_shared_data: offset(offset_of!(EcmascriptFunctionObject, shared_data)),shared_data_executable: offset(offset_of!(SharedFunctionInstanceData, executable)),shared_data_asm_call_metadata: offset(offset_of!(SharedFunctionInstanceData, asm_call_metadata)),executable_jit_entry_slot: offset(offset_of!(ExecutableHead, jit_entry_slot)),diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rsindex ed6a6204fe6..ffe0ab79422 100644--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs@@ -39,7 +39,7 @@ use crate::layout::cell::{CellHeader, Gc};use crate::layout::environment::DeclarativeEnvironment;use crate::layout::execution_context::ExecutionContext;use crate::layout::function_object::{- DirectGetterFunction, EcmascriptFunctionObject, FunctionObject, RawNativeFunction,+ DirectGetterFunction, EcmascriptFunctionObject, FunctionObject, RawNativeFunction, SharedFunctionInstanceData,};use crate::layout::object::{Object, TYPED_ARRAY_CACHED_DATA_OFFSET_INVALID, TypedArrayBase};use crate::layout::property_lookup_cache::{GlobalVariableCache, PropertyLookupCache, PropertyLookupCacheEntryType};@@ -357,10 +357,14 @@ fn native_call_target(vm: &Vm, feedback: &CallFeedback) -> Option<Gc<RawNativeFu/// The words of an ECMAScript function its frames start with, which stay the same for its lifetime.fn function_frame_fields(function: Gc<EcmascriptFunctionObject>) -> FunctionFrameFields {+ let shared_data = function.shared_data();FunctionFrameFields {script_or_module: [- word_of(function, offset_of!(EcmascriptFunctionObject, script_or_module)),- word_of(function, offset_of!(EcmascriptFunctionObject, script_or_module) + 8),+ word_of(shared_data, offset_of!(SharedFunctionInstanceData, script_or_module)),+ word_of(+ shared_data,+ offset_of!(SharedFunctionInstanceData, script_or_module) + 8,+ ),],environment: function.environment()diff --git a/Libraries/LibJS/Rust/src/layout/function_object.rs b/Libraries/LibJS/Rust/src/layout/function_object.rsindex 6cb4ee973e6..b40a78d141b 100644--- a/Libraries/LibJS/Rust/src/layout/function_object.rs+++ b/Libraries/LibJS/Rust/src/layout/function_object.rs@@ -13,6 +13,7 @@ use super::execution_context::ScriptOrModule;use super::member::{Member, NullableMember};use super::object::Object;use super::realm::Realm;+use super::value::Value;use crate::layout_forward::{EcmascriptFunctionObjectStorage, FlyStringSlot, RawNativeFunctionPointer, SharedFunctionInstanceDataStorage,};@@ -20,22 +21,18 @@ use crate::layout_forward::{#[repr(C)]pub struct FunctionObject {pub base: Object,- /// The interpreter reads the builtin and whether there is one as two separate bytes, like Optional<Builtin>.- pub builtin: Cell<u8>,- pub has_builtin: Cell<bool>,- /// Flags of the classes extending FunctionObject, in what would otherwise be padding (see- /// ecmascript_function_flags).- pub flags: Cell<u8>,- /// The shared data of an ECMAScript function object, in what would otherwise be padding (null for other- /// functions). See EcmascriptFunctionObject::shared_data().- pub ecmascript_shared_data: NullableMember<SharedFunctionInstanceData>,}-const _: () = assert!(size_of::<FunctionObject>() == 56);+const _: () = assert!(size_of::<FunctionObject>() == size_of::<Object>());++/// The property values of a function that are its "length" and "name".+pub const FUNCTION_LENGTH_AND_NAME_SLOTS: usize = 2;#[repr(C)]pub struct NativeFunction {pub base: FunctionObject,+ /// OPTIMIZATION: The named properties of most native functions are "length" and "name".+ pub inline_named_storage: [Cell<Value>; FUNCTION_LENGTH_AND_NAME_SLOTS],/// The name call stacks show, which only some native functions have.pub name: FlyStringSlot,pub initial_name: FlyStringSlot,@@ -45,14 +42,15 @@ pub struct NativeFunction {pub raw_native_function_index: Cell<u32>,}-const _: () = assert!(size_of::<NativeFunction>() == 80);+const _: () = assert!(size_of::<NativeFunction>() == 72);+const _: () = assert!(core::mem::offset_of!(NativeFunction, inline_named_storage) == size_of::<Object>());#[repr(C)]pub struct RawNativeFunction {pub base: NativeFunction,}-const _: () = assert!(size_of::<RawNativeFunction>() == 80);+const _: () = assert!(size_of::<RawNativeFunction>() == 72);/// A raw native getter that reads a wrapped value straight out of a platform object, at offsets the embedder provides:/// word offsets of pointer-sized fields, and the byte offset of the implementation value, which is a compressed cell@@ -87,17 +85,34 @@ pub struct NativeFunctionTableEntry {#[repr(C)]pub struct EcmascriptFunctionObject {pub base: FunctionObject,+ pub shared_data: NullableMember<SharedFunctionInstanceData>,pub environment: NullableMember<Environment>,+}++/// An ECMAScript function object that needs more than most: a method or a class constructor, or a function of code+/// with private names (see ecmascript_function_flags::HAS_RARE_DATA).+#[repr(C)]+pub struct EcmascriptFunctionObjectWithRareData {+ pub base: EcmascriptFunctionObject,pub private_environment: NullableMember<PrivateEnvironment>,- pub name: FlyStringSlot,- pub script_or_module: Cell<ScriptOrModule>,+ pub home_object: NullableMember<Object>,pub storage: EcmascriptFunctionObjectStorage,}/// The bits of FunctionObject::flags of an ECMAScript function object.pub mod ecmascript_function_flags {+ /// Set on all ECMAScript function objects, which keeps their flags from reading as a builtin.+ pub const IS_ECMASCRIPT_FUNCTION: u8 = 1 << 7;pub const MAY_NEED_LAZY_PROTOTYPE_INSTANTIATION: u8 = 1 << 0;pub const IS_METHOD: u8 = 1 << 1;+ /// The function does not have its "length" and "name" properties yet, and acts as if it had them, in front of the+ /// properties it has. Functions with this flag have shapes of their own: descendants of the premade shape of their+ /// kind, whose first property is not "length".+ pub const HAS_LAZY_LENGTH_AND_NAME: u8 = 1 << 2;+ /// The function is an EcmascriptFunctionObjectWithRareData.+ pub const HAS_RARE_DATA: u8 = 1 << 3;+ /// The function was given a name other than the one of its shared data, which is the value of its "name" property.+ pub const HAS_CUSTOM_NAME: u8 = 1 << 4;}/// Bits of SharedFunctionInstanceData::asm_call_metadata above the formal parameter count in its low 32 bits.@@ -120,6 +135,8 @@ pub struct SharedFunctionInstanceData {pub can_inline_call: Cell<bool>,/// How JIT code allocates the function environments of calls of the functions, once the runtime made one.pub call_environment_template: NullableMember<CallEnvironmentTemplate>,+ /// The [[ScriptOrModule]] of the functions, which is the same for all of them.+ pub script_or_module: Cell<ScriptOrModule>,pub storage: SharedFunctionInstanceDataStorage,}diff --git a/Libraries/LibJS/Rust/src/layout/object.rs b/Libraries/LibJS/Rust/src/layout/object.rsindex 393730baaf5..1e7254f20a9 100644--- a/Libraries/LibJS/Rust/src/layout/object.rs+++ b/Libraries/LibJS/Rust/src/layout/object.rs@@ -46,8 +46,6 @@ pub enum IndexedStorageKind {/// Packed and holey indexed storage keeps its capacity in a header right in front of the first element.pub const INDEXED_ELEMENTS_HEADER_SIZE: usize = 8;-pub const INLINE_NAMED_STORAGE_CAPACITY: usize = 2;-/// An object starts with the shape and the array-like size in the first word of its cell header, which LibGC leaves/// to the cell, followed by the rest of the header.#[repr(C)]@@ -59,13 +57,16 @@ pub struct Object {pub kind: CellKind,pub indexed_storage_kind: Cell<IndexedStorageKind>,pub flags: Cell<u16>,- /// How many named property values the inline storage holds. It ends the object, and only plain objects and- /// mapped arguments objects have cells with room for more than INLINE_NAMED_STORAGE_CAPACITY.+ /// How many named property values the inline storage holds. It comes right after the object, in the cells of the+ /// classes that make room for it.pub inline_named_capacity: Cell<u8>,+ /// What a function object keeps in the object: its builtin plus one (zero for none), or ecmascript_function_flags+ /// for an ECMAScript function object, which have the highest bit set. Zero for other objects.+ pub function_bits: Cell<u8>,/// Points at `inline_named_storage` until the object needs more room.pub named_properties: Cell<*mut Value>,pub indexed_elements: Cell<*mut Value>,- pub inline_named_storage: [Cell<Value>; INLINE_NAMED_STORAGE_CAPACITY],+ pub inline_named_storage: [Cell<Value>; 0],}const _: () = {@@ -75,11 +76,8 @@ const _: () = {assert!(offset_of!(Object, kind) == offset_of!(CellHeader, kind));};-const _: () = assert!(size_of::<Object>() == 48);-const _: () = assert!(- core::mem::offset_of!(Object, inline_named_storage) + INLINE_NAMED_STORAGE_CAPACITY * size_of::<Value>()- == size_of::<Object>()-);+const _: () = assert!(size_of::<Object>() == 32);+const _: () = assert!(core::mem::offset_of!(Object, inline_named_storage) == size_of::<Object>());/// Mirrors the Variant<Auto, Detached, size_t> the interpreter reads: the length first, then the alternative's index.#[repr(C)]diff --git a/Libraries/LibJS/Rust/src/runtime/arguments_object.rs b/Libraries/LibJS/Rust/src/runtime/arguments_object.rsindex 0966138cb88..644017fe027 100644--- a/Libraries/LibJS/Rust/src/runtime/arguments_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/arguments_object.rs@@ -15,7 +15,7 @@ use crate::gc::class::{GcCell, cast_ref, define_cell};use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::environment::Environment;-use crate::layout::object::{INLINE_NAMED_STORAGE_CAPACITY, Object};+use crate::layout::object::Object;use crate::layout::value::Value;use crate::runtime::completion::{Must, ThrowCompletionOr};use crate::runtime::object::{@@ -27,16 +27,17 @@ use crate::runtime::property_key::PropertyKey;use crate::runtime::realm::Realm;use crate::runtime::value::same_value;+const ARGUMENTS_OBJECT_NAMED_PROPERTY_COUNT: usize = 3;+/// The arguments exotic object of a function with a simple parameter list in sloppy mode code, whose indexed/// properties alias the bindings of the parameters they were passed for.#[repr(C)]#[derive(Trace)]pub struct ArgumentsObject {base: Object,- /// OPTIMIZATION: The named properties of arguments objects, "length", "callee" and @@iterator, fit in the inline- /// named storage with this slot after it.- #[gc(untraced = "the object traces it as the last slot of its inline named storage")]- extra_inline_named_storage: Cell<Value>,+ /// OPTIMIZATION: The named properties of arguments objects, "length", "callee" and @@iterator, are inline.+ #[gc(untraced = "the object traces its inline named storage")]+ inline_named_storage: [Cell<Value>; ARGUMENTS_OBJECT_NAMED_PROPERTY_COUNT],environment: Cell<Gc<Environment>>,#[gc(untraced)]parameter_map: RefCell<ParameterMap>,@@ -75,8 +76,8 @@ fn as_arguments_object(object: &Object) -> &ArgumentsObject {cast_ref(object)}-// NB: The inline named storage ends the object, so the extra slot continues it.-const _: () = assert!(core::mem::offset_of!(ArgumentsObject, extra_inline_named_storage) == size_of::<Object>());+// NB: The inline named storage of an object comes right after its Object.+const _: () = assert!(core::mem::offset_of!(ArgumentsObject, inline_named_storage) == size_of::<Object>());impl ArgumentsObject {pub fn create(vm: &Vm, realm: Gc<Realm>, environment: Gc<Environment>, parameter_list_is_empty: bool) -> Gc<Self> {@@ -89,10 +90,11 @@ impl ArgumentsObject {MayInterfereWithIndexedPropertyAccess::Yes},);- base.inline_named_capacity.set(INLINE_NAMED_STORAGE_CAPACITY as u8 + 1);+ base.inline_named_capacity+ .set(ARGUMENTS_OBJECT_NAMED_PROPERTY_COUNT as u8);allocate_object(vm, |_| ArgumentsObject {base,- extra_inline_named_storage: Cell::new(Value::UNDEFINED),+ inline_named_storage: [const { Cell::new(Value::UNDEFINED) }; ARGUMENTS_OBJECT_NAMED_PROPERTY_COUNT],environment: Cell::new(environment),parameter_map: RefCell::new(ParameterMap {names: Rc::from([]),diff --git a/Libraries/LibJS/Rust/src/runtime/array.rs b/Libraries/LibJS/Rust/src/runtime/array.rsindex ad53a01203c..9ef35047183 100644--- a/Libraries/LibJS/Rust/src/runtime/array.rs+++ b/Libraries/LibJS/Rust/src/runtime/array.rs@@ -43,7 +43,7 @@ pub struct Array {is_proxy_target: Cell<bool>,}-const _: () = assert!(size_of::<Array>() == 56);+const _: () = assert!(size_of::<Array>() == 40);/// The offsets of the flags JIT code checks before writing elements of an array.pub const ARRAY_LENGTH_WRITABLE_OFFSET: usize = core::mem::offset_of!(Array, length_writable);diff --git a/Libraries/LibJS/Rust/src/runtime/bound_function.rs b/Libraries/LibJS/Rust/src/runtime/bound_function.rsindex 7748ef6fcf0..1ad9cd9aa2a 100644--- a/Libraries/LibJS/Rust/src/runtime/bound_function.rs+++ b/Libraries/LibJS/Rust/src/runtime/bound_function.rs@@ -14,6 +14,7 @@ use crate::gc::class::{GcCell, cast_ref, define_cell};use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::execution_context::ExecutionContext;+use crate::layout::function_object::FUNCTION_LENGTH_AND_NAME_SLOTS;use crate::layout::object::Object;use crate::layout::value::Value;use crate::runtime::completion::ThrowCompletionOr;@@ -25,6 +26,9 @@ use crate::runtime::realm::Realm;#[derive(Trace)]pub struct BoundFunction {base: FunctionObject,+ /// OPTIMIZATION: The named properties of bound functions are "length" and "name".+ #[gc(untraced = "the object traces its inline named storage")]+ inline_named_storage: [Cell<Value>; FUNCTION_LENGTH_AND_NAME_SLOTS],bound_target_function: Cell<Gc<FunctionObject>>, // [[BoundTargetFunction]]bound_this: Cell<Value>, // [[BoundThis]]bound_arguments: OnceCell<Box<[Value]>>, // [[BoundArguments]]@@ -120,8 +124,10 @@ impl BoundFunction {// 7. Set obj.[[BoundTargetFunction]] to targetFunction.// 8. Set obj.[[BoundThis]] to boundThis.// 9. Set obj.[[BoundArguments]] to boundArgs.+ base.inline_named_capacity.set(FUNCTION_LENGTH_AND_NAME_SLOTS as u8);let object = allocate_object(vm, |_| BoundFunction {base,+ inline_named_storage: [const { Cell::new(Value::UNDEFINED) }; FUNCTION_LENGTH_AND_NAME_SLOTS],bound_target_function: Cell::new(target_function),bound_this: Cell::new(bound_this),bound_arguments: OnceCell::new(),diff --git a/Libraries/LibJS/Rust/src/runtime/class_construction.rs b/Libraries/LibJS/Rust/src/runtime/class_construction.rsindex 0fd657a33c4..bcbb8d4b85d 100644--- a/Libraries/LibJS/Rust/src/runtime/class_construction.rs+++ b/Libraries/LibJS/Rust/src/runtime/class_construction.rs@@ -145,6 +145,7 @@ pub fn construct_class(// FIXME: Step 14.a is done in the parser. By using a synthetic super(...args) which does not call @@iterator of %Array.prototype%let constructor_shared_data = executable.shared_function_data(blueprint.constructor_shared_function_data_index);+ constructor_shared_data.set_is_class_constructor();let class_constructor = EcmascriptFunctionObject::create_from_function_data(vm,realm,@@ -154,8 +155,7 @@ pub fn construct_class();class_constructor.set_name(vm, class_name);- class_constructor.set_home_object(Some(prototype));- class_constructor.set_is_class_constructor();+ class_constructor.set_home_object_of_class_constructor(prototype);class_constructor.define_direct_property(vm,&vm.names.prototype,@@ -196,16 +196,17 @@ pub fn construct_class(.shared_function_data_index.expect("a method has shared function data"),);- let method_function = EcmascriptFunctionObject::create_from_function_data(+ let method_function = EcmascriptFunctionObject::create_method(vm,realm,shared_data,vm.lexical_environment(),running_execution_context(vm).private_environment.get(),+ home_object,+ realm.function_prototype(),);let method_value = Value::from_object(method_function);- method_function.make_method(home_object);match &element_name {ClassElementName::PropertyKey(property_key) => match descriptor.kind {@@ -342,14 +343,15 @@ pub fn construct_class(});}- let function = EcmascriptFunctionObject::create_from_function_data(+ let function = EcmascriptFunctionObject::create_method(vm,realm,shared_data,vm.lexical_environment(),running_execution_context(vm).private_environment.get(),+ home_object,+ realm.function_prototype(),);- function.make_method(home_object);initializer = ClassFieldInitializer::Function(function);}}@@ -372,14 +374,15 @@ pub fn construct_class(.shared_function_data_index.expect("a static initializer has shared function data"),);- let body_function = EcmascriptFunctionObject::create_from_function_data(+ let body_function = EcmascriptFunctionObject::create_method(vm,realm,shared_data,vm.lexical_environment(),running_execution_context(vm).private_environment.get(),+ home_object,+ realm.function_prototype(),);- body_function.make_method(home_object);static_elements.push(StaticElement::Block(body_function));}}diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rsindex 2d5339ebb02..e7f78b1865f 100644--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs@@ -23,12 +23,11 @@ use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::environment::{Environment, PrivateEnvironment};use crate::layout::execution_context::{ExecutionContext, ScriptOrModule};-pub use crate::layout::function_object::EcmascriptFunctionObject;use crate::layout::function_object::ecmascript_function_flags;+pub use crate::layout::function_object::{EcmascriptFunctionObject, EcmascriptFunctionObjectWithRareData};use crate::layout::member::NullableMember;use crate::layout::object::{IndexedStorageKind, Object};use crate::layout::value::Value;-use crate::layout_forward::FlyStringSlot;use crate::runtime::abstract_operations::{create_unmapped_arguments_object, get_prototype_from_constructor, new_function_environment,};@@ -51,7 +50,7 @@ use crate::runtime::property_descriptor::PropertyDescriptor;use crate::runtime::property_key::PropertyKey;use crate::runtime::realm::Realm;use crate::runtime::shared_function_instance_data::{- ClassFieldInitializerName, ConstructorKind, FunctionKind, SharedFunctionInstanceData, ThisMode,+ ClassFieldInitializerName, ClosureCount, ConstructorKind, FunctionKind, SharedFunctionInstanceData, ThisMode,};use crate::source_code::SourceCode;use crate::utf16::to_utf16_fly_string;@@ -62,38 +61,39 @@ struct ClassData {private_methods: Vec<PrivateElement>, // [[PrivateMethods]]}-/// The parts of an ECMAScript function object that only methods and class constructors have.-#[derive(Default, Trace)]-struct RareData {- home_object: Cell<Option<Gc<Object>>>, // [[HomeObject]]- class_data: GcRefCell<ClassData>,-}--/// The parts of an ECMAScript function object the interpreter does not read.+/// The parts of a class constructor that the interpreter does not read.#[derive(Default)]pub struct EcmascriptFunctionObjectStorage {- rare_data: OnceCell<Box<RareData>>,+ class_data: OnceCell<Box<GcRefCell<ClassData>>>,}// NB: Every ECMAScript function has these bytes, so keep what only some functions need in the rare data.-const _: () = assert!(size_of::<EcmascriptFunctionObject>() == 96);+const _: () = assert!(size_of::<EcmascriptFunctionObject>() == 40);-// SAFETY: Visits the cells of the rare data, if there is any.+// SAFETY: Visits the cells of the class data, if there is any.unsafe impl Trace for EcmascriptFunctionObjectStorage {fn trace(&self, visitor: &mut Visitor) {- if let Some(rare_data) = self.rare_data.get() {- rare_data.trace(visitor);+ if let Some(class_data) = self.class_data.get() {+ class_data.trace(visitor);}}}pub static ECMASCRIPT_FUNCTION_OBJECT_METHODS: ObjectMethods = ObjectMethods {internal_get_own_property: EcmascriptFunctionObject::internal_get_own_property,- // NB: Functions answer for "prototype" before instantiating it lazily, and for "caller" and "arguments" if they- // support the legacy properties, whatever their shape holds. They answer for other names like ordinary- // objects.+ // NB: Functions answer for "length", "name" and "prototype" before creating them lazily, and for "caller" and+ // "arguments" if they support the legacy properties, whatever their shape holds. They answer for other names+ // like ordinary objects.is_cacheable_for_absence_of: |_, vm, property_key| {- *property_key != vm.names.prototype && *property_key != vm.names.caller && *property_key != vm.names.arguments+ *property_key != vm.names.prototype+ && *property_key != vm.names.caller+ && *property_key != vm.names.arguments+ && *property_key != vm.names.length+ && *property_key != vm.names.name+ },+ // NB: The non-enumerable lazy properties shadow enumerable ones of the prototype chain.+ eligible_for_own_property_enumeration_fast_path: |object| {+ !as_ecmascript_function(object).has_flag(ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME)},internal_own_property_keys: EcmascriptFunctionObject::internal_own_property_keys,internal_call: Some(EcmascriptFunctionObject::internal_call),@@ -118,14 +118,32 @@ unsafe impl Trace for EcmascriptFunctionObject {fn trace(&self, visitor: &mut Visitor) {self.base.trace(visitor);self.environment.trace(visitor);+ visitor.visit_nullable_member(&self.shared_data);+ }+}++define_cell!(+ EcmascriptFunctionObjectWithRareData,+ Object,+ extends: [EcmascriptFunctionObject, FunctionObject, Object],+ methods: ECMASCRIPT_FUNCTION_OBJECT_METHODS+);++// SAFETY: Visits every cell the rare data of an ECMAScript function object holds, besides those of the function.+unsafe impl Trace for EcmascriptFunctionObjectWithRareData {+ fn trace(&self, visitor: &mut Visitor) {+ self.base.trace(visitor);self.private_environment.trace(visitor);- visitor.visit_nullable_member(&self.base.ecmascript_shared_data);+ self.home_object.trace(visitor);self.storage.trace(visitor);- match self.script_or_module.get() {- ScriptOrModule::Empty => {}- ScriptOrModule::Script(script) => script.trace(visitor),- ScriptOrModule::Module(module) => module.trace(visitor),- }+ }+}++impl Deref for EcmascriptFunctionObjectWithRareData {+ type Target = EcmascriptFunctionObject;++ fn deref(&self) -> &EcmascriptFunctionObject {+ &self.base}}@@ -173,21 +191,85 @@ impl EcmascriptFunctionObject {private_environment: Option<Gc<PrivateEnvironment>>,prototype: Gc<Object>,) -> Gc<EcmascriptFunctionObject> {+ Self::create(+ vm,+ realm,+ shared_data,+ parent_environment,+ private_environment,+ None,+ prototype,+ )+ }++ /// A function that is a method of `home_object` (see make_method()).+ pub fn create_method(+ vm: &Vm,+ realm: Gc<Realm>,+ shared_data: Gc<SharedFunctionInstanceData>,+ parent_environment: Option<Gc<Environment>>,+ private_environment: Option<Gc<PrivateEnvironment>>,+ home_object: Gc<Object>,+ prototype: Gc<Object>,+ ) -> Gc<EcmascriptFunctionObject> {+ Self::create(+ vm,+ realm,+ shared_data,+ parent_environment,+ private_environment,+ Some(home_object),+ prototype,+ )+ }++ fn create(+ vm: &Vm,+ realm: Gc<Realm>,+ shared_data: Gc<SharedFunctionInstanceData>,+ parent_environment: Option<Gc<Environment>>,+ private_environment: Option<Gc<PrivateEnvironment>>,+ home_object: Option<Gc<Object>>,+ prototype: Gc<Object>,+ ) -> Gc<EcmascriptFunctionObject> {+ // 15. Set F.[[ScriptOrModule]] to GetActiveScriptOrModule().+ // OPTIMIZATION: Every function with the same shared data keeps the one of the first: the code that creates a+ // function belongs to the script or module of the function. Only code with no script or module+ // of its own, like an event handler, could see different ones.+ if shared_data.closure_count() == ClosureCount::None {+ shared_data.script_or_module.set(vm.get_active_script_or_module());+ }shared_data.note_closure_created();- let function = allocate_object(vm, |defer_gc| {- Self::new(- defer_gc,- vm,- shared_data,- parent_environment,- private_environment,- prototype,- )- });+ let function = if private_environment.is_some() || home_object.is_some() || shared_data.is_class_constructor() {+ let function = allocate_object(vm, |defer_gc| EcmascriptFunctionObjectWithRareData {+ base: Self::new(defer_gc, vm, shared_data, parent_environment, prototype),+ private_environment: NullableMember::new(private_environment),+ home_object: NullableMember::new(home_object),+ storage: EcmascriptFunctionObjectStorage::default(),+ });+ function.set_flag(ecmascript_function_flags::HAS_RARE_DATA, true);+ function.upcast()+ } else {+ allocate_object(vm, |defer_gc| {+ Self::new(defer_gc, vm, shared_data, parent_environment, prototype)+ })+ };function.initialize(vm, realm);+ if home_object.is_some() {+ function.make_method();+ }function}+ /// The function's rare data, if it is a function that has any.+ fn with_rare_data(&self) -> Option<&EcmascriptFunctionObjectWithRareData> {+ if !self.has_flag(ecmascript_function_flags::HAS_RARE_DATA) {+ return None;+ }+ // SAFETY: Only an EcmascriptFunctionObjectWithRareData has this flag.+ Some(unsafe { &*core::ptr::from_ref(self).cast::<EcmascriptFunctionObjectWithRareData>() })+ }+pub fn create_from_function_data(vm: &Vm,realm: Gc<Realm>,@@ -211,16 +293,17 @@ impl EcmascriptFunctionObject {vm: &Vm,shared_data: Gc<SharedFunctionInstanceData>,parent_environment: Option<Gc<Environment>>,- private_environment: Option<Gc<PrivateEnvironment>>,prototype: Gc<Object>,) -> Self {let base =FunctionObject::new_with_prototype(vm, Self::CLASS, prototype, MayInterfereWithIndexedPropertyAccess::No);base.set_is_ecmascript_function_object();- base.ecmascript_shared_data.set(Some(shared_data));+ base.function_bits+ .set(ecmascript_function_flags::IS_ECMASCRIPT_FUNCTION);- // OPTIMIZATION: Start from a premade shape that already has this function kind's own properties in spec order.- // Arrow functions use the same shape as other functions of their kind, but never get a lazy prototype.+ // OPTIMIZATION: Start from a premade shape: normal and async functions create their properties lazily, and+ // the other kinds already have theirs in spec order. Arrow functions use the same shape as other+ // functions of their kind, but never get a lazy prototype.let realm = base.shape().realm();let function_shape = match shared_data.kind() {FunctionKind::Normal => realm.normal_function_shape(),@@ -236,16 +319,10 @@ impl EcmascriptFunctionObject {.set(function_shape.create_prototype_transition(vm, Some(prototype)));}- // 15. Set F.[[ScriptOrModule]] to GetActiveScriptOrModule().- let script_or_module = vm.get_active_script_or_module();-Self {base,+ shared_data: NullableMember::new(Some(shared_data)),environment: NullableMember::new(parent_environment),- private_environment: NullableMember::new(private_environment),- name: FlyStringSlot::new(None),- script_or_module: Cell::new(script_or_module),- storage: EcmascriptFunctionObjectStorage::default(),}}@@ -256,23 +333,15 @@ impl EcmascriptFunctionObject {// which must give the properties in chronological order which in this case is the order they// are defined in the spec.- let name_string = PrimitiveString::create_from_fly_string(vm, &self.name());-- // NOTE: The constructor gave us a premade shape with "length" and "name" (and "prototype" for generator kinds) at- // these offsets, with the attributes the spec requires, so we only have to store the values.- self.initialize_direct(- realm.normal_function_length_offset(),- Value::from_i32(self.function_length()),- );- self.initialize_direct(realm.normal_function_name_offset(), Value::from_string(name_string));-match self.kind() {FunctionKind::Normal => {+ self.make_length_and_name_lazy();if !self.is_arrow_function() {self.set_flag(ecmascript_function_flags::MAY_NEED_LAZY_PROTOTYPE_INSTANTIATION, true);}}FunctionKind::Generator => {+ self.initialize_length_and_name(vm, realm);// prototype is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)self.initialize_direct(realm.generator_function_prototype_property_offset(),@@ -286,8 +355,10 @@ impl EcmascriptFunctionObject {FunctionKind::Async => {// 27.7.4 AsyncFunction Instances, https://tc39.es/ecma262/#sec-async-function-instances// AsyncFunction instances do not have a prototype property as they are not constructible.+ self.make_length_and_name_lazy();}FunctionKind::AsyncGenerator => {+ self.initialize_length_and_name(vm, realm);self.initialize_direct(realm.generator_function_prototype_property_offset(),Value::from_object(Object::create_prototype(@@ -300,6 +371,76 @@ impl EcmascriptFunctionObject {}}+ /// NOTE: The constructor gave us a premade shape with "length" and "name" (and "prototype" for generator kinds) at+ /// these offsets, with the attributes the spec requires, so we only have to store the values.+ fn initialize_length_and_name(&self, vm: &Vm, realm: Gc<Realm>) {+ let name_string = PrimitiveString::create_from_fly_string(vm, &self.name());+ self.initialize_direct(+ realm.generator_function_length_offset(),+ Value::from_i32(self.function_length()),+ );+ self.initialize_direct(realm.generator_function_name_offset(), Value::from_string(name_string));+ }++ /// OPTIMIZATION: Few functions ever have their "length" or "name" looked at, so they make them when that happens.+ fn make_length_and_name_lazy(&self) {+ self.set_flag(ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME, true);+ }++ /// Gives the function its "length" and "name" properties if it does not have them yet, as its first properties,+ /// since property keys come in the order they were added.+ #[inline(always)]+ pub(crate) fn reify_length_and_name(&self, vm: &Vm) {+ if self.has_flag(ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME) {+ self.reify_lazy_length_and_name(vm);+ }+ }++ #[cold]+ fn reify_lazy_length_and_name(&self, vm: &Vm) {+ let name_string = PrimitiveString::create_from_fly_string(vm, &self.name());+ self.reify_lazy_length_and_name_with_name(vm, name_string);+ }++ fn reify_lazy_length_and_name_with_name(&self, vm: &Vm, name_string: Gc<PrimitiveString>) {+ self.set_flag(ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME, false);++ let old_shape = self.shape();+ let realm = old_shape.realm();+ let mut shape = match self.kind() {+ FunctionKind::Normal => realm.normal_function_shape(),+ FunctionKind::Async => realm.async_function_shape(),+ FunctionKind::Generator | FunctionKind::AsyncGenerator => {+ unreachable!("generator functions have their length and name from the start")+ }+ };+ if shape.prototype() != old_shape.prototype() {+ shape = shape.create_prototype_transition(vm, old_shape.prototype());+ }+ let attributes = PropertyAttributes::new(Attribute::CONFIGURABLE);+ shape = shape.create_put_transition(vm, &vm.names.length, attributes);+ shape = shape.create_put_transition(vm, &vm.names.name, attributes);+ // NB: Adding the transitions can allocate, which must not happen while the shape is borrowed, so the properties+ // are read out first.+ let property_count = old_shape.property_count() as usize;+ let property_keys = MarkedVec::with_capacity(vm, property_count);+ let mut property_attributes = Vec::with_capacity(property_count);+ old_shape.for_each_property_in_insertion_order(|property_key, metadata| {+ property_keys.push(property_key.clone());+ property_attributes.push(metadata.attributes);+ ControlFlow::Continue(())+ });+ for (index, attributes) in property_attributes.into_iter().enumerate() {+ let property_key = property_keys.get(index).expect("the index is in bounds");+ shape = shape.create_put_transition(vm, &property_key, attributes);+ }++ self.prepend_named_properties(+ shape,+ &[Value::from_i32(self.function_length()), Value::from_string(name_string)],+ );+ }+pub fn as_ecmascript_function_gc(&self) -> Gc<EcmascriptFunctionObject> {Gc::from_ref(self)}@@ -488,9 +629,9 @@ impl EcmascriptFunctionObject {}// 10.2.7 MakeMethod ( F, homeObject ), https://tc39.es/ecma262/#sec-makemethod- pub fn make_method(&self, home_object: Gc<Object>) {+ fn make_method(&self) {// 1. Set F.[[HomeObject]] to homeObject.- self.rare_data().home_object.set(Some(home_object));+ // NB: create() did.self.set_flag(ecmascript_function_flags::IS_METHOD, true);self.set_flag(ecmascript_function_flags::MAY_NEED_LAZY_PROTOTYPE_INSTANTIATION, false);@@ -515,7 +656,7 @@ impl EcmascriptFunctionObject {callee_context.realm.set(self.realm());// 6. Set the ScriptOrModule of calleeContext to F.[[ScriptOrModule]].- callee_context.script_or_module.set(self.script_or_module.get());+ callee_context.script_or_module.set(self.script_or_module());if self.function_environment_needed() {// 7. Let localEnv be NewFunctionEnvironment(F, newTarget).@@ -541,7 +682,7 @@ impl EcmascriptFunctionObject {}// 10. Set the PrivateEnvironment of calleeContext to F.[[PrivateEnvironment]].- callee_context.private_environment.set(self.private_environment.get());+ callee_context.private_environment.set(self.private_environment());// 11. If callerContext is not already suspended, suspend callerContext.// 12. Push calleeContext onto the execution context stack; calleeContext is now the running execution context.@@ -697,8 +838,12 @@ impl EcmascriptFunctionObject {}pub fn set_name(&self, vm: &Vm, name: &Utf16FlyString) {- self.name.set(Some(name.clone()));+ self.set_flag(ecmascript_function_flags::HAS_CUSTOM_NAME, true);let name_string = PrimitiveString::create_from_fly_string(vm, name);+ if self.has_flag(ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME) {+ self.reify_lazy_length_and_name_with_name(vm, name_string);+ return;+ }let mut descriptor = PropertyDescriptor {value: Some(Value::from_string(name_string)),writable: Some(false),@@ -723,86 +868,88 @@ impl EcmascriptFunctionObject {/// nothing outside the heap as an object, and no property storage outside its cell.pub fn can_be_copied(&self) -> bool {let object = &self.base.base;- self.name.get().is_none()- && self.storage.rare_data.get().is_none()+ !self.has_flag(ecmascript_function_flags::HAS_CUSTOM_NAME)+ && !self.has_flag(ecmascript_function_flags::HAS_RARE_DATA)&& object.owns_nothing_outside_the_heap()&& object.named_storage_is_inline()&& object.indexed_storage_kind() == IndexedStorageKind::None}pub fn name(&self) -> Utf16FlyString {- self.name.get().unwrap_or_else(|| self.shared_data().name())+ if self.has_flag(ecmascript_function_flags::HAS_CUSTOM_NAME)+ && let Some(metadata) = self.shape().lookup(&Vm::of_cell(self).names.name)+ {+ let name = self.get_direct(metadata.offset);+ if name.is_string() {+ return to_utf16_fly_string(&name.as_string().utf16_string());+ }+ }+ self.shared_data().name()}fn has_flag(&self, flag: u8) -> bool {- self.base.flags.get() & flag != 0+ self.function_bits.get() & flag != 0}fn set_flag(&self, flag: u8, value: bool) {- let flags = self.base.flags.get();- self.base.flags.set(if value { flags | flag } else { flags & !flag });+ let flags = self.function_bits.get();+ self.function_bits.set(if value { flags | flag } else { flags & !flag });}- fn rare_data(&self) -> &RareData {- self.storage.rare_data.get_or_init(Box::default)+ fn class_data(&self) -> Option<&GcRefCell<ClassData>> {+ self.with_rare_data()?.storage.class_data.get().map(Box::as_ref)+ }++ fn class_data_to_add_to(&self) -> &GcRefCell<ClassData> {+ self.with_rare_data()+ .expect("a class constructor has rare data")+ .storage+ .class_data+ .get_or_init(Box::default)}pub fn has_class_data(&self) -> bool {- self.storage.rare_data.get().is_some_and(|rare_data| {- let class_data = rare_data.class_data.borrow();+ self.class_data().is_some_and(|class_data| {+ let class_data = class_data.borrow();!class_data.fields.is_empty() || !class_data.private_methods.is_empty()})}pub fn fields_count(&self) -> usize {- self.storage- .rare_data- .get()- .map_or(0, |rare_data| rare_data.class_data.borrow().fields.len())+ self.class_data()+ .map_or(0, |class_data| class_data.borrow().fields.len())}/// A copy of the field record at `index` of [[Fields]].pub fn field(&self, index: usize) -> ClassFieldDefinition {- self.storage- .rare_data- .get()- .expect("the function has class data")- .class_data- .borrow()- .fields[index]- .clone()+ self.class_data().expect("the function has class data").borrow().fields[index].clone()}pub fn add_field(&self, vm: &Vm, field: ClassFieldDefinition) {- self.rare_data().class_data.borrow_mut(vm).fields.push(field);+ self.class_data_to_add_to().borrow_mut(vm).fields.push(field);}pub fn private_methods_count(&self) -> usize {- self.storage- .rare_data- .get()- .map_or(0, |rare_data| rare_data.class_data.borrow().private_methods.len())+ self.class_data()+ .map_or(0, |class_data| class_data.borrow().private_methods.len())}/// A copy of the element at `index` of [[PrivateMethods]].pub fn private_method(&self, index: usize) -> PrivateElement {- self.storage- .rare_data- .get()+ self.class_data().expect("the function has class data")- .class_data.borrow().private_methods[index].clone()}pub fn add_private_method(&self, vm: &Vm, method: PrivateElement) {- self.rare_data().class_data.borrow_mut(vm).private_methods.push(method);+ self.class_data_to_add_to().borrow_mut(vm).private_methods.push(method);}pub fn shared_data(&self) -> Gc<SharedFunctionInstanceData> {// SAFETY: An ECMAScript function object gets its shared data when it is created, and keeps it.- unsafe { self.base.ecmascript_shared_data.get_unchecked() }+ unsafe { self.shared_data.get_unchecked() }}pub fn is_module_wrapper(&self) -> bool {@@ -826,10 +973,6 @@ impl EcmascriptFunctionObject {self.shared_data().parameter_binding_names()}- pub fn set_is_class_constructor(&self) {- self.shared_data().set_is_class_constructor();- }-pub fn bytecode_executable(&self) -> Option<Gc<Executable>> {self.shared_data().executable()}@@ -850,11 +993,11 @@ impl EcmascriptFunctionObject {}pub fn private_environment(&self) -> Option<Gc<PrivateEnvironment>> {- self.private_environment.get()+ self.with_rare_data()?.private_environment.get()}pub fn script_or_module(&self) -> ScriptOrModule {- self.script_or_module.get()+ self.shared_data().script_or_module.get()}pub fn constructor_kind(&self) -> ConstructorKind {@@ -890,14 +1033,15 @@ impl EcmascriptFunctionObject {}pub fn home_object(&self) -> Option<Gc<Object>> {- self.storage.rare_data.get()?.home_object.get()+ self.with_rare_data()?.home_object.get()}- pub fn set_home_object(&self, home_object: Option<Gc<Object>>) {- if home_object.is_none() && self.storage.rare_data.get().is_none() {- return;- }- self.rare_data().home_object.set(home_object);+ /// Gives a class constructor its home object, which it was created without.+ pub fn set_home_object_of_class_constructor(&self, home_object: Gc<Object>) {+ self.with_rare_data()+ .expect("a class constructor has rare data")+ .home_object+ .set(Some(home_object));}pub fn source_text(&self) -> Utf16String {@@ -936,8 +1080,9 @@ impl EcmascriptFunctionObject {// This is used by LibWeb to disassociate event handler attribute callback functions from the nearest script on the call stack.// https://html.spec.whatwg.org/multipage/webappapis.html#getting-the-current-value-of-the-event-handler Step 3.11+ // NB: This applies to every function with the same shared data, which such a callback function has to itself.pub fn set_script_or_module(&self, script_or_module: ScriptOrModule) {- self.script_or_module.set(script_or_module);+ self.shared_data().script_or_module.set(script_or_module);}pub fn class_field_initializer_name(&self) -> ClassFieldInitializerName {@@ -1069,6 +1214,12 @@ impl EcmascriptFunctionObject {) -> ThrowCompletionOr<Option<PropertyDescriptor>> {let function = as_ecmascript_function(object);+ if function.has_flag(ecmascript_function_flags::HAS_LAZY_LENGTH_AND_NAME)+ && (*property_key == vm.names.length || *property_key == vm.names.name)+ {+ function.reify_length_and_name(vm);+ }+if function.supports_legacy_caller_or_arguments() {let descriptor = object.ordinary_get_own_property(vm, property_key)?;if descriptor.is_some() {@@ -1122,6 +1273,7 @@ impl EcmascriptFunctionObject {fn internal_own_property_keys<'vm>(object: &Object, vm: &'vm Vm) -> ThrowCompletionOr<MarkedVec<'vm, Value>> {let function = as_ecmascript_function(object);+ function.reify_length_and_name(vm);if function.has_flag(ecmascript_function_flags::MAY_NEED_LAZY_PROTOTYPE_INSTANTIATION) {Self::internal_get_own_property(object, vm, &vm.names.prototype)?;}diff --git a/Libraries/LibJS/Rust/src/runtime/function_object.rs b/Libraries/LibJS/Rust/src/runtime/function_object.rsindex 603b7372580..70625bcde48 100644--- a/Libraries/LibJS/Rust/src/runtime/function_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/function_object.rs@@ -4,7 +4,6 @@* SPDX-License-Identifier: BSD-2-Clause*/-use core::cell::Cell;use core::ops::Deref;use ak::Utf16String;@@ -15,7 +14,7 @@ use crate::gc::visitor::{Trace, Visitor};use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;pub use crate::layout::function_object::FunctionObject;-use crate::layout::member::NullableMember;+use crate::layout::function_object::ecmascript_function_flags;use crate::layout::object::Object;use crate::layout::value::Value;use crate::runtime::class_field_definition::ClassElementName;@@ -91,25 +90,19 @@ impl FunctionObject {}fn from_object(object: Object) -> FunctionObject {- FunctionObject {- base: object,- builtin: Cell::new(0),- has_builtin: Cell::new(false),- flags: Cell::new(0),- ecmascript_shared_data: NullableMember::null(),- }+ FunctionObject { base: object }}pub fn builtin(&self) -> Option<Builtin> {- if !self.has_builtin.get() {+ let bits = self.function_bits.get();+ if bits == 0 || bits & ecmascript_function_flags::IS_ECMASCRIPT_FUNCTION != 0 {return None;}- Some(Builtin::from_u8(self.builtin.get()).expect("a function's builtin is a Builtin"))+ Some(Builtin::from_u8(bits - 1).expect("a function's builtin is a Builtin"))}pub fn set_builtin(&self, builtin: Option<Builtin>) {- self.builtin.set(builtin.map_or(0, |builtin| builtin as u8));- self.has_builtin.set(builtin.is_some());+ self.function_bits.set(builtin.map_or(0, |builtin| builtin as u8 + 1));}pub fn is_array_prototype_next_builtin(&self) -> bool {diff --git a/Libraries/LibJS/Rust/src/runtime/intrinsics.rs b/Libraries/LibJS/Rust/src/runtime/intrinsics.rsindex 6be7ace31b9..8dea1b7ed38 100644--- a/Libraries/LibJS/Rust/src/runtime/intrinsics.rs+++ b/Libraries/LibJS/Rust/src/runtime/intrinsics.rs@@ -207,8 +207,8 @@ define_intrinsics! {normal_function_prototype_constructor_offset: Cell<u32>,normal_function_shape: Cell<Option<Gc<Shape>>>,- normal_function_length_offset: Cell<u32>,- normal_function_name_offset: Cell<u32>,+ generator_function_length_offset: Cell<u32>,+ generator_function_name_offset: Cell<u32>,async_function_shape: Cell<Option<Gc<Shape>>>,generator_function_shape: Cell<Option<Gc<Shape>>>,@@ -536,8 +536,8 @@ created_intrinsics! {iterator_result_object_value_offset,iterator_result_object_done_offset,normal_function_prototype_constructor_offset,- normal_function_length_offset,- normal_function_name_offset,+ generator_function_length_offset,+ generator_function_name_offset,generator_function_prototype_property_offset,native_function_length_offset,native_function_name_offset,@@ -1171,12 +1171,6 @@ impl Intrinsics {let normal_function_shape = Shape::create(vm, realm);normal_function_shape.set_prototype_without_transition(vm, function_prototype);- normal_function_shape.add_property_without_transition(vm, &names.length, configurable);- normal_function_shape.add_property_without_transition(vm, &names.name, configurable);- self.normal_function_length_offset- .set(offset_of(normal_function_shape, &names.length));- self.normal_function_name_offset- .set(offset_of(normal_function_shape, &names.name));self.normal_function_shape.set(Some(normal_function_shape));let native_function_shape = Shape::create(vm, realm);@@ -1449,25 +1443,27 @@ impl Intrinsics {configurable,);- // OPTIMIZATION: Like normal functions, the other function kinds start from a premade shape with their own properties- // already in spec order, so creating one only has to store the property values.- let create_function_shape = |prototype: Gc<Object>, has_prototype_property: bool| {+ // NB: Like normal functions, async functions make their properties lazily.+ let async_function_shape = Shape::create(vm, realm);+ async_function_shape.set_prototype_without_transition(vm, self.async_function_prototype(vm));+ self.async_function_shape.set(Some(async_function_shape));++ // OPTIMIZATION: Generator functions start from a premade shape with their own properties already in spec order,+ // so creating one only has to store the property values.+ let create_function_shape = |prototype: Gc<Object>| {let shape = Shape::create(vm, realm);shape.set_prototype_without_transition(vm, prototype);shape.add_property_without_transition(vm, &names.length, configurable);shape.add_property_without_transition(vm, &names.name, configurable);- if has_prototype_property {- shape.add_property_without_transition(vm, &names.prototype, attributes(Attribute::WRITABLE));- }- assert!(offset_of(shape, &names.length) == self.normal_function_length_offset.get());- assert!(offset_of(shape, &names.name) == self.normal_function_name_offset.get());+ shape.add_property_without_transition(vm, &names.prototype, attributes(Attribute::WRITABLE));+ self.generator_function_length_offset+ .set(offset_of(shape, &names.length));+ self.generator_function_name_offset.set(offset_of(shape, &names.name));shape};- self.async_function_shape- .set(Some(create_function_shape(self.async_function_prototype(vm), false)));- let generator_function_shape = create_function_shape(self.generator_function_prototype(vm), true);+ let generator_function_shape = create_function_shape(self.generator_function_prototype(vm));self.generator_function_shape.set(Some(generator_function_shape));- let async_generator_function_shape = create_function_shape(self.async_generator_function_prototype(vm), true);+ let async_generator_function_shape = create_function_shape(self.async_generator_function_prototype(vm));self.async_generator_function_shape.set(Some(async_generator_function_shape));self.generator_function_prototype_property_offsetdiff --git a/Libraries/LibJS/Rust/src/runtime/native_function.rs b/Libraries/LibJS/Rust/src/runtime/native_function.rsindex abf60911916..108a6f74a7d 100644--- a/Libraries/LibJS/Rust/src/runtime/native_function.rs+++ b/Libraries/LibJS/Rust/src/runtime/native_function.rs@@ -17,6 +17,7 @@ use crate::gc::visitor::{Trace, Visitor};use crate::interpreter::vm::Vm;use crate::layout::cell::Gc;use crate::layout::execution_context::ExecutionContext;+use crate::layout::function_object::FUNCTION_LENGTH_AND_NAME_SLOTS;pub use crate::layout::function_object::{DirectGetterFunction, NativeFunction, NativeFunctionTableEntry, NativeFunctionType, RawNativeFunction,};@@ -132,8 +133,10 @@ impl NativeFunction {}fn from_function_object(base: FunctionObject, name: Option<Utf16FlyString>, realm: Gc<Realm>) -> NativeFunction {+ base.inline_named_capacity.set(FUNCTION_LENGTH_AND_NAME_SLOTS as u8);NativeFunction {base,+ inline_named_storage: [const { Cell::new(Value::UNDEFINED) }; FUNCTION_LENGTH_AND_NAME_SLOTS],name: FlyStringSlot::new(name),initial_name: FlyStringSlot::new(None),realm: Member::new(realm),diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rsindex b850b770cb9..60b41e47ef5 100644--- a/Libraries/LibJS/Rust/src/runtime/object.rs+++ b/Libraries/LibJS/Rust/src/runtime/object.rs@@ -15,7 +15,7 @@ use libjs_runtime_macros::Trace;use crate::bytecode::executable::PropertyLookupCache;use crate::bytecode::property_access::{Strict, put_by_property_key};use crate::embedding::abi_types::JSRealm;-use crate::gc::class::{Class, Extends, ExternalMemorySize, GcCell, class_at, define_cell};+use crate::gc::class::{Class, Extends, ExternalMemorySize, GcCell, cast_ref, class_at, define_cell};use crate::gc::defer_gc::DeferGc;use crate::gc::gc_ref_cell::GcRefCell;use crate::gc::heap::{Heap, RuntimeClassAllocator, SizeClassAllocator};@@ -27,9 +27,7 @@ use crate::layout::execution_context::ExecutionContext;use crate::layout::function_object::{EcmascriptFunctionObject, FunctionObject, SharedFunctionInstanceData};use crate::layout::host_class::JSValue;use crate::layout::member::Member;-pub use crate::layout::object::{- INDEXED_ELEMENTS_HEADER_SIZE, INLINE_NAMED_STORAGE_CAPACITY, IndexedStorageKind, Object, object_flag,-};+pub use crate::layout::object::{INDEXED_ELEMENTS_HEADER_SIZE, IndexedStorageKind, Object, object_flag};use crate::layout::value::Value;use crate::layout_forward::RawNativeFunctionPointer;use crate::runtime::abstract_operations::{@@ -473,23 +471,20 @@ fn remove_intrinsic_accessor(vm: &Vm, object: &Object, property_key: &PropertyKe/// Plain objects (exactly an Object, as create() and create_with_premade_shape() make them) come in size classes by how/// many named property values they hold inline: the smallest one that fits the properties of their shape at creation,-/// so that most of them never need named property storage of their own. The first size class is an Object's own cell.+/// so that most of them never need named property storage of their own.pub const PLAIN_OBJECT_INLINE_CAPACITIES: [u8; 6] = [2, 4, 6, 8, 12, 16];/// The inline capacity of plain objects created without properties. More would keep the properties many of them get/// right away (like `this` in constructors) inline, but makes the cells of all of them larger.pub const EMPTY_PLAIN_OBJECT_INLINE_CAPACITY: u8 = PLAIN_OBJECT_INLINE_CAPACITIES[0];-const _: () = assert!(PLAIN_OBJECT_INLINE_CAPACITIES[0] as usize == INLINE_NAMED_STORAGE_CAPACITY);--const PLAIN_OBJECT_SIZE_CLASS_CELL_SIZES: [u32; PLAIN_OBJECT_INLINE_CAPACITIES.len() - 1] = {- let mut cell_sizes = [0; PLAIN_OBJECT_INLINE_CAPACITIES.len() - 1];+const PLAIN_OBJECT_SIZE_CLASS_CELL_SIZES: [u32; PLAIN_OBJECT_INLINE_CAPACITIES.len()] = {+ let mut cell_sizes = [0; PLAIN_OBJECT_INLINE_CAPACITIES.len()];let mut index = 0;while index < cell_sizes.len() {- let inline_capacity = PLAIN_OBJECT_INLINE_CAPACITIES[index + 1] as usize;// NB: The inline named storage ends the object.cell_sizes[index] =- (size_of::<Object>() + (inline_capacity - INLINE_NAMED_STORAGE_CAPACITY) * size_of::<Value>()) as u32;+ (size_of::<Object>() + PLAIN_OBJECT_INLINE_CAPACITIES[index] as usize * size_of::<Value>()) as u32;index += 1;}cell_sizes@@ -509,8 +504,7 @@ pub fn plain_object_size_class(property_count: u32) -> usize {.unwrap_or(PLAIN_OBJECT_INLINE_CAPACITIES.len() - 1)}-/// The allocators of the plain object size classes beyond the first, which is Object's own allocator, for JIT code-/// that allocates plain objects itself.+/// The allocators of the plain object size classes, for JIT code that allocates plain objects itself.pub fn plain_object_size_classes(heap: &Heap) -> &[SizeClassAllocator] {heap.size_classes(Object::CLASS, &PLAIN_OBJECT_SIZE_CLASS_CELL_SIZES)}@@ -578,10 +572,11 @@ impl Object {kind: CellKind::Object,indexed_storage_kind: Cell::new(IndexedStorageKind::None),flags: Cell::new(object_flag::IS_EXTENSIBLE),- inline_named_capacity: Cell::new(INLINE_NAMED_STORAGE_CAPACITY as u8),+ inline_named_capacity: Cell::new(0),+ function_bits: Cell::new(0),named_properties: Cell::new(core::ptr::null_mut()),indexed_elements: Cell::new(core::ptr::null_mut()),- inline_named_storage: [const { Cell::new(Value::UNDEFINED) }; INLINE_NAMED_STORAGE_CAPACITY],+ inline_named_storage: [],};if may_interfere_with_indexed_property_access == MayInterfereWithIndexedPropertyAccess::Yes {object.set_may_interfere_with_indexed_property_access();@@ -649,26 +644,13 @@ impl Object {// 10.1.12 OrdinaryObjectCreate ( proto [ , additionalInternalSlotsList ] ), https://tc39.es/ecma262/#sec-ordinaryobjectcreatepub fn create(vm: &Vm, realm: Gc<Realm>, prototype: Option<Gc<Object>>) -> Gc<Object> {let Some(prototype) = prototype else {- return allocate_object(vm, |_| {- Self::new_with_shape(- Self::CLASS,- realm.empty_object_shape(),- MayInterfereWithIndexedPropertyAccess::No,- )- });+ return Self::create_in_size_class(vm, realm.empty_object_shape(), 0);};if prototype == realm.object_prototype() {- return allocate_object(vm, |_| {- Self::new_with_shape(- Self::CLASS,- realm.new_object_shape(),- MayInterfereWithIndexedPropertyAccess::No,- )- });+ return Self::create_in_size_class(vm, realm.new_object_shape(), 0);}- allocate_object(vm, |_| {- Self::new_with_prototype(vm, Self::CLASS, prototype, MayInterfereWithIndexedPropertyAccess::No)- })+ let object = Self::new_with_prototype(vm, Self::CLASS, prototype, MayInterfereWithIndexedPropertyAccess::No);+ Self::allocate_plain_object(vm, object, 0)}pub fn create_prototype(vm: &Vm, realm: Gc<Realm>, prototype: Option<Gc<Object>>) -> Gc<Object> {@@ -676,20 +658,11 @@ impl Object {if let Some(prototype) = prototype {shape.set_prototype_without_transition(vm, prototype);}- allocate_object(vm, |_| {- Self::new_with_shape(Self::CLASS, shape, MayInterfereWithIndexedPropertyAccess::No)- })+ Self::create_in_size_class(vm, shape, 0)}pub fn create_with_premade_shape(vm: &Vm, shape: Gc<Shape>) -> Gc<Object> {- let property_count = shape.property_count();- // OPTIMIZATION: Objects with up to two properties, the most common ones, are in the first size class.- if property_count <= INLINE_NAMED_STORAGE_CAPACITY as u32 {- return allocate_object(vm, |_| {- Self::new_with_shape(Self::CLASS, shape, MayInterfereWithIndexedPropertyAccess::No)- });- }- Self::create_in_size_class(vm, shape, property_count)+ Self::create_in_size_class(vm, shape, shape.property_count())}/// OrdinaryObjectCreate(prototype) for the `this` of a construction of the function with `shared_data`, with@@ -715,17 +688,17 @@ impl Object {}/// A plain object of the shape, in the size class for that many properties.- #[inline(never)]fn create_in_size_class(vm: &Vm, shape: Gc<Shape>, property_count: u32) -> Gc<Object> {let object = Self::new_with_shape(Self::CLASS, shape, MayInterfereWithIndexedPropertyAccess::No);- let size_class = plain_object_size_class(property_count);- if size_class == 0 {- return allocate_object(vm, |_| object);- }+ Self::allocate_plain_object(vm, object, plain_object_size_class(property_count))+ }++ #[inline(always)]+ fn allocate_plain_object(vm: &Vm, object: Object, size_class: usize) -> Gc<Object> {object.inline_named_capacity.set(PLAIN_OBJECT_INLINE_CAPACITIES[size_class]);- let size_class_allocator = plain_object_size_classes(vm.heap())[size_class - 1];+ let size_class_allocator = plain_object_size_classes(vm.heap())[size_class];set_up_named_storage(vm.heap().allocate_with_size_class(size_class_allocator, |_| object, Value::UNDEFINED),@@ -998,8 +971,8 @@ impl Object {return false;}let methods = self.methods();- // NB: ECMAScript function objects only answer "caller", "arguments" and a lazily created "prototype" themselves,- // and arrays only "length" and array indices.+ // NB: ECMAScript function objects only answer "caller", "arguments" and lazily created "length", "name" and+ // "prototype" properties themselves, and arrays only "length" and array indices.(core::ptr::fn_addr_eq(methods.internal_get_own_property,ORDINARY_OBJECT_METHODS.internal_get_own_property,@@ -1046,13 +1019,16 @@ impl Object {if core::ptr::fn_addr_eq(internal_get_own_property, ordinary_get_own_property) {return true;}- // NB: ECMAScript function objects only answer "caller", "arguments" and a lazily created "prototype" themselves.+ // NB: ECMAScript function objects only answer "caller", "arguments" and lazily created "length", "name" and+ // "prototype" properties themselves.core::ptr::fn_addr_eq(internal_get_own_property,ECMASCRIPT_FUNCTION_OBJECT_METHODS.internal_get_own_property,) && *property_key != vm.names.caller&& *property_key != vm.names.arguments&& *property_key != vm.names.prototype+ && *property_key != vm.names.length+ && *property_key != vm.names.name}// 7.3.13 HasOwnProperty ( O, P ), https://tc39.es/ecma262/#sec-hasownproperty@@ -2832,6 +2808,12 @@ impl Object {return None;}+ // NB: Setting a lazy property of a function must find it.+ if self.is_ecmascript_function_object() && (*property_key == vm.names.length || *property_key == vm.names.name)+ {+ self.reify_lazy_function_properties(vm);+ }+let Some(metadata) = self.shape().lookup(property_key) else {return self.storage_add(vm, property_key, value_and_attributes);};@@ -2873,6 +2855,8 @@ impl Object {if !self.shape().is_dictionary()&& self.shape().property_count() >= MAX_TRANSITIONS_BEFORE_CONVERTING_TO_DICTIONARY{+ // NB: Functions with lazy properties have shapes of their own, which are never dictionaries.+ self.reify_lazy_function_properties(vm);self.shape.set(self.shape().create_dictionary_transition(vm));}@@ -2893,6 +2877,9 @@ impl Object {pub fn storage_delete(&self, vm: &Vm, property_key: &PropertyKey) {assert!(self.storage_has(property_key));+ // NB: Functions with lazy properties have shapes of their own, which no deletion leads to.+ self.reify_lazy_function_properties(vm);+if property_key.is_number() && self.indexed_has(property_key.as_number()) {self.indexed_delete(property_key.as_number());return;@@ -3603,6 +3590,8 @@ impl Object {if self.shape().is_prototype_shape() {return;}+ // NB: Functions with lazy properties have shapes of their own, which no prototype shape is.+ self.reify_lazy_function_properties(vm);self.shape.set(self.shape().clone_for_prototype(vm));}@@ -3665,6 +3654,30 @@ impl Object {.cast_mut()}+ /// Puts the property values in front of the existing ones, as `shape` has them.+ pub(crate) fn prepend_named_properties(&self, shape: Gc<Shape>, values: &[Value]) {+ let existing_count = self.shape().property_count();+ self.ensure_named_storage_capacity(existing_count + values.len() as u32);+ let named_properties = self.named_properties.get();+ // SAFETY: The named storage has room for the existing and the new values.+ unsafe {+ core::ptr::copy(+ named_properties,+ named_properties.add(values.len()),+ existing_count as usize,+ );+ core::ptr::copy_nonoverlapping(values.as_ptr(), named_properties, values.len());+ }+ self.shape.set(shape);+ }++ #[inline(always)]+ fn reify_lazy_function_properties(&self, vm: &Vm) {+ if self.is_ecmascript_function_object() {+ cast_ref::<Object, EcmascriptFunctionObject>(self).reify_length_and_name(vm);+ }+ }+pub(crate) fn named_storage_is_inline(&self) -> bool {core::ptr::eq(self.named_properties.get(), self.inline_named_storage_pointer())}diff --git a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rsindex 1d2ef68d426..12e81cb8132 100644--- a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs@@ -41,7 +41,7 @@ pub struct PromiseResolvingFunction {kind: PromiseResolvingFunctionKind,}-const _: () = assert!(size_of::<PromiseResolvingFunction>() == 96);+const _: () = assert!(size_of::<PromiseResolvingFunction>() == 88);static PROMISE_RESOLVING_FUNCTION_VIRTUAL_METHODS: NativeFunctionMethods = NativeFunctionMethods {call: PromiseResolvingFunction::call,diff --git a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs b/Libraries/LibJS/Rust/src/runtime/proxy_object.rsindex a5ef09b611e..42ade3193db 100644--- a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/proxy_object.rs@@ -85,7 +85,7 @@ pub struct ProxyObject {is_revoked: Cell<bool>,}-const _: () = assert!(size_of::<ProxyObject>() == 72);+const _: () = assert!(size_of::<ProxyObject>() == 48);/// The internal methods of a Proxy exotic object. Every proxy has [[Call]] and [[Construct]]: IsCallable and/// IsConstructor only reach them for a proxy whose target has them, since the proxy keeps the IsFunction flag only ifdiff --git a/Libraries/LibJS/Rust/src/runtime/realm.rs b/Libraries/LibJS/Rust/src/runtime/realm.rsindex a8a706a3088..d7e94a320e9 100644--- a/Libraries/LibJS/Rust/src/runtime/realm.rs+++ b/Libraries/LibJS/Rust/src/runtime/realm.rs@@ -116,8 +116,8 @@ realm_intrinsic_accessors! {iterator_result_object_value_offset,iterator_result_object_done_offset,normal_function_prototype_constructor_offset,- normal_function_length_offset,- normal_function_name_offset,+ generator_function_length_offset,+ generator_function_name_offset,generator_function_prototype_property_offset,native_function_length_offset,native_function_name_offset,diff --git a/Libraries/LibJS/Rust/src/runtime/regexp_object.rs b/Libraries/LibJS/Rust/src/runtime/regexp_object.rsindex 02f05131b49..0c957cece36 100644--- a/Libraries/LibJS/Rust/src/runtime/regexp_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/regexp_object.rs@@ -133,6 +133,9 @@ const PARAGRAPH_SEPARATOR: u16 = 0x2029;#[derive(Trace)]pub struct RegExpObject {base: Object,+ /// OPTIMIZATION: The named property of most RegExp objects is "lastIndex".+ #[gc(untraced = "the object traces its inline named storage")]+ inline_named_storage: [Cell<Value>; 1],#[gc(untraced)]pattern: RefCell<Utf16String>,#[gc(untraced)]@@ -148,7 +151,8 @@ pub struct RegExpObject {cached_regex: RefCell<Option<Rc<EcmaScriptRegex>>>,}-const _: () = assert!(size_of::<RegExpObject>() == 104);+const _: () = assert!(size_of::<RegExpObject>() == 96);+const _: () = assert!(core::mem::offset_of!(RegExpObject, inline_named_storage) == size_of::<Object>());static REGEXP_OBJECT_METHODS: ObjectMethods = ObjectMethods {initialize: RegExpObject::initialize,@@ -197,7 +201,12 @@ impl RegExpObject {let shape = realm.intrinsics().new_regexp_object_shape(vm);let flag_bits = to_flag_bits(Utf16View::of_string(&flags));let regexp_object = allocate_object(vm, |_| RegExpObject {- base: Object::new_with_shape(Self::CLASS, shape, MayInterfereWithIndexedPropertyAccess::No),+ base: Self::with_inline_named_storage(Object::new_with_shape(+ Self::CLASS,+ shape,+ MayInterfereWithIndexedPropertyAccess::No,+ )),+ inline_named_storage: [const { Cell::new(Value::UNDEFINED) }; 1],pattern: RefCell::new(pattern),flags: RefCell::new(flags),flag_bits: Cell::new(flag_bits),@@ -209,10 +218,21 @@ impl RegExpObject {regexp_object}+ fn with_inline_named_storage(base: Object) -> Object {+ base.inline_named_capacity.set(1);+ base+ }+fn new(vm: &Vm, pattern: Utf16String, flags: Utf16String, prototype: Gc<Object>) -> RegExpObject {let flag_bits = to_flag_bits(Utf16View::of_string(&flags));RegExpObject {- base: Object::new_with_prototype(vm, Self::CLASS, prototype, MayInterfereWithIndexedPropertyAccess::No),+ base: Self::with_inline_named_storage(Object::new_with_prototype(+ vm,+ Self::CLASS,+ prototype,+ MayInterfereWithIndexedPropertyAccess::No,+ )),+ inline_named_storage: [const { Cell::new(Value::UNDEFINED) }; 1],pattern: RefCell::new(pattern),flags: RefCell::new(flags),flag_bits: Cell::new(flag_bits),diff --git a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rsindex a3368686ba1..7301f297436 100644--- a/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs+++ b/Libraries/LibJS/Rust/src/runtime/shared_function_instance_data.rs@@ -26,6 +26,7 @@ use crate::gc::visitor::{Trace, Visitor};use crate::interpreter::run::should_dump_bytecode;use crate::interpreter::vm::Vm;use crate::layout::cell::{CellHeader, Gc};+use crate::layout::execution_context::ScriptOrModule;use crate::layout::function_object::CallEnvironmentTemplate;pub use crate::layout::function_object::{SharedFunctionInstanceData, asm_call_metadata};use crate::layout::member::NullableMember;@@ -143,7 +144,7 @@ pub struct SharedFunctionInstanceDataStorage {}// NB: Every function has these bytes.-const _: () = assert!(size_of::<SharedFunctionInstanceData>() == 264);+const _: () = assert!(size_of::<SharedFunctionInstanceData>() == 280);/// The most properties that constructions make room for ahead (the largest inline capacity of plain objects).const MAX_CONSTRUCT_RESERVE: u32 = 16;@@ -158,6 +159,11 @@ unsafe impl Trace for SharedFunctionInstanceData {fn trace(&self, visitor: &mut Visitor) {self.executable.trace(visitor);self.call_environment_template.trace(visitor);+ match self.script_or_module.get() {+ ScriptOrModule::Empty => {}+ ScriptOrModule::Script(script) => script.trace(visitor),+ ScriptOrModule::Module(module) => module.trace(visitor),+ }self.storage.trace(visitor);}}@@ -231,6 +237,7 @@ impl SharedFunctionInstanceData {uses_this: Cell::new(false),can_inline_call: Cell::new(false),call_environment_template: NullableMember::null(),+ script_or_module: Cell::new(ScriptOrModule::Empty),storage: SharedFunctionInstanceDataStorage {name,source_code: RefCell::new(None),diff --git a/Libraries/LibJS/Rust/src/runtime/value.rs b/Libraries/LibJS/Rust/src/runtime/value.rsindex 89a9157aefe..5646d359ff7 100644--- a/Libraries/LibJS/Rust/src/runtime/value.rs+++ b/Libraries/LibJS/Rust/src/runtime/value.rs@@ -1766,7 +1766,7 @@ pub fn r#in(vm: &Vm, lhs: Value, rhs: Value) -> ThrowCompletionOr<Value> {/// Whether a function is the native function the realm installs as %Function.prototype%[@@hasInstance]. Only/// NativeFunction gives a function a builtin, so the builtin alone identifies it.fn is_ordinary_has_instance_builtin(function: Gc<FunctionObject>) -> bool {- function.has_builtin.get() && Builtin::from_u8(function.builtin.get()) == Some(Builtin::OrdinaryHasInstance)+ function.builtin() == Some(Builtin::OrdinaryHasInstance)}/// BoundFunction::bound_target_function() of a function that is a bound function exotic object.diff --git a/Tests/LibJS/Runtime/functions/function-lazy-length-and-name.js b/Tests/LibJS/Runtime/functions/function-lazy-length-and-name.jsnew file mode 100644index 00000000000..d91100d43bc--- /dev/null+++ b/Tests/LibJS/Runtime/functions/function-lazy-length-and-name.js@@ -0,0 +1,295 @@+const namesOf = f => Object.getOwnPropertyNames(f).filter(name => name !== "arguments" && name !== "caller");++describe("own properties of functions that never had them looked at", () => {+ test("length and name come first, whatever is added first", () => {+ function a(x, y) {}+ a.foo = 1;+ expect(namesOf(a).slice(0, 2)).toEqual(["length", "name"]);+ expect(a.length).toBe(2);+ expect(a.name).toBe("a");+ expect(namesOf(a)).toContain("foo");++ const b = () => {};+ Object.defineProperty(b, "bar", { value: 1 });+ expect(namesOf(b)).toEqual(["length", "name", "bar"]);++ const c = async x => {};+ c[Symbol.iterator] = 1;+ c.baz = 1;+ expect(namesOf(c)).toEqual(["length", "name", "baz"]);+ expect(c.length).toBe(1);+ });++ test("descriptors", () => {+ const f = function (x) {};+ expect(Object.getOwnPropertyDescriptor(f, "length")).toEqual({+ value: 1,+ writable: false,+ enumerable: false,+ configurable: true,+ });+ const g = function named() {};+ expect(Object.getOwnPropertyDescriptor(g, "name")).toEqual({+ value: "named",+ writable: false,+ enumerable: false,+ configurable: true,+ });+ });++ test("hasOwn and in", () => {+ expect(Object.hasOwn(() => {}, "length")).toBeTrue();+ expect(Object.hasOwn(() => {}, "name")).toBeTrue();+ expect((() => {}).hasOwnProperty("length")).toBeTrue();+ expect("name" in function () {}).toBeTrue();+ });++ test("deleting them reveals the ones of the prototype", () => {+ const f = function named(x) {};+ expect(delete f.name).toBeTrue();+ expect(Object.hasOwn(f, "name")).toBeFalse();+ expect(f.name).toBe("");+ expect(f.length).toBe(1);+ expect(delete f.length).toBeTrue();+ expect(f.length).toBe(0);+ });++ test("they are not writable", () => {+ const f = function named(x) {};+ f.length = 10;+ f.name = "other";+ expect(f.length).toBe(1);+ expect(f.name).toBe("named");++ const g = function named(x) {};+ expect(() => {+ "use strict";+ g.name = "other";+ }).toThrow(TypeError);+ expect(() => Object.assign(function named() {}, { name: "other" })).toThrow(TypeError);+ });++ test("they can be redefined", () => {+ const f = function named() {};+ Object.defineProperty(f, "name", { value: "other" });+ expect(f.name).toBe("other");+ expect(namesOf(f).slice(0, 2)).toEqual(["length", "name"]);+ });++ test("objects inheriting from a function see them", () => {+ const f = function named(x) {};+ const o = Object.create(f);+ expect(o.name).toBe("named");+ expect(o.length).toBe(1);++ // A non-writable inherited property keeps assignments from adding one.+ const g = function other() {};+ const p = Object.create(g);+ p.name = "mine";+ expect(Object.hasOwn(p, "name")).toBeFalse();+ expect(p.name).toBe("other");+ });++ test("they shadow enumerable properties of the prototype chain", () => {+ Object.prototype.name = 1;+ Function.prototype.length2 = 1;+ try {+ const keys = [];+ for (const key in function () {}) keys.push(key);+ expect(keys).not.toContain("name");+ expect(keys).toContain("length2");+ } finally {+ delete Object.prototype.name;+ delete Function.prototype.length2;+ }+ });++ test("freezing, sealing and preventing extensions", () => {+ const frozen = Object.freeze(function named(x) {});+ expect(Object.isFrozen(frozen)).toBeTrue();+ expect(frozen.name).toBe("named");+ expect(Object.getOwnPropertyDescriptor(frozen, "length").configurable).toBeFalse();++ const sealed = Object.seal(() => {});+ expect(Object.isSealed(sealed)).toBeTrue();+ expect(sealed.length).toBe(0);++ const fixed = Object.preventExtensions(function named(x) {});+ expect(fixed.length).toBe(1);+ expect(fixed.name).toBe("named");+ expect(Object.isExtensible(fixed)).toBeFalse();+ });++ test("changing the prototype first", () => {+ const f = function named(x) {};+ Object.setPrototypeOf(f, null);+ expect(f.length).toBe(1);+ expect(f.name).toBe("named");+ expect(Object.getOwnPropertyNames(f).slice(0, 2)).toEqual(["length", "name"]);+ });++ test("a function that is a prototype, before and after", () => {+ const f = function named(x) {};+ const o = Object.create(f);+ f.foo = 1;+ expect(o.foo).toBe(1);+ expect(o.length).toBe(1);+ expect(namesOf(f).slice(0, 2)).toEqual(["length", "name"]);+ });++ test("bind", () => {+ const f = function named(x, y) {};+ const bound = f.bind(null, 1);+ expect(bound.length).toBe(1);+ expect(bound.name).toBe("bound named");+ });++ test("classes", () => {+ class A {+ static x = 1;+ static m() {}+ }+ expect(A.name).toBe("A");+ expect(A.length).toBe(0);+ expect(namesOf(A).slice(0, 3)).toEqual(["length", "name", "prototype"]);+ expect(A.m.name).toBe("m");+ expect(namesOf(A.m)).toEqual(["length", "name"]);++ class B extends A {+ constructor(a, b) {+ super();+ }+ }+ expect(B.length).toBe(2);+ expect(B.name).toBe("B");+ });++ test("names set by the context", () => {+ const a = () => {};+ const o = { b: () => {}, ["c" + 1]: function () {}, get d() {} };+ expect(a.name).toBe("a");+ expect(o.b.name).toBe("b");+ expect(o.c1.name).toBe("c1");+ expect(Object.getOwnPropertyDescriptor(o, "d").get.name).toBe("get d");+ const k = Symbol("s");+ const p = { [k]: () => {} };+ expect(p[k].name).toBe("[s]");+ });++ test("properties added before the first look at them move behind them", () => {+ const f = function named(x) {};+ f.a = 1;+ f.b = 2;+ Object.defineProperty(f, "c", { value: 3, enumerable: false });+ expect(f.a).toBe(1);+ expect(f.name).toBe("named");+ expect(f.a).toBe(1);+ expect(f.b).toBe(2);+ expect(f.c).toBe(3);+ expect(f.length).toBe(1);+ expect(namesOf(f).filter(name => name !== "prototype")).toEqual(["length", "name", "a", "b", "c"]);+ expect(Object.getOwnPropertyDescriptor(f, "c").enumerable).toBeFalse();+ expect(Object.keys(f)).toEqual(["a", "b"]);+ });++ test("deleting an added property", () => {+ const f = () => {};+ f.a = 1;+ f.b = 2;+ expect(delete f.a).toBeTrue();+ expect(namesOf(f)).toEqual(["length", "name", "b"]);+ expect(f.b).toBe(2);+ });++ test("many added properties", () => {+ const f = () => {};+ for (let i = 0; i < 100; ++i) f["p" + i] = i;+ const names = namesOf(f);+ expect(names.length).toBe(102);+ expect(names.slice(0, 3)).toEqual(["length", "name", "p0"]);+ for (let i = 0; i < 100; ++i) expect(f["p" + i]).toBe(i);+ });++ test("accessors and changed attributes of added properties survive", () => {+ const f = () => {};+ let got = 0;+ Object.defineProperty(f, "a", {+ get() {+ return ++got;+ },+ configurable: true,+ });+ f.b = 1;+ Object.defineProperty(f, "b", { writable: false });+ expect(f.name).toBe("f");+ expect(f.a).toBe(1);+ expect(Object.getOwnPropertyDescriptor(f, "b").writable).toBeFalse();+ });++ test("objects inheriting from a function with added properties", () => {+ const f = function named(x) {};+ f.a = 1;+ const o = Object.create(f);+ expect(o.a).toBe(1);+ expect(o.name).toBe("named");+ expect(namesOf(f).slice(0, 3)).toEqual(["length", "name", "a"]);+ });++ test("the prototype property does not get ahead of them", () => {+ function F(a, b) {}+ F.prototype.x = 1;+ expect(new F().x).toBe(1);+ expect(F.length).toBe(2);+ expect(namesOf(F)).toEqual(["length", "name", "prototype"]);+ });++ test("cached additions keep functions apart", () => {+ const addTo = f => {+ f.a = 1;+ f.b = 2;+ return f;+ };+ for (let i = 0; i < 3000; ++i) {+ const f = addTo(x => x);+ if (i % 3 === 0) expect(f.length).toBe(1);+ if (i % 3 === 1) expect(namesOf(f)).toEqual(["length", "name", "a", "b"]);+ expect(f.a + f.b).toBe(3);+ if (i % 7 === 0) {+ expect(f.name).toBe("");+ expect(f.a + f.b).toBe(3);+ }+ }+ });++ test("inline caches do not mix functions up", () => {+ const lengthOf = f => f.length;+ const nameOf = f => f.name;+ const addTo = f => {+ f.foo = 1;+ return f;+ };+ for (let i = 0; i < 2000; ++i) {+ const f = function named(x, y) {};+ expect(lengthOf(f)).toBe(2);+ expect(nameOf(f)).toBe("named");+ const g = addTo(() => {});+ expect(namesOf(g)).toEqual(["length", "name", "foo"]);+ expect(g.foo).toBe(1);+ expect(lengthOf(addTo(x => x))).toBe(1);+ }+ });++ test("a closure copied in a loop", () => {+ const make = () => x => x;+ let last;+ for (let i = 0; i < 20000; ++i) {+ last = make();+ if (i % 1000 === 0) {+ expect(last.length).toBe(1);+ expect(namesOf(last)).toEqual(["length", "name"]);+ }+ }+ expect(last.length).toBe(1);+ expect(last.name).toBe("");+ });+});