Search in sources :

Example 6 with TryCatchLabel

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

the class FunctionCodeGenerator method generateAsyncFunctionCall.

/**
     * Generate bytecode for:
     * 
     * <pre>
     * calleeContext = newFunctionExecutionContext(function, null, thisValue)
     * function_init(calleeContext, function, arguments)
     * return EvaluateBody(calleeContext, generator)
     * </pre>
     * 
     * @param node
     *            the function node
     * @param mv
     *            the instruction visitor
     */
private void generateAsyncFunctionCall(FunctionNode node, InstructionVisitor mv) {
    Variable<OrdinaryAsyncFunction> function = mv.getParameter(FUNCTION, OrdinaryAsyncFunction.class);
    Variable<Object> thisValue = mv.getParameter(THIS_VALUE, Object.class);
    Variable<Object[]> arguments = mv.getParameter(ARGUMENTS, Object[].class);
    Variable<ExecutionContext> calleeContext = mv.newVariable("calleeContext", ExecutionContext.class);
    // (1) Create a new ExecutionContext
    prepareCallAndBindThis(node, calleeContext, function, thisValue, mv);
    // (2) Perform FunctionDeclarationInstantiation
    {
        TryCatchLabel startCatch = new TryCatchLabel();
        TryCatchLabel endCatch = new TryCatchLabel(), handlerCatch = new TryCatchLabel();
        Jump noException = new Jump();
        mv.mark(startCatch);
        functionDeclarationInstantiation(node, calleeContext, function, arguments, mv);
        mv.goTo(noException);
        mv.mark(endCatch);
        mv.catchHandler(handlerCatch, Types.ScriptException);
        {
            // stack: [exception] -> [cx, exception]
            mv.load(calleeContext);
            mv.swap();
            // stack: [cx, exception] -> [promise]
            mv.invoke(Methods.PromiseAbstractOperations_PromiseOf);
            mv._return();
        }
        mv.mark(noException);
        mv.tryCatch(startCatch, endCatch, handlerCatch, Types.ScriptException);
    }
    // (3) Perform EvaluateBody
    mv.load(calleeContext);
    mv.load(function);
    mv.invoke(Methods.OrdinaryAsyncFunction_EvaluateBody);
    // (4) Return result value
    mv._return();
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 7 with TryCatchLabel

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

the class FunctionCodeGenerator method generateLegacyFunctionCall.

/**
     * Generate bytecode for:
     * 
     * <pre>
     * oldCaller = function.getLegacyCaller()
     * oldArguments = function.getLegacyArguments()
     * function.setLegacyCaller(callerContext.getCurrentFunction())
     * try {
     *   calleeContext = newFunctionExecutionContext(function, null, thisValue)
     *   return OrdinaryCallEvaluateBody(function, argumentsList)
     * } finally {
     *   function.restoreLegacyProperties(oldCaller, oldArguments)
     * }
     * </pre>
     * 
     * @param node
     *            the function node
     * @param mv
     *            the instruction visitor
     */
private void generateLegacyFunctionCall(FunctionNode node, InstructionVisitor mv) {
    final boolean hasArguments = codegen.isEnabled(CompatibilityOption.FunctionArguments);
    final boolean hasCaller = codegen.isEnabled(CompatibilityOption.FunctionCaller);
    Variable<LegacyConstructorFunction> function = mv.getParameter(FUNCTION, LegacyConstructorFunction.class);
    Variable<ExecutionContext> callerContext = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    Variable<Object> thisValue = mv.getParameter(THIS_VALUE, Object.class);
    Variable<Object[]> arguments = mv.getParameter(ARGUMENTS, Object[].class);
    Variable<ExecutionContext> calleeContext = mv.newVariable("calleeContext", ExecutionContext.class);
    Variable<FunctionObject> oldCaller = mv.newVariable("oldCaller", FunctionObject.class);
    Variable<LegacyConstructorFunction.Arguments> oldArguments = mv.newVariable("oldArguments", LegacyConstructorFunction.Arguments.class);
    Variable<Throwable> throwable = mv.newVariable("throwable", Throwable.class);
    // (1) Retrieve 'caller' and 'arguments' and store in local variables
    if (hasCaller) {
        mv.load(function);
        mv.invoke(Methods.LegacyConstructorFunction_getLegacyCaller);
    } else {
        mv.anull();
    }
    mv.store(oldCaller);
    if (hasArguments) {
        mv.load(function);
        mv.invoke(Methods.LegacyConstructorFunction_getLegacyArguments);
    } else {
        mv.anull();
    }
    mv.store(oldArguments);
    // (2) Update 'caller' and 'arguments' properties
    if (hasCaller) {
        setLegacyCaller(function, callerContext, mv);
    }
    if (hasArguments) {
        setLegacyArguments(function, arguments, mv);
    }
    TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
    TryCatchLabel handlerFinally = new TryCatchLabel();
    mv.mark(startFinally);
    {
        // (3) Create a new ExecutionContext
        prepareCallAndBindThis(node, calleeContext, function, thisValue, mv);
        // (4) Call OrdinaryCallEvaluateBody
        ordinaryCallEvaluateBody(node, calleeContext, function, arguments, mv);
        // (5) Restore 'caller' and 'arguments'
        restoreLegacyProperties(function, oldCaller, oldArguments, mv);
        // (6) Return result value
        mv._return();
    }
    mv.mark(endFinally);
    // Exception: Restore 'caller' and 'arguments' and then rethrow exception
    mv.finallyHandler(handlerFinally);
    mv.store(throwable);
    restoreLegacyProperties(function, oldCaller, oldArguments, mv);
    mv.load(throwable);
    mv.athrow();
    mv.tryFinally(startFinally, endFinally, handlerFinally);
}
Also used : TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) LegacyConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)

