From 598adfebea1182b105e3958969dd509f588862a2 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Sun, 14 Jun 2020 17:53:16 +0200 Subject: [PATCH] Made `make_builtin_fn` and `make_constructor_fn` take a usize - Added `#[inline]` to some small functions --- boa/src/builtins/array/mod.rs | 2 +- boa/src/builtins/bigint/mod.rs | 2 +- boa/src/builtins/boolean/mod.rs | 3 +- boa/src/builtins/error/mod.rs | 3 +- boa/src/builtins/error/range.rs | 3 +- boa/src/builtins/error/type.rs | 3 +- boa/src/builtins/function/mod.rs | 4 +- boa/src/builtins/number/mod.rs | 11 ++--- boa/src/builtins/object/internal_methods.rs | 47 +++++++++------------ boa/src/builtins/object/mod.rs | 2 + boa/src/builtins/regexp/mod.rs | 2 +- boa/src/builtins/string/mod.rs | 2 +- boa/src/builtins/symbol/mod.rs | 2 +- boa/src/builtins/value/mod.rs | 10 ++++- boa/src/builtins/value/tests.rs | 1 + boa/src/exec/mod.rs | 7 +++ 16 files changed, 56 insertions(+), 48 deletions(-) diff --git a/boa/src/builtins/array/mod.rs b/boa/src/builtins/array/mod.rs index 3075073c04a..0517e0c3ccf 100644 --- a/boa/src/builtins/array/mod.rs +++ b/boa/src/builtins/array/mod.rs @@ -36,7 +36,7 @@ impl Array { pub(crate) const NAME: &'static str = "Array"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 1; + pub(crate) const LENGTH: usize = 1; /// Creates a new `Array` instance. pub(crate) fn new_array(interpreter: &Interpreter) -> ResultValue { diff --git a/boa/src/builtins/bigint/mod.rs b/boa/src/builtins/bigint/mod.rs index dbe456c911f..eb2a2cf89ec 100644 --- a/boa/src/builtins/bigint/mod.rs +++ b/boa/src/builtins/bigint/mod.rs @@ -48,7 +48,7 @@ impl BigInt { pub(crate) const NAME: &'static str = "BigInt"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 1; + pub(crate) const LENGTH: usize = 1; /// The abstract operation thisBigIntValue takes argument value. /// diff --git a/boa/src/builtins/boolean/mod.rs b/boa/src/builtins/boolean/mod.rs index 6f384da7ecf..e2d5dd2ee3c 100644 --- a/boa/src/builtins/boolean/mod.rs +++ b/boa/src/builtins/boolean/mod.rs @@ -31,7 +31,7 @@ impl Boolean { pub(crate) const NAME: &'static str = "Boolean"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 1; + pub(crate) const LENGTH: usize = 1; /// An Utility function used to get the internal [[BooleanData]]. /// @@ -93,6 +93,7 @@ impl Boolean { /// /// [spec]: https://tc39.es/ecma262/#sec-boolean.prototype.valueof /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/valueOf + #[inline] pub(crate) fn value_of(this: &mut Value, _: &[Value], ctx: &mut Interpreter) -> ResultValue { Ok(Value::from(Self::this_boolean_value(this, ctx)?)) } diff --git a/boa/src/builtins/error/mod.rs b/boa/src/builtins/error/mod.rs index 49d3cafad2f..7bdee31bc7c 100644 --- a/boa/src/builtins/error/mod.rs +++ b/boa/src/builtins/error/mod.rs @@ -39,7 +39,7 @@ impl Error { pub(crate) const NAME: &'static str = "Error"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 1; + pub(crate) const LENGTH: usize = 1; /// Create a new error object. pub(crate) fn make_error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { @@ -94,6 +94,7 @@ impl Error { } /// Initialise the global object with the `Error` object. + #[inline] pub(crate) fn init(global: &Value) -> (&str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); diff --git a/boa/src/builtins/error/range.rs b/boa/src/builtins/error/range.rs index e45ae14a830..c51df0b350b 100644 --- a/boa/src/builtins/error/range.rs +++ b/boa/src/builtins/error/range.rs @@ -29,7 +29,7 @@ impl RangeError { pub(crate) const NAME: &'static str = "RangeError"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 1; + pub(crate) const LENGTH: usize = 1; /// Create a new error object. pub(crate) fn make_error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { @@ -84,6 +84,7 @@ impl RangeError { } /// Initialise the global object with the `RangeError` object. + #[inline] pub(crate) fn init(global: &Value) -> (&str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); diff --git a/boa/src/builtins/error/type.rs b/boa/src/builtins/error/type.rs index d61afae6d0a..3a4f2de9775 100644 --- a/boa/src/builtins/error/type.rs +++ b/boa/src/builtins/error/type.rs @@ -35,7 +35,7 @@ impl TypeError { pub(crate) const NAME: &'static str = "TypeError"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 1; + pub(crate) const LENGTH: usize = 1; /// Create a new error object. pub(crate) fn make_error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { @@ -91,6 +91,7 @@ impl TypeError { } /// Initialise the global object with the `RangeError` object. + #[inline] pub(crate) fn init(global: &Value) -> (&str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); diff --git a/boa/src/builtins/function/mod.rs b/boa/src/builtins/function/mod.rs index 5a54fa76d58..48d2c4c4645 100644 --- a/boa/src/builtins/function/mod.rs +++ b/boa/src/builtins/function/mod.rs @@ -407,7 +407,7 @@ pub fn create(global: &Value) -> Value { /// So far this is only used by internal functions pub fn make_constructor_fn( name: &str, - length: i32, + length: usize, body: NativeFunctionData, global: &Value, prototype: Value, @@ -473,7 +473,7 @@ pub fn make_constructor_fn( /// some other number of arguments. /// /// If no length is provided, the length will be set to 0. -pub fn make_builtin_fn(function: NativeFunctionData, name: N, parent: &Value, length: i32) +pub fn make_builtin_fn(function: NativeFunctionData, name: N, parent: &Value, length: usize) where N: Into, { diff --git a/boa/src/builtins/number/mod.rs b/boa/src/builtins/number/mod.rs index e7cf809820f..faf3e3c7228 100644 --- a/boa/src/builtins/number/mod.rs +++ b/boa/src/builtins/number/mod.rs @@ -44,7 +44,7 @@ impl Number { pub(crate) const NAME: &'static str = "Number"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 1; + pub(crate) const LENGTH: usize = 1; /// This function returns a `Result` of the number `Value`. /// @@ -539,17 +539,12 @@ impl Number { make_builtin_fn(Self::to_string, "toString", &prototype, 1); make_builtin_fn(Self::value_of, "valueOf", &prototype, 0); - make_builtin_fn( - Self::parse_int, - "parseInt", - global, - PARSE_INT_MAX_ARG_COUNT as i32, - ); + make_builtin_fn(Self::parse_int, "parseInt", global, PARSE_INT_MAX_ARG_COUNT); make_builtin_fn( Self::parse_float, "parseFloat", global, - PARSE_FLOAT_MAX_ARG_COUNT as i32, + PARSE_FLOAT_MAX_ARG_COUNT, ); let number = make_constructor_fn( diff --git a/boa/src/builtins/object/internal_methods.rs b/boa/src/builtins/object/internal_methods.rs index 9c809e829b2..996fddc90a4 100644 --- a/boa/src/builtins/object/internal_methods.rs +++ b/boa/src/builtins/object/internal_methods.rs @@ -295,11 +295,9 @@ impl Object { // Prop could either be a String or Symbol match *(*prop) { ValueData::String(ref st) => { - match self.properties.get(st) { - // If O does not have an own property with key P, return undefined. - // In this case we return a new empty Property - None => Property::default(), - Some(ref v) => { + self.properties() + .get(st) + .map_or_else(Property::default, |v| { let mut d = Property::default(); if v.is_data_descriptor() { d.value = v.value.clone(); @@ -312,30 +310,25 @@ impl Object { d.enumerable = v.enumerable; d.configurable = v.configurable; d - } - } + }) } - ValueData::Symbol(ref symbol) => { - match self.symbol_properties().get(&symbol.hash()) { - // If O does not have an own property with key P, return undefined. - // In this case we return a new empty Property - None => Property::default(), - Some(ref v) => { - let mut d = Property::default(); - if v.is_data_descriptor() { - d.value = v.value.clone(); - d.writable = v.writable; - } else { - debug_assert!(v.is_accessor_descriptor()); - d.get = v.get.clone(); - d.set = v.set.clone(); - } - d.enumerable = v.enumerable; - d.configurable = v.configurable; - d + ValueData::Symbol(ref symbol) => self + .symbol_properties() + .get(&symbol.hash()) + .map_or_else(Property::default, |v| { + let mut d = Property::default(); + if v.is_data_descriptor() { + d.value = v.value.clone(); + d.writable = v.writable; + } else { + debug_assert!(v.is_accessor_descriptor()); + d.get = v.get.clone(); + d.set = v.set.clone(); } - } - } + d.enumerable = v.enumerable; + d.configurable = v.configurable; + d + }), _ => Property::default(), } } diff --git a/boa/src/builtins/object/mod.rs b/boa/src/builtins/object/mod.rs index 24bdf573bac..0a7468f4e72 100644 --- a/boa/src/builtins/object/mod.rs +++ b/boa/src/builtins/object/mod.rs @@ -96,6 +96,7 @@ impl Display for ObjectData { impl Default for Object { /// Return a new ObjectData struct, with `kind` set to Ordinary + #[inline] fn default() -> Self { let mut object = Self { data: ObjectData::Ordinary, @@ -111,6 +112,7 @@ impl Default for Object { } impl Object { + #[inline] pub fn new() -> Self { Default::default() } diff --git a/boa/src/builtins/regexp/mod.rs b/boa/src/builtins/regexp/mod.rs index 569e6dd56af..c6970457788 100644 --- a/boa/src/builtins/regexp/mod.rs +++ b/boa/src/builtins/regexp/mod.rs @@ -65,7 +65,7 @@ impl RegExp { pub(crate) const NAME: &'static str = "RegExp"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 2; + pub(crate) const LENGTH: usize = 2; /// Create a new `RegExp` pub(crate) fn make_regexp( diff --git a/boa/src/builtins/string/mod.rs b/boa/src/builtins/string/mod.rs index d3da1ed3e7f..39b56c87379 100644 --- a/boa/src/builtins/string/mod.rs +++ b/boa/src/builtins/string/mod.rs @@ -40,7 +40,7 @@ impl String { pub(crate) const NAME: &'static str = "String"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 1; + pub(crate) const LENGTH: usize = 1; fn this_string_value(this: &Value, ctx: &mut Interpreter) -> Result { match this.data() { diff --git a/boa/src/builtins/symbol/mod.rs b/boa/src/builtins/symbol/mod.rs index deb1d3553d5..2c551c838d5 100644 --- a/boa/src/builtins/symbol/mod.rs +++ b/boa/src/builtins/symbol/mod.rs @@ -34,7 +34,7 @@ impl Symbol { pub(crate) const NAME: &'static str = "Symbol"; /// The amount of arguments this function object takes. - pub(crate) const LENGTH: i32 = 0; + pub(crate) const LENGTH: usize = 0; /// Returns the `Symbol`s description. pub fn description(&self) -> Option<&str> { diff --git a/boa/src/builtins/value/mod.rs b/boa/src/builtins/value/mod.rs index b69541374c2..73be83e9bbd 100644 --- a/boa/src/builtins/value/mod.rs +++ b/boa/src/builtins/value/mod.rs @@ -404,11 +404,13 @@ impl ValueData { } /// Returns true if the value is a bigint. + #[inline] pub fn is_bigint(&self) -> bool { matches!(self, Self::BigInt(_)) } /// Returns an optional reference to a `BigInt` if the value is a BigInt primitive. + #[inline] pub fn as_bigint(&self) -> Option<&BigInt> { match self { Self::BigInt(bigint) => Some(bigint), @@ -552,6 +554,7 @@ impl ValueData { /// Resolve the property in the object. /// /// Returns a copy of the Property. + #[inline] pub fn get_internal_slot(&self, field: &str) -> Value { let _timer = BoaProfiler::global().start_event("Value::get_internal_slot", "value"); @@ -599,6 +602,7 @@ impl ValueData { } /// Check whether an object has an internal state set. + #[inline] pub fn has_internal_state(&self) -> bool { matches!(self.as_object(), Some(object) if object.state().is_some()) } @@ -657,7 +661,8 @@ impl ValueData { } } - /// Check to see if the Value has the field, mainly used by environment records + /// Check to see if the Value has the field, mainly used by environment records. + #[inline] pub fn has_field(&self, field: &str) -> bool { let _timer = BoaProfiler::global().start_event("Value::has_field", "value"); self.get_property(field).is_some() @@ -709,7 +714,8 @@ impl ValueData { value } - /// Set the kind of an object + /// Set the kind of an object. + #[inline] pub fn set_data(&self, data: ObjectData) { if let Self::Object(ref obj) = *self { (*obj.deref().borrow_mut()).data = data; diff --git a/boa/src/builtins/value/tests.rs b/boa/src/builtins/value/tests.rs index bfe22614d9f..008b04076ad 100644 --- a/boa/src/builtins/value/tests.rs +++ b/boa/src/builtins/value/tests.rs @@ -111,6 +111,7 @@ fn abstract_equality_comparison() { ); } +/// Helper function to get the hash of a `Value`. fn hash_value(value: &Value) -> u64 { let mut hasher = DefaultHasher::new(); value.hash(&mut hasher); diff --git a/boa/src/exec/mod.rs b/boa/src/exec/mod.rs index 034f9f9e3eb..77e83ee912e 100644 --- a/boa/src/exec/mod.rs +++ b/boa/src/exec/mod.rs @@ -80,11 +80,13 @@ impl Interpreter { } /// Retrieves the `Realm` of this executor. + #[inline] pub(crate) fn realm(&self) -> &Realm { &self.realm } /// Retrieves the `Realm` of this executor as a mutable reference. + #[inline] pub(crate) fn realm_mut(&mut self) -> &mut Realm { &mut self.realm } @@ -92,6 +94,7 @@ impl Interpreter { /// Generates a new `Symbol` internal hash. /// /// This currently is an incremented value. + #[inline] pub(crate) fn generate_hash(&mut self) -> u32 { let hash = self.symbol_count; self.symbol_count += 1; @@ -548,13 +551,16 @@ impl Interpreter { } } + #[inline] pub(crate) fn set_current_state(&mut self, new_state: InterpreterState) { self.state = new_state } + #[inline] pub(crate) fn get_current_state(&self) -> &InterpreterState { &self.state } + /// Check if the `Value` can be converted to an `Object` /// /// The abstract operation `RequireObjectCoercible` takes argument argument. @@ -566,6 +572,7 @@ impl Interpreter { /// /// [table]: https://tc39.es/ecma262/#table-14 /// [spec]: https://tc39.es/ecma262/#sec-requireobjectcoercible + #[inline] pub fn require_object_coercible<'a>(&mut self, value: &'a Value) -> Result<&'a Value, Value> { if value.is_null_or_undefined() { self.throw_type_error("cannot convert null or undefined to Object")?;