}
private static MethodHandle createNativeGWT(DynamicMethod.NativeCall nativeCall, MethodHandle test, MethodHandle fallback, CacheEntry entry, JRubyCallSite site) {
try {
boolean isStatic = nativeCall.isStatic();
MethodHandle nativeTarget;
if (isStatic) {
nativeTarget = MethodHandles.lookup().findStatic(
nativeCall.getNativeTarget(),
nativeCall.getNativeName(),
MethodType.methodType(nativeCall.getNativeReturn(),
nativeCall.getNativeSignature()));
} else {
nativeTarget = MethodHandles.lookup().findVirtual(
nativeCall.getNativeTarget(),
nativeCall.getNativeName(),
MethodType.methodType(nativeCall.getNativeReturn(),
nativeCall.getNativeSignature()));
}
int argCount = getArgCount(nativeCall.getNativeSignature(), nativeCall.isStatic());
switch (argCount) {
case 0:
nativeTarget = MethodHandles.permuteArguments(nativeTarget, site.type(), isStatic ? new int[] {0, 2} : new int[] {2, 0});
break;
case -1:
case 1:
nativeTarget = MethodHandles.permuteArguments(nativeTarget, site.type(), isStatic ? new int[] {0, 2, 4} : new int[] {2, 0, 4});
break;
case 2:
nativeTarget = MethodHandles.permuteArguments(nativeTarget, site.type(), isStatic ? new int[] {0, 2, 4, 5} : new int[] {2, 0, 4, 5});
break;
case 3:
nativeTarget = MethodHandles.permuteArguments(nativeTarget, site.type(), isStatic ? new int[] {0, 2, 4, 5, 6} : new int[] {2, 0, 4, 5, 6});
break;
default:
throw new RuntimeException("unknown arg count: " + argCount);
}
MethodHandle myFallback = MethodHandles.insertArguments(fallback, 0, site);
MethodHandle myTest = MethodHandles.insertArguments(test, 0, entry.token);
MethodHandle gwt = MethodHandles.guardWithTest(myTest, nativeTarget, myFallback);
return MethodHandles.convertArguments(gwt, site.type());
} catch (Exception e) {
throw new RuntimeException(e);
}
}