Skip to content

Commit

Permalink
Fix super property access
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Jun 17, 2023
1 parent dfba57e commit 6884031
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 36 deletions.
4 changes: 2 additions & 2 deletions boa_engine/src/bytecompiler/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ impl ByteCompiler<'_, '_> {
self.functions.push(code);
self.emit(Opcode::GetFunction, &[index]);
self.emit_u8(0);
self.emit_opcode(Opcode::SetHomeObject);
self.emit_opcode(Opcode::SetHomeObjectClass);
self.emit(Opcode::Call, &[0]);
if let Some(name_index) = name_index {
self.emit(Opcode::DefineOwnPropertyByName, &[name_index]);
Expand Down Expand Up @@ -401,7 +401,7 @@ impl ByteCompiler<'_, '_> {
self.functions.push(code);
self.emit(Opcode::GetFunction, &[index]);
self.emit_u8(0);
self.emit_opcode(Opcode::SetHomeObject);
self.emit_opcode(Opcode::SetHomeObjectClass);
self.emit(Opcode::Call, &[0]);
self.emit_opcode(Opcode::Pop);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl ByteCompiler<'_, '_> {
name,
default_init,
} => {
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
match name {
PropertyName::Literal(name) => {
Expand Down Expand Up @@ -106,6 +107,7 @@ impl ByteCompiler<'_, '_> {
access,
default_init,
} => {
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
match name {
PropertyName::Literal(name) => {
Expand Down Expand Up @@ -145,6 +147,7 @@ impl ByteCompiler<'_, '_> {
pattern,
default_init,
} => {
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
match name {
PropertyName::Literal(name) => {
Expand Down
13 changes: 11 additions & 2 deletions boa_engine/src/bytecompiler/expression/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl ByteCompiler<'_, '_> {
self.compile_expr(access.target(), true);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);

self.emit(Opcode::GetPropertyByName, &[index]);
if short_circuit {
Expand All @@ -119,11 +120,13 @@ impl ByteCompiler<'_, '_> {
PropertyAccessField::Expr(expr) => {
self.compile_expr(access.target(), true);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
self.compile_expr(expr, true);

self.emit_opcode(Opcode::GetPropertyByValuePush);
if short_circuit {
pop_count = 2;
pop_count = 3;
early_exit = Some(self.emit_opcode_with_operand(opcode));
self.compile_expr(assign.rhs(), true);
} else {
Expand Down Expand Up @@ -164,6 +167,7 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::This);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::This);

self.emit(Opcode::GetPropertyByName, &[index]);
if short_circuit {
Expand All @@ -183,6 +187,7 @@ impl ByteCompiler<'_, '_> {
PropertyAccessField::Expr(expr) => {
self.emit_opcode(Opcode::Super);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::This);
self.compile_expr(expr, true);

self.emit_opcode(Opcode::GetPropertyByValuePush);
Expand All @@ -195,7 +200,11 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(opcode);
}

self.emit(Opcode::SetPropertyByValue, &[]);
self.emit_opcode(Opcode::This);
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(2);

self.emit_opcode(Opcode::SetPropertyByValue);
if !use_expr {
self.emit_opcode(Opcode::Pop);
}
Expand Down
5 changes: 3 additions & 2 deletions boa_engine/src/bytecompiler/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,16 @@ impl ByteCompiler<'_, '_> {
match template.tag() {
Expression::PropertyAccess(PropertyAccess::Simple(access)) => {
self.compile_expr(access.target(), true);
self.emit(Opcode::Dup, &[]);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
match access.field() {
PropertyAccessField::Const(field) => {
let index = self.get_or_insert_name((*field).into());
self.emit(Opcode::GetPropertyByName, &[index]);
}
PropertyAccessField::Expr(field) => {
self.compile_expr(field, true);
self.emit(Opcode::GetPropertyByValue, &[]);
self.emit_opcode(Opcode::GetPropertyByValue);
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions boa_engine/src/bytecompiler/expression/object_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl ByteCompiler<'_, '_> {
MethodDefinition::Get(expr) => match name {
PropertyName::Literal(name) => {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetHomeObject);
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::SetPropertyGetterByName, &[index]);
}
Expand All @@ -57,12 +58,20 @@ impl ByteCompiler<'_, '_> {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetFunctionName);
self.emit_u8(1);
self.emit_opcode(Opcode::RotateLeft);
self.emit_u8(3);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::SetHomeObject);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(3);
self.emit_opcode(Opcode::SetPropertyGetterByValue);
}
},
MethodDefinition::Set(expr) => match name {
PropertyName::Literal(name) => {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetHomeObject);
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::SetPropertySetterByName, &[index]);
}
Expand All @@ -73,12 +82,20 @@ impl ByteCompiler<'_, '_> {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetFunctionName);
self.emit_u8(2);
self.emit_opcode(Opcode::RotateLeft);
self.emit_u8(3);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::SetHomeObject);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(3);
self.emit_opcode(Opcode::SetPropertySetterByValue);
}
},
MethodDefinition::Ordinary(expr) => match name {
PropertyName::Literal(name) => {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetHomeObject);
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::DefineOwnPropertyByName, &[index]);
}
Expand All @@ -89,12 +106,20 @@ impl ByteCompiler<'_, '_> {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetFunctionName);
self.emit_u8(0);
self.emit_opcode(Opcode::RotateLeft);
self.emit_u8(3);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::SetHomeObject);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(3);
self.emit_opcode(Opcode::DefineOwnPropertyByValue);
}
},
MethodDefinition::Async(expr) => match name {
PropertyName::Literal(name) => {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetHomeObject);
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::DefineOwnPropertyByName, &[index]);
}
Expand All @@ -105,12 +130,20 @@ impl ByteCompiler<'_, '_> {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetFunctionName);
self.emit_u8(0);
self.emit_opcode(Opcode::RotateLeft);
self.emit_u8(3);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::SetHomeObject);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(3);
self.emit_opcode(Opcode::DefineOwnPropertyByValue);
}
},
MethodDefinition::Generator(expr) => match name {
PropertyName::Literal(name) => {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetHomeObject);
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::DefineOwnPropertyByName, &[index]);
}
Expand All @@ -121,12 +154,20 @@ impl ByteCompiler<'_, '_> {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetFunctionName);
self.emit_u8(0);
self.emit_opcode(Opcode::RotateLeft);
self.emit_u8(3);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::SetHomeObject);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(3);
self.emit_opcode(Opcode::DefineOwnPropertyByValue);
}
},
MethodDefinition::AsyncGenerator(expr) => match name {
PropertyName::Literal(name) => {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetHomeObject);
let index = self.get_or_insert_name((*name).into());
self.emit(Opcode::DefineOwnPropertyByName, &[index]);
}
Expand All @@ -137,6 +178,13 @@ impl ByteCompiler<'_, '_> {
self.object_method(expr.into());
self.emit_opcode(Opcode::SetFunctionName);
self.emit_u8(0);
self.emit_opcode(Opcode::RotateLeft);
self.emit_u8(3);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::SetHomeObject);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(3);
self.emit_opcode(Opcode::DefineOwnPropertyByValue);
}
},
Expand Down
11 changes: 10 additions & 1 deletion boa_engine/src/bytecompiler/expression/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl ByteCompiler<'_, '_> {
self.compile_expr(access.target(), true);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);

