Skip to content

Commit

Permalink
Made make_builtin_fn and make_constructor_fn take a usize
Browse files Browse the repository at this point in the history
 - Added `#[inline]` to some small functions
  • Loading branch information
HalidOdat committed Jun 14, 2020
1 parent 15abb5a commit 598adfe
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 48 deletions.
2 changes: 1 addition & 1 deletion boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]].
///
Expand Down Expand Up @@ -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)?))
}
Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");

Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");

Expand Down
3 changes: 2 additions & 1 deletion boa/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");

Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<N>(function: NativeFunctionData, name: N, parent: &Value, length: i32)
pub fn make_builtin_fn<N>(function: NativeFunctionData, name: N, parent: &Value, length: usize)
where
N: Into<String>,
{
Expand Down
11 changes: 3 additions & 8 deletions boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
///
Expand Down Expand Up @@ -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(
Expand Down
47 changes: 20 additions & 27 deletions boa/src/builtins/object/internal_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions boa/src/builtins/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -111,6 +112,7 @@ impl Default for Object {
}

impl Object {
#[inline]
pub fn new() -> Self {
Default::default()
}
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StdString, Value> {
match this.data() {
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/symbol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
10 changes: 8 additions & 2 deletions boa/src/builtins/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions boa/src/builtins/value/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions boa/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,21 @@ 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
}

/// 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;
Expand Down Expand Up @@ -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.
Expand All @@ -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")?;
Expand Down

0 comments on commit 598adfe

Please sign in to comment.