From 77f1fac3a94d9aa6c42cef383e11cec893c31316 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Tue, 6 Dec 2016 16:01:28 -0800 Subject: [PATCH] RFC: Support iterables in dangerous for-of loops Right now for-of loops are translated directly into typical for loops over the input's length. That's "dangerous" because it doesn't follow the spec, but works for Arrays. This proposes adding (partial, dangerous) support for iterables to for-of loops without altering the existing fast path dangerous-for-of behavior for Arrays and other looped content which has a `length` property. This is still a "dangerous" transform! - It still uses length when available instead of iterators - If an environment does not define Symbol (native or polyfill), it falls back to length - If a break or throw occurs in a loop over an iterator, the iterator is not properly "closed", which can impact code that uses `finally {}` blocks in generators. In order to do this, for-of loops are translated into a slightly longer form. For example, given the input: ```js for ( let member of array ) { doSomething( member ); } ``` The new transform will produce: ```js for ( var list = array, iter = list.length === void 0 && typeof Symbol === 'function' && list[Symbol.iterator], i = iter ? iter.call(list) : -1; iter ? !(iter = i.next()).done : ++i < list.length; ) { var member = iter ? iter.value : list[i]; doSomething( member ); } ``` Here's the same output with annotated expressions: ```js // Same for loop structure is kept for ( // Evaluate for-of's RHS (same as before) var list = array, // If the list does not have a length (preserve "dangerous" Array behavior) iter = list.length === void 0 && // And this environment understands Symbol typeof Symbol === 'function' && // Then get the list's defined iterator // Will be undefined if Symbol.iterator is undefined // Will be undefined if list does not define an iterator // Will throw if list is null/undefined (spec compliant) list[Symbol.iterator], // If an iterator function was found i = iter // Then produce an iterator ? iter.call(list) // Otherwise init an index integer : -1 ; // End init, begin test // If an iterator is in use iter // Get the next iterator-result, returning true if it is not done ? !(iter = i.next()).done // Otherwise, increment the index and return true if it's less than the list length : ++i < list.length ; // End test, no update ) { // If an iterator is in use var member = iter // Get the current value ? iter.value // Otherwise, get the index from the list : list[i]; doSomething( member ); } ``` --- src/program/types/ForOfStatement.js | 11 +++++---- test/samples/for-of.js | 36 ++++++++++++++--------------- test/samples/loops.js | 8 +++---- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/program/types/ForOfStatement.js b/src/program/types/ForOfStatement.js index afe479c0..387ba4ee 100644 --- a/src/program/types/ForOfStatement.js +++ b/src/program/types/ForOfStatement.js @@ -33,10 +33,11 @@ export default class ForOfStatement extends LoopStatement { const key = scope.createIdentifier( 'i' ); const list = scope.createIdentifier( 'list' ); + const useLen = scope.createIdentifier( 'useLen' ); if ( this.body.synthetic ) { code.insertRight( this.left.start, `{\n${i1}` ); - code.insertLeft( this.body.body[0].end, `\n${i0}}` ); + code.insertLeft( this.body.end, `\n${i0}}` ); } const bodyStart = this.body.body[0].start; @@ -46,7 +47,9 @@ export default class ForOfStatement extends LoopStatement { code.insertRight( this.right.start, `var ${key} = 0, ${list} = ` ); - code.insertLeft( this.right.end, `; ${key} < ${list}.length; ${key} += 1` ); + code.insertLeft( this.right.end, `, ${useLen} = typeof ${list}.length === 'number' || typeof Symbol !== 'function', ${list} = ${useLen} ? ${list} : ${list}[Symbol.iterator](); ${useLen} ? ${key} < ${list}.length : !(${key} = ${list}.next()).done;` ); + + const getVal = `${useLen} ? ${list}[${key}++] : ${key}.value`; // destructuring. TODO non declaration destructuring const declarator = this.left.type === 'VariableDeclaration' && this.left.declarations[0]; @@ -65,9 +68,9 @@ export default class ForOfStatement extends LoopStatement { }); code.insertLeft( this.left.start + this.left.kind.length + 1, ref ); - code.insertLeft( this.left.end, ` = ${list}[${key}];\n${i1}` ); + code.insertLeft( this.left.end, ` = ${getVal};\n${i1}` ); } else { - code.insertLeft( this.left.end, ` = ${list}[${key}];\n\n${i1}` ); + code.insertLeft( this.left.end, ` = ${getVal};\n\n${i1}` ); } super.transpile( code, transforms ); diff --git a/test/samples/for-of.js b/test/samples/for-of.js index 01363914..ae7bd5a4 100644 --- a/test/samples/for-of.js +++ b/test/samples/for-of.js @@ -22,8 +22,8 @@ module.exports = [ }`, output: ` - for ( var i = 0, list = array; i < list.length; i += 1 ) { - var member = list[i]; + for ( var i = 0, list = array, useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done; ) { + var member = useLen ? list[i++] : i.value; doSomething( member ); }` @@ -39,8 +39,8 @@ module.exports = [ }`, output: ` - for ( var i = 0, list = [ 'a', 'b', 'c' ]; i < list.length; i += 1 ) { - var member = list[i]; + for ( var i = 0, list = [ 'a', 'b', 'c' ], useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done; ) { + var member = useLen ? list[i++] : i.value; doSomething( member ); }` @@ -59,14 +59,14 @@ module.exports = [ output: ` var loop = function () { - var member = list[i]; + var member = useLen ? list[i++] : i.value; setTimeout( function () { doSomething( member ); }); }; - for ( var i = 0, list = [ 'a', 'b', 'c' ]; i < list.length; i += 1 ) loop();` + for ( var i = 0, list = [ 'a', 'b', 'c' ], useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done; ) loop();` }, { @@ -77,8 +77,8 @@ module.exports = [ for ( let member of array ) console.log( member );`, output: ` - for ( var i = 0, list = array; i < list.length; i += 1 ) { - var member = list[i]; + for ( var i = 0, list = array, useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done; ) { + var member = useLen ? list[i++] : i.value; console.log( member ); }` @@ -94,8 +94,8 @@ module.exports = [ }`, output: ` - for (var i = 0, list = this.keys; i < list.length; i += 1) { - var key = list[i]; + for (var i = 0, list = this.keys, useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done;) { + var key = useLen ? list[i++] : i.value; console.log(key); }` @@ -111,8 +111,8 @@ module.exports = [ }`, output: ` - for ( var i = 0, list = items; i < list.length; i += 1 ) { - var item = list[i]; + for ( var i = 0, list = items, useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done; ) { + var item = useLen ? list[i++] : i.value; if ( item.foo ) { continue; } }` @@ -136,7 +136,7 @@ module.exports = [ var this$1 = this; var loop = function () { - var item = list[i]; + var item = useLen ? list[i++] : i.value; console.log( this$1, arguments$1, item ); setTimeout( function () { @@ -144,7 +144,7 @@ module.exports = [ }); }; - for ( var i = 0, list = items; i < list.length; i += 1 ) loop();` + for ( var i = 0, list = items, useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done; ) loop();` }, { @@ -187,7 +187,7 @@ module.exports = [ output: ` function foo () { var loop = function () { - var x = list[i]; + var x = useLen ? list[i++] : i.value; setTimeout( function () { console.log( x ); @@ -196,7 +196,7 @@ module.exports = [ if ( x > 10 ) { return {}; } }; - for ( var i = 0, list = y; i < list.length; i += 1 ) { + for ( var i = 0, list = y, useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done; ) { var returned = loop(); if ( returned ) return returned.v; @@ -214,8 +214,8 @@ module.exports = [ }`, output: ` - for (var i = 0, list = [{x: 1, y: 2}]; i < list.length; i += 1) { - var ref = list[i]; + for (var i = 0, list = [{x: 1, y: 2}], useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done;) { + var ref = useLen ? list[i++] : i.value; var x = ref.x; var y = ref.y; diff --git a/test/samples/loops.js b/test/samples/loops.js index 10c4857c..4ef0cf54 100644 --- a/test/samples/loops.js +++ b/test/samples/loops.js @@ -504,8 +504,8 @@ module.exports = [ output: ` for (var a = 0; a < 10; a++) { var j = 1, k = (void 0); - for (var i = 0, list = c; i < list.length; i += 1) { - var b = list[i]; + for (var i = 0, list = c, useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done;) { + var b = useLen ? list[i++] : i.value; var x = (void 0), y = 2 f(b, j, k, x, y) @@ -526,8 +526,8 @@ module.exports = [ } `, output: ` - for (var i = 0, list = c; i < list.length; i += 1) { - var b = list[i]; + for (var i = 0, list = c, useLen = typeof list.length === 'number' || typeof Symbol !== 'function', list = useLen ? list : list[Symbol.iterator](); useLen ? i < list.length : !(i = list.next()).done;) { + var b = useLen ? list[i++] : i.value; var x = (void 0), y = 2, z = (void 0); f(b, x++, y++, z++)