self.emit(Opcode::GetPropertyByName, &[index]);
self.emit_opcode(opcode);
Expand All @@ -81,13 +82,15 @@ impl ByteCompiler<'_, '_> {
PropertyAccessField::Expr(expr) => {
self.compile_expr(access.target(), true);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::Dup);
self.compile_expr(expr, true);

self.emit_opcode(Opcode::GetPropertyByValuePush);
self.emit_opcode(opcode);
if post {
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(4);
self.emit_u8(5);
}

self.emit_opcode(Opcode::SetPropertyByValue);
Expand Down Expand Up @@ -120,6 +123,7 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::This);
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::This);

self.emit(Opcode::GetPropertyByName, &[index]);
self.emit_opcode(opcode);
Expand All @@ -136,6 +140,7 @@ impl ByteCompiler<'_, '_> {
PropertyAccessField::Expr(expr) => {
self.emit_opcode(Opcode::Super);
self.emit_opcode(Opcode::Dup);
self.emit_opcode(Opcode::This);
self.compile_expr(expr, true);

self.emit_opcode(Opcode::GetPropertyByValuePush);
Expand All @@ -145,6 +150,10 @@ impl ByteCompiler<'_, '_> {
self.emit_u8(2);
}

self.emit_opcode(Opcode::This);
self.emit_opcode(Opcode::RotateRight);
self.emit_u8(2);

self.emit_opcode(Opcode::SetPropertyByValue);
if post {
self.emit_opcode(Opcode::Pop);
Expand Down
Loading

0 comments on commit 6884031

Please sign in to comment.