# HG changeset patch # User Doug Simon # Date 1391731715 -3600 # Node ID 3089e9a7cf447eb14f4c6a6c4a2d7814ded0a602 # Parent 3e7fa4fd91995dd9ff61530aa2db26ef628e405b fixed bug in passing primitive arrays through native function handles diff -r 3e7fa4fd9199 -r 3089e9a7cf44 graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/nfi/NativeFunctionInterfaceTest.java --- a/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/nfi/NativeFunctionInterfaceTest.java Thu Feb 06 23:28:16 2014 +0100 +++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/nfi/NativeFunctionInterfaceTest.java Fri Feb 07 01:08:35 2014 +0100 @@ -209,4 +209,34 @@ } assertEquals(Math.pow(3D, 5.5D), result, 0); } + + @Test + public void test8() { + String formatString = "AB %f%f"; + long formatCString = writeCString("AB %f%f", malloc(formatString.length() + 1)); + + String expected = "AB 1.0000001.000000"; + int bufferLength = expected.length() + 1; + byte[] buffer = new byte[bufferLength]; + + NativeFunctionHandle snprintf = ffi.getFunctionHandle("snprintf", int.class, byte[].class, int.class, long.class, double.class, double.class); + int result = (int) snprintf.call(buffer, bufferLength, formatCString, 1.0D, 1.0D); + + // trim trailing '\0' + String actual = new String(buffer, 0, expected.length()); + + assertEquals(expected, actual); + Assert.assertEquals(expected.length(), result); + } + + @Test + public void test9() { + double[] src = {2454.346D, 98789.22D, Double.MAX_VALUE, Double.MIN_NORMAL, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}; + double[] dst = new double[src.length]; + + NativeFunctionHandle memcpy = ffi.getFunctionHandle("memcpy", void.class, double[].class, double[].class, int.class); + memcpy.call(dst, src, src.length * (Double.SIZE / Byte.SIZE)); + + assertArrayEquals(src, dst, 0.0D); + } } diff -r 3e7fa4fd9199 -r 3089e9a7cf44 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nfi/HotSpotNativeFunctionInterface.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nfi/HotSpotNativeFunctionInterface.java Thu Feb 06 23:28:16 2014 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nfi/HotSpotNativeFunctionInterface.java Fri Feb 07 01:08:35 2014 +0100 @@ -27,13 +27,13 @@ import static com.oracle.graal.hotspot.nfi.NativeCallStubGraphBuilder.*; import com.oracle.graal.api.code.*; -import com.oracle.graal.api.code.CallingConvention.*; +import com.oracle.graal.api.code.CallingConvention.Type; import com.oracle.graal.api.meta.*; -import com.oracle.graal.api.meta.ProfilingInfo.*; +import com.oracle.graal.api.meta.ProfilingInfo.TriState; import com.oracle.graal.compiler.*; import com.oracle.graal.compiler.target.*; import com.oracle.graal.debug.*; -import com.oracle.graal.debug.Debug.*; +import com.oracle.graal.debug.Debug.Scope; import com.oracle.graal.hotspot.*; import com.oracle.graal.hotspot.meta.*; import com.oracle.graal.lir.asm.*; @@ -88,7 +88,7 @@ @Override public HotSpotNativeFunctionHandle getFunctionHandle(NativeLibraryHandle library, String name, Class returnType, Class... argumentTypes) { HotSpotNativeFunctionPointer functionPointer = lookupFunctionPointer(name, library, true); - return getFunctionHandle(functionPointer, returnType, argumentTypes); + return createHandle(functionPointer, returnType, argumentTypes); } @Override @@ -116,6 +116,7 @@ if (name == null || library == null) { throw new NullPointerException(); } + if (dllLookupFunctionHandle == null) { dllLookupFunctionHandle = createHandle(functionLookupFunctionPointer, long.class, long.class, long.class); } diff -r 3e7fa4fd9199 -r 3089e9a7cf44 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nfi/HotSpotNativeLibraryHandle.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nfi/HotSpotNativeLibraryHandle.java Thu Feb 06 23:28:16 2014 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nfi/HotSpotNativeLibraryHandle.java Fri Feb 07 01:08:35 2014 +0100 @@ -39,6 +39,20 @@ } @Override + public boolean equals(Object obj) { + if (obj instanceof HotSpotNativeLibraryHandle) { + HotSpotNativeLibraryHandle that = (HotSpotNativeLibraryHandle) obj; + return that.value == value; + } + return false; + } + + @Override + public int hashCode() { + return (int) value; + } + + @Override public String toString() { return name + "@" + value; } diff -r 3e7fa4fd9199 -r 3089e9a7cf44 graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nfi/NativeCallStubGraphBuilder.java --- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nfi/NativeCallStubGraphBuilder.java Thu Feb 06 23:28:16 2014 +0100 +++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/nfi/NativeCallStubGraphBuilder.java Fri Feb 07 01:08:35 2014 +0100 @@ -146,7 +146,7 @@ throw new IllegalArgumentException("Parameter type not supported: " + clazz); } if (componentType.isPrimitive()) { - Kind.fromJavaClass(componentType); + return Kind.fromJavaClass(componentType); } throw new IllegalArgumentException("Parameter type not supported: " + clazz); }