From f0d731009f23f1e6cd020f94cdec9e5bb8ad6df7 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 30 Mar 2010 18:27:53 -0400 Subject: [PATCH] merged matehat's utility branch, arraySlice -> slice, removed dead dependency... --- lib/lexer.js | 4 ++-- lib/nodes.js | 10 +++++----- lib/optparse.js | 2 +- lib/rewriter.js | 4 ++-- lib/utilities.js | 7 +++---- src/nodes.coffee | 6 +++--- src/utilities.coffee | 18 +++++++++++------- 7 files changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index 90914b17..c0e662df 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -1,6 +1,6 @@ (function(){ var ACCESSORS, ASSIGNMENT, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_KEYWORDS, COMMENT, COMMENT_CLEANER, CONVERSIONS, HALF_ASSIGNMENTS, HEREDOC, HEREDOC_INDENT, IDENTIFIER, INTERPOLATION, JS_CLEANER, JS_FORBIDDEN, JS_KEYWORDS, KEYWORDS, LAST_DENT, LAST_DENTS, LINE_BREAK, Lexer, MULTILINER, MULTI_DENT, NOT_REGEX, NO_NEWLINE, NUMBER, OPERATOR, REGEX_ESCAPE, REGEX_FLAGS, REGEX_INTERPOLATION, REGEX_START, RESERVED, Rewriter, STRING_NEWLINES, WHITESPACE, _a, _b, _c, _d, balanced_string, compact, count, helpers, include, k, starts, u; - var __arraySlice = Array.prototype.slice, __hasProp = Object.prototype.hasOwnProperty; + var __slice = Array.prototype.slice, __hasProp = Object.prototype.hasOwnProperty; // The CoffeeScript Lexer. Uses a series of token-matching regexes to attempt // matches against the beginning of the source code. When a match is found, // a token is produced, we consume the match, and start again. Tokens are in the @@ -237,7 +237,7 @@ // balanced (ie. strings, JS literals). Lexer.prototype.balanced_token = function balanced_token() { var delimited; - delimited = __arraySlice.call(arguments, 0, arguments.length - 0); + delimited = __slice.call(arguments, 0, arguments.length - 0); return balanced_string(this.chunk, delimited); }; // Matches and conumes comments. We pass through comments into JavaScript, diff --git a/lib/nodes.js b/lib/nodes.js index e99d40ed..6e61d3bb 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -14,11 +14,11 @@ }, __bind = function(func, obj, args) { obj = obj || {}; return (typeof args !== 'undefined' && args !== null) ? function() { - return func.apply(obj, args.concat(__arraySlice.call(arguments, 0))); + return func.apply(obj, args.concat(__slice.call(arguments, 0))); } : function() { return func.apply(obj, arguments); }; - }, __arraySlice = Array.prototype.slice; + }, __slice = Array.prototype.slice; // `nodes.coffee` contains all of the node classes for the syntax tree. Most // nodes are created as the result of actions in the [grammar](grammar.html), // but some are created by other nodes as a method of code generation. To convert @@ -1157,15 +1157,15 @@ o.scope.assign(trailing.compile(o), "arguments[arguments.length - " + this.trailings.length + " + " + i + "]"); i += 1; } - return "" + name + " = " + (o.scope.utility('arraySlice')) + ".call(arguments, " + this.index + ", arguments.length - " + (this.trailings.length) + ")"; + return "" + name + " = " + (o.scope.utility('slice')) + ".call(arguments, " + this.index + ", arguments.length - " + (this.trailings.length) + ")"; }; // A compiling a splat as a destructuring assignment means slicing arguments // from the right-hand-side's corresponding array. SplatNode.prototype.compile_value = function compile_value(o, name, index, trailings) { if ((typeof trailings !== "undefined" && trailings !== null)) { - return "" + (o.scope.utility('arraySlice')) + ".call(" + name + ", " + index + ", " + (name) + ".length - " + trailings + ")"; + return "" + (o.scope.utility('slice')) + ".call(" + name + ", " + index + ", " + (name) + ".length - " + trailings + ")"; } else { - return "" + (o.scope.utility('arraySlice')) + ".call(" + name + ", " + index + ")"; + return "" + (o.scope.utility('slice')) + ".call(" + name + ", " + index + ")"; } }; // Utility function that converts arbitrary number of elements, mixed with diff --git a/lib/optparse.js b/lib/optparse.js index 09b22be2..3eb7b1d3 100755 --- a/lib/optparse.js +++ b/lib/optparse.js @@ -24,7 +24,7 @@ arguments: [] }; args = normalize_arguments(args); - while (arg = args.shift()) { + while ((arg = args.shift())) { is_option = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG)); matched_rule = false; _b = this.rules; diff --git a/lib/rewriter.js b/lib/rewriter.js index 3f169074..3a3399f1 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -3,11 +3,11 @@ var __bind = function(func, obj, args) { obj = obj || {}; return (typeof args !== 'undefined' && args !== null) ? function() { - return func.apply(obj, args.concat(__arraySlice.call(arguments, 0))); + return func.apply(obj, args.concat(__slice.call(arguments, 0))); } : function() { return func.apply(obj, arguments); }; - }, __arraySlice = Array.prototype.slice, __hasProp = Object.prototype.hasOwnProperty; + }, __slice = Array.prototype.slice, __hasProp = Object.prototype.hasOwnProperty; // The CoffeeScript language has a good deal of optional syntax, implicit syntax, // and shorthand syntax. This can greatly complicate a grammar and bloat // the resulting parse table. Instead of making the parser handle it all, we take diff --git a/lib/utilities.js b/lib/utilities.js index 026b7cf4..c63bc2f2 100644 --- a/lib/utilities.js +++ b/lib/utilities.js @@ -12,15 +12,14 @@ return "" + (utilities.key(key)) + " = " + (utilities.functions[key].replace(/\n/g, "\n" + tab) || 'undefined'); }; utilities.dependencies = { - bind: ['arraySlice'], - splice: ['range'] + bind: ['slice'] }; utilities.functions = { extend: "function(child, parent) {\n var ctor = function(){ };\n ctor.prototype = parent.prototype;\n child.__superClass__ = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n}", - bind: "function(func, obj, args) {\n obj = obj || {};\n return (typeof args !== 'undefined' && args !== null) ? function() {\n return func.apply(obj, args.concat(" + (utilities.key('arraySlice')) + ".call(arguments, 0)));\n } : function() {\n return func.apply(obj, arguments);\n };\n}", + bind: "function(func, obj, args) {\n obj = obj || {};\n return (typeof args !== 'undefined' && args !== null) ? function() {\n return func.apply(obj, args.concat(" + (utilities.key('slice')) + ".call(arguments, 0)));\n } : function() {\n return func.apply(obj, arguments);\n };\n}", range: "function(array, from, to, exclusive) {\n return [\n (from < 0 ? from + array.length : from || 0),\n (to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)\n ];\n}", hasProp: 'Object.prototype.hasOwnProperty', - arraySlice: 'Array.prototype.slice' + slice: 'Array.prototype.slice' }; return utilities; }).call(this); diff --git a/src/nodes.coffee b/src/nodes.coffee index 5cf43e2e..ca71aa1f 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -845,13 +845,13 @@ exports.SplatNode: class SplatNode extends BaseNode for trailing in @trailings o.scope.assign(trailing.compile(o), "arguments[arguments.length - $@trailings.length + $i]") i: + 1 - "$name = ${o.scope.utility('arraySlice')}.call(arguments, $@index, arguments.length - ${@trailings.length})" + "$name = ${o.scope.utility('slice')}.call(arguments, $@index, arguments.length - ${@trailings.length})" # A compiling a splat as a destructuring assignment means slicing arguments # from the right-hand-side's corresponding array. compile_value: (o, name, index, trailings) -> - if trailings? then "${o.scope.utility('arraySlice')}.call($name, $index, ${name}.length - $trailings)" \ - else "${o.scope.utility('arraySlice')}.call($name, $index)" + if trailings? then "${o.scope.utility('slice')}.call($name, $index, ${name}.length - $trailings)" \ + else "${o.scope.utility('slice')}.call($name, $index)" # Utility function that converts arbitrary number of elements, mixed with # splats, to a proper array diff --git a/src/utilities.coffee b/src/utilities.coffee index 47943a4b..23cd0766 100644 --- a/src/utilities.coffee +++ b/src/utilities.coffee @@ -3,16 +3,16 @@ this.exports: this unless process? exports.utilities: class utilities @key: (name) -> "__$name" - + @format: (key, tab) -> "${utilities.key(key)} = ${utilities.functions[key].replace(/\n/g, "\n$tab") or 'undefined'}" - + @dependencies: { - bind: ['arraySlice'] - splice: ['range'] + bind: ['slice'] } - + @functions: { + extend: """ function(child, parent) { var ctor = function(){ }; @@ -22,16 +22,18 @@ exports.utilities: class utilities child.prototype.constructor = child; } """ + bind: """ function(func, obj, args) { obj = obj || {}; return (typeof args !== 'undefined' && args !== null) ? function() { - return func.apply(obj, args.concat(${utilities.key('arraySlice')}.call(arguments, 0))); + return func.apply(obj, args.concat(${utilities.key('slice')}.call(arguments, 0))); } : function() { return func.apply(obj, arguments); }; } """ + range: """ function(array, from, to, exclusive) { return [ @@ -40,6 +42,8 @@ exports.utilities: class utilities ]; } """ + hasProp: 'Object.prototype.hasOwnProperty' - arraySlice: 'Array.prototype.slice' + + slice: 'Array.prototype.slice' } \ No newline at end of file