Skip to content

Commit

Permalink
Change Value::new_object so that it does not use global
Browse files Browse the repository at this point in the history
  • Loading branch information
tofpie committed Dec 29, 2020
1 parent 9e5379e commit 92e4fc1
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 61 deletions.
7 changes: 3 additions & 4 deletions boa/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ArrayIterator {
array: Value,
kind: ArrayIterationKind,
) -> Result<Value> {
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()
Expand Down Expand Up @@ -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,
)?;
Expand All @@ -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()
Expand Down
11 changes: 1 addition & 10 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,7 @@ impl Array {

/// Creates a new `Array` instance.
pub(crate) fn new_array(context: &Context) -> Result<Value> {
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()
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/iterable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))
}
Expand Down
5 changes: 2 additions & 3 deletions boa/src/builtins/map/map_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl MapIterator {
map: Value,
kind: MapIterationKind,
) -> Result<Value> {
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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] )`
Expand Down
5 changes: 2 additions & 3 deletions boa/src/builtins/string/string_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl StringIterator {
}

pub fn create_string_iterator(context: &mut Context, string: Value) -> Result<Value> {
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()
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 4 additions & 13 deletions boa/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 1 addition & 6 deletions boa/src/syntax/ast/node/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ impl Object {

impl Executable for Object {
fn run(&self, context: &mut Context) -> Result<Value> {
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() {
Expand Down
18 changes: 3 additions & 15 deletions boa/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions boa/src/value/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();
Expand Down

0 comments on commit 92e4fc1

Please sign in to comment.