Permalink
Browse files

If we use a global inside a closure in a function, make sure to decla…

…re the winxed var in the outer scope so it doesn't get lost. If it's a closure at the top-level, declare the global in the closure like normal
  • Loading branch information...
1 parent bf96751 commit 4bdee6596351b6415ec016b162cdc986120ccd53 @Whiteknight committed Apr 1, 2012
Showing with 9 additions and 3 deletions.
  1. +0 −1 js2wxst0.js
  2. +9 −2 stage0/js/wast.js
View
@@ -4,7 +4,6 @@ var argv = process.argv;
var node_exe = argv.shift();
var js2wx_exe = argv.shift().split("/").pop();
-
// Stage 0 Driver Program
// This function runs the stage 0 compiler. It parses JavaScript input to an
// AST and eventually outputs Winxed code.
View
@@ -168,6 +168,7 @@ def(wast, "MainFunctionDecl", {
var fwd_fetch = "";
for (var g in st_globals) {
+ //sys.puts("Saw global " + g + " locally");
if (g in st.globals_seen_locally)
fwd_fetch += emitter.emit("var " + g + " = __fetch_global('" + g + "');\n");
else
@@ -233,8 +234,12 @@ def(wast, "ClosureDecl", {
wx += emitter.emit("function (" + this.args.map(function(a) { return a.toWinxed(st); }).join(", ") + ") {\n");
emitter.increase_indent();
var stmts = this.children.map(function(c) { return emitter.emit(c.toWinxed(st)) + ";\n"; }).join("");
- for (var gsl in st.globals_seen_locally)
- wx += emitter.emit("var " + gsl + " = __fetch_global('" + gsl + "');\n");
+ for (var gsl in st.globals_seen_locally) {
+ if (st.parent.declare_vars_locally == true) // This is a function
+ st.parent.seeGlobalLocally(gsl);
+ else
+ wx += emitter.emit("var " + gsl + " = __fetch_global('" + gsl + "');\n");
+ }
wx += stmts;
emitter.decrease_indent();
wx += emitter.emit("}\n");
@@ -281,6 +286,7 @@ def(wast, "VariableDeclare", {
setInitializer : function(i) { this.initializer = i; },
toWinxed : function(st) {
var n = this.name.name;
+ //sys.puts("Declaring variable " + n + " + " + st.declare_vars_locally);
if (n == undefined)
n = this.name.value;
var wx = "";
@@ -310,6 +316,7 @@ def(wast, "VariableName", {
toWinxed : function(st) {
var n = this.name.toString();
var locale = st.findSymbol(n);
+ //sys.puts("Using variable " + n + " " + locale);
if (locale == null) {
st.addGlobal(n);
st.seeGlobalLocally(n);

0 comments on commit 4bdee65

Please sign in to comment.