Search in sources :

Example 6 with LegacyConstructorFunction

use of com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction 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 7 with LegacyConstructorFunction

use of com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction 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 8 with LegacyConstructorFunction

use of com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction 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

LegacyConstructorFunction (com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)8 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)6 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)5 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)4 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)2 Constructor (com.github.anba.es6draft.runtime.types.Constructor)2 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)1 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)1 Property (com.github.anba.es6draft.runtime.types.Property)1 OrdinaryAsyncFunction (com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction)1 OrdinaryAsyncGenerator (com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncGenerator)1 OrdinaryConstructorFunction (com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction)1 OrdinaryGenerator (com.github.anba.es6draft.runtime.types.builtins.OrdinaryGenerator)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1