another reshuffle ... removed utilities.coffee entirely.

This commit is contained in:
Jeremy Ashkenas
2010-03-30 19:42:09 -04:00
parent 4a8c2e8a13
commit 6d7a04228f
5 changed files with 92 additions and 114 deletions

View File

@@ -430,10 +430,15 @@ 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'))
CurryNode.assign_bind()
ref: new ValueNode literal '__bind'
(new CallNode(ref, [@meth, @context, literal(@arguments(o))])).compile o
@assign_bind: ->
Scope.root.assign '__slice', UTILITIES['slice']
Scope.root.assign '__bind', UTILITIES['bind']
#### ExtendsNode
# Node to extend an object's prototype with an ancestor object.
@@ -447,7 +452,7 @@ exports.ExtendsNode: class ExtendsNode extends BaseNode
# Hooks one constructor into another's prototype chain.
compile_node: (o) ->
ref: new ValueNode literal(o.scope.utility('extend'))
ref: new ValueNode literal(Scope.root.assign('__extend', UTILITIES['extend']))
(new CallNode ref, [@child, @parent]).compile o
#### AccessorNode
@@ -548,7 +553,7 @@ exports.SliceNode: class SliceNode extends BaseNode
to: if @range.to? then @range.to else literal('null')
exclusive: if @range.exclusive then 'true' else 'false'
v: o.scope.free_variable()
rng: new CallNode new ValueNode(literal(o.scope.utility('range'))), [literal(array), from, to, literal(exclusive)]
rng: new CallNode new ValueNode(literal(Scope.root.assign('__range', UTILITIES['range']))), [literal(array), from, to, literal(exclusive)]
args: literal "[($v = ${rng.compile(o)})[0], $v[1] - $v[0]].concat(${replace.compile(o)})"
call: new CallNode new ValueNode(literal(array), [literal('.splice.apply')]), [literal(array), args]
call.compile(o)
@@ -558,7 +563,7 @@ exports.SliceNode: class SliceNode extends BaseNode
from: if @range.from? then @range.from else literal('null')
to: if @range.to? then @range.to else literal('null')
exclusive: if @range.exclusive then 'true' else 'false'
rng: new CallNode new ValueNode(literal(o.scope.utility('range'))), [literal(array), from, to, literal(exclusive)]
rng: new CallNode new ValueNode(literal(Scope.root.assign('__range', UTILITIES['range']))), [literal(array), from, to, literal(exclusive)]
call: new CallNode new ValueNode(literal(array), [literal('.slice.apply')]), [literal(array), rng]
call.compile(o)
@@ -802,8 +807,8 @@ 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'))
CurryNode.assign_bind()
ref: new ValueNode literal('__bind')
(new CallNode ref, [literal(func), literal('this')]).compile o
top_sensitive: ->
@@ -847,13 +852,14 @@ 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('slice')}.call(arguments, $@index, arguments.length - ${@trailings.length})"
"$name = ${Scope.root.assign('__slice', UTILITIES['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('slice')}.call($name, $index, ${name}.length - $trailings)" \
else "${o.scope.utility('slice')}.call($name, $index)"
Scope.root.assign '__slice', UTILITIES['slice']
trail: if trailings then ", ${name}.length - $trailings" else ''
"__slice.call($name, $index$trail)"
# Utility function that converts arbitrary number of elements, mixed with
# splats, to a proper array
@@ -1172,7 +1178,7 @@ exports.ForNode: class ForNode extends BaseNode
if @filter
body: Expressions.wrap([new IfNode(@filter, body)])
if @object
for_part: "$ivar in $svar) { if (${o.scope.utility('hasProp')}.call($svar, $ivar)"
for_part: "$ivar in $svar) { if (${Scope.root.assign('__hasProp', UTILITIES['hasProp'])}.call($svar, $ivar)"
body: body.compile(merge(o, {indent: body_dent, top: true}))
vars: if range then name else "$name, $ivar"
close: if @object then '}}\n' else '}\n'
@@ -1322,6 +1328,44 @@ ClosureNode: exports.ClosureNode: {
}
# Utility Functions
# -----------------
UTILITIES: {
extend: """
function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
child.__superClass__ = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
}
"""
bind: """
function(func, obj, args) {
return function() {
return func.apply(obj || {}, args ? args.concat(__slice.call(arguments, 0)) : arguments);
};
}
"""
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'
}
# Constants
# ---------