aboutsummaryrefslogtreecommitdiff
path: root/lib/scope.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-01-26 19:14:18 +0800
committerRichard van Velzen <rvanvelzen1@gmail.com>2017-01-26 12:14:18 +0100
commit0d7d4918eb6fb73c3cf9863479b3e66d38fad6df (patch)
treeaba146adb5371e19935493b132fc605abbe081ca /lib/scope.js
parent48284844a461e6113bb9911cdcdad7ab8a3d85de (diff)
downloadtracifyjs-0d7d4918eb6fb73c3cf9863479b3e66d38fad6df.tar.gz
tracifyjs-0d7d4918eb6fb73c3cf9863479b3e66d38fad6df.zip
augment evaluate to extract within objects (#1425)
- gated by `unsafe` - replaces previous optimisation specific to String.length - "123"[0] => 1 - [1, 2, 3][0] => 1 - [1, 2, 3].length => 3 - does not apply to objects with overridden prototype functions
Diffstat (limited to 'lib/scope.js')
-rw-r--r--lib/scope.js17
1 files changed, 13 insertions, 4 deletions
diff --git a/lib/scope.js b/lib/scope.js
index bc5db90d..ae792a0a 100644
--- a/lib/scope.js
+++ b/lib/scope.js
@@ -184,6 +184,17 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
var func = null;
var globals = self.globals = new Dictionary();
var tw = new TreeWalker(function(node, descend){
+ function isModified(node, level) {
+ var parent = tw.parent(level);
+ if (parent instanceof AST_Unary && (parent.operator === "++" || parent.operator === "--")
+ || parent instanceof AST_Assign && parent.left === node
+ || parent instanceof AST_Call && parent.expression === node) {
+ return true;
+ } else if (parent instanceof AST_PropAccess && parent.expression === node) {
+ return isModified(parent, level + 1);
+ }
+ }
+
if (node instanceof AST_Lambda) {
var prev_func = func;
func = node;
@@ -197,8 +208,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
}
if (node instanceof AST_SymbolRef) {
var name = node.name;
- var parent = tw.parent();
- if (name == "eval" && parent instanceof AST_Call) {
+ if (name == "eval" && tw.parent() instanceof AST_Call) {
for (var s = node.scope; s && !s.uses_eval; s = s.parent_scope) {
s.uses_eval = true;
}
@@ -220,8 +230,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
sym = g;
}
node.thedef = sym;
- if (parent instanceof AST_Unary && (parent.operator === "++" || parent.operator === "--")
- || parent instanceof AST_Assign && parent.left === node) {
+ if (isModified(node, 0)) {
sym.modified = true;
}
node.reference();