Skip to content

Truly independent web browser

LibJS: Read arguments.length and arguments[i] from the frame

Andreas Kling8abf151e5a2a
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index 146a8833609..7f31eabb8a9 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -107,6 +107,7 @@ asm_dispatch_table:
.quad asm_handler_ThrowConstAssignment
.quad asm_handler_ThrowNotAFunction
.quad asm_handler_GetArgumentCount
+ .quad asm_handler_GetArgument
.quad asm_handler_Throw
.quad asm_handler_Debugger
.quad asm_handler_Await
@@ -268,7 +269,6 @@ asm_dispatch_table:
.quad asm_handler_fallback
.quad asm_handler_fallback
.quad asm_handler_fallback
- .quad asm_handler_fallback
.globl CSYM(js_interpreter_debug_dispatch_table)
CSYM(js_interpreter_debug_dispatch_table):
@@ -909,6 +909,10 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_GetArgumentCount_hot_end
.quad asm_handler_GetArgumentCount_cold
.quad asm_handler_GetArgumentCount_cold_end
+ .quad asm_handler_GetArgument
+ .quad asm_handler_GetArgument_hot_end
+ .quad asm_handler_GetArgument_cold
+ .quad asm_handler_GetArgument_cold_end
.quad asm_handler_Throw
.quad asm_handler_Throw_hot_end
.quad asm_handler_Throw_cold
@@ -1553,10 +1557,6 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_fallback_hot_end
.quad asm_handler_fallback_cold
.quad asm_handler_fallback_cold_end
- .quad asm_handler_fallback
- .quad asm_handler_fallback_hot_end
- .quad asm_handler_fallback_cold
- .quad asm_handler_fallback_cold_end
.text
@@ -4295,6 +4295,79 @@ asm_handler_ThrowIfNullish:
br x10
asm_handler_ThrowIfNullish_hot_end:
+.p2align 4
+asm_handler_GetArgumentCount:
+ ldr w0, [x28, #104]
+ tbnz x0, #31, .Lasm_GetArgumentCount.ssa_block_1
+ movk x0, #0x7ffa, lsl #48
+ ldrb w9, [x21, #1]
+ str x0, [x27, x9, lsl #3]
+ ldrb w9, [x21, #2]
+ add x21, x21, #2
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_GetArgumentCount.ssa_block_1:
+ mov x0, x20
+ sub w1, w21, w26
+ str w1, [x28, #64]
+ mov x0, x20
+ sub w1, w21, w26
+ mov x2, x21
+ bl CSYM(asm_slow_path_get_argument_count)
+ tbnz x0, #63, .Lasm_GetArgumentCount.scalar_result_done_0
+ tbz x0, #32, .Lasm_GetArgumentCount.scalar_result_done_0
+ ldrb w9, [x21, #1]
+ str x1, [x27, x9, lsl #3]
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_GetArgumentCount.scalar_result_done_0:
+ tbz x0, #63, 1f
+ b .Lexit
+1:
+ tbz x0, #32, .Lasm_GetArgumentCount.slow_path_transfer_1
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_GetArgumentCount.slow_path_transfer_1:
+ ldr x28, [x20]
+ ldr x9, [x28, #88]
+ ldr x26, [x9, #40]
+ add x27, x28, #136
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+asm_handler_GetArgumentCount_hot_end:
+
+.p2align 4
+asm_handler_GetArgument:
+ ldrb w0, [x21, #2]
+ ldr x0, [x27, x0, lsl #3]
+ lsr x9, x0, #48
+ cmp x9, x22
+ b.ne .Lasm_GetArgument.ssa_block_1
+ sxtw x0, w0
+ tbz w0, #31, 1f
+ b .Lasm_GetArgument.ssa_block_1
+1:
+ ldr w1, [x28, #104]
+ cmp x0, x1
+ b.hs .Lasm_GetArgument.ssa_block_1
+ ldp w1, w2, [x28, #116]
+ subs x1, x1, x2
+ adds x1, x1, x0
+ ldr x0, [x27, x1, lsl #3]
+ ldrb w9, [x21, #1]
+ str x0, [x27, x9, lsl #3]
+ ldrb w9, [x21, #4]
+ add x21, x21, #4
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+asm_handler_GetArgument_hot_end:
+
.p2align 4
asm_handler_ToPrimitiveWithStringHint:
ldrb w0, [x21, #2]
@@ -12540,33 +12613,46 @@ asm_handler_ThrowNotAFunction_hot_end:
asm_handler_ThrowNotAFunction_cold:
asm_handler_ThrowNotAFunction_cold_end:
-.p2align 4
-asm_handler_GetArgumentCount:
+asm_handler_GetArgumentCount_cold:
+asm_handler_GetArgumentCount_cold_end:
+asm_handler_GetArgument_cold:
+// Cold paths for GetArgument
+.Lasm_GetArgument.ssa_block_1:
mov x0, x20
sub w1, w21, w26
str w1, [x28, #64]
+ ldrb w9, [x21, #2]
+ ldr x10, [x27, x9, lsl #3]
+.Lasm_GetArgument.scalar_value_ready_0:
+ mov x3, x10
mov x0, x20
sub w1, w21, w26
mov x2, x21
- bl CSYM(asm_slow_path_get_argument_count)
- tbnz x0, #63, .Lasm_GetArgumentCount.scalar_result_done_0
- tbz x0, #32, .Lasm_GetArgumentCount.scalar_result_done_0
+ bl CSYM(asm_slow_path_get_argument)
+ tbz x0, #63, 1f
+ b .Lasm_GetArgument.scalar_result_done_1
+1:
+ tbnz x0, #32, 1f
+ b .Lasm_GetArgument.scalar_result_done_1
+1:
ldrb w9, [x21, #1]
str x1, [x27, x9, lsl #3]
add x21, x26, w0, uxtw
ldrb w9, [x21]
ldr x10, [x19, x9, lsl #3]
br x10
-.Lasm_GetArgumentCount.scalar_result_done_0:
+.Lasm_GetArgument.scalar_result_done_1:
tbz x0, #63, 1f
b .Lexit
1:
- tbz x0, #32, .Lasm_GetArgumentCount.slow_path_transfer_1
+ tbnz x0, #32, 1f
+ b .Lasm_GetArgument.slow_path_transfer_2
+1:
add x21, x26, w0, uxtw
ldrb w9, [x21]
ldr x10, [x19, x9, lsl #3]
br x10
-.Lasm_GetArgumentCount.slow_path_transfer_1:
+.Lasm_GetArgument.slow_path_transfer_2:
ldr x28, [x20]
ldr x9, [x28, #88]
ldr x26, [x9, #40]
@@ -12575,10 +12661,7 @@ asm_handler_GetArgumentCount:
ldrb w9, [x21]
ldr x10, [x19, x9, lsl #3]
br x10
-asm_handler_GetArgumentCount_hot_end:
-asm_handler_GetArgumentCount_cold:
-asm_handler_GetArgumentCount_cold_end:
-
+asm_handler_GetArgument_cold_end:
.p2align 4
asm_handler_Throw:
mov x0, x20
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
index c6b96dcf946..9b63a2bf4d2 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -108,6 +108,7 @@ asm_dispatch_table:
.quad asm_handler_ThrowConstAssignment
.quad asm_handler_ThrowNotAFunction
.quad asm_handler_GetArgumentCount
+ .quad asm_handler_GetArgument
.quad asm_handler_Throw
.quad asm_handler_Debugger
.quad asm_handler_Await
@@ -269,7 +270,6 @@ asm_dispatch_table:
.quad asm_handler_fallback
.quad asm_handler_fallback
.quad asm_handler_fallback
- .quad asm_handler_fallback
.globl CSYM(js_interpreter_debug_dispatch_table)
CSYM(js_interpreter_debug_dispatch_table):
@@ -910,6 +910,10 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_GetArgumentCount_hot_end
.quad asm_handler_GetArgumentCount_cold
.quad asm_handler_GetArgumentCount_cold_end
+ .quad asm_handler_GetArgument
+ .quad asm_handler_GetArgument_hot_end
+ .quad asm_handler_GetArgument_cold
+ .quad asm_handler_GetArgument_cold_end
.quad asm_handler_Throw
.quad asm_handler_Throw_hot_end
.quad asm_handler_Throw_cold
@@ -1554,10 +1558,6 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_fallback_hot_end
.quad asm_handler_fallback_cold
.quad asm_handler_fallback_cold_end
- .quad asm_handler_fallback
- .quad asm_handler_fallback_hot_end
- .quad asm_handler_fallback_cold
- .quad asm_handler_fallback_cold_end
.text
@@ -4271,6 +4271,80 @@ asm_handler_ThrowIfNullish:
jmp [r12 + rax * 8]
asm_handler_ThrowIfNullish_hot_end:
+.p2align 6
+asm_handler_GetArgumentCount:
+ mov eax, DWORD PTR [rbx - 32]
+ test eax, eax
+ js .Lasm_GetArgumentCount.ssa_block_1
+ movabs r11, 9221683186994511872
+ or rax, r11
+ movzx r11d, BYTE PTR [r14 + r13 + 1]
+ mov QWORD PTR [rbx + r11 * 8], rax
+ add r13d, 2
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_GetArgumentCount.ssa_block_1:
+ mov DWORD PTR [rbx + -72], r13d
+ lea r11, [r14 + r13]
+ mov rdi, QWORD PTR [rbp - 48]
+ mov esi, r13d
+ lea rdx, [r14 + r13]
+ call CSYM(asm_slow_path_get_argument_count)
+ test rax, rax
+ js .Lasm_GetArgumentCount.scalar_result_done_0
+ bt rax, 32
+ jnc .Lasm_GetArgumentCount.scalar_result_done_0
+ lea rcx, [r14 + r13]
+ movzx r10d, BYTE PTR [rcx + 1]
+ mov QWORD PTR [rbx + r10 * 8], rdx
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_GetArgumentCount.scalar_result_done_0:
+ test rax, rax
+ js .Lexit
+ bt rax, 32
+ jnc .Lasm_GetArgumentCount.slow_path_transfer_1
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_GetArgumentCount.slow_path_transfer_1:
+ mov rcx, QWORD PTR [rbp - 48]
+ mov rbx, QWORD PTR [rcx]
+ add rbx, 136
+ mov rcx, QWORD PTR [rbx + -48]
+ mov r14, QWORD PTR [rcx + 40]
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+asm_handler_GetArgumentCount_hot_end:
+
+.p2align 6
+asm_handler_GetArgument:
+ movzx eax, BYTE PTR [r14 + r13 + 2]
+ mov rax, QWORD PTR [rbx + rax * 8]
+ mov r11, rax
+ shr r11, 48
+ cmp r11w, 32762
+ jne .Lasm_GetArgument.ssa_block_1
+ movsxd rax, eax
+ test eax, eax
+ js .Lasm_GetArgument.ssa_block_1
+ mov ecx, DWORD PTR [rbx - 32]
+ cmp rax, rcx
+ jae .Lasm_GetArgument.ssa_block_1
+ mov ecx, DWORD PTR [rbx - 20]
+ mov edx, DWORD PTR [rbx - 16]
+ sub rcx, rdx
+ add rcx, rax
+ mov rax, QWORD PTR [rbx + rcx * 8]
+ movzx r11d, BYTE PTR [r14 + r13 + 1]
+ mov QWORD PTR [rbx + r11 * 8], rax
+ add r13d, 4
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+asm_handler_GetArgument_hot_end:
+
.p2align 6
asm_handler_ToPrimitiveWithStringHint:
movzx eax, BYTE PTR [r14 + r13 + 2]
@@ -12267,33 +12341,40 @@ asm_handler_ThrowNotAFunction_hot_end:
asm_handler_ThrowNotAFunction_cold:
asm_handler_ThrowNotAFunction_cold_end:
-.p2align 4
-asm_handler_GetArgumentCount:
+asm_handler_GetArgumentCount_cold:
+asm_handler_GetArgumentCount_cold_end:
+asm_handler_GetArgument_cold:
+# Cold paths for GetArgument
+.Lasm_GetArgument.ssa_block_1:
mov DWORD PTR [rbx + -72], r13d
lea r11, [r14 + r13]
+ movzx r10d, BYTE PTR [r11 + 2]
+ mov rax, QWORD PTR [rbx + r10 * 8]
+.Lasm_GetArgument.scalar_value_ready_0:
+ mov rcx, rax
mov rdi, QWORD PTR [rbp - 48]
mov esi, r13d
lea rdx, [r14 + r13]
- call CSYM(asm_slow_path_get_argument_count)
+ call CSYM(asm_slow_path_get_argument)
test rax, rax
- js .Lasm_GetArgumentCount.scalar_result_done_0
+ js .Lasm_GetArgument.scalar_result_done_1
bt rax, 32
- jnc .Lasm_GetArgumentCount.scalar_result_done_0
+ jnc .Lasm_GetArgument.scalar_result_done_1
lea rcx, [r14 + r13]
movzx r10d, BYTE PTR [rcx + 1]
mov QWORD PTR [rbx + r10 * 8], rdx
mov r13d, eax
movzx eax, BYTE PTR [r14 + r13]
jmp [r12 + rax * 8]
-.Lasm_GetArgumentCount.scalar_result_done_0:
+.Lasm_GetArgument.scalar_result_done_1:
test rax, rax
js .Lexit
bt rax, 32
- jnc .Lasm_GetArgumentCount.slow_path_transfer_1
+ jnc .Lasm_GetArgument.slow_path_transfer_2
mov r13d, eax
movzx eax, BYTE PTR [r14 + r13]
jmp [r12 + rax * 8]
-.Lasm_GetArgumentCount.slow_path_transfer_1:
+.Lasm_GetArgument.slow_path_transfer_2:
mov rcx, QWORD PTR [rbp - 48]
mov rbx, QWORD PTR [rcx]
add rbx, 136
@@ -12302,10 +12383,7 @@ asm_handler_GetArgumentCount:
mov r13d, eax
movzx eax, BYTE PTR [r14 + r13]
jmp [r12 + rax * 8]
-asm_handler_GetArgumentCount_hot_end:
-asm_handler_GetArgumentCount_cold:
-asm_handler_GetArgumentCount_cold_end:
-
+asm_handler_GetArgument_cold_end:
.p2align 4
asm_handler_Throw:
mov DWORD PTR [rbx + -72], r13d
diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flap
index b19e89020b2..3e34761d3aa 100644
--- a/Libraries/LibJS/Interpreter/interpreter.flap
+++ b/Libraries/LibJS/Interpreter/interpreter.flap
@@ -2626,7 +2626,34 @@ handler ThrowNotAFunction(
handler GetArgumentCount(
dst: out Operand
-) @cold = call_slow_path(get_argument_count);
+) {
+ let count = exec_ctx.passed_argument_count;
+ guard count <= INT32_MAX else {
+ call_slow_path(get_argument_count);
+ };
+ store(dst, box_i32(assume_i32(count)));
+ dispatch_next;
+}
+
+# `arguments[index]` of a function that has no arguments object, which reads the passed argument from the frame.
+handler GetArgument(
+ dst: out Operand,
+ index: in Operand,
+ kind: ArgumentsKind
+) {
+ let slow = || @cold {
+ call_slow_path(get_argument);
+ };
+ let key = load(index);
+ guard extract_tag(key) == INT32_TAG else slow;
+ let signed_index = unbox_i32(key);
+ guard signed_index >= 0 else slow;
+ let argument_index = assume_u32(signed_index);
+ guard argument_index < exec_ctx.passed_argument_count else slow;
+ let first_argument: u32 = exec_ctx.slot_count - exec_ctx.argument_count;
+ store(dst, values[first_argument + argument_index]);
+ dispatch_next;
+}
handler Throw(
src: Operand
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
index 0be8c366ca8..5385293a6c5 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
@@ -137,7 +137,7 @@ pub fn handling(opcode: OpCode) -> Handling {
GENERIC_CALL_READS_WHOLE_FRAME
}
- O::CreateArguments | O::CreateRestParams | O::Debugger => GENERIC_READS_WHOLE_FRAME,
+ O::CreateArguments | O::CreateRestParams | O::GetArgument | O::Debugger => GENERIC_READS_WHOLE_FRAME,
// Slow paths that only allocate or inspect values.
O::NewObject
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index 669d4f66eb6..b2bad6cb504 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -150,6 +150,7 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool, derived_construct:
| OpCode::LeavePrivateEnvironment
| OpCode::CreateArguments
| OpCode::CreateRestParams
+ | OpCode::GetArgument
| OpCode::CallDirectEval
| OpCode::CallDirectEvalWithArgumentArray
| OpCode::Debugger
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index 2ae03cc4f2a..a5f6cddb15b 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -58,6 +58,9 @@ enum Location {
Argument(u32),
Environment,
Outer,
+ /// The arguments object of a function that reads only `arguments.length` and `arguments[key]` of it, which its frame
+ /// answers without the object.
+ Frame,
}
/// A binding of the function being generated, or what a free name means.
@@ -231,6 +234,11 @@ pub struct CodeScope {
var_of_arguments_is_captured: bool,
/// Whether a lexical declaration of the name `arguments` takes the place of the arguments object.
has_lexical_arguments: bool,
+ /// Whether the frame may answer for the arguments object, which nothing else needs, unless the parameters say
+ /// otherwise.
+ may_read_arguments_from_frame: bool,
+ /// What the arguments object would be, if the frame answers for it.
+ arguments_from_frame: Option<ArgumentsKind>,
/// Whether a call of the function creates an environment of its own, which the environment coordinates count.
has_function_environment: bool,
/// Whether the lexical declarations of the function have an environment of their own.
@@ -330,6 +338,11 @@ impl CodeScope {
self.flags & function_flag::SIMPLE_PARAMETERS != 0
}
+ /// Whether the function, of simple parameters, has none.
+ pub(super) fn has_no_simple_parameters(&self) -> bool {
+ self.simple_parameters.is_empty()
+ }
+
#[inline(always)]
pub(super) fn is_program(&self) -> bool {
self.program.is_some()
@@ -438,9 +451,11 @@ impl Generator<'_> {
let early_start = at;
let mut has_captures = false;
let mut has_var_of_arguments = false;
+ let mut declares_arguments = false;
for _ in 0..early_count {
let early = ScopeBinding::from_entry(read_varint(summary, &mut at));
has_captures |= early.is_captured;
+ declares_arguments |= early.name.0 == N_ARGUMENTS;
if early.name.0 == N_ARGUMENTS && flags & function_flag::SIMPLE_PARAMETERS != 0 {
if early.kind == BindingKind::Plain {
has_var_of_arguments = true;
@@ -458,6 +473,27 @@ impl Generator<'_> {
scope.bitmap_start = summary_start + at;
scope.bitmap_end = summary_end;
+ use function_flag::*;
+ let is_simple = flags & SIMPLE_PARAMETERS != 0;
+ let is_mapped = is_simple && flags & STRICT == 0;
+ // NB: The frame keeps the values that were passed, which the parameters of an unmapped arguments object must not
+ // overwrite. Generators and async functions keep the object, as their frames get suspended.
+ let may_read_arguments_from_frame = !is_arrow
+ && !declares_arguments
+ && flags & ARGUMENTS_FROM_FRAME != 0
+ && flags & (USES_ARGUMENTS | MIGHT_NEED_ARGUMENTS) != 0
+ && flags
+ & (CAPTURES_ARGUMENTS
+ | CONTAINS_EVAL
+ | GENERATOR
+ | ASYNC
+ | FIELD_INITIALIZER
+ | STATIC_BLOCK
+ | DEFAULT_CONSTRUCTOR)
+ == 0
+ && (is_mapped || flags & READS_ARGUMENTS_INDEX == 0 || (is_simple && flags & MAY_WRITE_PARAMETERS == 0));
+ self.context.scope.may_read_arguments_from_frame = may_read_arguments_from_frame;
+
self.begin_stream();
self.begin_head();
self.put(op::Enter {});
@@ -468,6 +504,7 @@ impl Generator<'_> {
&& flags & function_flag::CAPTURES_ARGUMENTS != 0
&& flags & (function_flag::MIGHT_NEED_ARGUMENTS | function_flag::USES_ARGUMENTS) != 0)
|| (!is_arrow
+ && !may_read_arguments_from_frame
&& flags & function_flag::ARGUMENTS_ACCESS != 0
&& flags & function_flag::SIMPLE_PARAMETERS != 0
&& index.parameter_count[row.0 as usize] != 0);
@@ -476,7 +513,6 @@ impl Generator<'_> {
self.end_head();
self.begin_stream_temporaries();
- use function_flag::*;
if flags & (FIELD_INITIALIZER | STATIC_BLOCK | DEFAULT_CONSTRUCTOR) == 0
&& (flags & SIMPLE_PARAMETERS == 0
|| self.contains_direct_call_to_eval_in_non_strict_mode
@@ -508,6 +544,8 @@ impl Generator<'_> {
scope.has_function_in_parameters = false;
scope.var_of_arguments_is_captured = false;
scope.has_lexical_arguments = false;
+ scope.may_read_arguments_from_frame = false;
+ scope.arguments_from_frame = None;
scope.has_function_environment = false;
scope.has_mapped_parameters = false;
scope.deferred_early_bindings = None;
@@ -924,8 +962,12 @@ impl Generator<'_> {
return;
}
let is_named_arguments = names.iter().any(|name| name.0 == N_ARGUMENTS);
- let needs_arguments_object =
+ let mut needs_arguments_object =
mentions_arguments && !is_named_arguments && !self.context.scope.has_lexical_arguments;
+ let mut reads_arguments_from_frame = needs_arguments_object && self.context.scope.may_read_arguments_from_frame;
+ if reads_arguments_from_frame && !is_simple {
+ needs_arguments_object = false;
+ }
let is_mapped = needs_arguments_object && !self.strict && is_simple;
if !is_simple {
self.context.scope.has_parameter_scope = true;
@@ -948,6 +990,9 @@ impl Generator<'_> {
if needs_arguments_object {
self.create_arguments_object(flags, false, false);
}
+ if reads_arguments_from_frame {
+ self.read_arguments_from_frame(ArgumentsKind::Unmapped);
+ }
return;
}
@@ -956,12 +1001,14 @@ impl Generator<'_> {
let mut names = std::mem::take(&mut self.context.scope.simple_parameters);
names.clear();
let mut has_duplicates = false;
+ let first_parameter_binding = self.context.scope.bindings.len();
+ let first_parameter = self.context.scope.parameters.len();
for (index, &name) in parameter_names.iter().enumerate() {
let first = self.context.scope.binding_of(name);
// NB: A name that comes up again is the one binding, of which the summary knows once.
let is_captured = match first {
Some(first) => self.context.scope.binding(first).location == Location::Environment,
- None => self.next_binding_is_captured() || forces_environment,
+ None => self.next_binding_is_captured() || (forces_environment && !reads_arguments_from_frame),
};
has_duplicates |= first.is_some();
names.push(SimpleParameter {
@@ -988,6 +1035,27 @@ impl Generator<'_> {
is_parameter: false,
});
}
+ if reads_arguments_from_frame {
+ // NB: The frame has the values of the parameters that a mapped arguments object aliases only while none of
+ // them is in the environment.
+ if is_mapped && names.iter().any(|parameter| parameter.is_captured) {
+ reads_arguments_from_frame = false;
+ if forces_environment {
+ let scope = &mut self.context.scope;
+ for parameter in &mut names {
+ parameter.is_captured = true;
+ }
+ for binding in &mut scope.bindings[first_parameter_binding..] {
+ binding.location = Location::Environment;
+ }
+ for (_, is_local) in &mut scope.parameters[first_parameter..] {
+ *is_local = false;
+ }
+ }
+ } else {
+ needs_arguments_object = false;
+ }
+ }
let creates_bindings = is_mapped && !names.is_empty() && names.iter().all(|parameter| parameter.is_captured);
let has_environment_parameters = names.iter().any(|parameter| parameter.is_captured);
if has_environment_parameters || needs_arguments_object {
@@ -1015,6 +1083,13 @@ impl Generator<'_> {
if needs_arguments_object {
self.create_arguments_object(flags, is_mapped, creates_bindings);
}
+ if reads_arguments_from_frame {
+ self.read_arguments_from_frame(if self.strict {
+ ArgumentsKind::Unmapped
+ } else {
+ ArgumentsKind::Mapped
+ });
+ }
if has_environment_parameters {
self.in_head(|generator| {
let undefined = generator.context.constants.add_undefined();
@@ -1039,7 +1114,7 @@ impl Generator<'_> {
});
}
let scope = &mut self.context.scope;
- scope.has_mapped_parameters = is_mapped;
+ scope.has_mapped_parameters = is_mapped && needs_arguments_object;
scope.simple_parameters = names;
}
@@ -1155,6 +1230,34 @@ impl Generator<'_> {
}
}
+ /// Binds `arguments` to what the frame answers for an arguments object of `kind`, which nothing creates.
+ fn read_arguments_from_frame(&mut self, kind: ArgumentsKind) {
+ let scope = &mut self.context.scope;
+ scope.arguments_from_frame = Some(kind);
+ let in_parameters = scope.has_parameter_scope;
+ scope.add_binding(Binding {
+ name: Name(N_ARGUMENTS),
+ location: Location::Frame,
+ kind: Some(DeclarationKind::Var),
+ is_global: false,
+ block: BlockId::default(),
+ shadowed: None,
+ is_catch_parameter: false,
+ in_parameters,
+ is_parameter: false,
+ });
+ }
+
+ /// What the frame answers for the arguments object at this point of the code, if `arguments` means that.
+ pub(super) fn arguments_from_frame(&mut self) -> Option<ArgumentsKind> {
+ let kind = self.context.scope.arguments_from_frame?;
+ if self.context.scope.with_depth != 0 {
+ return None;
+ }
+ let binding = self.context.scope.binding_of(Name(N_ARGUMENTS))?;
+ (self.context.scope.binding(binding).location == Location::Frame).then_some(kind)
+ }
+
fn create_arguments_object(&mut self, flags: u32, is_mapped: bool, creates_bindings: bool) {
let name = Name(N_ARGUMENTS);
let is_captured = flags & (function_flag::CAPTURES_ARGUMENTS | function_flag::CONTAINS_EVAL) != 0
@@ -1397,6 +1500,10 @@ impl Generator<'_> {
Location::Local(index) => (Some(LocalType::Variable), index),
Location::Argument(index) => (Some(LocalType::Argument), index),
Location::Environment | Location::Outer => (None, 0),
+ Location::Frame => {
+ debug_assert!(false, "the frame only answers for arguments.length and arguments[key]");
+ (None, 0)
+ }
};
Identifier {
name: binding.name,
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index 7f8ab4ca71a..ae671cc0290 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -43,6 +43,7 @@ use crate::slice_block::SliceBlock;
use crate::slice_block::SliceBlockBuilder;
use crate::u32_from_usize;
+use libjs_abi::ArgumentsKind;
use libjs_abi::FunctionNamePrefix;
use super::bindings::*;
@@ -139,6 +140,9 @@ pub enum ValKind {
InvalidTarget,
/// The abstract operation `name` of the engine, about to be called.
BuiltinCallee,
+ /// `arguments`, which the frame answers for, about to be the base of `arguments.length` or `arguments[key]`. `name`
+ /// is the ArgumentsKind of the arguments object that it stands for.
+ FrameArguments,
}
/// Whether the binding of an identifier that gets written can come into being after the reference to it is made.
@@ -1482,10 +1486,49 @@ impl Generator<'_> {
#[inline(always)]
pub(super) fn pop_operand(&mut self) -> (Operand, u32) {
let value = self.pop_value();
+ if value.kind == ValKind::FrameArguments {
+ let kind = if value.name == ArgumentsKind::Mapped as u32 {
+ ArgumentsKind::Mapped
+ } else {
+ ArgumentsKind::Unmapped
+ };
+ return (self.create_arguments_for_frame(kind), value.first_temporary);
+ }
debug_assert!(matches!(value.kind, ValKind::Value | ValKind::Callee));
(value.a, value.first_temporary)
}
+ /// An arguments object of `kind` for a use of `arguments` that the frame does not answer for, which the parser rules
+ /// out.
+ #[cold]
+ fn create_arguments_for_frame(&mut self, kind: ArgumentsKind) -> Operand {
+ debug_assert!(false, "the frame only answers for arguments.length and arguments[key]");
+ // FIXME: A mapped arguments object would alias parameters that are not in the environment here, so one is made
+ // only without parameters, and an unmapped one, with a throwing `callee`, otherwise.
+ let kind = if kind == ArgumentsKind::Mapped && self.context.scope.has_no_simple_parameters() {
+ ArgumentsKind::Mapped
+ } else {
+ ArgumentsKind::Unmapped
+ };
+ let dst = self.temporary();
+ self.put(op::CreateArguments {
+ dst: Some(dst),
+ kind: kind as u8,
+ is_immutable: false,
+ creates_parameter_bindings: false,
+ });
+ dst
+ }
+
+ /// `arguments`, which the frame answers for as an arguments object of `kind` would.
+ pub(super) fn value_of_arguments_from_frame(&mut self, kind: ArgumentsKind) {
+ let first_temporary = self.next_temporary();
+ self.push_value(Operand::invalid(), first_temporary);
+ let value = self.context.stream.values.last_mut().expect("it was just pushed");
+ value.kind = ValKind::FrameArguments;
+ value.name = kind as u32;
+ }
+
/// Takes a copy of `local` for everything on the stack that holds it, right before something writes it.
#[inline(always)]
fn before_write_to_local(&mut self, local: Operand) {
@@ -1947,6 +1990,17 @@ impl Generator<'_> {
/// `value.name`, for the value on top of the stack.
#[inline(always)]
pub(super) fn value_of_member(&mut self, name: Name) {
+ if self.top_value_kind() == ValKind::FrameArguments {
+ let key = self.intern_property_key(name);
+ if self.context.names.is_length_property_key(key) {
+ let first_temporary = self.pop_value().first_temporary;
+ self.release_temporaries_from(first_temporary);
+ let dst = self.temporary();
+ self.put_with_destination(op::GetArgumentCount { dst }, dst);
+ self.push_value(dst, first_temporary);
+ return;
+ }
+ }
let (base, first_temporary) = self.pop_operand();
self.release_temporaries_from(first_temporary);
let dst = self.temporary();
@@ -1986,6 +2040,23 @@ impl Generator<'_> {
/// `base[key]`, for the two values on top of the stack.
pub(super) fn value_of_index(&mut self) {
+ let values = &self.context.stream.values;
+ if values.len() >= 2 && values[values.len() - 2].kind == ValKind::FrameArguments {
+ let (key, _) = self.pop_operand();
+ let base = self.pop_value();
+ self.release_temporaries_from(base.first_temporary);
+ let dst = self.temporary();
+ self.put_with_destination(
+ op::GetArgument {
+ dst,
+ index: key,
+ kind: base.name as u8,
+ },
+ dst,
+ );
+ self.push_value(dst, base.first_temporary);
+ return;
+ }
let (key, _) = self.pop_operand();
let (base, first_temporary) = self.pop_operand();
self.release_temporaries_from(first_temporary);
@@ -3148,7 +3219,11 @@ impl Generator<'_> {
});
}
ValKind::InvalidTarget => {}
- ValKind::Value | ValKind::Callee | ValKind::SuperThis | ValKind::BuiltinCallee => {
+ ValKind::Value
+ | ValKind::Callee
+ | ValKind::SuperThis
+ | ValKind::BuiltinCallee
+ | ValKind::FrameArguments => {
unreachable!("this is no target")
}
}
@@ -3275,7 +3350,12 @@ impl Generator<'_> {
});
self.push_value(dst, first_temporary);
}
- ValKind::InvalidTarget | ValKind::Value | ValKind::Callee | ValKind::SuperThis | ValKind::BuiltinCallee => {
+ ValKind::InvalidTarget
+ | ValKind::Value
+ | ValKind::Callee
+ | ValKind::SuperThis
+ | ValKind::BuiltinCallee
+ | ValKind::FrameArguments => {
unreachable!("this is no target")
}
}
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
index 9fb5155f30a..07971832d48 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
@@ -27,6 +27,7 @@ use crate::parser::OutOfStack;
use crate::parser::columns::NONE;
use crate::parser::columns::function_flag;
use crate::parser::columns::tag;
+use crate::parser::token::N_ARGUMENTS;
use crate::parser::token::N_PROTO;
use super::context::Generator;
@@ -125,8 +126,12 @@ impl Generator<'_> {
self.current_source_offset = SourceOffset(position);
match row_tag {
tag::IDENT => {
- let ident = self.identifier_of(Name(a));
- self.value_of_identifier(ident);
+ if let Some(kind) = (a == N_ARGUMENTS).then(|| self.arguments_from_frame()).flatten() {
+ self.value_of_arguments_from_frame(kind);
+ } else {
+ let ident = self.identifier_of(Name(a));
+ self.value_of_identifier(ident);
+ }
}
tag::IDENT_CALLEE => {
let ident = self.identifier_of(Name(a));
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
index 169f49edf75..b8f61df2cf2 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
@@ -45,8 +45,9 @@ use crate::runtime::function_environment::FunctionEnvironment;
use crate::runtime::function_object::FunctionObject;
use crate::runtime::native_javascript_backed_function::NativeJavaScriptBackedFunction;
use crate::runtime::object::{Object, StackFrameInfo};
-use crate::runtime::property_attributes::DEFAULT_ATTRIBUTES;
+use crate::runtime::property_attributes::{Attribute, DEFAULT_ATTRIBUTES, PropertyAttributes};
use crate::runtime::property_key::PropertyKey;
+use crate::runtime::realm::Realm;
use crate::runtime::shared_function_instance_data::{ClassFieldInitializerName, ConstructorKind, FunctionKind};
use crate::utf16::Utf16Display;
@@ -1146,6 +1147,72 @@ pub fn create_arguments(
SlowPathControl::continue_at(pc + op::CreateArguments::LENGTH)
}
+/// `arguments[index]` of a function without an arguments object, for an index that is not one of a passed argument as
+/// an int32. The own properties of an arguments object and properties it would not find at all are read without one;
+/// anything else looks the property up on an arguments object made for this one read.
+pub fn get_argument(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::GetArgument,
+ values: &mut op::GetArgumentValues,
+) -> SlowPathControl {
+ let property_key = asm_try!(vm, pc, values.index.to_property_key(vm));
+ let context = running_execution_context(vm);
+ let passed_arguments = &context.arguments()[..context.passed_argument_count.get() as usize];
+ let is_mapped = instruction.kind == ArgumentsKind::Mapped as u8;
+ let function = || {
+ context
+ .function
+ .get()
+ .expect("an arguments object is created for a function")
+ };
+ let realm = vm.current_realm().expect("there is a current realm");
+ let own_value = if property_key.is_number() {
+ match passed_arguments.get(property_key.as_number() as usize) {
+ Some(argument) => Some(argument.get()),
+ None => object_prototype_value(vm, realm, &property_key),
+ }
+ } else if property_key == vm.names.length {
+ Some(Value::from_f64(passed_arguments.len() as f64))
+ } else if property_key == vm.names.callee {
+ // NB: The callee of an unmapped arguments object is an accessor that throws, which the object made below runs.
+ is_mapped.then(|| Value::from_object(function()))
+ } else if property_key == PropertyKey::from(vm.well_known_symbols().iterator) {
+ Some(Value::from_object(realm.array_prototype_values_function()))
+ } else {
+ object_prototype_value(vm, realm, &property_key)
+ };
+ if let Some(value) = own_value {
+ values.dst = value;
+ return SlowPathControl::continue_at(pc + op::GetArgument::LENGTH);
+ }
+ // FIXME: An accessor on %Object.prototype% gets a new unmapped arguments object as `this` at each read, not one
+ // object for the whole call, and what it writes to that object (such as `this[0]` or `this.length`) does
+ // not reach later reads of `arguments` or, in a function with mapped arguments, the parameters.
+ let arguments_object = create_unmapped_arguments_object(vm, passed_arguments);
+ if is_mapped {
+ arguments_object.define_direct_property(
+ vm,
+ &vm.names.callee,
+ Value::from_object(function()),
+ PropertyAttributes::new(Attribute::WRITABLE | Attribute::CONFIGURABLE),
+ );
+ }
+ values.dst = asm_try!(vm, pc, arguments_object.get(vm, &property_key));
+ SlowPathControl::continue_at(pc + op::GetArgument::LENGTH)
+}
+
+/// What a lookup of `property_key`, which is no own property, gets from %Object.prototype% of `realm`, the prototype of
+/// an arguments object, whose own prototype is null: undefined without the property, the value of a data property, and
+/// `None` for an accessor, which needs a receiver.
+fn object_prototype_value(vm: &Vm, realm: Gc<Realm>, property_key: &PropertyKey) -> Option<Value> {
+ match realm.object_prototype().storage_get(vm, property_key) {
+ None => Some(Value::UNDEFINED),
+ Some(entry) if !entry.value.is_accessor() => Some(entry.value),
+ Some(_) => None,
+ }
+}
+
pub fn new_function(
vm: &Vm,
pc: u32,
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
index b08cdd43c8f..e29ce335bc6 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
@@ -1284,6 +1284,15 @@ impl RuntimeFunctions for Runtime {
calls::create_arguments(vm, pc, instruction, values)
}
+ fn get_argument(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::GetArgument,
+ values: &mut op::GetArgumentValues,
+ ) -> SlowPathControl {
+ calls::get_argument(vm, pc, instruction, values)
+ }
+
fn stack_overflow(vm: &Vm, pc: u32) -> SlowPathControl {
calls::stack_overflow(vm, pc)
}
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rs
index 2474fe73caf..339ad218505 100644
--- a/Libraries/LibJS/Rust/src/parser/base.rs
+++ b/Libraries/LibJS/Rust/src/parser/base.rs
@@ -199,6 +199,9 @@ pub(crate) struct FunctionState {
pub uses_this: bool,
pub uses_this_from_environment: bool,
pub might_need_arguments: bool,
+ pub arguments_other_uses: u32,
+ pub reads_arguments_index: bool,
+ pub may_write_parameters: bool,
pub in_class_of_sloppy_function: bool,
pub is_arrow: bool,
pub summary_ordinal: u32,
@@ -321,6 +324,13 @@ pub struct Parser<'a, U: Unit> {
pub(crate) uses_this: bool,
pub(crate) uses_this_from_environment: bool,
pub(crate) might_need_arguments: bool,
+ /// How many mentions of `arguments` in the function so far are something other than reading `arguments.length` or
+ /// `arguments[key]`, which the function can answer from its frame.
+ pub(crate) arguments_other_uses: u32,
+ /// Whether the function reads `arguments[key]`.
+ pub(crate) reads_arguments_index: bool,
+ /// Whether something in the function may write a binding of the name of one of its parameters.
+ pub(crate) may_write_parameters: bool,
/// Whether the code being parsed is in a class in a function that is not strict. The code of a class is strict, but
/// `arguments` in its heritage and computed keys is the arguments object of that function.
pub(crate) in_class_of_sloppy_function: bool,
@@ -538,6 +548,9 @@ impl<'a, U: Unit> Parser<'a, U> {
uses_this: false,
uses_this_from_environment: false,
might_need_arguments: false,
+ arguments_other_uses: 0,
+ reads_arguments_index: false,
+ may_write_parameters: false,
in_class_of_sloppy_function: false,
in_arrow: false,
@@ -637,6 +650,9 @@ impl<'a, U: Unit> Parser<'a, U> {
self.uses_this = false;
self.uses_this_from_environment = false;
self.might_need_arguments = false;
+ self.arguments_other_uses = 0;
+ self.reads_arguments_index = false;
+ self.may_write_parameters = false;
self.in_arrow = false;
self.cover_error = NONE;
self.potential_arrow_at = NONE;
@@ -1439,6 +1455,65 @@ impl<'a, U: Unit> Parser<'a, U> {
row
}
+ /// Whether `node` is the identifier `arguments`.
+ #[inline(always)]
+ pub(crate) fn is_arguments_ident(&self, node: u32) -> bool {
+ self.ast.tag[node as usize] == tag::IDENT && self.ast.a[node as usize] == N_ARGUMENTS
+ }
+
+ /// The mention of `arguments` that `parse_atom()` counted turned out to be the base of a read of `arguments.length`
+ /// or `arguments[key]`.
+ pub(crate) fn note_arguments_read(&mut self) {
+ debug_assert!(self.arguments_other_uses != 0);
+ self.arguments_other_uses = self.arguments_other_uses.saturating_sub(1);
+ }
+
+ /// The property expression `node` turned out to be something other than read, such as written, deleted or called on
+ /// its base, which takes the arguments object if the base is `arguments`.
+ pub(crate) fn note_arguments_property_not_read(&mut self, node: u32) {
+ if matches!(self.ast.tag[node as usize], tag::MEMBER | tag::INDEX)
+ && self.is_arguments_ident(self.ast.a[node as usize])
+ {
+ self.arguments_other_uses += 1;
+ }
+ }
+
+ /// Like `note_arguments_property_not_read()`, for `node`, which gets called or deleted, through parentheses and from the
+ /// end of an optional chain.
+ pub(crate) fn note_arguments_property_called_or_deleted(&mut self, mut node: u32) {
+ while self.ast.tag[node as usize] == tag::PAREN {
+ node = self.ast.a[node as usize];
+ }
+ if self.ast.tag[node as usize] == tag::CHAIN {
+ node = self.ast.a[node as usize];
+ }
+ self.note_arguments_property_not_read(node);
+ }
+
+ /// Something assigns to the name, which may be a parameter of the function being parsed.
+ pub(crate) fn note_assignment_to_name(&mut self, name: u32) {
+ if self.tracks_scopes && self.is_parameter_of_current_function(name) {
+ self.may_write_parameters = true;
+ }
+ }
+
+ /// Whether the closest function, counting static blocks, declares a parameter of the name.
+ fn is_parameter_of_current_function(&self, name: u32) -> bool {
+ if name as usize >= self.names.len() {
+ return false;
+ }
+ let Some(scope) = self
+ .scopes
+ .iter()
+ .rev()
+ .find(|scope| scope.flags & (scope_flag::FUNCTION | scope_flag::STATIC_BLOCK) != 0)
+ else {
+ return false;
+ };
+ let declaration = self.find_declaration(name, scope.id);
+ declaration != NONE && self.decls[declaration as usize].flags & decl::PARAMETER != 0
+ }
+
/// Whether the name is declared so far in the current function, as far as code that reads `arguments` can tell.
pub(crate) fn is_declared_in_current_function(&self, name: u32) -> bool {
let mut d = self.names.get(name as usize).map_or(NONE, |state| state.decl_head);
@@ -1588,6 +1663,9 @@ impl<'a, U: Unit> Parser<'a, U> {
return;
}
self.name_state(name).stamp |= DECLARED_BIT;
+ if !matches!(bind, Bind::None | Bind::Parameter) && self.is_parameter_of_current_function(name) {
+ self.may_write_parameters = true;
+ }
let top = self.scopes.len() - 1;
let flags = self.scopes[top].flags;
let scope_id = self.scopes[top].id;
@@ -1811,6 +1889,9 @@ impl<'a, U: Unit> Parser<'a, U> {
uses_this: self.uses_this,
uses_this_from_environment: self.uses_this_from_environment,
might_need_arguments: self.might_need_arguments,
+ arguments_other_uses: self.arguments_other_uses,
+ reads_arguments_index: self.reads_arguments_index,
+ may_write_parameters: self.may_write_parameters,
in_class_of_sloppy_function: self.in_class_of_sloppy_function,
is_arrow: self.in_arrow,
summary_ordinal: self.summary_ordinal,
@@ -1819,6 +1900,9 @@ impl<'a, U: Unit> Parser<'a, U> {
self.uses_this = false;
self.uses_this_from_environment = false;
self.might_need_arguments = false;
+ self.arguments_other_uses = 0;
+ self.reads_arguments_index = false;
+ self.may_write_parameters = false;
self.in_arrow = flags & function_flag::ARROW != 0;
self.in_class_of_sloppy_function &= self.in_arrow;
self.ctx = new_ctx;
@@ -1971,6 +2055,15 @@ impl<'a, U: Unit> Parser<'a, U> {
if self.might_need_arguments {
flags |= function_flag::MENTIONS_ARGUMENTS;
}
+ if self.arguments_other_uses == 0 {
+ flags |= function_flag::ARGUMENTS_FROM_FRAME;
+ }
+ if self.reads_arguments_index {
+ flags |= function_flag::READS_ARGUMENTS_INDEX;
+ }
+ if self.may_write_parameters {
+ flags |= function_flag::MAY_WRITE_PARAMETERS;
+ }
(flags, scope_row, body_scope_row)
}
@@ -2029,7 +2122,12 @@ impl<'a, U: Unit> Parser<'a, U> {
self.uses_this_from_environment =
state.uses_this_from_environment || (is_arrow && self.uses_this_from_environment);
// An arrow function has no arguments object, so that what it mentions counts for the function around it.
- self.might_need_arguments = state.might_need_arguments || (is_arrow && self.might_need_arguments);
+ let arrow_might_need_arguments = is_arrow && self.might_need_arguments;
+ self.might_need_arguments = state.might_need_arguments || arrow_might_need_arguments;
+ // NB: What an arrow function reads of `arguments` is not in the frame of the function around it.
+ self.arguments_other_uses = state.arguments_other_uses + u32::from(arrow_might_need_arguments);
+ self.reads_arguments_index = state.reads_arguments_index;
+ self.may_write_parameters = state.may_write_parameters;
self.in_class_of_sloppy_function = state.in_class_of_sloppy_function;
self.in_arrow = state.is_arrow;
@@ -2133,6 +2231,9 @@ impl<'a, U: Unit> Parser<'a, U> {
self.uses_this = false;
self.uses_this_from_environment = false;
self.might_need_arguments = false;
+ self.arguments_other_uses = 0;
+ self.reads_arguments_index = false;
+ self.may_write_parameters = false;
self.in_arrow = false;
self.cover_error = NONE;
self.potential_arrow_at = NONE;
@@ -2259,6 +2360,9 @@ impl<'a, U: Unit> Parser<'a, U> {
self.uses_this = state.uses_this;
self.uses_this_from_environment = state.uses_this_from_environment;
self.might_need_arguments = state.might_need_arguments;
+ self.arguments_other_uses = state.arguments_other_uses;
+ self.reads_arguments_index = state.reads_arguments_index;
+ self.may_write_parameters = state.may_write_parameters;
self.in_arrow = state.is_arrow;
self.ast.push(tag::FUNCTION, state.start, 0, row)
}
diff --git a/Libraries/LibJS/Rust/src/parser/columns.rs b/Libraries/LibJS/Rust/src/parser/columns.rs
index b3a3bc30323..5a1de6f1d9c 100644
--- a/Libraries/LibJS/Rust/src/parser/columns.rs
+++ b/Libraries/LibJS/Rust/src/parser/columns.rs
@@ -463,4 +463,11 @@ pub mod function_flag {
pub const DEFAULT_CONSTRUCTOR: u32 = 1 << 25;
/// A constructor of a class with a superclass, whose `this` is not initialized until it calls super().
pub const DERIVED_CONSTRUCTOR: u32 = 1 << 26;
+ /// Everything the function does with `arguments` is reading `arguments.length` or `arguments[key]`, which the frame
+ /// can answer without an arguments object.
+ pub const ARGUMENTS_FROM_FRAME: u32 = 1 << 27;
+ /// The function reads `arguments[key]`.
+ pub const READS_ARGUMENTS_INDEX: u32 = 1 << 28;
+ /// Something in the function may write a binding of the name of one of its parameters.
+ pub const MAY_WRITE_PARAMETERS: u32 = 1 << 29;
}
diff --git a/Libraries/LibJS/Rust/src/parser/expr.rs b/Libraries/LibJS/Rust/src/parser/expr.rs
index 6835a55c5ab..c32bbd4c46e 100644
--- a/Libraries/LibJS/Rust/src/parser/expr.rs
+++ b/Libraries/LibJS/Rust/src/parser/expr.rs
@@ -276,6 +276,7 @@ impl<U: Unit> Parser<'_, U> {
self.mark_at(tag::MARK_TYPEOF, 0, start);
}
if kind == T::KDelete {
+ self.note_arguments_property_called_or_deleted(argument);
self.mark_at(tag::MARK_DELETE, 0, start);
if self.strict && self.is_local_variable_access(argument) {
self.fail(start, "Deleting a local variable in strict mode");
@@ -388,6 +389,10 @@ impl<U: Unit> Parser<'_, U> {
self.next();
let property = self.parse_expression(false, false);
self.expect(T::RBracket);
+ if self.is_arguments_ident(base) {
+ self.note_arguments_read();
+ self.reads_arguments_index = true;
+ }
let index_tag = if self.marks && self.ast.tag[base as usize] == tag::SUPER {
tag::SUPER_INDEX
} else {
@@ -416,6 +421,7 @@ impl<U: Unit> Parser<'_, U> {
}
let is_eval = self.ast.tag[callee as usize] == tag::IDENT && self.ast.a[callee as usize] == N_EVAL;
let position = if optional_chained { chain_pos } else { operator_pos };
+ self.note_arguments_property_called_or_deleted(base);
self.mark_at(tag::MARK_CALLEE, 0, position);
let arguments = self.parse_arguments(position);
if is_eval {
@@ -444,6 +450,7 @@ impl<U: Unit> Parser<'_, U> {
self.fail(self.tok_start, "Tagged template in an optional chain");
return base;
}
+ self.note_arguments_property_called_or_deleted(base);
self.mark_at(tag::MARK_CALLEE, 0, start);
let subtree = self.begin_subtree();
let quasi = self.parse_template(true);
@@ -462,6 +469,7 @@ impl<U: Unit> Parser<'_, U> {
optional_chained = true;
self.next();
if self.tok == T::LParen {
+ self.note_arguments_property_called_or_deleted(base);
self.mark_at(tag::MARK_CALLEE, 0, chain_pos);
}
self.mark(tag::MARK_OPTIONAL, u32::from(chain_pos == operator_pos));
@@ -536,6 +544,9 @@ impl<U: Unit> Parser<'_, U> {
if optional && self.tok == T::Name && (name == N_ARGUMENTS || name == N_EVAL) {
self.might_need_arguments = true;
}
+ if !optional && self.is_arguments_ident(base) && self.interner.text(name) == b"length" {
+ self.note_arguments_read();
+ }
self.next();
let member_tag = if optional {
tag::OPT_MEMBER
@@ -764,6 +775,8 @@ impl<U: Unit> Parser<'_, U> {
self.check_identifier(name, start);
if name == N_ARGUMENTS || name == N_EVAL {
self.might_need_arguments = true;
+ // NB: A read of `arguments.length` or `arguments[key]` takes this back (see `note_arguments_read()`).
+ self.arguments_other_uses += u32::from(name == N_ARGUMENTS);
if name == N_ARGUMENTS
&& (!self.strict || self.in_class_of_sloppy_function)
&& !self.is_declared_in_current_function(name)
@@ -1231,6 +1244,8 @@ impl<U: Unit> Parser<'_, U> {
}
self.check_identifier(key_name, start);
self.note_use(key_name);
+ // NB: A shorthand property of the name `arguments` reads or, in a pattern, writes the arguments object itself.
+ self.arguments_other_uses += u32::from(key_name == N_ARGUMENTS);
let mut value = self.ast.push(tag::IDENT, start, key_name, 0);
if self.tok == T::Assign {
self.cover_error = self.tok_start;
@@ -1532,17 +1547,21 @@ impl<U: Unit> Parser<'_, U> {
if binding {
self.check_binding_name(name, pos);
self.declare(name, bind, pos);
- } else if self.strict && (name == N_EVAL || name == N_ARGUMENTS) {
- self.fail(
- pos,
- "Binding pattern target may not be called 'arguments' or 'eval' in strict mode",
- );
+ } else {
+ self.note_assignment_to_name(name);
+ if self.strict && (name == N_EVAL || name == N_ARGUMENTS) {
+ self.fail(
+ pos,
+ "Binding pattern target may not be called 'arguments' or 'eval' in strict mode",
+ );
+ }
}
}
tag::MEMBER | tag::INDEX | tag::PRIVATE_MEMBER => {
if binding {
self.fail(pos, "Binding member expression");
}
+ self.note_arguments_property_not_read(node);
}
tag::PAREN => {
if binding {
@@ -1661,6 +1680,7 @@ impl<U: Unit> Parser<'_, U> {
match self.ast.tag[inner as usize] {
tag::IDENT => {
let name = self.ast.a[inner as usize];
+ self.note_assignment_to_name(name);
if self.strict && (name == N_EVAL || name == N_ARGUMENTS) {
self.fail(
pos,
@@ -1668,7 +1688,7 @@ impl<U: Unit> Parser<'_, U> {
);
}
}
- tag::MEMBER | tag::INDEX | tag::PRIVATE_MEMBER => {}
+ tag::MEMBER | tag::INDEX | tag::PRIVATE_MEMBER => self.note_arguments_property_not_read(inner),
tag::CALL if !self.strict && allow_call => {}
_ if self.marks => {}
_ => self.fail(pos, message),
diff --git a/Libraries/LibJS/Rust/src/parser/stmt.rs b/Libraries/LibJS/Rust/src/parser/stmt.rs
index b9e39a74641..7869648a7c3 100644
--- a/Libraries/LibJS/Rust/src/parser/stmt.rs
+++ b/Libraries/LibJS/Rust/src/parser/stmt.rs
@@ -268,6 +268,8 @@ impl<U: Unit> Parser<'_, U> {
let func = self.new_function_id(NONE);
self.summary_rows[func as usize] = summary_row;
self.open_scope_with(scope_flag::WITH, func);
+ // NB: `arguments` in the body may be a property of the object.
+ self.arguments_other_uses += 1;
self.with_depth += 1;
let body = self.parse_statement(Context::Single, false);
self.with_depth -= 1;
@@ -1023,6 +1025,9 @@ impl<U: Unit> Parser<'_, U> {
uses_this: false,
uses_this_from_environment: false,
might_need_arguments: false,
+ arguments_other_uses: 0,
+ reads_arguments_index: false,
+ may_write_parameters: false,
in_class_of_sloppy_function: self.in_class_of_sloppy_function,
is_arrow: false,
summary_ordinal: 0,
diff --git a/Tests/LibJS/Bytecode/expected/arguments-from-frame.txt b/Tests/LibJS/Bytecode/expected/arguments-from-frame.txt
new file mode 100644
index 00000000000..f23816768e8
--- /dev/null
+++ b/Tests/LibJS/Bytecode/expected/arguments-from-frame.txt
@@ -0,0 +1,103 @@
+$bb66ea92 arguments-from-frame.js:23:1
+ Registers: 7
+ Blocks: 1
+ Constants:
+ [0] = Undefined
+
+block0:
+ [ 0] Enter
+ [ 1] Mov dst:reg5, src:Undefined
+ [ 4] GetGlobal value_feedback:0, dst:reg6, `count`
+ [ c] Call call_feedback:0, dst:reg5, callee:reg6, this_value:Undefined
+ [ 13] GetGlobal value_feedback:1, dst:reg6, `beforeArrow`
+ [ 1b] Call call_feedback:1, dst:reg5, callee:reg6, this_value:Undefined
+ [ 22] GetGlobal value_feedback:2, dst:reg6, `sloppy`
+ [ 2a] Call call_feedback:2, dst:reg5, callee:reg6, this_value:Undefined
+ [ 31] GetGlobal value_feedback:3, dst:reg6, `strict`
+ [ 39] Call call_feedback:3, dst:reg5, callee:reg6, this_value:Undefined
+ [ 40] GetGlobal value_feedback:4, dst:reg6, `writesParameter`
+ [ 48] Call call_feedback:4, dst:reg5, callee:reg6, this_value:Undefined
+ [ 4f] End value:reg5
+
+
+count$2e49fd9c arguments-from-frame.js:2:5
+ Registers: 6
+ Blocks: 1
+
+block0:
+ [ 0] Enter
+ [ 1] GetArgumentCount dst:reg5
+ [ 3] Return value:reg5
+
+
+beforeArrow$67dfce48 arguments-from-frame.js:20:11
+ Registers: 6
+ Blocks: 1
+
+block0:
+ [ 0] Enter
+ [ 1] CreateVariable `count`, is_immutable:true, is_global:false, is_strict:true
+ [ 8] GetArgumentCount dst:reg5
+ [ a] InitializeLexicalBinding `count`, src:reg5
+ [ 11] NewFunction dst:reg5, shared_function_data_index:0
+ [ 18] Return value:reg5
+
+
+sloppy$28c325fe arguments-from-frame.js:5:7
+ Registers: 6
+ Blocks: 4
+ Locals: s~0, i~1
+ Constants:
+ [0] = Int32(1)
+ [1] = Int32(0)
+
+block0:
+ [ 0] Enter
+ [ 1] Mov dst:arg0, src:Int32(1)
+ [ 4] Mov dst:s~0, src:Int32(0)
+ [ 7] Mov dst:i~1, src:Int32(0)
+ [ a] Jump target:block2
+
+block1:
+ [ f] GetArgument dst:reg5, index:i~1
+ [ 13] Add arith_feedback:0, dst:s~0, lhs:s~0, rhs:reg5
+ [ 19] Increment arith_feedback:1, dst:i~1
+
+block2:
+ [ 1d] GetArgumentCount dst:reg5
+ [ 1f] JumpLessThanLoop arith_feedback:2, lhs:i~1, rhs:reg5, true_target:block1, false_target:block3
+
+block3:
+ [ 2c] GetArgument dst:reg5, index:Int32(0)
+ [ 30] Add arith_feedback:3, dst:reg5, lhs:s~0, rhs:reg5
+ [ 36] Return value:reg5
+
+
+strict$26eb3520 arguments-from-frame.js:12:5
+ Registers: 7
+ Blocks: 1
+ Constants:
+ [0] = Int32(0)
+
+block0:
+ [ 0] Enter
+ [ 1] GetArgument dst:reg5, index:Int32(0)
+ [ 5] GetArgumentCount dst:reg6
+ [ 7] Add arith_feedback:0, dst:reg5, lhs:reg5, rhs:reg6
+ [ d] Return value:reg5
+
+
+writesParameter$d1c7262e arguments-from-frame.js:16:7
+ Registers: 6
+ Blocks: 1
+ Locals: arguments~0
+ Constants:
+ [0] = Int32(1)
+ [1] = Int32(0)
+
+block0:
+ [ 0] Enter
+ [ 1] CreateArguments dst:arguments~0, is_immutable:true, creates_parameter_bindings:false
+ [ 6] Mov dst:arg0, src:Int32(1)
+ [ 9] GetByValue keyed_feedback:0, dst:reg5, base:arguments~0, property:Int32(0)
+ [ 11] Return value:reg5
diff --git a/Tests/LibJS/Bytecode/expected/mapped-arguments-parameter-bindings.txt b/Tests/LibJS/Bytecode/expected/mapped-arguments-parameter-bindings.txt
index cf1c05fe0dc..e2308c403a6 100644
--- a/Tests/LibJS/Bytecode/expected/mapped-arguments-parameter-bindings.txt
+++ b/Tests/LibJS/Bytecode/expected/mapped-arguments-parameter-bindings.txt
@@ -30,7 +30,7 @@ block0:
[ 14] InitializeLexicalBinding `b`, src:Undefined
[ 1b] SetLexicalBinding `b`, src:arg1
[ 22] SetLexicalBinding `a`, src:arg2
- [ 29] GetLength keyed_feedback:0, dst:reg5, base:arguments~0
+ [ 29] GetById dst:reg5, base:arguments~0, `foo`
[ 30] GetBinding value_feedback:0, dst:reg6, `a`
[ 39] Add arith_feedback:0, dst:reg5, lhs:reg5, rhs:reg6
[ 3f] GetBinding value_feedback:1, dst:reg6, `b`
diff --git a/Tests/LibJS/Bytecode/input/arguments-from-frame.js b/Tests/LibJS/Bytecode/input/arguments-from-frame.js
new file mode 100644
index 00000000000..bd20dcc92c4
--- /dev/null
+++ b/Tests/LibJS/Bytecode/input/arguments-from-frame.js
@@ -0,0 +1,27 @@
+function count(a, b) {
+ return arguments.length;
+}
+function sloppy(a) {
+ a = 1;
+ let s = 0;
+ for (let i = 0; i < arguments.length; ++i) s += arguments[i];
+ return s + arguments[0];
+}
+function strict(a) {
+ "use strict";
+ return arguments[0] + arguments.length;
+}
+function writesParameter(a) {
+ "use strict";
+ a = 1;
+ return arguments[0];
+}
+function beforeArrow(a) {
+ const count = arguments.length;
+ return () => count;
+}
+count();
+beforeArrow();
+sloppy();
+strict();
+writesParameter();
diff --git a/Tests/LibJS/Bytecode/input/mapped-arguments-parameter-bindings.js b/Tests/LibJS/Bytecode/input/mapped-arguments-parameter-bindings.js
index b3a09d92341..ddc114e5754 100644
--- a/Tests/LibJS/Bytecode/input/mapped-arguments-parameter-bindings.js
+++ b/Tests/LibJS/Bytecode/input/mapped-arguments-parameter-bindings.js
@@ -1,4 +1,4 @@
function forward(a, b, a) {
- return arguments.length + a + b;
+ return arguments.foo + a + b;
}
forward(1, 2, 3);