Skip to content

Truly independent web browser

LibJS: Forward arguments of default derived constructors directly

Andreas Klingd5c33c44cedc
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index 6824b6c7754..47181a22d76 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -163,6 +163,7 @@ asm_dispatch_table:
.quad asm_handler_CallConstructWithArgumentArray
.quad asm_handler_SuperCallWithArgumentArray
.quad asm_handler_SuperCall
+ .quad asm_handler_SuperCallWithForwardedArguments
.quad asm_handler_BindThisFromSuperCall
.quad asm_handler_NewObject
.quad asm_handler_NewObjectWithNoPrototype
@@ -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):
@@ -1133,6 +1133,10 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_SuperCall_hot_end
.quad asm_handler_SuperCall_cold
.quad asm_handler_SuperCall_cold_end
+ .quad asm_handler_SuperCallWithForwardedArguments
+ .quad asm_handler_SuperCallWithForwardedArguments_hot_end
+ .quad asm_handler_SuperCallWithForwardedArguments_cold
+ .quad asm_handler_SuperCallWithForwardedArguments_cold_end
.quad asm_handler_BindThisFromSuperCall
.quad asm_handler_BindThisFromSuperCall_hot_end
.quad asm_handler_BindThisFromSuperCall_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
@@ -16709,6 +16709,49 @@ asm_handler_SuperCall_hot_end:
asm_handler_SuperCall_cold:
asm_handler_SuperCall_cold_end:
+.p2align 4
+asm_handler_SuperCallWithForwardedArguments:
+ mov x0, x20
+ sub w1, w21, w26
+ str w1, [x28, #64]
+ ldrb w9, [x21, #4]
+ ldr x10, [x27, x9, lsl #3]
+.Lasm_SuperCallWithForwardedArguments.scalar_value_ready_0:
+ mov x3, x10
+ mov x0, x20
+ sub w1, w21, w26
+ mov x2, x21
+ bl CSYM(asm_slow_path_super_call_with_forwarded_arguments)
+ tbnz x0, #63, .Lasm_SuperCallWithForwardedArguments.scalar_result_done_1
+ tbz x0, #32, .Lasm_SuperCallWithForwardedArguments.scalar_result_done_1
+ ldrb w9, [x21, #3]
+ str x1, [x27, x9, lsl #3]
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_SuperCallWithForwardedArguments.scalar_result_done_1:
+ tbz x0, #63, 1f
+ b .Lexit
+1:
+ tbz x0, #32, .Lasm_SuperCallWithForwardedArguments.slow_path_transfer_2
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_SuperCallWithForwardedArguments.slow_path_transfer_2:
+ 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_SuperCallWithForwardedArguments_hot_end:
+asm_handler_SuperCallWithForwardedArguments_cold:
+asm_handler_SuperCallWithForwardedArguments_cold_end:
+
.p2align 4
asm_handler_BindThisFromSuperCall:
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 b3e8f746103..62b0942cade 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -164,6 +164,7 @@ asm_dispatch_table:
.quad asm_handler_CallConstructWithArgumentArray
.quad asm_handler_SuperCallWithArgumentArray
.quad asm_handler_SuperCall
+ .quad asm_handler_SuperCallWithForwardedArguments
.quad asm_handler_BindThisFromSuperCall
.quad asm_handler_NewObject
.quad asm_handler_NewObjectWithNoPrototype
@@ -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):
@@ -1134,6 +1134,10 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_SuperCall_hot_end
.quad asm_handler_SuperCall_cold
.quad asm_handler_SuperCall_cold_end
+ .quad asm_handler_SuperCallWithForwardedArguments
+ .quad asm_handler_SuperCallWithForwardedArguments_hot_end
+ .quad asm_handler_SuperCallWithForwardedArguments_cold
+ .quad asm_handler_SuperCallWithForwardedArguments_cold_end
.quad asm_handler_BindThisFromSuperCall
.quad asm_handler_BindThisFromSuperCall_hot_end
.quad asm_handler_BindThisFromSuperCall_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
@@ -16269,6 +16269,49 @@ asm_handler_SuperCall_hot_end:
asm_handler_SuperCall_cold:
asm_handler_SuperCall_cold_end:
+.p2align 4
+asm_handler_SuperCallWithForwardedArguments:
+ mov DWORD PTR [rbx + -72], r13d
+ lea r11, [r14 + r13]
+ movzx r10d, BYTE PTR [r11 + 4]
+ mov rax, QWORD PTR [rbx + r10 * 8]
+.Lasm_SuperCallWithForwardedArguments.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_super_call_with_forwarded_arguments)
+ test rax, rax
+ js .Lasm_SuperCallWithForwardedArguments.scalar_result_done_1
+ bt rax, 32
+ jnc .Lasm_SuperCallWithForwardedArguments.scalar_result_done_1
+ lea rcx, [r14 + r13]
+ movzx r10d, BYTE PTR [rcx + 3]
+ mov QWORD PTR [rbx + r10 * 8], rdx
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_SuperCallWithForwardedArguments.scalar_result_done_1:
+ test rax, rax
+ js .Lexit
+ bt rax, 32
+ jnc .Lasm_SuperCallWithForwardedArguments.slow_path_transfer_2
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_SuperCallWithForwardedArguments.slow_path_transfer_2:
+ 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_SuperCallWithForwardedArguments_hot_end:
+asm_handler_SuperCallWithForwardedArguments_cold:
+asm_handler_SuperCallWithForwardedArguments_cold_end:
+
.p2align 4
asm_handler_BindThisFromSuperCall:
mov DWORD PTR [rbx + -72], r13d
diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flap
index cd6b2a2d8f1..b521a7f11f5 100644
--- a/Libraries/LibJS/Interpreter/interpreter.flap
+++ b/Libraries/LibJS/Interpreter/interpreter.flap
@@ -4402,8 +4402,7 @@ handler SuperCallWithArgumentArray(
call_feedback: CallFeedbackIndex,
dst: out Operand,
super_constructor: Operand,
- arguments: Operand,
- is_synthetic: bool
+ arguments: Operand
) @cold = call_slow_path(super_call_with_argument_array);
handler SuperCall(
@@ -4414,6 +4413,12 @@ handler SuperCall(
arguments: Operand[]
) @cold = call_slow_path(super_call);
+handler SuperCallWithForwardedArguments(
+ call_feedback: CallFeedbackIndex,
+ dst: out Operand,
+ super_constructor: Operand
+) @cold = call_slow_path(super_call_with_forwarded_arguments);
+
handler BindThisFromSuperCall(
result: Operand
) @cold = call_slow_path(bind_this_from_super_call);
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
index 4c54e5f9713..0be8c366ca8 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
@@ -133,7 +133,9 @@ pub fn handling(opcode: OpCode) -> Handling {
| O::SuperCallWithArgumentArray
| O::SuperCall => GENERIC_CALL,
O::BindThisFromSuperCall => GENERIC,
- O::CallDirectEval | O::CallDirectEvalWithArgumentArray => GENERIC_CALL_READS_WHOLE_FRAME,
+ O::CallDirectEval | O::CallDirectEvalWithArgumentArray | O::SuperCallWithForwardedArguments => {
+ GENERIC_CALL_READS_WHOLE_FRAME
+ }
O::CreateArguments | O::CreateRestParams | O::Debugger => GENERIC_READS_WHOLE_FRAME,
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index 7dd31269006..a1e246a5095 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -154,6 +154,7 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool) -> bool {
| OpCode::GetSuperConstructor
| OpCode::SuperCallWithArgumentArray
| OpCode::SuperCall
+ | OpCode::SuperCallWithForwardedArguments
| OpCode::BindThisFromSuperCall
);
// NB: The base of `super` property access is built from the callee's home object (see
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
index 6d470f1992b..d740ebc9872 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/constructs.rs
@@ -599,22 +599,18 @@ impl Generator<'_> {
if !is_derived {
return;
}
- let arguments = self.temporary();
- self.put(op::CreateRestParams {
- dst: arguments,
- rest_index: 0,
- });
+ // NB: The arguments go to the super constructor as they are, without the array iterator that
+ // `super(...args)` would use.
let super_constructor = self.temporary();
self.put(op::GetSuperConstructor { dst: super_constructor });
let dst = self.temporary();
let call_feedback = self.next_call_feedback();
- self.put(op::SuperCallWithArgumentArray {
+ self.put(op::SuperCallWithForwardedArguments {
call_feedback,
dst,
super_constructor,
- arguments,
- is_synthetic: true,
});
+ self.put(op::BindThisFromSuperCall { result: dst });
self.put(op::Return { value: dst });
}
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index 6cc9ee7d8da..bd47d983ae3 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -2051,7 +2051,6 @@ impl Generator<'_> {
dst,
super_constructor,
arguments,
- is_synthetic: false,
},
dst,
);
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
index 09c9a5fa47d..97ea2d1c5c9 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
@@ -7,12 +7,14 @@
//! The slow paths for calls, function and class creation, and their helpers.
use ak::ScopeGuard;
+use core::ops::Deref;
use libjs_abi::{ArgumentsKind, FunctionNamePrefix};
use super::strict_at;
use crate::bytecode::encoding::OptionalOperand;
use crate::bytecode::op;
use crate::bytecode::property_access::Strict;
+use crate::gc::root::{MarkedValues, MarkedVec};
use crate::interpreter::runtime_functions::{
SlowPathControl, asm_try, handle_asm_exception, unimplemented_runtime_function,
};
@@ -24,8 +26,7 @@ use crate::layout::feedback::CallFeedbackForwarding;
use crate::layout::value::Value;
use crate::runtime::abstract_operations::{
self, CallerMode, create_mapped_arguments_object, create_unmapped_arguments_object, function_object_as_object,
- function_with_frame_as_this_environment, get_prototype_from_constructor, get_this_environment,
- length_of_array_like, perform_eval,
+ function_with_frame_as_this_environment, get_prototype_from_constructor, get_this_environment, perform_eval,
};
use crate::runtime::array::Array;
use crate::runtime::bound_function::BoundFunction;
@@ -686,31 +687,19 @@ pub fn call_construct_with_argument_array(
pub fn super_call_with_argument_array(
vm: &Vm,
pc: u32,
- instruction: &op::SuperCallWithArgumentArray,
+ _instruction: &op::SuperCallWithArgumentArray,
values: &mut op::SuperCallWithArgumentArrayValues,
) -> SlowPathControl {
let argument_array = as_array_exotic_object(values.arguments);
- // NB: The default derived constructor reads its arguments without the array iterator, which a changed
- // %Array.prototype%[@@iterator] must not see.
- let argument_array_length = if instruction.is_synthetic {
- length_of_array_like(vm, &argument_array).must()
- } else {
- u64::from(argument_array.indexed_array_like_size())
- };
- let argument_count = u32::try_from(argument_array_length).expect("the argument count fits in u32");
- let is_synthetic = instruction.is_synthetic;
+ let argument_count = argument_array.indexed_array_like_size();
let result = match construct_for_super_call(vm, pc, values.super_constructor, argument_count, |slots| {
for (index, slot) in slots.iter().enumerate() {
- if is_synthetic {
- slot.set(argument_array.get_without_side_effects(vm, &PropertyKey::from_number(index as u64)));
- } else {
- let index = u32::try_from(index).expect("the argument index fits in u32");
- slot.set(
- argument_array
- .indexed_get(index)
- .map_or(Value::UNDEFINED, |value| value.value),
- );
- }
+ let index = u32::try_from(index).expect("the argument index fits in u32");
+ slot.set(
+ argument_array
+ .indexed_get(index)
+ .map_or(Value::UNDEFINED, |value| value.value),
+ );
}
}) {
Ok(result) => result,
@@ -743,6 +732,29 @@ pub fn super_call(
)
}
+/// Steps 1, 2, 5 and 6 of the evaluation of `super(...)` for the SuperCallWithForwardedArguments instruction of a
+/// default derived constructor, which passes on the arguments of the running frame.
+pub fn super_call_with_forwarded_arguments(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::SuperCallWithForwardedArguments,
+ values: &mut op::SuperCallWithForwardedArgumentsValues,
+) -> SlowPathControl {
+ let frame = running_execution_context(vm);
+ let mut storage = ArgumentStorage::empty();
+ storage.fill_from_passed_arguments(vm, frame);
+ let arguments = storage.values();
+ super_call_with_arguments(
+ vm,
+ pc,
+ pc + op::SuperCallWithForwardedArguments::LENGTH,
+ instruction.dst.0.into(),
+ values.super_constructor,
+ &arguments,
+ &mut values.dst,
+ )
+}
+
fn super_call_with_arguments(
vm: &Vm,
pc: u32,
@@ -1213,3 +1225,96 @@ pub fn handle_raw_native_exception(vm: &Vm, exception: Value) -> SlowPathControl
vm.unwind_inline_frame_for_exception();
handle_asm_exception(vm, caller_pc - 1, exception)
}
+
+/// The argument values of a call, which it keeps alive: few enough on the stack, which the garbage collector scans,
+/// and others in a rooted list.
+///
+/// NB: The argument values cannot stay alive in the slots of the calling frame: frames of inlined calls that compiled
+/// code pushes for a call are not initialized, and the garbage collector does not visit their registers and locals.
+pub(crate) struct ArgumentStorage<'vm> {
+ inline: [Value; ArgumentStorage::INLINE_CAPACITY],
+ count: usize,
+ rooted: Option<MarkedVec<'vm, Value>>,
+}
+
+impl<'vm> ArgumentStorage<'vm> {
+ const INLINE_CAPACITY: usize = 16;
+
+ /// Storage without values, which `fill()` puts in. They go in where the storage is, and not into storage that is
+ /// moved there: a move leaves its source behind on the stack, where nothing clears it (see `drop()`).
+ pub(crate) fn empty() -> Self {
+ Self {
+ inline: [Value::UNDEFINED; Self::INLINE_CAPACITY],
+ count: 0,
+ rooted: None,
+ }
+ }
+
+ pub(crate) fn fill_from(&mut self, vm: &'vm Vm, values: impl ExactSizeIterator<Item = Value>) {
+ self.fill(vm, values.len(), |slots| {
+ for (slot, value) in slots.iter_mut().zip(values) {
+ *slot = value;
+ }
+ });
+ }
+
+ /// Takes the arguments passed to the frame `frame`.
+ pub(crate) fn fill_from_passed_arguments(&mut self, vm: &'vm Vm, frame: &ExecutionContext) {
+ let passed_arguments = &frame.arguments()[..frame.passed_argument_count.get() as usize];
+ self.fill_from(vm, passed_arguments.iter().map(|argument| argument.get()));
+ }
+
+ /// Takes `count` values, which `fill` writes into the slots it gets. Nothing may collect garbage while it does.
+ pub(crate) fn fill(&mut self, vm: &'vm Vm, count: usize, fill: impl FnOnce(&mut [Value])) {
+ debug_assert!(self.count == 0 && self.rooted.is_none(), "the storage is filled once");
+ self.count = count;
+ if count <= Self::INLINE_CAPACITY {
+ fill(&mut self.inline[..count]);
+ } else {
+ let rooted = MarkedVec::with_capacity(vm, count);
+ rooted.resize(count, Value::UNDEFINED);
+ fill(&mut rooted.values_mut());
+ self.rooted = Some(rooted);
+ }
+ }
+
+ pub(crate) fn values(&self) -> ArgumentValues<'_> {
+ match &self.rooted {
+ Some(rooted) => ArgumentValues::Rooted(rooted.values()),
+ None => ArgumentValues::Inline(&self.inline[..self.count]),
+ }
+ }
+}
+
+impl Drop for ArgumentStorage<'_> {
+ /// Clears the values on the stack once the call is done with them. The garbage collector scans the stack
+ /// conservatively, and frames that do not initialize all of their slots come to lie over this one later: a value
+ /// left behind would keep its cell alive for as long as such a frame is there.
+ fn drop(&mut self) {
+ if self.rooted.is_some() {
+ return;
+ }
+ for slot in &mut self.inline[..self.count] {
+ // NB: The writes are volatile so that they are not taken for dead stores.
+ // SAFETY: The slot is a valid, aligned value of this storage.
+ unsafe { core::ptr::write_volatile(slot, Value::UNDEFINED) };
+ }
+ }
+}
+
+/// The values of an ArgumentStorage, which it lends.
+pub(crate) enum ArgumentValues<'a> {
+ Inline(&'a [Value]),
+ Rooted(MarkedValues<'a, Value>),
+}
+
+impl Deref for ArgumentValues<'_> {
+ type Target = [Value];
+
+ fn deref(&self) -> &[Value] {
+ match self {
+ Self::Inline(values) => values,
+ Self::Rooted(values) => values,
+ }
+ }
+}
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
index 503ce99764e..b08cdd43c8f 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
@@ -1403,6 +1403,22 @@ impl RuntimeFunctions for Runtime {
super_call_slow_path(vm, pc, instruction, values, arguments)
}
+ fn super_call_with_forwarded_arguments(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::SuperCallWithForwardedArguments,
+ values: &mut op::SuperCallWithForwardedArgumentsValues,
+ ) -> SlowPathControl {
+ feedback::record_call(
+ vm,
+ instruction.call_feedback,
+ values.super_constructor,
+ call_feedback_flags::SAW_CONSTRUCT,
+ );
+ let control = calls::super_call_with_forwarded_arguments(vm, pc, instruction, values);
+ feedback::record_call_value_after(vm, control, instruction.call_feedback, || values.dst)
+ }
+
fn bind_this_from_super_call(
vm: &Vm,
pc: u32,
diff --git a/Libraries/LibJS/Rust/src/jit/calls.rs b/Libraries/LibJS/Rust/src/jit/calls.rs
index 5ac2ea42755..4b14cc2c8da 100644
--- a/Libraries/LibJS/Rust/src/jit/calls.rs
+++ b/Libraries/LibJS/Rust/src/jit/calls.rs
@@ -7,7 +7,6 @@
//! Calls from JIT code: the call instructions it leaves to the runtime, and the runtime functions its direct calls,
//! the call stub and its call intrinsics use.
-use core::ops::Deref;
use core::ptr::NonNull;
use libjs_abi::register;
@@ -22,11 +21,11 @@ use crate::bytecode::executable::Executable;
use crate::bytecode::instruction::OpCode;
use crate::bytecode::instruction_at;
use crate::bytecode::op;
-use crate::gc::root::{MarkedValues, MarkedVec};
+use crate::gc::root::MarkedVec;
use crate::interpreter::runtime_functions::{Runtime, RuntimeFunctions, SlowPathControl, handle_asm_exception};
use crate::interpreter::slow_paths;
use crate::interpreter::slow_paths::calls::{
- CallType, execute_asm_call, record_callback, throw_error, throw_if_needed_for_asm_call,
+ ArgumentStorage, CallType, execute_asm_call, record_callback, throw_error, throw_if_needed_for_asm_call,
};
use crate::interpreter::slow_paths::feedback::record_forwarded_call;
use crate::interpreter::slow_paths::property_access::{CachedAccessors, try_get_by_id_cache_with};
@@ -551,93 +550,6 @@ fn call_from_jit_or_generic(
site.store_result(result)
}
-/// The argument values of a call from JIT code, which it keeps alive: few enough on the stack, which the garbage
-/// collector scans, and others in a rooted list.
-///
-/// NB: The argument values cannot stay alive in the slots of the calling frame: frames of inlined calls that compiled
-/// code pushes for a call are not initialized, and the garbage collector does not visit their registers and locals.
-struct ArgumentStorage<'vm> {
- inline: [Value; ArgumentStorage::INLINE_CAPACITY],
- count: usize,
- rooted: Option<MarkedVec<'vm, Value>>,
-}
-
-impl<'vm> ArgumentStorage<'vm> {
- const INLINE_CAPACITY: usize = 16;
-
- /// Storage without values, which `fill()` puts in. They go in where the storage is, and not into storage that is
- /// moved there: a move leaves its source behind on the stack, where nothing clears it (see `drop()`).
- fn empty() -> Self {
- Self {
- inline: [Value::UNDEFINED; Self::INLINE_CAPACITY],
- count: 0,
- rooted: None,
- }
- }
-
- fn fill_from(&mut self, vm: &'vm Vm, values: impl ExactSizeIterator<Item = Value>) {
- self.fill(vm, values.len(), |slots| {
- for (slot, value) in slots.iter_mut().zip(values) {
- *slot = value;
- }
- });
- }
-
- /// Takes `count` values, which `fill` writes into the slots it gets. Nothing may collect garbage while it does.
- fn fill(&mut self, vm: &'vm Vm, count: usize, fill: impl FnOnce(&mut [Value])) {
- debug_assert!(self.count == 0 && self.rooted.is_none(), "the storage is filled once");
- self.count = count;
- if count <= Self::INLINE_CAPACITY {
- fill(&mut self.inline[..count]);
- } else {
- let rooted = MarkedVec::with_capacity(vm, count);
- rooted.resize(count, Value::UNDEFINED);
- fill(&mut rooted.values_mut());
- self.rooted = Some(rooted);
- }
- }
-
- fn values(&self) -> ArgumentValues<'_> {
- match &self.rooted {
- Some(rooted) => ArgumentValues::Rooted(rooted.values()),
- None => ArgumentValues::Inline(&self.inline[..self.count]),
- }
- }
-}
-
-impl Drop for ArgumentStorage<'_> {
- /// Clears the values on the stack once the call is done with them. The garbage collector scans the stack
- /// conservatively, and frames that do not initialize all of their slots come to lie over this one later: a value
- /// left behind would keep its cell alive for as long as such a frame is there.
- fn drop(&mut self) {
- if self.rooted.is_some() {
- return;
- }
- for slot in &mut self.inline[..self.count] {
- // NB: The writes are volatile so that they are not taken for dead stores.
- // SAFETY: The slot is a valid, aligned value of this storage.
- unsafe { core::ptr::write_volatile(slot, Value::UNDEFINED) };
- }
- }
-}
-
-/// The values of an ArgumentStorage, which it lends.
-enum ArgumentValues<'a> {
- Inline(&'a [Value]),
- Rooted(MarkedValues<'a, Value>),
-}
-
-impl Deref for ArgumentValues<'_> {
- type Target = [Value];
-
- fn deref(&self) -> &[Value] {
- match self {
- Self::Inline(values) => values,
- Self::Rooted(values) => values,
- }
- }
-}
-
/// Runs the generic slow path of a call instruction, and stores its result in the frame if the call returned normally.
/// A callee frame the slow path pushed for the interpreter to run runs to completion here, since the frame's JIT code
/// waits for the call.
@@ -748,28 +660,8 @@ pub unsafe extern "C" fn libjs_jit_call(vm: *const Vm, frame: *mut ExecutionCont
storage.fill_from(vm, operands.iter().map(|operand| load(*operand)));
let arguments = storage.values();
let super_constructor = load(call.super_constructor);
- if let Some(function) = value_as_ecmascript_function_object(super_constructor)
- && function.kind() == FunctionKind::Normal
- && function.has_constructor()
- && function.bytecode_executable().is_some()
- {
- // NB: Steps 1 and 2 of the evaluation of `super(...)`. BindThisFromSuperCall does steps 7 and later.
- let new_target = vm.get_new_target();
- assert!(new_target.is_object());
- slow_paths::feedback::record_call(
- vm,
- call.call_feedback,
- super_constructor,
- crate::bytecode::feedback::call_feedback_flags::SAW_CONSTRUCT,
- );
- return construct_ecmascript_function_from_jit(
- vm,
- &site,
- function,
- new_target.as_function(),
- &arguments,
- )
- .0;
+ if let Some(control) = super_call_from_jit(vm, &site, call.call_feedback, super_constructor, &arguments) {
+ return control.0;
}
let mut values = op::SuperCallValues {
dst: Value::EMPTY,
@@ -779,6 +671,23 @@ pub unsafe extern "C" fn libjs_jit_call(vm: *const Vm, frame: *mut ExecutionCont
let control = slow_paths::super_call_slow_path(vm, pc, call, &mut values, &arguments);
finish_generic_call(vm, &site, control, values.dst)
}
+ _ if opcode == OpCode::SuperCallWithForwardedArguments as u8 => {
+ let call = instruction!(SuperCallWithForwardedArguments);
+ let site = site(call.dst, op::SuperCallWithForwardedArguments::LENGTH);
+ let mut storage = ArgumentStorage::empty();
+ storage.fill_from_passed_arguments(vm, frame_ref);
+ let arguments = storage.values();
+ let super_constructor = load(call.super_constructor);
+ if let Some(control) = super_call_from_jit(vm, &site, call.call_feedback, super_constructor, &arguments) {
+ return control.0;
+ }
+ let mut values = op::SuperCallWithForwardedArgumentsValues {
+ dst: Value::EMPTY,
+ super_constructor,
+ };
+ let control = Runtime::super_call_with_forwarded_arguments(vm, pc, call, &mut values);
+ finish_generic_call(vm, &site, control, values.dst)
+ }
_ if opcode == OpCode::CallDirectEval as u8 => {
let call = instruction!(CallDirectEval);
let site = site(call.dst, call.length());
@@ -847,6 +756,40 @@ pub unsafe extern "C" fn libjs_jit_call(vm: *const Vm, frame: *mut ExecutionCont
control.0
}
+/// Steps 1, 2, 5 and 6 of the evaluation of `super(...)` from JIT code, for a super constructor that is an ECMAScript
+/// function: it constructs in an inline frame linked to the call site's frame, like a construct. BindThisFromSuperCall
+/// does steps 7 and later. Returns nothing for other super constructors.
+fn super_call_from_jit(
+ vm: &Vm,
+ site: &JitCallSite,
+ call_feedback: u16,
+ super_constructor: Value,
+ arguments: &[Value],
+) -> Option<SlowPathControl> {
+ if let Some(function) = value_as_ecmascript_function_object(super_constructor)
+ && function.kind() == FunctionKind::Normal
+ && function.has_constructor()
+ && function.bytecode_executable().is_some()
+ {
+ let new_target = vm.get_new_target();
+ assert!(new_target.is_object());
+ slow_paths::feedback::record_call(
+ vm,
+ call_feedback,
+ super_constructor,
+ crate::bytecode::feedback::call_feedback_flags::SAW_CONSTRUCT,
+ );
+ return Some(construct_ecmascript_function_from_jit(
+ vm,
+ site,
+ function,
+ new_target.as_function(),
+ arguments,
+ ));
+ }
+ None
+}
+
/// `g(...arguments)`, built as NewArray, ArrayAppend of the arguments object the code never created and
/// CallWithArgumentArray at pc.
fn call_spreading_arguments_from_jit(
diff --git a/Tests/LibJS/Bytecode/expected/super-call-spread-register-order.txt b/Tests/LibJS/Bytecode/expected/super-call-spread-register-order.txt
index 60d7e291a8a..4335449c444 100644
--- a/Tests/LibJS/Bytecode/expected/super-call-spread-register-order.txt
+++ b/Tests/LibJS/Bytecode/expected/super-call-spread-register-order.txt
@@ -20,7 +20,7 @@ block0:
[ 48] End value:reg5
-C$ff78eb2c super-call-spread-register-order.js:2:17
+C$4fa52a3d super-call-spread-register-order.js:2:17
Registers: 7
Blocks: 1
Constants:
@@ -32,5 +32,5 @@ block0:
[ 7] GetSuperConstructor dst:reg5
[ 9] NewArray dst:reg6
[ c] ArrayAppend dst:reg6, src:arg0, is_spread:true
- [ 10] SuperCallWithArgumentArray call_feedback:0, dst:reg5, super_constructor:reg5, arguments:reg6, is_synthetic:false
- [ 17] End value:Undefined
+ [ 10] SuperCallWithArgumentArray call_feedback:0, dst:reg5, super_constructor:reg5, arguments:reg6
+ [ 16] End value:Undefined
diff --git a/Tests/LibJS/Bytecode/expected/super-evaluation-order.txt b/Tests/LibJS/Bytecode/expected/super-evaluation-order.txt
index 6dab329a35f..985564a2d8b 100644
--- a/Tests/LibJS/Bytecode/expected/super-evaluation-order.txt
+++ b/Tests/LibJS/Bytecode/expected/super-evaluation-order.txt
@@ -33,16 +33,16 @@ block0:
[ 91] End value:reg5
-A$53ad7806 super-evaluation-order.js:1:1
- Registers: 8
+A$f6c9e738 super-evaluation-order.js:1:1
+ Registers: 7
Blocks: 1
block0:
[ 0] Enter
- [ 1] CreateRestParams dst:reg5, rest_index:0
- [ 7] GetSuperConstructor dst:reg6
- [ 9] SuperCallWithArgumentArray call_feedback:0, dst:reg7, super_constructor:reg6, arguments:reg5, is_synthetic:true
- [ 10] Return value:reg7
+ [ 1] GetSuperConstructor dst:reg5
+ [ 3] SuperCallWithForwardedArguments call_feedback:0, dst:reg6, super_constructor:reg5
+ [ 8] BindThisFromSuperCall result:reg6
+ [ a] Return value:reg6
read$4b849658 super-evaluation-order.js:3:9
diff --git a/Tests/LibJS/Bytecode/expected/super-length-access.txt b/Tests/LibJS/Bytecode/expected/super-length-access.txt
index aed5d12fc15..21977002128 100644
--- a/Tests/LibJS/Bytecode/expected/super-length-access.txt
+++ b/Tests/LibJS/Bytecode/expected/super-length-access.txt
@@ -29,16 +29,16 @@ block0:
[ 81] End value:reg5
-B$00eeb12b super-length-access.js:7:1
- Registers: 8
+B$1d2145b5 super-length-access.js:7:1
+ Registers: 7
Blocks: 1
block0:
[ 0] Enter
- [ 1] CreateRestParams dst:reg5, rest_index:0
- [ 7] GetSuperConstructor dst:reg6
- [ 9] SuperCallWithArgumentArray call_feedback:0, dst:reg7, super_constructor:reg6, arguments:reg5, is_synthetic:true
- [ 10] Return value:reg7
+ [ 1] GetSuperConstructor dst:reg5
+ [ 3] SuperCallWithForwardedArguments call_feedback:0, dst:reg6, super_constructor:reg5
+ [ 8] BindThisFromSuperCall result:reg6
+ [ a] Return value:reg6
A$5e284e97
diff --git a/Tests/LibJS/Bytecode/expected/super-optional-call-this.txt b/Tests/LibJS/Bytecode/expected/super-optional-call-this.txt
index f1f4266ace5..e12298d9aa6 100644
--- a/Tests/LibJS/Bytecode/expected/super-optional-call-this.txt
+++ b/Tests/LibJS/Bytecode/expected/super-optional-call-this.txt
@@ -28,16 +28,16 @@ block0:
[ 81] End value:reg5
-Foo$ea8d03b4 super-optional-call-this.js:4:1
- Registers: 8
+Foo$c8d28d0a super-optional-call-this.js:4:1
+ Registers: 7
Blocks: 1
block0:
[ 0] Enter
- [ 1] CreateRestParams dst:reg5, rest_index:0
- [ 7] GetSuperConstructor dst:reg6
- [ 9] SuperCallWithArgumentArray call_feedback:0, dst:reg7, super_constructor:reg6, arguments:reg5, is_synthetic:true
- [ 10] Return value:reg7
+ [ 1] GetSuperConstructor dst:reg5
+ [ 3] SuperCallWithForwardedArguments call_feedback:0, dst:reg6, super_constructor:reg5
+ [ 8] BindThisFromSuperCall result:reg6
+ [ a] Return value:reg6
Base$138475b3
diff --git a/Tests/LibJS/Runtime/classes/class-super-call-paths.js b/Tests/LibJS/Runtime/classes/class-super-call-paths.js
index e42c0609e3e..80686f36aa8 100644
--- a/Tests/LibJS/Runtime/classes/class-super-call-paths.js
+++ b/Tests/LibJS/Runtime/classes/class-super-call-paths.js
@@ -81,6 +81,35 @@ describe("arguments of super()", () => {
Array.prototype[Symbol.iterator] = original;
}
});
+ test("default derived constructors pass the arguments on without the array iterator", () => {
+ class A {
+ constructor(a, b) {
+ this.a = a;
+ this.b = b;
+ this.count = arguments.length;
+ }
+ }
+ class Default extends A {}
+ class DefaultOfDefault extends Default {}
+ const original = Array.prototype[Symbol.iterator];
+ Array.prototype[Symbol.iterator] = function* () {
+ yield "changed";
+ };
+ try {
+ repeat(i => {
+ const none = new Default();
+ expect(none.count).toBe(0);
+ expect(none.a).toBeUndefined();
+ const many = new DefaultOfDefault(i, 2, 3);
+ expect(many.a).toBe(i);
+ expect(many.b).toBe(2);
+ expect(many.count).toBe(3);
+ expect(many).toBeInstanceOf(DefaultOfDefault);
+ });
+ } finally {
+ Array.prototype[Symbol.iterator] = original;
+ }
+ });
});
describe("parents of derived constructors", () => {