aboutsummaryrefslogtreecommitdiff
path: root/lib/scope.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2018-01-03 17:18:38 +0800
committerGitHub <noreply@github.com>2018-01-03 17:18:38 +0800
commit14778e049b12e131fc05ddacff9cda56dfede77d (patch)
tree61364a11fba69abf8f1362c30937361c071aa157 /lib/scope.js
parent446fb0198bd737c8d34035cc40932ed24ca83bbb (diff)
downloadtracifyjs-14778e049b12e131fc05ddacff9cda56dfede77d.tar.gz
tracifyjs-14778e049b12e131fc05ddacff9cda56dfede77d.zip
fix `reduce_vars` on `AST_Defun` (#2708)
Diffstat (limited to 'lib/scope.js')
-rw-r--r--lib/scope.js25
1 files changed, 13 insertions, 12 deletions
diff --git a/lib/scope.js b/lib/scope.js
index bceec289..79b2475d 100644
--- a/lib/scope.js
+++ b/lib/scope.js
@@ -43,9 +43,10 @@
"use strict";
-function SymbolDef(scope, orig) {
+function SymbolDef(scope, orig, init) {
this.name = orig.name;
this.orig = [ orig ];
+ this.init = init;
this.eliminated = 0;
this.scope = scope;
this.references = [];
@@ -158,10 +159,10 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
// scope when we encounter the AST_Defun node (which is
// instanceof AST_Scope) but we get to the symbol a bit
// later.
- (node.scope = defun.parent_scope).def_function(node);
+ (node.scope = defun.parent_scope).def_function(node, defun);
}
else if (node instanceof AST_SymbolVar) {
- defun.def_variable(node);
+ defun.def_variable(node, node.TYPE == "SymbolVar" ? null : undefined);
if (defun !== scope) {
node.mark_enclosed(options);
var def = scope.find_variable(node);
@@ -306,21 +307,21 @@ AST_Scope.DEFMETHOD("find_variable", function(name){
|| (this.parent_scope && this.parent_scope.find_variable(name));
});
-AST_Scope.DEFMETHOD("def_function", function(symbol){
- var def = this.def_variable(symbol);
+AST_Scope.DEFMETHOD("def_function", function(symbol, init){
+ var def = this.def_variable(symbol, init);
+ if (!def.init) def.init = init;
this.functions.set(symbol.name, def);
return def;
});
-AST_Scope.DEFMETHOD("def_variable", function(symbol){
- var def;
- if (!this.variables.has(symbol.name)) {
- def = new SymbolDef(this, symbol);
+AST_Scope.DEFMETHOD("def_variable", function(symbol, init){
+ var def = this.variables.get(symbol.name);
+ if (def) {
+ def.orig.push(symbol);
+ } else {
+ def = new SymbolDef(this, symbol, init);
this.variables.set(symbol.name, def);
def.global = !this.parent_scope;
- } else {
- def = this.variables.get(symbol.name);
- def.orig.push(symbol);
}
return symbol.thedef = def;
});