Search in sources :

Example 6 with TempLabel

use of com.github.anba.es6draft.compiler.Labels.TempLabel in project es6draft by anba.

the class StatementGenerator method visitTryFinally.

/**
 * 13.15.8 Runtime Semantics: Evaluation<br>
 *
 * <code>try-finally</code>
 *
 * @param node
 *            the try-statement
 * @param mv
 *            the code visitor
 * @return the completion value
 */
private Completion visitTryFinally(TryStatement node, CodeVisitor mv) {
    TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
    TryCatchLabel handlerFinally = new TryCatchLabel();
    TryCatchLabel handlerFinallyStackOverflow = new TryCatchLabel();
    Jump noException = new Jump();
    mv.enterVariableScope();
    Variable<LexicalEnvironment<?>> savedEnv = saveEnvironment(mv);
    MutableValue<Object> completion = mv.enterFinallyScoped(node);
    /* step 1 */
    // Emit try-block
    mv.mark(startFinally);
    Completion tryResult = emitTryBlock(node, noException, mv);
    mv.mark(endFinally);
    // Restore temporary abrupt targets
    List<TempLabel> tempLabels = mv.exitFinallyScoped();
    /* step 2 */
    // Emit finally-block
    Completion finallyResult = emitFinallyBlock(node, savedEnv, completion, tryResult, Completion.Abrupt, handlerFinally, handlerFinallyStackOverflow, noException, tempLabels, mv);
    mv.exitVariableScope();
    mv.tryCatch(startFinally, endFinally, handlerFinally, Types.ScriptException);
    mv.tryCatch(startFinally, endFinally, handlerFinallyStackOverflow, Types.Error);
    /* steps 3-6 */
    return finallyResult.then(tryResult);
}
Also used : LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) Jump(com.github.anba.es6draft.compiler.assembler.Jump) TempLabel(com.github.anba.es6draft.compiler.Labels.TempLabel)

Example 7 with TempLabel

use of com.github.anba.es6draft.compiler.Labels.TempLabel in project es6draft by anba.

the class StatementGenerator method ForInOfBodyEvaluation.

/**
 * 13.7.5.13 Runtime Semantics: ForIn/OfBodyEvaluation (lhs, stmt, iterator, lhsKind, labelSet)
 * <p>
 * stack: [Iterator] {@literal ->} []
 *
 * @param <FORSTATEMENT>
 *            the for-statement node type
 * @param node
 *            the for-statement node
 * @param mv
 *            the code visitor
 * @return the completion value
 */
private <FORSTATEMENT extends IterationStatement & ForIterationNode> Completion ForInOfBodyEvaluation(FORSTATEMENT node, CodeVisitor mv) {
    assert mv.getStackSize() == 1;
    ContinueLabel lblContinue = new ContinueLabel();
    BreakLabel lblBreak = new BreakLabel();
    Jump enter = new Jump(), test = new Jump();
    mv.enterVariableScope();
    Variable<ScriptIterator<?>> iterator = mv.newVariable("iter", ScriptIterator.class).uncheckedCast();
    // stack: [Iterator] -> []
    mv.store(iterator);
    Variable<Object> nextValue = mv.newVariable("nextValue", Object.class);
    Variable<LexicalEnvironment<?>> savedEnv = saveEnvironment(node, mv);
    /* step 2 */
    if (node.hasCompletionValue()) {
        mv.storeUndefinedAsCompletionValue();
    }
    /* steps 3-4 (not applicable) */
    /* step 5 (repeat loop) */
    mv.nonDestructiveGoTo(test);
    /* steps 5.d-e */
    mv.mark(enter);
    mv.load(iterator);
    mv.lineInfo(node);
    mv.invoke(Methods.Iterator_next);
    mv.store(nextValue);
    /* steps 5.f-l */
    {
        mv.enterIteration(node, lblBreak, lblContinue);
        mv.enterWrapped();
        new IterationGenerator<FORSTATEMENT>(codegen) {

            @Override
            protected Completion iterationBody(FORSTATEMENT node, Variable<ScriptIterator<?>> iterator, CodeVisitor mv) {
                return ForInOfBodyEvaluationInner(node, nextValue, mv);
            }

            @Override
            protected MutableValue<Object> enterIteration(FORSTATEMENT node, CodeVisitor mv) {
                return mv.enterIterationBody(node);
            }

            @Override
            protected List<TempLabel> exitIteration(FORSTATEMENT node, CodeVisitor mv) {
                return mv.exitIterationBody(node);
            }
        }.generate(node, iterator, test, mv);
        mv.exitWrapped();
        mv.exitIteration(node);
    }
    /* steps 5.m-n */
    if (lblContinue.isTarget()) {
        mv.mark(lblContinue);
        restoreEnvironment(savedEnv, mv);
    }
    /* steps 5.a-c */
    mv.mark(test);
    mv.load(iterator);
    mv.lineInfo(node);
    mv.invoke(Methods.Iterator_hasNext);
    mv.ifne(enter);
    /* steps 5.m-n */
    if (lblBreak.isTarget()) {
        mv.mark(lblBreak);
        restoreEnvironment(savedEnv, mv);
    }
    mv.exitVariableScope();
    return Completion.Normal;
}
Also used : Variable(com.github.anba.es6draft.compiler.assembler.Variable) Jump(com.github.anba.es6draft.compiler.assembler.Jump) TempLabel(com.github.anba.es6draft.compiler.Labels.TempLabel) ScriptIterator(com.github.anba.es6draft.runtime.internal.ScriptIterator) ContinueLabel(com.github.anba.es6draft.compiler.Labels.ContinueLabel) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) BreakLabel(com.github.anba.es6draft.compiler.Labels.BreakLabel)

Aggregations

TempLabel (com.github.anba.es6draft.compiler.Labels.TempLabel)7 Jump (com.github.anba.es6draft.compiler.assembler.Jump)6 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)4 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)4 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)4 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)3 BreakLabel (com.github.anba.es6draft.compiler.Labels.BreakLabel)2 ContinueLabel (com.github.anba.es6draft.compiler.Labels.ContinueLabel)2 Variable (com.github.anba.es6draft.compiler.assembler.Variable)2 ScriptIterator (com.github.anba.es6draft.runtime.internal.ScriptIterator)2 Completion (com.github.anba.es6draft.compiler.StatementGenerator.Completion)1