-
Notifications
You must be signed in to change notification settings - Fork 686
Reduce code duplication between Array.forEach, some, and every #2275
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
Reduce code duplication between Array.forEach, some, and every #2275
Conversation
| * @return ecma value | ||
| * Returned value must be freed with ecma_free_value. | ||
| * If the argument is convertible to boolean true, returns ECMA_VALUE_TRUE, | ||
| * otherwise returns ECMA_VALUE_EMPTY. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To specify the return value we usually use the @returns element in the docs. (please see a method above)
| ecma_builtin_array_prototype_object_every (ecma_value_t this_arg, /**< this argument */ | ||
| ecma_value_t arg1, /**< callbackfn */ | ||
| ecma_value_t arg2) /**< thisArg */ | ||
| ecma_builtin_convert_true_maybe (ecma_value_t var) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No comment for the input argument.
| * If the argument is convertible to boolean false, returns ECMA_VALUE_FALSE, | ||
| * otherwise returns ECMA_VALUE_EMPTY. | ||
| */ | ||
| static ecma_value_t |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments are the same as above.
| * Applies the provided function to each element of the array as long as | ||
| * the return value stays undefined. If the return value stays undefined | ||
| * after processing the whole array, a default value can be returned. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just like above, the @return should be used.
| * after processing the whole array, a default value can be returned. | ||
| */ | ||
| static ecma_value_t | ||
| ecma_builtin_array_apply (ecma_value_t this_arg, /**< this argument */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this function the comments like: /* 1. */ are referring to which part of the standard? We should mention that in the comments of the function (just like in the every/some/etc.) case.
| ecma_value_t arg1, /**< callbackfn */ | ||
| ecma_value_t arg2, /**< thisArg */ | ||
| ecma_value_t (*check_stop_cb)(ecma_value_t), /**< retval update fn */ | ||
| ecma_value_t default_retval /**< default if empty */ ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO, I would avoid using the callback method to do the work. What we could do is to only pass an enum value (something like: ARRAY_EVERY, _SOME, _FOR_EACH) and based on this value we can calculate the stop and default return values.
|
|
||
| /** | ||
| * Applies the provided function to each element of the array as long as | ||
| * the return value stays undefined. If the return value stays undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the comment the undefined should be empty, as each callback returns ECMA_VALUE_EMPTY. The empty value is different from the undefined.
6807858 to
4775884
Compare
|
Thanks, I've updated the patch to use a routine mode enum. |
zherczeg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
4775884 to
bf1c380
Compare
|
Rebased to master to fix the Travis build. |
rerobika
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice patch! I have just one remark.
| if (ecma_op_to_boolean (call_value)) | ||
| { | ||
| ret_value = ECMA_VALUE_TRUE; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This if structure can be merged like this:
if (mode == ARRAY_ROUTINE_EVERY || mode == ARRAY_ROUTINE_SOME)
{
ret_value = ecma_op_to_boolean (call_value) ? ECMA_VALUE_TRUE : ECMA_VALUE_FALSE;
}Therefore ecma_op_to_boolean (call_value) is performed only once in both cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah there's a small issue actually, there are cases when the ret_value shouldn't change. The if structure can still be reduced to
if (mode == ARRAY_ROUTINE_EVERY && !ecma_op_to_boolean (call_value))
{
ret_value = ECMA_VALUE_FALSE;
}
else if (mode == ARRAY_ROUTINE_SOME && ecma_op_to_boolean (call_value))
{
ret_value = ECMA_VALUE_TRUE;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh, I see. Still looks better now.
Their implementation only differs in the stop condition and the final return value. JerryScript-DCO-1.0-Signed-off-by: Mátyás Mustoha mmatyas@inf.u-szeged.hu
bf1c380 to
eedeeff
Compare
LaszloLango
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…script-project#2275) Their implementation only differs in the stop condition and the final return value. JerryScript-DCO-1.0-Signed-off-by: Mátyás Mustoha mmatyas@inf.u-szeged.hu
Their implementation only differs in the stop condition and the
final return value.
JerryScript-DCO-1.0-Signed-off-by: Mátyás Mustoha mmatyas@inf.u-szeged.hu