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

Refactor PropertyDescriptor #794

Merged
merged 6 commits into from
Oct 8, 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
5 changes: 2 additions & 3 deletions boa/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, Value},
object::ObjectData,
property::{Attribute, Property},
property::{Attribute, DataDescriptor},
BoaProfiler, Context, Result,
};
use gc::{Finalize, Trace};
Expand Down Expand Up @@ -131,8 +131,7 @@ impl ArrayIterator {
.set_prototype_instance(iterator_prototype);

let to_string_tag = ctx.well_known_symbols().to_string_tag_symbol();
let to_string_tag_property =
Property::data_descriptor(Value::string("Array Iterator"), Attribute::CONFIGURABLE);
let to_string_tag_property = DataDescriptor::new("Array Iterator", Attribute::CONFIGURABLE);
array_iterator.set_property(to_string_tag, to_string_tag_property);
array_iterator
}
Expand Down
10 changes: 5 additions & 5 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
builtins::array::array_iterator::{ArrayIterationKind, ArrayIterator},
builtins::BuiltIn,
object::{ConstructorBuilder, FunctionBuilder, ObjectData, PROTOTYPE},
property::{Attribute, Property},
property::{Attribute, DataDescriptor},
value::{same_value_zero, Value},
BoaProfiler, Context, Result,
};
Expand Down Expand Up @@ -129,8 +129,8 @@ impl Array {
}

