Search in sources :

Example 1 with Jump

use of com.github.anba.es6draft.compiler.assembler.Jump in project es6draft by anba.

the class ComprehensionGenerator method visit.

/**
     * Runtime Semantics: ComprehensionComponentEvaluation
     * <p>
     * ComprehensionIf : if ( AssignmentExpression )
     */
@Override
public Void visit(ComprehensionIf node, CodeVisitor mv) {
    /* steps 1-2 */
    ValType type = expression(node.getTest(), mv);
    /* steps 3-4 */
    ToBoolean(type, mv);
    /* steps 5-6 */
    Jump lblTest = new Jump();
    mv.ifeq(lblTest);
    {
        /* step 5a */
        elements.next().accept(this, mv);
    }
    mv.mark(lblTest);
    return null;
}
Also used : Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 2 with Jump

use of com.github.anba.es6draft.compiler.assembler.Jump 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 3 with Jump

use of com.github.anba.es6draft.compiler.assembler.Jump 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 4 with Jump

use of com.github.anba.es6draft.compiler.assembler.Jump in project es6draft by anba.

the class DefaultCodeGenerator method returnAfterResume.

private void returnAfterResume(CodeVisitor mv) {
    Jump isReturn = new Jump();
    mv.dup();
    mv.instanceOf(Types.ReturnValue);
    mv.ifeq(isReturn);
    {
        mv.checkcast(Types.ReturnValue);
        mv.invoke(Methods.ReturnValue_getValue);
        if (mv.getStackSize() == 1) {
            mv.returnCompletion();
        } else {
            mv.enterVariableScope();
            Variable<Object> returnValue = mv.newVariable("returnValue", Object.class);
            mv.store(returnValue);
            mv.popStack();
            mv.returnCompletion(returnValue);
            mv.exitVariableScope();
        }
    }
    mv.mark(isReturn);
}
Also used : Variable(com.github.anba.es6draft.compiler.assembler.Variable) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 5 with Jump

use of com.github.anba.es6draft.compiler.assembler.Jump in project es6draft by anba.

the class DefaultCodeGenerator method IteratorClose.

/**
     * <pre>
     * Callable returnMethod = GetMethod(cx, iterator, "return");
     * if (returnMethod != null) {
     *   &lt;invoke return&gt;
     * }
     * </pre>
     * 
     * @param node
     *            the ast node
     * @param iterator
     *            the script iterator object
     * @param invokeReturn
     *            the code snippet to invoke return()
     * @param mv
     *            the code visitor
     */
final void IteratorClose(Node node, Variable<ScriptObject> iterator, Consumer<Variable<Callable>> invokeReturn, CodeVisitor mv) {
    mv.enterVariableScope();
    Variable<Callable> returnMethod = mv.newVariable("returnMethod", Callable.class);
    GetMethod(node, iterator, "return", mv);
    mv.store(returnMethod);
    Jump done = new Jump();
    mv.load(returnMethod);
    mv.ifnull(done);
    {
        invokeReturn.accept(returnMethod);
    }
    mv.mark(done);
    mv.exitVariableScope();
}
Also used : Jump(com.github.anba.es6draft.compiler.assembler.Jump) Callable(com.github.anba.es6draft.runtime.types.Callable)

Aggregations

Jump (com.github.anba.es6draft.compiler.assembler.Jump)46 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)14 Name (com.github.anba.es6draft.ast.scope.Name)10 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)9 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)9 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)8 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)7 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)7 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)7 ScriptName (com.github.anba.es6draft.compiler.CodeGenerator.ScriptName)6 BreakLabel (com.github.anba.es6draft.compiler.Labels.BreakLabel)6 TempLabel (com.github.anba.es6draft.compiler.Labels.TempLabel)6 Variable (com.github.anba.es6draft.compiler.assembler.Variable)6 EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)6 ContinueLabel (com.github.anba.es6draft.compiler.Labels.ContinueLabel)5 Completion (com.github.anba.es6draft.compiler.StatementGenerator.Completion)5 BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)4 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)3 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)3 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)3