Skip to content

Commit

Permalink
Make GcObject::contruct not take 'this' (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Aug 25, 2020
1 parent 14d7791 commit 4f5930c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
12 changes: 7 additions & 5 deletions boa/src/builtins/object/gcobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! The `GcObject` is a garbage collected Object.

use super::Object;
use super::{Object, PROTOTYPE};
use crate::{
builtins::{
function::{create_unmapped_arguments_object, BuiltInFunction, Function},
Expand Down Expand Up @@ -132,15 +132,17 @@ impl GcObject {
}

/// <https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget>
pub fn construct(&self, this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
pub fn construct(&self, args: &[Value], ctx: &mut Interpreter) -> Result<Value> {
let this = Object::create(self.borrow().get(&PROTOTYPE.into())).into();

let this_function_object = self.clone();
let object = self.borrow();
if let Some(function) = object.as_function() {
if function.is_constructable() {
match function {
Function::BuiltIn(BuiltInFunction(function), _) => {
function(this, args, ctx)?;
Ok(this.clone())
function(&this, args, ctx)?;
Ok(this)
}
Function::Ordinary {
body,
Expand All @@ -152,7 +154,7 @@ impl GcObject {
// <https://tc39.es/ecma262/#sec-prepareforordinarycall>
let local_env = new_function_environment(
this_function_object,
Some(this.clone()),
Some(this),
Some(environment.clone()),
// Arrow functions do not have a this binding https://tc39.es/ecma262/#sec-function-environment-records
if flags.is_lexical_this_mode() {
Expand Down
13 changes: 2 additions & 11 deletions boa/src/exec/new/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use super::{Executable, Interpreter};
use crate::{
builtins::{object::PROTOTYPE, Value},
syntax::ast::node::New,
BoaProfiler, Result,
};
use crate::{builtins::Value, syntax::ast::node::New, BoaProfiler, Result};

impl Executable for New {
fn run(&self, interpreter: &mut Interpreter) -> Result<Value> {
Expand All @@ -14,14 +10,9 @@ impl Executable for New {
for arg in self.args() {
v_args.push(arg.run(interpreter)?);
}
let this = Value::new_object(None);
// Create a blank object, then set its __proto__ property to the [Constructor].prototype
this.as_object_mut()
.expect("this was not an object")
.set_prototype(func_object.get_field(PROTOTYPE));

match func_object {
Value::Object(ref object) => object.construct(&this, &v_args, interpreter),
Value::Object(ref object) => object.construct(&v_args, interpreter),
_ => Ok(Value::undefined()),
}
}
Expand Down

0 comments on commit 4f5930c

Please sign in to comment.