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

Define all property methods of constructors #1109

Merged
merged 2 commits into from
Feb 6, 2021
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
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