Last Comment Bug 366941 - Get rid of the "for([key, value] in obj)" form so that normal array destructuring works in
: Get rid of the "for([key, value] in obj)" form so that normal array destructu...
: dev-doc-complete
Product: Core
Classification: Components
Component: JavaScript Engine (show other bugs)
: Trunk
: All All
: P1 normal (vote)
: mozilla1.9alpha4
Assigned To: Brendan Eich [:brendan]
: Jason Orendorff [:jorendorff]
Depends on:
Blocks: js1.8 1083493
  Show dependency treegraph
Reported: 2007-01-13 17:59 PST by Robert Sayre
Modified: 2014-10-15 14:25 PDT (History)
11 users (show)
bob: in‑testsuite+
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---

fix (6.54 KB, patch)
2007-05-18 13:32 PDT, Brendan Eich [:brendan]
mrbkap: review+
Details | Diff | Splinter Review

Description User image Robert Sayre 2007-01-13 17:59:56 PST
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);
js> for each (var [foo, bar, baz] in list2) {
    print(foo + "," + bar + "," + baz);
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);
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
Comment 1 User image Brendan Eich [:brendan] 2007-01-13 18:16:22 PST
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.

Comment 2 User image Brendan Eich [:brendan] 2007-01-13 18:25:56 PST
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.

Comment 3 User image Brendan Eich [:brendan] 2007-03-30 17:30:29 PDT
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.

Comment 4 User image Brendan Eich [:brendan] 2007-05-18 13:32:33 PDT
Created attachment 265299 [details] [diff] [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.

Comment 5 User image Brendan Eich [:brendan] 2007-05-18 13:57:05 PDT
Fixed on trunk:

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

Comment 6 User image Bob Clary [:bc:] 2007-05-27 14:30:31 PDT
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.
Comment 7 User image Brendan Eich [:brendan] 2007-06-27 14:13:23 PDT
*** Bug 386048 has been marked as a duplicate of this bug. ***
Comment 9 User image Bob Clary [:bc:] 2007-09-18 11:40:26 PDT
verified fixed 1.9.0 linux/mac*/windows.

Note You need to log in before you can comment on or make changes to this bug.