Search in sources :

Example 21 with Jump

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

the class StatementGenerator method emitFinallyBlock.

private Completion emitFinallyBlock(TryStatement node, Variable<LexicalEnvironment<?>> savedEnv, Value<Object> completion, Completion tryResult, Completion catchResult, TryCatchLabel handlerFinally, TryCatchLabel handlerFinallyStackOverflow, Jump noException, List<TempLabel> tempLabels, CodeVisitor mv) {
    BlockStatement finallyBlock = node.getFinallyBlock();
    assert finallyBlock != null;
    // various finally blocks (1 - 4)
    // (1) finally block for abrupt throw completions within 'try-catch'
    mv.enterVariableScope();
    Variable<Throwable> throwable = mv.newVariable("throwable", Throwable.class);
    mv.catchHandler(handlerFinallyStackOverflow, Types.Error);
    mv.invoke(Methods.ScriptRuntime_stackOverflowError);
    mv.catchHandler(handlerFinally, Types.ScriptException);
    mv.store(throwable);
    restoreEnvironment(savedEnv, mv);
    Completion finallyResult = finallyBlock.accept(this, mv);
    if (!finallyResult.isAbrupt()) {
        mv.load(throwable);
        mv.athrow();
    }
    mv.exitVariableScope();
    // (2) finally block if 'try' did not complete abruptly
    // (3) finally block if 'catch' did not complete abruptly
    Jump exceptionHandled = null;
    if (!tryResult.isAbrupt() || !catchResult.isAbrupt()) {
        mv.mark(noException);
        finallyBlock.accept(this, mv);
        if (!finallyResult.isAbrupt()) {
            if (node.hasCompletionValue()) {
                mv.storeCompletionValue(completion);
            }
            if (!tempLabels.isEmpty()) {
                exceptionHandled = new Jump();
                mv.goTo(exceptionHandled);
            }
        }
    }
    // (4) finally blocks for other abrupt completion (return, break, continue)
    for (TempLabel temp : tempLabels) {
        if (temp.isTarget()) {
            mv.mark(temp);
            restoreEnvironment(savedEnv, mv);
            finallyBlock.accept(this, mv);
            if (!finallyResult.isAbrupt()) {
                if (node.hasCompletionValue()) {
                    mv.storeCompletionValue(completion);
                }
                mv.goTo(temp, completion);
            }
        }
    }
    if (exceptionHandled != null) {
        mv.mark(exceptionHandled);
    }
    return finallyResult.nonEmpty();
}
Also used : Jump(com.github.anba.es6draft.compiler.assembler.Jump) TempLabel(com.github.anba.es6draft.compiler.Labels.TempLabel)

Example 22 with Jump

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

the class GlobalDeclarationInstantiationGenerator method generate.

