Search in sources :

Example 31 with Jump

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

the class DefaultCodeGenerator method delegatedYield.

private void delegatedYield(Expression node, BiConsumer<Variable<ScriptObject>, Variable<Object>> iterNext, BiConsumer<Variable<ScriptObject>, Variable<Object>> iterThrow, BiConsumer<Variable<ScriptObject>, Variable<Object>> iterReturn, CodeVisitor mv) {
    Jump iteratorNext = new Jump();
    Jump generatorYield = new Jump();
    Jump generatorYieldOrReturn = new Jump();
    Jump done = new Jump();
    mv.lineInfo(node);
    mv.enterVariableScope();
    Variable<ScriptObject> iterator = mv.newVariable("iterator", ScriptObject.class);
    Variable<ScriptObject> innerResult = mv.newVariable("innerResult", ScriptObject.class);
    Variable<Object> received = mv.newVariable("received", Object.class);
    /* steps 3-4 */
    // stack: [value] -> []
    mv.loadExecutionContext();
    mv.swap();
    mv.invoke(Methods.AbstractOperations_GetIterator);
    mv.store(iterator);
    /* step 5 */
    // stack: [] -> []
    mv.loadUndefined();
    mv.store(received);
    /* step 6.a.i-6.a.ii */
    // stack: [] -> []
    mv.mark(iteratorNext);
    iterNext.accept(iterator, received);
    mv.store(innerResult);
    /* steps 6.a.iii-6.a.v */
    // stack: [] -> []
    IteratorComplete(node, innerResult, mv);
    mv.ifne(done);
    /* step 6.a.vi */
    // stack: [] -> [Object(innerResult)]
    // force stack top to Object-type
    mv.mark(generatorYield);
    mv.load(innerResult);
    mv.checkcast(Types.Object);
    mv.suspend();
    mv.store(received);
    /* step 6.b */
    Jump isException = new Jump();
    mv.load(received);
    mv.instanceOf(Types.ScriptException);
    mv.ifeq(isException);
    {
        /* steps 6.b.iii.1-4, 6.b.iv */
        iterThrow.accept(iterator, received);
        mv.store(innerResult);
        mv.goTo(generatorYieldOrReturn);
    }
    mv.mark(isException);
    /* step 6.c */
    mv.load(received);
    mv.instanceOf(Types.ReturnValue);
    mv.ifeq(iteratorNext);
    {
        /* steps 6.c.i-vii */
        iterReturn.accept(iterator, received);
        mv.store(innerResult);
        mv.load(innerResult);
        mv.ifnonnull(generatorYieldOrReturn);
        {
            /* step 6.c.iv */
            mv.popStack();
            mv.returnCompletion(__ -> {
                mv.load(received);
                mv.checkcast(Types.ReturnValue);
                mv.invoke(Methods.ReturnValue_getValue);
            });
        }
    }
    mv.mark(generatorYieldOrReturn);
    /* steps 6.b.iii.5-6, 6.c.viii-ix */
    IteratorComplete(node, innerResult, mv);
    mv.ifeq(generatorYield);
    /* step 6.b.iii.7, 6.c.x */
    mv.popStack();
    mv.returnCompletion(__ -> {
        IteratorValue(node, innerResult, mv);
    });
    /* step 6.a.v */
    mv.mark(done);
    IteratorValue(node, innerResult, mv);
    mv.exitVariableScope();
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) 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 32 with Jump

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

the class DefaultCodeGenerator method asyncIteratorClose.

/**
     * Emit:
     * 
     * <pre>
     * Callable returnMethod = GetMethod(cx, iterator, "return");
     * if (returnMethod != null) {
     *   try {
     *     returnMethod.call(cx, iterator);
     *     await;
     *   } catch (ScriptException e) {
     *     if (throwable != e) {
     *       throwable.addSuppressed(e);
     *     }
     *   }
     * }
     * </pre>
     * 
     * @param node
     *            the ast node
     * @param iterator
     *            the script iterator object
     * @param mv
     *            the code visitor
     */
