From 4f5930cc5eb7899bb9a2766bb7b885c09d7d8155 Mon Sep 17 00:00:00 2001 From: Halid Odat Date: Tue, 25 Aug 2020 10:15:44 +0200 Subject: [PATCH] Make `GcObject::contruct` not take 'this' (#660) --- boa/src/builtins/object/gcobject.rs | 12 +++++++----- boa/src/exec/new/mod.rs | 13 ++----------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/boa/src/builtins/object/gcobject.rs b/boa/src/builtins/object/gcobject.rs index 2dd0cbeb138..59dc448e657 100644 --- a/boa/src/builtins/object/gcobject.rs +++ b/boa/src/builtins/object/gcobject.rs @@ -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}, @@ -132,15 +132,17 @@ impl GcObject { } /// - pub fn construct(&self, this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub fn construct(&self, args: &[Value], ctx: &mut Interpreter) -> Result { + 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, @@ -152,7 +154,7 @@ impl GcObject { // 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() { diff --git a/boa/src/exec/new/mod.rs b/boa/src/exec/new/mod.rs index 5bb6bec9e30..eb038481576 100644 --- a/boa/src/exec/new/mod.rs +++ b/boa/src/exec/new/mod.rs @@ -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 { @@ -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()), } }