Search in sources :

Example 11 with BlockScope

use of com.github.anba.es6draft.ast.scope.BlockScope in project es6draft by anba.

the class StatementGenerator method visit.

/**
 * 13.7.4 The for Statement
 * <p>
 * 13.1.8 Runtime Semantics: Evaluation<br>
 * 13.1.7 Runtime Semantics: LabelledEvaluation<br>
 * 13.7.4.7 Runtime Semantics: LabelledEvaluation
 */
@Override
public Completion visit(ForStatement node, CodeVisitor mv) {
    assert mv.getStackSize() == 0;
    boolean perIterationsLets = false;
    BlockScope scope = node.getScope();
    Node head = node.getHead();
    if (head == null) {
    // empty
    } else if (head instanceof Expression) {
        ValType type = expression(((Expression) head).emptyCompletion(), mv);
        mv.pop(type);
    } else if (head instanceof VariableStatement) {
        head.accept(this, mv);
    } else {
        assert head instanceof LexicalDeclaration;
        LexicalDeclaration lexDecl = (LexicalDeclaration) head;
        List<Name> boundNames = BoundNames(lexDecl);
        boolean isConst = IsConstantDeclaration(lexDecl);
        perIterationsLets = !isConst && !boundNames.isEmpty();
        if (scope.isPresent()) {
            mv.enterVariableScope();
            Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
            // stack: [] -> [loopEnv]
            newDeclarativeEnvironment(scope, mv);
            // stack: [loopEnv] -> [loopEnv]
            getEnvRec(envRec, mv);
            // stack: [loopEnv] -> [loopEnv]
            for (Name dn : boundNames) {
                BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, dn);
                if (isConst) {
                    op.createImmutableBinding(envRec, dn, true, mv);
                } else {
                    op.createMutableBinding(envRec, dn, false, mv);
                }
            }
            // stack: [loopEnv] -> []
            pushLexicalEnvironment(mv);
            mv.exitVariableScope();
        }
        mv.enterScope(node);
        lexDecl.accept(this, mv);
    }
    Completion result = ForBodyEvaluation(node, perIterationsLets, mv);
    if (head instanceof LexicalDeclaration) {
        mv.exitScope();
        if (scope.isPresent() && !result.isAbrupt()) {
            popLexicalEnvironment(mv);
        }
    }
    return result;
}
Also used : InitializeBoundName(com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)

Example 12 with BlockScope

use of com.github.anba.es6draft.ast.scope.BlockScope in project es6draft by anba.

the class StatementGenerator method ForInOfHeadEvaluation.

/**
 * 13.7.5.12 Runtime Semantics: ForIn/OfHeadEvaluation (TDZnames, expr, iterationKind, labelSet)
 * <p>
 * stack: [] {@literal ->} [Iterator]
 *
 * @param <FORSTATEMENT>
 *            the for-statement node type
 * @param node
 *            the for-statement node
 * @param iterationKind
 *            the for-statement's iteration kind
 * @param lblFail
 *            the target instruction if the expression node does not produce an object type
 * @param mv
 *            the code visitor
 * @return the value type of the expression
 */
