Search in sources :

Example 26 with LexicalEnvironment

use of com.github.anba.es6draft.runtime.LexicalEnvironment 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)

Example 27 with LexicalEnvironment

use of com.github.anba.es6draft.runtime.LexicalEnvironment in project es6draft by anba.

the class StatementGenerator method visitTryCatch.

/**
 * 13.15.8 Runtime Semantics: Evaluation<br>
 *
 * <code>try-catch</code>
 *
 * @param node
 *            the try-statement
 * @param mv
 *            the code visitor
 * @return the completion value
 */
private Completion visitTryCatch(TryStatement node, CodeVisitor mv) {
    TryCatchLabel startCatch = new TryCatchLabel(), endCatch = new TryCatchLabel();
    TryCatchLabel handlerCatch = new TryCatchLabel();
    TryCatchLabel handlerCatchStackOverflow = new TryCatchLabel();
    Jump exceptionHandled = new Jump();
    mv.enterVariableScope();
    Variable<LexicalEnvironment<?>> savedEnv = saveEnvironment(mv);
    /* step 1 */
    // Emit try-block
    mv.mark(startCatch);
    Completion tryResult = emitTryBlock(node, exceptionHandled, mv);
    mv.mark(endCatch);
    /* step 2 */
    // Emit catch-block
    Completion catchResult = emitCatchBlock(node, savedEnv, handlerCatch, handlerCatchStackOverflow, mv);
    /* step 3 */
    if (!tryResult.isAbrupt()) {
        mv.mark(exceptionHandled);
    }
    mv.exitVariableScope();
    mv.tryCatch(startCatch, endCatch, handlerCatch, Types.ScriptException);
    mv.tryCatch(startCatch, endCatch, handlerCatchStackOverflow, Types.Error);
    /* steps 4-6 */
    return tryResult.select(catchResult);
}
Also used : LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 28 with LexicalEnvironment

use of com.github.anba.es6draft.runtime.LexicalEnvironment in project es6draft by anba.

the class StatementGenerator method visit.

/**
 * 13.7.3 The while Statement
 * <p>
 * 13.1.8 Runtime Semantics: Evaluation<br>
 * 13.1.7 Runtime Semantics: LabelledEvaluation<br>
 * 13.7.3.6 Runtime Semantics: LabelledEvaluation
 */
@Override
public Completion visit(WhileStatement node, CodeVisitor mv) {
    assert mv.getStackSize() == 0;
    Jump lblNext = new Jump(), lblTest = new Jump();
    ContinueLabel lblContinue = new ContinueLabel();
    BreakLabel lblBreak = new BreakLabel();
    Bool btest = Bool.evaluate(node.getTest());
    mv.enterVariableScope();
    Variable<LexicalEnvironment<?>> savedEnv = saveEnvironment(node, mv);
    /* step 1 */
    if (node.hasCompletionValue()) {
        mv.storeUndefinedAsCompletionValue();
    }
    /* step 2 (repeat loop) */
    if (btest != Bool.True) {
        mv.nonDestructiveGoTo(lblTest);
    }
    mv.mark(lblNext);
    /* steps 2.e-g */
    Completion result;
    {
        mv.enterIteration(node, lblBreak, lblContinue);
        result = node.getStatement().accept(this, mv);
        mv.exitIteration(node);
    }
    /* step 2.f (abrupt completion - continue) */
    if (lblContinue.isTarget()) {
        mv.mark(lblContinue);
        restoreEnvironment(savedEnv, mv);
    }
    /* steps 2.a-d */
    if (btest != Bool.True) {
        mv.mark(lblTest);
        testExpressionFallthrough(node.getTest(), lblNext, mv);
    } else if (!result.isAbrupt() || lblContinue.isTarget()) {
        mv.goTo(lblNext);
    }
    /* step 2.f (abrupt completion - break) */
    if (lblBreak.isTarget()) {
        mv.mark(lblBreak);
        restoreEnvironment(savedEnv, mv);
    }
    mv.exitVariableScope();
    /* steps 2.d, 2.f */
    if (btest == Bool.True && !lblBreak.isTarget()) {
        // abrupt or infinite loop
        return Completion.Abrupt;
    }
    return Completion.Normal;
}
Also used : ContinueLabel(com.github.anba.es6draft.compiler.Labels.ContinueLabel) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) BreakLabel(com.github.anba.es6draft.compiler.Labels.BreakLabel) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 29 with LexicalEnvironment

