Skip to content

Commit

Permalink
Fix this in function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Jun 30, 2022
1 parent 6b4ebf9 commit 4516249
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
27 changes: 26 additions & 1 deletion boa_engine/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2162,10 +2162,35 @@ impl<'b> ByteCompiler<'b> {
self.emit(Opcode::Swap, &[]);
self.emit(Opcode::GetPropertyByValue, &[]);
}
Node::GetSuperField(get_super_field) => {
if kind == CallKind::Call {
self.emit_opcode(Opcode::This);
}
self.emit_opcode(Opcode::Super);
match get_super_field {
GetSuperField::Const(field) => {
let index = self.get_or_insert_name(*field);
self.emit(Opcode::GetPropertyByName, &[index]);
}
GetSuperField::Expr(expr) => {
self.compile_expr(expr, true)?;
self.emit_opcode(Opcode::Swap);
self.emit_opcode(Opcode::GetPropertyByValue);
}
}
}
Node::GetPrivateField(get_private_field) => {
self.compile_expr(get_private_field.obj(), true)?;
if kind == CallKind::Call {
self.emit(Opcode::Dup, &[]);
}
let index = self.get_or_insert_name(get_private_field.field());
self.emit(Opcode::GetPrivateField, &[index]);
}
expr => {
self.compile_expr(expr, true)?;
if kind == CallKind::Call || kind == CallKind::CallEval {
self.emit_opcode(Opcode::This);
self.emit_opcode(Opcode::PushUndefined);
self.emit_opcode(Opcode::Swap);
}
}
Expand Down
24 changes: 4 additions & 20 deletions boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1707,17 +1707,13 @@ impl Context {
arguments.reverse();

let func = self.vm.pop();
let mut this = self.vm.pop();
let this = self.vm.pop();

let object = match func {
JsValue::Object(ref object) if object.is_callable() => object.clone(),
_ => return self.throw_type_error("not a callable function"),
};

if this.is_null_or_undefined() {
this = self.global_object().clone().into();
}

// A native function with the name "eval" implies, that is this the built-in eval function.
let eval = matches!(object.borrow().as_function(), Some(Function::Native { .. }));

Expand Down Expand Up @@ -1748,7 +1744,7 @@ impl Context {
}
arguments.reverse();
let func = self.vm.pop();
let mut this = self.vm.pop();
let this = self.vm.pop();

let iterator_record = rest_argument.get_iterator(self, None, None)?;
let mut rest_arguments = Vec::new();
Expand All @@ -1762,10 +1758,6 @@ impl Context {
_ => return self.throw_type_error("not a callable function"),
};

if this.is_null_or_undefined() {
this = self.global_object().clone().into();
}

// A native function with the name "eval" implies, that is this the built-in eval function.
let eval = matches!(object.borrow().as_function(), Some(Function::Native { .. }));

Expand Down Expand Up @@ -1796,17 +1788,13 @@ impl Context {
arguments.reverse();

let func = self.vm.pop();
let mut this = self.vm.pop();
let this = self.vm.pop();

let object = match func {
JsValue::Object(ref object) if object.is_callable() => object.clone(),
_ => return self.throw_type_error("not a callable function"),
};

if this.is_null_or_undefined() {
this = self.global_object().clone().into();
}

let result = object.__call__(&this, &arguments, self)?;

self.vm.push(result);
Expand All @@ -1823,7 +1811,7 @@ impl Context {
}
arguments.reverse();
let func = self.vm.pop();
let mut this = self.vm.pop();
let this = self.vm.pop();

let iterator_record = rest_argument.get_iterator(self, None, None)?;
let mut rest_arguments = Vec::new();
Expand All @@ -1837,10 +1825,6 @@ impl Context {
_ => return self.throw_type_error("not a callable function"),
};

if this.is_null_or_undefined() {
this = self.global_object().clone().into();
}

let result = object.__call__(&this, &arguments, self)?;

self.vm.push(result);
Expand Down

0 comments on commit 4516249

Please sign in to comment.