From 0d7d4918eb6fb73c3cf9863479b3e66d38fad6df Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 26 Jan 2017 19:14:18 +0800 Subject: 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 --- lib/scope.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'lib/scope.js') 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(); -- cgit v1.2.3