use of com.github.anba.es6draft.runtime.LexicalEnvironment in project es6draft by anba.

the class StatementGenerator method ForInBodyEvaluation.

/**
 * 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 ForInBodyEvaluation(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<Iterator<?>> iterator = mv.newVariable("iter", Iterator.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);
        ForInOfBodyEvaluationInner(node, nextValue, mv);
        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 : ContinueLabel(com.github.anba.es6draft.compiler.Labels.ContinueLabel) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) ScriptIterator(com.github.anba.es6draft.runtime.internal.ScriptIterator) Iterator(java.util.Iterator) 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) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 30 with LexicalEnvironment

use of com.github.anba.es6draft.runtime.LexicalEnvironment in project es6draft by anba.

the class StatementGenerator method ForBodyEvaluation.

/**
 * 13.7.4.8 Runtime Semantics: ForBodyEvaluation(test, increment, stmt, perIterationBindings, labelSet)
 */
private Completion ForBodyEvaluation(ForStatement node, boolean perIterationsLets, CodeVisitor mv) {
    assert mv.getStackSize() == 0;
    mv.enterVariableScope();
    /* step 1 */
    if (node.hasCompletionValue()) {
        mv.storeUndefinedAsCompletionValue();
    }
    /* steps 2-3 */
    Variable<LexicalEnvironment<?>> savedEnv;
    if (perIterationsLets) {
        savedEnv = mv.newVariable("savedEnv", LexicalEnvironment.class).uncheckedCast();
        CreatePerIterationEnvironment(savedEnv, mv);
    } else {
        savedEnv = saveEnvironment(node, mv);
    }
    Jump lblTest = new Jump(), lblStmt = new Jump();
    ContinueLabel lblContinue = new ContinueLabel();
    BreakLabel lblBreak = new BreakLabel();
    Bool btest = node.getTest() != null ? Bool.evaluate(node.getTest()) : Bool.True;
    /* steps 4.b-d */
    Completion result;
    if (btest != Bool.True) {
        mv.nonDestructiveGoTo(lblTest);
    }
    mv.mark(lblStmt);
    {
        mv.enterIteration(node, lblBreak, lblContinue);
        result = node.getStatement().accept(this, mv);
        mv.exitIteration(node);
    }
    /* step 4.c (abrupt completion - continue) */
    if (lblContinue.isTarget()) {
        mv.mark(lblContinue);
        restoreEnvironment(savedEnv, mv);
    }
    /* steps 4.e-f */
    if (perIterationsLets && (!result.isAbrupt() || lblContinue.isTarget())) {
        CreatePerIterationEnvironment(savedEnv, mv);
    }
    /* step 4.g */
    if (node.getStep() != null && (!result.isAbrupt() || lblContinue.isTarget())) {
        ValType type = expression(node.getStep().emptyCompletion(), mv);
        mv.pop(type);
    }
    /* step 4.a */
    if (btest != Bool.True) {
        mv.mark(lblTest);
        testExpressionFallthrough(node.getTest(), lblStmt, mv);
    } else {
        mv.goTo(lblStmt);
    }
    /* step 4.c (abrupt completion - break) */
    if (lblBreak.isTarget()) {
        mv.mark(lblBreak);
        restoreEnvironment(savedEnv, mv);
    }
    mv.exitVariableScope();
    if (btest == Bool.True && !lblBreak.isTarget()) {
        // abrupt or infinite loop
        return Completion.Abrupt;
    }
    return Completion.Normal;
}
Also used : ContinueLabel(com.github.anba.es6draft.compiler.Labels.ContinueLabel) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) BreakLabel(com.github.anba.es6draft.compiler.Labels.BreakLabel) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Aggregations

LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)32 Name (com.github.anba.es6draft.ast.scope.Name)16 Jump (com.github.anba.es6draft.compiler.assembler.Jump)16 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)15 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)15 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)15 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)12 Declaration (com.github.anba.es6draft.ast.Declaration)10 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)10 HashSet (java.util.HashSet)10 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)9 BreakLabel (com.github.anba.es6draft.compiler.Labels.BreakLabel)8 ArrayDeque (java.util.ArrayDeque)8 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)7 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)7 VariableDeclaration (com.github.anba.es6draft.ast.VariableDeclaration)6 VariableStatement (com.github.anba.es6draft.ast.VariableStatement)6 ContinueLabel (com.github.anba.es6draft.compiler.Labels.ContinueLabel)6 Undefined (com.github.anba.es6draft.runtime.types.Undefined)6 BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)5