diff --git a/boa/src/builtins/array/array_iterator.rs b/boa/src/builtins/array/array_iterator.rs index d46f79da8b1..729f012b933 100644 --- a/boa/src/builtins/array/array_iterator.rs +++ b/boa/src/builtins/array/array_iterator.rs @@ -50,7 +50,7 @@ impl ArrayIterator { array: Value, kind: ArrayIterationKind, ) -> Result { - let array_iterator = Value::new_object(Some(context.global_object()), context); + let array_iterator = Value::new_object(context); array_iterator.set_data(ObjectData::ArrayIterator(Self::new(array, kind))); array_iterator .as_object() @@ -97,7 +97,7 @@ impl ArrayIterator { ArrayIterationKind::KeyAndValue => { let element_value = array_iterator.array.get_field(index, context)?; let result = Array::constructor( - &Value::new_object(Some(context.global_object()), context), + &Value::new_object(context), &[index.into(), element_value], context, )?; @@ -119,11 +119,10 @@ impl ArrayIterator { /// /// [spec]: https://tc39.es/ecma262/#sec-%arrayiteratorprototype%-object pub(crate) fn create_prototype(context: &mut Context, iterator_prototype: Value) -> Value { - let global = context.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); // Create prototype - let array_iterator = Value::new_object(Some(global), context); + let array_iterator = Value::new_object(context); make_builtin_fn(Self::next, "next", &array_iterator, 0, context); array_iterator .as_object() diff --git a/boa/src/builtins/array/mod.rs b/boa/src/builtins/array/mod.rs index 1d364fa743b..dafb17ec0fb 100644 --- a/boa/src/builtins/array/mod.rs +++ b/boa/src/builtins/array/mod.rs @@ -217,16 +217,7 @@ impl Array { /// Creates a new `Array` instance. pub(crate) fn new_array(context: &Context) -> Result { - let array = Value::new_object( - Some( - &context - .realm() - .environment - .get_global_object() - .expect("Could not get global object"), - ), - context, - ); + let array = Value::new_object(context); array.set_data(ObjectData::Array); array .as_object() diff --git a/boa/src/builtins/iterable/mod.rs b/boa/src/builtins/iterable/mod.rs index 54273788d75..d99ee418cd6 100644 --- a/boa/src/builtins/iterable/mod.rs +++ b/boa/src/builtins/iterable/mod.rs @@ -58,7 +58,7 @@ impl IteratorPrototypes { /// /// Generates an object supporting the IteratorResult interface. pub fn create_iter_result_object(context: &mut Context, value: Value, done: bool) -> Value { - let object = Value::new_object(Some(context.global_object()), context); + let object = Value::new_object(context); // TODO: Fix attributes of value and done let value_property = DataDescriptor::new(value, Attribute::all()); let done_property = DataDescriptor::new(done, Attribute::all()); diff --git a/boa/src/builtins/json/mod.rs b/boa/src/builtins/json/mod.rs index 84c00cd6152..85d766f30e2 100644 --- a/boa/src/builtins/json/mod.rs +++ b/boa/src/builtins/json/mod.rs @@ -75,7 +75,7 @@ impl Json { let j = Value::from_json(json, context); match args.get(1) { Some(reviver) if reviver.is_function() => { - let mut holder = Value::new_object(None, context); + let mut holder = Value::object(Object::default()); holder.set_field("", j, context)?; Self::walk(reviver, context, &mut holder, &PropertyKey::from("")) } diff --git a/boa/src/builtins/map/map_iterator.rs b/boa/src/builtins/map/map_iterator.rs index 2d2a582d00a..a02456d34d7 100644 --- a/boa/src/builtins/map/map_iterator.rs +++ b/boa/src/builtins/map/map_iterator.rs @@ -50,7 +50,7 @@ impl MapIterator { map: Value, kind: MapIterationKind, ) -> Result { - let map_iterator = Value::new_object(Some(context.global_object()), context); + let map_iterator = Value::new_object(context); map_iterator.set_data(ObjectData::MapIterator(Self::new(map, kind))); map_iterator .as_object() @@ -139,11 +139,10 @@ impl MapIterator { /// /// [spec]: https://tc39.es/ecma262/#sec-%mapiteratorprototype%-object pub(crate) fn create_prototype(context: &mut Context, iterator_prototype: Value) -> Value { - let global = context.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); // Create prototype - let map_iterator = Value::new_object(Some(global), context); + let map_iterator = Value::new_object(context); make_builtin_fn(Self::next, "next", &map_iterator, 0, context); map_iterator .as_object() diff --git a/boa/src/builtins/object/mod.rs b/boa/src/builtins/object/mod.rs index af7ec496f1c..c13eff4b173 100644 --- a/boa/src/builtins/object/mod.rs +++ b/boa/src/builtins/object/mod.rs @@ -83,9 +83,7 @@ impl Object { return Ok(arg.to_object(context)?.into()); } } - let global = context.global_object(); - - Ok(Value::new_object(Some(global), context)) + Ok(Value::new_object(context)) } /// `Object.create( proto, [propertiesObject] )` diff --git a/boa/src/builtins/string/string_iterator.rs b/boa/src/builtins/string/string_iterator.rs index 73c8cebd61c..e1a56748015 100644 --- a/boa/src/builtins/string/string_iterator.rs +++ b/boa/src/builtins/string/string_iterator.rs @@ -23,7 +23,7 @@ impl StringIterator { } pub fn create_string_iterator(context: &mut Context, string: Value) -> Result { - let string_iterator = Value::new_object(Some(context.global_object()), context); + let string_iterator = Value::new_object(context); string_iterator.set_data(ObjectData::StringIterator(Self::new(string))); string_iterator .as_object() @@ -70,11 +70,10 @@ impl StringIterator { /// /// [spec]: https://tc39.es/ecma262/#sec-%arrayiteratorprototype%-object pub(crate) fn create_prototype(context: &mut Context, iterator_prototype: Value) -> Value { - let global = context.global_object(); let _timer = BoaProfiler::global().start_event("String Iterator", "init"); // Create prototype - let array_iterator = Value::new_object(Some(global), context); + let array_iterator = Value::new_object(context); make_builtin_fn(Self::next, "next", &array_iterator, 0, context); array_iterator .as_object() diff --git a/boa/src/context.rs b/boa/src/context.rs index d27fb84608b..c56e7170d73 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -315,7 +315,7 @@ impl Context { /// Construct an empty object. #[inline] - pub fn construct_object(&mut self) -> GcObject { + pub fn construct_object(&self) -> GcObject { let object_prototype: Value = self.standard_objects().object_object().prototype().into(); GcObject::new(Object::create(object_prototype)) } @@ -486,7 +486,7 @@ impl Context { self.standard_objects().function_object().prototype().into(); // Every new function has a prototype property pre-made - let proto = Value::new_object(Some(self.global_object()), self); + let proto = Value::new_object(self); let params = params.into(); let params_len = params.len(); @@ -520,7 +520,7 @@ impl Context { let function_prototype: Value = self.standard_objects().object_object().prototype().into(); // Every new function has a prototype property pre-made - let proto = Value::new_object(Some(self.global_object()), self); + let proto = Value::new_object(self); let mut function = GcObject::new(Object::function( Function::BuiltIn(body.into(), FunctionFlags::CALLABLE), function_prototype, @@ -572,16 +572,7 @@ impl Context { .iter() .map(|(key, value)| { // Construct a new array containing the key-value pair - let array = Value::new_object( - Some( - &self - .realm() - .environment - .get_global_object() - .expect("Could not get global object"), - ), - self, - ); + let array = Value::new_object(self); array.set_data(ObjectData::Array); array.as_object().expect("object").set_prototype_instance( self.realm() diff --git a/boa/src/syntax/ast/node/object/mod.rs b/boa/src/syntax/ast/node/object/mod.rs index a30336e851a..cb522892d30 100644 --- a/boa/src/syntax/ast/node/object/mod.rs +++ b/boa/src/syntax/ast/node/object/mod.rs @@ -73,12 +73,7 @@ impl Object { impl Executable for Object { fn run(&self, context: &mut Context) -> Result { - let global_val = &context - .realm() - .environment - .get_global_object() - .expect("Could not get the global object"); - let obj = Value::new_object(Some(global_val), context); + let obj = Value::new_object(context); // TODO: Implement the rest of the property types. for property in self.properties().iter() { diff --git a/boa/src/value/mod.rs b/boa/src/value/mod.rs index 87ef209b1eb..2e79a1e65b9 100644 --- a/boa/src/value/mod.rs +++ b/boa/src/value/mod.rs @@ -158,21 +158,9 @@ impl Value { } /// Returns a new empty object - pub fn new_object(global: Option<&Value>, context: &Context) -> Self { + pub fn new_object(context: &Context) -> Self { let _timer = BoaProfiler::global().start_event("new_object", "value"); - - if global.is_some() { - let object = Object::create( - context - .standard_objects() - .object_object() - .prototype() - .into(), - ); - Self::object(object) - } else { - Self::object(Object::default()) - } + context.construct_object().into() } /// Convert from a JSON value to a JS value @@ -209,7 +197,7 @@ impl Value { new_obj } JSONValue::Object(obj) => { - let new_obj = Value::new_object(Some(context.global_object()), context); + let new_obj = Value::new_object(context); for (key, json) in obj.into_iter() { let value = Self::from_json(json, context); new_obj.set_property( diff --git a/boa/src/value/tests.rs b/boa/src/value/tests.rs index 386229e9fe0..0fef0eb1f4c 100644 --- a/boa/src/value/tests.rs +++ b/boa/src/value/tests.rs @@ -9,7 +9,7 @@ use std::hash::{Hash, Hasher}; #[test] fn is_object() { let context = Context::new(); - let val = Value::new_object(None, &context); + let val = Value::new_object(&context); assert_eq!(val.is_object(), true); } @@ -31,7 +31,7 @@ fn undefined() { #[test] fn get_set_field() { let mut context = Context::new(); - let obj = Value::new_object(None, &context); + let obj = Value::new_object(&context); // Create string and convert it to a Value let s = Value::from("bar"); obj.set_field("foo", s, &mut context).unwrap();