Skip to content

Truly independent web browser

LibJS: Pass super() arguments as operands and enter the parent inline

Andreas Kling4e57f6de5446
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index aec594c23cd..6824b6c7754 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -162,6 +162,8 @@ asm_dispatch_table:
.quad asm_handler_CallDirectEvalWithArgumentArray
.quad asm_handler_CallConstructWithArgumentArray
.quad asm_handler_SuperCallWithArgumentArray
+ .quad asm_handler_SuperCall
+ .quad asm_handler_BindThisFromSuperCall
.quad asm_handler_NewObject
.quad asm_handler_NewObjectWithNoPrototype
.quad asm_handler_CacheObjectShape
@@ -267,8 +269,6 @@ asm_dispatch_table:
.quad asm_handler_fallback
.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):
@@ -1129,6 +1129,14 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_SuperCallWithArgumentArray_hot_end
.quad asm_handler_SuperCallWithArgumentArray_cold
.quad asm_handler_SuperCallWithArgumentArray_cold_end
+ .quad asm_handler_SuperCall
+ .quad asm_handler_SuperCall_hot_end
+ .quad asm_handler_SuperCall_cold
+ .quad asm_handler_SuperCall_cold_end
+ .quad asm_handler_BindThisFromSuperCall
+ .quad asm_handler_BindThisFromSuperCall_hot_end
+ .quad asm_handler_BindThisFromSuperCall_cold
+ .quad asm_handler_BindThisFromSuperCall_cold_end
.quad asm_handler_NewObject
.quad asm_handler_NewObject_hot_end
.quad asm_handler_NewObject_cold
@@ -1549,14 +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
- .quad asm_handler_fallback
- .quad asm_handler_fallback_hot_end
- .quad asm_handler_fallback_cold
- .quad asm_handler_fallback_cold_end
.text
@@ -16617,6 +16617,139 @@ asm_handler_SuperCallWithArgumentArray_hot_end:
asm_handler_SuperCallWithArgumentArray_cold:
asm_handler_SuperCallWithArgumentArray_cold_end:
+.p2align 4
+asm_handler_SuperCall:
+ mov x0, x20
+ sub w1, w21, w26
+ str w1, [x28, #64]
+ add x9, sp, #0
+ ldrb w10, [x21, #5]
+ lsl x10, x10, #3
+ add x10, x10, #47
+ and x10, x10, #0xfffffffffffffff0
+ sub x14, x9, x10
+ ldr x12, [x20, #40]
+ mov w13, #32768
+ add x12, x12, x13
+ cmp x14, x12
+ b.hs .Lasm_SuperCall.values_allocate_1
+ mov x0, x20
+ sub w1, w21, w26
+ bl CSYM(asm_slow_path_stack_overflow)
+ b .Lasm_SuperCall.values_call_done_0
+.Lasm_SuperCall.values_allocate_1:
+.Lasm_SuperCall.values_probe_2:
+ sub x10, sp, #1, lsl #12
+ cmp x10, x14
+ b.ls .Lasm_SuperCall.values_allocated_3
+ add sp, x10, #0
+ str xzr, [sp]
+ b .Lasm_SuperCall.values_probe_2
+.Lasm_SuperCall.values_allocated_3:
+ add sp, x14, #0
+ str x9, [sp]
+ ldrb w12, [x21, #4]
+ ldr x11, [x27, x12, lsl #3]
+.Lasm_SuperCall.value_ready_4:
+ str x11, [sp, #24]
+ mov w9, #0
+ ldrb w10, [x21, #5]
+ add x14, sp, #32
+ add x3, x21, #6
+.Lasm_SuperCall.array_value_next_5:
+ cmp x9, x10
+ b.hs .Lasm_SuperCall.array_values_end_7
+ ldrb w12, [x3, x9]
+ ldr x11, [x27, x12, lsl #3]
+.Lasm_SuperCall.array_value_ready_6:
+ str x11, [x14, x9, lsl #3]
+ add x9, x9, #1
+ b .Lasm_SuperCall.array_value_next_5
+.Lasm_SuperCall.array_values_end_7:
+ mov x0, x20
+ sub w1, w21, w26
+ mov x2, x21
+ add x3, sp, #16
+ bl CSYM(asm_slow_path_super_call)
+ tbnz x0, #63, .Lasm_SuperCall.values_exceptional_outputs_9
+ tbz x0, #32, .Lasm_SuperCall.values_exceptional_outputs_9
+ ldrb w12, [x21, #3]
+ ldr x11, [sp, #16]
+ str x11, [x27, x12, lsl #3]
+.Lasm_SuperCall.skip_output_value_10:
+ ldr x9, [sp]
+ add sp, x9, #0
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_SuperCall.values_exceptional_outputs_9:
+.Lasm_SuperCall.values_restore_stack_8:
+ ldr x9, [sp]
+ add sp, x9, #0
+.Lasm_SuperCall.values_call_done_0:
+ tbz x0, #63, 1f
+ b .Lexit
+1:
+ tbz x0, #32, .Lasm_SuperCall.slow_path_transfer_11
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_SuperCall.slow_path_transfer_11:
+ 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_SuperCall_hot_end:
+asm_handler_SuperCall_cold:
+asm_handler_SuperCall_cold_end:
+
+.p2align 4
+asm_handler_BindThisFromSuperCall:
+ mov x0, x20
+ sub w1, w21, w26
+ str w1, [x28, #64]
+ ldrb w9, [x21, #1]
+ ldr x10, [x27, x9, lsl #3]
+.Lasm_BindThisFromSuperCall.scalar_value_ready_0:
+ mov x3, x10
+ mov x0, x20
+ sub w1, w21, w26
+ mov x2, x21
+ bl CSYM(asm_slow_path_bind_this_from_super_call)
+ tbnz x0, #63, .Lasm_BindThisFromSuperCall.scalar_result_done_1
+ tbz x0, #32, .Lasm_BindThisFromSuperCall.scalar_result_done_1
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_BindThisFromSuperCall.scalar_result_done_1:
+ tbz x0, #63, 1f
+ b .Lexit
+1:
+ tbz x0, #32, .Lasm_BindThisFromSuperCall.slow_path_transfer_2
+ add x21, x26, w0, uxtw
+ ldrb w9, [x21]
+ ldr x10, [x19, x9, lsl #3]
+ br x10
+.Lasm_BindThisFromSuperCall.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_BindThisFromSuperCall_hot_end:
+asm_handler_BindThisFromSuperCall_cold:
+asm_handler_BindThisFromSuperCall_cold_end:
+
.p2align 4
asm_handler_NewObject:
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 2deb89d9e78..b3e8f746103 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -163,6 +163,8 @@ asm_dispatch_table:
.quad asm_handler_CallDirectEvalWithArgumentArray
.quad asm_handler_CallConstructWithArgumentArray
.quad asm_handler_SuperCallWithArgumentArray
+ .quad asm_handler_SuperCall
+ .quad asm_handler_BindThisFromSuperCall
.quad asm_handler_NewObject
.quad asm_handler_NewObjectWithNoPrototype
.quad asm_handler_CacheObjectShape
@@ -268,8 +270,6 @@ asm_dispatch_table:
.quad asm_handler_fallback
.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):
@@ -1130,6 +1130,14 @@ CSYM(js_interpreter_handler_ranges):
.quad asm_handler_SuperCallWithArgumentArray_hot_end
.quad asm_handler_SuperCallWithArgumentArray_cold
.quad asm_handler_SuperCallWithArgumentArray_cold_end
+ .quad asm_handler_SuperCall
+ .quad asm_handler_SuperCall_hot_end
+ .quad asm_handler_SuperCall_cold
+ .quad asm_handler_SuperCall_cold_end
+ .quad asm_handler_BindThisFromSuperCall
+ .quad asm_handler_BindThisFromSuperCall_hot_end
+ .quad asm_handler_BindThisFromSuperCall_cold
+ .quad asm_handler_BindThisFromSuperCall_cold_end
.quad asm_handler_NewObject
.quad asm_handler_NewObject_hot_end
.quad asm_handler_NewObject_cold
@@ -1550,14 +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
- .quad asm_handler_fallback
- .quad asm_handler_fallback_hot_end
- .quad asm_handler_fallback_cold
- .quad asm_handler_fallback_cold_end
.text
@@ -16179,6 +16179,136 @@ asm_handler_SuperCallWithArgumentArray_hot_end:
asm_handler_SuperCallWithArgumentArray_cold:
asm_handler_SuperCallWithArgumentArray_cold_end:
+.p2align 4
+asm_handler_SuperCall:
+ mov DWORD PTR [rbx + -72], r13d
+ lea rcx, [r14 + r13]
+ mov r11, rsp
+ movzx r10d, BYTE PTR [rcx + 5]
+ shl r10, 3
+ add r10, 47
+ and r10, -16
+ mov rax, rsp
+ sub rax, r10
+ mov rdx, QWORD PTR [rbp - 48]
+ mov rdx, QWORD PTR [rdx + 40]
+ add rdx, 32768
+ cmp rax, rdx
+ jae .Lasm_SuperCall.values_allocate_1
+ mov rdi, QWORD PTR [rbp - 48]
+ mov esi, r13d
+ lea rdx, [r14 + r13]
+ call CSYM(asm_slow_path_stack_overflow)
+ jmp .Lasm_SuperCall.values_call_done_0
+.Lasm_SuperCall.values_allocate_1:
+.Lasm_SuperCall.values_probe_2:
+ lea r10, [rsp - 4096]
+ cmp r10, rax
+ jbe .Lasm_SuperCall.values_allocated_3
+ mov rsp, r10
+ mov QWORD PTR [rsp], 0
+ jmp .Lasm_SuperCall.values_probe_2
+.Lasm_SuperCall.values_allocated_3:
+ mov rsp, rax
+ mov QWORD PTR [rsp], r11
+ movzx r10d, BYTE PTR [rcx + 4]
+ mov rax, QWORD PTR [rbx + r10 * 8]
+.Lasm_SuperCall.value_ready_4:
+ mov QWORD PTR [rsp + 24], rax
+ mov r11d, 0
+ movzx r10d, BYTE PTR [rcx + 5]
+.Lasm_SuperCall.array_value_next_5:
+ cmp r11, r10
+ jae .Lasm_SuperCall.array_values_end_7
+ movzx edx, BYTE PTR [rcx + r11 + 6]
+ mov rax, QWORD PTR [rbx + rdx * 8]
+.Lasm_SuperCall.array_value_ready_6:
+ mov QWORD PTR [rsp + r11 * 8 + 32], rax
+ add r11, 1
+ jmp .Lasm_SuperCall.array_value_next_5
+.Lasm_SuperCall.array_values_end_7:
+ mov rdi, QWORD PTR [rbp - 48]
+ mov esi, r13d
+ lea rdx, [r14 + r13]
+ lea rcx, [rsp + 16]
+ call CSYM(asm_slow_path_super_call)
+ test rax, rax
+ js .Lasm_SuperCall.values_exceptional_outputs_9
+ bt rax, 32
+ jnc .Lasm_SuperCall.values_exceptional_outputs_9
+ lea rcx, [r14 + r13]
+ movzx r10d, BYTE PTR [rcx + 3]
+ mov r11, QWORD PTR [rsp + 16]
+ mov QWORD PTR [rbx + r10 * 8], r11
+.Lasm_SuperCall.skip_output_value_10:
+ mov rsp, QWORD PTR [rsp]
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_SuperCall.values_exceptional_outputs_9:
+.Lasm_SuperCall.values_restore_stack_8:
+ mov rsp, QWORD PTR [rsp]
+.Lasm_SuperCall.values_call_done_0:
+ test rax, rax
+ js .Lexit
+ bt rax, 32
+ jnc .Lasm_SuperCall.slow_path_transfer_11
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_SuperCall.slow_path_transfer_11:
+ 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_SuperCall_hot_end:
+asm_handler_SuperCall_cold:
+asm_handler_SuperCall_cold_end:
+
+.p2align 4
+asm_handler_BindThisFromSuperCall:
+ mov DWORD PTR [rbx + -72], r13d
+ lea r11, [r14 + r13]
+ movzx r10d, BYTE PTR [r11 + 1]
+ mov rax, QWORD PTR [rbx + r10 * 8]
+.Lasm_BindThisFromSuperCall.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_bind_this_from_super_call)
+ test rax, rax
+ js .Lasm_BindThisFromSuperCall.scalar_result_done_1
+ bt rax, 32
+ jnc .Lasm_BindThisFromSuperCall.scalar_result_done_1
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_BindThisFromSuperCall.scalar_result_done_1:
+ test rax, rax
+ js .Lexit
+ bt rax, 32
+ jnc .Lasm_BindThisFromSuperCall.slow_path_transfer_2
+ mov r13d, eax
+ movzx eax, BYTE PTR [r14 + r13]
+ jmp [r12 + rax * 8]
+.Lasm_BindThisFromSuperCall.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_BindThisFromSuperCall_hot_end:
+asm_handler_BindThisFromSuperCall_cold:
+asm_handler_BindThisFromSuperCall_cold_end:
+
.p2align 4
asm_handler_NewObject:
mov DWORD PTR [rbx + -72], r13d
diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flap
index 5f02309bfac..cd6b2a2d8f1 100644
--- a/Libraries/LibJS/Interpreter/interpreter.flap
+++ b/Libraries/LibJS/Interpreter/interpreter.flap
@@ -4406,6 +4406,18 @@ handler SuperCallWithArgumentArray(
is_synthetic: bool
) @cold = call_slow_path(super_call_with_argument_array);
+handler SuperCall(
+ call_feedback: CallFeedbackIndex,
+ dst: out Operand,
+ super_constructor: Operand,
+ argument_count: u8,
+ arguments: Operand[]
+) @cold = call_slow_path(super_call);
+
+handler BindThisFromSuperCall(
+ result: Operand
+) @cold = call_slow_path(bind_this_from_super_call);
+
handler NewObject(
dst: out Operand,
cache: ObjectShapeCacheIndex
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
index fae7bce639f..4c54e5f9713 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/handling.rs
@@ -130,7 +130,9 @@ pub fn handling(opcode: OpCode) -> Handling {
| O::CallConstruct
| O::CallWithArgumentArray
| O::CallConstructWithArgumentArray
- | O::SuperCallWithArgumentArray => GENERIC_CALL,
+ | O::SuperCallWithArgumentArray
+ | O::SuperCall => GENERIC_CALL,
+ O::BindThisFromSuperCall => GENERIC,
O::CallDirectEval | O::CallDirectEvalWithArgumentArray => 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 5570eed741e..7dd31269006 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -153,6 +153,8 @@ fn can_inline(callee: &Function<'_>, virtual_arguments: bool) -> bool {
| OpCode::GetNewTarget
| OpCode::GetSuperConstructor
| OpCode::SuperCallWithArgumentArray
+ | OpCode::SuperCall
+ | OpCode::BindThisFromSuperCall
);
// NB: The base of `super` property access is built from the callee's home object (see
// `try_build_resolve_super_base()`).
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index 1715a6038c1..6cc9ee7d8da 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -2058,6 +2058,33 @@ impl Generator<'_> {
self.push_value(dst, first_temporary);
}
+ /// `super(...)` without spread arguments, whose arguments are on top of the stack above the super constructor.
+ pub(super) fn super_call_with_operands(&mut self, argument_count: usize) {
+ if argument_count > MAX_OPERAND_LIST_LENGTH {
+ self.array(argument_count);
+ self.super_call();
+ return;
+ }
+ let arguments = self.take_operands(argument_count);
+ let (super_constructor, first_temporary) = self.pop_operand();
+ self.release_temporaries_from(first_temporary);
+ let dst = self.temporary();
+ let call_feedback = self.next_call_feedback();
+ self.put_with_destination(
+ op::SuperCall {
+ call_feedback,
+ dst,
+ super_constructor,
+ argument_count: operand_list_count(arguments.len()),
+ arguments: &arguments,
+ },
+ dst,
+ );
+ self.put(op::BindThisFromSuperCall { result: dst });
+ self.context.stream.operands = arguments;
+ self.push_value(dst, first_temporary);
+ }
+
/// `#name in value`, for the value on top of the stack, which the name stands in for below it.
pub(super) fn private_name_in(&mut self, name: Name) {
let (base, _) = self.pop_operand();
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
index 46b2420f759..b8735d0faec 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/sweep.rs
@@ -247,9 +247,10 @@ impl Generator<'_> {
.iter()
.any(|argument| tree.node_tag(argument) == tag::SPREAD)
{
- self.array(tree.node_list(b).len());
+ self.super_call_with_operands(tree.node_list(b).len());
+ } else {
+ self.super_call();
}
- self.super_call();
}
tag::SUPER_MEMBER => self.value_of_super_property(Some(Name(b)), false),
tag::SUPER_MEMBER_CALLEE => self.value_of_super_property(Some(Name(b)), true),
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
index 0c0839ecd94..09c9a5fa47d 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
@@ -681,48 +681,226 @@ pub fn call_construct_with_argument_array(
SlowPathControl::continue_at(pc + op::CallConstructWithArgumentArray::LENGTH)
}
+// 13.3.7.1 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
+// SuperCall : super Arguments
pub fn super_call_with_argument_array(
vm: &Vm,
pc: u32,
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 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),
+ );
+ }
+ }
+ }) {
+ Ok(result) => result,
+ Err(control) => return control,
+ };
+ if let Some(control) = bind_this_from_super_call_result(vm, pc, result) {
+ return control;
+ }
+ values.dst = Value::from_object(result);
+ SlowPathControl::continue_at(pc + op::SuperCallWithArgumentArray::LENGTH)
+}
+
+/// Steps 1, 2, 5 and 6 of the evaluation of `super(...)` for a SuperCall instruction, whose arguments are operands. A
+/// BindThisFromSuperCall instruction does the rest once the super constructor returned into the destination.
+pub fn super_call(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::SuperCall,
+ values: &mut op::SuperCallValues,
+ arguments: &[Value],
+) -> SlowPathControl {
+ super_call_with_arguments(
+ vm,
+ pc,
+ pc + instruction.length(),
+ instruction.dst.0.into(),
+ values.super_constructor,
+ arguments,
+ &mut values.dst,
+ )
+}
+
+fn super_call_with_arguments(
+ vm: &Vm,
+ pc: u32,
+ return_pc: u32,
+ dst_raw: u32,
+ super_constructor: Value,
+ arguments: &[Value],
+ dst: &mut Value,
+) -> SlowPathControl {
+ // OPTIMIZATION: An ordinary ECMAScript super constructor runs in an inline frame, like the construct fast path
+ // does. BindThisFromSuperCall checks the value the frame returns.
+ if let Some(function) = value_as_ecmascript_function_object(super_constructor)
+ && function.kind() == FunctionKind::Normal
+ && super_constructor.is_constructor()
+ && match function.constructor_kind() {
+ ConstructorKind::Base => !function.has_class_data(),
+ // NB: The frame of such a derived constructor returns its this value for undefined (see
+ // ExecutionContext::caller_is_derived_construct).
+ ConstructorKind::Derived => !function.shared_data().function_environment_has_this_binding(),
+ }
+ // NB: Taken before anything allocates: a collection could flush the code of the function otherwise, which this
+ // local keeps from happening (see `bytecode::flushing`).
+ && let Some(executable) = function.bytecode_executable()
+ {
+ let new_target = vm.get_new_target();
+ assert!(new_target.is_object());
+ let new_target = new_target.as_function();
+ let mut this_object = None;
+ if function.constructor_kind() == ConstructorKind::Base {
+ // 10.2.2 [[Construct]], step 3.a: Let thisArgument be ? OrdinaryCreateFromConstructor(newTarget,
+ // "%Object.prototype%").
+ let prototype = asm_try!(
+ vm,
+ pc,
+ get_prototype_from_constructor(vm, new_target, Intrinsics::object_prototype)
+ );
+ // NB: With room for the properties the constructor is known to add.
+ this_object = Some(Object::create_for_construct(
+ vm,
+ vm.current_realm().expect("a construction runs in a realm"),
+ prototype,
+ function.shared_data(),
+ ));
+ }
+ let Some(context) = vm.push_inline_frame_without_this(
+ function,
+ executable,
+ arguments,
+ argument_count_of(arguments.len()),
+ return_pc,
+ dst_raw,
+ Some(function_object_as_object(new_target)),
+ this_object.is_some(),
+ ) else {
+ return throw_error(vm, pc, ErrorKind::InternalError, ErrorType::CallStackSizeExceeded, &[]);
+ };
+ // SAFETY: The frame was just entered, and the interpreter runs it next.
+ let context = unsafe { context.as_ref() };
+ if let Some(this_object) = this_object {
+ vm.bind_this_in_inline_frame(function, context, Value::from_object(this_object));
+ // Constructors retain their receiver even when the body never reads this.
+ context.this_value.set(Value::from_object(this_object));
+ } else {
+ context.caller_is_construct.set(true);
+ context.caller_is_derived_construct.set(true);
+ }
+ return SlowPathControl::dispatch_at(0);
+ }
+
+ let result =
+ match construct_for_super_call(vm, pc, super_constructor, argument_count_of(arguments.len()), |slots| {
+ for (slot, value) in slots.iter().zip(arguments) {
+ slot.set(*value);
+ }
+ }) {
+ Ok(result) => result,
+ Err(control) => return control,
+ };
+ *dst = Value::from_object(result);
+ SlowPathControl::continue_at(return_pc)
+}
+
+/// Steps 7 to 12 of the evaluation of `super(...)`, after a SuperCall instruction, for the value the super constructor
+/// returned. An inline frame of a derived super constructor returns its this value (empty if super() was never called
+/// in it) for undefined, or what it returned otherwise, which steps 12 to 17 of its [[Construct]] check here.
+pub fn bind_this_from_super_call(vm: &Vm, pc: u32, values: &mut op::BindThisFromSuperCallValues) -> SlowPathControl {
+ let result = values.result;
+ if !result.is_object() {
+ if result.is_empty() {
+ return throw_error(
+ vm,
+ pc,
+ ErrorKind::ReferenceError,
+ ErrorType::ThisHasNotBeenInitialized,
+ &[],
+ );
+ }
+ return throw_error(
+ vm,
+ pc,
+ ErrorKind::TypeError,
+ ErrorType::DerivedConstructorReturningInvalidValue,
+ &[],
+ );
+ }
+ if let Some(control) = bind_this_from_super_call_result(vm, pc, result.as_object()) {
+ return control;
+ }
+ SlowPathControl::continue_at(pc + op::BindThisFromSuperCall::LENGTH)
+}
+
+/// Steps 1, 2, 5 and 6 of the evaluation of `super(...)`: constructs with the super constructor and the new target of
+/// the running function through the generic [[Construct]], with `fill_arguments` writing the arguments.
+fn construct_for_super_call(
+ vm: &Vm,
+ pc: u32,
+ super_constructor: Value,
+ argument_count: u32,
+ fill_arguments: impl FnOnce(&[std::cell::Cell<Value>]),
+) -> Result<Gc<Object>, SlowPathControl> {
+ // 1. Let newTarget be GetNewTarget().
let new_target = vm.get_new_target();
+ // 2. Assert: newTarget is a constructor.
assert!(new_target.is_object());
- let super_constructor = values.super_constructor;
+ // 5. If IsConstructor(superCtor) is false, throw a TypeError exception.
if !super_constructor.is_constructor() {
running_execution_context(vm).program_counter.set(pc);
- return throw_error(
+ return Err(throw_error(
vm,
pc,
ErrorKind::TypeError,
ErrorType::NotAConstructor,
&[&"Super constructor"],
- );
+ ));
}
let function = super_constructor.as_function();
-
- let argument_array = as_array_exotic_object(values.arguments);
- 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 stack_frame_info = match stack_frame_info_for_call(vm, function, argument_count) {
+ Ok(info) => info,
+ Err(throw) => return Err(handle_asm_exception(vm, pc, throw.value())),
};
- let argument_array_length = u32::try_from(argument_array_length).expect("the argument count fits in u32");
-
- let stack_frame_info = asm_try!(vm, pc, stack_frame_info_for_call(vm, function, argument_array_length));
let stack = vm.interpreter_stack();
let stack_mark = stack.top.get();
let Some(callee_context) = stack.allocate(
stack_frame_info.registers_and_locals_count,
stack_frame_info.constant_count,
- argument_array_length.max(stack_frame_info.argument_count),
+ argument_count.max(stack_frame_info.argument_count),
) else {
running_execution_context(vm).program_counter.set(pc);
- return throw_error(vm, pc, ErrorKind::InternalError, ErrorType::CallStackSizeExceeded, &[]);
+ return Err(throw_error(
+ vm,
+ pc,
+ ErrorKind::InternalError,
+ ErrorType::CallStackSizeExceeded,
+ &[],
+ ));
};
let _deallocate_guard = ScopeGuard::new(|| {
if stack.top.get() > stack_mark {
@@ -733,68 +911,56 @@ pub fn super_call_with_argument_array(
let callee_context = unsafe { callee_context.as_ref() };
let callee_context_argument_values = callee_context.arguments();
- let insn_argument_count = argument_array_length as usize;
-
- if instruction.is_synthetic {
- for (index, slot) in callee_context_argument_values
- .iter()
- .take(insn_argument_count)
- .enumerate()
- {
- slot.set(argument_array.get_without_side_effects(vm, &PropertyKey::from_number(index as u64)));
- }
- } else {
- for (index, slot) in callee_context_argument_values
- .iter()
- .take(insn_argument_count)
- .enumerate()
- {
- let index = u32::try_from(index).expect("the argument index fits in u32");
- if let Some(value) = argument_array.indexed_get(index) {
- slot.set(value.value);
- } else {
- slot.set(Value::UNDEFINED);
- }
- }
- }
- for slot in &callee_context_argument_values[insn_argument_count..] {
+ let passed = argument_count as usize;
+ fill_arguments(&callee_context_argument_values[..passed]);
+ for slot in &callee_context_argument_values[passed..] {
slot.set(Value::UNDEFINED);
}
- callee_context.passed_argument_count.set(argument_array_length);
+ callee_context.passed_argument_count.set(argument_count);
- let result = asm_try!(
- vm,
- pc,
- internal_construct(vm, function, callee_context, new_target.as_function())
- );
+ // 6. Let result be ? Construct(superCtor, argList, newTarget).
+ internal_construct(vm, function, callee_context, new_target.as_function())
+ .map_err(|throw| handle_asm_exception(vm, pc, throw.value()))
+}
+/// Steps 7 to 12 of the evaluation of `super(...)` for `result`, what the super constructor returned. Returns the
+/// control of a thrown exception.
+fn bind_this_from_super_call_result(vm: &Vm, pc: u32, result: Gc<Object>) -> Option<SlowPathControl> {
let f = if let Some(function) = function_with_frame_as_this_environment(vm) {
// OPTIMIZATION: BindThisValue() of the function environment, for a running function whose frame answers for
// it. The empty this value of the frame means that the binding is not initialized.
let frame = running_execution_context(vm);
if !frame.this_value.get().is_empty() {
- return throw_error(
+ return Some(throw_error(
vm,
pc,
ErrorKind::ReferenceError,
ErrorType::ThisIsAlreadyInitialized,
&[],
- );
+ ));
}
frame.this_value.set(Value::from_object(result));
function
} else {
+ // 7. Let thisER be GetThisEnvironment().
+ // 8. Assert: thisER is a Function Environment Record.
let this_environment = get_this_environment(vm)
.downcast::<FunctionEnvironment>()
.expect("super() runs in a function environment");
- asm_try!(vm, pc, this_environment.bind_this_value(vm, Value::from_object(result)));
+ // 9. Perform ? BindThisValue(thisER, result).
+ if let Err(throw) = this_environment.bind_this_value(vm, Value::from_object(result)) {
+ return Some(handle_asm_exception(vm, pc, throw.value()));
+ }
as_ecmascript_function_object(this_environment.function_object())
.expect("super() runs in an ECMAScript function")
};
- asm_try!(vm, pc, result.initialize_instance_elements(vm, f));
-
- values.dst = Value::from_object(result);
- SlowPathControl::continue_at(pc + op::SuperCallWithArgumentArray::LENGTH)
+ // 10. Let funcObj be thisER.[[FunctionObject]].
+ // 11. Assert: funcObj is an ECMAScript function object.
+ // 12. Perform ? InitializeInstanceElements(result, funcObj).
+ if let Err(throw) = result.initialize_instance_elements(vm, f) {
+ return Some(handle_asm_exception(vm, pc, throw.value()));
+ }
+ None
}
/// Records the function a call of a builtin written in JavaScript passes it as its first argument, which the builtins
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
index 7a2726eec82..503ce99764e 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
@@ -134,6 +134,24 @@ pub fn call_construct_slow_path(
feedback::record_call_value_after(vm, control, instruction.call_feedback, || values.dst)
}
+/// The slow path of SuperCall, for JIT code too, whose arguments are not in an operand record.
+pub fn super_call_slow_path(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::SuperCall,
+ values: &mut op::SuperCallValues,
+ arguments: &[Value],
+) -> SlowPathControl {
+ feedback::record_call(
+ vm,
+ instruction.call_feedback,
+ values.super_constructor,
+ call_feedback_flags::SAW_CONSTRUCT,
+ );
+ let control = calls::super_call(vm, pc, instruction, values, arguments);
+ feedback::record_call_value_after(vm, control, instruction.call_feedback, || values.dst)
+}
+
impl RuntimeFunctions for Runtime {
// Arithmetic, comparisons, conversions and the jumps on comparisons: operators.rs.
@@ -1375,6 +1393,25 @@ impl RuntimeFunctions for Runtime {
feedback::record_call_value_after(vm, control, instruction.call_feedback, || values.dst)
}
+ fn super_call(
+ vm: &Vm,
+ pc: u32,
+ instruction: &op::SuperCall,
+ values: &mut op::SuperCallValues,
+ arguments: &mut [Value],
+ ) -> SlowPathControl {
+ super_call_slow_path(vm, pc, instruction, values, arguments)
+ }
+
+ fn bind_this_from_super_call(
+ vm: &Vm,
+ pc: u32,
+ _instruction: &op::BindThisFromSuperCall,
+ values: &mut op::BindThisFromSuperCallValues,
+ ) -> SlowPathControl {
+ calls::bind_this_from_super_call(vm, pc, values)
+ }
+
fn super_call_with_argument_array(
vm: &Vm,
pc: u32,
diff --git a/Libraries/LibJS/Rust/src/jit/calls.rs b/Libraries/LibJS/Rust/src/jit/calls.rs
index fb8221bc2f6..5ac2ea42755 100644
--- a/Libraries/LibJS/Rust/src/jit/calls.rs
+++ b/Libraries/LibJS/Rust/src/jit/calls.rs
@@ -34,12 +34,12 @@ use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::execution_context::ExecutionContext;
use crate::layout::feedback::CallFeedbackForwarding;
-use crate::layout::function_object::{EcmascriptFunctionObject, NativeFunction, RawNativeFunction};
+use crate::layout::function_object::{EcmascriptFunctionObject, FunctionObject, NativeFunction, RawNativeFunction};
use crate::layout::object::{IndexedStorageKind, Object};
use crate::layout::value::Value;
use crate::runtime::abstract_operations::{
- call_function_object, create_unmapped_arguments_object, get_prototype_from_constructor, length_of_array_like,
- throw_if_too_many_arguments,
+ call_function_object, create_unmapped_arguments_object, function_object_as_object, get_prototype_from_constructor,
+ length_of_array_like, throw_if_too_many_arguments,
};
use crate::runtime::array::Array;
use crate::runtime::bound_function::BoundFunction;
@@ -191,6 +191,7 @@ fn construct_ecmascript_function_from_jit(
vm: &Vm,
site: &JitCallSite,
function: Gc<EcmascriptFunctionObject>,
+ new_target: Gc<FunctionObject>,
arguments: &[Value],
) -> SlowPathControl {
if vm.did_reach_stack_space_limit() {
@@ -205,7 +206,7 @@ fn construct_ecmascript_function_from_jit(
if kind == ConstructorKind::Base {
// a. Let thisArgument be ? OrdinaryCreateFromConstructor(newTarget, "%Object.prototype%").
// NB: With room for the properties the constructor is known to add.
- match get_prototype_from_constructor(vm, function.as_function_object_gc(), Intrinsics::object_prototype) {
+ match get_prototype_from_constructor(vm, new_target, Intrinsics::object_prototype) {
Ok(prototype) => {
this_argument = Some(Object::create_for_construct(
vm,
@@ -235,7 +236,7 @@ fn construct_ecmascript_function_from_jit(
argument_count,
site.return_pc,
site.dst,
- Some(function.upcast()),
+ Some(function_object_as_object(new_target)),
false,
) else {
return throw_call_stack_size_exceeded(vm, site.pc);
@@ -722,7 +723,14 @@ pub unsafe extern "C" fn libjs_jit_call(vm: *const Vm, frame: *mut ExecutionCont
&& function.has_constructor()
&& function.bytecode_executable().is_some()
{
- return construct_ecmascript_function_from_jit(vm, &site, function, &arguments).0;
+ return construct_ecmascript_function_from_jit(
+ vm,
+ &site,
+ function,
+ function.as_function_object_gc(),
+ &arguments,
+ )
+ .0;
}
let mut values = op::CallConstructValues {
dst: Value::EMPTY,
@@ -732,6 +740,45 @@ pub unsafe extern "C" fn libjs_jit_call(vm: *const Vm, frame: *mut ExecutionCont
let control = slow_paths::call_construct_slow_path(vm, pc, call, &mut values, &arguments);
finish_generic_call(vm, &site, control, values.dst)
}
+ _ if opcode == OpCode::SuperCall as u8 => {
+ let call = instruction!(SuperCall);
+ let site = site(call.dst, call.length());
+ let operands = call.arguments();
+ let mut storage = ArgumentStorage::empty();
+ 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;
+ }
+ let mut values = op::SuperCallValues {
+ dst: Value::EMPTY,
+ super_constructor,
+ arguments: [],
+ };
+ 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::CallDirectEval as u8 => {
let call = instruction!(CallDirectEval);
let site = site(call.dst, call.length());
diff --git a/Tests/LibJS/Bytecode/expected/super-computed-eval-order.txt b/Tests/LibJS/Bytecode/expected/super-computed-eval-order.txt
index b34b34b0c3b..ec6f0ae230a 100644
--- a/Tests/LibJS/Bytecode/expected/super-computed-eval-order.txt
+++ b/Tests/LibJS/Bytecode/expected/super-computed-eval-order.txt
@@ -28,8 +28,8 @@ block0:
[ 80] End value:reg5
-Derived$691b5ec5 super-computed-eval-order.js:4:14
- Registers: 7
+Derived$0973dce7 super-computed-eval-order.js:4:14
+ Registers: 6
Blocks: 1
Constants:
[0] = Undefined
@@ -37,9 +37,9 @@ Derived$691b5ec5 super-computed-eval-order.js:4:14
block0:
[ 0] Enter
[ 1] GetSuperConstructor dst:reg5
- [ 3] NewArray dst:reg6
- [ 6] SuperCallWithArgumentArray call_feedback:0, dst:reg5, super_constructor:reg5, arguments:reg6, is_synthetic:false
- [ d] End value:Undefined
+ [ 3] SuperCall call_feedback:0, dst:reg5, super_constructor:reg5
+ [ 9] BindThisFromSuperCall result:reg5
+ [ b] End value:Undefined
Base$138475b3
diff --git a/Tests/LibJS/Bytecode/expected/super-computed-string-to-id.txt b/Tests/LibJS/Bytecode/expected/super-computed-string-to-id.txt
index 240083895c0..b9d2eda508a 100644
--- a/Tests/LibJS/Bytecode/expected/super-computed-string-to-id.txt
+++ b/Tests/LibJS/Bytecode/expected/super-computed-string-to-id.txt
@@ -20,8 +20,8 @@ block0:
[ 48] End value:reg5
-C$d8e5b9e0 super-computed-string-to-id.js:3:14
- Registers: 7
+C$793e3802 super-computed-string-to-id.js:3:14
+ Registers: 6
Blocks: 1
Constants:
[0] = Int32(1)
@@ -30,9 +30,9 @@ C$d8e5b9e0 super-computed-string-to-id.js:3:14
block0:
[ 0] Enter
[ 1] GetSuperConstructor dst:reg5
- [ 3] NewArray dst:reg6
- [ 6] SuperCallWithArgumentArray call_feedback:0, dst:reg5, super_constructor:reg5, arguments:reg6, is_synthetic:false
- [ d] ResolveThisBinding
- [ e] ResolveSuperBase dst:reg5
- [ 10] PutByIdWithThis base:reg5, this_value:this, `foo`, src:Int32(1), kind:Normal
- [ 19] End value:Undefined
+ [ 3] SuperCall call_feedback:0, dst:reg5, super_constructor:reg5
+ [ 9] BindThisFromSuperCall result:reg5
+ [ b] ResolveThisBinding
+ [ c] ResolveSuperBase dst:reg5
+ [ e] PutByIdWithThis base:reg5, this_value:this, `foo`, src:Int32(1), kind:Normal
+ [ 17] End value:Undefined
diff --git a/Tests/LibJS/Bytecode/expected/super-constructor-call.txt b/Tests/LibJS/Bytecode/expected/super-constructor-call.txt
index 7a847a8ab0c..8bb7bb97c06 100644
--- a/Tests/LibJS/Bytecode/expected/super-constructor-call.txt
+++ b/Tests/LibJS/Bytecode/expected/super-constructor-call.txt
@@ -26,8 +26,8 @@ block0:
[ 72] End value:reg5
-Derived$f734a90a super-constructor-call.js:4:14
- Registers: 7
+Derived$18ef1fb4 super-constructor-call.js:4:14
+ Registers: 6
Blocks: 1
Constants:
[0] = Undefined
@@ -35,9 +35,9 @@ Derived$f734a90a super-constructor-call.js:4:14
block0:
[ 0] Enter
[ 1] GetSuperConstructor dst:reg5
- [ 3] NewArray dst:reg6, elements:[arg0]
- [ 7] SuperCallWithArgumentArray call_feedback:0, dst:reg5, super_constructor:reg5, arguments:reg6, is_synthetic:false
- [ e] End value:Undefined
+ [ 3] SuperCall call_feedback:0, dst:reg5, super_constructor:reg5, arguments:[arg0]
+ [ a] BindThisFromSuperCall result:reg5
+ [ c] End value:Undefined
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 023cfb0b7d4..e42c0609e3e 100644
--- a/Tests/LibJS/Runtime/classes/class-super-call-paths.js
+++ b/Tests/LibJS/Runtime/classes/class-super-call-paths.js
@@ -253,3 +253,282 @@ describe("super property access", () => {
expect(other).toBe(3 * ITERATIONS);
});
});
+
+describe("super() with arguments as operands", () => {
+ test("this is bound once, also when the parent ran in an inline frame", () => {
+ class A {
+ constructor(x) {
+ this.x = x;
+ }
+ }
+ class B extends A {
+ constructor(mode) {
+ if (mode === "read before") this.y = 1;
+ super(1);
+ if (mode === "twice") super(2);
+ }
+ }
+ repeat(i => {
+ expect(new B("plain").x).toBe(1);
+ if (i % 100 === 0) {
+ expect(() => new B("read before")).toThrowWithMessage(
+ ReferenceError,
+ "|this| has not been initialized"
+ );
+ expect(() => new B("twice")).toThrowWithMessage(ReferenceError, "|this| is already initialized");
+ }
+ });
+ });
+
+ test("return overrides of a derived parent", () => {
+ class A {}
+ const object = {};
+ class P extends A {
+ constructor(mode) {
+ if (mode !== "no super") super();
+ if (mode === "object") return object;
+ if (mode === "number") return 1;
+ }
+ }
+ class B extends P {
+ constructor(mode) {
+ super(mode);
+ this.after = true;
+ }
+ }
+ repeat(i => {
+ const b = new B("plain");
+ expect(b).toBeInstanceOf(B);
+ expect(b.after).toBeTrue();
+ expect(new B("object")).toBe(object);
+ expect(object.after).toBeTrue();
+ if (i % 100 === 0) {
+ expect(() => new B("number")).toThrowWithMessage(TypeError, "Derived constructor return invalid value");
+ expect(() => new B("no super")).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
+ }
+ });
+ });
+
+ test("return overrides of a base parent", () => {
+ const object = {};
+ class A {
+ constructor(mode) {
+ this.a = 1;
+ if (mode === "object") return object;
+ if (mode === "number") return 1;
+ }
+ }
+ class B extends A {
+ constructor(mode) {
+ super(mode);
+ this.b = 2;
+ }
+ }
+ repeat(() => {
+ const b = new B("number");
+ expect(b).toBeInstanceOf(B);
+ expect(b.a).toBe(1);
+ expect(new B("object")).toBe(object);
+ });
+ });
+
+ test("fields and private methods are there right after super() returns", () => {
+ class A {
+ constructor(x) {
+ this.x = x;
+ }
+ }
+ class B extends A {
+ field = this.x + 1;
+ #secret() {
+ return this.field;
+ }
+ constructor(x) {
+ super(x);
+ this.seen = this.#secret();
+ }
+ }
+ class C extends B {
+ other = this.field * 2;
+ constructor(x) {
+ super(x);
+ this.seen2 = this.other;
+ }
+ }
+ repeat(i => {
+ const c = new C(i);
+ expect(c.x).toBe(i);
+ expect(c.field).toBe(i + 1);
+ expect(c.seen).toBe(i + 1);
+ expect(c.seen2).toBe(2 * (i + 1));
+ });
+ });
+
+ test("parents with fields of their own", () => {
+ class A {
+ a = 1;
+ constructor(x) {
+ this.x = x + this.a;
+ }
+ }
+ class B extends A {
+ constructor(x) {
+ super(x);
+ }
+ }
+ repeat(i => expect(new B(i).x).toBe(i + 1));
+ });
+
+ test("argument counts above and below the parameter count", () => {
+ class A {
+ constructor(a, b) {
+ this.a = a;
+ this.b = b;
+ this.count = arguments.length;
+ }
+ }
+ class Fewer extends A {
+ constructor() {
+ super(1);
+ }
+ }
+ class More extends A {
+ constructor() {
+ super(1, 2, 3, 4);
+ }
+ }
+ repeat(() => {
+ const fewer = new Fewer();
+ expect(fewer.b).toBeUndefined();
+ expect(fewer.count).toBe(1);
+ const more = new More();
+ expect(more.b).toBe(2);
+ expect(more.count).toBe(4);
+ });
+ });
+
+ test("three levels with an arrow and eval in the middle", () => {
+ class A {
+ constructor(x) {
+ this.x = x;
+ this.target = new.target;
+ }
+ }
+ class B extends A {
+ constructor(x) {
+ const call = () => super(x);
+ call();
+ }
+ }
+ class C extends B {
+ constructor(x) {
+ super(x);
+ }
+ }
+ class D extends A {
+ constructor(x) {
+ eval("super(x)");
+ }
+ }
+ class E extends D {
+ constructor(x) {
+ super(x);
+ }
+ }
+ repeat(i => {
+ const c = new C(i);
+ expect(c.x).toBe(i);
+ expect(c.target).toBe(C);
+ expect(new E(i).target).toBe(E);
+ });
+ });
+
+ test("new.target whose prototype comes from a getter or is not an object", () => {
+ class A {
+ constructor() {
+ this.a = 1;
+ }
+ }
+ class B extends A {
+ constructor() {
+ super();
+ }
+ }
+ let gets = 0;
+ const WithGetter = new Proxy(function () {}, {
+ get(target, key) {
+ if (key !== "prototype") return target[key];
+ gets++;
+ return Array.prototype;
+ },
+ });
+ function NotAnObject() {}
+ NotAnObject.prototype = 1;
+ repeat(() => {
+ expect(Object.getPrototypeOf(Reflect.construct(B, [], WithGetter))).toBe(Array.prototype);
+ expect(Object.getPrototypeOf(Reflect.construct(B, [], NotAnObject))).toBe(Object.prototype);
+ });
+ expect(gets).toBe(ITERATIONS);
+ });
+
+ test("parent changes while the derived constructor is hot", () => {
+ class A {
+ constructor() {
+ this.from = "A";
+ }
+ }
+ class B extends A {
+ constructor(x) {
+ super(x);
+ }
+ }
+ const NativeParent = function (x) {
+ return new Number(x);
+ };
+ repeat(i => {
+ if (i === ITERATIONS / 2) Object.setPrototypeOf(B, Array);
+ const b = new B(3);
+ if (i < ITERATIONS / 2) expect(b.from).toBe("A");
+ else {
+ expect(Array.isArray(b)).toBeTrue();
+ expect(b.length).toBe(3);
+ expect(b).toBeInstanceOf(B);
+ }
+ });
+ Object.setPrototypeOf(B, NativeParent);
+ expect(new B(5).valueOf()).toBe(5);
+ });
+
+ test("stack overflow in a recursive construct", () => {
+ class A {
+ constructor() {}
+ }
+ class B extends A {
+ constructor() {
+ super();
+ new B();
+ }
+ }
+ expect(() => new B()).toThrow(InternalError);
+ });
+
+ test("async and generator methods using super", async () => {
+ class A {
+ value() {
+ return 1;
+ }
+ }
+ class B extends A {
+ *generator() {
+ yield super.value();
+ }
+ async asynchronous() {
+ await null;
+ return super.value() + 1;
+ }
+ }
+ const b = new B();
+ repeat(() => expect(b.generator().next().value).toBe(1));
+ expect(await b.asynchronous()).toBe(2);
+ });
+});