Search in sources :

Example 1 with TryCatchLabel

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

the class FunctionCodeGenerator method generateLegacyFunctionConstruct.

/**
     * Generate bytecode for:
     * 
     * <pre>
     * oldCaller = function.getLegacyCaller()
     * oldArguments = function.getLegacyArguments()
     * function.setLegacyCaller(callerContext.getCurrentFunction())
     * try {
     *   thisArgument = OrdinaryCreateFromConstructor(callerContext, newTarget, %ObjectPrototype%)
     *   calleeContext = newFunctionExecutionContext(function, newTarget, thisArgument)
     *   result = OrdinaryCallEvaluateBody(function, argumentsList)
     *   return returnResultOrThis(result)
     * } finally {
     *   function.restoreLegacyProperties(oldCaller, oldArguments)
     * }
     * </pre>
     * 
     * @param node
     *            the function node
     * @param mv
     *            the instruction visitor
     */
private void generateLegacyFunctionConstruct(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<Constructor> newTarget = mv.getParameter(NEW_TARGET, Constructor.class);
    Variable<Object[]> arguments = mv.getParameter(ARGUMENTS, Object[].class);
    Variable<ScriptObject> thisArg = mv.newVariable("thisArgument", ScriptObject.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 this-argument
        ordinaryCreateFromConstructor(callerContext, newTarget, thisArg, mv);
        // (4) Create a new ExecutionContext
        prepareCallAndBindThis(node, calleeContext, function, newTarget, thisArg, mv);
        // (5) Call OrdinaryCallEvaluateBody
        ordinaryCallEvaluateBody(node, calleeContext, function, arguments, mv);
        // (6) Restore 'caller' and 'arguments'
        restoreLegacyProperties(function, oldCaller, oldArguments, mv);
        // (7) Return result value
        returnResultOrThis(thisArg, false, mv);
    }
    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 : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Constructor(com.github.anba.es6draft.runtime.types.Constructor) 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 2 with TryCatchLabel

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

the class FunctionCodeGenerator method asyncFunctionCall.

/**
 * Generate bytecode for:
 *
 * <pre>
 * calleeContext = newFunctionExecutionContext(function, null, thisValue)
 * function_init(calleeContext, function, arguments)
 * return EvaluateBody(calleeContext, generator)
 * </pre>
 *
 * @param codegen
 *            the code generator
 * @param node
 *            the function node
 * @param method
 *            the bytecode method
 * @param function
 *            the script function
 */
static void asyncFunctionCall(CodeGenerator codegen, FunctionNode node, MethodCode method, FunctionCode function) {
    callMethod(node, method, mv -> {
        Variable<OrdinaryAsyncFunction> fn = mv.getFunction(OrdinaryAsyncFunction.class);
        Variable<Object> thisValue = mv.getThisValue();
        Variable<Object[]> arguments = mv.getArguments();
        Variable<ExecutionContext> calleeContext = mv.newVariable("calleeContext", ExecutionContext.class);
        // (1) Create a new ExecutionContext
        prepareCallAndBindThis(node, calleeContext, fn, thisValue, mv);
        // (2) Perform FunctionDeclarationInstantiation
        {
            TryCatchLabel startCatch = new TryCatchLabel();
            TryCatchLabel endCatch = new TryCatchLabel(), handlerCatch = new TryCatchLabel();
            Jump noException = new Jump();
            mv.mark(startCatch);
            functionDeclarationInstantiation(function.instantiation, calleeContext, fn, 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(fn);
        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 3 with TryCatchLabel

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

the class StatementGenerator method visitTryCatchFinally.

/**
 * 13.15.8 Runtime Semantics: Evaluation<br>
 *
 * <code>try-catch-finally</code>
 *
 * @param node
 *            the try-statement
 * @param mv
 *            the code visitor
 * @return the completion value
 */
private Completion visitTryCatchFinally(TryStatement node, CodeVisitor mv) {
    TryCatchLabel startCatchFinally = new TryCatchLabel();
    TryCatchLabel endCatch = new TryCatchLabel(), handlerCatch = new TryCatchLabel();
    TryCatchLabel endFinally = new TryCatchLabel(), handlerFinally = new TryCatchLabel();
    TryCatchLabel handlerCatchStackOverflow = 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(startCatchFinally);
    Completion tryResult = emitTryBlock(node, noException, mv);
    mv.mark(endCatch);
    /* steps 2-3 */
    // Emit catch-block
    Completion catchResult = emitCatchBlock(node, savedEnv, handlerCatch, handlerCatchStackOverflow, mv);
    if (!catchResult.isAbrupt()) {
        mv.goTo(noException);
    }
    mv.mark(endFinally);
    // Restore temporary abrupt targets
    List<TempLabel> tempLabels = mv.exitFinallyScoped();
    /* step 4 */
    // Emit finally-block
    Completion finallyResult = emitFinallyBlock(node, savedEnv, completion, tryResult, catchResult, handlerFinally, handlerFinallyStackOverflow, noException, tempLabels, mv);
    mv.exitVariableScope();
    mv.tryCatch(startCatchFinally, endCatch, handlerCatch, Types.ScriptException);
    mv.tryCatch(startCatchFinally, endCatch, handlerCatchStackOverflow, Types.Error);
    mv.tryCatch(startCatchFinally, endFinally, handlerFinally, Types.ScriptException);
    mv.tryCatch(startCatchFinally, endFinally, handlerFinallyStackOverflow, Types.Error);
    /* steps 5-8 */
    return finallyResult.then(tryResult.select(catchResult));
}
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 4 with TryCatchLabel

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

the class AbstractIterationGenerator method generate.

/**
 * Emit code for the wrapped iteration.
 *
 * @param node
 *            the ast node
 * @param iterator
 *            the iterator variable
 * @param target
 *            the target label
 * @param mv
 *            the code visitor
 * @return the completion value
 */
public final Completion generate(NODE node, Variable<ITERATOR> iterator, Jump target, CodeVisitor mv) {
    TryCatchLabel startIteration = new TryCatchLabel(), endIteration = new TryCatchLabel();
    TryCatchLabel handlerCatch = new TryCatchLabel();
    TryCatchLabel handlerCatchStackOverflow = null;
    if (codegen.isEnabled(Compiler.Option.IterationCatchStackOverflow)) {
        handlerCatchStackOverflow = new TryCatchLabel();
    }
    boolean hasTarget = target != null;
    if (!hasTarget) {
        target = new Jump();
    }
    mv.enterVariableScope();
    MutableValue<Object> completion = enterIteration(node, mv);
    // Emit loop body
    mv.mark(startIteration);
    Completion loopBodyResult = iterationBody(node, iterator, mv);
    if (!loopBodyResult.isAbrupt()) {
        mv.goTo(target);
    }
    mv.mark(endIteration);
    // Restore temporary abrupt targets
    List<TempLabel> tempLabels = exitIteration(node, mv);
    // Emit throw handler
    emitThrowHandler(node, iterator, handlerCatch, handlerCatchStackOverflow, mv);
    // Emit return handler
    emitReturnHandler(node, iterator, completion, tempLabels, mv);
    mv.exitVariableScope();
    mv.tryCatch(startIteration, endIteration, handlerCatch, Types.ScriptException);
    if (handlerCatchStackOverflow != null) {
        mv.tryCatch(startIteration, endIteration, handlerCatchStackOverflow, Types.Error);
    }
    if (!hasTarget) {
        mv.mark(target);
        epilogue(node, iterator, mv);
    }
    return loopBodyResult;
}
Also used : Completion(com.github.anba.es6draft.compiler.StatementGenerator.Completion) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) Jump(com.github.anba.es6draft.compiler.assembler.Jump) TempLabel(com.github.anba.es6draft.compiler.Labels.TempLabel)

Example 5 with TryCatchLabel

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

the class ScriptCodeGenerator method generateGlobalScriptEvaluation.

/**
     * 15.1.7 Runtime Semantics: ScriptEvaluation
     * 
     * @param node
     *            the script node
     * @param mv
     *            the instruction visitor
     */
private void generateGlobalScriptEvaluation(Script node, InstructionVisitor mv) {
    Variable<ExecutionContext> callerContext = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    Variable<com.github.anba.es6draft.Script> script = mv.getParameter(SCRIPT, com.github.anba.es6draft.Script.class);
    Variable<Realm> realm = mv.newVariable("realm", Realm.class);
    Variable<ExecutionContext> scriptCxt = mv.newVariable("scriptCxt", ExecutionContext.class);
    Variable<ExecutionContext> oldScriptContext = mv.newVariable("oldScriptContext", ExecutionContext.class);
    Variable<Object> result = mv.newVariable("result", Object.class);
    Variable<Throwable> throwable = mv.newVariable("throwable", Throwable.class);
    getRealm(callerContext, realm, mv);
    /* steps 1-2 (not applicable) */
    /* steps 3-7 */
    newScriptExecutionContext(realm, script, scriptCxt, mv);
    /* step 8 */
    getScriptContext(realm, oldScriptContext, mv);
    /* step 9 */
    setScriptContext(realm, scriptCxt, mv);
    TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
    TryCatchLabel handlerFinally = new TryCatchLabel();
    mv.mark(startFinally);
    {
        /* step 10 */
        mv.load(scriptCxt);
        mv.invoke(codegen.methodDesc(node, ScriptName.Init));
        /* steps 11-12 */
        mv.load(scriptCxt);
        mv.invoke(codegen.methodDesc(node, ScriptName.Code));
        mv.store(result);
        /* steps 13-15  */
        setScriptContext(realm, oldScriptContext, mv);
        /* step 16 */
        mv.load(result);
        mv._return();
    }
    mv.mark(endFinally);
    // Exception: Restore script context and then rethrow exception
    mv.finallyHandler(handlerFinally);
    mv.store(throwable);
    /* steps 13-15 */
    setScriptContext(realm, oldScriptContext, mv);
    mv.load(throwable);
    mv.athrow();
    mv.tryFinally(startFinally, endFinally, handlerFinally);
}
Also used : Script(com.github.anba.es6draft.ast.Script) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Realm(com.github.anba.es6draft.runtime.Realm)

Aggregations

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