Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HomeObject for private class methods #3897

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/engine/src/bytecompiler/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,21 +423,33 @@ impl ByteCompiler<'_> {
self.emit_with_varying_operand(Opcode::PushClassPrivateSetter, index);
}
MethodDefinition::Ordinary(expr) => {
self.emit(Opcode::RotateLeft, &[Operand::U8(3)]);
self.emit_opcode(Opcode::Dup);
self.emit(Opcode::RotateRight, &[Operand::U8(4)]);
Comment on lines +426 to +428
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought: I have seen this pattern in a lot of places. Maybe it would be good to extract this operation into a common compiler method. Something like dup_indexed or similar.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totaly agree. I think this might be something for the bytecode optimizer too. I would wait for #3798 until we work on that since that is probably gonna change some things.

self.method(expr.into());
let index = self.get_or_insert_private_name(*name);
self.emit_with_varying_operand(Opcode::PushClassPrivateMethod, index);
}
MethodDefinition::Async(expr) => {
self.emit(Opcode::RotateLeft, &[Operand::U8(3)]);
self.emit_opcode(Opcode::Dup);
self.emit(Opcode::RotateRight, &[Operand::U8(4)]);
self.method(expr.into());
let index = self.get_or_insert_private_name(*name);
self.emit_with_varying_operand(Opcode::PushClassPrivateMethod, index);
}
MethodDefinition::Generator(expr) => {
self.emit(Opcode::RotateLeft, &[Operand::U8(3)]);
self.emit_opcode(Opcode::Dup);
self.emit(Opcode::RotateRight, &[Operand::U8(4)]);
self.method(expr.into());
let index = self.get_or_insert_private_name(*name);
self.emit_with_varying_operand(Opcode::PushClassPrivateMethod, index);
}
MethodDefinition::AsyncGenerator(expr) => {
self.emit(Opcode::RotateLeft, &[Operand::U8(3)]);
self.emit_opcode(Opcode::Dup);
self.emit(Opcode::RotateRight, &[Operand::U8(4)]);
self.method(expr.into());
let index = self.get_or_insert_private_name(*name);
self.emit_with_varying_operand(Opcode::PushClassPrivateMethod, index);
Expand Down
2 changes: 1 addition & 1 deletion core/engine/src/vm/opcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ generate_opcodes! {
///
/// Operands: index: `u32`
///
/// Stack: class, method **=>**
/// Stack: class, class_proto, method **=>**
PushClassPrivateMethod { index: VaryingOperand },

/// Deletes a property by name of an object.
Expand Down
13 changes: 10 additions & 3 deletions core/engine/src/vm/opcode/push/class/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ impl PushClassPrivateMethod {
let name = context.vm.frame().code_block().constant_string(index);
let method = context.vm.pop();
let method_object = method.as_callable().expect("method must be callable");
let class_proto = context.vm.pop();
let class_proto_object = class_proto
.as_object()
.expect("class_proto must be function object");
let class = context.vm.pop();
let class_object = class.as_object().expect("class must be function object");

let name_string = js_string!(js_str!("#"), &name);
let desc = PropertyDescriptor::builder()
Expand All @@ -35,9 +41,10 @@ impl PushClassPrivateMethod {
&mut InternalMethodContext::new(context),
)
.expect("failed to set name property on private method");

let class = context.vm.pop();
let class_object = class.as_object().expect("class must be function object");
method_object
.downcast_mut::<OrdinaryFunction>()
.expect("method must be function object")
.set_home_object(class_proto_object.clone());

class_object
.downcast_mut::<OrdinaryFunction>()
Expand Down
Loading