Browse Source

Define all property methods of constructors (#1109)

* 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>
pull/1119/head
João Borges 4 years ago committed by GitHub
parent
commit
a5bc7dda20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 68
      boa/src/class.rs
  2. 64
      boa/src/object/mod.rs

68
boa/src/class.rs

@ -63,7 +63,7 @@
use crate::{
builtins::function::NativeFunction,
object::{ConstructorBuilder, GcObject, NativeObject, ObjectData},
property::{Attribute, PropertyKey},
property::{Attribute, PropertyDescriptor, PropertyKey},
Context, Result, Value,
};
@ -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]
@ -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]
@ -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 {

64
boa/src/object/mod.rs

@ -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,
};
@ -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
@ -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
@ -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`.

Loading…
Cancel
Save