Search in sources :

Example 1 with ScriptIterator

use of com.github.anba.es6draft.runtime.internal.ScriptIterator in project es6draft by anba.

the class ComprehensionGenerator method visit.

/**
     * Runtime Semantics: ComprehensionComponentEvaluation
     * <p>
     * ComprehensionFor : for ( ForBinding of AssignmentExpression )
     */
@Override
public Void visit(LegacyComprehensionFor node, CodeVisitor mv) {
    Jump lblTest = new Jump(), lblLoop = new Jump(), lblFail = new Jump();
    Variable<ScriptIterator<?>> iter = iterators.next();
    ValType type = expressionBoxed(node.getExpression(), mv);
    if (type != ValType.Object) {
        // fail-safe behaviour for null/undefined values in legacy comprehensions
        Jump loopstart = new Jump();
        mv.dup();
        isUndefinedOrNull(mv);
        mv.ifeq(loopstart);
        mv.pop();
        mv.goTo(lblFail);
        mv.mark(loopstart);
    }
    IterationKind iterationKind = node.getIterationKind();
    if (iterationKind == IterationKind.Enumerate || iterationKind == IterationKind.EnumerateValues) {
        // legacy generator mode, both, for-in and for-each, perform Iterate on generators
        Jump l0 = new Jump(), l1 = new Jump();
        mv.dup();
        mv.instanceOf(Types.GeneratorObject);
        mv.ifeq(l0);
        mv.dup();
        mv.checkcast(Types.GeneratorObject);
        mv.invoke(Methods.GeneratorObject_isLegacyGenerator);
        mv.ifeq(l0);
        mv.loadExecutionContext();
        mv.lineInfo(node.getExpression());
        mv.invoke(Methods.ScriptRuntime_iterate);
        mv.goTo(l1);
        mv.mark(l0);
        mv.loadExecutionContext();
        mv.lineInfo(node.getExpression());
        if (iterationKind == IterationKind.Enumerate) {
            mv.invoke(Methods.ScriptRuntime_enumerate);
        } else {
            mv.invoke(Methods.ScriptRuntime_enumerateValues);
        }
        mv.mark(l1);
    } else {
        assert iterationKind == IterationKind.Iterate;
        mv.loadExecutionContext();
        mv.lineInfo(node.getExpression());
        mv.invoke(Methods.ScriptRuntime_iterate);
    }
    mv.store(iter);
    mv.nonDestructiveGoTo(lblTest);
    mv.mark(lblLoop);
    mv.load(iter);
    mv.lineInfo(node);
    mv.invoke(Methods.Iterator_next);
    new IterationGenerator<LegacyComprehensionFor>(codegen) {

        @Override
        protected Completion iterationBody(LegacyComprehensionFor node, Variable<ScriptIterator<?>> iterator, CodeVisitor mv) {
            // stack: [nextValue] -> []
            BindingInitialization(codegen, node.getBinding(), mv);
            elements.next().accept(ComprehensionGenerator.this, mv);
            return Completion.Normal;
        }

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

        @Override
        protected List<TempLabel> exitIteration(LegacyComprehensionFor node, CodeVisitor mv) {
            return mv.exitIteration();
        }
    }.generate(node, iter, mv);
    mv.mark(lblTest);
    mv.load(iter);
    mv.lineInfo(node);
    mv.invoke(Methods.Iterator_hasNext);
    mv.ifne(lblLoop);
    mv.mark(lblFail);
    return null;
}
Also used : Completion(com.github.anba.es6draft.compiler.StatementGenerator.Completion) IterationKind(com.github.anba.es6draft.ast.LegacyComprehensionFor.IterationKind) LegacyComprehensionFor(com.github.anba.es6draft.ast.LegacyComprehensionFor) MutableValue(com.github.anba.es6draft.compiler.assembler.MutableValue) ArrayList(java.util.ArrayList) List(java.util.List) Jump(com.github.anba.es6draft.compiler.assembler.Jump) ScriptIterator(com.github.anba.es6draft.runtime.internal.ScriptIterator)

Example 2 with ScriptIterator

use of com.github.anba.es6draft.runtime.internal.ScriptIterator in project es6draft by anba.

the class ComprehensionGenerator method visit.

/**
     * Runtime Semantics: ComprehensionComponentEvaluation
     * <p>
     * ComprehensionFor : for ( ForBinding of AssignmentExpression )
     */
