From a5bc7dda20d0048bf0e9370ee3fec571ba65611e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Borges?= Date: Sat, 6 Feb 2021 01:04:21 +0000 Subject: [PATCH] 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 --- boa/src/class.rs | 68 +++++++++++++++++++++++++++++++++++++++++-- boa/src/object/mod.rs | 64 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 126 insertions(+), 6 deletions(-) diff --git a/boa/src/class.rs b/boa/src/class.rs index 6f33c154f3..a28b132440 100644 --- a/boa/src/class.rs +++ b/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( + &mut self, + key: K, + get: Option, + set: Option, + attribute: Attribute, + ) -> &mut Self + where + K: Into, + { + 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( + &mut self, + key: K, + get: Option, + set: Option, + attribute: Attribute, + ) -> &mut Self + where + K: Into, + { + 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(&mut self, key: K, property: P) -> &mut Self + where + K: Into, + P: Into, + { + 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(&mut self, key: K, property: P) -> &mut Self + where + K: Into, + P: Into, + { + self.builder.static_property_descriptor(key, property); + self + } + /// Return the current context. #[inline] pub fn context(&mut self) -> &'_ mut Context { diff --git a/boa/src/object/mod.rs b/boa/src/object/mod.rs index 93874f107a..da2f80a4ee 100644 --- a/boa/src/object/mod.rs +++ b/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(&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(&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( + &mut self, + key: K, + get: Option, + set: Option, + attribute: Attribute, + ) -> &mut Self + where + K: Into, + { + 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( + &mut self, + key: K, + get: Option, + set: Option, + attribute: Attribute, + ) -> &mut Self + where + K: Into, + { + 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(&mut self, key: K, property: P) -> &mut Self + where + K: Into, + P: Into, + { + 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(&mut self, key: K, property: P) -> &mut Self + where + K: Into, + P: Into, + { + let property = property.into(); + self.constructor_object.borrow_mut().insert(key, property); + self + } + /// Specify how many arguments the constructor function takes. /// /// Default is `0`.