Example 8 with TryCatchLabel

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

the class StatementGenerator method visitTryFinally.

/**
     * 13.15.8 Runtime Semantics: Evaluation<br>
     * 
     * <code>try-finally</code>
     * 
     * @param node
     *            the try-statement
     * @param mv
     *            the code visitor
     * @return the completion value
     */
private Completion visitTryFinally(TryStatement node, CodeVisitor mv) {
    TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
    TryCatchLabel handlerFinally = new TryCatchLabel();
    TryCatchLabel handlerFinallyStackOverflow = new TryCatchLabel();
    Jump noException = new Jump();
    mv.enterVariableScope();
    Variable<LexicalEnvironment<?>> savedEnv = saveEnvironment(mv);
    MutableValue<Object> completion = mv.enterFinallyScoped(node);
    /* step 1 */
    // Emit try-block
    mv.mark(startFinally);
    Completion tryResult = emitTryBlock(node, noException, mv);
    mv.mark(endFinally);
    // Restore temporary abrupt targets
    List<TempLabel> tempLabels = mv.exitFinallyScoped();
    /* step 2 */
    // Emit finally-block
    Completion finallyResult = emitFinallyBlock(node, savedEnv, completion, tryResult, Completion.Abrupt, handlerFinally, handlerFinallyStackOverflow, noException, tempLabels, mv);
    mv.exitVariableScope();
    mv.tryCatch(startFinally, endFinally, handlerFinally, Types.ScriptException);
    mv.tryCatch(startFinally, endFinally, handlerFinallyStackOverflow, Types.Error);
    /* steps 3-6 */
    return finallyResult.then(tryResult);
}
Also used : LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) Jump(com.github.anba.es6draft.compiler.assembler.Jump) TempLabel(com.github.anba.es6draft.compiler.Labels.TempLabel)

Example 9 with TryCatchLabel

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

the class StatementGenerator method visitTryCatch.

/**
     * 13.15.8 Runtime Semantics: Evaluation<br>
     * 
     * <code>try-catch</code>
     * 
     * @param node
     *            the try-statement
     * @param mv
     *            the code visitor
     * @return the completion value
     */
private Completion visitTryCatch(TryStatement node, CodeVisitor mv) {
    TryCatchLabel startCatch = new TryCatchLabel(), endCatch = new TryCatchLabel();
    TryCatchLabel handlerCatch = new TryCatchLabel();
    TryCatchLabel handlerCatchStackOverflow = new TryCatchLabel();
    Jump exceptionHandled = new Jump();
    mv.enterVariableScope();
    Variable<LexicalEnvironment<?>> savedEnv = saveEnvironment(mv);
    /* step 1 */
    // Emit try-block
    mv.mark(startCatch);
    Completion tryResult = emitTryBlock(node, exceptionHandled, mv);
    mv.mark(endCatch);
    /* step 2 */
    // Emit catch-block
    Completion catchResult = emitCatchBlock(node, savedEnv, handlerCatch, handlerCatchStackOverflow, mv);
    /* step 3 */
    if (!tryResult.isAbrupt()) {
        mv.mark(exceptionHandled);
    }
    mv.exitVariableScope();
    mv.tryCatch(startCatch, endCatch, handlerCatch, Types.ScriptException);
    mv.tryCatch(startCatch, endCatch, handlerCatchStackOverflow, Types.Error);
    /* steps 4-6 */
    return tryResult.select(catchResult);
}
Also used : LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Aggregations

TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)9 Jump (com.github.anba.es6draft.compiler.assembler.Jump)6 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)5 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)5 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4 TempLabel (com.github.anba.es6draft.compiler.Labels.TempLabel)3 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)3 LegacyConstructorFunction (com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)2 Script (com.github.anba.es6draft.ast.Script)1 Completion (com.github.anba.es6draft.compiler.StatementGenerator.Completion)1 Variable (com.github.anba.es6draft.compiler.assembler.Variable)1 Realm (com.github.anba.es6draft.runtime.Realm)1 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)1 Constructor (com.github.anba.es6draft.runtime.types.Constructor)1 OrdinaryAsyncFunction (com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction)1