Skip to content

Truly independent web browser

LibJS: Read the variable environment of a frame from its function

Andreas Klinge702d86369b5
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index e76d21ffbbe..6e9d9347446 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -14,7 +14,7 @@
CSYM(js_interpreter_dispatch_table):
asm_dispatch_table:
.quad asm_handler_Enter
- .quad asm_handler_EnterWithPrivateEnvironment
+ .quad asm_handler_EnterWithOwnEnvironments
.quad asm_handler_Mov
.quad asm_handler_MovWide
.quad asm_handler_WideNameIndices
@@ -537,10 +537,10 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_Enter_hot_end
.quad asm_handler_Enter_cold
.quad asm_handler_Enter_cold_end
- .quad asm_handler_EnterWithPrivateEnvironment
- .quad asm_handler_EnterWithPrivateEnvironment_hot_end
- .quad asm_handler_EnterWithPrivateEnvironment_cold
- .quad asm_handler_EnterWithPrivateEnvironment_cold_end
+ .quad asm_handler_EnterWithOwnEnvironments
+ .quad asm_handler_EnterWithOwnEnvironments_hot_end
+ .quad asm_handler_EnterWithOwnEnvironments_cold
+ .quad asm_handler_EnterWithOwnEnvironments_cold_end
.quad asm_handler_Mov
.quad asm_handler_Mov_hot_end
.quad asm_handler_Mov_cold
@@ -1699,63 +1699,66 @@ asm_handler_Enter:
asm_handler_Enter_hot_end:
.p2align 4
-asm_handler_EnterWithPrivateEnvironment:
+asm_handler_EnterWithOwnEnvironments:
ldr x0, [x28]
- cbz x0, .Lasm_EnterWithPrivateEnvironment.ssa_block_2
+ cbz x0, .Lasm_EnterWithOwnEnvironments.ssa_block_2
str xzr, [x28, #48]
ldrb w9, [x0, #15]
mov w10, #8
tst x9, x10
- b.eq .Lasm_EnterWithPrivateEnvironment.ssa_block_2
+ b.eq .Lasm_EnterWithOwnEnvironments.ssa_block_4
ldr w0, [x0, #76]
add x9, x24, w0, uxtw #3
cmp w0, #0
csel x0, x9, xzr, ne
str x0, [x28, #48]
-.Lasm_EnterWithPrivateEnvironment.ssa_block_2:
+.Lasm_EnterWithOwnEnvironments.ssa_block_4:
+ ldr x0, [x28, #32]
+ str x0, [x28, #40]
+.Lasm_EnterWithOwnEnvironments.ssa_block_2:
ldr x0, [x28, #88]
- cbz x0, .Lasm_EnterWithPrivateEnvironment.assert_predicate_failure_12
+ cbz x0, .Lasm_EnterWithOwnEnvironments.assert_predicate_failure_12
ldrb w9, [x0, #109]
- cbz w9, .Lasm_EnterWithPrivateEnvironment.ssa_block_6
+ cbz w9, .Lasm_EnterWithOwnEnvironments.ssa_block_6
strb wzr, [x0, #109]
-.Lasm_EnterWithPrivateEnvironment.ssa_block_6:
+.Lasm_EnterWithOwnEnvironments.ssa_block_6:
ldr w2, [x0, #12]
movz x3, #0x7ffb, lsl #48
mov w1, #5
-.Lasm_EnterWithPrivateEnvironment.ssa_block_9:
+.Lasm_EnterWithOwnEnvironments.ssa_block_9:
add x4, x1, #1
cmp x4, x2
- b.hs .Lasm_EnterWithPrivateEnvironment.ssa_block_8
+ b.hs .Lasm_EnterWithOwnEnvironments.ssa_block_8
add x10, x27, x1, lsl #3
stp x3, x3, [x10]
add x1, x1, #2
- b .Lasm_EnterWithPrivateEnvironment.ssa_block_9
-.Lasm_EnterWithPrivateEnvironment.ssa_block_8:
+ b .Lasm_EnterWithOwnEnvironments.ssa_block_9
+.Lasm_EnterWithOwnEnvironments.ssa_block_8:
cmp x1, x2
- b.hs .Lasm_EnterWithPrivateEnvironment.ssa_block_11
+ b.hs .Lasm_EnterWithOwnEnvironments.ssa_block_11
str x3, [x27, x1, lsl #3]
-.Lasm_EnterWithPrivateEnvironment.ssa_block_11:
+.Lasm_EnterWithOwnEnvironments.ssa_block_11:
ldr w3, [x0, #16]
ldr x1, [x0, #32]
subs x3, x3, x2
mov x0, #0
-.Lasm_EnterWithPrivateEnvironment.ssa_block_14:
+.Lasm_EnterWithOwnEnvironments.ssa_block_14:
cmp x0, x3
- b.hs .Lasm_EnterWithPrivateEnvironment.ssa_block_13
- cbz x1, .Lasm_EnterWithPrivateEnvironment.assert_predicate_failure_42
+ b.hs .Lasm_EnterWithOwnEnvironments.ssa_block_13
+ cbz x1, .Lasm_EnterWithOwnEnvironments.assert_predicate_failure_46
ldr x4, [x1, x0, lsl #3]
str x4, [x27, x2, lsl #3]
add x0, x0, #1
add x2, x2, #1
- b .Lasm_EnterWithPrivateEnvironment.ssa_block_14
-.Lasm_EnterWithPrivateEnvironment.ssa_block_13:
+ b .Lasm_EnterWithOwnEnvironments.ssa_block_14
+.Lasm_EnterWithOwnEnvironments.ssa_block_13:
mov w9, #1
strb w9, [x28, #79]
ldrb w9, [x21, #1]
add x21, x21, #1
ldr x10, [x19, x9, lsl #3]
br x10
-asm_handler_EnterWithPrivateEnvironment_hot_end:
+asm_handler_EnterWithOwnEnvironments_hot_end:
.p2align 4
asm_handler_Mov:
@@ -5482,7 +5485,7 @@ asm_handler_GetById:
ldr x2, [x2, x4, lsl #3]
cbz x2, .Lasm_GetById.ssa_block_1
and x2, x2, #0xfffffffffffffffc
- cbz x2, .Lasm_GetById.assert_predicate_failure_334
+ cbz x2, .Lasm_GetById.assert_predicate_failure_328
ldr x9, [x2, #24]
cmp x9, x0
b.ne .Lasm_GetById.ssa_block_15
@@ -6313,10 +6316,10 @@ asm_handler_Call:
b.ls .Lasm_Call.ssa_block_23
mov x3, x4
.Lasm_Call.ssa_block_23:
- cbz x2, .Lasm_Call.assert_predicate_failure_254
+ cbz x2, .Lasm_Call.assert_predicate_failure_248
ldp w0, w4, [x2, #12]
cmp x4, x0
- b.lo .Lasm_Call.assert_predicate_failure_260
+ b.lo .Lasm_Call.assert_predicate_failure_254
movn w0, #0
subs x0, x0, x4
cmp x0, x3
@@ -6331,7 +6334,7 @@ asm_handler_Call:
add x5, x5, #136
adds x5, x5, x0
cmp x5, x0
- b.lo .Lasm_Call.assert_predicate_failure_274
+ b.lo .Lasm_Call.assert_predicate_failure_268
cmp x5, x8
b.hi .Lasm_Call.ssa_block_1
mov x8, x20
@@ -6344,8 +6347,8 @@ asm_handler_Call:
add x9, x24, w1, uxtw #3
cmp w1, #0
csel x1, x9, xzr, ne
- cbz x1, .Lasm_Call.assert_predicate_failure_295
- stp x1, x1, [x0, #32]
+ cbz x1, .Lasm_Call.assert_predicate_failure_289
+ str x1, [x0, #32]
stp x6, x2, [x0, #80]
movz x1, #0x7ffb, lsl #48
stp x1, x1, [x0, #136]
@@ -6371,7 +6374,7 @@ asm_handler_Call:
stp w1, w5, [x0, #108]
ldrb w2, [x21, #6]
cmp x3, x2
- b.lo .Lasm_Call.assert_predicate_failure_353
+ b.lo .Lasm_Call.assert_predicate_failure_345
add x6, x21, #7
add x5, x0, #136
add x7, x28, #136
@@ -6397,7 +6400,7 @@ asm_handler_Call:
b .Lasm_Call.ssa_block_31
.Lasm_Call.ssa_block_30:
ldr x1, [x0, #88]
- cbz x1, .Lasm_Call.assert_predicate_failure_375
+ cbz x1, .Lasm_Call.assert_predicate_failure_367
ldr x26, [x1, #40]
cbz x26, .Lasm_Call.assert_failure_6
mov x1, x20
@@ -6533,10 +6536,9 @@ asm_handler_Call:
stp w2, w2, [x0, #116]
str w2, [x0, #104]
str x1, [x0]
- ldp x1, x3, [x28, #32]
- cbz x1, .Lasm_Call.assert_predicate_failure_127
- cbz x3, .Lasm_Call.assert_predicate_failure_129
- stp x1, x3, [x0, #32]
+ ldr x1, [x28, #32]
+ cbz x1, .Lasm_Call.assert_predicate_failure_125
+ str x1, [x0, #32]
ldrb w1, [x21, #5]
ldr x1, [x27, x1, lsl #3]
mov w3, #0
@@ -6583,7 +6585,7 @@ asm_handler_Call:
adds x4, x4, x0
ldr w0, [x4, #8]
cmp x0, #1
- b.hs .Lasm_Call.assert_predicate_failure_200
+ b.hs .Lasm_Call.assert_predicate_failure_194
ldr x2, [x4]
cbz x2, .Lasm_Call.assert_failure_25
mov x9, x2
@@ -6591,10 +6593,10 @@ asm_handler_Call:
blr x9
and x1, x1, #0xff
cmp x1, #2
- b.hs .Lasm_Call.assert_predicate_failure_207
+ b.hs .Lasm_Call.assert_predicate_failure_201
cbnz x1, .Lasm_Call.ssa_block_6
ldr x1, [x28, #96]
- cbz x1, .Lasm_Call.assert_predicate_failure_213
+ cbz x1, .Lasm_Call.assert_predicate_failure_207
mov x2, x20
str x1, [x2]
str x28, [x2, #16]
@@ -8618,8 +8620,8 @@ asm_handler_JumpLooselyInequalsLoopRhsInt32_hot_end:
asm_cold_handler_paths:
asm_handler_Enter_cold:
asm_handler_Enter_cold_end:
-asm_handler_EnterWithPrivateEnvironment_cold:
-asm_handler_EnterWithPrivateEnvironment_cold_end:
+asm_handler_EnterWithOwnEnvironments_cold:
+asm_handler_EnterWithOwnEnvironments_cold_end:
asm_handler_Mov_cold:
asm_handler_Mov_cold_end:
asm_handler_MovWide_cold:
@@ -14555,10 +14557,9 @@ asm_handler_GetById_cold:
stp w4, w4, [x0, #116]
str wzr, [x0, #104]
str x1, [x0]
- ldp x1, x4, [x28, #32]
- cbz x1, .Lasm_GetById.assert_predicate_failure_206
- cbz x4, .Lasm_GetById.assert_predicate_failure_208
- stp x1, x4, [x0, #32]
+ ldr x1, [x28, #32]
+ cbz x1, .Lasm_GetById.assert_predicate_failure_204
+ str x1, [x0, #32]
and x3, x3, #0x7ffffffff
movz x1, #0xfff9, lsl #48
orr x3, x3, x1
@@ -14590,10 +14591,10 @@ asm_handler_GetById_cold:
blr x9
and x1, x1, #0xff
cmp x1, #2
- b.hs .Lasm_GetById.assert_predicate_failure_264
+ b.hs .Lasm_GetById.assert_predicate_failure_258
cbnz x1, .Lasm_GetById.ssa_block_6
ldr x1, [x28, #96]
- cbz x1, .Lasm_GetById.assert_predicate_failure_273
+ cbz x1, .Lasm_GetById.assert_predicate_failure_267
mov x2, x20
str x1, [x2]
str x28, [x2, #16]
@@ -21293,9 +21294,9 @@ asm_assertion_failure_traps:
brk #0
.Lasm_Enter.assert_predicate_failure_29:
brk #0
-.Lasm_EnterWithPrivateEnvironment.assert_predicate_failure_12:
+.Lasm_EnterWithOwnEnvironments.assert_predicate_failure_12:
brk #0
-.Lasm_EnterWithPrivateEnvironment.assert_predicate_failure_42:
+.Lasm_EnterWithOwnEnvironments.assert_predicate_failure_46:
brk #0
.Lasm_JumpIf.assert_predicate_failure_7:
brk #0
@@ -21467,7 +21468,7 @@ asm_assertion_failure_traps:
brk #0
.Lasm_GetById.assert_failure_1:
brk #0
-.Lasm_GetById.assert_predicate_failure_334:
+.Lasm_GetById.assert_predicate_failure_328:
brk #0
.Lasm_GetById.assert_failure_2:
brk #0
@@ -21491,15 +21492,13 @@ asm_assertion_failure_traps:
brk #0
.Lasm_GetById.assert_predicate_failure_187:
brk #0
-.Lasm_GetById.assert_predicate_failure_206:
- brk #0
-.Lasm_GetById.assert_predicate_failure_208:
+.Lasm_GetById.assert_predicate_failure_204:
brk #0
.Lasm_GetById.assert_failure_10:
brk #0
-.Lasm_GetById.assert_predicate_failure_264:
+.Lasm_GetById.assert_predicate_failure_258:
brk #0
-.Lasm_GetById.assert_predicate_failure_273:
+.Lasm_GetById.assert_predicate_failure_267:
brk #0
.Lasm_GetById.assert_predicate_failure_26:
brk #0
@@ -21613,21 +21612,21 @@ asm_assertion_failure_traps:
brk #0
.Lasm_Call.assert_failure_3:
brk #0
-.Lasm_Call.assert_predicate_failure_254:
+.Lasm_Call.assert_predicate_failure_248:
brk #0
-.Lasm_Call.assert_predicate_failure_260:
+.Lasm_Call.assert_predicate_failure_254:
brk #0
.Lasm_Call.assert_failure_4:
brk #0
.Lasm_Call.assert_failure_5:
brk #0
-.Lasm_Call.assert_predicate_failure_274:
+.Lasm_Call.assert_predicate_failure_268:
brk #0
-.Lasm_Call.assert_predicate_failure_295:
+.Lasm_Call.assert_predicate_failure_289:
brk #0
-.Lasm_Call.assert_predicate_failure_353:
+.Lasm_Call.assert_predicate_failure_345:
brk #0
-.Lasm_Call.assert_predicate_failure_375:
+.Lasm_Call.assert_predicate_failure_367:
brk #0
.Lasm_Call.assert_failure_6:
brk #0
@@ -21649,19 +21648,17 @@ asm_assertion_failure_traps:
brk #0
.Lasm_Call.assert_predicate_failure_109:
brk #0
-.Lasm_Call.assert_predicate_failure_127:
- brk #0
-.Lasm_Call.assert_predicate_failure_129:
+.Lasm_Call.assert_predicate_failure_125:
brk #0
.Lasm_Call.assert_failure_24:
brk #0
-.Lasm_Call.assert_predicate_failure_200:
+.Lasm_Call.assert_predicate_failure_194:
brk #0
.Lasm_Call.assert_failure_25:
brk #0
-.Lasm_Call.assert_predicate_failure_207:
+.Lasm_Call.assert_predicate_failure_201:
brk #0
-.Lasm_Call.assert_predicate_failure_213:
+.Lasm_Call.assert_predicate_failure_207:
brk #0
.Lasm_Call.assert_predicate_failure_91:
brk #0
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
index 81b33b0f8e4..63d07065875 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -15,7 +15,7 @@
CSYM(js_interpreter_dispatch_table):
asm_dispatch_table:
.quad asm_handler_Enter
- .quad asm_handler_EnterWithPrivateEnvironment
+ .quad asm_handler_EnterWithOwnEnvironments
.quad asm_handler_Mov
.quad asm_handler_MovWide
.quad asm_handler_WideNameIndices
@@ -538,10 +538,10 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_Enter_hot_end
.quad asm_handler_Enter_cold
.quad asm_handler_Enter_cold_end
- .quad asm_handler_EnterWithPrivateEnvironment
- .quad asm_handler_EnterWithPrivateEnvironment_hot_end
- .quad asm_handler_EnterWithPrivateEnvironment_cold
- .quad asm_handler_EnterWithPrivateEnvironment_cold_end
+ .quad asm_handler_EnterWithOwnEnvironments
+ .quad asm_handler_EnterWithOwnEnvironments_hot_end
+ .quad asm_handler_EnterWithOwnEnvironments_cold
+ .quad asm_handler_EnterWithOwnEnvironments_cold_end
.quad asm_handler_Mov
.quad asm_handler_Mov_hot_end
.quad asm_handler_Mov_cold
@@ -1679,62 +1679,65 @@ asm_handler_Enter:
asm_handler_Enter_hot_end:
.p2align 6
-asm_handler_EnterWithPrivateEnvironment:
+asm_handler_EnterWithOwnEnvironments:
mov rax, QWORD PTR [rbx - 136]
test rax, rax
- jz .Lasm_EnterWithPrivateEnvironment.ssa_block_2
+ jz .Lasm_EnterWithOwnEnvironments.ssa_block_2
mov QWORD PTR [rbx - 88], 0
test BYTE PTR [rax + 15], 8
- jz .Lasm_EnterWithPrivateEnvironment.ssa_block_2
+ jz .Lasm_EnterWithOwnEnvironments.ssa_block_4
mov eax, DWORD PTR [rax + 76]
lea r11, [r15 + rax * 8]
test eax, eax
cmovnz rax, r11
mov QWORD PTR [rbx - 88], rax
-.Lasm_EnterWithPrivateEnvironment.ssa_block_2:
+.Lasm_EnterWithOwnEnvironments.ssa_block_4:
+ mov rax, QWORD PTR [rbx - 104]
+ mov QWORD PTR [rbx - 96], rax
+.Lasm_EnterWithOwnEnvironments.ssa_block_2:
mov rax, QWORD PTR [rbx - 48]
test rax, rax
- jz .Lasm_EnterWithPrivateEnvironment.assert_predicate_failure_12
+ jz .Lasm_EnterWithOwnEnvironments.assert_predicate_failure_12
cmp BYTE PTR [rax + 109], 0
- jz .Lasm_EnterWithPrivateEnvironment.ssa_block_6
+ jz .Lasm_EnterWithOwnEnvironments.ssa_block_6
mov BYTE PTR [rax + 109], 0
-.Lasm_EnterWithPrivateEnvironment.ssa_block_6:
+.Lasm_EnterWithOwnEnvironments.ssa_block_6:
mov edx, DWORD PTR [rax + 12]
movabs rsi, 9221964661971222528
mov rcx, 5
-.Lasm_EnterWithPrivateEnvironment.ssa_block_9:
+.Lasm_EnterWithOwnEnvironments.ssa_block_9:
lea rdi, [rcx + 1]
cmp rdi, rdx
- jae .Lasm_EnterWithPrivateEnvironment.ssa_block_8
+ jae .Lasm_EnterWithOwnEnvironments.ssa_block_8
mov QWORD PTR [rbx + rcx * 8], rsi
mov QWORD PTR [rbx + rcx * 8 + 8], rsi
lea rcx, [rcx + 2]
- jmp .Lasm_EnterWithPrivateEnvironment.ssa_block_9
-.Lasm_EnterWithPrivateEnvironment.ssa_block_8:
+ jmp .Lasm_EnterWithOwnEnvironments.ssa_block_9
+.Lasm_EnterWithOwnEnvironments.ssa_block_8:
cmp rcx, rdx
- jae .Lasm_EnterWithPrivateEnvironment.ssa_block_11
+ jae .Lasm_EnterWithOwnEnvironments.ssa_block_11
mov QWORD PTR [rbx + rcx * 8], rsi
-.Lasm_EnterWithPrivateEnvironment.ssa_block_11:
+.Lasm_EnterWithOwnEnvironments.ssa_block_11:
mov esi, DWORD PTR [rax + 16]
mov rcx, QWORD PTR [rax + 32]
sub rsi, rdx
xor rax, rax
-.Lasm_EnterWithPrivateEnvironment.ssa_block_14:
+.Lasm_EnterWithOwnEnvironments.ssa_block_14:
cmp rax, rsi
- jae .Lasm_EnterWithPrivateEnvironment.ssa_block_13
+ jae .Lasm_EnterWithOwnEnvironments.ssa_block_13
test rcx, rcx
- jz .Lasm_EnterWithPrivateEnvironment.assert_predicate_failure_42
+ jz .Lasm_EnterWithOwnEnvironments.assert_predicate_failure_46
mov rdi, QWORD PTR [rcx + rax * 8]
mov QWORD PTR [rbx + rdx * 8], rdi
lea rax, [rax + 1]
lea rdx, [rdx + 1]
- jmp .Lasm_EnterWithPrivateEnvironment.ssa_block_14
-.Lasm_EnterWithPrivateEnvironment.ssa_block_13:
+ jmp .Lasm_EnterWithOwnEnvironments.ssa_block_14
+.Lasm_EnterWithOwnEnvironments.ssa_block_13:
mov BYTE PTR [rbx - 57], 1
add r13d, 1
movzx eax, BYTE PTR [r14 + r13]
jmp [r12 + rax * 8]
-asm_handler_EnterWithPrivateEnvironment_hot_end:
+asm_handler_EnterWithOwnEnvironments_hot_end:
.p2align 6
asm_handler_Mov:
@@ -5480,7 +5483,7 @@ asm_handler_GetById:
jz .Lasm_GetById.ssa_block_1
and rdx, -4
test rdx, rdx
- jz .Lasm_GetById.assert_predicate_failure_334
+ jz .Lasm_GetById.assert_predicate_failure_328
cmp QWORD PTR [rdx + 24], rax
jne .Lasm_GetById.ssa_block_15
mov ecx, DWORD PTR [rdx]
@@ -6367,11 +6370,11 @@ asm_handler_Call:
mov rsi, rdi
.Lasm_Call.ssa_block_23:
test rdx, rdx
- jz .Lasm_Call.assert_predicate_failure_254
+ jz .Lasm_Call.assert_predicate_failure_248
mov eax, DWORD PTR [rdx + 12]
mov edi, DWORD PTR [rdx + 16]
cmp rdi, rax
- jb .Lasm_Call.assert_predicate_failure_260
+ jb .Lasm_Call.assert_predicate_failure_254
mov eax, 4294967295
sub rax, rdi
cmp rax, rsi
@@ -6390,7 +6393,7 @@ asm_handler_Call:
lea r8, [r8 + 136]
add r8, rax
cmp r8, rax
- jb .Lasm_Call.assert_predicate_failure_274
+ jb .Lasm_Call.assert_predicate_failure_268
cmp r8, r11
ja .Lasm_Call.ssa_block_1
mov r11, QWORD PTR [rbp - 48]
@@ -6405,9 +6408,8 @@ asm_handler_Call:
test ecx, ecx
cmovnz rcx, r11
test rcx, rcx
- jz .Lasm_Call.assert_predicate_failure_295
+ jz .Lasm_Call.assert_predicate_failure_289
mov QWORD PTR [rax + 32], rcx
- mov QWORD PTR [rax + 40], rcx
mov QWORD PTR [rax + 80], r9
mov QWORD PTR [rax + 88], rdx
movabs rcx, 9221964661971222528
@@ -6438,7 +6440,7 @@ asm_handler_Call:
mov DWORD PTR [rax + 112], r8d
movzx edx, BYTE PTR [r14 + r13 + 6]
cmp rsi, rdx
- jb .Lasm_Call.assert_predicate_failure_353
+ jb .Lasm_Call.assert_predicate_failure_345
lea r9, [r14 + r13 + 7]
lea r8, [rax + 136]
lea r10, [rbx]
@@ -6465,7 +6467,7 @@ asm_handler_Call:
.Lasm_Call.ssa_block_30:
mov rcx, QWORD PTR [rax + 88]
test rcx, rcx
- jz .Lasm_Call.assert_predicate_failure_375
+ jz .Lasm_Call.assert_predicate_failure_367
mov r14, QWORD PTR [rcx + 40]
test r14, r14
jz .Lasm_Call.assert_failure_6
@@ -6609,13 +6611,9 @@ asm_handler_Call:
mov DWORD PTR [rax + 104], edx
mov QWORD PTR [rax], rcx
mov rcx, QWORD PTR [rbx - 104]
- mov rsi, QWORD PTR [rbx - 96]
test rcx, rcx
- jz .Lasm_Call.assert_predicate_failure_127
- test rsi, rsi
- jz .Lasm_Call.assert_predicate_failure_129
+ jz .Lasm_Call.assert_predicate_failure_125
mov QWORD PTR [rax + 32], rcx
- mov QWORD PTR [rax + 40], rsi
movzx ecx, BYTE PTR [r14 + r13 + 5]
mov rcx, QWORD PTR [rbx + rcx * 8]
mov rsi, 0
@@ -6665,7 +6663,7 @@ asm_handler_Call:
add rdi, rax
mov eax, DWORD PTR [rdi + 8]
cmp rax, 1
- jae .Lasm_Call.assert_predicate_failure_200
+ jae .Lasm_Call.assert_predicate_failure_194
mov rdx, QWORD PTR [rdi]
test rdx, rdx
jz .Lasm_Call.assert_failure_25
@@ -6675,12 +6673,12 @@ asm_handler_Call:
mov rcx, rdx
and rcx, 255
cmp rcx, 2
- jae .Lasm_Call.assert_predicate_failure_207
+ jae .Lasm_Call.assert_predicate_failure_201
test rcx, rcx
jnz .Lasm_Call.ssa_block_6
mov rcx, QWORD PTR [rbx - 40]
test rcx, rcx
- jz .Lasm_Call.assert_predicate_failure_213
+ jz .Lasm_Call.assert_predicate_failure_207
mov rdx, QWORD PTR [rbp - 48]
mov QWORD PTR [rdx], rcx
lea r11, [rbx - 136]
@@ -8604,8 +8602,8 @@ asm_handler_JumpLooselyInequalsLoopRhsInt32_hot_end:
asm_cold_handler_paths:
asm_handler_Enter_cold:
asm_handler_Enter_cold_end:
-asm_handler_EnterWithPrivateEnvironment_cold:
-asm_handler_EnterWithPrivateEnvironment_cold_end:
+asm_handler_EnterWithOwnEnvironments_cold:
+asm_handler_EnterWithOwnEnvironments_cold_end:
asm_handler_Mov_cold:
asm_handler_Mov_cold_end:
asm_handler_MovWide_cold:
@@ -14155,13 +14153,9 @@ asm_handler_GetById_cold:
mov DWORD PTR [rax + 104], 0
mov QWORD PTR [rax], rcx
mov rcx, QWORD PTR [rbx - 104]
- mov rdi, QWORD PTR [rbx - 96]
test rcx, rcx
- jz .Lasm_GetById.assert_predicate_failure_206
- test rdi, rdi
- jz .Lasm_GetById.assert_predicate_failure_208
+ jz .Lasm_GetById.assert_predicate_failure_204
mov QWORD PTR [rax + 32], rcx
- mov QWORD PTR [rax + 40], rdi
movabs rcx, 34359738367
and rsi, rcx
movabs rcx, -1970324836974592
@@ -14198,12 +14192,12 @@ asm_handler_GetById_cold:
mov rcx, rdx
and rcx, 255
cmp rcx, 2
- jae .Lasm_GetById.assert_predicate_failure_264
+ jae .Lasm_GetById.assert_predicate_failure_258
test rcx, rcx
jnz .Lasm_GetById.ssa_block_6
mov rcx, QWORD PTR [rbx - 40]
test rcx, rcx
- jz .Lasm_GetById.assert_predicate_failure_273
+ jz .Lasm_GetById.assert_predicate_failure_267
mov rdx, QWORD PTR [rbp - 48]
mov QWORD PTR [rdx], rcx
lea r11, [rbx - 136]
@@ -20674,9 +20668,9 @@ asm_assertion_failure_traps:
ud2
.Lasm_Enter.assert_predicate_failure_29:
ud2
-.Lasm_EnterWithPrivateEnvironment.assert_predicate_failure_12:
+.Lasm_EnterWithOwnEnvironments.assert_predicate_failure_12:
ud2
-.Lasm_EnterWithPrivateEnvironment.assert_predicate_failure_42:
+.Lasm_EnterWithOwnEnvironments.assert_predicate_failure_46:
ud2
.Lasm_JumpIf.assert_predicate_failure_7:
ud2
@@ -20848,7 +20842,7 @@ asm_assertion_failure_traps:
ud2
.Lasm_GetById.assert_failure_1:
ud2
-.Lasm_GetById.assert_predicate_failure_334:
+.Lasm_GetById.assert_predicate_failure_328:
ud2
.Lasm_GetById.assert_failure_2:
ud2
@@ -20872,15 +20866,13 @@ asm_assertion_failure_traps:
ud2
.Lasm_GetById.assert_predicate_failure_187:
ud2
-.Lasm_GetById.assert_predicate_failure_206:
- ud2
-.Lasm_GetById.assert_predicate_failure_208:
+.Lasm_GetById.assert_predicate_failure_204:
ud2
.Lasm_GetById.assert_failure_10:
ud2
-.Lasm_GetById.assert_predicate_failure_264:
+.Lasm_GetById.assert_predicate_failure_258:
ud2
-.Lasm_GetById.assert_predicate_failure_273:
+.Lasm_GetById.assert_predicate_failure_267:
ud2
.Lasm_GetById.assert_predicate_failure_26:
ud2
@@ -20994,21 +20986,21 @@ asm_assertion_failure_traps:
ud2
.Lasm_Call.assert_failure_3:
ud2
-.Lasm_Call.assert_predicate_failure_254:
+.Lasm_Call.assert_predicate_failure_248:
ud2
-.Lasm_Call.assert_predicate_failure_260:
+.Lasm_Call.assert_predicate_failure_254:
ud2
.Lasm_Call.assert_failure_4:
ud2
.Lasm_Call.assert_failure_5:
ud2
-.Lasm_Call.assert_predicate_failure_274:
+.Lasm_Call.assert_predicate_failure_268:
ud2
-.Lasm_Call.assert_predicate_failure_295:
+.Lasm_Call.assert_predicate_failure_289:
ud2
-.Lasm_Call.assert_predicate_failure_353:
+.Lasm_Call.assert_predicate_failure_345:
ud2
-.Lasm_Call.assert_predicate_failure_375:
+.Lasm_Call.assert_predicate_failure_367:
ud2
.Lasm_Call.assert_failure_6:
ud2
@@ -21030,19 +21022,17 @@ asm_assertion_failure_traps:
ud2
.Lasm_Call.assert_predicate_failure_109:
ud2
-.Lasm_Call.assert_predicate_failure_127:
- ud2
-.Lasm_Call.assert_predicate_failure_129:
+.Lasm_Call.assert_predicate_failure_125:
ud2
.Lasm_Call.assert_failure_24:
ud2
-.Lasm_Call.assert_predicate_failure_200:
+.Lasm_Call.assert_predicate_failure_194:
ud2
.Lasm_Call.assert_failure_25:
ud2
-.Lasm_Call.assert_predicate_failure_207:
+.Lasm_Call.assert_predicate_failure_201:
ud2
-.Lasm_Call.assert_predicate_failure_213:
+.Lasm_Call.assert_predicate_failure_207:
ud2
.Lasm_Call.assert_predicate_failure_91:
ud2
diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flap
index 6117a682007..a0e80cd7dd2 100644
--- a/Libraries/LibJS/Interpreter/interpreter.flap
+++ b/Libraries/LibJS/Interpreter/interpreter.flap
@@ -1316,9 +1316,9 @@ handler Enter() {
dispatch_next;
}
-# NB: Code that makes private environments starts with this, and its frames keep the running private environment
-# from here on. Others read it from their function.
-handler EnterWithPrivateEnvironment() {
+# NB: Code that makes private or variable environments starts with this, and its frames keep their running private
+# and variable environments from here on. Others read them from their function.
+handler EnterWithOwnEnvironments() {
let function = exec_ctx.function;
if function != 0 {
let ecmascript_function: ECMAScriptFunctionObject = alias(function);
@@ -1326,6 +1326,8 @@ handler EnterWithPrivateEnvironment() {
if function.class_bits has ECMASCRIPT_FUNCTION_FLAG_HAS_RARE_DATA {
exec_ctx.private_environment = ecmascript_function.private_environment;
}
+ # NB: The variable environment of a frame starts out as its lexical environment.
+ exec_ctx.variable_environment = exec_ctx.lexical_environment;
}
enter_frame();
dispatch_next;
@@ -3342,11 +3344,8 @@ handler GetById(
native_frame.passed_argument_count = 0;
native_frame.function = function;
let lexical_environment = exec_ctx.lexical_environment;
- let variable_environment = exec_ctx.variable_environment;
assert(lexical_environment != 0);
- assert(variable_environment != 0);
native_frame.lexical_environment = lexical_environment;
- native_frame.variable_environment = variable_environment;
let receiver: Value = alias(box_object(receiver_object));
native_frame.this_value = receiver;
native_frame.executable = null;
@@ -4060,7 +4059,6 @@ inline fn enter_ecmascript_function_call(
let lexical_environment = ecmascript_function.environment;
assert(lexical_environment != 0);
frame.lexical_environment = lexical_environment;
- frame.variable_environment = lexical_environment;
frame.this_value = receiver;
frame.executable = executable_pointer;
let empty_tag: Value = Value<Empty>;
@@ -4150,11 +4148,8 @@ inline fn call_raw_native_function(
native_frame.passed_argument_count = actual_argument_count;
native_frame.function = function;
let lexical_environment = exec_ctx.lexical_environment;
- let variable_environment = exec_ctx.variable_environment;
assert(lexical_environment != 0);
- assert(variable_environment != 0);
native_frame.lexical_environment = lexical_environment;
- native_frame.variable_environment = variable_environment;
let receiver = load(this_value);
native_frame.this_value = receiver;
# Raw native frames deliberately have no executable.
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/environments.rs b/Libraries/LibJS/JIT/Rust/src/builder/environments.rs
index c776248ecc1..b458b9aa4c6 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/environments.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/environments.rs
@@ -42,7 +42,24 @@ impl GraphBuilder<'_, '_> {
if let Some(value) = self.frame.field(field) {
return Ok(value);
}
- let value = self.emit(Op::LoadFrameField { field }, &[], Some(Repr::Pointer));
+ let executable = self.function.executable;
+ let from_function = match field {
+ FrameField::PrivateEnvironment => executable.frames_read_private_environment_from_function,
+ FrameField::VariableEnvironment => executable.frames_read_variable_environment_from_function,
+ _ => false,
+ };
+ let value = if from_function {
+ let function = self.frame_field(FrameField::Function)?;
+ self.emit(
+ Op::LoadFunctionEnvironment {
+ private: field == FrameField::PrivateEnvironment,
+ },
+ &[function],
+ Some(Repr::Pointer),
+ )
+ } else {
+ self.emit(Op::LoadFrameField { field }, &[], Some(Repr::Pointer))
+ };
self.frame.set_field(field, value);
return Ok(value);
}
@@ -52,7 +69,7 @@ impl GraphBuilder<'_, '_> {
let private = match field {
FrameField::LexicalEnvironment | FrameField::VariableEnvironment => Some(false),
FrameField::PrivateEnvironment => Some(true),
- FrameField::Realm | FrameField::Executable => None,
+ FrameField::Realm | FrameField::Executable | FrameField::Function => None,
};
if let Some(private) = private {
// NB: A closure the code creates itself has the environments
@@ -73,6 +90,7 @@ impl GraphBuilder<'_, '_> {
FrameField::LexicalEnvironment | FrameField::VariableEnvironment => executable.environment,
FrameField::Realm => executable.function.map(|function| function.realm),
FrameField::Executable => Some(executable.cell),
+ FrameField::Function => executable.function.map(|function| function.function),
// NB: Inlined callees never read it.
FrameField::PrivateEnvironment => None,
}
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
index 3bc258dcd68..b35bb9e2e02 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
@@ -117,6 +117,7 @@ pub fn handling(opcode: OpCode) -> Handling {
O::GetLexicalEnvironment
| O::SetLexicalEnvironment
| O::LeavePrivateEnvironment
+ | O::EnterWithOwnEnvironments
| O::IsCallable
| O::GetArgumentCount => Handling::Native,
@@ -124,9 +125,6 @@ pub fn handling(opcode: OpCode) -> Handling {
// (see `build_catch_entries()`). Handlers of inlined callees run
// in the interpreter to their end (see `can_inline()`).
O::Catch => Handling::Unsupported("starts an exception handler, which the interpreter runs"),
- // NB: Code with class bodies that have private names runs rarely, and only its frames keep a private
- // environment, which the others read from their function.
- O::EnterWithPrivateEnvironment => Handling::Unsupported("makes private environments"),
// Calls.
O::Call
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index d8923869881..bfd08922734 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -155,6 +155,7 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool, derived_construct:
| OpCode::CreateVariableEnvironment
| OpCode::EnterObjectEnvironment
| OpCode::LeavePrivateEnvironment
+ | OpCode::EnterWithOwnEnvironments
| OpCode::CreateArguments
| OpCode::CreateRestParams
| OpCode::CallDirectEval
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
index 0942c036dfa..d00afa9b196 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/mod.rs
@@ -366,6 +366,7 @@ fn replaces_environments(opcode: OpCode) -> bool {
| OpCode::CreateVariableEnvironment
| OpCode::CreatePrivateEnvironment
| OpCode::LeavePrivateEnvironment
+ | OpCode::EnterWithOwnEnvironments
| OpCode::EnterObjectEnvironment
| OpCode::NewClass
| OpCode::CallDirectEval
@@ -1538,10 +1539,13 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
/// that matters, for the others.
fn try_build_natively(&mut self, instruction: &Instruction) -> Result<Option<Flow>, CompileFailure> {
match *instruction {
- Instruction::Enter => {
- // NB: Inlined frames are set up by the caller.
+ Instruction::Enter | Instruction::EnterWithOwnEnvironments => {
+ // NB: Inlined frames are set up by the caller, and their code never keeps its own environments.
if !self.function.is_virtual() {
self.emit(Op::InitializeFrame, &[], None);
+ if matches!(instruction, Instruction::EnterWithOwnEnvironments) {
+ self.emit(Op::KeepOwnEnvironments, &[], None);
+ }
}
let empty = self.constant(value::EMPTY);
for slot in RESERVED_REGISTER_COUNT as usize..self.function.layout.registers_and_locals_count as usize {
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
index 940e4ee849e..ef25667d38c 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
@@ -657,7 +657,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
u64::from(header.argument_count),
),
(offsets.execution_context_lexical_environment, 8, fields.environment),
- (offsets.execution_context_variable_environment, 8, fields.environment),
(offsets.execution_context_this_value, 8, value::EMPTY),
// NB: Frames get their id when the debugger first needs it.
(offsets.execution_context_frame_id, 8, 0),
@@ -677,7 +676,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
function.uses_this.then_some(offsets.execution_context_this_value),
function.uses_this.then_some(slot(THIS_VALUE_REGISTER)),
environments.then_some(offsets.execution_context_lexical_environment),
- environments.then_some(offsets.execution_context_variable_environment),
target.closures.then_some(offsets.execution_context_function),
]
.into_iter()
@@ -714,8 +712,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
self.emit_load_compressed_cell(temp, &Address::new(aux, offsets.ecmascript_function_environment as i32));
self.masm
.store64(&field(offsets.execution_context_lexical_environment), temp);
- self.masm
- .store64(&field(offsets.execution_context_variable_environment), temp);
}
for (index, argument) in (0..).zip(&call.argument_operands) {
@@ -1447,19 +1443,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
self.masm.store64(&vm_field(offsets.vm_interpreter_stack_top), temp);
// Fill in the frame like the interpreter's call fast path.
- for (source, destination) in [
- (
- offsets.execution_context_lexical_environment,
- offsets.execution_context_lexical_environment,
- ),
- (
- offsets.execution_context_variable_environment,
- offsets.execution_context_variable_environment,
- ),
- ] {
- self.masm.load64(temp, &frame_field(source));
- self.masm.store64(&field(destination), temp);
- }
+ self.masm
+ .load64(temp, &frame_field(offsets.execution_context_lexical_environment));
+ self.masm
+ .store64(&field(offsets.execution_context_lexical_environment), temp);
self.store_call_operand(&field(offsets.execution_context_this_value), this_operand)?;
// NB: Frames get their id when the debugger first needs it.
self.masm.store_imm64(&field(offsets.execution_context_frame_id), 0);
@@ -1578,13 +1565,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
// script or module.
let caller_field = |offset: u32| Address::new(self.pinned.frame, offset as i32);
let field = |offset: u32| Address::new(new_frame, offset as i32);
- for offset in [
- offsets.execution_context_lexical_environment,
- offsets.execution_context_variable_environment,
- ] {
- self.masm.load64(temp, &caller_field(offset));
- self.masm.store64(&field(offset), temp);
- }
+ self.masm
+ .load64(temp, &caller_field(offsets.execution_context_lexical_environment));
+ self.masm
+ .store64(&field(offsets.execution_context_lexical_environment), temp);
self.masm
.store64(&field(offsets.execution_context_caller_frame), self.pinned.frame);
constants.extend([
@@ -1601,7 +1585,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
constants.extend([
(offsets.execution_context_function, 8, header.function.function.0),
(offsets.execution_context_lexical_environment, 8, fields.environment),
- (offsets.execution_context_variable_environment, 8, fields.environment),
// NB: Frames get their id when the debugger first needs it.
(offsets.execution_context_frame_id, 8, 0),
]);
@@ -1649,8 +1632,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
self.emit_load_compressed_cell(temp, &function_field(offsets.ecmascript_function_environment));
self.masm
.store64(&field(offsets.execution_context_lexical_environment), temp);
- self.masm
- .store64(&field(offsets.execution_context_variable_environment), temp);
// NB: Frames get their id when the debugger first needs it.
self.masm.store_imm64(&field(offsets.execution_context_frame_id), 0);
self.masm
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs
index 91f51d4d315..507f7f619fb 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs
@@ -1065,13 +1065,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
self.masm.store64(&vm_field(offsets.vm_interpreter_stack_top), temp);
// Fill in the frame like the interpreter's call fast path.
- for offset in [
- offsets.execution_context_lexical_environment,
- offsets.execution_context_variable_environment,
- ] {
- self.masm.load64(temp, &frame_field(offset));
- self.masm.store64(&field(offset), temp);
- }
+ self.masm
+ .load64(temp, &frame_field(offsets.execution_context_lexical_environment));
+ self.masm
+ .store64(&field(offsets.execution_context_lexical_environment), temp);
let this_local = self.stub_local(local::THIS);
self.masm.load64(temp, &this_local);
self.masm.store64(&field(offsets.execution_context_this_value), temp);
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs b/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs
index 22e91de7169..502c06efddb 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/lowering_info.rs
@@ -270,6 +270,8 @@ pub fn lowering_info(node: &Node) -> LoweringInfo {
// The index of the appended binding.
Op::AppendEnvironmentBinding => plain.with_temps(1),
Op::BoxCell | Op::SetLexicalEnvironment | Op::LeavePrivateEnvironment | Op::IsCallable => plain,
+ // The function, besides the scratch register.
+ Op::KeepOwnEnvironments => plain.with_temps(1),
Op::ToBoolean => LoweringInfo {
inputs: InputConstraints::all(InputConstraint::Fixed(FixedRegister::Argument(0)), 1),
output: OutputConstraint::Return,
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
index 9d7cf76add0..c6ea8777266 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/mod.rs
@@ -1613,6 +1613,30 @@ impl<'a, M: PortableMacroAssembler> Codegen<'a, '_, M> {
];
self.emit_constant_fields(self.pinned.frame, self.pinned.scratch, &mut fields)?;
}
+ Op::KeepOwnEnvironments => {
+ let (scratch, function, without_function) =
+ (self.pinned.scratch, self.temp(node_id, 0), self.masm.new_label());
+ let offsets = self.runtime.offsets;
+ self.masm
+ .load64(function, &self.frame_field(offsets.execution_context_function));
+ self.masm
+ .branch_test64(Condition::Zero, function, u64::MAX, without_function);
+ self.emit_load_function_private_environment(scratch, function);
+ self.masm.store64(
+ &self.frame_field(offsets.execution_context_private_environment),
+ scratch,
+ );
+ // NB: The variable environment of a frame starts out as its lexical environment.
+ self.masm.load64(
+ scratch,
+ &self.frame_field(offsets.execution_context_lexical_environment),
+ );
+ self.masm.store64(
+ &self.frame_field(offsets.execution_context_variable_environment),
+ scratch,
+ );
+ self.masm.bind(without_function);
+ }
Op::LeavePrivateEnvironment => {
let scratch = self.pinned.scratch;
let field = self.frame_field(self.runtime.offsets.execution_context_private_environment);
@@ -2239,23 +2263,8 @@ impl<'a, M: PortableMacroAssembler> Codegen<'a, '_, M> {
let offset = match field {
FrameField::LexicalEnvironment => offsets.execution_context_lexical_environment,
FrameField::VariableEnvironment => offsets.execution_context_variable_environment,
- // NB: Code that makes private environments is not compiled, so a frame with a function has the
- // function's.
- FrameField::PrivateEnvironment => {
- let (scratch, done, without_function) =
- (self.pinned.scratch, self.masm.new_label(), self.masm.new_label());
- self.masm
- .load64(scratch, &self.frame_field(offsets.execution_context_function));
- self.masm
- .branch_test64(Condition::Zero, scratch, u64::MAX, without_function);
- self.emit_load_function_private_environment(output, scratch);
- self.masm.jump(done);
- self.masm.bind(without_function);
- self.masm
- .load64(output, &self.frame_field(offsets.execution_context_private_environment));
- self.masm.bind(done);
- return;
- }
+ FrameField::PrivateEnvironment => offsets.execution_context_private_environment,
+ FrameField::Function => offsets.execution_context_function,
// NB: The running frame runs in the current realm.
FrameField::Realm => {
self.masm
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
index 58f5709bbdd..43d57976a31 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
@@ -3855,7 +3855,7 @@ mod direct_calls {
let half = |offset: usize| (frame[offset / 8] >> (8 * (offset % 8))) as u32;
let byte = |offset: usize| (frame[offset / 8] >> (8 * (offset % 8))) as u8;
assert_eq!(word(64), callee.function.as_ptr() as u64, "function");
- assert_eq!((word(8), word(96)), (FUNCTION_WORDS[0], FUNCTION_WORDS[0]));
+ assert_eq!(word(8), FUNCTION_WORDS[0], "lexical environment");
assert_eq!(word(104), 0, "no frame id yet");
assert_eq!(half(0), 0, "program counter");
assert_eq!((byte(118), byte(24)), (0, 0), "not a construct call, not initialized");
@@ -4254,10 +4254,7 @@ mod direct_calls {
let half = |offset: usize| (frame[offset / 8] >> (8 * (offset % 8))) as u32;
let byte = |offset: usize| (frame[offset / 8] >> (8 * (offset % 8))) as u8;
assert_eq!(word(64), callee.function.as_ptr() as u64, "function");
- assert_eq!(
- (word(8), word(96)),
- (environment(FUNCTION_WORDS[0]), environment(FUNCTION_WORDS[0]))
- );
+ assert_eq!(word(8), environment(FUNCTION_WORDS[0]), "lexical environment");
assert_eq!(word(104), 0, "no frame id yet");
assert_eq!(word(120), value::EMPTY, "no this value");
assert_eq!(word(32), callee.callee.executable.as_ptr() as u64);
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs
index efbdb264d01..c2d88ca7b65 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/dump.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/dump.rs
@@ -238,6 +238,7 @@ fn write_op(f: &mut fmt::Formatter<'_>, layout: &FrameLayout, op: &Op, inputs: &
Op::AppendEnvironmentBinding => inputs.after(f, format_args!("AppendEnvironmentBinding")),
Op::SetLexicalEnvironment => inputs.after(f, format_args!("SetLexicalEnvironment")),
Op::LeavePrivateEnvironment => inputs.after(f, format_args!("LeavePrivateEnvironment")),
+ Op::KeepOwnEnvironments => inputs.after(f, format_args!("KeepOwnEnvironments")),
Op::IsCallable => inputs.after(f, format_args!("IsCallable")),
Op::CheckInt32 => inputs.after(f, format_args!("CheckInt32")),
Op::CheckNumber => inputs.after(f, format_args!("CheckNumber")),
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/info.rs b/Libraries/LibJS/JIT/Rust/src/ir/info.rs
index 693ee6c06e0..bf6b0b92384 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/info.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/info.rs
@@ -271,7 +271,7 @@ impl Op<'_> {
writes: Locations::FRAME,
..P::PURE
}),
- Op::LeavePrivateEnvironment => OpInfo::pure(I::NONE).with_properties(P {
+ Op::LeavePrivateEnvironment | Op::KeepOwnEnvironments => OpInfo::pure(I::NONE).with_properties(P {
reads: Locations::FRAME,
writes: Locations::FRAME,
..P::PURE
diff --git a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
index 40ebb4faeeb..f07e508ce83 100644
--- a/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
+++ b/Libraries/LibJS/JIT/Rust/src/ir/mod.rs
@@ -90,15 +90,17 @@ pub enum FrameField {
PrivateEnvironment,
Realm,
Executable,
+ Function,
}
impl FrameField {
- pub const ALL: [FrameField; 5] = [
+ pub const ALL: [FrameField; 6] = [
FrameField::LexicalEnvironment,
FrameField::VariableEnvironment,
FrameField::PrivateEnvironment,
FrameField::Realm,
FrameField::Executable,
+ FrameField::Function,
];
/// Whether instructions of the frame's function can change the field:
@@ -549,6 +551,9 @@ define_ops! {
SetLexicalEnvironment,
/// The `LeavePrivateEnvironment` bytecode.
LeavePrivateEnvironment,
+ /// What `EnterWithOwnEnvironments` does besides `InitializeFrame`: a frame of a function keeps the function's
+ /// private environment and its lexical environment as its variable environment.
+ KeepOwnEnvironments,
/// The `IsCallable` bytecode: a boolean value telling whether input 0 is a function object.
IsCallable,
/// Truthiness of the tagged input 0 as a `Repr::Bool`, computed by the
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index c1515101c9e..e35adebb652 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -62,6 +62,11 @@ impl CellId {
/// once, so the same index can be the target of several slots.
#[derive(Debug, Clone, Copy, Default)]
pub struct ExecutableSnapshot {
+ /// Whether frames of the executable (of the compiled one, for index 0) get their private environment from their
+ /// function rather than from the frame field, which holds an old value in them.
+ pub frames_read_private_environment_from_function: bool,
+ /// The same, for the variable environment, which is the function's `[[Environment]]`.
+ pub frames_read_variable_environment_from_function: bool,
/// A copy of the assembled bytecode, exactly as the interpreter runs it.
pub bytecode: List<u8>,
/// The address of the executable's own bytecode on the main thread
diff --git a/Libraries/LibJS/Runtime/ExecutionContext.h b/Libraries/LibJS/Runtime/ExecutionContext.h
index 25355159ba2..c55d98abd6e 100644
--- a/Libraries/LibJS/Runtime/ExecutionContext.h
+++ b/Libraries/LibJS/Runtime/ExecutionContext.h
@@ -120,8 +120,9 @@ struct JS_API ExecutionContext {
// The [[ScriptOrModule]] of a context without a function. That of a context with a function comes from the
// function, and this holds an old value: use script_or_module() and set_script_or_module().
ScriptOrModule script_or_module_without_function;
- GC::Ptr<Environment> lexical_environment; // [[LexicalEnvironment]]
- GC::Ptr<Environment> variable_environment; // [[VariableEnvironment]]
+ GC::Ptr<Environment> lexical_environment; // [[LexicalEnvironment]]
+ // NB: Only valid in frames that keep their own variable environment (see the Rust ExecutionContext).
+ GC::Ptr<Environment> variable_environment_in_frame;
// NB: Only valid in frames without a function, and frames of code that makes private environments.
GC::Ptr<PrivateEnvironment> private_environment_in_frame;
@@ -217,7 +218,7 @@ JS_ASSERT_EXECUTION_CONTEXT_FIELD_LAYOUT(function, FUNCTION)
JS_ASSERT_EXECUTION_CONTEXT_FIELD_LAYOUT(realm_without_function, REALM_WITHOUT_FUNCTION)
JS_ASSERT_EXECUTION_CONTEXT_FIELD_LAYOUT(script_or_module_without_function, SCRIPT_OR_MODULE_WITHOUT_FUNCTION)
JS_ASSERT_EXECUTION_CONTEXT_FIELD_LAYOUT(lexical_environment, LEXICAL_ENVIRONMENT)
-JS_ASSERT_EXECUTION_CONTEXT_FIELD_LAYOUT(variable_environment, VARIABLE_ENVIRONMENT)
+JS_ASSERT_EXECUTION_CONTEXT_FIELD_LAYOUT(variable_environment_in_frame, VARIABLE_ENVIRONMENT_IN_FRAME)
JS_ASSERT_EXECUTION_CONTEXT_FIELD_LAYOUT(private_environment_in_frame, PRIVATE_ENVIRONMENT_IN_FRAME)
JS_ASSERT_EXECUTION_CONTEXT_FIELD_LAYOUT(frame_id, FRAME_ID)
JS_ASSERT_EXECUTION_CONTEXT_FIELD_LAYOUT(program_counter, PROGRAM_COUNTER)
diff --git a/Libraries/LibJS/Runtime/VM.h b/Libraries/LibJS/Runtime/VM.h
index 91a20ae7b19..06956f07d0d 100644
--- a/Libraries/LibJS/Runtime/VM.h
+++ b/Libraries/LibJS/Runtime/VM.h
@@ -344,9 +344,6 @@ public:
Environment const* lexical_environment() const { return running_execution_context().lexical_environment.ptr(); }
Environment* lexical_environment() { return running_execution_context().lexical_environment.ptr(); }
- Environment const* variable_environment() const { return running_execution_context().variable_environment.ptr(); }
- Environment* variable_environment() { return running_execution_context().variable_environment.ptr(); }
-
// https://tc39.es/ecma262/#current-realm
// The value of the Realm component of the running execution context is also called the current Realm Record.
Realm const* current_realm() const
diff --git a/Libraries/LibJS/Rust/generate/layout_header.rs b/Libraries/LibJS/Rust/generate/layout_header.rs
index dfed26354e8..0d8012275df 100644
--- a/Libraries/LibJS/Rust/generate/layout_header.rs
+++ b/Libraries/LibJS/Rust/generate/layout_header.rs
@@ -329,7 +329,7 @@ pub fn generate() -> LayoutHeader {
realm_without_function,
script_or_module_without_function,
lexical_environment,
- variable_environment,
+ variable_environment_in_frame,
private_environment_in_frame,
frame_id,
program_counter,
diff --git a/Libraries/LibJS/Rust/generate/layout_table.rs b/Libraries/LibJS/Rust/generate/layout_table.rs
index 86fbc326f95..b8af3d65196 100644
--- a/Libraries/LibJS/Rust/generate/layout_table.rs
+++ b/Libraries/LibJS/Rust/generate/layout_table.rs
@@ -335,7 +335,7 @@ pub fn generate(configuration: &LayoutConfiguration) -> LayoutWriter {
field!(w, "EXECUTION_CONTEXT_REALM_WITHOUT_FUNCTION", "ExecutionContext.realm_without_function", "Realm", ExecutionContext, realm_without_function, 8, "nullable", "cell", "function_and_realm");
field!(w, "EXECUTION_CONTEXT_SCRIPT_OR_MODULE_WITHOUT_FUNCTION", "ExecutionContext.script_or_module_without_function", "u64", ExecutionContext, script_or_module_without_function, 8, "nullable", "scalar");
field!(w, "EXECUTION_CONTEXT_LEXICAL_ENVIRONMENT", "ExecutionContext.lexical_environment", "Environment", ExecutionContext, lexical_environment, 8, "nullable", "cell", "environments");
- field!(w, "EXECUTION_CONTEXT_VARIABLE_ENVIRONMENT", "ExecutionContext.variable_environment", "Environment", ExecutionContext, variable_environment, 8, "nullable", "cell", "environments");
+ field!(w, "EXECUTION_CONTEXT_VARIABLE_ENVIRONMENT", "ExecutionContext.variable_environment", "Environment", ExecutionContext, variable_environment_in_frame, 8, "nullable", "cell", "environments");
field!(w, "EXECUTION_CONTEXT_PRIVATE_ENVIRONMENT", "ExecutionContext.private_environment", "PrivateEnvironment", ExecutionContext, private_environment_in_frame, 8, "nullable", "cell");
field!(w, "EXECUTION_CONTEXT_YIELD_CONTINUATION", "ExecutionContext.yield_continuation", "u32", ExecutionContext, yield_continuation, 4, "nullable", "scalar");
field!(w, "EXECUTION_CONTEXT_YIELD_IS_AWAIT", "ExecutionContext.yield_is_await", "bool", ExecutionContext, yield_is_await, 1, "nullable", "scalar");
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
index 2a6f3ce452b..40f1e7d3341 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
@@ -380,7 +380,7 @@ impl Generator<'_> {
/// The private names of a class come next, whose environment is current until the class is done.
fn enter_class_private_environment(&mut self) {
self.put(op::CreatePrivateEnvironment {});
- self.makes_private_environments = true;
+ self.keeps_own_environments = true;
let outer_handler = self.handler();
// NB: An exception that leaves the function takes the private environment with its execution context.
let handler = outer_handler.map(|_| Handler {
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/context.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/context.rs
index be8d3d78a07..1ce7c91dbc7 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/context.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/context.rs
@@ -109,8 +109,8 @@ pub struct Generator<'t> {
pub(super) builtin_abstract_operations_enabled: bool,
pub(super) eager_compile_direct_iifes: bool,
pub(super) contains_direct_call_to_eval_in_non_strict_mode: bool,
- /// Whether the code makes private environments, so that it starts with `EnterWithPrivateEnvironment`.
- pub(super) makes_private_environments: bool,
+ /// Whether the code makes private or variable environments, so that it starts with `EnterWithOwnEnvironments`.
+ pub(super) keeps_own_environments: bool,
pub(super) enclosing_environment_scope: Option<Arc<EnclosingEnvironmentScope>>,
pub(super) variable_environment_coordinate_scope_index: Option<usize>,
pub(super) enclosing_code: Arc<EnclosingCode>,
@@ -193,7 +193,7 @@ impl<'t> Generator<'t> {
builtin_abstract_operations_enabled: false,
eager_compile_direct_iifes: false,
contains_direct_call_to_eval_in_non_strict_mode: false,
- makes_private_environments: false,
+ keeps_own_environments: false,
enclosing_environment_scope: None,
variable_environment_coordinate_scope_index: None,
enclosing_code,
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index 20e54ee1d20..ea05f8748bf 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -1344,6 +1344,7 @@ impl Generator<'_> {
&& (scope.has_function_in_parameters || parameters_have_environment));
if needs_variable_environment {
self.note_creates_environment();
+ self.keeps_own_environments = true;
let environment = self.held_register();
self.in_head(|generator| {
generator.put(op::CreateVariableEnvironment {
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index 907ec2fefcc..93f081be57a 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -4224,11 +4224,11 @@ impl Generator<'_> {
pub(super) fn finish_stream(&mut self, other_tables_size: usize) -> (AssembledBytecode, SliceBlockBuilder) {
// NB: Only code that runs in frames without a function starts without Enter, and those frames keep their
// private environment anyway.
- if self.makes_private_environments
+ if self.keeps_own_environments
&& let Some(enter) = self.context.stream.head.bytes.first_mut()
&& *enter == OpCode::Enter as u8
{
- *enter = OpCode::EnterWithPrivateEnvironment as u8;
+ *enter = OpCode::EnterWithOwnEnvironments as u8;
}
// NB: Folding leaves constants that nothing uses, and they would take up slots in every frame.
let mut constant_uses = vec![0u32; self.context.constants.values.len()];
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 57e69572af6..6080b11d33a 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -2008,10 +2008,10 @@ impl Executable {
&self.code.bytecode
}
- /// Whether the code makes private environments, which its frames keep (see
- /// `ExecutionContext::keeps_private_environment()`).
- pub fn makes_private_environments(&self) -> bool {
- self.bytecode().first() == Some(&(OpCode::EnterWithPrivateEnvironment as u8))
+ /// Whether the code makes private or variable environments, so that its frames keep their own (see
+ /// `ExecutionContext::keeps_private_environment()` and `ExecutionContext::keeps_variable_environment()`).
+ pub fn keeps_own_environments(&self) -> bool {
+ self.bytecode().first() == Some(&(OpCode::EnterWithOwnEnvironments as u8))
}
pub fn constants(&self) -> &[Value] {
diff --git a/Libraries/LibJS/Rust/src/debugger.rs b/Libraries/LibJS/Rust/src/debugger.rs
index 08e8c07ce1e..77488a7abab 100644
--- a/Libraries/LibJS/Rust/src/debugger.rs
+++ b/Libraries/LibJS/Rust/src/debugger.rs
@@ -289,6 +289,13 @@ impl Debugger {
// NB: The copy must hold the objects that a frame of an inlined call has markers for.
crate::jit::translate::resolve_deferred_arguments(vm, execution_context);
let context = execution_context.copy();
+ // NB: The copy runs as a context without a function, which keeps its own environments: the evaluated code
+ // declares its variables in the local environment, and the variable environment of a frame of a function
+ // without a function environment would be the function's [[Environment]].
+ context.function.set(None);
+ context.set_realm(execution_context.realm());
+ context.set_script_or_module(execution_context.script_or_module());
+ context.set_private_environment(execution_context.private_environment());
let local_environment = new_declarative_environment(
vm,
context
@@ -314,7 +321,7 @@ impl Debugger {
}
}
context.lexical_environment.set(Some(local_environment.upcast()));
- context.variable_environment.set(Some(local_environment.upcast()));
+ context.set_variable_environment(Some(local_environment.upcast()));
vm.push_execution_context_checking_stack_space(context.as_non_null())?;
let _pop_context = ScopeGuard::new(|| {
vm.pop_execution_context();
diff --git a/Libraries/LibJS/Rust/src/interpreter/execution_context.rs b/Libraries/LibJS/Rust/src/interpreter/execution_context.rs
index 58438dde528..4cfffc08067 100644
--- a/Libraries/LibJS/Rust/src/interpreter/execution_context.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/execution_context.rs
@@ -11,10 +11,11 @@ use std::alloc::{Layout, alloc, dealloc, handle_alloc_error};
use crate::bytecode::executable::Executable;
use crate::gc::visitor::{Trace, Visitor};
use crate::layout::cell::Gc;
-use crate::layout::environment::PrivateEnvironment;
+use crate::layout::environment::{Environment, PrivateEnvironment};
use crate::layout::execution_context::{ExecutionContext, ScriptOrModule, ScriptOrModuleSlot};
use crate::layout::value::Value;
use crate::runtime::ecmascript_function_object::{EcmascriptFunctionObject, as_ecmascript_function_object};
+use crate::runtime::native_function::NativeFunction;
use crate::runtime::realm::Realm;
use libjs_abi::register::RESERVED_REGISTER_COUNT;
@@ -60,14 +61,67 @@ impl ExecutionContext {
self.realm_without_function.set(realm);
}
+ /// Whether the context's code starts with `EnterWithOwnEnvironments`.
+ fn runs_code_that_keeps_own_environments(&self) -> bool {
+ self.executable
+ .get()
+ .is_some_and(|executable| Executable::from_head(executable).keeps_own_environments())
+ }
+
/// Whether the context keeps its own [[PrivateEnvironment]]: one without a function, or one whose code makes
/// private environments.
pub fn keeps_private_environment(&self) -> bool {
- self.function.get().is_none()
- || self
- .executable
- .get()
- .is_some_and(|executable| Executable::from_head(executable).makes_private_environments())
+ self.function.get().is_none() || self.runs_code_that_keeps_own_environments()
+ }
+
+ /// Whether the context keeps its own [[VariableEnvironment]]: one without a function, one of a function that needs
+ /// a function environment, or one whose code makes variable environments. That of another ECMAScript function is
+ /// the function's [[Environment]], and a native function has none.
+ pub fn keeps_variable_environment(&self) -> bool {
+ let Some(function) = self.function.get() else {
+ return true;
+ };
+ if let Some(function) = function.downcast::<EcmascriptFunctionObject>() {
+ return function.function_environment_needed() || self.runs_code_that_keeps_own_environments();
+ }
+ function
+ .downcast::<NativeFunction>()
+ .is_some_and(|function| function.function_environment_needed())
+ }
+
+ /// The [[VariableEnvironment]] of the context.
+ pub fn variable_environment(&self) -> Option<Gc<Environment>> {
+ if self.keeps_variable_environment() {
+ return self.variable_environment_in_frame.get();
+ }
+ self.function
+ .get()
+ .and_then(|function| function.downcast::<EcmascriptFunctionObject>())
+ .and_then(|function| function.environment())
+ }
+
+ /// Sets the [[VariableEnvironment]] of a context that keeps its own.
+ pub fn set_variable_environment(&self, variable_environment: Option<Gc<Environment>>) {
+ debug_assert!(
+ self.keeps_variable_environment(),
+ "a context of a function without a function environment has its function's"
+ );
+ self.variable_environment_in_frame.set(variable_environment);
+ }
+
+ /// Whether the variable environment field holds the context's [[VariableEnvironment]], which the code of a
+ /// context that keeps its own sets when it starts.
+ fn holds_variable_environment(&self) -> bool {
+ let Some(function) = self.function.get() else {
+ return true;
+ };
+ match function.downcast::<EcmascriptFunctionObject>() {
+ Some(function) => {
+ function.function_environment_needed()
+ || (self.frame_initialized.get() && self.runs_code_that_keeps_own_environments())
+ }
+ None => self.keeps_variable_environment(),
+ }
}
/// The [[PrivateEnvironment]] of the context. One that does not keep its own has its function's.
@@ -118,7 +172,7 @@ impl ExecutionContext {
realm_without_function: Cell::new(None),
script_or_module_without_function: ScriptOrModuleSlot::new(ScriptOrModule::Empty),
lexical_environment: Cell::new(None),
- variable_environment: Cell::new(None),
+ variable_environment_in_frame: Cell::new(None),
private_environment_in_frame: Cell::new(None),
frame_id: Cell::new(0),
program_counter: Cell::new(0),
@@ -182,7 +236,8 @@ impl ExecutionContext {
copy.script_or_module_without_function
.set(self.script_or_module_without_function.get());
copy.lexical_environment.set(self.lexical_environment.get());
- copy.variable_environment.set(self.variable_environment.get());
+ copy.variable_environment_in_frame
+ .set(self.variable_environment_in_frame.get());
copy.private_environment_in_frame
.set(self.private_environment_in_frame.get());
copy.program_counter.set(self.program_counter.get());
@@ -221,7 +276,8 @@ impl ExecutionContext {
copy.script_or_module_without_function
.set(self.script_or_module_without_function.get());
copy.lexical_environment.set(self.lexical_environment.get());
- copy.variable_environment.set(self.variable_environment.get());
+ copy.variable_environment_in_frame
+ .set(self.variable_environment_in_frame.get());
copy.private_environment_in_frame
.set(self.private_environment_in_frame.get());
copy.program_counter.set(self.program_counter.get());
@@ -246,7 +302,9 @@ impl ExecutionContext {
unsafe impl Trace for ExecutionContext {
fn trace(&self, visitor: &mut Visitor) {
self.function.trace(visitor);
- self.variable_environment.trace(visitor);
+ if self.holds_variable_environment() {
+ self.variable_environment_in_frame.trace(visitor);
+ }
self.lexical_environment.trace(visitor);
self.this_value.trace(visitor);
self.new_target.trace(visitor);
diff --git a/Libraries/LibJS/Rust/src/interpreter/run.rs b/Libraries/LibJS/Rust/src/interpreter/run.rs
index 78e2d405e0a..2377bd9a421 100644
--- a/Libraries/LibJS/Rust/src/interpreter/run.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/run.rs
@@ -308,9 +308,7 @@ impl Vm {
script_context_ref.set_script_or_module(ScriptOrModule::Script(script_record));
// 6. Set the VariableEnvironment of scriptContext to globalEnv.
- script_context_ref
- .variable_environment
- .set(Some(global_environment.upcast()));
+ script_context_ref.set_variable_environment(Some(global_environment.upcast()));
// 7. Set the LexicalEnvironment of scriptContext to globalEnv.
script_context_ref
@@ -460,7 +458,9 @@ impl Vm {
.set(new_target.map_or(Value::UNDEFINED, Value::from_object));
let environment = callee_function.inline_call_environment(self, new_target);
callee_context.lexical_environment.set(environment);
- callee_context.variable_environment.set(environment);
+ // NB: Only the frame of a function that needs a function environment keeps the field, but a store costs less
+ // than asking.
+ callee_context.variable_environment_in_frame.set(environment);
Some(self.enter_inline_frame(callee_context, callee_executable))
}
@@ -511,9 +511,6 @@ impl Vm {
callee_context
.lexical_environment
.set(caller_context.lexical_environment.get());
- callee_context
- .variable_environment
- .set(caller_context.variable_environment.get());
Some(self.enter_inline_frame(callee_context, callee_executable))
}
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs
index f62e82b75c5..87a2db5c7fc 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/bindings.rs
@@ -163,7 +163,7 @@ fn running_execution_context_environment(vm: &Vm, mode: EnvironmentMode) -> Gc<E
.expect("bindings are created in an execution context");
match mode {
EnvironmentMode::Lexical => context.lexical_environment.get(),
- EnvironmentMode::Var => context.variable_environment.get(),
+ EnvironmentMode::Var => context.variable_environment(),
}
.expect("the running execution context has its environments")
}
@@ -1226,8 +1226,7 @@ pub fn set_variable_binding_of_block_function(
let name = get_identifier(vm, &instruction.identifier);
let context = running_execution_context(vm);
let variable_environment = context
- .variable_environment
- .get()
+ .variable_environment()
.expect("program code has a VariableEnvironment");
let mut environment = context
.lexical_environment
@@ -1452,7 +1451,7 @@ pub fn verify_environment_coordinate(
let context = running_execution_context(vm);
let mut environment = match environment_mode_of(instruction.mode) {
EnvironmentMode::Lexical => context.lexical_environment.get(),
- EnvironmentMode::Var => context.variable_environment.get(),
+ EnvironmentMode::Var => context.variable_environment(),
};
let mut hop = 0;
@@ -1620,9 +1619,7 @@ pub fn create_variable_environment_slow_path(
shape_cache,
instruction.capacity,
);
- running_execution_context
- .variable_environment
- .set(Some(var_environment.upcast()));
+ running_execution_context.set_variable_environment(Some(var_environment.upcast()));
running_execution_context
.lexical_environment
.set(Some(var_environment.upcast()));
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 48cca017ec6..9265330f083 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -2371,7 +2371,7 @@ impl Vm {
}
pub fn variable_environment(&self) -> Option<Gc<Environment>> {
- self.running_execution_context_ref().variable_environment.get()
+ self.running_execution_context_ref().variable_environment()
}
/// The Realm of the running execution context, which C++ VM::realm() dereferences.
diff --git a/Libraries/LibJS/Rust/src/jit/dispatch_tables.rs b/Libraries/LibJS/Rust/src/jit/dispatch_tables.rs
index 842656f4821..d309ea3cf13 100644
--- a/Libraries/LibJS/Rust/src/jit/dispatch_tables.rs
+++ b/Libraries/LibJS/Rust/src/jit/dispatch_tables.rs
@@ -31,7 +31,7 @@ pub fn warming_up_dispatch_table() -> Box<DispatchTable> {
mixed_dispatch_table(|name| {
matches!(
name,
- "Enter" | "EnterWithPrivateEnvironment" | "Call" | "Return" | "End"
+ "Enter" | "EnterWithOwnEnvironments" | "Call" | "Return" | "End"
) || name.contains("Loop")
})
}
diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
index 8204de7c645..b8aa9c7d736 100644
--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs
+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
@@ -145,7 +145,7 @@ fn runtime_offsets() -> RuntimeOffsets {
accessor_getter: offset(offset_of!(Accessor, getter)),
accessor_setter: offset(offset_of!(Accessor, setter)),
execution_context_function: offset(offset_of!(ExecutionContext, function)),
- execution_context_variable_environment: offset(offset_of!(ExecutionContext, variable_environment)),
+ execution_context_variable_environment: offset(offset_of!(ExecutionContext, variable_environment_in_frame)),
execution_context_frame_id: offset(offset_of!(ExecutionContext, frame_id)),
execution_context_yield_continuation: offset(offset_of!(ExecutionContext, yield_continuation)),
execution_context_yield_is_await: offset(offset_of!(ExecutionContext, yield_is_await)),
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index 1318c0f3632..2a92de40b33 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -1324,7 +1324,24 @@ impl SnapshotBuilder<'_> {
self.environment_templates.clear();
let intrinsics = realm.intrinsics();
+ // NB: The same rules as ExecutionContext::keeps_private_environment() and keeps_variable_environment().
+ let frame_function = match entry.function {
+ Some(InlinedFunction::Ecmascript(function)) => Some(function),
+ Some(InlinedFunction::Builtin(_)) => None,
+ None => self.compiled_function,
+ };
+ let keeps_own_environments = executable.keeps_own_environments();
+ let (frames_read_private_environment_from_function, frames_read_variable_environment_from_function) =
+ match frame_function {
+ Some(function) => (
+ !keeps_own_environments,
+ !keeps_own_environments && !function.function_environment_needed(),
+ ),
+ None => (false, false),
+ };
ExecutableSnapshot {
+ frames_read_private_environment_from_function,
+ frames_read_variable_environment_from_function,
bytecode: self.snapshot.add(bytecode),
bytecode_address: bytecode.as_ptr() as u64,
layout: FrameLayout {
diff --git a/Libraries/LibJS/Rust/src/jit/translate.rs b/Libraries/LibJS/Rust/src/jit/translate.rs
index 14adce2848b..a35a698896f 100644
--- a/Libraries/LibJS/Rust/src/jit/translate.rs
+++ b/Libraries/LibJS/Rust/src/jit/translate.rs
@@ -509,10 +509,7 @@ pub(super) fn create_arguments_object(vm: &Vm, frame: &ExecutionContext, mapped:
function,
ecmascript_function.mapped_argument_names(),
passed_arguments,
- frame
- .variable_environment
- .get()
- .expect("a function runs in an environment"),
+ frame.variable_environment().expect("a function runs in an environment"),
))
}
diff --git a/Libraries/LibJS/Rust/src/layout/execution_context.rs b/Libraries/LibJS/Rust/src/layout/execution_context.rs
index a4d7d87ef1c..2c775cb9898 100644
--- a/Libraries/LibJS/Rust/src/layout/execution_context.rs
+++ b/Libraries/LibJS/Rust/src/layout/execution_context.rs
@@ -83,10 +83,13 @@ pub struct ExecutionContext {
/// it from the function, and this holds an old value: read it through `ExecutionContext::script_or_module()`.
pub script_or_module_without_function: ScriptOrModuleSlot,
pub lexical_environment: Cell<Option<Gc<Environment>>>,
- pub variable_environment: Cell<Option<Gc<Environment>>>,
- /// The [[PrivateEnvironment]] of a frame without a function, or of a frame whose code makes private environments
- /// (which starts with `EnterWithPrivateEnvironment`). Other frames get it from their function, and this holds an
- /// old value: read it through `ExecutionContext::private_environment()`.
+ /// The [[VariableEnvironment]] of a frame that keeps its own (see `ExecutionContext::keeps_variable_environment()`).
+ /// Other frames get it from their function, and this holds an old value: read it through
+ /// `ExecutionContext::variable_environment()`.
+ pub variable_environment_in_frame: Cell<Option<Gc<Environment>>>,
+ /// The [[PrivateEnvironment]] of a frame without a function, or of a frame whose code starts with
+ /// `EnterWithOwnEnvironments`. Other frames get it from their function, and this holds an old value: read it
+ /// through `ExecutionContext::private_environment()`.
pub private_environment_in_frame: Cell<Option<Gc<PrivateEnvironment>>>,
/// Tells frames apart for the debugger and the JIT. Frames have none (0) until something asks (see
/// Vm::frame_id_of()).
diff --git a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
index 210bcd7b81b..4ef878913b0 100644
--- a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
+++ b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
@@ -1204,8 +1204,7 @@ pub fn perform_eval(vm: &Vm, x: Value, strict_caller: CallerMode, direct: EvalMo
// b. Let varEnv be runningContext's VariableEnvironment.
variable_environment = running_context
- .variable_environment
- .get()
+ .variable_environment()
.expect("the running execution context has a VariableEnvironment");
// c. Let privateEnv be runningContext's PrivateEnvironment.
@@ -1274,7 +1273,7 @@ pub fn perform_eval(vm: &Vm, x: Value, strict_caller: CallerMode, direct: EvalMo
eval_context.set_script_or_module(running_context.script_or_module());
// 26. Set evalContext's VariableEnvironment to varEnv.
- eval_context.variable_environment.set(Some(variable_environment));
+ eval_context.set_variable_environment(Some(variable_environment));
// 27. Set evalContext's LexicalEnvironment to lexEnv.
eval_context.lexical_environment.set(Some(lexical_environment));
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
index b8b58be678a..281475efa48 100644
--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
@@ -866,12 +866,11 @@ impl EcmascriptFunctionObject {
callee_context.lexical_environment.set(Some(local_environment.upcast()));
// 9. Set the VariableEnvironment of calleeContext to localEnv.
- callee_context
- .variable_environment
- .set(Some(local_environment.upcast()));
+ callee_context.set_variable_environment(Some(local_environment.upcast()));
} else {
+ // NB: ExecutionContext::variable_environment() reads it from the function, and the code sets it in frames
+ // that keep their own.
callee_context.lexical_environment.set(self.environment());
- callee_context.variable_environment.set(self.environment());
}
// 10. Set the PrivateEnvironment of calleeContext to F.[[PrivateEnvironment]].
diff --git a/Libraries/LibJS/Rust/src/runtime/native_function.rs b/Libraries/LibJS/Rust/src/runtime/native_function.rs
index 1522af1090d..877dddfe443 100644
--- a/Libraries/LibJS/Rust/src/runtime/native_function.rs
+++ b/Libraries/LibJS/Rust/src/runtime/native_function.rs
@@ -318,16 +318,11 @@ impl NativeFunction {
callee_context.lexical_environment.set(Some(local_environment.upcast()));
// 9. Set the VariableEnvironment of calleeContext to localEnv.
- callee_context
- .variable_environment
- .set(Some(local_environment.upcast()));
+ callee_context.set_variable_environment(Some(local_environment.upcast()));
} else {
callee_context
.lexical_environment
.set(caller_context.lexical_environment.get());
- callee_context
- .variable_environment
- .set(caller_context.variable_environment.get());
}
// </8.> --------------------------------------------------------------------------
@@ -396,16 +391,11 @@ impl NativeFunction {
callee_context.lexical_environment.set(Some(local_environment.upcast()));
// 9. Set the VariableEnvironment of calleeContext to localEnv.
- callee_context
- .variable_environment
- .set(Some(local_environment.upcast()));
+ callee_context.set_variable_environment(Some(local_environment.upcast()));
} else {
callee_context
.lexical_environment
.set(caller_context.lexical_environment.get());
- callee_context
- .variable_environment
- .set(caller_context.variable_environment.get());
}
// </8.> --------------------------------------------------------------------------
diff --git a/Libraries/LibJS/Rust/src/runtime/source_text_module.rs b/Libraries/LibJS/Rust/src/runtime/source_text_module.rs
index 152a3bd6ca8..d82951d0d9c 100644
--- a/Libraries/LibJS/Rust/src/runtime/source_text_module.rs
+++ b/Libraries/LibJS/Rust/src/runtime/source_text_module.rs
@@ -558,8 +558,7 @@ impl SourceTextModule {
// 13. Set the VariableEnvironment of moduleContext to module.[[Environment]].
self.execution_context
- .variable_environment
- .set(Some(environment.upcast()));
+ .set_variable_environment(Some(environment.upcast()));
// 14. Set the LexicalEnvironment of moduleContext to module.[[Environment]].
self.execution_context
@@ -923,7 +922,7 @@ impl SourceTextModule {
let environment = self.environment().expect("a linked module has an environment");
// 6. Set the VariableEnvironment of moduleContext to module.[[Environment]].
- module_context_ref.variable_environment.set(Some(environment.upcast()));
+ module_context_ref.set_variable_environment(Some(environment.upcast()));
// 7. Set the LexicalEnvironment of moduleContext to module.[[Environment]].
module_context_ref.lexical_environment.set(Some(environment.upcast()));
diff --git a/Libraries/LibJS/Rust/src/runtime/synthetic_module.rs b/Libraries/LibJS/Rust/src/runtime/synthetic_module.rs
index 73052e24659..dbb79782b82 100644
--- a/Libraries/LibJS/Rust/src/runtime/synthetic_module.rs
+++ b/Libraries/LibJS/Rust/src/runtime/synthetic_module.rs
@@ -264,7 +264,7 @@ impl SyntheticModule {
// 5. Set the VariableEnvironment of moduleContext to module.[[Environment]].
let environment = self.environment().map(Gc::upcast);
- module_context_ref.variable_environment.set(environment);
+ module_context_ref.set_variable_environment(environment);
// 6. Set the LexicalEnvironment of moduleContext to module.[[Environment]].
module_context_ref.lexical_environment.set(environment);
diff --git a/Tests/LibJS/Bytecode/expected/annexb-var-coordinate-with-default-param.txt b/Tests/LibJS/Bytecode/expected/annexb-var-coordinate-with-default-param.txt
index c78df29e165..2081ebdb22e 100644
--- a/Tests/LibJS/Bytecode/expected/annexb-var-coordinate-with-default-param.txt
+++ b/Tests/LibJS/Bytecode/expected/annexb-var-coordinate-with-default-param.txt
@@ -22,7 +22,7 @@ f$62596dad annexb-var-coordinate-with-default-param.js:3:12
[2] = Int32(2)
block0:
- [ 0] Enter
+ [ 0] EnterWithOwnEnvironments
[ 1] CreateVariable `x`, is_immutable:false, is_global:false, is_strict:false
[ 8] GetLexicalEnvironment dst:reg4
[ a] JumpUndefined condition:arg0, true_target:block1, false_target:block2
diff --git a/Tests/LibJS/Bytecode/expected/parameter-environment-capacity.txt b/Tests/LibJS/Bytecode/expected/parameter-environment-capacity.txt
index dd95adc6fbf..64c612c6a38 100644
--- a/Tests/LibJS/Bytecode/expected/parameter-environment-capacity.txt
+++ b/Tests/LibJS/Bytecode/expected/parameter-environment-capacity.txt
@@ -63,7 +63,7 @@ method$5d5353bc parameter-environment-capacity.js:11:12
[1] = String("a")
block0:
- [ 0] Enter
+ [ 0] EnterWithOwnEnvironments
[ 1] CreateVariable `mode`, is_immutable:false, is_global:false, is_strict:false
[ 8] CreateVariable `index`, is_immutable:false, is_global:false, is_strict:false
[ f] GetLexicalEnvironment dst:reg4
diff --git a/Tests/LibJS/Bytecode/expected/postfix-increment-private-member.txt b/Tests/LibJS/Bytecode/expected/postfix-increment-private-member.txt
index 644d15d29f2..f80dfb0cccb 100644
--- a/Tests/LibJS/Bytecode/expected/postfix-increment-private-member.txt
+++ b/Tests/LibJS/Bytecode/expected/postfix-increment-private-member.txt
@@ -6,7 +6,7 @@ $1d8ca011 postfix-increment-private-member.js:1:1
[1] = String("nextId")
block0:
- [ 0] EnterWithPrivateEnvironment
+ [ 0] EnterWithOwnEnvironments
[ 1] GetLexicalEnvironment dst:reg4
[ 3] Mov dst:reg5, src:Undefined
[ 6] CreateLexicalEnvironment dst:reg7, parent:reg4, capacity:1, is_catch_environment:false
diff --git a/Tests/LibJS/Bytecode/expected/private-identifier-access-patterns.txt b/Tests/LibJS/Bytecode/expected/private-identifier-access-patterns.txt
index 12bb877fdbd..772d2b5119c 100644
--- a/Tests/LibJS/Bytecode/expected/private-identifier-access-patterns.txt
+++ b/Tests/LibJS/Bytecode/expected/private-identifier-access-patterns.txt
@@ -6,7 +6,7 @@ $f0373b3d private-identifier-access-patterns.js:1:1
[1] = String("method")
block0:
- [ 0] EnterWithPrivateEnvironment
+ [ 0] EnterWithOwnEnvironments
[ 1] GetLexicalEnvironment dst:reg4
[ 3] Mov dst:reg5, src:Undefined
[ 6] CreateLexicalEnvironment dst:reg7, parent:reg4, capacity:1, is_catch_environment:false
diff --git a/Tests/LibJS/Bytecode/expected/private-logical-assignment-register-order.txt b/Tests/LibJS/Bytecode/expected/private-logical-assignment-register-order.txt
index 8ae5471b1d5..95b710ff69f 100644
--- a/Tests/LibJS/Bytecode/expected/private-logical-assignment-register-order.txt
+++ b/Tests/LibJS/Bytecode/expected/private-logical-assignment-register-order.txt
@@ -6,7 +6,7 @@ $1d8ca011 private-logical-assignment-register-order.js:1:1
[1] = String("f")
block0:
- [ 0] EnterWithPrivateEnvironment
+ [ 0] EnterWithOwnEnvironments
[ 1] GetLexicalEnvironment dst:reg4
[ 3] Mov dst:reg5, src:Undefined
[ 6] CreateLexicalEnvironment dst:reg7, parent:reg4, capacity:1, is_catch_environment:false
diff --git a/Tests/LibJS/Bytecode/expected/private-method-call-description.txt b/Tests/LibJS/Bytecode/expected/private-method-call-description.txt
index 1d6dd549231..0ef14678431 100644
--- a/Tests/LibJS/Bytecode/expected/private-method-call-description.txt
+++ b/Tests/LibJS/Bytecode/expected/private-method-call-description.txt
@@ -6,7 +6,7 @@ $1d8ca011 private-method-call-description.js:1:1
[1] = String("call")
block0:
- [ 0] EnterWithPrivateEnvironment
+ [ 0] EnterWithOwnEnvironments
[ 1] GetLexicalEnvironment dst:reg4
[ 3] Mov dst:reg5, src:Undefined
[ 6] CreateLexicalEnvironment dst:reg7, parent:reg4, capacity:1, is_catch_environment:false
diff --git a/Tests/LibJS/Bytecode/expected/var-env-capacity-with-param-expressions.txt b/Tests/LibJS/Bytecode/expected/var-env-capacity-with-param-expressions.txt
index 743ea58c5de..54151bbf255 100644
--- a/Tests/LibJS/Bytecode/expected/var-env-capacity-with-param-expressions.txt
+++ b/Tests/LibJS/Bytecode/expected/var-env-capacity-with-param-expressions.txt
@@ -21,7 +21,7 @@ f$150d3c39 var-env-capacity-with-param-expressions.js:1:12
[2] = String("")
block0:
- [ 0] Enter
+ [ 0] EnterWithOwnEnvironments
[ 1] GetLexicalEnvironment dst:reg4
[ 3] CreateLexicalEnvironment dst:reg5, parent:reg4, capacity:1, is_catch_environment:false
[ f] CreateVariable `x`, is_immutable:false, is_global:false, is_strict:false
diff --git a/Tests/LibJS/Debugger/expected/var-declarations.txt b/Tests/LibJS/Debugger/expected/var-declarations.txt
new file mode 100644
index 00000000000..673b06ee57d
--- /dev/null
+++ b/Tests/LibJS/Debugger/expected/var-declarations.txt
@@ -0,0 +1,5 @@
+Paused at <ladybird>/Tests/LibJS/Debugger/input/var-declarations.js:7:1 (entry)
+Paused at <ladybird>/Tests/LibJS/Debugger/input/var-declarations.js:3:5 (debugger statement)
+2
+Paused at <ladybird>/Tests/LibJS/Debugger/input/var-declarations.js:8:1 (debugger statement)
+"undefined"
diff --git a/Tests/LibJS/Debugger/input/var-declarations.commands b/Tests/LibJS/Debugger/input/var-declarations.commands
new file mode 100644
index 00000000000..9a38493fba6
--- /dev/null
+++ b/Tests/LibJS/Debugger/input/var-declarations.commands
@@ -0,0 +1,5 @@
+c
+p var declared = value + 1; declared
+c
+p typeof declared
+c
diff --git a/Tests/LibJS/Debugger/input/var-declarations.js b/Tests/LibJS/Debugger/input/var-declarations.js
new file mode 100644
index 00000000000..748097bc022
--- /dev/null
+++ b/Tests/LibJS/Debugger/input/var-declarations.js
@@ -0,0 +1,8 @@
+function pausing() {
+ let value = 1;
+ debugger;
+ return value;
+}
+
+pausing();
+debugger;
diff --git a/Tests/LibJS/Runtime/functions/function-variable-environment-frames.js b/Tests/LibJS/Runtime/functions/function-variable-environment-frames.js
new file mode 100644
index 00000000000..767c8ba9420
--- /dev/null
+++ b/Tests/LibJS/Runtime/functions/function-variable-environment-frames.js
@@ -0,0 +1,89 @@
+test("parameter expressions that capture a parameter keep their own variable environment", () => {
+ function f(a, read = () => a) {
+ var a = 2;
+ return [read(), a];
+ }
+ for (let i = 0; i < 100; ++i) expect(f(1)).toEqual([1, 2]);
+});
+
+test("functions in parameters see the parameters, the body sees its variables", () => {
+ function f(
+ x,
+ g = function () {
+ return x;
+ }
+ ) {
+ var x;
+ var y = g();
+ x = 5;
+ return [y, g(), x];
+ }
+ for (let i = 0; i < 100; ++i) expect(f(3)).toEqual([3, 3, 5]);
+});
+
+test("sloppy direct eval declares variables in the function", () => {
+ function f(code) {
+ eval(code);
+ return typeof declared === "undefined" ? "none" : declared;
+ }
+ for (let i = 0; i < 50; ++i) {
+ expect(f("var declared = 'yes'")).toBe("yes");
+ expect(f("let declared = 'no'")).toBe("none");
+ }
+});
+
+test("sloppy direct eval in parameter expressions", () => {
+ function f(a = eval("var b = 7; b"), c = () => b) {
+ return [a, c()];
+ }
+ for (let i = 0; i < 50; ++i) expect(f()).toEqual([7, 7]);
+});
+
+test("block functions in sloppy functions set their variable binding", () => {
+ function f() {
+ {
+ function inner() {
+ return "inner";
+ }
+ }
+ return inner();
+ }
+ function g(x = 1) {
+ {
+ function inner() {
+ return x;
+ }
+ }
+ return inner();
+ }
+ for (let i = 0; i < 100; ++i) {
+ expect(f()).toBe("inner");
+ expect(g()).toBe(1);
+ }
+});
+
+test("generators and async functions with parameter expressions", async () => {
+ function* g(a, read = () => a) {
+ var a = 2;
+ yield read();
+ yield a;
+ }
+ async function h(a, read = () => a) {
+ var a = 4;
+ await null;
+ return [read(), a];
+ }
+ for (let i = 0; i < 50; ++i) expect([...g(1)]).toEqual([1, 2]);
+ expect(await h(3)).toEqual([3, 4]);
+});
+
+test("var bindings of closures in a hot function", () => {
+ function counter() {
+ var count = 0;
+ return () => ++count;
+ }
+ const next = counter();
+ let last = 0;
+ for (let i = 0; i < 1000; ++i) last = next();
+ expect(last).toBe(1000);
+});