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);
}
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);
}
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;
}
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;
}
}
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;
}
Aggregations