Started to update the closure extractor to work with Rhino 1.7.3.
git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@4438 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
parent
1e0ad9dbc6
commit
174a30454c
|
@ -172,6 +172,8 @@ import com.ibm.wala.util.debug.UnimplementedError;
|
|||
*
|
||||
*/
|
||||
public class ClosureExtractor extends CAstRewriterExt {
|
||||
private static final boolean RHINO_1_7_2 = false;
|
||||
|
||||
private LinkedList<ExtractionPolicy> policies = new LinkedList<ExtractionPolicy>();
|
||||
private final ExtractionPolicyFactory policyFactory;
|
||||
|
||||
|
@ -388,6 +390,8 @@ public class ClosureExtractor extends CAstRewriterExt {
|
|||
}
|
||||
|
||||
private int anonymous_counter = 0;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private List<CAstNode> extractRegion(CAstNode root, CAstControlFlowMap cfg, ExtractionPos context, Map<Pair<CAstNode, NoKey>, CAstNode> nodeMap) {
|
||||
CAstEntity entity = getCurrentEntity();
|
||||
|
||||
|
@ -412,26 +416,28 @@ public class ClosureExtractor extends CAstRewriterExt {
|
|||
*/
|
||||
ArrayList<CAstNode> prologue = new ArrayList<CAstNode>();
|
||||
ArrayList<CAstNode> fun_body_stmts = new ArrayList<CAstNode>();
|
||||
CAstNode self_ref = makeVarRef(name);
|
||||
CAstNode self_assign = Ast.makeNode(ASSIGN, self_ref, makeVarRef(name));
|
||||
addFlow(self_ref, JavaScriptTypes.ReferenceError, CAstControlFlowMap.EXCEPTION_TO_EXIT, entity.getControlFlow());
|
||||
fun_body_stmts.add(self_assign);
|
||||
if(RHINO_1_7_2) {
|
||||
CAstNode self_ref = makeVarRef(name);
|
||||
CAstNode self_assign = Ast.makeNode(ASSIGN, self_ref, makeVarRef(name));
|
||||
addFlow(self_ref, JavaScriptTypes.ReferenceError, CAstControlFlowMap.EXCEPTION_TO_EXIT, entity.getControlFlow());
|
||||
fun_body_stmts.add(self_assign);
|
||||
}
|
||||
|
||||
// if we are extracting a block, unwrap it
|
||||
if(extractingBlock) {
|
||||
CAstNode block = root.getChild(context.getStart());
|
||||
for(int i=0;i<block.getChildCount();++i)
|
||||
fun_body_stmts.add(block.getChild(i));
|
||||
if(mayCompleteNormally(block))
|
||||
if(RHINO_1_7_2 && mayCompleteNormally(block))
|
||||
fun_body_stmts.add(markSynthetic(Ast.makeNode(RETURN)));
|
||||
} else {
|
||||
if(context.getRegion() instanceof TwoLevelExtractionRegion) {
|
||||
CAstNode start = root.getChild(context.getStart());
|
||||
TwoLevelExtractionRegion tler = (TwoLevelExtractionRegion)context.getRegion();
|
||||
if(start.getKind() != CAstNode.BLOCK_EXPR)
|
||||
throw new IllegalArgumentException("Invalid two-level extraction region.");
|
||||
if(tler.getEndInner() != -1)
|
||||
throw new UnimplementedError("Two-level extraction not fully implemented.");
|
||||
if(start.getKind() != CAstNode.BLOCK_EXPR)
|
||||
throw new IllegalArgumentException("Invalid two-level extraction region.");
|
||||
int i;
|
||||
for(i=0;i<tler.getStartInner();++i)
|
||||
prologue.add(copyNodes(start.getChild(i), cfg, context, nodeMap));
|
||||
|
@ -449,11 +455,11 @@ public class ClosureExtractor extends CAstRewriterExt {
|
|||
fun_body_stmts.add(root.getChild(context.getStart()));
|
||||
}
|
||||
}
|
||||
if(mayCompleteNormally(root.getChild(context.getEnd()-1)))
|
||||
if(RHINO_1_7_2 && mayCompleteNormally(root.getChild(context.getEnd()-1)))
|
||||
fun_body_stmts.add(markSynthetic(Ast.makeNode(RETURN)));
|
||||
}
|
||||
CAstNode inner_block = Ast.makeNode(BLOCK_STMT, fun_body_stmts.toArray(new CAstNode[0]));
|
||||
CAstNode fun_body = Ast.makeNode(BLOCK_STMT, inner_block);
|
||||
CAstNode fun_body = RHINO_1_7_2 ? Ast.makeNode(BLOCK_STMT, inner_block) : inner_block;
|
||||
|
||||
/*
|
||||
* Now we rewrite the body and construct a Rewrite object.
|
||||
|
|
|
@ -13,7 +13,9 @@ package com.ibm.wala.cast.js.ipa.callgraph.correlations.extraction;
|
|||
|
||||
import static com.ibm.wala.cast.tree.CAstNode.ASSIGN;
|
||||
import static com.ibm.wala.cast.tree.CAstNode.BLOCK_STMT;
|
||||
import static com.ibm.wala.cast.tree.CAstNode.DECL_STMT;
|
||||
import static com.ibm.wala.cast.tree.CAstNode.EACH_ELEMENT_GET;
|
||||
import static com.ibm.wala.cast.tree.CAstNode.LABEL_STMT;
|
||||
import static com.ibm.wala.cast.tree.CAstNode.VAR;
|
||||
|
||||
import java.util.Collections;
|
||||
|
@ -21,7 +23,11 @@ import java.util.List;
|
|||
|
||||
import com.ibm.wala.cast.tree.CAstEntity;
|
||||
import com.ibm.wala.cast.tree.CAstNode;
|
||||
import com.ibm.wala.cast.tree.pattern.*;
|
||||
import com.ibm.wala.cast.tree.pattern.Alt;
|
||||
import com.ibm.wala.cast.tree.pattern.AnyNode;
|
||||
import com.ibm.wala.cast.tree.pattern.NodeOfKind;
|
||||
import com.ibm.wala.cast.tree.pattern.SomeConstant;
|
||||
import com.ibm.wala.cast.tree.pattern.SubtreeOfKind;
|
||||
|
||||
/**
|
||||
* A policy telling a {@link ClosureExtractor} to extract the body of every for-in loop.
|
||||
|
@ -47,7 +53,27 @@ public class ForInBodyExtractionPolicy extends ExtractionPolicy {
|
|||
@Override
|
||||
public List<ExtractionRegion> extract(CAstNode node) {
|
||||
SomeConstant loopVar = new SomeConstant();
|
||||
/* matches the following pattern:
|
||||
|
||||
/*
|
||||
* matches Rhino 1.7.3 encoding of for-in loop bodies:
|
||||
*
|
||||
* BLOCK_STMT
|
||||
* decl/assign of loop variable
|
||||
* <loopBody>
|
||||
*/
|
||||
if(new NodeOfKind(BLOCK_STMT,
|
||||
new Alt(new NodeOfKind(DECL_STMT,
|
||||
loopVar,
|
||||
new SubtreeOfKind(EACH_ELEMENT_GET)),
|
||||
new NodeOfKind(ASSIGN,
|
||||
new NodeOfKind(VAR, loopVar),
|
||||
new SubtreeOfKind(EACH_ELEMENT_GET))),
|
||||
new AnyNode(),
|
||||
new SubtreeOfKind(LABEL_STMT)).matches(node)) {
|
||||
return Collections.singletonList(new ExtractionRegion(1, 2, Collections.singletonList((String)loopVar.getLastMatch())));
|
||||
}
|
||||
|
||||
/* matches Rhino < 1.7.3 encoding of for-in loop bodies:
|
||||
*
|
||||
* BLOCK_STMT
|
||||
* ASSIGN
|
||||
|
@ -55,8 +81,6 @@ public class ForInBodyExtractionPolicy extends ExtractionPolicy {
|
|||
* EACH_ELEMENT_GET
|
||||
* VAR <forin_tmp>
|
||||
* <loopBody>
|
||||
*
|
||||
* TODO: this is too brittle; what if future versions of Rhino encode for-in loops differently?
|
||||
*/
|
||||
if(new NodeOfKind(BLOCK_STMT,
|
||||
new NodeOfKind(ASSIGN,
|
||||
|
|
Loading…
Reference in New Issue