Skip to content

Implement Iterator interface and Array iterators #2640

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

Merged
merged 1 commit into from
Feb 8, 2019

Conversation

rerobika
Copy link
Member

Related part of the standard: https://www.ecma-international.org/ecma-262/6.0/#sec-array-iterator-objects

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]

@rerobika rerobika force-pushed the array_iterator branch 2 times, most recently from 647241a to ad426b2 Compare December 17, 2018 12:21
@rerobika rerobika changed the title Implement Array iterators Implement Iterator interface and Array iterators Dec 17, 2018
@rerobika rerobika force-pushed the array_iterator branch 3 times, most recently from da66b1c to 9b1a982 Compare December 21, 2018 10:07
@zherczeg
Copy link
Member

zherczeg commented Jan 9, 2019

Please add a test to var a = []; a.keys().toString();

@rerobika rerobika added development Feature implementation ES2015 Related to ES2015 features labels Jan 30, 2019
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 after minor fix.

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

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
@rerobika
Copy link
Member Author

rerobika commented Feb 8, 2019

@LaszloLango Thanks for the review, I've updated the PR according to it.

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

@rerobika rerobika merged commit 4f5ffb4 into jerryscript-project:master Feb 8, 2019
ecma_object_t *array_object_p = ECMA_GET_POINTER (ecma_object_t,
ext_obj_p->u.pseudo_array.u2.iterated_value_cp);


Copy link
Contributor

Choose a reason for hiding this comment

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

There is two newlines there. One is enough.

uint32_t length;

/* 8 - 9. */
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering if this length query construct is used elsewhere (did not find any proof for it yet). IMHO this would make a good helper function which can return the length of an array/typedarray, but maybe in the future.


ecma_extended_object_t *ext_array_obj_p = (ecma_extended_object_t *) array_object_p;

length = ext_array_obj_p->u.array.length;
Copy link
Contributor

@galpeter galpeter Feb 8, 2019

Choose a reason for hiding this comment

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

For an ordinary array shouldn't we access the length property? Like what would happen if there is an array created like this:

var arr = [];
arr.length = 30;

And then doing iteration on this new array.

else
{
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
JERRY_ASSERT (ecma_get_object_type (array_object_p) == ECMA_OBJECT_TYPE_ARRAY);
Copy link
Contributor

Choose a reason for hiding this comment

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

What would happen if we are iterating over a function's arguments array? AFAIK that has a different type.

}

ecma_object_t *obj_p = ecma_get_object_from_value (this_val);
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this conversion object->extended_object allowed here? Shouldn't there be a check if it is an extended object at all?

* The %IteratorPrototype% object's '@@iterator' routine
*
* See also:
* ECMA-262 v6, 22.1.2.1
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 Ecma docs the 22.1.2.1 is for Array.from ( items [ , mapfn [ , thisArg ] ] ). Is this See also entry ok? The reference 25.1.2.1 %IteratorPrototype% [ @@iterator ] ( ) would be better IMHO.

@@ -75,6 +76,7 @@ LIT_MAGIC_STRING_JOIN = "join"
LIT_MAGIC_STRING_KEYS = "keys"
LIT_MAGIC_STRING_NAME = "name"
LIT_MAGIC_STRING_NULL = "null"
LIT_MAGIC_STRING_NEXT = "next"
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't the entry next be before the null?

array = [0, 1, 2, 3, 4, 5];
iterator = array.entries ();

for (var i = 0; i < array.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

So... this means that if an element is removed from the array the iterator also loses that element, and after the loop the iterator.next().done call will be true?

@rerobika rerobika deleted the array_iterator branch February 28, 2019 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development Feature implementation ES2015 Related to ES2015 features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants