From c619f36326cac4475305f63b55df3230501130ab Mon Sep 17 00:00:00 2001 From: 54k1 <54k1@protonmail.com> Date: Thu, 6 Aug 2020 23:01:13 +0530 Subject: [PATCH 1/2] Set __proto__ of function instances --- boa/src/builtins/array/mod.rs | 58 +++++++++++++--------- boa/src/builtins/bigint/mod.rs | 8 +-- boa/src/builtins/boolean/mod.rs | 4 +- boa/src/builtins/console/mod.rs | 38 +++++++------- boa/src/builtins/error/mod.rs | 2 +- boa/src/builtins/error/range.rs | 2 +- boa/src/builtins/error/reference.rs | 2 +- boa/src/builtins/error/syntax.rs | 2 +- boa/src/builtins/error/type.rs | 2 +- boa/src/builtins/function/mod.rs | 16 +++++- boa/src/builtins/json/mod.rs | 4 +- boa/src/builtins/map/mod.rs | 12 ++--- boa/src/builtins/math/mod.rs | 77 +++++++++++++++-------------- boa/src/builtins/number/mod.rs | 71 ++++++++++++++++++++------ boa/src/builtins/object/mod.rs | 21 +++++--- boa/src/builtins/regexp/mod.rs | 6 +-- boa/src/builtins/string/mod.rs | 69 ++++++++++++++++---------- boa/src/builtins/symbol/mod.rs | 2 +- 18 files changed, 245 insertions(+), 151 deletions(-) diff --git a/boa/src/builtins/array/mod.rs b/boa/src/builtins/array/mod.rs index abb5512ec26..6976a17b9d2 100644 --- a/boa/src/builtins/array/mod.rs +++ b/boa/src/builtins/array/mod.rs @@ -1124,28 +1124,40 @@ impl Array { prototype.set_property("length", length); - make_builtin_fn(Self::concat, "concat", &prototype, 1); - make_builtin_fn(Self::push, "push", &prototype, 1); - make_builtin_fn(Self::index_of, "indexOf", &prototype, 1); - make_builtin_fn(Self::last_index_of, "lastIndexOf", &prototype, 1); - make_builtin_fn(Self::includes_value, "includes", &prototype, 1); - make_builtin_fn(Self::map, "map", &prototype, 1); - make_builtin_fn(Self::fill, "fill", &prototype, 1); - make_builtin_fn(Self::for_each, "forEach", &prototype, 1); - make_builtin_fn(Self::filter, "filter", &prototype, 1); - make_builtin_fn(Self::pop, "pop", &prototype, 0); - make_builtin_fn(Self::join, "join", &prototype, 1); - make_builtin_fn(Self::to_string, "toString", &prototype, 0); - make_builtin_fn(Self::reverse, "reverse", &prototype, 0); - make_builtin_fn(Self::shift, "shift", &prototype, 0); - make_builtin_fn(Self::unshift, "unshift", &prototype, 1); - make_builtin_fn(Self::every, "every", &prototype, 1); - make_builtin_fn(Self::find, "find", &prototype, 1); - make_builtin_fn(Self::find_index, "findIndex", &prototype, 1); - make_builtin_fn(Self::slice, "slice", &prototype, 2); - make_builtin_fn(Self::some, "some", &prototype, 2); - make_builtin_fn(Self::reduce, "reduce", &prototype, 2); - make_builtin_fn(Self::reduce_right, "reduceRight", &prototype, 2); + make_builtin_fn(Self::concat, "concat", &prototype, 1, interpreter); + make_builtin_fn(Self::push, "push", &prototype, 1, interpreter); + make_builtin_fn(Self::index_of, "indexOf", &prototype, 1, interpreter); + make_builtin_fn( + Self::last_index_of, + "lastIndexOf", + &prototype, + 1, + interpreter, + ); + make_builtin_fn(Self::includes_value, "includes", &prototype, 1, interpreter); + make_builtin_fn(Self::map, "map", &prototype, 1, interpreter); + make_builtin_fn(Self::fill, "fill", &prototype, 1, interpreter); + make_builtin_fn(Self::for_each, "forEach", &prototype, 1, interpreter); + make_builtin_fn(Self::filter, "filter", &prototype, 1, interpreter); + make_builtin_fn(Self::pop, "pop", &prototype, 0, interpreter); + make_builtin_fn(Self::join, "join", &prototype, 1, interpreter); + make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); + make_builtin_fn(Self::reverse, "reverse", &prototype, 0, interpreter); + make_builtin_fn(Self::shift, "shift", &prototype, 0, interpreter); + make_builtin_fn(Self::unshift, "unshift", &prototype, 1, interpreter); + make_builtin_fn(Self::every, "every", &prototype, 1, interpreter); + make_builtin_fn(Self::find, "find", &prototype, 1, interpreter); + make_builtin_fn(Self::find_index, "findIndex", &prototype, 1, interpreter); + make_builtin_fn(Self::slice, "slice", &prototype, 2, interpreter); + make_builtin_fn(Self::some, "some", &prototype, 2, interpreter); + make_builtin_fn(Self::reduce, "reduce", &prototype, 2, interpreter); + make_builtin_fn( + Self::reduce_right, + "reduceRight", + &prototype, + 2, + interpreter, + ); let array = make_constructor_fn( Self::NAME, @@ -1158,7 +1170,7 @@ impl Array { ); // Static Methods - make_builtin_fn(Self::is_array, "isArray", &array, 1); + make_builtin_fn(Self::is_array, "isArray", &array, 1, interpreter); (Self::NAME, array) } diff --git a/boa/src/builtins/bigint/mod.rs b/boa/src/builtins/bigint/mod.rs index aac4b68ff48..45a82a1a71f 100644 --- a/boa/src/builtins/bigint/mod.rs +++ b/boa/src/builtins/bigint/mod.rs @@ -206,8 +206,8 @@ impl BigInt { 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); + make_builtin_fn(Self::to_string, "toString", &prototype, 1, interpreter); + make_builtin_fn(Self::value_of, "valueOf", &prototype, 0, interpreter); let bigint_object = make_constructor_fn( Self::NAME, @@ -219,8 +219,8 @@ impl BigInt { true, ); - make_builtin_fn(Self::as_int_n, "asIntN", &bigint_object, 2); - make_builtin_fn(Self::as_uint_n, "asUintN", &bigint_object, 2); + make_builtin_fn(Self::as_int_n, "asIntN", &bigint_object, 2, interpreter); + make_builtin_fn(Self::as_uint_n, "asUintN", &bigint_object, 2, interpreter); (Self::NAME, bigint_object) } diff --git a/boa/src/builtins/boolean/mod.rs b/boa/src/builtins/boolean/mod.rs index 1698f33c16c..f29d6f47263 100644 --- a/boa/src/builtins/boolean/mod.rs +++ b/boa/src/builtins/boolean/mod.rs @@ -106,8 +106,8 @@ impl Boolean { // 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_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); + make_builtin_fn(Self::value_of, "valueOf", &prototype, 0, interpreter); let boolean_object = make_constructor_fn( Self::NAME, diff --git a/boa/src/builtins/console/mod.rs b/boa/src/builtins/console/mod.rs index c362e6a6fa0..54935719582 100644 --- a/boa/src/builtins/console/mod.rs +++ b/boa/src/builtins/console/mod.rs @@ -495,25 +495,25 @@ impl Console { let console = Value::new_object(Some(global)); - make_builtin_fn(Self::assert, "assert", &console, 0); - make_builtin_fn(Self::clear, "clear", &console, 0); - make_builtin_fn(Self::debug, "debug", &console, 0); - make_builtin_fn(Self::error, "error", &console, 0); - make_builtin_fn(Self::info, "info", &console, 0); - make_builtin_fn(Self::log, "log", &console, 0); - make_builtin_fn(Self::trace, "trace", &console, 0); - make_builtin_fn(Self::warn, "warn", &console, 0); - make_builtin_fn(Self::error, "exception", &console, 0); - make_builtin_fn(Self::count, "count", &console, 0); - make_builtin_fn(Self::count_reset, "countReset", &console, 0); - make_builtin_fn(Self::group, "group", &console, 0); - make_builtin_fn(Self::group, "groupCollapsed", &console, 0); - make_builtin_fn(Self::group_end, "groupEnd", &console, 0); - make_builtin_fn(Self::time, "time", &console, 0); - make_builtin_fn(Self::time_log, "timeLog", &console, 0); - make_builtin_fn(Self::time_end, "timeEnd", &console, 0); - make_builtin_fn(Self::dir, "dir", &console, 0); - make_builtin_fn(Self::dir, "dirxml", &console, 0); + make_builtin_fn(Self::assert, "assert", &console, 0, interpreter); + make_builtin_fn(Self::clear, "clear", &console, 0, interpreter); + make_builtin_fn(Self::debug, "debug", &console, 0, interpreter); + make_builtin_fn(Self::error, "error", &console, 0, interpreter); + make_builtin_fn(Self::info, "info", &console, 0, interpreter); + make_builtin_fn(Self::log, "log", &console, 0, interpreter); + make_builtin_fn(Self::trace, "trace", &console, 0, interpreter); + make_builtin_fn(Self::warn, "warn", &console, 0, interpreter); + make_builtin_fn(Self::error, "exception", &console, 0, interpreter); + make_builtin_fn(Self::count, "count", &console, 0, interpreter); + make_builtin_fn(Self::count_reset, "countReset", &console, 0, interpreter); + make_builtin_fn(Self::group, "group", &console, 0, interpreter); + make_builtin_fn(Self::group, "groupCollapsed", &console, 0, interpreter); + make_builtin_fn(Self::group_end, "groupEnd", &console, 0, interpreter); + make_builtin_fn(Self::time, "time", &console, 0, interpreter); + make_builtin_fn(Self::time_log, "timeLog", &console, 0, interpreter); + make_builtin_fn(Self::time_end, "timeEnd", &console, 0, interpreter); + make_builtin_fn(Self::dir, "dir", &console, 0, interpreter); + make_builtin_fn(Self::dir, "dirxml", &console, 0, interpreter); (Self::NAME, console) } diff --git a/boa/src/builtins/error/mod.rs b/boa/src/builtins/error/mod.rs index 99fc5f7af01..51024a8cc04 100644 --- a/boa/src/builtins/error/mod.rs +++ b/boa/src/builtins/error/mod.rs @@ -84,7 +84,7 @@ impl Error { prototype.set_field("name", Self::NAME); prototype.set_field("message", ""); - make_builtin_fn(Self::to_string, "toString", &prototype, 0); + make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); let error_object = make_constructor_fn( Self::NAME, diff --git a/boa/src/builtins/error/range.rs b/boa/src/builtins/error/range.rs index d4b2ea778f1..87c6dc2740e 100644 --- a/boa/src/builtins/error/range.rs +++ b/boa/src/builtins/error/range.rs @@ -70,7 +70,7 @@ impl RangeError { prototype.set_field("name", Self::NAME); prototype.set_field("message", ""); - make_builtin_fn(Self::to_string, "toString", &prototype, 0); + make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); let range_error_object = make_constructor_fn( Self::NAME, diff --git a/boa/src/builtins/error/reference.rs b/boa/src/builtins/error/reference.rs index 1636beec4b7..baa79d31371 100644 --- a/boa/src/builtins/error/reference.rs +++ b/boa/src/builtins/error/reference.rs @@ -68,7 +68,7 @@ impl ReferenceError { prototype.set_field("name", Self::NAME); prototype.set_field("message", ""); - make_builtin_fn(Self::to_string, "toString", &prototype, 0); + make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); let reference_error_object = make_constructor_fn( Self::NAME, diff --git a/boa/src/builtins/error/syntax.rs b/boa/src/builtins/error/syntax.rs index 07b4497f295..508015770f3 100644 --- a/boa/src/builtins/error/syntax.rs +++ b/boa/src/builtins/error/syntax.rs @@ -72,7 +72,7 @@ impl SyntaxError { prototype.set_field("name", Self::NAME); prototype.set_field("message", ""); - make_builtin_fn(Self::to_string, "toString", &prototype, 0); + make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); let syntax_error_object = make_constructor_fn( Self::NAME, diff --git a/boa/src/builtins/error/type.rs b/boa/src/builtins/error/type.rs index 63bec5122b5..5e61e91a593 100644 --- a/boa/src/builtins/error/type.rs +++ b/boa/src/builtins/error/type.rs @@ -76,7 +76,7 @@ impl TypeError { prototype.set_field("name", Self::NAME); prototype.set_field("message", ""); - make_builtin_fn(Self::to_string, "toString", &prototype, 0); + make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); let type_error_object = make_constructor_fn( Self::NAME, diff --git a/boa/src/builtins/function/mod.rs b/boa/src/builtins/function/mod.rs index 51d4e3763c6..c3e1cf5a8b7 100644 --- a/boa/src/builtins/function/mod.rs +++ b/boa/src/builtins/function/mod.rs @@ -513,8 +513,13 @@ 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(function: NativeFunctionData, name: N, parent: &Value, length: usize) -where +pub fn make_builtin_fn( + function: NativeFunctionData, + name: N, + parent: &Value, + length: usize, + interpreter: &Interpreter, +) where N: Into, { let name = name.into(); @@ -522,6 +527,13 @@ where // FIXME: function needs the Function prototype set. let mut function = Object::function(Function::builtin(Vec::new(), function), Value::null()); + function.set_prototype( + interpreter + .global() + .get_field("Function") + .get_field("prototype"), + ); + function.insert_field("length", Value::from(length)); parent diff --git a/boa/src/builtins/json/mod.rs b/boa/src/builtins/json/mod.rs index adc570fefdd..1393b483856 100644 --- a/boa/src/builtins/json/mod.rs +++ b/boa/src/builtins/json/mod.rs @@ -177,8 +177,8 @@ impl Json { 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); + make_builtin_fn(Self::parse, "parse", &json, 2, interpreter); + make_builtin_fn(Self::stringify, "stringify", &json, 3, interpreter); (Self::NAME, json) } diff --git a/boa/src/builtins/map/mod.rs b/boa/src/builtins/map/mod.rs index 0de4d536193..a6eaaf6033b 100644 --- a/boa/src/builtins/map/mod.rs +++ b/boa/src/builtins/map/mod.rs @@ -291,12 +291,12 @@ impl Map { // Create prototype let prototype = Value::new_object(Some(global)); - make_builtin_fn(Self::set, "set", &prototype, 2); - make_builtin_fn(Self::delete, "delete", &prototype, 1); - make_builtin_fn(Self::get, "get", &prototype, 1); - make_builtin_fn(Self::clear, "clear", &prototype, 0); - make_builtin_fn(Self::has, "has", &prototype, 1); - make_builtin_fn(Self::for_each, "forEach", &prototype, 1); + make_builtin_fn(Self::set, "set", &prototype, 2, interpreter); + make_builtin_fn(Self::delete, "delete", &prototype, 1, interpreter); + make_builtin_fn(Self::get, "get", &prototype, 1, interpreter); + make_builtin_fn(Self::clear, "clear", &prototype, 0, interpreter); + make_builtin_fn(Self::has, "has", &prototype, 1, interpreter); + make_builtin_fn(Self::for_each, "forEach", &prototype, 1, interpreter); let map_object = make_constructor_fn( Self::NAME, diff --git a/boa/src/builtins/math/mod.rs b/boa/src/builtins/math/mod.rs index 49189cb1f4c..fa8af32ee13 100644 --- a/boa/src/builtins/math/mod.rs +++ b/boa/src/builtins/math/mod.rs @@ -641,7 +641,8 @@ impl Math { } /// Create a new `Math` object - pub(crate) fn create(global: &Value) -> Value { + pub(crate) fn create(interpreter: &mut Interpreter) -> Value { + let global = interpreter.global(); let _timer = BoaProfiler::global().start_event("math:create", "init"); let math = Value::new_object(Some(global)); @@ -656,41 +657,42 @@ impl Math { properties.insert_field("SQRT2", Value::from(f64::consts::SQRT_2)); properties.insert_field("PI", Value::from(f64::consts::PI)); } - make_builtin_fn(Self::abs, "abs", &math, 1); - make_builtin_fn(Self::acos, "acos", &math, 1); - make_builtin_fn(Self::acosh, "acosh", &math, 1); - make_builtin_fn(Self::asin, "asin", &math, 1); - make_builtin_fn(Self::asinh, "asinh", &math, 1); - make_builtin_fn(Self::atan, "atan", &math, 1); - make_builtin_fn(Self::atanh, "atanh", &math, 1); - make_builtin_fn(Self::atan2, "atan2", &math, 2); - make_builtin_fn(Self::cbrt, "cbrt", &math, 1); - make_builtin_fn(Self::ceil, "ceil", &math, 1); - make_builtin_fn(Self::clz32, "clz32", &math, 1); - make_builtin_fn(Self::cos, "cos", &math, 1); - make_builtin_fn(Self::cosh, "cosh", &math, 1); - make_builtin_fn(Self::exp, "exp", &math, 1); - make_builtin_fn(Self::expm1, "expm1", &math, 1); - make_builtin_fn(Self::floor, "floor", &math, 1); - make_builtin_fn(Self::fround, "fround", &math, 1); - make_builtin_fn(Self::hypot, "hypot", &math, 1); - make_builtin_fn(Self::imul, "imul", &math, 1); - make_builtin_fn(Self::log, "log", &math, 1); - make_builtin_fn(Self::log1p, "log1p", &math, 1); - make_builtin_fn(Self::log10, "log10", &math, 1); - make_builtin_fn(Self::log2, "log2", &math, 1); - make_builtin_fn(Self::max, "max", &math, 2); - make_builtin_fn(Self::min, "min", &math, 2); - make_builtin_fn(Self::pow, "pow", &math, 2); - make_builtin_fn(Self::random, "random", &math, 0); - make_builtin_fn(Self::round, "round", &math, 1); - make_builtin_fn(Self::sign, "sign", &math, 1); - make_builtin_fn(Self::sin, "sin", &math, 1); - make_builtin_fn(Self::sinh, "sinh", &math, 1); - make_builtin_fn(Self::sqrt, "sqrt", &math, 1); - make_builtin_fn(Self::tan, "tan", &math, 1); - make_builtin_fn(Self::tanh, "tanh", &math, 1); - make_builtin_fn(Self::trunc, "trunc", &math, 1); + + make_builtin_fn(Self::abs, "abs", &math, 1, interpreter); + make_builtin_fn(Self::acos, "acos", &math, 1, interpreter); + make_builtin_fn(Self::acosh, "acosh", &math, 1, interpreter); + make_builtin_fn(Self::asin, "asin", &math, 1, interpreter); + make_builtin_fn(Self::asinh, "asinh", &math, 1, interpreter); + make_builtin_fn(Self::atan, "atan", &math, 1, interpreter); + make_builtin_fn(Self::atanh, "atanh", &math, 1, interpreter); + make_builtin_fn(Self::atan2, "atan2", &math, 2, interpreter); + make_builtin_fn(Self::cbrt, "cbrt", &math, 1, interpreter); + make_builtin_fn(Self::ceil, "ceil", &math, 1, interpreter); + make_builtin_fn(Self::clz32, "clz32", &math, 1, interpreter); + make_builtin_fn(Self::cos, "cos", &math, 1, interpreter); + make_builtin_fn(Self::cosh, "cosh", &math, 1, interpreter); + make_builtin_fn(Self::exp, "exp", &math, 1, interpreter); + make_builtin_fn(Self::expm1, "expm1", &math, 1, interpreter); + make_builtin_fn(Self::floor, "floor", &math, 1, interpreter); + make_builtin_fn(Self::fround, "fround", &math, 1, interpreter); + make_builtin_fn(Self::hypot, "hypot", &math, 1, interpreter); + make_builtin_fn(Self::imul, "imul", &math, 1, interpreter); + make_builtin_fn(Self::log, "log", &math, 1, interpreter); + make_builtin_fn(Self::log1p, "log1p", &math, 1, interpreter); + make_builtin_fn(Self::log10, "log10", &math, 1, interpreter); + make_builtin_fn(Self::log2, "log2", &math, 1, interpreter); + make_builtin_fn(Self::max, "max", &math, 2, interpreter); + make_builtin_fn(Self::min, "min", &math, 2, interpreter); + make_builtin_fn(Self::pow, "pow", &math, 2, interpreter); + make_builtin_fn(Self::random, "random", &math, 0, interpreter); + make_builtin_fn(Self::round, "round", &math, 1, interpreter); + make_builtin_fn(Self::sign, "sign", &math, 1, interpreter); + make_builtin_fn(Self::sin, "sin", &math, 1, interpreter); + make_builtin_fn(Self::sinh, "sinh", &math, 1, interpreter); + make_builtin_fn(Self::sqrt, "sqrt", &math, 1, interpreter); + make_builtin_fn(Self::tan, "tan", &math, 1, interpreter); + make_builtin_fn(Self::tanh, "tanh", &math, 1, interpreter); + make_builtin_fn(Self::trunc, "trunc", &math, 1, interpreter); math } @@ -698,9 +700,8 @@ impl Math { /// Initialise the `Math` object on the global object. #[inline] pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); - (Self::NAME, Self::create(global)) + (Self::NAME, Self::create(interpreter)) } } diff --git a/boa/src/builtins/number/mod.rs b/boa/src/builtins/number/mod.rs index fa738d75c05..d34b6065913 100644 --- a/boa/src/builtins/number/mod.rs +++ b/boa/src/builtins/number/mod.rs @@ -733,23 +733,48 @@ impl Number { let prototype = Value::new_object(Some(global)); - make_builtin_fn(Self::to_exponential, "toExponential", &prototype, 1); - make_builtin_fn(Self::to_fixed, "toFixed", &prototype, 1); - make_builtin_fn(Self::to_locale_string, "toLocaleString", &prototype, 0); - make_builtin_fn(Self::to_precision, "toPrecision", &prototype, 1); - 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); + make_builtin_fn( + Self::to_exponential, + "toExponential", + &prototype, + 1, + interpreter, + ); + make_builtin_fn(Self::to_fixed, "toFixed", &prototype, 1, interpreter); + make_builtin_fn( + Self::to_locale_string, + "toLocaleString", + &prototype, + 0, + interpreter, + ); + make_builtin_fn( + Self::to_precision, + "toPrecision", + &prototype, + 1, + interpreter, + ); + make_builtin_fn(Self::to_string, "toString", &prototype, 1, interpreter); + make_builtin_fn(Self::value_of, "valueOf", &prototype, 0, interpreter); + + make_builtin_fn( + Self::parse_int, + "parseInt", + global, + PARSE_INT_MAX_ARG_COUNT, + interpreter, + ); make_builtin_fn( Self::parse_float, "parseFloat", global, PARSE_FLOAT_MAX_ARG_COUNT, + interpreter, ); - make_builtin_fn(Self::global_is_finite, "isFinite", global, 1); - make_builtin_fn(Self::global_is_nan, "isNaN", global, 1); + make_builtin_fn(Self::global_is_finite, "isFinite", global, 1, interpreter); + make_builtin_fn(Self::global_is_nan, "isNaN", global, 1, interpreter); let number_object = make_constructor_fn( Self::NAME, @@ -761,10 +786,28 @@ impl Number { true, ); - make_builtin_fn(Self::number_is_finite, "isFinite", &number_object, 1); - make_builtin_fn(Self::number_is_nan, "isNaN", &number_object, 1); - make_builtin_fn(Self::is_safe_integer, "isSafeInteger", &number_object, 1); - make_builtin_fn(Self::number_is_integer, "isInteger", &number_object, 1); + make_builtin_fn( + Self::number_is_finite, + "isFinite", + &number_object, + 1, + interpreter, + ); + make_builtin_fn(Self::number_is_nan, "isNaN", &number_object, 1, interpreter); + make_builtin_fn( + Self::is_safe_integer, + "isSafeInteger", + &number_object, + 1, + interpreter, + ); + make_builtin_fn( + Self::number_is_integer, + "isInteger", + &number_object, + 1, + interpreter, + ); // Constants from: // https://tc39.es/ecma262/#sec-properties-of-the-number-constructor diff --git a/boa/src/builtins/object/mod.rs b/boa/src/builtins/object/mod.rs index 77ac66274db..cc574d3554b 100644 --- a/boa/src/builtins/object/mod.rs +++ b/boa/src/builtins/object/mod.rs @@ -581,23 +581,30 @@ pub fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { let prototype = Value::new_object(None); - make_builtin_fn(has_own_property, "hasOwnProperty", &prototype, 0); + make_builtin_fn( + has_own_property, + "hasOwnProperty", + &prototype, + 0, + interpreter, + ); make_builtin_fn( property_is_enumerable, "propertyIsEnumerable", &prototype, 0, + interpreter, ); - make_builtin_fn(to_string, "toString", &prototype, 0); + make_builtin_fn(to_string, "toString", &prototype, 0, interpreter); let object = make_constructor_fn("Object", 1, make_object, global, prototype, true, true); // static methods of the builtin Object - make_builtin_fn(create, "create", &object, 2); - make_builtin_fn(set_prototype_of, "setPrototypeOf", &object, 2); - make_builtin_fn(get_prototype_of, "getPrototypeOf", &object, 1); - make_builtin_fn(define_property, "defineProperty", &object, 3); - make_builtin_fn(is, "is", &object, 2); + make_builtin_fn(create, "create", &object, 2, interpreter); + make_builtin_fn(set_prototype_of, "setPrototypeOf", &object, 2, interpreter); + make_builtin_fn(get_prototype_of, "getPrototypeOf", &object, 1, interpreter); + make_builtin_fn(define_property, "defineProperty", &object, 3, interpreter); + make_builtin_fn(is, "is", &object, 2, interpreter); ("Object", object) } diff --git a/boa/src/builtins/regexp/mod.rs b/boa/src/builtins/regexp/mod.rs index a537f83f6e2..579ec609308 100644 --- a/boa/src/builtins/regexp/mod.rs +++ b/boa/src/builtins/regexp/mod.rs @@ -487,9 +487,9 @@ impl RegExp { .unwrap() .insert_field("lastIndex", Value::from(0)); - make_builtin_fn(Self::test, "test", &prototype, 1); - make_builtin_fn(Self::exec, "exec", &prototype, 1); - make_builtin_fn(Self::to_string, "toString", &prototype, 0); + make_builtin_fn(Self::test, "test", &prototype, 1, interpreter); + make_builtin_fn(Self::exec, "exec", &prototype, 1, interpreter); + make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); // TODO: make them accessor properties, not methods. // make_builtin_fn(Self::get_dot_all, "dotAll", &prototype, 0); diff --git a/boa/src/builtins/string/mod.rs b/boa/src/builtins/string/mod.rs index 4b83ba13fad..80bc7909d75 100644 --- a/boa/src/builtins/string/mod.rs +++ b/boa/src/builtins/string/mod.rs @@ -1003,39 +1003,58 @@ impl String { /// Initialise the `String` object on the global object. #[inline] pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); // Create `String` `prototype` + + let global = interpreter.global(); let prototype = Value::new_object(Some(global)); let length = Property::default().value(Value::from(0)); prototype.set_property("length", length); - make_builtin_fn(Self::char_at, "charAt", &prototype, 1); - make_builtin_fn(Self::char_code_at, "charCodeAt", &prototype, 1); - make_builtin_fn(Self::to_string, "toString", &prototype, 0); - make_builtin_fn(Self::concat, "concat", &prototype, 1); - make_builtin_fn(Self::repeat, "repeat", &prototype, 1); - make_builtin_fn(Self::slice, "slice", &prototype, 2); - make_builtin_fn(Self::starts_with, "startsWith", &prototype, 1); - make_builtin_fn(Self::ends_with, "endsWith", &prototype, 1); - make_builtin_fn(Self::includes, "includes", &prototype, 1); - make_builtin_fn(Self::index_of, "indexOf", &prototype, 1); - make_builtin_fn(Self::last_index_of, "lastIndexOf", &prototype, 1); - make_builtin_fn(Self::r#match, "match", &prototype, 1); - make_builtin_fn(Self::pad_end, "padEnd", &prototype, 1); - make_builtin_fn(Self::pad_start, "padStart", &prototype, 1); - make_builtin_fn(Self::trim, "trim", &prototype, 0); - make_builtin_fn(Self::trim_start, "trimStart", &prototype, 0); - make_builtin_fn(Self::trim_end, "trimEnd", &prototype, 0); - make_builtin_fn(Self::to_lowercase, "toLowerCase", &prototype, 0); - make_builtin_fn(Self::to_uppercase, "toUpperCase", &prototype, 0); - make_builtin_fn(Self::substring, "substring", &prototype, 2); - make_builtin_fn(Self::substr, "substr", &prototype, 2); - make_builtin_fn(Self::value_of, "valueOf", &prototype, 0); - make_builtin_fn(Self::match_all, "matchAll", &prototype, 1); - make_builtin_fn(Self::replace, "replace", &prototype, 2); + make_builtin_fn(Self::char_at, "charAt", &prototype, 1, interpreter); + make_builtin_fn(Self::char_code_at, "charCodeAt", &prototype, 1, interpreter); + make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); + make_builtin_fn(Self::concat, "concat", &prototype, 1, interpreter); + make_builtin_fn(Self::repeat, "repeat", &prototype, 1, interpreter); + make_builtin_fn(Self::slice, "slice", &prototype, 2, interpreter); + make_builtin_fn(Self::starts_with, "startsWith", &prototype, 1, interpreter); + make_builtin_fn(Self::ends_with, "endsWith", &prototype, 1, interpreter); + make_builtin_fn(Self::includes, "includes", &prototype, 1, interpreter); + make_builtin_fn(Self::index_of, "indexOf", &prototype, 1, interpreter); + make_builtin_fn( + Self::last_index_of, + "lastIndexOf", + &prototype, + 1, + interpreter, + ); + make_builtin_fn(Self::r#match, "match", &prototype, 1, interpreter); + make_builtin_fn(Self::pad_end, "padEnd", &prototype, 1, interpreter); + make_builtin_fn(Self::pad_start, "padStart", &prototype, 1, interpreter); + make_builtin_fn(Self::trim, "trim", &prototype, 0, interpreter); + make_builtin_fn(Self::trim_start, "trimStart", &prototype, 0, interpreter); + make_builtin_fn(Self::trim_end, "trimEnd", &prototype, 0, interpreter); + make_builtin_fn( + Self::to_lowercase, + "toLowerCase", + &prototype, + 0, + interpreter, + ); + make_builtin_fn( + Self::to_uppercase, + "toUpperCase", + &prototype, + 0, + interpreter, + ); + make_builtin_fn(Self::substring, "substring", &prototype, 2, interpreter); + make_builtin_fn(Self::substr, "substr", &prototype, 2, interpreter); + make_builtin_fn(Self::value_of, "valueOf", &prototype, 0, interpreter); + make_builtin_fn(Self::match_all, "matchAll", &prototype, 1, interpreter); + make_builtin_fn(Self::replace, "replace", &prototype, 2, interpreter); let string_object = make_constructor_fn( Self::NAME, diff --git a/boa/src/builtins/symbol/mod.rs b/boa/src/builtins/symbol/mod.rs index 3498970fecc..853aa90d869 100644 --- a/boa/src/builtins/symbol/mod.rs +++ b/boa/src/builtins/symbol/mod.rs @@ -141,7 +141,7 @@ impl Symbol { // Create prototype object let prototype = Value::new_object(Some(global)); - make_builtin_fn(Self::to_string, "toString", &prototype, 0); + make_builtin_fn(Self::to_string, "toString", &prototype, 0, interpreter); let symbol_object = make_constructor_fn( Self::NAME, From 03e797c81f15a15e6e7e802b46ca06ef21a62eef Mon Sep 17 00:00:00 2001 From: 54k1 <54k1@protonmail.com> Date: Fri, 7 Aug 2020 07:11:37 +0530 Subject: [PATCH 2/2] Initialize prototype in Object::function and remove FIXME --- boa/src/builtins/function/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/boa/src/builtins/function/mod.rs b/boa/src/builtins/function/mod.rs index c3e1cf5a8b7..fb701dd6b10 100644 --- a/boa/src/builtins/function/mod.rs +++ b/boa/src/builtins/function/mod.rs @@ -525,9 +525,8 @@ pub fn make_builtin_fn( let name = name.into(); let _timer = BoaProfiler::global().start_event(&format!("make_builtin_fn: {}", &name), "init"); - // FIXME: function needs the Function prototype set. - let mut function = Object::function(Function::builtin(Vec::new(), function), Value::null()); - function.set_prototype( + let mut function = Object::function( + Function::builtin(Vec::new(), function), interpreter .global() .get_field("Function")