final void asyncIteratorClose(Node node, Variable<ScriptObject> iterator, Variable<? extends Throwable> throwable, CodeVisitor mv) {
    IteratorClose(node, iterator, returnMethod -> {
        TryCatchLabel startCatch = new TryCatchLabel();
        TryCatchLabel endCatch = new TryCatchLabel(), handlerCatch = new TryCatchLabel();
        Jump noException = new Jump();
        mv.mark(startCatch);
        {
            InvokeMethod(node, mv, returnMethod, iterator);
            await(node, mv);
            mv.pop();
            mv.goTo(noException);
        }
        mv.mark(endCatch);
        mv.catchHandler(handlerCatch, Types.ScriptException);
        {
            mv.enterVariableScope();
            Variable<ScriptException> exception = mv.newVariable("exception", ScriptException.class);
            mv.store(exception);
            mv.load(throwable);
            mv.load(exception);
            mv.ifacmpeq(noException);
            {
                mv.load(throwable);
                mv.load(exception);
                mv.invoke(Methods.Throwable_addSuppressed);
            }
            mv.exitVariableScope();
        }
        mv.tryCatch(startCatch, endCatch, handlerCatch, Types.ScriptException);
        mv.mark(noException);
    }, mv);
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) Variable(com.github.anba.es6draft.compiler.assembler.Variable) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 33 with Jump

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

the class DefaultCodeGenerator method throwAfterResume.

private void throwAfterResume(CodeVisitor mv) {
    Jump isException = new Jump();
    mv.dup();
    mv.instanceOf(Types.ScriptException);
    mv.ifeq(isException);
    {
        mv.checkcast(Types.ScriptException);
        mv.athrow();
    }
    mv.mark(isException);
}
Also used : Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 34 with Jump

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

the class EvalDeclarationInstantiationGenerator method createFunctions.

/**
     * 18.2.1.2, step 14
     */
private void createFunctions(ArrayDeque<HoistableDeclaration> functionsToInitialize, boolean strict, Variable<ExecutionContext> context, Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> lexEnv, Variable<FunctionObject> fo, Variable<? extends EnvironmentRecord> varEnvRec, InstructionVisitor mv) {
    for (HoistableDeclaration f : functionsToInitialize) {
        Name fn = BoundName(f);
        // stack: [] -> []
        InstantiateFunctionObject(context, lexEnv, f, mv);
        mv.store(fo);
        BindingOp<EnvironmentRecord> op = BindingOp.LOOKUP;
        Jump funcAlreadyDeclared = new Jump(), after = new Jump();
        op.hasBinding(varEnvRec, fn, mv);
        mv.ifne(funcAlreadyDeclared);
        {
            op.createMutableBinding(varEnvRec, fn, true, mv);
            op.initializeBinding(varEnvRec, fn, fo, mv);
            mv.goTo(after);
        }
        mv.mark(funcAlreadyDeclared);
        {
            op.setMutableBinding(varEnvRec, fn, fo, strict, mv);
        }
        mv.mark(after);
    }
}
Also used : HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Jump(com.github.anba.es6draft.compiler.assembler.Jump) ScriptName(com.github.anba.es6draft.compiler.CodeGenerator.ScriptName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 35 with Jump

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

the class EvalDeclarationInstantiationGenerator method createVarDeclarations.

/**
     * 18.2.1.2, step 15
     */
private void createVarDeclarations(LinkedHashSet<Name> declaredVarNames, Variable<? extends EnvironmentRecord> varEnvRec, Variable<Undefined> undef, InstructionVisitor mv) {
    for (Name vn : declaredVarNames) {
        BindingOp<EnvironmentRecord> op = BindingOp.LOOKUP;
        Jump varAlreadyDeclared = new Jump();
        op.hasBinding(varEnvRec, vn, mv);
        mv.ifne(varAlreadyDeclared);
        {
            op.createMutableBinding(varEnvRec, vn, true, mv);
            op.initializeBinding(varEnvRec, vn, undef, mv);
        }
        mv.mark(varAlreadyDeclared);
    }
}
Also used : EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Jump(com.github.anba.es6draft.compiler.assembler.Jump) ScriptName(com.github.anba.es6draft.compiler.CodeGenerator.ScriptName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

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