private void generate(Script script, InstructionVisitor mv) {
    Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    Variable<LexicalEnvironment<GlobalEnvironmentRecord>> env = mv.newVariable("globalEnv", LexicalEnvironment.class).uncheckedCast();
    Variable<GlobalEnvironmentRecord> envRec = mv.newVariable("envRec", GlobalEnvironmentRecord.class);
    Variable<FunctionObject> fo = null;
    /* steps 1-2 */
    getLexicalEnvironment(context, env, mv);
    getEnvironmentRecord(env, envRec, mv);
    /* step 3 */
    HashSet<Name> lexNames = new HashSet<>();
    /* step 4 */
    HashSet<Name> varNames = new HashSet<>();
    // Iterate over declarations to be able to emit line-info entries.
    for (Declaration d : LexicallyScopedDeclarations(script)) {
        assert !(d instanceof HoistableDeclaration);
        for (Name name : BoundNames(d)) {
            if (lexNames.add(name)) {
                canDeclareLexicalScopedOrThrow(context, envRec, d, name, mv);
            }
        }
    }
    // Iterate over declarations to be able to emit line-info entries.
    for (StatementListItem item : VarScopedDeclarations(script)) {
        if (item instanceof VariableStatement) {
            for (VariableDeclaration vd : ((VariableStatement) item).getElements()) {
                for (Name name : BoundNames(vd)) {
                    if (varNames.add(name)) {
                        canDeclareVarScopedOrThrow(context, envRec, vd, name, mv);
                    }
                }
            }
        } else {
            HoistableDeclaration d = (HoistableDeclaration) item;
            Name name = BoundName(d);
            if (varNames.add(name)) {
                canDeclareVarScopedOrThrow(context, envRec, d, name, mv);
            }
        }
    }
    /* step 7 */
    List<StatementListItem> varDeclarations = VarScopedDeclarations(script);
    /* step 8 */
    ArrayDeque<HoistableDeclaration> functionsToInitialize = new ArrayDeque<>();
    /* step 9 */
    HashSet<Name> declaredFunctionNames = new HashSet<>();
    /* step 10 */
    for (StatementListItem item : reverse(varDeclarations)) {
        if (item instanceof HoistableDeclaration) {
            HoistableDeclaration d = (HoistableDeclaration) item;
            Name fn = BoundName(d);
            if (declaredFunctionNames.add(fn)) {
                canDeclareGlobalFunctionOrThrow(context, envRec, d, fn, mv);
                functionsToInitialize.addFirst(d);
            }
        }
    }
    if (!functionsToInitialize.isEmpty()) {
        fo = mv.newVariable("fo", FunctionObject.class);
    }
    /* step 11 */
    LinkedHashMap<Name, VariableDeclaration> declaredVarNames = new LinkedHashMap<>();
    /* step 12 */
    for (StatementListItem d : varDeclarations) {
        if (d instanceof VariableStatement) {
            for (VariableDeclaration vd : ((VariableStatement) d).getElements()) {
                for (Name vn : BoundNames(vd)) {
                    if (!declaredFunctionNames.contains(vn)) {
                        canDeclareGlobalVarOrThrow(context, envRec, vd, vn, mv);
                        declaredVarNames.put(vn, vd);
                    }
                }
            }
        }
    }
    // ES2016: Block-scoped global function declarations
    if (hasBlockFunctions(script)) {
        int idCounter = 0;
        HashSet<Name> declaredFunctionOrVarNames = new HashSet<>();
        declaredFunctionOrVarNames.addAll(declaredFunctionNames);
        declaredFunctionOrVarNames.addAll(declaredVarNames.keySet());
        for (FunctionDeclaration f : script.getScope().blockFunctions()) {
            Name fn = BoundName(f);
            Jump next = new Jump();
            // Runtime check always required for global block-level function declarations.
            f.setLegacyBlockScopeId(++idCounter);
            // FIXME: spec issue - avoid (observable!) duplicate checks for same name?
            // FIXME: spec issue - property creation order important?
            canDeclareGlobalFunction(envRec, f, fn, next, mv);
            setLegacyBlockFunction(context, f, mv);
            if (declaredFunctionOrVarNames.add(fn)) {
                createGlobalFunctionBinding(envRec, f, fn, false, mv);
            }
            mv.mark(next);
        }
    }
    /* step 14 */
    List<Declaration> lexDeclarations = LexicallyScopedDeclarations(script);
    /* step 15 */
    for (Declaration d : lexDeclarations) {
        assert !(d instanceof HoistableDeclaration);
        mv.lineInfo(d);
        for (Name dn : BoundNames(d)) {
            BindingOp<GlobalEnvironmentRecord> op = BindingOp.of(envRec, dn);
            if (d.isConstDeclaration()) {
                op.createImmutableBinding(envRec, dn, true, mv);
            } else {
                op.createMutableBinding(envRec, dn, false, mv);
            }
        }
    }
    /* step 16 */
    for (HoistableDeclaration f : functionsToInitialize) {
        Name fn = BoundName(f);
        InstantiateFunctionObject(context, env, f, mv);
        mv.store(fo);
        createGlobalFunctionBinding(envRec, f, fn, fo, false, mv);
    }
    /* step 17 */
    for (Map.Entry<Name, VariableDeclaration> e : declaredVarNames.entrySet()) {
        createGlobalVarBinding(envRec, e.getValue(), e.getKey(), false, mv);
    }
    /* step 18 */
    mv._return();
}
Also used : GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ScriptName(com.github.anba.es6draft.compiler.CodeGenerator.ScriptName) Name(com.github.anba.es6draft.ast.scope.Name) LinkedHashMap(java.util.LinkedHashMap) FunctionDeclaration(com.github.anba.es6draft.ast.FunctionDeclaration) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) VariableDeclaration(com.github.anba.es6draft.ast.VariableDeclaration) FunctionDeclaration(com.github.anba.es6draft.ast.FunctionDeclaration) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) Declaration(com.github.anba.es6draft.ast.Declaration) VariableDeclaration(com.github.anba.es6draft.ast.VariableDeclaration) HashSet(java.util.HashSet) Jump(com.github.anba.es6draft.compiler.assembler.Jump) ArrayDeque(java.util.ArrayDeque) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) VariableStatement(com.github.anba.es6draft.ast.VariableStatement) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) StatementListItem(com.github.anba.es6draft.ast.StatementListItem) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 23 with Jump

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

the class StatementGenerator method CatchClauseEvaluation.

/**
     * Extension: 'catch-if' statement
     */
private Completion CatchClauseEvaluation(GuardedCatchNode node, Jump catchWithGuardedLabel, CodeVisitor mv) {
    /* steps 1-6 */
    enterCatchScope(node, mv);
    /* step 7 */
    Jump l0 = new Jump();
    Completion result;
    ToBoolean(expression(node.getGuard(), mv), mv);
    mv.ifeq(l0);
    {
        result = node.getCatchBlock().accept(this, mv);
        if (!result.isAbrupt()) {
            if (node.getScope().isPresent()) {
                popLexicalEnvironment(mv);
            }
            mv.goTo(catchWithGuardedLabel);
        }
    }
    mv.mark(l0);
    /* step 8 */
    exitCatchScope(node, Completion.Normal, mv);
    /* step 9 */
    return result;
}
Also used : Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 24 with Jump

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

