Search in sources :

Example 11 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 scriptInit
 *            the script declaration instantiation method
 * @param scriptBody
 *            the script body method
 * @param mv
 *            the instruction visitor
 */
private static void generateGlobalScriptEvaluation(Script node, MethodName scriptInit, MethodName scriptBody, ScriptEvalVisitor mv) {
    Variable<ExecutionContext> callerContext = mv.getCallerContext();
    Variable<com.github.anba.es6draft.Script> script = mv.getScript();
    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(scriptInit);
        /* steps 11-12 */
        mv.load(scriptCxt);
        mv.invoke(scriptBody);
        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)

Example 12 with TryCatchLabel

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

the class FunctionCodeGenerator method legacyFunctionCall.

/**
 * 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 codegen
 *            the code generator
 * @param node
 *            the function node
 * @param method
 *            the bytecode method
 * @param function
 *            the script function
 */
static void legacyFunctionCall(CodeGenerator codegen, FunctionDefinition node, MethodCode method, FunctionCode function) {
    callMethod(node, method, mv -> {
        final boolean hasArguments = codegen.isEnabled(CompatibilityOption.FunctionArguments);
        final boolean hasCaller = codegen.isEnabled(CompatibilityOption.FunctionCaller);
        Variable<LegacyConstructorFunction> fn = mv.getFunction(LegacyConstructorFunction.class);
        Variable<ExecutionContext> callerContext = mv.getCallerContext();
        Variable<Object> thisValue = mv.getThisValue();
        Variable<Object[]> arguments = mv.getArguments();
        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) {
            getLegacyCaller(fn, mv);
        } else {
            mv.anull();
        }
        mv.store(oldCaller);
        if (hasArguments) {
            getLegacyArguments(fn, mv);
        } else {
            mv.anull();
        }
        mv.store(oldArguments);
        // (2) Update 'caller' and 'arguments' properties
        if (hasCaller) {
            setLegacyCaller(fn, callerContext, mv);
        }
        if (hasArguments) {
            setLegacyArguments(fn, arguments, mv);
        }
        TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
        TryCatchLabel handlerFinally = new TryCatchLabel();
        mv.mark(startFinally);
        {
            // (3) Create a new ExecutionContext
            prepareCallAndBindThis(node, calleeContext, fn, thisValue, mv);
            // (4) Call OrdinaryCallEvaluateBody
            ordinaryCallEvaluateBody(function.instantiation, function.body, calleeContext, fn, arguments, mv);
            // (5) Restore 'caller' and 'arguments'
            restoreLegacyProperties(fn, 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(fn, 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 13 with TryCatchLabel

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

the class FunctionCodeGenerator method legacyFunctionConstruct.

/**
 * 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 codegen
 *            the code generator
 * @param node
 *            the function node
 * @param method
 *            the bytecode method
 * @param function
 *            the script function
 */
static void legacyFunctionConstruct(CodeGenerator codegen, FunctionDefinition node, MethodCode method, FunctionCode function) {
    constructMethod(node, method, mv -> {
        final boolean hasArguments = codegen.isEnabled(CompatibilityOption.FunctionArguments);
        final boolean hasCaller = codegen.isEnabled(CompatibilityOption.FunctionCaller);
        Variable<LegacyConstructorFunction> fn = mv.getFunction(LegacyConstructorFunction.class);
        Variable<ExecutionContext> callerContext = mv.getCallerContext();
        Variable<Constructor> newTarget = mv.getNewTarget();
        Variable<Object[]> arguments = mv.getArguments();
        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) {
            getLegacyCaller(fn, mv);
        } else {
            mv.anull();
        }
        mv.store(oldCaller);
        if (hasArguments) {
            getLegacyArguments(fn, mv);
        } else {
            mv.anull();
        }
        mv.store(oldArguments);
        // (2) Update 'caller' and 'arguments' properties
        if (hasCaller) {
            setLegacyCaller(fn, callerContext, mv);
        }
        if (hasArguments) {
            setLegacyArguments(fn, 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, fn, newTarget, thisArg, mv);
            // (5) Call OrdinaryCallEvaluateBody
            ordinaryCallEvaluateBody(function.instantiation, function.body, calleeContext, fn, arguments, mv);
            // (6) Restore 'caller' and 'arguments'
            restoreLegacyProperties(fn, 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(fn, 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) LegacyConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)

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