From e275148998638bdcf795257ed03941ca34e33018 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sat, 18 Feb 2017 19:27:31 +0800 Subject: enhance `global_defs` - support arrays, objects & AST_Node - support `"a.b":1` on both cli & API - emit warning if variable is modified - override top-level variables fixes #1416 closes #1198 closes #1469 --- lib/scope.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'lib/scope.js') diff --git a/lib/scope.js b/lib/scope.js index 6ad12616..b0d92d7d 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -206,14 +206,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ node.scope.uses_arguments = true; } if (!sym) { - if (globals.has(name)) { - sym = globals.get(name); - } else { - sym = new SymbolDef(self, globals.size(), node); - sym.undeclared = true; - sym.global = true; - globals.set(name, sym); - } + sym = self.def_global(node); } node.thedef = sym; node.reference(options); @@ -227,6 +220,19 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ } }); +AST_Toplevel.DEFMETHOD("def_global", function(node){ + var globals = this.globals, name = node.name; + if (globals.has(name)) { + return globals.get(name); + } else { + var g = new SymbolDef(this, globals.size(), node); + g.undeclared = true; + g.global = true; + globals.set(name, g); + return g; + } +}); + AST_Scope.DEFMETHOD("init_scope_vars", function(){ this.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions) this.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope) -- cgit v1.2.3