Search in sources :

Example 66 with MethodHandle

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

the class Bootstrap method concatSetup.

private static void concatSetup(MutableCallSite callsite, MethodType type) {
    MethodHandle target, test, generic;
    // CharSequence..., ExecutionContext
    int numberOfStrings = type.parameterCount() - 1;
    if (numberOfStrings <= CONCAT_MAX_SPECIALIZATION) {
        assert numberOfStrings >= CONCAT_MIN_PARAMS;
        int index = numberOfStrings - CONCAT_MIN_PARAMS;
        target = concatMH[index];
        test = testConcatMH[index];
        generic = concatConsMH[index];
        setCallSiteTarget(callsite, target, test, generic);
    } else {
        final int index = CONCAT_MAX_SPECIALIZATION - CONCAT_MIN_PARAMS + 1;
        target = concatMH[index].asCollector(CharSequence[].class, numberOfStrings);
        test = testConcatMH[index].asCollector(CharSequence[].class, numberOfStrings);
        generic = concatConsMH[index].asCollector(CharSequence[].class, numberOfStrings);
    }
    setCallSiteTarget(callsite, target, test, generic);
}
Also used : MethodHandle(java.lang.invoke.MethodHandle)

Example 67 with MethodHandle

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

the class ScriptRuntime method runtimeBootstrap.

public static CallSite runtimeBootstrap(MethodHandles.Lookup caller, String name, MethodType type) {
    assert "rt:stack".equals(name) || "rt:locals".equals(name);
    MethodHandle mh = MethodHandles.identity(Object[].class);
    mh = mh.asCollector(Object[].class, type.parameterCount());
    mh = mh.asType(type);
    return new ConstantCallSite(mh);
}
Also used : ConstantCallSite(java.lang.invoke.ConstantCallSite) GeneratorObject(com.github.anba.es6draft.runtime.objects.iteration.GeneratorObject) TypedArrayObject(com.github.anba.es6draft.runtime.objects.binary.TypedArrayObject) CompiledObject(com.github.anba.es6draft.compiler.CompiledObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 68 with MethodHandle

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

the class NativeTailCallFunction method tailCallAdapter.

private static MethodHandle tailCallAdapter(MethodHandle mh) {
    MethodHandle result = TailCallInvocation.getTailCallHandler();
    result = MethodHandles.dropArguments(result, 2, Object.class, Object[].class);
    result = MethodHandles.dropArguments(result, 1, ExecutionContext.class);
    result = MethodHandles.foldArguments(result, mh);
    return result;
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) MethodHandle(java.lang.invoke.MethodHandle)

Example 69 with MethodHandle

use of java.lang.invoke.MethodHandle in project groovy by apache.

the class TypeTransformers method createSAMTransform.

/**
     * creates a method handle able to transform the given Closure into a SAM type
     * if the given parameter is a SAM type 
     */
private static MethodHandle createSAMTransform(Object arg, Class parameter) {
    Method method = CachedSAMClass.getSAMMethod(parameter);
    if (method == null)
        return null;
    // TODO: have to think about how to optimize this!
    if (parameter.isInterface()) {
        if (Traits.isTrait(parameter)) {
            // the following code will basically do this:
            // Map<String,Closure> impl = Collections.singletonMap(method.getName(),arg);
            // return ProxyGenerator.INSTANCE.instantiateAggregate(impl,Collections.singletonList(clazz));
            // TO_SAMTRAIT_PROXY is a handle (Object,Object,ProxyGenerator,Class)GroovyObject
            // where the second object is the input closure, everything else
            // needs to be provide and is in remaining order: method name,
            // ProxyGenerator.INSTANCE and singletonList(parameter)
            MethodHandle ret = TO_SAMTRAIT_PROXY;
            ret = MethodHandles.insertArguments(ret, 2, ProxyGenerator.INSTANCE, Collections.singletonList(parameter));
            ret = MethodHandles.insertArguments(ret, 0, method.getName());
            return ret;
        }
        // the following code will basically do this:
        // return Proxy.newProxyInstance(
        //        arg.getClass().getClassLoader(),
        //        new Class[]{parameter},
        //        new ConvertedClosure((Closure) arg));
        // TO_REFLECTIVE_PROXY will do that for us, though
        // input is the closure, the method name, the class loader and the 
        // class[]. All of that but the closure must be provided here  
        MethodHandle ret = TO_REFLECTIVE_PROXY;
        ret = MethodHandles.insertArguments(ret, 1, method.getName(), arg.getClass().getClassLoader(), new Class[] { parameter });
        return ret;
    } else {
        // the following code will basically do this:
        //Map<String, Object> m = Collections.singletonMap(method.getName(), arg);
        //return ProxyGenerator.INSTANCE.
        //            instantiateAggregateFromBaseClass(m, parameter);
        // TO_GENERATED_PROXY is a handle (Object,Object,ProxyGenerator,Class)GroovyObject
        // where the second object is the input closure, everything else
        // needs to be provide and is in remaining order: method name, 
        // ProxyGenerator.INSTANCE and parameter
        MethodHandle ret = TO_GENERATED_PROXY;
        ret = MethodHandles.insertArguments(ret, 2, ProxyGenerator.INSTANCE, parameter);
        ret = MethodHandles.insertArguments(ret, 0, method.getName());
        return ret;
    }
}
Also used : CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle)

Example 70 with MethodHandle

use of java.lang.invoke.MethodHandle in project groovy by apache.

the class IndyArrayAccess method buildGetter.

private static MethodHandle buildGetter(Class arrayClass) {
    MethodHandle get = MethodHandles.arrayElementGetter(arrayClass);
    MethodHandle fallback = MethodHandles.explicitCastArguments(get, get.type().changeParameterType(0, Object.class));
    fallback = MethodHandles.dropArguments(fallback, 2, int.class);
    MethodType reorderType = fallback.type().insertParameterTypes(0, int.class).dropParameterTypes(2, 3);
    fallback = MethodHandles.permuteArguments(fallback, reorderType, 1, 0, 0);
    fallback = MethodHandles.foldArguments(fallback, normalizeIndex);
    fallback = MethodHandles.explicitCastArguments(fallback, get.type());
    MethodHandle guard = MethodHandles.dropArguments(notNegative, 0, arrayClass);
    MethodHandle handle = MethodHandles.guardWithTest(guard, get, fallback);
    return handle;
}
Also used : 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