Skip to content

Commit

Permalink
fixes #1409: creating large ranges outside of a function body
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelficarra committed Jun 2, 2011
1 parent e240621 commit 22bc54f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
26 changes: 17 additions & 9 deletions lib/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 17 additions & 11 deletions src/nodes.coffee
Expand Up @@ -633,8 +633,8 @@ exports.Range = class Range extends Base
# But only if they need to be cached to avoid double evaluation.
compileVariables: (o) ->
o = merge o, top: true
[@from, @fromVar] = @from.cache o, LEVEL_LIST
[@to, @toVar] = @to.cache o, LEVEL_LIST
[@fromC, @fromVar] = @from.cache o, LEVEL_LIST
[@toC, @toVar] = @to.cache o, LEVEL_LIST
[@step, @stepVar] = step.cache o, LEVEL_LIST if step = del o, 'step'
[@fromNum, @toNum] = [@fromVar.match(SIMPLENUM), @toVar.match(SIMPLENUM)]
@stepNum = @stepVar.match(SIMPLENUM) if @stepVar
Expand All @@ -644,15 +644,15 @@ exports.Range = class Range extends Base
compileNode: (o) ->
@compileVariables o unless @fromVar
return @compileArray(o) unless o.index

# Set up endpoints.
known = @fromNum and @toNum
idx = del o, 'index'
varPart = "#{idx} = #{@from}"
varPart += ", #{@to}" if @to isnt @toVar
varPart = "#{idx} = #{@fromC}"
varPart += ", #{@toC}" if @toC isnt @toVar
varPart += ", #{@step}" if @step isnt @stepVar
[lt, gt] = ["#{idx} <#{@equals}", "#{idx} >#{@equals}"]

# Generate the condition.
condPart = if @stepNum
condPart = if +@stepNum > 0 then "#{lt} #{@toVar}" else "#{gt} #{@toVar}"
Expand All @@ -662,18 +662,18 @@ exports.Range = class Range extends Base
else
cond = "#{@fromVar} <= #{@toVar}"
condPart = "#{cond} ? #{lt} #{@toVar} : #{gt} #{@toVar}"

# Generate the step.
stepPart = if @stepVar
"#{idx} += #{@stepVar}"
else if known
if from <= to then "#{idx}++" else "#{idx}--"
else
"#{cond} ? #{idx}++ : #{idx}--"

# The final loop body.
"#{varPart}; #{condPart}; #{stepPart}"


# When used as a value, expand the range into the equivalent array.
compileArray: (o) ->
Expand All @@ -689,11 +689,17 @@ exports.Range = class Range extends Base
o.index = i
body = @compileNode o
else
vars = "#{i} = #{@from}" + if @to isnt @toVar then ", #{@to}" else ''
vars = "#{i} = #{@fromC}" + if @toC isnt @toVar then ", #{@toC}" else ''
cond = "#{@fromVar} <= #{@toVar}"
body = "var #{vars}; #{cond} ? #{i} <#{@equals} #{@toVar} : #{i} >#{@equals} #{@toVar}; #{cond} ? #{i}++ : #{i}--"
post = "{ #{result}.push(#{i}); }\n#{idt}return #{result};\n#{o.indent}"
"(function() {#{pre}\n#{idt}for (#{body})#{post}}).apply(this, arguments)"
hasArgs = (node) ->
node?.contains (n) ->
n instanceof Literal and
n.value is 'arguments' and
not n.asKey
args = ', arguments' if hasArgs(@from) or hasArgs(@to) or hasArgs(@step)
"(function() {#{pre}\n#{idt}for (#{body})#{post}}).apply(this#{args ? ''})"

#### Slice

Expand Down

2 comments on commit 22bc54f

@jashkenas
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is compiling as an array, there should never be a step (SyntaxError).

@michaelficarra
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed... er, changed: 8ce1fdb

Please sign in to comment.