From 4c9e39afa1afd8685c5773f2c14f7b61cb05ea42 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 6 Jan 2015 17:05:52 +1100 Subject: [PATCH] replace slice with a loop in rest parameters --- lib/6to5/transformation/templates/rest.js | 3 +++ .../transformers/es6-rest-parameters.js | 27 +++++++++++-------- .../arrow-functions/expected.js | 7 +++-- .../es6-rest-parameters/multiple/expected.js | 13 ++++++--- .../es6-rest-parameters/single/expected.js | 13 ++++++--- .../es6-spread/arguments-concat/expected.js | 2 +- .../es6-spread/arguments/expected.js | 2 +- .../method-call-array-literal/expected.js | 2 +- .../es6-spread/method-call-first/expected.js | 2 +- .../es6-spread/method-call-middle/expected.js | 2 +- .../method-call-multiple-args/expected.js | 2 +- .../method-call-multiple/expected.js | 2 +- .../method-call-single-arg/expected.js | 2 +- .../misc/custom-runtime/actual.js | 4 +-- .../misc/custom-runtime/expected.js | 2 +- .../transformation/misc/runtime/actual.js | 4 +-- .../transformation/misc/runtime/expected.js | 2 +- 17 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 lib/6to5/transformation/templates/rest.js diff --git a/lib/6to5/transformation/templates/rest.js b/lib/6to5/transformation/templates/rest.js new file mode 100644 index 0000000000..f758de4d15 --- /dev/null +++ b/lib/6to5/transformation/templates/rest.js @@ -0,0 +1,3 @@ +for (var KEY = START; KEY < ARGUMENTS.length; KEY++) { + ARRAY[$__0] = ARGUMENTS[KEY]; +} diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index 37a32657dd..07eaf67870 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -1,6 +1,7 @@ -var t = require("../../types"); +var util = require("../../util"); +var t = require("../../types"); -exports.Function = function (node, parent, file) { +exports.Function = function (node, parent, file, scope) { if (!node.rest) return; var rest = node.rest; @@ -8,15 +9,19 @@ exports.Function = function (node, parent, file) { t.ensureBlock(node); - var call = file.toArray(t.identifier("arguments")); + var argsId = t.identifier("arguments"); + argsId._ignoreAliasFunctions = true; - if (node.params.length) { - call.arguments.push(t.literal(node.params.length)); - } + node.body.body.unshift( + t.variableDeclaration("var", [ + t.variableDeclarator(rest, t.arrayExpression([])) + ]), - call._ignoreAliasFunctions = true; - - node.body.body.unshift(t.variableDeclaration("var", [ - t.variableDeclarator(rest, call) - ])); + util.template("rest", { + ARGUMENTS: argsId, + START: t.literal(node.params.length), + ARRAY: rest, + KEY: file.generateUidIdentifier("key") + }) + ); }; diff --git a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js index 49c1d11e53..01275baf79 100644 --- a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js @@ -1,6 +1,9 @@ "use strict"; -var _slice = Array.prototype.slice; var concat = function () { - var arrs = _slice.call(arguments); + var arrs = []; + + for (var _key = 0; _key < arguments.length; _key++) { + arrs[$__0] = arguments[_key]; + } }; diff --git a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js index f2f9ea4fe4..006d4860d5 100644 --- a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js @@ -1,10 +1,17 @@ "use strict"; -var _slice = Array.prototype.slice; var t = function (f) { - var items = _slice.call(arguments, 1); + var items = []; + + for (var _key = 1; _key < arguments.length; _key++) { + items[$__0] = arguments[_key]; + } }; function t(f) { - var items = _slice.call(arguments, 1); + var items = []; + + for (var _key2 = 1; _key2 < arguments.length; _key2++) { + items[$__0] = arguments[_key2]; + } } diff --git a/test/fixtures/transformation/es6-rest-parameters/single/expected.js b/test/fixtures/transformation/es6-rest-parameters/single/expected.js index 306e5f6f68..2735c9409d 100644 --- a/test/fixtures/transformation/es6-rest-parameters/single/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/single/expected.js @@ -1,10 +1,17 @@ "use strict"; -var _slice = Array.prototype.slice; var t = function () { - var items = _slice.call(arguments); + var items = []; + + for (var _key = 0; _key < arguments.length; _key++) { + items[$__0] = arguments[_key]; + } }; function t() { - var items = _slice.call(arguments); + var items = []; + + for (var _key2 = 0; _key2 < arguments.length; _key2++) { + items[$__0] = arguments[_key2]; + } } diff --git a/test/fixtures/transformation/es6-spread/arguments-concat/expected.js b/test/fixtures/transformation/es6-spread/arguments-concat/expected.js index 1ba8c3aeb2..a95f66cf85 100644 --- a/test/fixtures/transformation/es6-spread/arguments-concat/expected.js +++ b/test/fixtures/transformation/es6-spread/arguments-concat/expected.js @@ -2,7 +2,7 @@ var _slice = Array.prototype.slice; function foo() { - return bar.apply(null, ["test"].concat(_slice.call(arguments))); + return bar.apply(undefined, ["test"].concat(_slice.call(arguments))); } function bar(one, two, three) { diff --git a/test/fixtures/transformation/es6-spread/arguments/expected.js b/test/fixtures/transformation/es6-spread/arguments/expected.js index c78c808ef0..52a18f07e7 100644 --- a/test/fixtures/transformation/es6-spread/arguments/expected.js +++ b/test/fixtures/transformation/es6-spread/arguments/expected.js @@ -1,7 +1,7 @@ "use strict"; function foo() { - return bar.apply(null, arguments); + return bar.apply(undefined, arguments); } function bar(one, two, three) { diff --git a/test/fixtures/transformation/es6-spread/method-call-array-literal/expected.js b/test/fixtures/transformation/es6-spread/method-call-array-literal/expected.js index 0fd8eaaaf2..a370047756 100644 --- a/test/fixtures/transformation/es6-spread/method-call-array-literal/expected.js +++ b/test/fixtures/transformation/es6-spread/method-call-array-literal/expected.js @@ -1,3 +1,3 @@ "use strict"; -f.apply(null, [1, 2, 3]); +f.apply(undefined, [1, 2, 3]); diff --git a/test/fixtures/transformation/es6-spread/method-call-first/expected.js b/test/fixtures/transformation/es6-spread/method-call-first/expected.js index ce4428f54d..1e9fad9adc 100644 --- a/test/fixtures/transformation/es6-spread/method-call-first/expected.js +++ b/test/fixtures/transformation/es6-spread/method-call-first/expected.js @@ -4,4 +4,4 @@ var _toArray = function (arr) { return Array.isArray(arr) ? arr : Array.from(arr); }; -add.apply(null, _toArray(numbers).concat([foo, bar])); +add.apply(undefined, _toArray(numbers).concat([foo, bar])); diff --git a/test/fixtures/transformation/es6-spread/method-call-middle/expected.js b/test/fixtures/transformation/es6-spread/method-call-middle/expected.js index 65b6db88be..aadd19e09e 100644 --- a/test/fixtures/transformation/es6-spread/method-call-middle/expected.js +++ b/test/fixtures/transformation/es6-spread/method-call-middle/expected.js @@ -4,4 +4,4 @@ var _toArray = function (arr) { return Array.isArray(arr) ? arr : Array.from(arr); }; -add.apply(null, [foo].concat(_toArray(numbers), [bar])); +add.apply(undefined, [foo].concat(_toArray(numbers), [bar])); diff --git a/test/fixtures/transformation/es6-spread/method-call-multiple-args/expected.js b/test/fixtures/transformation/es6-spread/method-call-multiple-args/expected.js index 5f85a3d901..27fe7bf456 100644 --- a/test/fixtures/transformation/es6-spread/method-call-multiple-args/expected.js +++ b/test/fixtures/transformation/es6-spread/method-call-multiple-args/expected.js @@ -4,4 +4,4 @@ var _toArray = function (arr) { return Array.isArray(arr) ? arr : Array.from(arr); }; -add.apply(null, [foo, bar].concat(_toArray(numbers))); +add.apply(undefined, [foo, bar].concat(_toArray(numbers))); diff --git a/test/fixtures/transformation/es6-spread/method-call-multiple/expected.js b/test/fixtures/transformation/es6-spread/method-call-multiple/expected.js index 3bf4677ba9..334aa997d7 100644 --- a/test/fixtures/transformation/es6-spread/method-call-multiple/expected.js +++ b/test/fixtures/transformation/es6-spread/method-call-multiple/expected.js @@ -4,4 +4,4 @@ var _toArray = function (arr) { return Array.isArray(arr) ? arr : Array.from(arr); }; -add.apply(null, [foo].concat(_toArray(numbers), [bar, what], _toArray(test))); +add.apply(undefined, [foo].concat(_toArray(numbers), [bar, what], _toArray(test))); diff --git a/test/fixtures/transformation/es6-spread/method-call-single-arg/expected.js b/test/fixtures/transformation/es6-spread/method-call-single-arg/expected.js index 4a1da586f0..349be59486 100644 --- a/test/fixtures/transformation/es6-spread/method-call-single-arg/expected.js +++ b/test/fixtures/transformation/es6-spread/method-call-single-arg/expected.js @@ -4,4 +4,4 @@ var _toArray = function (arr) { return Array.isArray(arr) ? arr : Array.from(arr); }; -add.apply(null, _toArray(numbers)); +add.apply(undefined, _toArray(numbers)); diff --git a/test/fixtures/transformation/misc/custom-runtime/actual.js b/test/fixtures/transformation/misc/custom-runtime/actual.js index a32bce3200..e1ca907ba1 100644 --- a/test/fixtures/transformation/misc/custom-runtime/actual.js +++ b/test/fixtures/transformation/misc/custom-runtime/actual.js @@ -1,3 +1,3 @@ -function foo(...test) { - +function foo() { + test("bar", ...arguments); } diff --git a/test/fixtures/transformation/misc/custom-runtime/expected.js b/test/fixtures/transformation/misc/custom-runtime/expected.js index 94200c84ce..8139b181c5 100644 --- a/test/fixtures/transformation/misc/custom-runtime/expected.js +++ b/test/fixtures/transformation/misc/custom-runtime/expected.js @@ -1,5 +1,5 @@ "use strict"; function foo() { - var test = customNamespace.slice.call(arguments); + test.apply(undefined, ["bar"].concat(customNamespace.slice.call(arguments))); } diff --git a/test/fixtures/transformation/misc/runtime/actual.js b/test/fixtures/transformation/misc/runtime/actual.js index a32bce3200..e1ca907ba1 100644 --- a/test/fixtures/transformation/misc/runtime/actual.js +++ b/test/fixtures/transformation/misc/runtime/actual.js @@ -1,3 +1,3 @@ -function foo(...test) { - +function foo() { + test("bar", ...arguments); } diff --git a/test/fixtures/transformation/misc/runtime/expected.js b/test/fixtures/transformation/misc/runtime/expected.js index 1427ac1536..cda4ff0fa5 100644 --- a/test/fixtures/transformation/misc/runtime/expected.js +++ b/test/fixtures/transformation/misc/runtime/expected.js @@ -1,5 +1,5 @@ "use strict"; function foo() { - var test = to5Runtime.slice.call(arguments); + test.apply(undefined, ["bar"].concat(to5Runtime.slice.call(arguments))); }