本文整理汇总了Java中jdk.nashorn.internal.ir.Block.accept方法的典型用法代码示例。如果您正苦于以下问题:Java Block.accept方法的具体用法?Java Block.accept怎么用?Java Block.accept使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jdk.nashorn.internal.ir.Block
的用法示例。
在下文中一共展示了Block.accept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: enterTryNode
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
@Override
public boolean enterTryNode(final TryNode tryNode) {
tryNode.toString(sb, printTypes);
printLocalVariableConversion(tryNode);
tryNode.getBody().accept(this);
final List<Block> catchBlocks = tryNode.getCatchBlocks();
for (final Block catchBlock : catchBlocks) {
final CatchNode catchNode = (CatchNode)catchBlock.getStatements().get(0);
catchNode.toString(sb, printTypes);
catchNode.getBody().accept(this);
}
final Block finallyBody = tryNode.getFinallyBody();
if (finallyBody != null) {
sb.append(" finally ");
finallyBody.accept(this);
}
return false;
}
示例2: enterTryNode
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
@Override
public boolean enterTryNode(final TryNode tryNode) {
tryNode.toString(sb, printTypes);
printLocalVariableConversion(tryNode);
tryNode.getBody().accept(this);
final List<Block> catchBlocks = tryNode.getCatchBlocks();
for (final Block catchBlock : catchBlocks) {
final CatchNode catchNode = (CatchNode)catchBlock.getStatements().get(0);
catchNode.toString(sb, printTypes);
catchNode.getBody().accept(this);
}
final Block finallyBody = tryNode.getFinallyBody();
if (finallyBody != null) {
sb.append(" finally ");
finallyBody.accept(this);
}
for (final Block inlinedFinally : tryNode.getInlinedFinallies()) {
inlinedFinally.accept(this);
}
return false;
}
示例3: acceptDeclarations
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
/**
* Define symbols for all variable declarations at the top of the function scope. This way we can get around
* problems like
*
* while (true) {
* break;
* if (true) {
* var s;
* }
* }
*
* to an arbitrary nesting depth.
*
* see NASHORN-73
*
* @param functionNode the FunctionNode we are entering
* @param body the body of the FunctionNode we are entering
*/
private void acceptDeclarations(final FunctionNode functionNode, final Block body) {
// This visitor will assign symbol to all declared variables.
body.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {
@Override
protected boolean enterDefault(final Node node) {
// Don't bother visiting expressions; var is a statement, it can't be inside an expression.
// This will also prevent visiting nested functions (as FunctionNode is an expression).
return !(node instanceof Expression);
}
@Override
public Node leaveVarNode(final VarNode varNode) {
final IdentNode ident = varNode.getName();
final boolean blockScoped = varNode.isBlockScoped();
if (blockScoped && lc.inUnprotectedSwitchContext()) {
throwUnprotectedSwitchError(varNode);
}
final Block block = blockScoped ? lc.getCurrentBlock() : body;
final Symbol symbol = defineSymbol(block, ident.getName(), ident, varNode.getSymbolFlags());
if (varNode.isFunctionDeclaration()) {
symbol.setIsFunctionDeclaration();
}
return varNode.setName(ident.setSymbol(symbol));
}
});
}
示例4: extractVarNodes
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
private static void extractVarNodes(final Block block, final List<Statement> statements) {
final LexicalContext lc = new LexicalContext();
block.accept(lc, new NodeVisitor<LexicalContext>(lc) {
@Override
public boolean enterVarNode(final VarNode varNode) {
statements.add(varNode.setInit(null));
return false;
}
});
}
示例5: enterDoWhileLoop
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
private void enterDoWhileLoop(final WhileNode loopNode) {
final JoinPredecessorExpression test = loopNode.getTest();
final Block body = loopNode.getBody();
final Label continueLabel = loopNode.getContinueLabel();
final Label breakLabel = loopNode.getBreakLabel();
final Map<Symbol, LvarType> beforeLoopTypes = localVariableTypes;
final Label repeatLabel = new Label("");
for(;;) {
jumpToLabel(loopNode, repeatLabel, beforeLoopTypes);
final Map<Symbol, LvarType> beforeRepeatTypes = localVariableTypes;
body.accept(this);
if(reachable) {
jumpToLabel(body, continueLabel);
}
joinOnLabel(continueLabel);
if(!reachable) {
break;
}
test.accept(this);
jumpToLabel(test, breakLabel);
if(isAlwaysFalse(test)) {
break;
}
jumpToLabel(test, repeatLabel);
joinOnLabel(repeatLabel);
if(localVariableTypes.equals(beforeRepeatTypes)) {
break;
}
resetJoinPoint(continueLabel);
resetJoinPoint(breakLabel);
resetJoinPoint(repeatLabel);
}
if(isAlwaysTrue(test)) {
doesNotContinueSequentially();
}
leaveBreakable(loopNode);
}
示例6: enterDoWhile
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
private void enterDoWhile(final WhileNode whileNode) {
final int liveLocalsOnContinueOrBreak = method.getUsedSlotsWithLiveTemporaries();
method.beforeJoinPoint(whileNode);
final Block body = whileNode.getBody();
body.accept(this);
emitContinueLabel(whileNode.getContinueLabel(), liveLocalsOnContinueOrBreak);
if(method.isReachable()) {
lineNumber(whileNode);
final JoinPredecessorExpression test = whileNode.getTest();
final Label bodyEntryLabel = body.getEntryLabel();
final boolean testHasLiveConversion = LocalVariableConversion.hasLiveConversion(test);
if(Expression.isAlwaysFalse(test)) {
loadAndDiscard(test);
if(testHasLiveConversion) {
method.beforeJoinPoint(test);
}
} else if(testHasLiveConversion) {
// If we have conversions after the test in do-while, they need to be effected on both branches.
final Label beforeExit = new Label("do_while_preexit");
emitBranch(test.getExpression(), beforeExit, false);
method.beforeJoinPoint(test);
method._goto(bodyEntryLabel);
method.label(beforeExit);
method.beforeJoinPoint(test);
} else {
emitBranch(test.getExpression(), bodyEntryLabel, true);
}
}
method.breakLabel(whileNode.getBreakLabel(), liveLocalsOnContinueOrBreak);
}
示例7: acceptDeclarations
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
/**
* Define symbols for all variable declarations at the top of the function scope. This way we can get around
* problems like
*
* while (true) {
* break;
* if (true) {
* var s;
* }
* }
*
* to an arbitrary nesting depth.
*
* see NASHORN-73
*
* @param functionNode the FunctionNode we are entering
* @param body the body of the FunctionNode we are entering
*/
private void acceptDeclarations(final FunctionNode functionNode, final Block body) {
// This visitor will assign symbol to all declared variables.
body.accept(new SimpleNodeVisitor() {
@Override
protected boolean enterDefault(final Node node) {
// Don't bother visiting expressions; var is a statement, it can't be inside an expression.
// This will also prevent visiting nested functions (as FunctionNode is an expression).
return !(node instanceof Expression);
}
@Override
public Node leaveVarNode(final VarNode varNode) {
final IdentNode ident = varNode.getName();
final boolean blockScoped = varNode.isBlockScoped();
if (blockScoped && lc.inUnprotectedSwitchContext()) {
throwUnprotectedSwitchError(varNode);
}
final Block block = blockScoped ? lc.getCurrentBlock() : body;
final Symbol symbol = defineSymbol(block, ident.getName(), ident, varNode.getSymbolFlags());
if (varNode.isFunctionDeclaration()) {
symbol.setIsFunctionDeclaration();
}
return varNode.setName(ident.setSymbol(symbol));
}
});
}
示例8: enterDoWhileLoop
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
private void enterDoWhileLoop(final WhileNode loopNode) {
assertTypeStackIsEmpty();
final JoinPredecessorExpression test = loopNode.getTest();
final Block body = loopNode.getBody();
final Label continueLabel = loopNode.getContinueLabel();
final Label breakLabel = loopNode.getBreakLabel();
final Map<Symbol, LvarType> beforeLoopTypes = localVariableTypes;
final Label repeatLabel = new Label("");
for(;;) {
jumpToLabel(loopNode, repeatLabel, beforeLoopTypes);
final Map<Symbol, LvarType> beforeRepeatTypes = localVariableTypes;
body.accept(this);
if(reachable) {
jumpToLabel(body, continueLabel);
}
joinOnLabel(continueLabel);
if(!reachable) {
break;
}
visitExpressionOnEmptyStack(test);
jumpToLabel(test, breakLabel);
if(isAlwaysFalse(test)) {
break;
}
jumpToLabel(test, repeatLabel);
joinOnLabel(repeatLabel);
if(localVariableTypes.equals(beforeRepeatTypes)) {
break;
}
resetJoinPoint(continueLabel);
resetJoinPoint(breakLabel);
resetJoinPoint(repeatLabel);
}
if(isAlwaysTrue(test)) {
doesNotContinueSequentially();
}
leaveBreakable(loopNode);
}
示例9: enterIfNode
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
@Override
public boolean enterIfNode(final IfNode ifNode) {
if(!reachable) {
return false;
}
final Expression test = ifNode.getTest();
final Block pass = ifNode.getPass();
final Block fail = ifNode.getFail();
test.accept(this);
final Map<Symbol, LvarType> afterTestLvarTypes = localVariableTypes;
if(!isAlwaysFalse(test)) {
pass.accept(this);
}
final Map<Symbol, LvarType> passLvarTypes = localVariableTypes;
final boolean reachableFromPass = reachable;
reachable = true;
localVariableTypes = afterTestLvarTypes;
if(!isAlwaysTrue(test) && fail != null) {
fail.accept(this);
final boolean reachableFromFail = reachable;
reachable |= reachableFromPass;
if(!reachable) {
return false;
}
if(reachableFromFail) {
if(reachableFromPass) {
final Map<Symbol, LvarType> failLvarTypes = localVariableTypes;
localVariableTypes = getUnionTypes(passLvarTypes, failLvarTypes);
setConversion(pass, passLvarTypes, localVariableTypes);
setConversion(fail, failLvarTypes, localVariableTypes);
}
return false;
}
}
if(reachableFromPass) {
localVariableTypes = getUnionTypes(afterTestLvarTypes, passLvarTypes);
// IfNode itself is associated with conversions that might need to be performed after the test if there's no
// else branch. E.g.
// if(x = 1, cond) { x = 1.0 } must widen "x = 1" to a double.
setConversion(pass, passLvarTypes, localVariableTypes);
setConversion(ifNode, afterTestLvarTypes, localVariableTypes);
} else {
localVariableTypes = afterTestLvarTypes;
}
return false;
}
示例10: enterTestFirstLoop
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
private void enterTestFirstLoop(final LoopNode loopNode, final JoinPredecessorExpression modify,
final Expression iteratorValues, final boolean iteratorValuesAreObject) {
final JoinPredecessorExpression test = loopNode.getTest();
if(isAlwaysFalse(test)) {
test.accept(this);
return;
}
final Label continueLabel = loopNode.getContinueLabel();
final Label breakLabel = loopNode.getBreakLabel();
final Label repeatLabel = modify == null ? continueLabel : new Label("");
final Map<Symbol, LvarType> beforeLoopTypes = localVariableTypes;
for(;;) {
jumpToLabel(loopNode, repeatLabel, beforeLoopTypes);
final Map<Symbol, LvarType> beforeRepeatTypes = localVariableTypes;
if(test != null) {
test.accept(this);
}
if(!isAlwaysTrue(test)) {
jumpToLabel(test, breakLabel);
}
if(iteratorValues instanceof IdentNode) {
final IdentNode ident = (IdentNode)iteratorValues;
// Receives iterator values; the optimistic type of the iterator values is tracked on the
// identifier, but we override optimism if it's known that the object being iterated over will
// never have primitive property names.
onAssignment(ident, iteratorValuesAreObject ? LvarType.OBJECT :
toLvarType(compiler.getOptimisticType(ident)));
}
final Block body = loopNode.getBody();
body.accept(this);
if(reachable) {
jumpToLabel(body, continueLabel);
}
joinOnLabel(continueLabel);
if(!reachable) {
break;
}
if(modify != null) {
modify.accept(this);
jumpToLabel(modify, repeatLabel);
joinOnLabel(repeatLabel);
}
if(localVariableTypes.equals(beforeRepeatTypes)) {
break;
}
// Reset the join points and repeat the analysis
resetJoinPoint(continueLabel);
resetJoinPoint(breakLabel);
resetJoinPoint(repeatLabel);
}
if(isAlwaysTrue(test) && iteratorValues == null) {
doesNotContinueSequentially();
}
leaveBreakable(loopNode);
}
示例11: enterTryNode
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
@Override
public boolean enterTryNode(final TryNode tryNode) {
if(!reachable) {
return false;
}
// This is the label for the join point at the entry of the catch blocks.
final Label catchLabel = new Label("");
catchLabels.push(catchLabel);
// Presume that even the start of the try block can immediately go to the catch
jumpToLabel(tryNode, catchLabel);
final Block body = tryNode.getBody();
body.accept(this);
catchLabels.pop();
// Final exit label for the whole try/catch construct (after the try block and after all catches).
final Label endLabel = new Label("");
boolean canExit = false;
if(reachable) {
jumpToLabel(body, endLabel);
canExit = true;
}
doesNotContinueSequentially();
joinOnLabel(catchLabel);
for(final CatchNode catchNode: tryNode.getCatches()) {
final IdentNode exception = catchNode.getException();
onAssignment(exception, LvarType.OBJECT);
final Expression condition = catchNode.getExceptionCondition();
if(condition != null) {
condition.accept(this);
}
final Map<Symbol, LvarType> afterConditionTypes = localVariableTypes;
final Block catchBody = catchNode.getBody();
// TODO: currently, we consider that the catch blocks are always reachable from the try block as currently
// we lack enough analysis to prove that no statement before a break/continue/return in the try block can
// throw an exception.
reachable = true;
catchBody.accept(this);
final Symbol exceptionSymbol = exception.getSymbol();
if(reachable) {
localVariableTypes = cloneMap(localVariableTypes);
localVariableTypes.remove(exceptionSymbol);
jumpToLabel(catchBody, endLabel);
canExit = true;
}
localVariableTypes = cloneMap(afterConditionTypes);
localVariableTypes.remove(exceptionSymbol);
}
// NOTE: if we had one or more conditional catch blocks with no unconditional catch block following them, then
// there will be an unconditional rethrow, so the join point can never be reached from the last
// conditionExpression.
doesNotContinueSequentially();
if(canExit) {
joinOnLabel(endLabel);
}
return false;
}
示例12: enterForIn
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
private void enterForIn(final ForNode forNode) {
loadExpression(forNode.getModify(), TypeBounds.OBJECT);
method.invoke(forNode.isForEach() ? ScriptRuntime.TO_VALUE_ITERATOR : ScriptRuntime.TO_PROPERTY_ITERATOR);
final Symbol iterSymbol = forNode.getIterator();
final int iterSlot = iterSymbol.getSlot(Type.OBJECT);
method.store(iterSymbol, ITERATOR_TYPE);
method.beforeJoinPoint(forNode);
final Label continueLabel = forNode.getContinueLabel();
final Label breakLabel = forNode.getBreakLabel();
method.label(continueLabel);
method.load(ITERATOR_TYPE, iterSlot);
method.invoke(interfaceCallNoLookup(ITERATOR_CLASS, "hasNext", boolean.class));
final JoinPredecessorExpression test = forNode.getTest();
final Block body = forNode.getBody();
if(LocalVariableConversion.hasLiveConversion(test)) {
final Label afterConversion = new Label("for_in_after_test_conv");
method.ifne(afterConversion);
method.beforeJoinPoint(test);
method._goto(breakLabel);
method.label(afterConversion);
} else {
method.ifeq(breakLabel);
}
new Store<Expression>(forNode.getInit()) {
@Override
protected void storeNonDiscard() {
// This expression is neither part of a discard, nor needs to be left on the stack after it was
// stored, so we override storeNonDiscard to be a no-op.
}
@Override
protected void evaluate() {
new OptimisticOperation((Optimistic)forNode.getInit(), TypeBounds.UNBOUNDED) {
@Override
void loadStack() {
method.load(ITERATOR_TYPE, iterSlot);
}
@Override
void consumeStack() {
method.invoke(interfaceCallNoLookup(ITERATOR_CLASS, "next", Object.class));
convertOptimisticReturnValue();
}
}.emit();
}
}.store();
body.accept(this);
if(method.isReachable()) {
method._goto(continueLabel);
}
method.label(breakLabel);
}
示例13: enterIfNode
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
@Override
public boolean enterIfNode(final IfNode ifNode) {
if(!method.isReachable()) {
return false;
}
enterStatement(ifNode);
final Expression test = ifNode.getTest();
final Block pass = ifNode.getPass();
final Block fail = ifNode.getFail();
if (Expression.isAlwaysTrue(test)) {
loadAndDiscard(test);
pass.accept(this);
return false;
} else if (Expression.isAlwaysFalse(test)) {
loadAndDiscard(test);
if (fail != null) {
fail.accept(this);
}
return false;
}
final boolean hasFailConversion = LocalVariableConversion.hasLiveConversion(ifNode);
final Label failLabel = new Label("if_fail");
final Label afterLabel = (fail == null && !hasFailConversion) ? null : new Label("if_done");
emitBranch(test, failLabel, false);
pass.accept(this);
if(method.isReachable() && afterLabel != null) {
method._goto(afterLabel); //don't fallthru to fail block
}
method.label(failLabel);
if (fail != null) {
fail.accept(this);
} else if(hasFailConversion) {
method.beforeJoinPoint(ifNode);
}
if(afterLabel != null && afterLabel.isReachable()) {
method.label(afterLabel);
}
return false;
}
示例14: enterForOrWhile
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
private void enterForOrWhile(final LoopNode loopNode, final JoinPredecessorExpression modify) {
// NOTE: the usual pattern for compiling test-first loops is "GOTO test; body; test; IFNE body". We use the less
// conventional "test; IFEQ break; body; GOTO test; break;". It has one extra unconditional GOTO in each repeat
// of the loop, but it's not a problem for modern JIT compilers. We do this because our local variable type
// tracking is unfortunately not really prepared for out-of-order execution, e.g. compiling the following
// contrived but legal JavaScript code snippet would fail because the test changes the type of "i" from object
// to double: var i = {valueOf: function() { return 1} }; while(--i >= 0) { ... }
// Instead of adding more complexity to the local variable type tracking, we instead choose to emit this
// different code shape.
final int liveLocalsOnBreak = method.getUsedSlotsWithLiveTemporaries();
final JoinPredecessorExpression test = loopNode.getTest();
if(Expression.isAlwaysFalse(test)) {
loadAndDiscard(test);
return;
}
method.beforeJoinPoint(loopNode);
final Label continueLabel = loopNode.getContinueLabel();
final Label repeatLabel = modify != null ? new Label("for_repeat") : continueLabel;
method.label(repeatLabel);
final int liveLocalsOnContinue = method.getUsedSlotsWithLiveTemporaries();
final Block body = loopNode.getBody();
final Label breakLabel = loopNode.getBreakLabel();
final boolean testHasLiveConversion = test != null && LocalVariableConversion.hasLiveConversion(test);
if(Expression.isAlwaysTrue(test)) {
if(test != null) {
loadAndDiscard(test);
if(testHasLiveConversion) {
method.beforeJoinPoint(test);
}
}
} else if (test != null) {
if (testHasLiveConversion) {
emitBranch(test.getExpression(), body.getEntryLabel(), true);
method.beforeJoinPoint(test);
method._goto(breakLabel);
} else {
emitBranch(test.getExpression(), breakLabel, false);
}
}
body.accept(this);
if(repeatLabel != continueLabel) {
emitContinueLabel(continueLabel, liveLocalsOnContinue);
}
if (loopNode.hasPerIterationScope() && lc.getCurrentBlock().needsScope()) {
// ES6 for loops with LET init need a new scope for each iteration. We just create a shallow copy here.
method.loadCompilerConstant(SCOPE);
method.invoke(virtualCallNoLookup(ScriptObject.class, "copy", ScriptObject.class));
method.storeCompilerConstant(SCOPE);
}
if(method.isReachable()) {
if(modify != null) {
lineNumber(loopNode);
loadAndDiscard(modify);
method.beforeJoinPoint(modify);
}
method._goto(repeatLabel);
}
method.breakLabel(breakLabel, liveLocalsOnBreak);
}
示例15: enterWithNode
import jdk.nashorn.internal.ir.Block; //导入方法依赖的package包/类
@Override
public boolean enterWithNode(final WithNode withNode) {
if(!method.isReachable()) {
return false;
}
enterStatement(withNode);
final Expression expression = withNode.getExpression();
final Block body = withNode.getBody();
// It is possible to have a "pathological" case where the with block does not reference *any* identifiers. It's
// pointless, but legal. In that case, if nothing else in the method forced the assignment of a slot to the
// scope object, its' possible that it won't have a slot assigned. In this case we'll only evaluate expression
// for its side effect and visit the body, and not bother opening and closing a WithObject.
final boolean hasScope = method.hasScope();
if (hasScope) {
method.loadCompilerConstant(SCOPE);
}
loadExpressionAsObject(expression);
final Label tryLabel;
if (hasScope) {
// Construct a WithObject if we have a scope
method.invoke(ScriptRuntime.OPEN_WITH);
method.storeCompilerConstant(SCOPE);
tryLabel = new Label("with_try");
method.label(tryLabel);
} else {
// We just loaded the expression for its side effect and to check
// for null or undefined value.
globalCheckObjectCoercible();
tryLabel = null;
}
// Always process body
body.accept(this);
if (hasScope) {
// Ensure we always close the WithObject
final Label endLabel = new Label("with_end");
final Label catchLabel = new Label("with_catch");
final Label exitLabel = new Label("with_exit");
method.label(endLabel);
// Somewhat conservatively presume that if the body is not empty, it can throw an exception. In any case,
// we must prevent trying to emit a try-catch for empty range, as it causes a verification error.
final boolean bodyCanThrow = endLabel.isAfter(tryLabel);
if(bodyCanThrow) {
method._try(tryLabel, endLabel, catchLabel);
}
final boolean reachable = method.isReachable();
if(reachable) {
popScope();
if(bodyCanThrow) {
method._goto(exitLabel);
}
}
if(bodyCanThrow) {
method._catch(catchLabel);
popScopeException();
method.athrow();
if(reachable) {
method.label(exitLabel);
}
}
}
return false;
}