Skip to content

Commit

Permalink
Refactor PropertyDescriptor to follow the spec more closely
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Jul 26, 2021
1 parent 7c046ea commit 80cdbea
Show file tree
Hide file tree
Showing 53 changed files with 1,631 additions and 1,252 deletions.
18 changes: 14 additions & 4 deletions boa/examples/classes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use boa::{
class::{Class, ClassBuilder},
gc::{Finalize, Trace},
property::Attribute,
property::PropertyDescriptor,
Context, Result, Value,
};

Expand Down Expand Up @@ -104,14 +104,24 @@ impl Class for Person {

// Add a inherited property with the value `10`, with default attribute.
// (`READONLY, NON_ENUMERABLE, PERMANENT).
class.property("inheritedProperty", 10, Attribute::default());
class.property(
"inheritedProperty",
PropertyDescriptor::builder()
.value(10)
.writable(false)
.enumerable(false)
.configurable(false),
);

// Add a static property with the value `"Im a static property"`, with default attribute.
// (`WRITABLE, ENUMERABLE, PERMANENT`).
class.static_property(
"staticProperty",
"Im a static property",
Attribute::WRITABLE | Attribute::ENUMERABLE | Attribute::PERMANENT,
PropertyDescriptor::builder()
.value("Im a static property")
.writable(true)
.enumerable(true)
.configurable(false),
);

Ok(())
Expand Down
11 changes: 6 additions & 5 deletions boa/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, Value},
gc::{Finalize, Trace},
object::{GcObject, ObjectData},
property::{Attribute, DataDescriptor},
property::PropertyDescriptor,
symbol::WellKnownSymbols,
BoaProfiler, Context, Result,
};
Expand Down Expand Up @@ -128,10 +128,11 @@ impl ArrayIterator {
array_iterator.set_prototype_instance(iterator_prototype);

let to_string_tag = WellKnownSymbols::to_string_tag();
let to_string_tag_property = DataDescriptor::new(
"Array Iterator",
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
);
let to_string_tag_property = PropertyDescriptor::builder()
.value("Array Iterator")
.writable(false)
.enumerable(false)
.configurable(true);
array_iterator.insert(to_string_tag, to_string_tag_property);
array_iterator
}
Expand Down
134 changes: 98 additions & 36 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
builtins::BuiltIn,
builtins::Number,
object::{ConstructorBuilder, FunctionBuilder, GcObject, ObjectData, PROTOTYPE},
property::{Attribute, DataDescriptor},
property::PropertyDescriptor,
symbol::WellKnownSymbols,
value::{IntegerOrInfinity, Value},
BoaProfiler, Context, JsString, Result,
Expand All @@ -36,11 +36,7 @@ pub(crate) struct Array;
impl BuiltIn for Array {
const NAME: &'static str = "Array";

fn attribute() -> Attribute {
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE
}

fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
fn init(context: &mut Context) -> (&'static str, PropertyDescriptor) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let symbol_iterator = WellKnownSymbols::iterator();
Expand All @@ -65,26 +61,35 @@ impl BuiltIn for Array {
)
.name(Self::NAME)
.length(Self::LENGTH)
.static_accessor(
.static_property(
WellKnownSymbols::species(),
Some(get_species),
None,
Attribute::CONFIGURABLE,
PropertyDescriptor::builder()
.get(get_species)
.configurable(true),
)
.property(
"length",
0,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
PropertyDescriptor::builder()
.value(0)
.writable(true)
.enumerable(false)
.configurable(false),
)
.property(
"values",
values_function.clone(),
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
PropertyDescriptor::builder()
.value(values_function.clone())
.writable(true)
.enumerable(false)
.configurable(true),
)
.property(
symbol_iterator,
values_function,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
PropertyDescriptor::builder()
.value(values_function)
.writable(true)
.enumerable(false)
.configurable(true),
)
.method(Self::concat, "concat", 1)
.method(Self::push, "push", 1)
Expand Down Expand Up @@ -118,7 +123,15 @@ impl BuiltIn for Array {
.static_method(Self::of, "of", 0)
.build();

(Self::NAME, array.into(), Self::attribute())
(
Self::NAME,
PropertyDescriptor::builder()
.value(array)
.writable(true)
.enumerable(false)
.configurable(true)
.build(),
)
}
}

Expand Down Expand Up @@ -231,11 +244,16 @@ impl Array {
array.borrow_mut().data = ObjectData::Array;

// 6. Perform ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
let length = DataDescriptor::new(
length as f64,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,

array.ordinary_define_own_property(
"length".into(),
PropertyDescriptor::builder()
.value(length as f64)
.writable(true)
.enumerable(false)
.configurable(false)
.build(),
);
array.ordinary_define_own_property("length".into(), length.into());

Ok(array)
}
Expand All @@ -248,11 +266,15 @@ impl Array {
.as_object()
.expect("'array' should be an object")
.set_prototype_instance(context.standard_objects().array_object().prototype().into());
let length = DataDescriptor::new(
Value::from(0),
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
array.set_property(
"length",
PropertyDescriptor::builder()
.value(0)
.writable(true)
.enumerable(false)
.configurable(false)
.build(),
);
array.set_property("length", length);
array
}

Expand All @@ -274,14 +296,25 @@ impl Array {
}

// Create length
let length = DataDescriptor::new(
array_contents.len(),
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
array_obj_ptr.set_property(
"length".to_string(),
PropertyDescriptor::builder()
.value(array_contents.len())
.writable(true)
.enumerable(false)
.configurable(false)
.build(),
);
array_obj_ptr.set_property("length".to_string(), length);

for (n, value) in array_contents.iter().enumerate() {
array_obj_ptr.set_property(n, DataDescriptor::new(value, Attribute::all()));
array_obj_ptr.set_property(
n,
PropertyDescriptor::builder()
.value(value)
.configurable(true)
.enumerable(true)
.writable(true),
);
}
Ok(array_obj_ptr)
}
Expand Down Expand Up @@ -371,7 +404,14 @@ impl Array {

for (n, value) in add_values.iter().enumerate() {
let new_index = orig_length.wrapping_add(n);
array_ptr.set_property(new_index, DataDescriptor::new(value, Attribute::all()));
array_ptr.set_property(
new_index,
PropertyDescriptor::builder()
.value(value)
.configurable(true)
.enumerable(true)
.writable(true),
);
}

array_ptr.set_field(
Expand Down Expand Up @@ -982,7 +1022,12 @@ impl Array {
// iii. Perform ? CreateDataPropertyOrThrow(A, Pk, mappedValue).
arr.ordinary_define_own_property(
k.into(),
DataDescriptor::new(value, Attribute::all()).into(),
PropertyDescriptor::builder()
.value(value)
.configurable(true)
.enumerable(true)
.writable(true)
.into(),
);
}
// d. Set k to k + 1.
Expand Down Expand Up @@ -1387,8 +1432,14 @@ impl Array {
}

// 2. Perform CreateDataPropertyOrThrow(target, targetIndex, element)
target
.set_property(target_index, DataDescriptor::new(element, Attribute::all()));
target.set_property(
target_index,
PropertyDescriptor::builder()
.value(element)
.configurable(true)
.enumerable(true)
.writable(true),
);

// 3. Set targetIndex to targetIndex + 1
target_index = target_index.saturating_add(1);
Expand Down Expand Up @@ -1422,7 +1473,14 @@ impl Array {
let fin = Self::get_relative_end(context, args.get(2), len)?;

for i in start..fin {
this.set_property(i, DataDescriptor::new(value.clone(), Attribute::all()));
this.set_property(
i,
PropertyDescriptor::builder()
.value(value.clone())
.configurable(true)
.enumerable(true)
.writable(true),
);
}

Ok(this.clone())
Expand Down Expand Up @@ -1504,7 +1562,11 @@ impl Array {
for i in from..from.saturating_add(span) {
new_array.set_property(
new_array_len,
DataDescriptor::new(this.get_field(i, context)?, Attribute::all()),
PropertyDescriptor::builder()
.value(this.get_field(i, context)?)
.configurable(true)
.enumerable(true)
.writable(true),
);
new_array_len = new_array_len.saturating_add(1);
}
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/array/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1542,5 +1542,5 @@ fn array_length_is_not_enumerable() {

let array = Array::new_array(&context);
let desc = array.get_property("length").unwrap();
assert!(!desc.enumerable());
assert!(!desc.expect_enumerable());
}
25 changes: 16 additions & 9 deletions boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use crate::{
builtins::BuiltIn,
object::{ConstructorBuilder, ObjectData},
property::Attribute,
property::PropertyDescriptor,
symbol::WellKnownSymbols,
BoaProfiler, Context, JsBigInt, Result, Value,
};
Expand All @@ -29,11 +29,7 @@ pub struct BigInt;
impl BuiltIn for BigInt {
const NAME: &'static str = "BigInt";

fn attribute() -> Attribute {
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE
}

fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
fn init(context: &mut Context) -> (&'static str, PropertyDescriptor) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let to_string_tag = WellKnownSymbols::to_string_tag();
Expand All @@ -53,12 +49,23 @@ impl BuiltIn for BigInt {
.constructable(false)
.property(
to_string_tag,
Self::NAME,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
PropertyDescriptor::builder()
.value(Self::NAME)
.writable(false)
.enumerable(false)
.configurable(true),
)
.build();

(Self::NAME, bigint_object.into(), Self::attribute())
(
Self::NAME,
PropertyDescriptor::builder()
.value(bigint_object)
.writable(true)
.enumerable(false)
.configurable(false)
.build(),
)
}
}

Expand Down
18 changes: 11 additions & 7 deletions boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod tests;
use crate::{
builtins::BuiltIn,
object::{ConstructorBuilder, ObjectData, PROTOTYPE},
property::Attribute,
property::PropertyDescriptor,
BoaProfiler, Context, Result, Value,
};

Expand All @@ -27,11 +27,7 @@ impl BuiltIn for Boolean {
/// The name of the object.
const NAME: &'static str = "Boolean";

fn attribute() -> Attribute {
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE
}

fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
fn init(context: &mut Context) -> (&'static str, PropertyDescriptor) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let boolean_object = ConstructorBuilder::with_standard_object(
Expand All @@ -45,7 +41,15 @@ impl BuiltIn for Boolean {
.method(Self::value_of, "valueOf", 0)
.build();

(Self::NAME, boolean_object.into(), Self::attribute())
(
Self::NAME,
PropertyDescriptor::builder()
.value(boolean_object)
.writable(true)
.enumerable(false)
.configurable(true)
.build(),
)
}
}

Expand Down
Loading

0 comments on commit 80cdbea

Please sign in to comment.