mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
more refactors to utilities ... removing dependencies and the namespacing
This commit is contained in:
parent
f3a60edc5d
commit
4a8c2e8a13
7 changed files with 46 additions and 60 deletions
|
|
@ -11,11 +11,11 @@
|
|||
(from < 0 ? from + array.length : from || 0),
|
||||
(to < 0 ? to + array.length : to || array.length) + (exclusive ? 0 : 1)
|
||||
];
|
||||
}, __bind = function(func, obj, args) {
|
||||
}, __slice = Array.prototype.slice, __bind = function(func, obj, args) {
|
||||
return function() {
|
||||
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
||||
};
|
||||
}, __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
|
||||
|
|
@ -596,6 +596,7 @@
|
|||
};
|
||||
CurryNode.prototype.compile_node = function compile_node(o) {
|
||||
var ref;
|
||||
o.scope.utility('slice');
|
||||
ref = new ValueNode(literal(o.scope.utility('bind')));
|
||||
return (new CallNode(ref, [this.meth, this.context, literal(this.arguments(o))])).compile(o);
|
||||
};
|
||||
|
|
@ -1083,6 +1084,7 @@
|
|||
if (!(this.bound)) {
|
||||
return func;
|
||||
}
|
||||
o.scope.utility('slice');
|
||||
ref = new ValueNode(literal(o.scope.utility('bind')));
|
||||
return (new CallNode(ref, [literal(func), literal('this')])).compile(o);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
(function(){
|
||||
var BALANCED_PAIRS, EXPRESSION_CLOSE, EXPRESSION_END, EXPRESSION_START, IMPLICIT_BLOCK, IMPLICIT_CALL, IMPLICIT_END, IMPLICIT_FUNC, INVERSES, Rewriter, SINGLE_CLOSERS, SINGLE_LINERS, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, helpers, include, pair;
|
||||
var __bind = function(func, obj, args) {
|
||||
var __slice = Array.prototype.slice, __bind = function(func, obj, args) {
|
||||
return function() {
|
||||
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
||||
};
|
||||
}, __slice = Array.prototype.slice, __hasProp = Object.prototype.hasOwnProperty;
|
||||
}, __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
|
||||
|
|
|
|||
13
lib/scope.js
13
lib/scope.js
|
|
@ -94,18 +94,13 @@
|
|||
// Ensure the CoffeeScript utility object is included in the top level
|
||||
// then return a CallNode curried constructor bound to the utility function
|
||||
Scope.prototype.utility = function utility(name) {
|
||||
var _a, _b, _c, _d, dep;
|
||||
var _a;
|
||||
if (this.parent) {
|
||||
return Scope.root.utility(name);
|
||||
}
|
||||
if ((typeof (_d = utilities.functions[name]) !== "undefined" && _d !== null)) {
|
||||
if ((typeof (_a = utilities[name]) !== "undefined" && _a !== null)) {
|
||||
this.utilities = this.utilities || {};
|
||||
this.utilities[name] = utilities.functions[name];
|
||||
_b = (utilities.dependencies[name] || []);
|
||||
for (_a = 0, _c = _b.length; _a < _c; _a++) {
|
||||
dep = _b[_a];
|
||||
!this.utilities[dep] ? this.utility(dep) : null;
|
||||
}
|
||||
this.utilities[name] = utilities[name];
|
||||
}
|
||||
return "__" + name;
|
||||
};
|
||||
|
|
@ -115,7 +110,7 @@
|
|||
var _a, _b, key;
|
||||
_a = []; _b = this.utilities;
|
||||
for (key in _b) { if (__hasProp.call(_b, key)) {
|
||||
_a.push("__" + key + " = " + (utilities.functions[key]));
|
||||
_a.push("__" + key + " = " + (utilities[key]));
|
||||
}}
|
||||
return _a;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,15 +3,10 @@
|
|||
this.exports = this;
|
||||
}
|
||||
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 }",
|
||||
hasProp: 'Object.prototype.hasOwnProperty',
|
||||
slice: 'Array.prototype.slice'
|
||||
}
|
||||
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'
|
||||
};
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -430,6 +430,7 @@ exports.CurryNode: class CurryNode extends CallNode
|
|||
(new ArrayNode(@args)).compile o
|
||||
|
||||
compile_node: (o) ->
|
||||
o.scope.utility('slice')
|
||||
ref: new ValueNode literal(o.scope.utility('bind'))
|
||||
(new CallNode(ref, [@meth, @context, literal(@arguments(o))])).compile o
|
||||
|
||||
|
|
@ -801,6 +802,7 @@ exports.CodeNode: class CodeNode extends BaseNode
|
|||
func: "function${ if @bound then '' else name_part }(${ params.join(', ') }) {$code${@idt(if @bound then 1 else 0)}}"
|
||||
func: "($func)" if top and not @bound
|
||||
return func unless @bound
|
||||
o.scope.utility('slice')
|
||||
ref: new ValueNode literal(o.scope.utility('bind'))
|
||||
(new CallNode ref, [literal(func), literal('this')]).compile o
|
||||
|
||||
|
|
|
|||
|
|
@ -69,16 +69,15 @@ exports.Scope: class Scope
|
|||
# then return a CallNode curried constructor bound to the utility function
|
||||
utility: (name) ->
|
||||
return Scope.root.utility(name) if @parent
|
||||
if utilities.functions[name]?
|
||||
if utilities[name]?
|
||||
@utilities: or {}
|
||||
@utilities[name]: utilities.functions[name]
|
||||
@utility(dep) for dep in (utilities.dependencies[name] or []) when not @utilities[dep]
|
||||
@utilities[name]: utilities[name]
|
||||
"__$name"
|
||||
|
||||
# Formats an javascript object containing the utility methods required
|
||||
# in the scope
|
||||
included_utilities: ->
|
||||
"__$key = ${utilities.functions[key]}" for key of @utilities
|
||||
"__$key = ${utilities[key]}" for key of @utilities
|
||||
|
||||
# Does this scope reference any variables that need to be declared in the
|
||||
# given function body?
|
||||
|
|
|
|||
|
|
@ -2,42 +2,35 @@ this.exports: this unless process?
|
|||
|
||||
exports.utilities: {
|
||||
|
||||
dependencies: {
|
||||
bind: ['slice']
|
||||
}
|
||||
extend: """
|
||||
function(child, parent) {
|
||||
var ctor = function(){ };
|
||||
ctor.prototype = parent.prototype;
|
||||
child.__superClass__ = parent.prototype;
|
||||
child.prototype = new ctor();
|
||||
child.prototype.constructor = child;
|
||||
}
|
||||
"""
|
||||
|
||||
functions: {
|
||||
bind: """
|
||||
function(func, obj, args) {
|
||||
return function() {
|
||||
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
||||
};
|
||||
}
|
||||
"""
|
||||
|
||||
extend: """
|
||||
function(child, parent) {
|
||||
var ctor = function(){ };
|
||||
ctor.prototype = parent.prototype;
|
||||
child.__superClass__ = parent.prototype;
|
||||
child.prototype = new ctor();
|
||||
child.prototype.constructor = child;
|
||||
}
|
||||
"""
|
||||
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)
|
||||
];
|
||||
}
|
||||
"""
|
||||
|
||||
bind: """
|
||||
function(func, obj, args) {
|
||||
return function() {
|
||||
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
|
||||
};
|
||||
}
|
||||
"""
|
||||
hasProp: 'Object.prototype.hasOwnProperty'
|
||||
|
||||
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)
|
||||
];
|
||||
}
|
||||
"""
|
||||
|
||||
hasProp: 'Object.prototype.hasOwnProperty'
|
||||
|
||||
slice: 'Array.prototype.slice'
|
||||
}
|
||||
slice: 'Array.prototype.slice'
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue