Search in sources :

Example 61 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class Properties method toCanonical.

private static MethodHandle toCanonical(MethodHandle handle, int fixedArguments, boolean varargs, Method method) {
    assert !handle.isVarargsCollector();
    MethodType type = handle.type();
    int actual = type.parameterCount() - fixedArguments - (varargs ? 1 : 0);
    Object[] defaults = method != null ? methodDefaults(method, fixedArguments, actual) : null;
    MethodHandle filter = Parameters.filter(actual, varargs, defaults);
    MethodHandle spreader = MethodHandles.spreadInvoker(type, fixedArguments);
    spreader = MethodHandles.insertArguments(spreader, 0, handle);
    spreader = MethodHandles.filterArguments(spreader, fixedArguments, filter);
    return spreader;
}
Also used : MethodType(java.lang.invoke.MethodType) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 62 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class Bootstrap method addSetup.

@SuppressWarnings("unused")
private static MethodHandle addSetup(MutableCallSite callsite, Object arg1, Object arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.String) {
        target = addStringMH;
    } else if (type == Type.Number) {
        target = addNumberMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestFor(type), addGenericMH);
}
Also used : Type(com.github.anba.es6draft.runtime.types.Type) MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 63 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class Bootstrap method bootstrapDynamic.

/**
     * The invokedynamic bootstrapping method.
     * 
     * @param caller
     *            the caller lookup
     * @param name
     *            the instruction name
     * @param type
     *            the expected method type
     * @return the invokedynamic call-site object
     */
public static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
    // System.out.printf("type: %s\n", type);
    try {
        MutableCallSite callsite = new MutableCallSite(type);
        MethodHandle setup;
        switch(name) {
            case CallNames.CALL:
                setup = MethodHandles.insertArguments(callSetupMH, 0, callsite);
                break;
            case CallNames.CONSTRUCT:
                setup = MethodHandles.insertArguments(constructSetupMH, 0, callsite);
                break;
            case CallNames.SUPER:
                setup = MethodHandles.insertArguments(superSetupMH, 0, callsite);
                break;
            case CallNames.ADD:
                setup = MethodHandles.insertArguments(addSetupMH, 0, callsite);
                break;
            case CallNames.EQ:
                setup = MethodHandles.insertArguments(eqCmpSetupMH, 0, callsite);
                break;
            case CallNames.SHEQ:
                setup = MethodHandles.insertArguments(strictEqCmpSetupMH, 0, callsite);
                break;
            case CallNames.LT:
                setup = MethodHandles.insertArguments(relCmpSetupMH, 0, callsite, RelationalOperator.LessThan);
                break;
            case CallNames.GT:
                setup = MethodHandles.insertArguments(relCmpSetupMH, 0, callsite, RelationalOperator.GreaterThan);
                break;
            case CallNames.LE:
                setup = MethodHandles.insertArguments(relCmpSetupMH, 0, callsite, RelationalOperator.LessThanEquals);
                break;
            case CallNames.GE:
                setup = MethodHandles.insertArguments(relCmpSetupMH, 0, callsite, RelationalOperator.GreaterThanEquals);
                break;
            case CallNames.CONCAT:
                concatSetup(callsite, type);
                return callsite;
            default:
                throw new IllegalArgumentException(name);
        }
        callsite.setTarget(setupCallSiteTarget(type, setup));
        return callsite;
    } catch (StackOverflowError e) {
        switch(name) {
            case CallNames.CALL:
                return stackOverFlow_Call;
            case CallNames.CONSTRUCT:
                return stackOverFlow_Construct;
            case CallNames.SUPER:
                return stackOverFlow_Super;
            case CallNames.CONCAT:
                return new ConstantCallSite(MethodHandles.dropArguments(stackOverFlow_Concat, 0, type.parameterArray()));
            case CallNames.ADD:
                return stackOverFlow_Add;
            case CallNames.EQ:
                return stackOverFlow_Eq;
            case CallNames.SHEQ:
                return stackOverFlow_StrictEq;
            case CallNames.LT:
            case CallNames.GT:
            case CallNames.LE:
            case CallNames.GE:
                return stackOverFlow_Cmp;
            default:
                throw new IllegalArgumentException(name);
        }
    }
}
Also used : MutableCallSite(java.lang.invoke.MutableCallSite) ConstantCallSite(java.lang.invoke.ConstantCallSite) MethodHandle(java.lang.invoke.MethodHandle)

Example 64 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class Bootstrap method callSetup.

@SuppressWarnings("unused")
private static MethodHandle callSetup(MutableCallSite callsite, Object function, ExecutionContext cx, Object thisValue, Object[] arguments) {
    MethodHandle target, test;
    if (function instanceof FunctionObject) {
        FunctionObject fn = (FunctionObject) function;
        test = MethodHandles.insertArguments(testFunctionObjectMH, 1, fn.getMethodInfo());
        target = fn.getCallMethod();
    } else if (function instanceof BuiltinFunction) {
        BuiltinFunction fn = (BuiltinFunction) function;
        test = MethodHandles.insertArguments(testBuiltinFunctionMH, 1, fn.getMethodInfo());
        target = fn.getCallMethod();
    } else {
        target = test = null;
    }
    return setCallSiteTarget(callsite, target, test, callGenericMH);
}
Also used : BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 65 with MethodHandle

use of java.lang.invoke.MethodHandle in project es6draft by anba.

the class Bootstrap method strictEqCmpSetup.

@SuppressWarnings("unused")
private static MethodHandle strictEqCmpSetup(MutableCallSite callsite, Object arg1, Object arg2) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.String) {
        target = strictEqCmpStringMH;
    } else if (type == Type.Number) {
        target = strictEqCmpNumberMH;
    } else if (type == Type.Boolean) {
        target = strictEqCmpBooleanMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestFor(type), strictEqCmpGenericMH);
}
Also used : Type(com.github.anba.es6draft.runtime.types.Type) MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)302 Test (org.junit.Test)101 MethodType (java.lang.invoke.MethodType)44 Type (com.facebook.presto.spi.type.Type)37 Method (java.lang.reflect.Method)18 OperatorType (com.facebook.presto.spi.function.OperatorType)14 MethodHandles (java.lang.invoke.MethodHandles)13 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)11 Signature (com.facebook.presto.metadata.Signature)10 CallSite (java.lang.invoke.CallSite)10 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)9 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)8 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)7 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 Parameter (com.facebook.presto.bytecode.Parameter)6 PrestoException (com.facebook.presto.spi.PrestoException)6 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)6 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)6