more utility simplifications

This commit is contained in:
Jeremy Ashkenas 2010-03-30 19:17:40 -04:00
parent 832e1d8cb8
commit 1e1146d61d
6 changed files with 44 additions and 65 deletions

View File

@ -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 __slice = Array.prototype.slice, __hasProp = Object.prototype.hasOwnProperty;
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, balanced_string, compact, count, helpers, include, starts;
var __slice = Array.prototype.slice;
// 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
@ -610,13 +610,7 @@
// The list of keywords that are reserved by JavaScript, but not used, or are
// used by CoffeeScript internally. We throw an error when these are encountered,
// to avoid having a JavaScript error at runtime.
RESERVED = ["case", "default", "do", "function", "var", "void", "with", "const", "let", "debugger", "enum", "export", "import", "native"].concat((function() {
_c = []; _d = (u = require('./utilities')).utilities.functions;
for (k in _d) { if (__hasProp.call(_d, k)) {
_c.push("__" + k);
}}
return _c;
}).call(this));
RESERVED = ["case", "default", "do", "function", "var", "void", "with", "const", "let", "debugger", "enum", "export", "import", "native"];
// The superset of both JavaScript keywords and reserved words, none of which may
// be used as identifiers or properties.
JS_FORBIDDEN = JS_KEYWORDS.concat(RESERVED);

View File

@ -112,17 +112,13 @@
};
// Formats an javascript object containing the utility methods required
// in the scope
Scope.prototype.included_utilities = function included_utilities(tab) {
var _a, _b, _c, key;
if ((typeof (_c = this.utilities) !== "undefined" && _c !== null)) {
_a = []; _b = this.utilities;
for (key in _b) { if (__hasProp.call(_b, key)) {
_a.push(utilities.format(key, tab));
}}
return _a;
} else {
return [];
}
Scope.prototype.included_utilities = function included_utilities() {
var _a, _b, key;
_a = []; _b = this.utilities;
for (key in _b) { if (__hasProp.call(_b, key)) {
_a.push("__" + key + " = " + (utilities.functions[key]));
}}
return _a;
};
// Does this scope reference any variables that need to be declared in the
// given function body?
@ -134,8 +130,7 @@
// Does this scope reference any assignments that need to be declared at the
// top of the given function body?
Scope.prototype.has_assignments = function has_assignments(body) {
var _a;
return body === this.expressions && ((typeof (_a = this.utilities) !== "undefined" && _a !== null) || this.any(function(k, val) {
return body === this.expressions && (this.utilities || this.any(function(k, val) {
return val.assigned;
}));
};
@ -168,7 +163,7 @@
};
// Compile the JavaScript for all of the variable assignments in this scope.
Scope.prototype.compiled_assignments = function compiled_assignments(tab) {
return this.assigned_variables().concat(this.included_utilities(tab)).join(', ');
return this.assigned_variables().concat(this.included_utilities()).join(', ');
};
return Scope;
}).call(this);

View File

@ -1,21 +1,17 @@
(function(){
var utils;
if (!((typeof process !== "undefined" && process !== null))) {
this.exports = this;
}
exports.utilities = (utils = {
format: function format(key, tab) {
return "__" + key + " = " + (utils.functions[key].replace(/\n/g, "\n" + tab) || 'undefined');
},
exports.utilities = {
dependencies: {
bind: ['slice']
},
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 return function() {\n return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : 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}",
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 return function() {\n return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : 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',
slice: 'Array.prototype.slice'
}
});
};
})();

View File

@ -463,8 +463,7 @@ KEYWORDS: JS_KEYWORDS.concat COFFEE_KEYWORDS
# to avoid having a JavaScript error at runtime.
RESERVED: [
"case", "default", "do", "function", "var", "void", "with"
"const", "let", "debugger", "enum", "export", "import", "native",
("__$k" for k of (u: require './utilities').utilities.functions)...
"const", "let", "debugger", "enum", "export", "import", "native"
]
# The superset of both JavaScript keywords and reserved words, none of which may

View File

@ -74,10 +74,8 @@ exports.Scope: class Scope
# Formats an javascript object containing the utility methods required
# in the scope
included_utilities: (tab) ->
if @utilities?
utilities.format(key, tab) for key of @utilities
else []
included_utilities: ->
"__$key = ${utilities.functions[key]}" for key of @utilities
# Does this scope reference any variables that need to be declared in the
# given function body?
@ -87,7 +85,7 @@ exports.Scope: class Scope
# Does this scope reference any assignments that need to be declared at the
# top of the given function body?
has_assignments: (body) ->
body is @expressions and (@utilities? or @any (k, val) -> val.assigned)
body is @expressions and (@utilities or @any (k, val) -> val.assigned)
# Return the list of variables first declared in this scope.
declared_variables: ->
@ -104,4 +102,4 @@ exports.Scope: class Scope
# Compile the JavaScript for all of the variable assignments in this scope.
compiled_assignments: (tab) ->
[@assigned_variables()..., @included_utilities(tab)...].join ', '
[@assigned_variables()..., @included_utilities()...].join ', '

View File

@ -1,9 +1,6 @@
this.exports: this unless process?
exports.utilities: utils: {
format: (key, tab) ->
"__$key = ${utils.functions[key].replace(/\n/g, "\n$tab") or 'undefined'}"
exports.utilities: {
dependencies: {
bind: ['slice']
@ -11,32 +8,32 @@ exports.utilities: utils: {
functions: {
extend: """
extend: """
function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
child.__superClass__ = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
}
"""
var ctor = function(){ };
ctor.prototype = parent.prototype;
child.__superClass__ = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
}
"""
bind: """
bind: """
function(func, obj, args) {
return function() {
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
};
}
"""
return function() {
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
};
}
"""
range: """
range: """
function(array, from, to, exclusive) {
return [
(from < 0 ? from + array.length : from || 0),
(to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)
];
}
"""
return [
(from < 0 ? from + array.length : from || 0),
(to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)
];
}
"""
hasProp: 'Object.prototype.hasOwnProperty'