Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged create into init for builtins #547

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,11 @@ impl Array {
Ok(Value::from(false))
}

/// Create a new `Array` object.
pub(crate) fn create(global: &Value) -> Value {
/// Initialise the `Array` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

// Create prototype
let prototype = Value::new_object(Some(global));
let length = Property::default().value(Value::from(0));
Expand Down Expand Up @@ -998,14 +1001,6 @@ impl Array {
// Static Methods
make_builtin_fn(Self::is_array, "isArray", &array, 1);

array
}

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

(Self::NAME, Self::create(global))
(Self::NAME, array)
}
}
23 changes: 9 additions & 14 deletions boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,17 @@ impl BigInt {
))
}

/// Create a new `Number` object
pub(crate) fn create(global: &Value) -> Value {
/// Initialise the `BigInt` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let prototype = Value::new_object(Some(global));

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

let big_int = make_constructor_fn(
let bigint_object = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_bigint,
Expand All @@ -214,18 +217,10 @@ impl BigInt {
false,
);

make_builtin_fn(Self::as_int_n, "asIntN", &big_int, 2);
make_builtin_fn(Self::as_uint_n, "asUintN", &big_int, 2);

big_int
}

/// Initialise the `BigInt` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
make_builtin_fn(Self::as_int_n, "asIntN", &bigint_object, 2);
make_builtin_fn(Self::as_uint_n, "asUintN", &bigint_object, 2);

(Self::NAME, Self::create(global))
(Self::NAME, bigint_object)
}
}

Expand Down
19 changes: 8 additions & 11 deletions boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,27 @@ impl Boolean {
Ok(Value::from(Self::this_boolean_value(this, ctx)?))
}

/// Create a new `Boolean` object.
pub(crate) fn create(global: &Value) -> Value {
/// Initialise the `Boolean` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

// Create Prototype
// https://tc39.es/ecma262/#sec-properties-of-the-boolean-prototype-object
let prototype = Value::new_object(Some(global));

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

make_constructor_fn(
let boolean_object = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::construct_boolean,
global,
prototype,
true,
)
}

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

(Self::NAME, Self::create(global))
(Self::NAME, boolean_object)
}
}
8 changes: 0 additions & 8 deletions boa/src/builtins/boolean/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
use super::*;
use crate::{builtins::value::same_value, exec::Interpreter, forward, forward_val, realm::Realm};

#[test]
fn check_boolean_constructor_is_function() {
let global = Value::new_object(None);
let boolean_constructor = Boolean::create(&global);
assert_eq!(boolean_constructor.is_function(), true);
}

/// Test the correct type is returned from call and construct
#[allow(clippy::result_unwrap_used)]
#[test]
Expand Down
17 changes: 6 additions & 11 deletions boa/src/builtins/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,11 @@ impl Console {
Ok(Value::undefined())
}

/// Create a new `console` object
pub(crate) fn create(global: &Value) -> Value {
/// Initialise the `console` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let console = Value::new_object(Some(global));

make_builtin_fn(Self::assert, "assert", &console, 0);
Expand All @@ -511,14 +514,6 @@ impl Console {
make_builtin_fn(Self::dir, "dir", &console, 0);
make_builtin_fn(Self::dir, "dirxml", &console, 0);

console
}

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

(Self::NAME, Self::create(global))
(Self::NAME, console)
}
}
22 changes: 10 additions & 12 deletions boa/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,26 @@ impl Error {
Ok(Value::from(format!("{}: {}", name, message)))
}

/// Create a new `Error` object.
pub(crate) fn create(global: &Value) -> Value {
/// 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");

let prototype = Value::new_object(Some(global));
prototype.set_field("message", Value::from(""));
prototype.set_field("name", Self::NAME);
prototype.set_field("message", "");

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

make_constructor_fn(
let error_object = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_error,
global,
prototype,
true,
)
}

/// 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");
);

(Self::NAME, Self::create(global))
(Self::NAME, error_object)
}
}
19 changes: 8 additions & 11 deletions boa/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,26 @@ impl RangeError {
Ok(Value::from(format!("{}: {}", name, message)))
}

/// Create a new `RangeError` object.
pub(crate) fn create(global: &Value) -> Value {
/// 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");

let prototype = Value::new_object(Some(global));
prototype.set_field("name", Self::NAME);
prototype.set_field("message", "");

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

make_constructor_fn(
let range_error_object = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_error,
global,
prototype,
true,
)
}

/// 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");
);

(Self::NAME, Self::create(global))
(Self::NAME, range_error_object)
}
}
17 changes: 7 additions & 10 deletions boa/src/builtins/error/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,25 @@ impl ReferenceError {
Ok(Value::from(format!("{}: {}", name, message)))
}

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

let prototype = Value::new_object(Some(global));
prototype.set_field("name", Self::NAME);
prototype.set_field("message", "");

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

make_constructor_fn(
let reference_error_object = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_error,
global,
prototype,
true,
)
}

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

(Self::NAME, Self::create(global))
(Self::NAME, reference_error_object)
}
}
20 changes: 9 additions & 11 deletions boa/src/builtins/error/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
exec::Interpreter,
profiler::BoaProfiler,
};

/// JavaScript `SyntaxError` impleentation.
#[derive(Debug, Clone, Copy)]
pub(crate) struct SyntaxError;
Expand Down Expand Up @@ -61,29 +62,26 @@ impl SyntaxError {
Ok(format!("{}: {}", name, message).into())
}

/// Create a new `SyntaxError` object.
pub(crate) fn create(global: &Value) -> Value {
/// Initialise the global object with the `SyntaxError` object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

let prototype = Value::new_object(Some(global));
prototype.set_field("name", Self::NAME);
prototype.set_field("message", "");

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

make_constructor_fn(
let syntax_error_object = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_error,
global,
prototype,
true,
)
}

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

(Self::NAME, Self::create(global))
(Self::NAME, syntax_error_object)
}
}
19 changes: 8 additions & 11 deletions boa/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,26 @@ impl TypeError {
Ok(Value::from(format!("{}: {}", name, message)))
}

/// Create a new `RangeError` object.
pub(crate) fn create(global: &Value) -> Value {
/// 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");

let prototype = Value::new_object(Some(global));
prototype.set_field("name", Self::NAME);
prototype.set_field("message", "");

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

make_constructor_fn(
let type_error_object = make_constructor_fn(
Self::NAME,
Self::LENGTH,
Self::make_error,
global,
prototype,
true,
)
}

/// 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");
);

(Self::NAME, Self::create(global))
(Self::NAME, type_error_object)
}
}
12 changes: 5 additions & 7 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,6 @@ pub fn make_function(this: &Value, _: &[Value], _: &mut Interpreter) -> ResultVa
Ok(this.clone())
}

pub fn create(global: &Value) -> Value {
let prototype = Value::new_object(Some(global));

make_constructor_fn("Function", 1, make_function, global, prototype, true)
}

/// Creates a new constructor function
///
/// This utility function handling linking the new Constructor to the prototype.
Expand Down Expand Up @@ -510,6 +504,10 @@ where
#[inline]
pub fn init(global: &Value) -> (&str, Value) {
let _timer = BoaProfiler::global().start_event("function", "init");
let prototype = Value::new_object(Some(global));

let function_object =
make_constructor_fn("Function", 1, make_function, global, prototype, true);

("Function", create(global))
("Function", function_object)
}
Loading