The default bug view has changed. See this FAQ.

Get rid of the "for([key, value] in obj)" form so that normal array destructuring works in for..in

VERIFIED FIXED in mozilla1.9alpha4

Status

()

Core
JavaScript Engine
P1
normal
VERIFIED FIXED
10 years ago
3 years ago

People

(Reporter: Robert Sayre, Assigned: brendan)

Tracking

({dev-doc-complete})

Trunk
mozilla1.9alpha4
dev-doc-complete
Points:
---
Dependency tree / graph
Bug Flags:
in-testsuite +

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Reporter)

Description

10 years ago
I'm pretty sure this a bug-- note the output of "5,6,9" at the end.

js> var list1 = [[1,2],[3,4],[5,6]];
js> var list2 = [[1,2,3],[4,5,6],[7,8,9]];
js> for each (var [foo, bar] in list1) {
    print(foo + "," + bar);
  }
1,2
3,4
5,6
js> for each (var [foo, bar, baz] in list2) {
    print(foo + "," + bar + "," + baz);
  }
1,2,3
4,5,6
7,8,9
js> function gen(list) {
    for each (var test in list) {
      yield test;
    }
  }
js> var iter1 = gen(list1);
js> for (var [foo, bar] in iter1) {
    print(foo + "," + bar);
  }
1,2
3,4
5,6
js> var iter2 = gen(list2);
js> for (var [foo, bar, baz] in iter2) {
    print(foo + "," + bar + "," + baz);
  }
typein:56: SyntaxError: invalid for/in left-hand side:
typein:56: for (var [foo, bar, baz] in iter2) {
typein:56: .........................^
js> 5,6,9
js>
(Assignee)

Comment 1

10 years ago
Yes, bug -- for the enumeration case (where the object to the right of in is not an iterator), ES4 as proposed requires a length-2 pattern. For iteration (where the object to the right of in is an iterator and can return any value), there should be no such restriction.

/be
Assignee: general → brendan
(Assignee)

Comment 2

10 years ago
Enumeration destructures via [key, value], hence the restriction. The error check is in the parser currently, but it can't decide the type of the expression to the right of 'in', in general. So the draft ES4 spec should not specify SyntaxError; the destructuring enumeration error case (pattern other than array of length 2) must be a TypeError.

/be
Status: NEW → ASSIGNED
OS: Linux → All
Hardware: PC → All
(Assignee)

Comment 3

10 years ago
ES4 proposal now says for ([key, value] in obj) is just a pair destructuring of the value returned by obj's iterator, by default the identifier string or Name if obj does not have a custom iterator::get. So sayrer's induction works as hoped, and anyone wanting the JS1.7-era key,value default enumeration will have to use for ([key, value] in iterator::items(obj)).

This bug should get fixed for 1.9 if possible, so the 1.7 experiment does not leak too far from the lab.

/be
(Assignee)

Updated

10 years ago
Priority: -- → P1
Target Milestone: --- → mozilla1.9alpha4
(Assignee)

Updated

10 years ago
Blocks: 380236
(Assignee)

Comment 4

10 years ago
Created attachment 265299 [details] [diff] [review]
fix

In JS1.8, use for ([k,v] in Iterator(o)) instead of JS1.7's for ([k,v] in o). For JS2/ES4, we want iterator::items, but I'm not adding that or related APIs for 1.8.

/be
Attachment #265299 - Flags: review?(mrbkap)

Updated

10 years ago
Summary: 3+ element destructuring assignment fails in for/in loop with a generator on the righthand side → Get rid of the "for([key, value] in obj)" form so that normal array destructuring works in for..in

Updated

10 years ago
Attachment #265299 - Flags: review?(mrbkap) → review+
(Assignee)

Comment 5

10 years ago
Fixed on trunk:

js/src/jsapi.c 3.322
js/src/jsparse.c 3.282

/be
Status: ASSIGNED → RESOLVED
Last Resolved: 10 years ago
Resolution: --- → FIXED

Comment 6

10 years ago
2007-05-27 13:13 mozilla/js/tests/browser.js 	                        1.3
2007-05-27 13:13 mozilla/js/tests/js1_7/geniter/regress-366941.js 	1.1
2007-05-27 13:13 mozilla/js/tests/js1_8/shell.js 	                1.4
2007-05-27 13:13 mozilla/js/tests/js1_8/regress/regress-366941.js 	1.1 

didn't include the Iterator(...) stuff since I wasn't clear on what was going on.
Flags: in-testsuite+
(Assignee)

Updated

10 years ago
Duplicate of this bug: 386048

Comment 8

10 years ago
This is mentioned on http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Looping_across_objects and http://developer.mozilla.org/en/docs/New_in_JavaScript_1.8
Keywords: dev-doc-complete

Comment 9

10 years ago
verified fixed 1.9.0 linux/mac*/windows.
Status: RESOLVED → VERIFIED
Blocks: 1083493
You need to log in before you can comment on or make changes to this bug.