Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
hoist function globals (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 30, 2015
1 parent a2d1ed7 commit d481f21
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions compilers/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function GlobalTransformer(name, deps, exportName, globals) {
this.deps = deps;
this.exportName = exportName;
this.varGlobals = [];
this.fnGlobals = [];
this.globals = globals;
this.inOuterScope = true;
return ParseTreeTransformer.call(this);
Expand Down Expand Up @@ -49,7 +50,7 @@ GlobalTransformer.prototype.exitScope = function(revert) {
GlobalTransformer.prototype.transformFunctionDeclaration = function(tree) {
// named functions in outer scope are globals
if (this.inOuterScope && tree.name)
this.varGlobals.push(tree.name.identifierToken.value);
this.fnGlobals.push(tree.name.identifierToken.value);
var revert = this.enterScope();
tree = ParseTreeTransformer.prototype.transformFunctionDeclaration.call(this, tree);
this.exitScope(revert);
Expand All @@ -66,10 +67,15 @@ GlobalTransformer.prototype.transformFunctionExpression = function(tree) {
GlobalTransformer.prototype.transformScript = function(tree) {
tree = ParseTreeTransformer.prototype.transformScript.call(this, tree);

// hoist function declaration assignments to the global
var scriptItemList = this.fnGlobals.map(function(g) {
return parseStatement(['this["' + g + '"] = ' + g + ';']);
})
// for globals defined as "var x = 5;" in outer scope, add "this.x = x;" at end
var scriptItemList = this.varGlobals.map(function(g) {
.concat(this.varGlobals.map(function(g) {
return parseStatement(['var ' + g + ' = this["' + g + '"];']);
}).concat(tree.scriptItemList).concat(this.varGlobals.map(function(g) {
}))
.concat(tree.scriptItemList).concat(this.varGlobals.map(function(g) {
return parseStatement(['this["' + g + '"] = ' + g + ';']);
}));

Expand Down

0 comments on commit d481f21

Please sign in to comment.