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

Implement macro for setting builtin functions #206

Merged
merged 9 commits into from
Nov 12, 2019
Merged

Implement macro for setting builtin functions #206

merged 9 commits into from
Nov 12, 2019

Conversation

Stupremee
Copy link
Contributor

@Stupremee Stupremee commented Nov 9, 2019

Description

This pull request introduces a new macro called make_fn in the builtins module,
which can be used to simplify setting a builtin function and replaces every "old" way with the new macro.

Example

let some_func = to_value(func as NativeFunctionData);
some_func.set_field_slice("length", to_value(1_i32));
some_prototype.set_field_slice("someFunc", some_func); 

becomes

make_fn!(some_func, named "someFunc", with length 1, of some_prototype);

and

some_prototype.set_field_slice("someFunc", (some_func as NativeFunctionData)); 

becomes

make_fn!(some_func, named "someFunc", of some_prototype);

This change was requested #193

@jasonwilliams
Copy link
Member

Do we want to call this macro make_builtin_fn so it’s explicit that’s what it’s for?
As we can also make dynamic functions (js code) later on.

@Stupremee
Copy link
Contributor Author

Why does some functions, e.g. findIndexor find, in the array primitive doesn't set the length field?

@Stupremee Stupremee marked this pull request as ready for review November 9, 2019 13:04
@IovoslavIovchev
Copy link
Contributor

@Stupremee basically, that is where the idea for this macro came from. Some functions do not set the length when they should simply because it is something that is easy to be missed during implementation.

@Stupremee
Copy link
Contributor Author

Ah okay. So I should set the length field for every method that doesn't set it?

$p.set_field_slice($name, $fn);
};
($fn:ident, named $name:expr, of $p:ident) => {
$p.set_field_slice($name, to_value($fn as NativeFunctionData));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any functions that do not have their length set (e.g. it's undefined)? If so, this line is fine.

But if not, I think it would be better if you set the length to 0 by default.

@IovoslavIovchev
Copy link
Contributor

@Stupremee yep

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length should be 2.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
make_builtin_fn!(atan2, named "atan2", with length 1, of math);
make_builtin_fn!(atan2, named "atan2", with length 2, of math);

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_builtin_fn!(to_exponential, named "toExponential", of number_prototype);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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!(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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
make_builtin_fn!(pad_end, named "padEnd", with length 2, of proto);
make_builtin_fn!(pad_end, named "padEnd", 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
make_builtin_fn!(pad_start, named "padStart", with length 2, of proto);
make_builtin_fn!(pad_start, named "padStart", with length 1, of proto);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants