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

Make GcObject::contruct not take 'this' #660

Merged
merged 1 commit into from
Aug 25, 2020
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: 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