diff --git a/boa/src/builtins/array/mod.rs b/boa/src/builtins/array/mod.rs index 0e9bf0aa41b..c6650a7c875 100644 --- a/boa/src/builtins/array/mod.rs +++ b/boa/src/builtins/array/mod.rs @@ -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)); @@ -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) } } diff --git a/boa/src/builtins/bigint/mod.rs b/boa/src/builtins/bigint/mod.rs index e21643b003a..d6352364c18 100644 --- a/boa/src/builtins/bigint/mod.rs +++ b/boa/src/builtins/bigint/mod.rs @@ -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, @@ -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) } } diff --git a/boa/src/builtins/boolean/mod.rs b/boa/src/builtins/boolean/mod.rs index a0b9df09ff1..81826821eff 100644 --- a/boa/src/builtins/boolean/mod.rs +++ b/boa/src/builtins/boolean/mod.rs @@ -96,8 +96,11 @@ 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)); @@ -105,21 +108,15 @@ impl Boolean { 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) } } diff --git a/boa/src/builtins/boolean/tests.rs b/boa/src/builtins/boolean/tests.rs index 6c5cf22c382..bc8ffe230d7 100644 --- a/boa/src/builtins/boolean/tests.rs +++ b/boa/src/builtins/boolean/tests.rs @@ -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] diff --git a/boa/src/builtins/console/mod.rs b/boa/src/builtins/console/mod.rs index 056e5b7281b..48799ed900b 100644 --- a/boa/src/builtins/console/mod.rs +++ b/boa/src/builtins/console/mod.rs @@ -523,8 +523,11 @@ pub fn dir(this: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { Ok(Value::undefined()) } -/// Create a new `console` object -pub fn create(global: &Value) -> Value { +/// Initialise the `console` object on the global object. +#[inline] +pub fn init(global: &Value) -> (&str, Value) { + let _timer = BoaProfiler::global().start_event("console", "init"); + let console = Value::new_object(Some(global)); make_builtin_fn(assert, "assert", &console, 0); @@ -549,13 +552,5 @@ pub fn create(global: &Value) -> Value { console.set_internal_state(ConsoleState::default()); - console -} - -/// Initialise the `console` object on the global object. -#[inline] -pub fn init(global: &Value) -> (&str, Value) { - let _timer = BoaProfiler::global().start_event("console", "init"); - - ("console", create(global)) + ("console", console) } diff --git a/boa/src/builtins/error/mod.rs b/boa/src/builtins/error/mod.rs index f19f0fcf679..24943f00d01 100644 --- a/boa/src/builtins/error/mod.rs +++ b/boa/src/builtins/error/mod.rs @@ -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) } } diff --git a/boa/src/builtins/error/range.rs b/boa/src/builtins/error/range.rs index 04276281f75..400122dd956 100644 --- a/boa/src/builtins/error/range.rs +++ b/boa/src/builtins/error/range.rs @@ -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) } } diff --git a/boa/src/builtins/error/reference.rs b/boa/src/builtins/error/reference.rs index 6c2edef05ef..93d66488fce 100644 --- a/boa/src/builtins/error/reference.rs +++ b/boa/src/builtins/error/reference.rs @@ -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) } } diff --git a/boa/src/builtins/error/syntax.rs b/boa/src/builtins/error/syntax.rs index d7f954189ad..22a3da1f140 100644 --- a/boa/src/builtins/error/syntax.rs +++ b/boa/src/builtins/error/syntax.rs @@ -21,6 +21,7 @@ use crate::{ exec::Interpreter, profiler::BoaProfiler, }; + /// JavaScript `SyntaxError` impleentation. #[derive(Debug, Clone, Copy)] pub(crate) struct SyntaxError; @@ -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) } } diff --git a/boa/src/builtins/error/type.rs b/boa/src/builtins/error/type.rs index 23fe45b6637..727d836c509 100644 --- a/boa/src/builtins/error/type.rs +++ b/boa/src/builtins/error/type.rs @@ -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) } } diff --git a/boa/src/builtins/function/mod.rs b/boa/src/builtins/function/mod.rs index d98c99ad8f3..50f955f928b 100644 --- a/boa/src/builtins/function/mod.rs +++ b/boa/src/builtins/function/mod.rs @@ -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. @@ -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) } diff --git a/boa/src/builtins/json/mod.rs b/boa/src/builtins/json/mod.rs index d266c3019f0..75ab63a4302 100644 --- a/boa/src/builtins/json/mod.rs +++ b/boa/src/builtins/json/mod.rs @@ -170,21 +170,15 @@ impl Json { } } - /// Create a new `JSON` object. - pub(crate) fn create(global: &Value) -> Value { - let json = Value::new_object(Some(global)); - - make_builtin_fn(Self::parse, "parse", &json, 2); - make_builtin_fn(Self::stringify, "stringify", &json, 3); - - json - } - /// Initialise the `JSON` object on the global object. #[inline] pub(crate) fn init(global: &Value) -> (&str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); + let json = Value::new_object(Some(global)); + + make_builtin_fn(Self::parse, "parse", &json, 2); + make_builtin_fn(Self::stringify, "stringify", &json, 3); - (Self::NAME, Self::create(global)) + (Self::NAME, json) } } diff --git a/boa/src/builtins/number/mod.rs b/boa/src/builtins/number/mod.rs index 6422bc244d7..a69e94c5199 100644 --- a/boa/src/builtins/number/mod.rs +++ b/boa/src/builtins/number/mod.rs @@ -555,8 +555,11 @@ impl Number { } } - /// Create a new `Number` object - pub(crate) fn create(global: &Value) -> Value { + /// Initialise the `Number` 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_exponential, "toExponential", &prototype, 1); @@ -574,7 +577,7 @@ impl Number { PARSE_FLOAT_MAX_ARG_COUNT, ); - let number = make_constructor_fn( + let number_object = make_constructor_fn( Self::NAME, Self::LENGTH, Self::make_number, @@ -586,7 +589,7 @@ impl Number { // Constants from: // https://tc39.es/ecma262/#sec-properties-of-the-number-constructor { - let mut properties = number.as_object_mut().expect("'Number' object"); + let mut properties = number_object.as_object_mut().expect("'Number' object"); properties.insert_field("EPSILON", Value::from(f64::EPSILON)); properties.insert_field("MAX_SAFE_INTEGER", Value::from(Self::MAX_SAFE_INTEGER)); properties.insert_field("MIN_SAFE_INTEGER", Value::from(Self::MIN_SAFE_INTEGER)); @@ -597,15 +600,7 @@ impl Number { properties.insert_field("NaN", Value::from(f64::NAN)); } - number - } - - /// Initialise the `Number` 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, number_object) } /// The abstract operation Number::equal takes arguments diff --git a/boa/src/builtins/number/tests.rs b/boa/src/builtins/number/tests.rs index 2696ee2f269..b2fcafd0caa 100644 --- a/boa/src/builtins/number/tests.rs +++ b/boa/src/builtins/number/tests.rs @@ -1,11 +1,6 @@ #![allow(clippy::float_cmp)] -use crate::{ - builtins::{Number, Value}, - exec::Interpreter, - forward, forward_val, - realm::Realm, -}; +use crate::{builtins::Number, exec::Interpreter, forward, forward_val, realm::Realm}; #[test] fn integer_number_primitive_to_number_object() { @@ -19,13 +14,6 @@ fn integer_number_primitive_to_number_object() { assert_eq!(forward(&mut engine, scenario), "true"); } -#[test] -fn check_number_constructor_is_function() { - let global = Value::new_object(None); - let number_constructor = Number::create(&global); - assert_eq!(number_constructor.is_function(), true); -} - #[test] fn call_number() { let realm = Realm::create(); diff --git a/boa/src/builtins/object/mod.rs b/boa/src/builtins/object/mod.rs index 58c2aeedaa1..b6a25879a28 100644 --- a/boa/src/builtins/object/mod.rs +++ b/boa/src/builtins/object/mod.rs @@ -502,8 +502,11 @@ pub fn property_is_enumerable(this: &Value, args: &[Value], ctx: &mut Interprete })) } -/// Create a new `Object` object. -pub fn create(global: &Value) -> Value { +/// Initialise the `Object` object on the global object. +#[inline] +pub fn init(global: &Value) -> (&str, Value) { + let _timer = BoaProfiler::global().start_event("object", "init"); + let prototype = Value::new_object(None); make_builtin_fn(has_own_property, "hasOwnProperty", &prototype, 0); @@ -522,13 +525,5 @@ pub fn create(global: &Value) -> Value { make_builtin_fn(define_property, "defineProperty", &object, 3); make_builtin_fn(is, "is", &object, 2); - object -} - -/// Initialise the `Object` object on the global object. -#[inline] -pub fn init(global: &Value) -> (&str, Value) { - let _timer = BoaProfiler::global().start_event("object", "init"); - - ("Object", create(global)) + ("Object", object) } diff --git a/boa/src/builtins/string/mod.rs b/boa/src/builtins/string/mod.rs index 6ff42c903cc..fa0e00eaf92 100644 --- a/boa/src/builtins/string/mod.rs +++ b/boa/src/builtins/string/mod.rs @@ -999,9 +999,12 @@ impl String { RegExp::match_all(&re, ctx.to_string(this)?.to_string()) } - /// Create a new `String` object. - pub(crate) fn create(global: &Value) -> Value { - // Create prototype + /// Initialise the `String` object on the global object. + #[inline] + pub(crate) fn init(global: &Value) -> (&str, Value) { + let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); + + // Create `String` `prototype` let prototype = Value::new_object(Some(global)); let length = Property::default().value(Value::from(0)); @@ -1032,21 +1035,15 @@ impl String { make_builtin_fn(Self::match_all, "matchAll", &prototype, 1); make_builtin_fn(Self::replace, "replace", &prototype, 2); - make_constructor_fn( + let string_object = make_constructor_fn( Self::NAME, Self::LENGTH, Self::make_string, global, prototype, true, - ) - } - - /// Initialise the `String` 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, string_object) } } diff --git a/boa/src/builtins/string/tests.rs b/boa/src/builtins/string/tests.rs index 6a1a1502730..ce4673de07c 100644 --- a/boa/src/builtins/string/tests.rs +++ b/boa/src/builtins/string/tests.rs @@ -1,39 +1,33 @@ -use super::*; use crate::{exec::Interpreter, forward, forward_val, realm::Realm}; +///TODO: re-enable when getProperty() is finished; #[test] -fn check_string_constructor_is_function() { - let global = Value::new_object(None); - let string_constructor = String::create(&global); - assert_eq!(string_constructor.is_function(), true); +#[ignore] +fn length() { + //TEST262: https://github.com/tc39/test262/blob/master/test/built-ins/String/length.js + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + let init = r#" + const a = new String(' '); + const b = new String('\ud834\udf06'); + const c = new String(' \b '); + cosnt d = new String('中文长度') + "#; + eprintln!("{}", forward(&mut engine, init)); + let a = forward(&mut engine, "a.length"); + assert_eq!(a, "1"); + let b = forward(&mut engine, "b.length"); + // TODO: fix this + // unicode surrogate pair length should be 1 + // utf16/usc2 length should be 2 + // utf8 length should be 4 + assert_eq!(b, "2"); + let c = forward(&mut engine, "c.length"); + assert_eq!(c, "3"); + let d = forward(&mut engine, "d.length"); + assert_eq!(d, "4"); } -// #[test] -// TODO: re-enable when getProperty() is finished; -// fn length() { -// //TEST262: https://github.com/tc39/test262/blob/master/test/built-ins/String/length.js -// let mut engine = Interpreter::new(); -// let init = r#" -// const a = new String(' '); -// const b = new String('\ud834\udf06'); -// const c = new String(' \b '); -// cosnt d = new String('中文长度') -// "#; -// eprintln!("{}", forward(&mut engine, init)); -// let a = forward(&mut engine, "a.length"); -// assert_eq!(a, String::from("1")); -// let b = forward(&mut engine, "b.length"); -// // TODO: fix this -// // unicode surrogate pair length should be 1 -// // utf16/usc2 length should be 2 -// // utf8 length should be 4 -// //assert_eq!(b, String::from("2")); -// let c = forward(&mut engine, "c.length"); -// assert_eq!(c, String::from("3")); -// let d = forward(&mut engine, "d.length"); -// assert_eq!(d, String::from("4")); -// } - #[test] fn new_string_has_length() { let realm = Realm::create(); diff --git a/boa/src/builtins/symbol/mod.rs b/boa/src/builtins/symbol/mod.rs index a82a0a530c0..0a615378d4c 100644 --- a/boa/src/builtins/symbol/mod.rs +++ b/boa/src/builtins/symbol/mod.rs @@ -98,27 +98,25 @@ impl Symbol { Ok(Value::from(format!("Symbol({})", description))) } - /// Create a new `Symbol` object. - pub(crate) fn create(global: &Value) -> Value { + /// Initialise the `Symbol` object on the global object. + #[inline] + pub fn init(global: &Value) -> (&str, Value) { + let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); + // Create prototype object let prototype = Value::new_object(Some(global)); make_builtin_fn(Self::to_string, "toString", &prototype, 0); - make_constructor_fn( + + let symbol_object = make_constructor_fn( Self::NAME, Self::LENGTH, Self::call, global, prototype, false, - ) - } - - /// Initialise the `Symbol` object on the global object. - #[inline] - pub fn init(global: &Value) -> (&str, Value) { - let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); + ); - (Self::NAME, Self::create(global)) + (Self::NAME, symbol_object) } } diff --git a/boa/src/builtins/symbol/tests.rs b/boa/src/builtins/symbol/tests.rs index 16cb6c23a0b..4dd2b462d82 100644 --- a/boa/src/builtins/symbol/tests.rs +++ b/boa/src/builtins/symbol/tests.rs @@ -1,13 +1,5 @@ -use super::*; use crate::{exec::Interpreter, forward, forward_val, realm::Realm}; -#[test] -fn check_symbol_constructor_is_function() { - let global = Value::new_object(None); - let symbol_constructor = Symbol::create(&global); - assert_eq!(symbol_constructor.is_function(), true); -} - #[test] fn call_symbol_and_check_return_type() { let realm = Realm::create();