// finally create length property
let length = Property::data_descriptor(
length.into(),
let length = DataDescriptor::new(
length,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
);

Expand Down Expand Up @@ -171,8 +171,8 @@ impl Array {
}

// Create length
let length = Property::data_descriptor(
array_contents.len().into(),
let length = DataDescriptor::new(
array_contents.len(),
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
);
array_obj_ptr.set_property("length".to_string(), length);
Expand Down
9 changes: 4 additions & 5 deletions boa/src/builtins/date/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ fn date_this_time_value() {
let message_property = &error
.get_property("message")
.expect("Expected 'message' property")
.value;
.as_data_descriptor()
.unwrap()
.value();

assert_eq!(
&Some(Value::string("\'this\' is not a Date")),
message_property
);
assert_eq!(Value::string("\'this\' is not a Date"), *message_property);
}

#[test]
Expand Down
18 changes: 9 additions & 9 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
builtins::{Array, BuiltIn},
environment::lexical_environment::Environment,
object::{ConstructorBuilder, FunctionBuilder, Object, ObjectData, PROTOTYPE},
property::{Attribute, Property},
property::{Attribute, DataDescriptor},
syntax::ast::node::{FormalParameter, RcStatementList},
BoaProfiler, Context, Result, Value,
};
Expand Down Expand Up @@ -174,16 +174,16 @@ pub fn create_unmapped_arguments_object(arguments_list: &[Value]) -> Value {
let len = arguments_list.len();
let mut obj = Object::default();
// Set length
let length = Property::data_descriptor(
len.into(),
let length = DataDescriptor::new(
len,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
);
// Define length as a property
obj.define_own_property("length", length);
obj.define_own_property("length", length.into());
let mut index: usize = 0;
while index < len {
let val = arguments_list.get(index).expect("Could not get argument");
let prop = Property::data_descriptor(
let prop = DataDescriptor::new(
val.clone(),
Attribute::WRITABLE | Attribute::ENUMERABLE | Attribute::CONFIGURABLE,
);
Expand Down Expand Up @@ -222,14 +222,14 @@ pub fn make_constructor_fn(
let mut constructor =
Object::function(function, global.get_field("Function").get_field(PROTOTYPE));

let length = Property::data_descriptor(
length.into(),
let length = DataDescriptor::new(
length,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
);
constructor.insert("length", length);

let name = Property::data_descriptor(
name.into(),
let name = DataDescriptor::new(
name,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
);
constructor.insert("name", name);
Expand Down
19 changes: 12 additions & 7 deletions boa/src/builtins/iterable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
builtins::string::string_iterator::StringIterator,
builtins::ArrayIterator,
object::{GcObject, ObjectInitializer},
property::Property,
property::{Attribute, DataDescriptor},
BoaProfiler, Context, Result, Value,
};

Expand Down Expand Up @@ -47,23 +47,25 @@ impl IteratorPrototypes {
/// Generates an object supporting the IteratorResult interface.
pub fn create_iter_result_object(ctx: &mut Context, value: Value, done: bool) -> Value {
let object = Value::new_object(Some(ctx.global_object()));
let value_property = Property::default().value(value);
let done_property = Property::default().value(Value::boolean(done));
// TODO: Fix attributes of value and done
let value_property = DataDescriptor::new(value, Attribute::all());
let done_property = DataDescriptor::new(done, Attribute::all());
object.set_property("value", value_property);
object.set_property("done", done_property);
object
}

/// Get an iterator record
pub fn get_iterator(ctx: &mut Context, iterable: Value) -> Result<IteratorRecord> {
// TODO: Fix the accessor handling
let iterator_function = iterable
.get_property(ctx.well_known_symbols().iterator_symbol())
.and_then(|mut p| p.value.take())
.map(|p| p.as_data_descriptor().unwrap().value())
.ok_or_else(|| ctx.construct_type_error("Not an iterable"))?;
let iterator_object = ctx.call(&iterator_function, &iterable, &[])?;
let next_function = iterator_object
.get_property("next")
.and_then(|mut p| p.value.take())
.map(|p| p.as_data_descriptor().unwrap().value())
.ok_or_else(|| ctx.construct_type_error("Could not find property `next`"))?;
Ok(IteratorRecord::new(iterator_object, next_function))
}
Expand Down Expand Up @@ -111,14 +113,17 @@ impl IteratorRecord {
/// [spec]: https://tc39.es/ecma262/#sec-iteratornext
pub(crate) fn next(&self, ctx: &mut Context) -> Result<IteratorResult> {
let next = ctx.call(&self.next_function, &self.iterator_object, &[])?;
// FIXME: handle accessor descriptors
let done = next
.get_property("done")
.and_then(|mut p| p.value.take())
.map(|p| p.as_data_descriptor().unwrap().value())
.and_then(|v| v.as_boolean())
.ok_or_else(|| ctx.construct_type_error("Could not find property `done`"))?;

// FIXME: handle accessor descriptors
let next_result = next
.get_property("value")
.and_then(|mut p| p.value.take())
.map(|p| p.as_data_descriptor().unwrap().value())
.unwrap_or_default();
Ok(IteratorResult::new(next_result, done))
}
Expand Down
21 changes: 13 additions & 8 deletions boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use crate::{
builtins::BuiltIn,
object::ObjectInitializer,
property::{Attribute, Property, PropertyKey},
property::{Attribute, DataDescriptor, PropertyKey},
BoaProfiler, Context, Result, Value,
};
use serde_json::{self, Value as JSONValue};
Expand Down Expand Up @@ -155,16 +155,20 @@ impl Json {
let object_to_return = Value::new_object(None);
for (key, val) in obj
.iter()
.filter_map(|(k, v)| v.value.as_ref().map(|value| (k, value)))
// FIXME: handle accessor descriptors
.map(|(k, v)| (k, v.as_data_descriptor().unwrap().value()))
{
let this_arg = object.clone();
object_to_return.set_property(
key.to_owned(),
Property::default().value(ctx.call(
replacer,
&this_arg,
&[Value::from(key.clone()), val.clone()],
)?),
DataDescriptor::new(
ctx.call(
replacer,
&this_arg,
&[Value::from(key.clone()), val.clone()],
)?,
Attribute::all(),
),
);
}
Ok(Value::from(object_to_return.to_json(ctx)?.to_string()))
Expand All @@ -182,7 +186,8 @@ impl Json {
for field in fields {
if let Some(value) = object
.get_property(field.to_string(ctx)?)
.and_then(|prop| prop.value.as_ref().map(|v| v.to_json(ctx)))
// FIXME: handle accessor descriptors
.map(|prop| prop.as_data_descriptor().unwrap().value().to_json(ctx))
.transpose()?
{
obj_to_return.insert(field.to_string(ctx)?.to_string(), value);
Expand Down
6 changes: 3 additions & 3 deletions boa/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
builtins::BuiltIn,
object::{ConstructorBuilder, ObjectData, PROTOTYPE},
property::{Attribute, Property},
property::{Attribute, DataDescriptor},
BoaProfiler, Context, Result, Value,
};
use ordered_map::OrderedMap;
Expand Down Expand Up @@ -100,8 +100,8 @@ impl Map {

/// Helper function to set the size property.
fn set_size(this: &Value, size: usize) {
let size = Property::data_descriptor(
size.into(),
let size = DataDescriptor::new(
size,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
);

Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) use self::{
undefined::Undefined,
};
use crate::{
property::{Attribute, Property},
property::{Attribute, DataDescriptor},
Context, Value,
};

Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn init(context: &mut Context) {

for init in &globals {
let (name, value, attribute) = init(context);
let property = Property::data_descriptor(value, attribute);
let property = DataDescriptor::new(value, attribute);
global_object.borrow_mut().insert(name, property);
}
}
19 changes: 11 additions & 8 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use crate::{
builtins::BuiltIn,
object::{ConstructorBuilder, Object as BuiltinObject, ObjectData},
property::{Attribute, Property},
property::Attribute,
value::{same_value, Value},
BoaProfiler, Context, Result,
};
Expand Down Expand Up @@ -134,13 +134,18 @@ impl Object {
}

/// Define a property in an object
pub fn define_property(_: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> {
pub fn define_property(_: &Value, args: &[Value], context: &mut Context) -> Result<Value> {
let obj = args.get(0).expect("Cannot get object");
let prop = args
.get(1)
.expect("Cannot get object")
.to_property_key(ctx)?;
let desc = Property::from(args.get(2).expect("Cannot get object"));
.to_property_key(context)?;

let desc = if let Value::Object(ref object) = args.get(2).cloned().unwrap_or_default() {
object.to_property_descriptor(context)?
} else {
return context.throw_type_error("Property description must be an object");
};
obj.set_property(prop, desc);
Ok(Value::undefined())
}
Expand Down Expand Up @@ -246,12 +251,10 @@ impl Object {
};

let key = key.to_property_key(ctx)?;
let own_property = this
.to_object(ctx)
.map(|obj| obj.borrow().get_own_property(&key));
let own_property = this.to_object(ctx)?.borrow().get_own_property(&key);

Ok(own_property.map_or(Value::from(false), |own_prop| {
Value::from(own_prop.enumerable_or(false))
Value::from(own_prop.enumerable())
}))
}
}
10 changes: 5 additions & 5 deletions boa/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
builtins::BuiltIn,
gc::{empty_trace, Finalize, Trace},
object::{ConstructorBuilder, ObjectData},
property::{Attribute, Property},
property::{Attribute, DataDescriptor},
value::{RcString, Value},
BoaProfiler, Context, Result,
};
Expand Down Expand Up @@ -370,9 +370,9 @@ impl RegExp {
let result = Value::from(result);
result.set_property(
"index",
Property::default().value(Value::from(m.total().start)),
DataDescriptor::new(m.total().start, Attribute::all()),
);
result.set_property("input", Property::default().value(Value::from(arg_str)));
result.set_property("input", DataDescriptor::new(arg_str, Attribute::all()));
result
} else {
if regex.use_last_index {
Expand Down Expand Up @@ -473,11 +473,11 @@ impl RegExp {

match_val.set_property(
"index",
Property::default().value(Value::from(mat.total().start)),
DataDescriptor::new(mat.total().start, Attribute::all()),
);
match_val.set_property(
"input",
Property::default().value(Value::from(arg_str.clone())),
DataDescriptor::new(arg_str.clone(), Attribute::all()),
);
matches.push(match_val);

Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/string/string_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::builtins::string::code_point_at;
use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object},
object::ObjectData,
property::{Attribute, Property},
property::{Attribute, DataDescriptor},
BoaProfiler, Context, Result, Value,
};
use gc::{Finalize, Trace};
Expand Down Expand Up @@ -82,7 +82,7 @@ impl StringIterator {

let to_string_tag = ctx.well_known_symbols().to_string_tag_symbol();
let to_string_tag_property =
Property::data_descriptor(Value::string("String Iterator"), Attribute::CONFIGURABLE);
DataDescriptor::new("String Iterator", Attribute::CONFIGURABLE);
array_iterator.set_property(to_string_tag, to_string_tag_property);
array_iterator
}
Expand Down
4 changes: 2 additions & 2 deletions boa/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
class::{Class, ClassBuilder},
exec::Interpreter,
object::{GcObject, Object, ObjectData, PROTOTYPE},
property::{Property, PropertyKey},
property::{DataDescriptor, PropertyKey},
realm::Realm,
syntax::{
ast::{
Expand Down Expand Up @@ -608,7 +608,7 @@ impl Context {
T::init(&mut class_builder)?;

let class = class_builder.build();
let property = Property::data_descriptor(class.into(), T::ATTRIBUTE);
let property = DataDescriptor::new(class, T::ATTRIBUTE);
self.global_object()
.as_object_mut()
.unwrap()
Expand Down
Loading