Browse Source

Merged `create` into `init` for builtins (#547)

pull/554/head
HalidOdat 4 years ago committed by GitHub
parent
commit
1e82e7c95a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      boa/src/builtins/array/mod.rs
  2. 23
      boa/src/builtins/bigint/mod.rs
  3. 19
      boa/src/builtins/boolean/mod.rs
  4. 8
      boa/src/builtins/boolean/tests.rs
  5. 17
      boa/src/builtins/console/mod.rs
  6. 22
      boa/src/builtins/error/mod.rs
  7. 19
      boa/src/builtins/error/range.rs
  8. 17
      boa/src/builtins/error/reference.rs
  9. 20
      boa/src/builtins/error/syntax.rs
  10. 19
      boa/src/builtins/error/type.rs
  11. 12
      boa/src/builtins/function/mod.rs
  12. 16
      boa/src/builtins/json/mod.rs
  13. 21
      boa/src/builtins/number/mod.rs
  14. 14
      boa/src/builtins/number/tests.rs
  15. 17
      boa/src/builtins/object/mod.rs
  16. 21
      boa/src/builtins/string/mod.rs
  17. 56
      boa/src/builtins/string/tests.rs
  18. 20
      boa/src/builtins/symbol/mod.rs
  19. 8
      boa/src/builtins/symbol/tests.rs

17
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)
}
}

23
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)
}
}

19
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)
}
}

8
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]

17
boa/src/builtins/console/mod.rs

@ -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);
@ -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
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)
}
}

19
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)
}
}

17
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)
}
}

20
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)
}
}

19
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)
}
}

12
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)
}

16
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)
}
}

21
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

14
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();

17
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)
}

21
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)
}
}

56
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();

20
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)
}
}

8
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();

Loading…
Cancel
Save