nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/Bootstrap.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/Bootstrap.java Mon Nov 03 07:29:46 2014 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/Bootstrap.java Mon Nov 03 09:49:52 2014 +0100
@@ -42,6 +42,8 @@
import jdk.internal.dynalink.linker.GuardedInvocation;
import jdk.internal.dynalink.linker.LinkRequest;
import jdk.internal.dynalink.linker.LinkerServices;
+import jdk.internal.dynalink.linker.MethodTypeConversionStrategy;
+import jdk.internal.dynalink.support.TypeUtilities;
import jdk.nashorn.api.scripting.JSObject;
import jdk.nashorn.internal.codegen.CompilerConstants.Call;
import jdk.nashorn.internal.codegen.ObjectClassGenerator;
@@ -106,6 +108,12 @@
return OptimisticReturnFilters.filterOptimisticReturnValue(inv, desc).asType(linkerServices, desc.getMethodType());
}
});
+ factory.setAutoConversionStrategy(new MethodTypeConversionStrategy() {
+ @Override
+ public MethodHandle asType(final MethodHandle target, final MethodType newType) {
+ return unboxReturnType(target, newType);
+ }
+ });
final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD);
if (relinkThreshold > -1) {
factory.setUnstableRelinkThreshold(relinkThreshold);
@@ -420,4 +428,29 @@
static GuardedInvocation asTypeSafeReturn(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) {
return inv == null ? null : inv.asTypeSafeReturn(linkerServices, desc.getMethodType());
}
+
+ /**
+ * Adapts the return type of the method handle with {@code explicitCastArguments} when it is an unboxing
+ * conversion. This will ensure that nulls are unwrapped to false or 0.
+ * @param target the target method handle
+ * @param newType the desired new type. Note that this method does not adapt the method handle completely to the
+ * new type, it only adapts the return type; this is allowed as per
+ * {@link DynamicLinkerFactory#setAutoConversionStrategy(MethodTypeConversionStrategy)}, which is what this method
+ * is used for.
+ * @return the method handle with adapted return type, if it required an unboxing conversion.
+ */
+ private static MethodHandle unboxReturnType(final MethodHandle target, final MethodType newType) {
+ final MethodType targetType = target.type();
+ final Class<?> oldReturnType = targetType.returnType();
+ if (TypeUtilities.isWrapperType(oldReturnType)) {
+ final Class<?> newReturnType = newType.returnType();
+ if (newReturnType.isPrimitive()) {
+ // The contract of setAutoConversionStrategy is such that the difference between newType and targetType
+ // can only be JLS method invocation conversions.
+ assert TypeUtilities.isMethodInvocationConvertible(oldReturnType, newReturnType);
+ return MethodHandles.explicitCastArguments(target, targetType.changeReturnType(newReturnType));
+ }
+ }
+ return target;
+ }
}