Skip to content

Commit

Permalink
Fix gensym leak at toplevel
Browse files Browse the repository at this point in the history
This closes #251.
  • Loading branch information
ef4 committed May 28, 2015
1 parent 1de9cf2 commit 09d8f13
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/cli.js

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

33 changes: 20 additions & 13 deletions lib/compiler.js

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

2 changes: 1 addition & 1 deletion lib/helpers.js

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

2 changes: 1 addition & 1 deletion lib/js-nodes.js

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

2 changes: 1 addition & 1 deletion lib/nodes.js

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

2 changes: 1 addition & 1 deletion lib/optimiser.js

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

25 changes: 16 additions & 9 deletions src/compiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,9 @@ class exports.Compiler
[].unshift.apply block, otherHelpers

decls = nub concatMap block, declarationsNeededRecursive
if decls.length > 0
if options.bare
block.unshift makeVarDeclaration decls.map (name) -> new JS.Identifier(name)
else
# add a function wrapper
block = [stmt new JS.UnaryExpression 'void', new JS.CallExpression (memberAccess (new JS.FunctionExpression null, [], new JS.BlockStatement block), 'call'), [new JS.ThisExpression]]
if decls.length and not options.bare
# add a function wrapper
block = [stmt new JS.UnaryExpression 'void', new JS.CallExpression (memberAccess (new JS.FunctionExpression null, [], new JS.BlockStatement block), 'call'), [new JS.ThisExpression]]
# generate node
pkg = require './../package.json'
program = new JS.Program block
Expand Down Expand Up @@ -1094,7 +1091,7 @@ class exports.Compiler
generatedSymbols[node.uniqueId] = formatted

# TODO: comments
generateMutatingWalker (state) ->
generateChildSymbols = generateMutatingWalker (state) ->
state.declaredSymbols = union state.declaredSymbols, declarationsNeeded this
{declaredSymbols, usedSymbols, nsCounters} = state
newNode = if @instanceof JS.GenSym
Expand All @@ -1105,7 +1102,7 @@ class exports.Compiler
params = concatMap @params, collectIdentifiers
nsCounters_ = {}
nsCounters_[k] = v for own k, v of nsCounters
newNode = generateSymbols this,
newNode = generateChildSymbols this,
declaredSymbols: union declaredSymbols, params
usedSymbols: union usedSymbols, params
nsCounters: nsCounters_
Expand All @@ -1117,10 +1114,19 @@ class exports.Compiler
decls = map declNames, (name) -> new JS.Identifier name
newNode.body.body.unshift makeVarDeclaration decls if decls.length > 0
newNode
else generateSymbols this, state
else generateChildSymbols this, state
state.declaredSymbols = union declaredSymbols, declarationsNeededRecursive newNode
newNode

(jsAST, state) ->
inScope = (state.declaredSymbols ? []).slice()
program = generateChildSymbols(jsAST, state)
if program.instanceof JS.Program
needed = nub difference (concatMap program.body, declarationsNeededRecursive), inScope
if needed.length > 0
program.body.unshift makeVarDeclaration needed.map((n) -> new JS.Identifier(n))
program

defaultRule = ->
throw new Error "compile: Non-exhaustive patterns in case: #{@className}"

Expand All @@ -1133,3 +1139,4 @@ class exports.Compiler
declaredSymbols: inScope
usedSymbols: union jsReserved[..], collectIdentifiers jsAST
nsCounters: {}

0 comments on commit 09d8f13

Please sign in to comment.