Closed Bug 366941 Opened 14 years ago Closed 14 years ago

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

Categories

(Core :: JavaScript Engine, defect, P1)

defect

Tracking

()

VERIFIED FIXED
mozilla1.9alpha4

People

(Reporter: sayrer, Assigned: brendan)

References

Details

(Keywords: dev-doc-complete)

Attachments

(1 file)

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>
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
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
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
Priority: -- → P1
Target Milestone: --- → mozilla1.9alpha4
Blocks: js1.8
Attached patch fixSplinter Review
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)
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
Attachment #265299 - Flags: review?(mrbkap) → review+
Fixed on trunk:

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

/be
Status: ASSIGNED → RESOLVED
Closed: 14 years ago
Resolution: --- → FIXED
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+
Duplicate of this bug: 386048
verified fixed 1.9.0 linux/mac*/windows.
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.