@Override
public Void visit(ComprehensionFor node, CodeVisitor mv) {
    Jump lblTest = new Jump(), lblLoop = new Jump();
    Variable<ScriptIterator<?>> iter = iterators.next();
    /* steps 1-2 */
    expressionBoxed(node.getExpression(), mv);
    /* steps 3-4 */
    mv.loadExecutionContext();
    mv.lineInfo(node.getExpression());
    mv.invoke(Methods.ScriptRuntime_iterate);
    mv.store(iter);
    /* step 5 (not applicable) */
    /* step 6 */
    mv.nonDestructiveGoTo(lblTest);
    /* steps 6.d-e */
    mv.mark(lblLoop);
    mv.load(iter);
    mv.lineInfo(node);
    mv.invoke(Methods.Iterator_next);
    /* steps 6.f-j */
    BlockScope scope = node.getScope();
    if (scope.isPresent()) {
        mv.enterVariableScope();
        Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> env = mv.newVariable("env", LexicalEnvironment.class).uncheckedCast();
        Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
        // stack: [nextValue] -> []
        Variable<Object> nextValue = mv.newVariable("nextValue", Object.class);
        mv.store(nextValue);
        newDeclarativeEnvironment(scope, mv);
        mv.store(env);
        getEnvRec(env, envRec, mv);
        for (Name name : BoundNames(node.getBinding())) {
            BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, name);
            op.createMutableBinding(envRec, name, false, mv);
        }
        BindingInitialization(codegen, envRec, node.getBinding(), nextValue, mv);
        mv.load(env);
        pushLexicalEnvironment(mv);
        mv.exitVariableScope();
    } else {
        // stack: [nextValue] -> []
        mv.pop();
    }
    /* step 6.k */
    mv.enterScope(node);
    new IterationGenerator<ComprehensionFor>(codegen) {

        @Override
        protected Completion iterationBody(ComprehensionFor node, Variable<ScriptIterator<?>> iterator, CodeVisitor mv) {
            elements.next().accept(ComprehensionGenerator.this, mv);
            return Completion.Normal;
        }

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

        @Override
        protected List<TempLabel> exitIteration(ComprehensionFor node, CodeVisitor mv) {
            return mv.exitIteration();
        }
    }.generate(node, iter, mv);
    mv.exitScope();
    /* steps 6.l-m */
    if (scope.isPresent()) {
        popLexicalEnvironment(mv);
    }
    /* steps 6.a-c */
    mv.mark(lblTest);
    mv.load(iter);
    mv.lineInfo(node);
    mv.invoke(Methods.Iterator_hasNext);
    mv.ifne(lblLoop);
    return null;
}
Also used : LegacyComprehensionFor(com.github.anba.es6draft.ast.LegacyComprehensionFor) ComprehensionFor(com.github.anba.es6draft.ast.ComprehensionFor) MutableValue(com.github.anba.es6draft.compiler.assembler.MutableValue) Jump(com.github.anba.es6draft.compiler.assembler.Jump) ScriptIterator(com.github.anba.es6draft.runtime.internal.ScriptIterator) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) Completion(com.github.anba.es6draft.compiler.StatementGenerator.Completion) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) ArrayList(java.util.ArrayList) List(java.util.List) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)

Example 3 with ScriptIterator

use of com.github.anba.es6draft.runtime.internal.ScriptIterator 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 4 with ScriptIterator

use of com.github.anba.es6draft.runtime.internal.ScriptIterator in project es6draft by anba.

the class ComprehensionGenerator method visit.

/**
     * Runtime Semantics: ComprehensionEvaluation
     */
@Override
public Void visit(Comprehension node, CodeVisitor mv) {
    ArrayList<Node> list = new ArrayList<>(node.getList().size() + 1);
    list.addAll(node.getList());
    list.add(node.getExpression());
    elements = list.iterator();
    // Create variables early so they'll appear next to each other in the local variable map.
    ArrayList<Variable<ScriptIterator<?>>> iters = new ArrayList<>();
    for (ComprehensionQualifier e : node.getList()) {
        if (e instanceof ComprehensionFor || e instanceof LegacyComprehensionFor) {
            Variable<ScriptIterator<?>> iter = mv.newVariable("iter", ScriptIterator.class).uncheckedCast();
            iters.add(iter);
        }
    }
    iterators = iters.iterator();
    // Start generating code.
    elements.next().accept(this, mv);
    return null;
}
Also used : LegacyComprehensionFor(com.github.anba.es6draft.ast.LegacyComprehensionFor) ComprehensionFor(com.github.anba.es6draft.ast.ComprehensionFor) Variable(com.github.anba.es6draft.compiler.assembler.Variable) Node(com.github.anba.es6draft.ast.Node) ComprehensionQualifier(com.github.anba.es6draft.ast.ComprehensionQualifier) ArrayList(java.util.ArrayList) LegacyComprehensionFor(com.github.anba.es6draft.ast.LegacyComprehensionFor) ScriptIterator(com.github.anba.es6draft.runtime.internal.ScriptIterator)

Aggregations

ScriptIterator (com.github.anba.es6draft.runtime.internal.ScriptIterator)4 LegacyComprehensionFor (com.github.anba.es6draft.ast.LegacyComprehensionFor)3 Jump (com.github.anba.es6draft.compiler.assembler.Jump)3 ArrayList (java.util.ArrayList)3 ComprehensionFor (com.github.anba.es6draft.ast.ComprehensionFor)2 Completion (com.github.anba.es6draft.compiler.StatementGenerator.Completion)2 MutableValue (com.github.anba.es6draft.compiler.assembler.MutableValue)2 Variable (com.github.anba.es6draft.compiler.assembler.Variable)2 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)2 List (java.util.List)2 ComprehensionQualifier (com.github.anba.es6draft.ast.ComprehensionQualifier)1 IterationKind (com.github.anba.es6draft.ast.LegacyComprehensionFor.IterationKind)1 Node (com.github.anba.es6draft.ast.Node)1 BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)1 Name (com.github.anba.es6draft.ast.scope.Name)1 BreakLabel (com.github.anba.es6draft.compiler.Labels.BreakLabel)1 ContinueLabel (com.github.anba.es6draft.compiler.Labels.ContinueLabel)1 TempLabel (com.github.anba.es6draft.compiler.Labels.TempLabel)1 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)1 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)1