Closed
Bug 366941
Opened 17 years ago
Closed 17 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)
Core
JavaScript Engine
Tracking
()
VERIFIED
FIXED
mozilla1.9alpha4
People
(Reporter: sayrer, Assigned: brendan)
References
Details
(Keywords: dev-doc-complete)
Attachments
(1 file)
6.54 KB,
patch
|
mrbkap
:
review+
|
Details | Diff | Splinter Review |
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•17 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•17 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•17 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•17 years ago
|
Priority: -- → P1
Target Milestone: --- → mozilla1.9alpha4
Assignee | ||
Comment 4•17 years ago
|
||
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•17 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•17 years ago
|
Attachment #265299 -
Flags: review?(mrbkap) → review+
Assignee | ||
Comment 5•17 years ago
|
||
Fixed on trunk: js/src/jsapi.c 3.322 js/src/jsparse.c 3.282 /be
Status: ASSIGNED → RESOLVED
Closed: 17 years ago
Resolution: --- → FIXED
Comment 6•17 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+
Comment 8•17 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
You need to log in
before you can comment on or make changes to this bug.
Description
•