Skip to content

Commit

Permalink
Refactor: Define all property methods of constructors
Browse files Browse the repository at this point in the history
6 = 3 * 2
2 instance and static properties
3 data, accessor and properties

discussed in #400
  • Loading branch information
RageKnify committed Feb 2, 2021
1 parent 0fdfdfb commit ffd7041
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 56 deletions.
4 changes: 2 additions & 2 deletions boa/examples/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ 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.data_property("inheritedProperty", 10, Attribute::default());

// Add a static property with the value `"Im a static property"`, with default attribute.
// (`WRITABLE, ENUMERABLE, PERMANENT`).
class.static_property(
class.static_data_property(
"staticProperty",
"Im a static property",
Attribute::WRITABLE | Attribute::ENUMERABLE | Attribute::PERMANENT,
Expand Down
6 changes: 3 additions & 3 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ impl BuiltIn for Array {
)
.name(Self::NAME)
.length(Self::LENGTH)
.property(
.data_property(
"length",
0,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::PERMANENT,
)
.property(
.data_property(
"values",
values_function.clone(),
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
.data_property(
symbol_iterator,
values_function,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/error/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl BuiltIn for EvalError {
.name(Self::NAME)
.length(Self::LENGTH)
.inherit(error_prototype.into())
.property("name", Self::NAME, attribute)
.property("message", "", attribute)
.data_property("name", Self::NAME, attribute)
.data_property("message", "", attribute)
.build();

(Self::NAME, eval_error_object.into(), Self::attribute())
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl BuiltIn for Error {
)
.name(Self::NAME)
.length(Self::LENGTH)
.property("name", Self::NAME, attribute)
.property("message", "", attribute)
.data_property("name", Self::NAME, attribute)
.data_property("message", "", attribute)
.method(Self::to_string, "toString", 0)
.build();

Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ impl BuiltIn for RangeError {
.name(Self::NAME)
.length(Self::LENGTH)
.inherit(error_prototype.into())
.property("name", Self::NAME, attribute)
.property("message", "", attribute)
.data_property("name", Self::NAME, attribute)
.data_property("message", "", attribute)
.build();

(Self::NAME, range_error_object.into(), Self::attribute())
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/error/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl BuiltIn for ReferenceError {
.name(Self::NAME)
.length(Self::LENGTH)
.inherit(error_prototype.into())
.property("name", Self::NAME, attribute)
.property("message", "", attribute)
.data_property("name", Self::NAME, attribute)
.data_property("message", "", attribute)
.build();

(Self::NAME, reference_error_object.into(), Self::attribute())
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/error/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ impl BuiltIn for SyntaxError {
.name(Self::NAME)
.length(Self::LENGTH)
.inherit(error_prototype.into())
.property("name", Self::NAME, attribute)
.property("message", "", attribute)
.data_property("name", Self::NAME, attribute)
.data_property("message", "", attribute)
.build();

(Self::NAME, syntax_error_object.into(), Self::attribute())
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl BuiltIn for TypeError {
.name(Self::NAME)
.length(Self::LENGTH)
.inherit(error_prototype.into())
.property("name", Self::NAME, attribute)
.property("message", "", attribute)
.data_property("name", Self::NAME, attribute)
.data_property("message", "", attribute)
.build();

(Self::NAME, type_error_object.into(), Self::attribute())
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/error/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl BuiltIn for UriError {
.name(Self::NAME)
.length(Self::LENGTH)
.inherit(error_prototype.into())
.property("name", Self::NAME, attribute)
.property("message", "", attribute)
.data_property("name", Self::NAME, attribute)
.data_property("message", "", attribute)
.build();

(Self::NAME, uri_error_object.into(), Self::attribute())
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ impl BuiltIn for Map {
let map_object = ConstructorBuilder::new(context, Self::constructor)
.name(Self::NAME)
.length(Self::LENGTH)
.property(
.data_property(
"entries",
entries_function.clone(),
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
.data_property(
iterator_symbol,
entries_function,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
Expand Down
16 changes: 8 additions & 8 deletions boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ impl BuiltIn for Number {
)
.name(Self::NAME)
.length(Self::LENGTH)
.static_property("EPSILON", f64::EPSILON, attribute)
.static_property("MAX_SAFE_INTEGER", Self::MAX_SAFE_INTEGER, attribute)
.static_property("MIN_SAFE_INTEGER", Self::MIN_SAFE_INTEGER, attribute)
.static_property("MAX_VALUE", Self::MAX_VALUE, attribute)
.static_property("MIN_VALUE", Self::MIN_VALUE, attribute)
.static_property("NEGATIVE_INFINITY", f64::NEG_INFINITY, attribute)
.static_property("POSITIVE_INFINITY", f64::INFINITY, attribute)
.static_property("NaN", f64::NAN, attribute)
.static_data_property("EPSILON", f64::EPSILON, attribute)
.static_data_property("MAX_SAFE_INTEGER", Self::MAX_SAFE_INTEGER, attribute)
.static_data_property("MIN_SAFE_INTEGER", Self::MIN_SAFE_INTEGER, attribute)
.static_data_property("MAX_VALUE", Self::MAX_VALUE, attribute)
.static_data_property("MIN_VALUE", Self::MIN_VALUE, attribute)
.static_data_property("NEGATIVE_INFINITY", f64::NEG_INFINITY, attribute)
.static_data_property("POSITIVE_INFINITY", f64::INFINITY, attribute)
.static_data_property("NaN", f64::NAN, attribute)
.method(Self::to_exponential, "toExponential", 1)
.method(Self::to_fixed, "toFixed", 1)
.method(Self::to_locale_string, "toLocaleString", 0)
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl BuiltIn for RegExp {
)
.name(Self::NAME)
.length(Self::LENGTH)
.property("lastIndex", 0, Attribute::all())
.data_property("lastIndex", 0, Attribute::all())
.method(Self::test, "test", 1)
.method(Self::exec, "exec", 1)
.method(Self::to_string, "toString", 0)
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl BuiltIn for String {
)
.name(Self::NAME)
.length(Self::LENGTH)
.property("length", 0, attribute)
.data_property("length", 0, attribute)
.method(Self::char_at, "charAt", 1)
.method(Self::char_code_at, "charCodeAt", 1)
.method(Self::code_point_at, "codePointAt", 1)
Expand Down
26 changes: 13 additions & 13 deletions boa/src/builtins/symbol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,19 @@ impl BuiltIn for Symbol {
)
.name(Self::NAME)
.length(Self::LENGTH)
.static_property("asyncIterator", symbol_async_iterator, attribute)
.static_property("hasInstance", symbol_has_instance, attribute)
.static_property("isConcatSpreadable", symbol_is_concat_spreadable, attribute)
.static_property("iterator", symbol_iterator, attribute)
.static_property("match", symbol_match, attribute)
.static_property("matchAll", symbol_match_all, attribute)
.static_property("replace", symbol_replace, attribute)
.static_property("search", symbol_search, attribute)
.static_property("species", symbol_species, attribute)
.static_property("split", symbol_split, attribute)
.static_property("toPrimitive", symbol_to_primitive, attribute)
.static_property("toStringTag", symbol_to_string_tag, attribute)
.static_property("unscopables", symbol_unscopables, attribute)
.static_data_property("asyncIterator", symbol_async_iterator, attribute)
.static_data_property("hasInstance", symbol_has_instance, attribute)
.static_data_property("isConcatSpreadable", symbol_is_concat_spreadable, attribute)
.static_data_property("iterator", symbol_iterator, attribute)
.static_data_property("match", symbol_match, attribute)
.static_data_property("matchAll", symbol_match_all, attribute)
.static_data_property("replace", symbol_replace, attribute)
.static_data_property("search", symbol_search, attribute)
.static_data_property("species", symbol_species, attribute)
.static_data_property("split", symbol_split, attribute)
.static_data_property("toPrimitive", symbol_to_primitive, attribute)
.static_data_property("toStringTag", symbol_to_string_tag, attribute)
.static_data_property("unscopables", symbol_unscopables, attribute)
.method(Self::to_string, "toString", 0)
.callable(true)
.constructable(false)
Expand Down
82 changes: 75 additions & 7 deletions boa/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
use crate::{
builtins::function::NativeFunction,
object::{ConstructorBuilder, GcObject, NativeObject, ObjectData},
property::{Attribute, PropertyKey},
property::{Attribute, PropertyDescriptor, PropertyKey},
Context, Result, Value,
};

Expand Down Expand Up @@ -156,29 +156,97 @@ impl<'context> ClassBuilder<'context> {
self
}

/// Add a property to the class, with the specified attribute.
/// Add a data property to the class, with the specified attribute.
///
/// It is added to `prototype`.
#[inline]
pub fn property<K, V>(&mut self, key: K, value: V, attribute: Attribute) -> &mut Self
pub fn data_property<K, V>(&mut self, key: K, value: V, attribute: Attribute) -> &mut Self
where
K: Into<PropertyKey>,
V: Into<Value>,
{
self.builder.property(key, value, attribute);
self.builder.data_property(key, value, attribute);
self
}

/// Add a static property to the class, with the specified attribute.
/// Add a static data property to the class, with the specified attribute.
///
/// It is added to class object itself.
#[inline]
pub fn static_property<K, V>(&mut self, key: K, value: V, attribute: Attribute) -> &mut Self
pub fn static_data_property<K, V>(
&mut self,
key: K,
value: V,
attribute: Attribute,
) -> &mut Self
where
K: Into<PropertyKey>,
V: Into<Value>,
{
self.builder.static_property(key, value, attribute);
self.builder.static_data_property(key, value, attribute);
self
}

/// Add an accessor property to the class, with the specified attribute.
///
/// It is added to `prototype`.
#[inline]
pub fn accessor_property<K>(
&mut self,
key: K,
get: Option<GcObject>,
set: Option<GcObject>,
attribute: Attribute,
) -> &mut Self
where
K: Into<PropertyKey>,
{
self.builder.accessor_property(key, get, set, attribute);
self
}

/// Add a static accessor property to the class, with the specified attribute.
///
/// It is added to class object itself.
#[inline]
pub fn static_accessor_property<K>(
&mut self,
key: K,
get: Option<GcObject>,
set: Option<GcObject>,
attribute: Attribute,
) -> &mut Self
where
K: Into<PropertyKey>,
{
self.builder
.static_accessor_property(key, get, set, attribute);
self
}

/// Add a property descriptor to the class, with the specified attribute.
///
/// It is added to `prototype`.
#[inline]
pub fn property_descriptor<K, P>(&mut self, key: K, property: P) -> &mut Self
where
K: Into<PropertyKey>,
P: Into<PropertyDescriptor>,
{
self.builder.property_descriptor(key, property);
self
}

/// Add a static property descriptor to the class, with the specified attribute.
///
/// It is added to class object itself.
#[inline]
pub fn static_property_descriptor<K, P>(&mut self, key: K, property: P) -> &mut Self
where
K: Into<PropertyKey>,
P: Into<PropertyDescriptor>,
{
self.builder.static_property_descriptor(key, property);
self
}

Expand Down
Loading

0 comments on commit ffd7041

Please sign in to comment.