the class StatementGenerator method visit.

/**
     * 13.6 The if Statement
     * <p>
     * 13.6.7 Runtime Semantics: Evaluation
     */
@Override
public Completion visit(IfStatement node, CodeVisitor mv) {
    Bool btest = Bool.evaluate(node.getTest());
    if (btest != Bool.Any) {
        if (node.hasCompletionValue()) {
            mv.storeUndefinedAsCompletionValue();
        }
        if (btest == Bool.True) {
            Completion resultThen = node.getThen().accept(this, mv);
            return resultThen.nonEmpty();
        }
        if (node.getOtherwise() != null) {
            Completion resultOtherwise = node.getOtherwise().accept(this, mv);
            return resultOtherwise.nonEmpty();
        }
        return Completion.Normal;
    }
    /* steps 1-3 */
    ValType type = expression(node.getTest(), mv);
    ToBoolean(type, mv);
    if (node.getOtherwise() != null) {
        // IfStatement : if ( Expression ) Statement else Statement
        Jump l0 = new Jump(), l1 = new Jump();
        /* step 4 */
        mv.ifeq(l0);
        if (node.hasCompletionValue()) {
            // TODO: Emit only when necessary, i.e. 'then' branch does not return a completion value.
            mv.storeUndefinedAsCompletionValue();
        }
        Completion resultThen = node.getThen().accept(this, mv);
        if (!resultThen.isAbrupt()) {
            mv.goTo(l1);
        }
        /* step 5 */
        mv.mark(l0);
        if (node.hasCompletionValue()) {
            // TODO: Emit only when necessary, i.e. 'otherwise' branch does not return a completion value.
            mv.storeUndefinedAsCompletionValue();
        }
        Completion resultOtherwise = node.getOtherwise().accept(this, mv);
        if (!resultThen.isAbrupt()) {
            mv.mark(l1);
        }
        /* steps 6-8 */
        return resultThen.select(resultOtherwise);
    } else {
        // IfStatement : if ( Expression ) Statement
        Jump l0 = new Jump();
        /* step 5 */
        mv.ifeq(l0);
        if (node.hasCompletionValue()) {
            mv.storeUndefinedAsCompletionValue();
        }
        Completion resultThen = node.getThen().accept(this, mv);
        if (node.hasCompletionValue() && mv.hasCompletion()) {
            if (!resultThen.isAbrupt()) {
                Jump l1 = new Jump();
                mv.goTo(l1);
                mv.mark(l0);
                mv.storeUndefinedAsCompletionValue();
                mv.mark(l1);
            } else {
                mv.mark(l0);
                mv.storeUndefinedAsCompletionValue();
            }
        } else {
            mv.mark(l0);
        }
        /* steps 4-5 */
        return resultThen.select(Completion.Normal);
    }
}
Also used : Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 25 with Jump

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

the class StatementGenerator method emitCatchBlock.

private Completion emitCatchBlock(TryStatement node, Variable<LexicalEnvironment<?>> savedEnv, TryCatchLabel handlerCatch, TryCatchLabel handlerCatchStackOverflow, CodeVisitor mv) {
    boolean hasFinally = node.getFinallyBlock() != null;
    CatchNode catchNode = node.getCatchNode();
    List<GuardedCatchNode> guardedCatchNodes = node.getGuardedCatchNodes();
    assert catchNode != null || !guardedCatchNodes.isEmpty();
    // StackOverflowError -> ScriptException
    mv.catchHandler(handlerCatchStackOverflow, Types.Error);
    mv.invoke(Methods.ScriptRuntime_stackOverflowError);
    mv.loadExecutionContext();
    mv.invoke(Methods.ScriptRuntime_toInternalError);
    mv.catchHandler(handlerCatch, Types.ScriptException);
    restoreEnvironment(savedEnv, mv);
    if (hasFinally) {
        mv.enterWrapped();
    }
    Completion catchResult;
    if (!guardedCatchNodes.isEmpty()) {
        mv.enterVariableScope();
        Variable<ScriptException> exception = mv.newVariable("exception", ScriptException.class);
        Jump catchWithGuardedLabel = new Jump();
        mv.store(exception);
        Completion result = null;
        for (GuardedCatchNode guardedCatchNode : guardedCatchNodes) {
            mv.load(exception);
            Completion guardedResult = CatchClauseEvaluation(guardedCatchNode, catchWithGuardedLabel, mv);
            result = result != null ? result.select(guardedResult) : guardedResult;
        }
        assert result != null;
        if (catchNode != null) {
            mv.load(exception);
            catchResult = CatchClauseEvaluation(catchNode, mv);
        } else {
            mv.load(exception);
            mv.athrow();
            catchResult = Completion.Throw;
        }
        if (!result.isAbrupt()) {
            mv.mark(catchWithGuardedLabel);
        }
        mv.exitVariableScope();
        catchResult = catchResult.select(result);
    } else {
        catchResult = CatchClauseEvaluation(catchNode, mv);
    }
    if (hasFinally) {
        mv.exitWrapped();
    }
    return catchResult.nonEmpty();
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

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