private <FORSTATEMENT extends IterationStatement & ForIterationNode> ValType ForInOfHeadEvaluation(FORSTATEMENT node, IterationKind iterationKind, Jump lblFail, CodeVisitor mv) {
    /* steps 1-2 */
    BlockScope scope = node.getScope();
    Node lhs = node.getHead();
    List<Name> tdzNames = null;
    if (lhs instanceof LexicalDeclaration) {
        tdzNames = BoundNames(forDeclarationBinding((LexicalDeclaration) lhs));
        assert scope.isPresent() == !tdzNames.isEmpty();
        if (scope.isPresent()) {
            mv.enterVariableScope();
            Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
            // stack: [] -> [TDZ]
            newDeclarativeEnvironment(scope, mv);
            // stack: [TDZ] -> [TDZ]
            getEnvRec(envRec, mv);
            // stack: [TDZ] -> [TDZ]
            for (Name name : tdzNames) {
                BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, name);
                op.createMutableBinding(envRec, name, false, mv);
            }
            mv.exitVariableScope();
            // stack: [TDZ] -> []
            pushLexicalEnvironment(mv);
        }
        mv.enterScope(node);
    }
    /* steps 3, 5-6 */
    Expression expr = node.getExpression();
    ValType type = expressionBoxed(expr, mv);
    /* step 4 */
    if (tdzNames != null) {
        mv.exitScope();
        if (scope.isPresent()) {
            popLexicalEnvironment(mv);
        }
    }
    /* steps 7-8 */
    if (iterationKind == IterationKind.Enumerate) {
        /* step 7.a */
        if (type != ValType.Object) {
            Jump loopstart = new Jump();
            mv.dup();
            isUndefinedOrNull(mv);
            mv.ifeq(loopstart);
            mv.pop();
            mv.goTo(lblFail);
            mv.mark(loopstart);
        }
        /* steps 7.b-c */
        mv.loadExecutionContext();
        mv.lineInfo(expr);
        mv.invoke(Methods.IteratorOperations_enumerate);
    } else if (iterationKind == IterationKind.AsyncIterate) {
        mv.loadExecutionContext();
        mv.lineInfo(expr);
        mv.invoke(Methods.IteratorOperations_asyncIterate);
    } else {
        /* step 8 */
        assert iterationKind == IterationKind.Iterate;
        mv.loadExecutionContext();
        mv.lineInfo(expr);
        mv.invoke(Methods.IteratorOperations_iterate);
    }
    return type;
}
Also used : BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Jump(com.github.anba.es6draft.compiler.assembler.Jump) InitializeBoundName(com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 13 with BlockScope

use of com.github.anba.es6draft.ast.scope.BlockScope in project es6draft by anba.

the class ExpressionGenerator method isEnclosedByLexicalDeclaration.

private boolean isEnclosedByLexicalDeclaration(Scope currentScope) {
    final boolean catchVar = codegen.isEnabled(CompatibilityOption.CatchVarStatement);
    TopLevelScope top = currentScope.getTop();
    for (Scope scope : currentScope) {
        if (scope instanceof BlockScope) {
            if (catchVar) {
                ScopedNode node = scope.getNode();
                if (node instanceof CatchClause && ((CatchClause) node).getCatchParameter() instanceof BindingIdentifier) {
                    continue;
                }
            }
            if (!((BlockScope) scope).lexicallyDeclaredNames().isEmpty()) {
                return true;
            }
        } else if (scope == top) {
            break;
        }
    }
    if (!top.lexicallyDeclaredNames().isEmpty()) {
        TopLevelNode<?> topNode = top.getNode();
        if (topNode instanceof Script) {
            return ((Script) topNode).isEvalScript();
        }
        return true;
    }
    return codegen.isEnabled(Parser.Option.EnclosedByLexicalDeclaration);
}
Also used : WithScope(com.github.anba.es6draft.ast.scope.WithScope) Scope(com.github.anba.es6draft.ast.scope.Scope) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) TopLevelScope(com.github.anba.es6draft.ast.scope.TopLevelScope) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) TopLevelScope(com.github.anba.es6draft.ast.scope.TopLevelScope)

Aggregations

BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)13 Name (com.github.anba.es6draft.ast.scope.Name)9 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)8 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)7 InitializeBoundName (com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName)5 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)5 Jump (com.github.anba.es6draft.compiler.assembler.Jump)3 Completion (com.github.anba.es6draft.compiler.StatementGenerator.Completion)2 Variable (com.github.anba.es6draft.compiler.assembler.Variable)2 ComprehensionFor (com.github.anba.es6draft.ast.ComprehensionFor)1 Scope (com.github.anba.es6draft.ast.scope.Scope)1 TopLevelScope (com.github.anba.es6draft.ast.scope.TopLevelScope)1 WithScope (com.github.anba.es6draft.ast.scope.WithScope)1 LabelledHashKey (com.github.anba.es6draft.compiler.CodeVisitor.LabelledHashKey)1 ValType (com.github.anba.es6draft.compiler.DefaultCodeGenerator.ValType)1 BreakLabel (com.github.anba.es6draft.compiler.Labels.BreakLabel)1 FieldName (com.github.anba.es6draft.compiler.assembler.FieldName)1 MutableValue (com.github.anba.es6draft.compiler.assembler.MutableValue)1 ScriptIterator (com.github.anba.es6draft.runtime.internal.ScriptIterator)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1