Skip to content

Commit

Permalink
Define all property methods of constructors (#1109)
Browse files Browse the repository at this point in the history
* Refactor: Define all property methods of constructors

* Refactor: Simplify naming of ConstructorBuilder methods

As per review in #1109 by @HalidOdat

Co-authored-by: Halid Odat <halidodat@gmail.com>
  • Loading branch information
RageKnify and HalidOdat authored Feb 6, 2021
1 parent acfc467 commit a5bc7dd
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 6 deletions.
68 changes: 65 additions & 3 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,7 +156,7 @@ 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]
Expand All @@ -169,7 +169,7 @@ impl<'context> ClassBuilder<'context> {
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]
Expand All @@ -182,6 +182,68 @@ impl<'context> ClassBuilder<'context> {
self
}

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

/// Return the current context.
#[inline]
pub fn context(&mut self) -> &'_ mut Context {
Expand Down
64 changes: 61 additions & 3 deletions boa/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
context::StandardConstructor,
gc::{Finalize, Trace},
property::{Attribute, DataDescriptor, PropertyDescriptor, PropertyKey},
property::{AccessorDescriptor, Attribute, DataDescriptor, PropertyDescriptor, PropertyKey},
value::{same_value, RcBigInt, RcString, RcSymbol, Value},
BoaProfiler, Context,
};
Expand Down Expand Up @@ -937,7 +937,7 @@ impl<'context> ConstructorBuilder<'context> {
self
}

/// Add new property to the constructors prototype.
/// Add new data property to the constructor's prototype.
#[inline]
pub fn property<K, V>(&mut self, key: K, value: V, attribute: Attribute) -> &mut Self
where
Expand All @@ -949,7 +949,7 @@ impl<'context> ConstructorBuilder<'context> {
self
}

/// Add new static property to the constructors object itself.
/// Add new static data property to the constructor object itself.
#[inline]
pub fn static_property<K, V>(&mut self, key: K, value: V, attribute: Attribute) -> &mut Self
where
Expand All @@ -961,6 +961,64 @@ impl<'context> ConstructorBuilder<'context> {
self
}

/// Add new accessor property to the constructor's prototype.
#[inline]
pub fn accessor<K>(
&mut self,
key: K,
get: Option<GcObject>,
set: Option<GcObject>,
attribute: Attribute,
) -> &mut Self
where
K: Into<PropertyKey>,
{
let property = AccessorDescriptor::new(get, set, attribute);
self.prototype.borrow_mut().insert(key, property);
self
}

/// Add new static accessor property to the constructor object itself.
#[inline]
pub fn static_accessor<K>(
&mut self,
key: K,
get: Option<GcObject>,
set: Option<GcObject>,
attribute: Attribute,
) -> &mut Self
where
K: Into<PropertyKey>,
{
let property = AccessorDescriptor::new(get, set, attribute);
self.constructor_object.borrow_mut().insert(key, property);
self
}

/// Add new property to the constructor's prototype.
#[inline]
pub fn property_descriptor<K, P>(&mut self, key: K, property: P) -> &mut Self
where
K: Into<PropertyKey>,
P: Into<PropertyDescriptor>,
{
let property = property.into();
self.prototype.borrow_mut().insert(key, property);
self
}

/// Add new static property to the constructor 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>,
{
let property = property.into();
self.constructor_object.borrow_mut().insert(key, property);
self
}

/// Specify how many arguments the constructor function takes.
///
/// Default is `0`.
Expand Down

0 comments on commit a5bc7dd

Please sign in to comment.