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

Set __proto__ of function instances #604

Merged
merged 2 commits into from
Aug 9, 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
58 changes: 35 additions & 23 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions boa/src/builtins/bigint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 19 additions & 19 deletions boa/src/builtins/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 15 additions & 4 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,26 @@ 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<N>(function: NativeFunctionData, name: N, parent: &Value, length: usize)
where
pub fn make_builtin_fn<N>(
function: NativeFunctionData,
name: N,
parent: &Value,
length: usize,
interpreter: &Interpreter,
) where
N: Into<String>,
{
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());
let mut function = Object::function(
Function::builtin(Vec::new(), function),
interpreter
.global()
.get_field("Function")
.get_field("prototype"),
HalidOdat marked this conversation as resolved.
Show resolved Hide resolved
);

function.insert_field("length", Value::from(length));

parent
Expand Down
4 changes: 2 additions & 2 deletions boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions boa/src/builtins/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
77 changes: 39 additions & 38 deletions boa/src/builtins/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -656,51 +657,51 @@ 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
}

/// 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))
}
}
Loading