Skip to content

Conversation

@mmatyas
Copy link
Contributor

@mmatyas mmatyas commented Apr 10, 2018

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

* @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.
Copy link
Contributor

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

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

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.
*/
Copy link
Contributor

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 */
Copy link
Contributor

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

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

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.

@mmatyas mmatyas force-pushed the builtin_codemin branch 2 times, most recently from 6807858 to 4775884 Compare April 11, 2018 09:33
@mmatyas
Copy link
Contributor Author

mmatyas commented Apr 11, 2018

Thanks, I've updated the patch to use a routine mode enum.

Copy link
Member

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

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

LGTM

@mmatyas
Copy link
Contributor Author

mmatyas commented Apr 13, 2018

Rebased to master to fix the Travis build.

Copy link
Member

@rerobika rerobika left a 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;
}
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, thanks!

Copy link
Contributor Author

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;
        }

Copy link
Member

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

@LaszloLango LaszloLango left a comment

Choose a reason for hiding this comment

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

LGTM

@LaszloLango LaszloLango merged commit 3f3d4a6 into jerryscript-project:master Apr 13, 2018
jimmy-huang pushed a commit to jimmy-huang/jerryscript that referenced this pull request May 8, 2018
…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
@mmatyas mmatyas deleted the builtin_codemin branch September 26, 2018 09:13
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.

5 participants