aboutsummaryrefslogtreecommitdiff
path: root/lib/scope.js
diff options
context:
space:
mode:
authoralexlamsl <alexlamsl@gmail.com>2017-02-18 19:27:31 +0800
committeralexlamsl <alexlamsl@gmail.com>2017-02-21 13:29:58 +0800
commite275148998638bdcf795257ed03941ca34e33018 (patch)
treec9019dec5f4a26a2ecfa47161cada4deff1c1bf7 /lib/scope.js
parent974247c8c0e57901ef776e86784c8c9a1b87b5de (diff)
downloadtracifyjs-e275148998638bdcf795257ed03941ca34e33018.tar.gz
tracifyjs-e275148998638bdcf795257ed03941ca34e33018.zip
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
Diffstat (limited to 'lib/scope.js')
-rw-r--r--lib/scope.js22
1 files changed, 14 insertions, 8 deletions
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)