Skip to content

Commit

Permalink
Abstracted Object name and function object length.
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jun 13, 2020
1 parent ff8796e commit d19ed61
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 98 deletions.
42 changes: 19 additions & 23 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ use crate::{
use std::{
borrow::Borrow,
cmp::{max, min},
ops::Deref,
};

/// JavaScript `Array` built-in implementation.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Array;

impl Array {
/// The name of the object.
pub(crate) const NAME: &'static str = "Array";

/// The amount of arguments this function object takes.
pub(crate) const LENGTH: i32 = 1;

/// Creates a new `Array` instance.
pub(crate) fn new_array(interpreter: &Interpreter) -> ResultValue {
let array = Value::new_object(Some(
Expand Down Expand Up @@ -167,25 +172,9 @@ impl Array {
args: &[Value],
_interpreter: &mut Interpreter,
) -> ResultValue {
let value_true = Value::boolean(true);
let value_false = Value::boolean(false);

match args.get(0) {
Some(arg) => {
match arg.data() {
// 1.
ValueData::Object(ref obj) => {
// 2.
if let ObjectData::Array = (*obj).deref().borrow().data {
return Ok(value_true);
}
Ok(value_false)
}
// 3.
_ => Ok(value_false),
}
}
None => Ok(value_false),
match args.get(0).and_then(|x| x.as_object()) {
Some(object) => Ok(Value::from(object.is_array())),
None => Ok(Value::from(false)),
}
}

Expand Down Expand Up @@ -1031,7 +1020,14 @@ impl Array {
make_builtin_fn(Self::slice, "slice", &prototype, 2);
make_builtin_fn(Self::some, "some", &prototype, 2);

let array = make_constructor_fn("Array", 1, Self::make_array, global, prototype, true);
let array = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_array,
global,
prototype,
true,
);

// Static Methods
make_builtin_fn(Self::is_array, "isArray", &array, 1);
Expand All @@ -1042,8 +1038,8 @@ impl Array {
/// Initialise the `Array` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("array", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("Array", Self::create(global))
(Self::NAME, Self::create(global))
}
}
19 changes: 16 additions & 3 deletions boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ mod tests;
pub struct BigInt(num_bigint::BigInt);

impl BigInt {
/// The name of the object.
pub(crate) const NAME: &'static str = "BigInt";

/// The amount of arguments this function object takes.
pub(crate) const LENGTH: i32 = 1;

/// The abstract operation thisBigIntValue takes argument value.
///
/// The phrase “this BigInt value” within the specification of a method refers to the
Expand Down Expand Up @@ -213,7 +219,14 @@ impl BigInt {
make_builtin_fn(Self::to_string, "toString", &prototype, 1);
make_builtin_fn(Self::value_of, "valueOf", &prototype, 0);

let big_int = make_constructor_fn("BigInt", 1, Self::make_bigint, global, prototype, false);
let big_int = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_bigint,
global,
prototype,
false,
);

make_builtin_fn(Self::as_int_n, "asIntN", &big_int, 2);
make_builtin_fn(Self::as_uint_n, "asUintN", &big_int, 2);
Expand All @@ -224,9 +237,9 @@ impl BigInt {
/// Initialise the `BigInt` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("bigint", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("BigInt", Self::create(global))
(Self::NAME, Self::create(global))
}
}

Expand Down
14 changes: 10 additions & 4 deletions boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ use crate::{
pub(crate) struct Boolean;

impl Boolean {
/// The name of the object.
pub(crate) const NAME: &'static str = "Boolean";

/// The amount of arguments this function object takes.
pub(crate) const LENGTH: i32 = 1;

/// An Utility function used to get the internal [[BooleanData]].
///
/// More information:
Expand Down Expand Up @@ -101,8 +107,8 @@ impl Boolean {
make_builtin_fn(Self::value_of, "valueOf", &prototype, 0);

make_constructor_fn(
"Boolean",
1,
Self::NAME,
Self::LENGTH,
Self::construct_boolean,
global,
prototype,
Expand All @@ -113,8 +119,8 @@ impl Boolean {
/// Initialise the `Boolean` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("boolean", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("Boolean", Self::create(global))
(Self::NAME, Self::create(global))
}
}
19 changes: 16 additions & 3 deletions boa/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ pub(crate) use self::range::RangeError;
pub(crate) struct Error;

impl Error {
/// The name of the object.
pub(crate) const NAME: &'static str = "Error";

/// The amount of arguments this function object takes.
pub(crate) const LENGTH: i32 = 1;

/// Create a new error object.
pub(crate) fn make_error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
if !args.is_empty() {
Expand Down Expand Up @@ -77,13 +83,20 @@ impl Error {

make_builtin_fn(Self::to_string, "toString", &prototype, 0);

make_constructor_fn("Error", 1, Self::make_error, global, prototype, true)
make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_error,
global,
prototype,
true,
)
}

/// Initialise the global object with the `Error` object.
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("error", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("Error", Self::create(global))
(Self::NAME, Self::create(global))
}
}
19 changes: 16 additions & 3 deletions boa/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ use crate::{
pub(crate) struct RangeError;

impl RangeError {
/// The name of the object.
pub(crate) const NAME: &'static str = "RangeError";

/// The amount of arguments this function object takes.
pub(crate) const LENGTH: i32 = 1;

/// Create a new error object.
pub(crate) fn make_error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
if !args.is_empty() {
Expand Down Expand Up @@ -67,13 +73,20 @@ impl RangeError {

make_builtin_fn(Self::to_string, "toString", &prototype, 0);

make_constructor_fn("RangeError", 1, Self::make_error, global, prototype, true)
make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_error,
global,
prototype,
true,
)
}

/// Initialise the global object with the `RangeError` object.
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("rangeerror", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("RangeError", Self::create(global))
(Self::NAME, Self::create(global))
}
}
19 changes: 16 additions & 3 deletions boa/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ use crate::{
pub(crate) struct TypeError;

impl TypeError {
/// The name of the object.
pub(crate) const NAME: &'static str = "TypeError";

/// The amount of arguments this function object takes.
pub(crate) const LENGTH: i32 = 1;

/// Create a new error object.
pub(crate) fn make_error(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
if !args.is_empty() {
Expand Down Expand Up @@ -74,13 +80,20 @@ impl TypeError {

make_builtin_fn(Self::to_string, "toString", &prototype, 0);

make_constructor_fn("TypeError", 1, Self::make_error, global, prototype, true)
make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_error,
global,
prototype,
true,
)
}

/// Initialise the global object with the `RangeError` object.
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("typeerror", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("TypeError", Self::create(global))
(Self::NAME, Self::create(global))
}
}
7 changes: 5 additions & 2 deletions boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ mod tests;
pub(crate) struct Json;

impl Json {
/// The name of the object.
pub(crate) const NAME: &'static str = "JSON";

/// `JSON.parse( text[, reviver] )`
///
/// This `JSON` method parses a JSON string, constructing the JavaScript value or object described by the string.
Expand Down Expand Up @@ -178,8 +181,8 @@ impl Json {
/// Initialise the `JSON` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("json", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("JSON", Self::create(global))
(Self::NAME, Self::create(global))
}
}
7 changes: 5 additions & 2 deletions boa/src/builtins/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ mod tests;
pub(crate) struct Math;

impl Math {
/// The name of the object.
pub(crate) const NAME: &'static str = "Math";

/// Get the absolute value of a number.
///
/// More information:
Expand Down Expand Up @@ -561,8 +564,8 @@ impl Math {
/// Initialise the `Math` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("math", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("Math", Self::create(global))
(Self::NAME, Self::create(global))
}
}
7 changes: 5 additions & 2 deletions boa/src/builtins/nan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ use crate::{builtins::value::Value, BoaProfiler};
pub(crate) struct NaN;

impl NaN {
/// The name of the property.
pub(crate) const NAME: &'static str = "NaN";

/// Initialize the `NaN` property on the global object.
#[inline]
pub(crate) fn init(_: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("NaN", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("NaN", Value::from(f64::NAN))
(Self::NAME, Value::from(f64::NAN))
}
}
19 changes: 16 additions & 3 deletions boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const PARSE_INT_MAX_ARG_COUNT: usize = 2;
const PARSE_FLOAT_MAX_ARG_COUNT: usize = 1;

impl Number {
/// The name of the object.
pub(crate) const NAME: &'static str = "Number";

/// The amount of arguments this function object takes.
pub(crate) const LENGTH: i32 = 1;

/// This function returns a `Result` of the number `Value`.
///
/// If the `Value` is a `Number` primitive of `Number` object the number is returned.
Expand Down Expand Up @@ -546,7 +552,14 @@ impl Number {
PARSE_FLOAT_MAX_ARG_COUNT as i32,
);

let number = make_constructor_fn("Number", 1, Self::make_number, global, prototype, true);
let number = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_number,
global,
prototype,
true,
);

// Constants from:
// https://tc39.es/ecma262/#sec-properties-of-the-number-constructor
Expand All @@ -568,9 +581,9 @@ impl Number {
/// Initialise the `Number` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("number", "init");
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

("Number", Self::create(global))
(Self::NAME, Self::create(global))
}

/// The abstract operation Number::equal takes arguments
Expand Down
Loading

0 comments on commit d19ed61

Please sign in to comment.