From 353f8a3526d96d27de4695c2ee427e13bad3eca7 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 9 Nov 2019 11:30:53 +0100 Subject: [PATCH 1/9] Add make_fn macro in builtins module --- src/lib/builtins/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib/builtins/mod.rs b/src/lib/builtins/mod.rs index a1c89dada9c..f4e2415bf19 100644 --- a/src/lib/builtins/mod.rs +++ b/src/lib/builtins/mod.rs @@ -1,3 +1,12 @@ +/// Macro to create a new member function of a prototype +macro_rules! make_fn { + ($fn:ident, named $name:expr, with length $l:tt, of $p:ident) => { + let $fn = to_value($fn as NativeFunctionData); + $fn.set_field_slice("length", to_value($l)); + $p.set_field_slice($name, $fn); + }; +} + /// The global `Array` object pub mod array; /// the global `Symbol` Object From b776dab806c9a24085a7cffdae506c967edb32ec Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 9 Nov 2019 13:25:31 +0100 Subject: [PATCH 2/9] Replace all array methods with the make_fn macro --- src/lib/builtins/array.rs | 51 ++++++++++++++------------------------- src/lib/builtins/mod.rs | 3 +++ 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/lib/builtins/array.rs b/src/lib/builtins/array.rs index 0b1dc7ddf20..e64a4443040 100644 --- a/src/lib/builtins/array.rs +++ b/src/lib/builtins/array.rs @@ -661,41 +661,26 @@ pub fn create_constructor(global: &Value) -> Value { // Create prototype let array_prototype = ValueData::new_obj(None); - let length = Property::default().get(to_value(get_array_length as NativeFunctionData)); - array_prototype.set_prop_slice("length", length); - let concat_func = to_value(concat as NativeFunctionData); - concat_func.set_field_slice("length", to_value(1_i32)); - array_prototype.set_field_slice("concat", concat_func); - let push_func = to_value(push as NativeFunctionData); - push_func.set_field_slice("length", to_value(1_i32)); - let index_of_func = to_value(index_of as NativeFunctionData); - index_of_func.set_field_slice("length", to_value(1_i32)); - let last_index_of_func = to_value(last_index_of as NativeFunctionData); - last_index_of_func.set_field_slice("length", to_value(1_i32)); - let includes_func = to_value(includes_value as NativeFunctionData); - includes_func.set_field_slice("length", to_value(1_i32)); - let map_func = to_value(map as NativeFunctionData); - map_func.set_field_slice("length", to_value(1_i32)); - let fill_func = to_value(fill as NativeFunctionData); - fill_func.set_field_slice("length", to_value(1_i32)); - - array_prototype.set_field_slice("push", push_func); - array_prototype.set_field_slice("pop", to_value(pop as NativeFunctionData)); - array_prototype.set_field_slice("join", to_value(join as NativeFunctionData)); - array_prototype.set_field_slice("reverse", to_value(reverse as NativeFunctionData)); - array_prototype.set_field_slice("shift", to_value(shift as NativeFunctionData)); - array_prototype.set_field_slice("unshift", to_value(unshift as NativeFunctionData)); - array_prototype.set_field_slice("every", to_value(every as NativeFunctionData)); - array_prototype.set_field_slice("find", to_value(find as NativeFunctionData)); - array_prototype.set_field_slice("findIndex", to_value(find_index as NativeFunctionData)); - array_prototype.set_field_slice("includes", includes_func); - array_prototype.set_field_slice("indexOf", index_of_func); - array_prototype.set_field_slice("lastIndexOf", last_index_of_func); - array_prototype.set_field_slice("fill", fill_func); - array_prototype.set_field_slice("slice", to_value(slice as NativeFunctionData)); - array_prototype.set_field_slice("map", map_func); + + make_fn!(concat, named "concat", with length 1, of array_prototype); + make_fn!(push, named "push", with length 1, of array_prototype); + make_fn!(index_of, named "indexOf", with length 1, of array_prototype); + make_fn!(last_index_of, named "lastIndexOf", with length 1, of array_prototype); + make_fn!(includes_value, named "includes", with length 1, of array_prototype); + make_fn!(map, named "map", with length 1, of array_prototype); + make_fn!(fill, named "fill", with length 1, of array_prototype); + + make_fn!(pop, named "pop", of array_prototype); + make_fn!(join, named "join", of array_prototype); + make_fn!(reverse, named "reverse", of array_prototype); + make_fn!(shift, named "shift", of array_prototype); + make_fn!(unshift, named "unshift", of array_prototype); + make_fn!(every, named "every", of array_prototype); + make_fn!(find, named "find", of array_prototype); + make_fn!(find_index, named "findIndex", of array_prototype); + make_fn!(slice, named "slice", of array_prototype); let array = to_value(array_constructor); array.set_field_slice(PROTOTYPE, to_value(array_prototype.clone())); diff --git a/src/lib/builtins/mod.rs b/src/lib/builtins/mod.rs index f4e2415bf19..65b5e2608ee 100644 --- a/src/lib/builtins/mod.rs +++ b/src/lib/builtins/mod.rs @@ -5,6 +5,9 @@ macro_rules! make_fn { $fn.set_field_slice("length", to_value($l)); $p.set_field_slice($name, $fn); }; + ($fn:ident, named $name:expr, of $p:ident) => { + $p.set_field_slice($name, to_value($fn as NativeFunctionData)); + }; } /// The global `Array` object From f3c0363f19c24ee95f9150feebfb722f96f5d67e Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 9 Nov 2019 14:01:41 +0100 Subject: [PATCH 3/9] Set builtins method with make_fn for bool, console, error, json, math, number, object, regexp and string --- src/lib/builtins/boolean.rs | 4 ++-- src/lib/builtins/console.rs | 6 ++--- src/lib/builtins/error.rs | 2 +- src/lib/builtins/json.rs | 4 ++-- src/lib/builtins/math.rs | 38 ++++++++++++++++---------------- src/lib/builtins/number.rs | 18 +++++---------- src/lib/builtins/object.rs | 15 +++---------- src/lib/builtins/regexp.rs | 5 +++-- src/lib/builtins/string.rs | 44 ++++++++++++++++++------------------- 9 files changed, 61 insertions(+), 75 deletions(-) diff --git a/src/lib/builtins/boolean.rs b/src/lib/builtins/boolean.rs index b5d9446c668..4a1b3262e62 100644 --- a/src/lib/builtins/boolean.rs +++ b/src/lib/builtins/boolean.rs @@ -56,8 +56,8 @@ pub fn create_constructor(global: &Value) -> Value { // https://tc39.es/ecma262/#sec-properties-of-the-boolean-prototype-object let boolean_prototype = ValueData::new_obj(Some(global)); boolean_prototype.set_internal_slot("BooleanData", to_boolean(&to_value(false))); - boolean_prototype.set_field_slice("toString", to_value(to_string as NativeFunctionData)); - boolean_prototype.set_field_slice("valueOf", to_value(value_of as NativeFunctionData)); + make_fn!(to_string, named "toString", of boolean_prototype); + make_fn!(value_of, named "valueOf", of boolean_prototype); let boolean_value = to_value(boolean); boolean_prototype.set_field_slice("constructor", to_value(boolean_value.clone())); diff --git a/src/lib/builtins/console.rs b/src/lib/builtins/console.rs index 58dca491618..04dcb8ce776 100644 --- a/src/lib/builtins/console.rs +++ b/src/lib/builtins/console.rs @@ -131,8 +131,8 @@ pub fn error(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { /// Create a new `console` object pub fn create_constructor(global: &Value) -> Value { let console = ValueData::new_obj(Some(global)); - console.set_field_slice("log", to_value(log as NativeFunctionData)); - console.set_field_slice("error", to_value(error as NativeFunctionData)); - console.set_field_slice("exception", to_value(error as NativeFunctionData)); + make_fn!(log, named "log", of console); + make_fn!(error, named "error", of console); + make_fn!(error, named "exception", of console); console } diff --git a/src/lib/builtins/error.rs b/src/lib/builtins/error.rs index 1fa3e7a0c89..9da95e90f86 100644 --- a/src/lib/builtins/error.rs +++ b/src/lib/builtins/error.rs @@ -36,7 +36,7 @@ pub fn _create(global: &Value) -> Value { let prototype = ValueData::new_obj(Some(global)); prototype.set_field_slice("message", to_value("")); prototype.set_field_slice("name", to_value("Error")); - prototype.set_field_slice("toString", to_value(to_string as NativeFunctionData)); + make_fn!(to_string, named "toString", of prototype); let error = to_value(make_error as NativeFunctionData); error.set_field_slice(PROTOTYPE, prototype); error diff --git a/src/lib/builtins/json.rs b/src/lib/builtins/json.rs index 8f184fb2f1e..ddf3279b851 100644 --- a/src/lib/builtins/json.rs +++ b/src/lib/builtins/json.rs @@ -33,8 +33,8 @@ pub fn create_constructor(global: &Value) -> Value { json.kind = ObjectKind::Ordinary; let prototype = ValueData::new_obj(Some(global)); - prototype.set_field_slice("parse", to_value(parse as NativeFunctionData)); - prototype.set_field_slice("stringify", to_value(stringify as NativeFunctionData)); + make_fn!(parse, named "parse", of prototype); + make_fn!(stringify, named "stringify", of prototype); let json_value = to_value(json); json_value.set_field_slice(PROTOTYPE, prototype); diff --git a/src/lib/builtins/math.rs b/src/lib/builtins/math.rs index cfdabf50cd8..e3686200cd7 100644 --- a/src/lib/builtins/math.rs +++ b/src/lib/builtins/math.rs @@ -203,24 +203,24 @@ pub fn create_constructor(global: &Value) -> Value { math.set_field_slice("SQRT1_2", to_value(0.5_f64.sqrt())); math.set_field_slice("SQRT2", to_value(f64::consts::SQRT_2)); math.set_field_slice("PI", to_value(f64::consts::PI)); - math.set_field_slice("abs", to_value(abs as NativeFunctionData)); - math.set_field_slice("acos", to_value(acos as NativeFunctionData)); - math.set_field_slice("asin", to_value(asin as NativeFunctionData)); - math.set_field_slice("atan", to_value(atan as NativeFunctionData)); - math.set_field_slice("atan2", to_value(atan2 as NativeFunctionData)); - math.set_field_slice("cbrt", to_value(cbrt as NativeFunctionData)); - math.set_field_slice("ceil", to_value(ceil as NativeFunctionData)); - math.set_field_slice("cos", to_value(cos as NativeFunctionData)); - math.set_field_slice("exp", to_value(exp as NativeFunctionData)); - math.set_field_slice("floor", to_value(floor as NativeFunctionData)); - math.set_field_slice("log", to_value(log as NativeFunctionData)); - math.set_field_slice("max", to_value(max as NativeFunctionData)); - math.set_field_slice("min", to_value(min as NativeFunctionData)); - math.set_field_slice("pow", to_value(pow as NativeFunctionData)); - math.set_field_slice("random", to_value(_random as NativeFunctionData)); - math.set_field_slice("round", to_value(round as NativeFunctionData)); - math.set_field_slice("sin", to_value(sin as NativeFunctionData)); - math.set_field_slice("sqrt", to_value(sqrt as NativeFunctionData)); - math.set_field_slice("tan", to_value(tan as NativeFunctionData)); + make_fn!(abs, named "abs", of math); + make_fn!(acos, named "acos", of math); + make_fn!(asin, named "asin", of math); + make_fn!(atan, named "atan", of math); + make_fn!(atan2, named "atan2", of math); + make_fn!(cbrt, named "cbrt", of math); + make_fn!(ceil, named "ceil", of math); + make_fn!(cos, named "cos", of math); + make_fn!(exp, named "exp", of math); + make_fn!(floor, named "floor", of math); + make_fn!(log, named "log", of math); + make_fn!(max, named "max", of math); + make_fn!(min, named "min", of math); + make_fn!(pow, named "pow", of math); + make_fn!(_random, named "random", of math); + make_fn!(round, named "round" , of math); + make_fn!(sin, named "sin", of math); + make_fn!(sqrt, named "sqrt", of math); + make_fn!(tan, named "tan", of math); math } diff --git a/src/lib/builtins/number.rs b/src/lib/builtins/number.rs index 6b99046235a..1b60afcf29e 100644 --- a/src/lib/builtins/number.rs +++ b/src/lib/builtins/number.rs @@ -145,18 +145,12 @@ pub fn create_constructor(global: &Value) -> Value { number_prototype.set_internal_slot("NumberData", to_value(0)); - number_prototype.set_field_slice( - "toExponential", - to_value(to_exponential as NativeFunctionData), - ); - number_prototype.set_field_slice("toFixed", to_value(to_fixed as NativeFunctionData)); - number_prototype.set_field_slice( - "toLocaleString", - to_value(to_locale_string as NativeFunctionData), - ); - number_prototype.set_field_slice("toPrecision", to_value(to_precision as NativeFunctionData)); - number_prototype.set_field_slice("toString", to_value(to_string as NativeFunctionData)); - number_prototype.set_field_slice("valueOf", to_value(value_of as NativeFunctionData)); + make_fn!(to_exponential, named "toExponential", of number_prototype); + make_fn!(to_fixed, named "toFixed", of number_prototype); + make_fn!(to_locale_string, named "toLocaleString", of number_prototype); + make_fn!(to_precision, named "toPrecision", of number_prototype); + make_fn!(to_string, named "toString", of number_prototype); + make_fn!(value_of, named "valueOf", of number_prototype); let number = to_value(number_constructor); number_prototype.set_field_slice("constructor", number.clone()); diff --git a/src/lib/builtins/object.rs b/src/lib/builtins/object.rs index 6bac66b72b3..8d595ab6235 100644 --- a/src/lib/builtins/object.rs +++ b/src/lib/builtins/object.rs @@ -565,17 +565,8 @@ pub fn create_constructor(_: &Value) -> Value { object.set_field_slice("length", to_value(1_i32)); object.set_field_slice(PROTOTYPE, to_value(prototype)); - object.set_field_slice( - "setPrototypeOf", - to_value(set_proto_of as NativeFunctionData), - ); - object.set_field_slice( - "getPrototypeOf", - to_value(get_proto_of as NativeFunctionData), - ); - object.set_field_slice( - "defineProperty", - to_value(define_prop as NativeFunctionData), - ); + make_fn!(set_proto_of, named "setPrototypeOf", of object); + make_fn!(get_proto_of, named "getPrototypeOf", of object); + make_fn!(define_prop, named "defineProperty", of object); object } diff --git a/src/lib/builtins/regexp.rs b/src/lib/builtins/regexp.rs index 5fd97217a3a..8bbef3ac7d6 100644 --- a/src/lib/builtins/regexp.rs +++ b/src/lib/builtins/regexp.rs @@ -336,8 +336,9 @@ pub fn create_constructor(global: &Value) -> Value { // Create prototype let proto = ValueData::new_obj(Some(global)); - proto.set_field_slice("test", to_value(test as NativeFunctionData)); - proto.set_field_slice("exec", to_value(exec as NativeFunctionData)); + make_fn!(test, named "test", of proto); + make_fn!(exec, named "exec", of proto); + make_fn!(to_string, named "toString", of proto); proto.set_field_slice("toString", to_value(to_string as NativeFunctionData)); proto.set_field_slice("lastIndex", to_value(0)); proto.set_prop_slice("dotAll", _make_prop(get_dot_all)); diff --git a/src/lib/builtins/string.rs b/src/lib/builtins/string.rs index d543072c095..f96ff3e8e78 100644 --- a/src/lib/builtins/string.rs +++ b/src/lib/builtins/string.rs @@ -744,28 +744,28 @@ pub fn create_constructor(global: &Value) -> Value { let prop = Property::default().get(to_value(get_string_length as NativeFunctionData)); proto.set_prop_slice("length", prop); - proto.set_field_slice("charAt", to_value(char_at as NativeFunctionData)); - proto.set_field_slice("charCodeAt", to_value(char_code_at as NativeFunctionData)); - proto.set_field_slice("toString", to_value(to_string as NativeFunctionData)); - proto.set_field_slice("concat", to_value(concat as NativeFunctionData)); - proto.set_field_slice("repeat", to_value(repeat as NativeFunctionData)); - proto.set_field_slice("slice", to_value(slice as NativeFunctionData)); - proto.set_field_slice("startsWith", to_value(starts_with as NativeFunctionData)); - proto.set_field_slice("endsWith", to_value(ends_with as NativeFunctionData)); - proto.set_field_slice("includes", to_value(includes as NativeFunctionData)); - proto.set_field_slice("indexOf", to_value(index_of as NativeFunctionData)); - proto.set_field_slice("lastIndexOf", to_value(last_index_of as NativeFunctionData)); - proto.set_field_slice("match", to_value(r#match as NativeFunctionData)); - proto.set_field_slice("padEnd", to_value(pad_end as NativeFunctionData)); - proto.set_field_slice("padStart", to_value(pad_start as NativeFunctionData)); - proto.set_field_slice("trim", to_value(trim as NativeFunctionData)); - proto.set_field_slice("trimStart", to_value(trim_start as NativeFunctionData)); - proto.set_field_slice("toLowerCase", to_value(to_lowercase as NativeFunctionData)); - proto.set_field_slice("toUpperCase", to_value(to_uppercase as NativeFunctionData)); - proto.set_field_slice("substring", to_value(substring as NativeFunctionData)); - proto.set_field_slice("substr", to_value(substr as NativeFunctionData)); - proto.set_field_slice("valueOf", to_value(value_of as NativeFunctionData)); - proto.set_field_slice("matchAll", to_value(match_all as NativeFunctionData)); + make_fn!(char_at, named "charAt", of proto); + make_fn!(char_code_at, named "charCodeAt", of proto); + make_fn!(to_string, named "toString", of proto); + make_fn!(concat, named "concat", of proto); + make_fn!(repeat, named "repeat", of proto); + make_fn!(slice, named "slice", of proto); + make_fn!(starts_with, named "startsWith", of proto); + make_fn!(ends_with, named "endsWith", of proto); + make_fn!(includes, named "includes", of proto); + make_fn!(index_of, named "indexOf", of proto); + make_fn!(last_index_of, named "lastIndexOf", of proto); + make_fn!(r#match, named "match", of proto); + make_fn!(pad_end, named "padEnd", of proto); + make_fn!(pad_start, named "padStart", of proto); + make_fn!(trim, named "trim", of proto); + make_fn!(trim_start, named "trimStart", of proto); + make_fn!(to_lowercase, named "toLowerCase", of proto); + make_fn!(to_uppercase, named "toUpperCase", of proto); + make_fn!(substring, named "substring", of proto); + make_fn!(substr, named "substr", of proto); + make_fn!(value_of, named "valueOf", of proto); + make_fn!(match_all, named "matchAll", of proto); let string = to_value(string_constructor); proto.set_field_slice("constructor", string.clone()); From 132961be93b704edc62ead2a432bfdbb64619181 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 9 Nov 2019 14:04:16 +0100 Subject: [PATCH 4/9] Rename make_fn macro to make_builtin_fn --- src/lib/builtins/array.rs | 34 ++++++++++++++-------------- src/lib/builtins/boolean.rs | 4 ++-- src/lib/builtins/console.rs | 6 ++--- src/lib/builtins/error.rs | 2 +- src/lib/builtins/json.rs | 4 ++-- src/lib/builtins/math.rs | 38 ++++++++++++++++---------------- src/lib/builtins/number.rs | 12 +++++----- src/lib/builtins/object.rs | 6 ++--- src/lib/builtins/regexp.rs | 6 ++--- src/lib/builtins/string.rs | 44 ++++++++++++++++++------------------- 10 files changed, 78 insertions(+), 78 deletions(-) diff --git a/src/lib/builtins/array.rs b/src/lib/builtins/array.rs index e64a4443040..05e08062dbf 100644 --- a/src/lib/builtins/array.rs +++ b/src/lib/builtins/array.rs @@ -664,23 +664,23 @@ pub fn create_constructor(global: &Value) -> Value { let length = Property::default().get(to_value(get_array_length as NativeFunctionData)); array_prototype.set_prop_slice("length", length); - make_fn!(concat, named "concat", with length 1, of array_prototype); - make_fn!(push, named "push", with length 1, of array_prototype); - make_fn!(index_of, named "indexOf", with length 1, of array_prototype); - make_fn!(last_index_of, named "lastIndexOf", with length 1, of array_prototype); - make_fn!(includes_value, named "includes", with length 1, of array_prototype); - make_fn!(map, named "map", with length 1, of array_prototype); - make_fn!(fill, named "fill", with length 1, of array_prototype); - - make_fn!(pop, named "pop", of array_prototype); - make_fn!(join, named "join", of array_prototype); - make_fn!(reverse, named "reverse", of array_prototype); - make_fn!(shift, named "shift", of array_prototype); - make_fn!(unshift, named "unshift", of array_prototype); - make_fn!(every, named "every", of array_prototype); - make_fn!(find, named "find", of array_prototype); - make_fn!(find_index, named "findIndex", of array_prototype); - make_fn!(slice, named "slice", of array_prototype); + make_builtin_fn!(concat, named "concat", with length 1, of array_prototype); + make_builtin_fn!(push, named "push", with length 1, of array_prototype); + make_builtin_fn!(index_of, named "indexOf", with length 1, of array_prototype); + make_builtin_fn!(last_index_of, named "lastIndexOf", with length 1, of array_prototype); + make_builtin_fn!(includes_value, named "includes", with length 1, of array_prototype); + make_builtin_fn!(map, named "map", with length 1, of array_prototype); + make_builtin_fn!(fill, named "fill", with length 1, of array_prototype); + + make_builtin_fn!(pop, named "pop", of array_prototype); + make_builtin_fn!(join, named "join", of array_prototype); + make_builtin_fn!(reverse, named "reverse", of array_prototype); + make_builtin_fn!(shift, named "shift", of array_prototype); + make_builtin_fn!(unshift, named "unshift", of array_prototype); + make_builtin_fn!(every, named "every", of array_prototype); + make_builtin_fn!(find, named "find", of array_prototype); + make_builtin_fn!(find_index, named "findIndex", of array_prototype); + make_builtin_fn!(slice, named "slice", of array_prototype); let array = to_value(array_constructor); array.set_field_slice(PROTOTYPE, to_value(array_prototype.clone())); diff --git a/src/lib/builtins/boolean.rs b/src/lib/builtins/boolean.rs index 4a1b3262e62..08ae55cc1d6 100644 --- a/src/lib/builtins/boolean.rs +++ b/src/lib/builtins/boolean.rs @@ -56,8 +56,8 @@ pub fn create_constructor(global: &Value) -> Value { // https://tc39.es/ecma262/#sec-properties-of-the-boolean-prototype-object let boolean_prototype = ValueData::new_obj(Some(global)); boolean_prototype.set_internal_slot("BooleanData", to_boolean(&to_value(false))); - make_fn!(to_string, named "toString", of boolean_prototype); - make_fn!(value_of, named "valueOf", of boolean_prototype); + make_builtin_fn!(to_string, named "toString", of boolean_prototype); + make_builtin_fn!(value_of, named "valueOf", of boolean_prototype); let boolean_value = to_value(boolean); boolean_prototype.set_field_slice("constructor", to_value(boolean_value.clone())); diff --git a/src/lib/builtins/console.rs b/src/lib/builtins/console.rs index 04dcb8ce776..8b0fd83d388 100644 --- a/src/lib/builtins/console.rs +++ b/src/lib/builtins/console.rs @@ -131,8 +131,8 @@ pub fn error(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { /// Create a new `console` object pub fn create_constructor(global: &Value) -> Value { let console = ValueData::new_obj(Some(global)); - make_fn!(log, named "log", of console); - make_fn!(error, named "error", of console); - make_fn!(error, named "exception", of console); + make_builtin_fn!(log, named "log", of console); + make_builtin_fn!(error, named "error", of console); + make_builtin_fn!(error, named "exception", of console); console } diff --git a/src/lib/builtins/error.rs b/src/lib/builtins/error.rs index 9da95e90f86..a272a8561ec 100644 --- a/src/lib/builtins/error.rs +++ b/src/lib/builtins/error.rs @@ -36,7 +36,7 @@ pub fn _create(global: &Value) -> Value { let prototype = ValueData::new_obj(Some(global)); prototype.set_field_slice("message", to_value("")); prototype.set_field_slice("name", to_value("Error")); - make_fn!(to_string, named "toString", of prototype); + make_builtin_fn!(to_string, named "toString", of prototype); let error = to_value(make_error as NativeFunctionData); error.set_field_slice(PROTOTYPE, prototype); error diff --git a/src/lib/builtins/json.rs b/src/lib/builtins/json.rs index ddf3279b851..184050df234 100644 --- a/src/lib/builtins/json.rs +++ b/src/lib/builtins/json.rs @@ -33,8 +33,8 @@ pub fn create_constructor(global: &Value) -> Value { json.kind = ObjectKind::Ordinary; let prototype = ValueData::new_obj(Some(global)); - make_fn!(parse, named "parse", of prototype); - make_fn!(stringify, named "stringify", of prototype); + make_builtin_fn!(parse, named "parse", of prototype); + make_builtin_fn!(stringify, named "stringify", of prototype); let json_value = to_value(json); json_value.set_field_slice(PROTOTYPE, prototype); diff --git a/src/lib/builtins/math.rs b/src/lib/builtins/math.rs index e3686200cd7..36fe747f92e 100644 --- a/src/lib/builtins/math.rs +++ b/src/lib/builtins/math.rs @@ -203,24 +203,24 @@ pub fn create_constructor(global: &Value) -> Value { math.set_field_slice("SQRT1_2", to_value(0.5_f64.sqrt())); math.set_field_slice("SQRT2", to_value(f64::consts::SQRT_2)); math.set_field_slice("PI", to_value(f64::consts::PI)); - make_fn!(abs, named "abs", of math); - make_fn!(acos, named "acos", of math); - make_fn!(asin, named "asin", of math); - make_fn!(atan, named "atan", of math); - make_fn!(atan2, named "atan2", of math); - make_fn!(cbrt, named "cbrt", of math); - make_fn!(ceil, named "ceil", of math); - make_fn!(cos, named "cos", of math); - make_fn!(exp, named "exp", of math); - make_fn!(floor, named "floor", of math); - make_fn!(log, named "log", of math); - make_fn!(max, named "max", of math); - make_fn!(min, named "min", of math); - make_fn!(pow, named "pow", of math); - make_fn!(_random, named "random", of math); - make_fn!(round, named "round" , of math); - make_fn!(sin, named "sin", of math); - make_fn!(sqrt, named "sqrt", of math); - make_fn!(tan, named "tan", of math); + make_builtin_fn!(abs, named "abs", of math); + make_builtin_fn!(acos, named "acos", of math); + make_builtin_fn!(asin, named "asin", of math); + make_builtin_fn!(atan, named "atan", of math); + make_builtin_fn!(atan2, named "atan2", of math); + make_builtin_fn!(cbrt, named "cbrt", of math); + make_builtin_fn!(ceil, named "ceil", of math); + make_builtin_fn!(cos, named "cos", of math); + make_builtin_fn!(exp, named "exp", of math); + make_builtin_fn!(floor, named "floor", of math); + make_builtin_fn!(log, named "log", of math); + make_builtin_fn!(max, named "max", of math); + make_builtin_fn!(min, named "min", of math); + make_builtin_fn!(pow, named "pow", of math); + make_builtin_fn!(_random, named "random", of math); + make_builtin_fn!(round, named "round" , of math); + make_builtin_fn!(sin, named "sin", of math); + make_builtin_fn!(sqrt, named "sqrt", of math); + make_builtin_fn!(tan, named "tan", of math); math } diff --git a/src/lib/builtins/number.rs b/src/lib/builtins/number.rs index 1b60afcf29e..0d3b0440c0d 100644 --- a/src/lib/builtins/number.rs +++ b/src/lib/builtins/number.rs @@ -145,12 +145,12 @@ pub fn create_constructor(global: &Value) -> Value { number_prototype.set_internal_slot("NumberData", to_value(0)); - make_fn!(to_exponential, named "toExponential", of number_prototype); - make_fn!(to_fixed, named "toFixed", of number_prototype); - make_fn!(to_locale_string, named "toLocaleString", of number_prototype); - make_fn!(to_precision, named "toPrecision", of number_prototype); - make_fn!(to_string, named "toString", of number_prototype); - make_fn!(value_of, named "valueOf", of number_prototype); + make_builtin_fn!(to_exponential, named "toExponential", of number_prototype); + make_builtin_fn!(to_fixed, named "toFixed", of number_prototype); + make_builtin_fn!(to_locale_string, named "toLocaleString", of number_prototype); + make_builtin_fn!(to_precision, named "toPrecision", of number_prototype); + make_builtin_fn!(to_string, named "toString", of number_prototype); + make_builtin_fn!(value_of, named "valueOf", of number_prototype); let number = to_value(number_constructor); number_prototype.set_field_slice("constructor", number.clone()); diff --git a/src/lib/builtins/object.rs b/src/lib/builtins/object.rs index 8d595ab6235..a0042f55e90 100644 --- a/src/lib/builtins/object.rs +++ b/src/lib/builtins/object.rs @@ -565,8 +565,8 @@ pub fn create_constructor(_: &Value) -> Value { object.set_field_slice("length", to_value(1_i32)); object.set_field_slice(PROTOTYPE, to_value(prototype)); - make_fn!(set_proto_of, named "setPrototypeOf", of object); - make_fn!(get_proto_of, named "getPrototypeOf", of object); - make_fn!(define_prop, named "defineProperty", of object); + make_builtin_fn!(set_proto_of, named "setPrototypeOf", of object); + make_builtin_fn!(get_proto_of, named "getPrototypeOf", of object); + make_builtin_fn!(define_prop, named "defineProperty", of object); object } diff --git a/src/lib/builtins/regexp.rs b/src/lib/builtins/regexp.rs index 8bbef3ac7d6..abae940f095 100644 --- a/src/lib/builtins/regexp.rs +++ b/src/lib/builtins/regexp.rs @@ -336,9 +336,9 @@ pub fn create_constructor(global: &Value) -> Value { // Create prototype let proto = ValueData::new_obj(Some(global)); - make_fn!(test, named "test", of proto); - make_fn!(exec, named "exec", of proto); - make_fn!(to_string, named "toString", of proto); + make_builtin_fn!(test, named "test", of proto); + make_builtin_fn!(exec, named "exec", of proto); + make_builtin_fn!(to_string, named "toString", of proto); proto.set_field_slice("toString", to_value(to_string as NativeFunctionData)); proto.set_field_slice("lastIndex", to_value(0)); proto.set_prop_slice("dotAll", _make_prop(get_dot_all)); diff --git a/src/lib/builtins/string.rs b/src/lib/builtins/string.rs index f96ff3e8e78..ad89b484c4f 100644 --- a/src/lib/builtins/string.rs +++ b/src/lib/builtins/string.rs @@ -744,28 +744,28 @@ pub fn create_constructor(global: &Value) -> Value { let prop = Property::default().get(to_value(get_string_length as NativeFunctionData)); proto.set_prop_slice("length", prop); - make_fn!(char_at, named "charAt", of proto); - make_fn!(char_code_at, named "charCodeAt", of proto); - make_fn!(to_string, named "toString", of proto); - make_fn!(concat, named "concat", of proto); - make_fn!(repeat, named "repeat", of proto); - make_fn!(slice, named "slice", of proto); - make_fn!(starts_with, named "startsWith", of proto); - make_fn!(ends_with, named "endsWith", of proto); - make_fn!(includes, named "includes", of proto); - make_fn!(index_of, named "indexOf", of proto); - make_fn!(last_index_of, named "lastIndexOf", of proto); - make_fn!(r#match, named "match", of proto); - make_fn!(pad_end, named "padEnd", of proto); - make_fn!(pad_start, named "padStart", of proto); - make_fn!(trim, named "trim", of proto); - make_fn!(trim_start, named "trimStart", of proto); - make_fn!(to_lowercase, named "toLowerCase", of proto); - make_fn!(to_uppercase, named "toUpperCase", of proto); - make_fn!(substring, named "substring", of proto); - make_fn!(substr, named "substr", of proto); - make_fn!(value_of, named "valueOf", of proto); - make_fn!(match_all, named "matchAll", of proto); + make_builtin_fn!(char_at, named "charAt", of proto); + make_builtin_fn!(char_code_at, named "charCodeAt", of proto); + make_builtin_fn!(to_string, named "toString", of proto); + make_builtin_fn!(concat, named "concat", of proto); + make_builtin_fn!(repeat, named "repeat", of proto); + make_builtin_fn!(slice, named "slice", of proto); + make_builtin_fn!(starts_with, named "startsWith", of proto); + make_builtin_fn!(ends_with, named "endsWith", of proto); + make_builtin_fn!(includes, named "includes", of proto); + make_builtin_fn!(index_of, named "indexOf", of proto); + make_builtin_fn!(last_index_of, named "lastIndexOf", of proto); + make_builtin_fn!(r#match, named "match", of proto); + make_builtin_fn!(pad_end, named "padEnd", of proto); + make_builtin_fn!(pad_start, named "padStart", of proto); + make_builtin_fn!(trim, named "trim", of proto); + make_builtin_fn!(trim_start, named "trimStart", of proto); + make_builtin_fn!(to_lowercase, named "toLowerCase", of proto); + make_builtin_fn!(to_uppercase, named "toUpperCase", of proto); + make_builtin_fn!(substring, named "substring", of proto); + make_builtin_fn!(substr, named "substr", of proto); + make_builtin_fn!(value_of, named "valueOf", of proto); + make_builtin_fn!(match_all, named "matchAll", of proto); let string = to_value(string_constructor); proto.set_field_slice("constructor", string.clone()); From a4bd4340613792e3f4c110b5179b1ee29b321d96 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 9 Nov 2019 14:09:03 +0100 Subject: [PATCH 5/9] Rename the actual macro rule to make_builtin_fn --- src/lib/builtins/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/builtins/mod.rs b/src/lib/builtins/mod.rs index 65b5e2608ee..a72b89b19bb 100644 --- a/src/lib/builtins/mod.rs +++ b/src/lib/builtins/mod.rs @@ -1,5 +1,5 @@ /// Macro to create a new member function of a prototype -macro_rules! make_fn { +macro_rules! make_builtin_fn { ($fn:ident, named $name:expr, with length $l:tt, of $p:ident) => { let $fn = to_value($fn as NativeFunctionData); $fn.set_field_slice("length", to_value($l)); From 1aa80d0b008297414013b79c693cda8f9ef3e896 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 9 Nov 2019 22:08:12 +0100 Subject: [PATCH 6/9] make_builtin_fn macro without length will result to length 0 --- src/lib/builtins/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/builtins/mod.rs b/src/lib/builtins/mod.rs index a72b89b19bb..18e79c7b0b4 100644 --- a/src/lib/builtins/mod.rs +++ b/src/lib/builtins/mod.rs @@ -1,4 +1,5 @@ /// Macro to create a new member function of a prototype +/// If no length is provided, the length will be set to 0. macro_rules! make_builtin_fn { ($fn:ident, named $name:expr, with length $l:tt, of $p:ident) => { let $fn = to_value($fn as NativeFunctionData); @@ -6,7 +7,7 @@ macro_rules! make_builtin_fn { $p.set_field_slice($name, $fn); }; ($fn:ident, named $name:expr, of $p:ident) => { - $p.set_field_slice($name, to_value($fn as NativeFunctionData)); + make_builtin_fn!($fn, named $name, with length 0, of $p); }; } From 6554b51e95b2cabee97fda4d2251aee90e03be0b Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 9 Nov 2019 22:34:45 +0100 Subject: [PATCH 7/9] Add length property for every builtin method --- src/lib/builtins/array.rs | 12 ++++++------ src/lib/builtins/console.rs | 6 +++--- src/lib/builtins/json.rs | 4 ++-- src/lib/builtins/math.rs | 36 ++++++++++++++++++------------------ src/lib/builtins/number.rs | 4 ++-- src/lib/builtins/object.rs | 6 +++--- src/lib/builtins/regexp.rs | 4 ++-- src/lib/builtins/string.rs | 32 ++++++++++++++++---------------- 8 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/lib/builtins/array.rs b/src/lib/builtins/array.rs index 05e08062dbf..ae0197449ac 100644 --- a/src/lib/builtins/array.rs +++ b/src/lib/builtins/array.rs @@ -673,14 +673,14 @@ pub fn create_constructor(global: &Value) -> Value { make_builtin_fn!(fill, named "fill", with length 1, of array_prototype); make_builtin_fn!(pop, named "pop", of array_prototype); - make_builtin_fn!(join, named "join", of array_prototype); + make_builtin_fn!(join, named "join", with length 1, of array_prototype); make_builtin_fn!(reverse, named "reverse", of array_prototype); make_builtin_fn!(shift, named "shift", of array_prototype); - make_builtin_fn!(unshift, named "unshift", of array_prototype); - make_builtin_fn!(every, named "every", of array_prototype); - make_builtin_fn!(find, named "find", of array_prototype); - make_builtin_fn!(find_index, named "findIndex", of array_prototype); - make_builtin_fn!(slice, named "slice", of array_prototype); + make_builtin_fn!(unshift, named "unshift", with length 1, of array_prototype); + make_builtin_fn!(every, named "every", with length 1, of array_prototype); + make_builtin_fn!(find, named "find", with length 1, of array_prototype); + make_builtin_fn!(find_index, named "findIndex", with length 1, of array_prototype); + make_builtin_fn!(slice, named "slice", with length 2, of array_prototype); let array = to_value(array_constructor); array.set_field_slice(PROTOTYPE, to_value(array_prototype.clone())); diff --git a/src/lib/builtins/console.rs b/src/lib/builtins/console.rs index 8b0fd83d388..58dca491618 100644 --- a/src/lib/builtins/console.rs +++ b/src/lib/builtins/console.rs @@ -131,8 +131,8 @@ pub fn error(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { /// Create a new `console` object pub fn create_constructor(global: &Value) -> Value { let console = ValueData::new_obj(Some(global)); - make_builtin_fn!(log, named "log", of console); - make_builtin_fn!(error, named "error", of console); - make_builtin_fn!(error, named "exception", of console); + console.set_field_slice("log", to_value(log as NativeFunctionData)); + console.set_field_slice("error", to_value(error as NativeFunctionData)); + console.set_field_slice("exception", to_value(error as NativeFunctionData)); console } diff --git a/src/lib/builtins/json.rs b/src/lib/builtins/json.rs index 184050df234..8be05f14501 100644 --- a/src/lib/builtins/json.rs +++ b/src/lib/builtins/json.rs @@ -33,8 +33,8 @@ pub fn create_constructor(global: &Value) -> Value { json.kind = ObjectKind::Ordinary; let prototype = ValueData::new_obj(Some(global)); - make_builtin_fn!(parse, named "parse", of prototype); - make_builtin_fn!(stringify, named "stringify", of prototype); + make_builtin_fn!(parse, named "parse", with length 2, of prototype); + make_builtin_fn!(stringify, named "stringify", with length 3, of prototype); let json_value = to_value(json); json_value.set_field_slice(PROTOTYPE, prototype); diff --git a/src/lib/builtins/math.rs b/src/lib/builtins/math.rs index 36fe747f92e..2bc051664ba 100644 --- a/src/lib/builtins/math.rs +++ b/src/lib/builtins/math.rs @@ -203,24 +203,24 @@ pub fn create_constructor(global: &Value) -> Value { math.set_field_slice("SQRT1_2", to_value(0.5_f64.sqrt())); math.set_field_slice("SQRT2", to_value(f64::consts::SQRT_2)); math.set_field_slice("PI", to_value(f64::consts::PI)); - make_builtin_fn!(abs, named "abs", of math); - make_builtin_fn!(acos, named "acos", of math); - make_builtin_fn!(asin, named "asin", of math); - make_builtin_fn!(atan, named "atan", of math); - make_builtin_fn!(atan2, named "atan2", of math); - make_builtin_fn!(cbrt, named "cbrt", of math); - make_builtin_fn!(ceil, named "ceil", of math); - make_builtin_fn!(cos, named "cos", of math); - make_builtin_fn!(exp, named "exp", of math); - make_builtin_fn!(floor, named "floor", of math); - make_builtin_fn!(log, named "log", of math); - make_builtin_fn!(max, named "max", of math); - make_builtin_fn!(min, named "min", of math); - make_builtin_fn!(pow, named "pow", of math); + make_builtin_fn!(abs, named "abs", with length 1, of math); + make_builtin_fn!(acos, named "acos", with length 1, of math); + make_builtin_fn!(asin, named "asin", with length 1, of math); + make_builtin_fn!(atan, named "atan", with length 1, of math); + make_builtin_fn!(atan2, named "atan2", with length 1, of math); + make_builtin_fn!(cbrt, named "cbrt", with length 1, of math); + make_builtin_fn!(ceil, named "ceil", with length 1, of math); + make_builtin_fn!(cos, named "cos", with length 1, of math); + make_builtin_fn!(exp, named "exp", with length 1, of math); + make_builtin_fn!(floor, named "floor", with length 1, of math); + make_builtin_fn!(log, named "log", with length 1, of math); + make_builtin_fn!(max, named "max", with length 2, of math); + make_builtin_fn!(min, named "min", with length 2, of math); + make_builtin_fn!(pow, named "pow", with length 2, of math); make_builtin_fn!(_random, named "random", of math); - make_builtin_fn!(round, named "round" , of math); - make_builtin_fn!(sin, named "sin", of math); - make_builtin_fn!(sqrt, named "sqrt", of math); - make_builtin_fn!(tan, named "tan", of math); + make_builtin_fn!(round, named "round", with length 1, of math); + make_builtin_fn!(sin, named "sin", with length 1, of math); + make_builtin_fn!(sqrt, named "sqrt", with length 1, of math); + make_builtin_fn!(tan, named "tan", with length 1, of math); math } diff --git a/src/lib/builtins/number.rs b/src/lib/builtins/number.rs index 0d3b0440c0d..b6e0854278e 100644 --- a/src/lib/builtins/number.rs +++ b/src/lib/builtins/number.rs @@ -146,9 +146,9 @@ pub fn create_constructor(global: &Value) -> Value { number_prototype.set_internal_slot("NumberData", to_value(0)); make_builtin_fn!(to_exponential, named "toExponential", of number_prototype); - make_builtin_fn!(to_fixed, named "toFixed", of number_prototype); + make_builtin_fn!(to_fixed, named "toFixed", with length 1, of number_prototype); make_builtin_fn!(to_locale_string, named "toLocaleString", of number_prototype); - make_builtin_fn!(to_precision, named "toPrecision", of number_prototype); + make_builtin_fn!(to_precision, named "toPrecision", with length 1, of number_prototype); make_builtin_fn!(to_string, named "toString", of number_prototype); make_builtin_fn!(value_of, named "valueOf", of number_prototype); diff --git a/src/lib/builtins/object.rs b/src/lib/builtins/object.rs index a0042f55e90..01cb83209c0 100644 --- a/src/lib/builtins/object.rs +++ b/src/lib/builtins/object.rs @@ -565,8 +565,8 @@ pub fn create_constructor(_: &Value) -> Value { object.set_field_slice("length", to_value(1_i32)); object.set_field_slice(PROTOTYPE, to_value(prototype)); - make_builtin_fn!(set_proto_of, named "setPrototypeOf", of object); - make_builtin_fn!(get_proto_of, named "getPrototypeOf", of object); - make_builtin_fn!(define_prop, named "defineProperty", of object); + make_builtin_fn!(set_proto_of, named "setPrototypeOf", with length 2, of object); + make_builtin_fn!(get_proto_of, named "getPrototypeOf", with length 1, of object); + make_builtin_fn!(define_prop, named "defineProperty", with length 3, of object); object } diff --git a/src/lib/builtins/regexp.rs b/src/lib/builtins/regexp.rs index abae940f095..2bc71255c0a 100644 --- a/src/lib/builtins/regexp.rs +++ b/src/lib/builtins/regexp.rs @@ -336,8 +336,8 @@ pub fn create_constructor(global: &Value) -> Value { // Create prototype let proto = ValueData::new_obj(Some(global)); - make_builtin_fn!(test, named "test", of proto); - make_builtin_fn!(exec, named "exec", of proto); + make_builtin_fn!(test, named "test", with length 1, of proto); + make_builtin_fn!(exec, named "exec", with length 1, of proto); make_builtin_fn!(to_string, named "toString", of proto); proto.set_field_slice("toString", to_value(to_string as NativeFunctionData)); proto.set_field_slice("lastIndex", to_value(0)); diff --git a/src/lib/builtins/string.rs b/src/lib/builtins/string.rs index ad89b484c4f..192a5cd4744 100644 --- a/src/lib/builtins/string.rs +++ b/src/lib/builtins/string.rs @@ -744,28 +744,28 @@ pub fn create_constructor(global: &Value) -> Value { let prop = Property::default().get(to_value(get_string_length as NativeFunctionData)); proto.set_prop_slice("length", prop); - make_builtin_fn!(char_at, named "charAt", of proto); - make_builtin_fn!(char_code_at, named "charCodeAt", of proto); + make_builtin_fn!(char_at, named "charAt", with length 1, of proto); + make_builtin_fn!(char_code_at, named "charCodeAt", with length 1, of proto); make_builtin_fn!(to_string, named "toString", of proto); - make_builtin_fn!(concat, named "concat", of proto); - make_builtin_fn!(repeat, named "repeat", of proto); - make_builtin_fn!(slice, named "slice", of proto); - make_builtin_fn!(starts_with, named "startsWith", of proto); - make_builtin_fn!(ends_with, named "endsWith", of proto); - make_builtin_fn!(includes, named "includes", of proto); - make_builtin_fn!(index_of, named "indexOf", of proto); - make_builtin_fn!(last_index_of, named "lastIndexOf", of proto); - make_builtin_fn!(r#match, named "match", of proto); - make_builtin_fn!(pad_end, named "padEnd", of proto); - make_builtin_fn!(pad_start, named "padStart", of proto); + make_builtin_fn!(concat, named "concat", with length 1, of proto); + make_builtin_fn!(repeat, named "repeat", with length 1, of proto); + make_builtin_fn!(slice, named "slice", with length 2, of proto); + make_builtin_fn!(starts_with, named "startsWith", with length 1, of proto); + make_builtin_fn!(ends_with, named "endsWith", with length 1, of proto); + make_builtin_fn!(includes, named "includes", with length 1, of proto); + make_builtin_fn!(index_of, named "indexOf", with length 1, of proto); + make_builtin_fn!(last_index_of, named "lastIndexOf", with length 1, of proto); + make_builtin_fn!(r#match, named "match", with length 1, of proto); + make_builtin_fn!(pad_end, named "padEnd", with length 2, of proto); + make_builtin_fn!(pad_start, named "padStart", with length 2, of proto); make_builtin_fn!(trim, named "trim", of proto); make_builtin_fn!(trim_start, named "trimStart", of proto); make_builtin_fn!(to_lowercase, named "toLowerCase", of proto); make_builtin_fn!(to_uppercase, named "toUpperCase", of proto); - make_builtin_fn!(substring, named "substring", of proto); - make_builtin_fn!(substr, named "substr", of proto); + make_builtin_fn!(substring, named "substring", with length 2, of proto); + make_builtin_fn!(substr, named "substr", with length 2, of proto); make_builtin_fn!(value_of, named "valueOf", of proto); - make_builtin_fn!(match_all, named "matchAll", of proto); + make_builtin_fn!(match_all, named "matchAll", with length 1, of proto); let string = to_value(string_constructor); proto.set_field_slice("constructor", string.clone()); From c2d64423669bec1753b3f8cc6bc7e7ce860a7db2 Mon Sep 17 00:00:00 2001 From: Justus K Date: Sat, 9 Nov 2019 22:37:11 +0100 Subject: [PATCH 8/9] Remove duplicate definition of toString method in regexp --- src/lib/builtins/regexp.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/builtins/regexp.rs b/src/lib/builtins/regexp.rs index 2bc71255c0a..a0878860f58 100644 --- a/src/lib/builtins/regexp.rs +++ b/src/lib/builtins/regexp.rs @@ -339,7 +339,6 @@ pub fn create_constructor(global: &Value) -> Value { make_builtin_fn!(test, named "test", with length 1, of proto); make_builtin_fn!(exec, named "exec", with length 1, of proto); make_builtin_fn!(to_string, named "toString", of proto); - proto.set_field_slice("toString", to_value(to_string as NativeFunctionData)); proto.set_field_slice("lastIndex", to_value(0)); proto.set_prop_slice("dotAll", _make_prop(get_dot_all)); proto.set_prop_slice("flags", _make_prop(get_flags)); From 8f64309022d010468136cfd6cef76c2adab97aa6 Mon Sep 17 00:00:00 2001 From: Justus K Date: Mon, 11 Nov 2019 15:23:49 +0100 Subject: [PATCH 9/9] Add missing length attributes to builtins methods --- src/lib/builtins/math.rs | 2 +- src/lib/builtins/number.rs | 4 ++-- src/lib/builtins/string.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/builtins/math.rs b/src/lib/builtins/math.rs index 2bc051664ba..b8e337c1e44 100644 --- a/src/lib/builtins/math.rs +++ b/src/lib/builtins/math.rs @@ -207,7 +207,7 @@ pub fn create_constructor(global: &Value) -> Value { make_builtin_fn!(acos, named "acos", with length 1, of math); make_builtin_fn!(asin, named "asin", with length 1, of math); make_builtin_fn!(atan, named "atan", with length 1, of math); - make_builtin_fn!(atan2, named "atan2", with length 1, of math); + make_builtin_fn!(atan2, named "atan2", with length 2, of math); make_builtin_fn!(cbrt, named "cbrt", with length 1, of math); make_builtin_fn!(ceil, named "ceil", with length 1, of math); make_builtin_fn!(cos, named "cos", with length 1, of math); diff --git a/src/lib/builtins/number.rs b/src/lib/builtins/number.rs index b6e0854278e..8426a176477 100644 --- a/src/lib/builtins/number.rs +++ b/src/lib/builtins/number.rs @@ -145,11 +145,11 @@ pub fn create_constructor(global: &Value) -> Value { number_prototype.set_internal_slot("NumberData", to_value(0)); - make_builtin_fn!(to_exponential, named "toExponential", of number_prototype); + make_builtin_fn!(to_exponential, named "toExponential", with length 1, of number_prototype); make_builtin_fn!(to_fixed, named "toFixed", with length 1, of number_prototype); make_builtin_fn!(to_locale_string, named "toLocaleString", of number_prototype); make_builtin_fn!(to_precision, named "toPrecision", with length 1, of number_prototype); - make_builtin_fn!(to_string, named "toString", of number_prototype); + make_builtin_fn!(to_string, named "toString", with length 1, of number_prototype); make_builtin_fn!(value_of, named "valueOf", of number_prototype); let number = to_value(number_constructor); diff --git a/src/lib/builtins/string.rs b/src/lib/builtins/string.rs index 192a5cd4744..1ea1e73cf32 100644 --- a/src/lib/builtins/string.rs +++ b/src/lib/builtins/string.rs @@ -756,8 +756,8 @@ pub fn create_constructor(global: &Value) -> Value { make_builtin_fn!(index_of, named "indexOf", with length 1, of proto); make_builtin_fn!(last_index_of, named "lastIndexOf", with length 1, of proto); make_builtin_fn!(r#match, named "match", with length 1, of proto); - make_builtin_fn!(pad_end, named "padEnd", with length 2, of proto); - make_builtin_fn!(pad_start, named "padStart", with length 2, of proto); + make_builtin_fn!(pad_end, named "padEnd", with length 1, of proto); + make_builtin_fn!(pad_start, named "padStart", with length 1, of proto); make_builtin_fn!(trim, named "trim", of proto); make_builtin_fn!(trim_start, named "trimStart", of proto); make_builtin_fn!(to_lowercase, named "toLowerCase", of proto);