Search in sources :

Example 81 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class PrimitiveTypeTests method test_addLongAndLongFromPointer.

@Test
public void test_addLongAndLongFromPointer() throws Throwable {
    MethodType mt = MethodType.methodType(long.class, MemoryAddress.class, long.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER, longLayout);
    Symbol functionSymbol = nativeLib.lookup("addLongAndLongFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment longSegmt = MemorySegment.allocateNative(longLayout);
    MemoryAccess.setLong(longSegmt, 57424L);
    long result = (long) mh.invokeExact(longSegmt.address(), 698235L);
    longSegmt.close();
    Assert.assertEquals(result, 755659L);
    FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), C_POINTER.withName("int"), longLayout.withName("long"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    longSegmt = MemorySegment.allocateNative(longLayout.withName("long"));
    MemoryAccess.setLong(longSegmt, 11111L);
    result = (long) mh.invokeExact(longSegmt.address(), 22222L);
    longSegmt.close();
    Assert.assertEquals(result, 33333L);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 82 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class InvalidDownCallTests method test_mismatchedBoolArgWithShortLayout.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*")
public void test_mismatchedBoolArgWithShortLayout() throws Throwable {
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_SHORT, C_CHAR);
    Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    fail("Failed to throw out IllegalArgumentException in the case of the mismatched layout");
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 83 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class InvalidDownCallTests method test_mismatchedIntArgTypeWithLongLayout.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*")
public void test_mismatchedIntArgTypeWithLongLayout() throws Throwable {
    MethodType mt = MethodType.methodType(long.class, int.class, long.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Longs").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    fail("Failed to throw out IllegalArgumentException in the case of the mismatched int type");
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 84 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class InvalidDownCallTests method test_invalidBooleanTypeOnReturn.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*")
public void test_invalidBooleanTypeOnReturn() throws Throwable {
    MethodType mt = MethodType.methodType(Boolean.class, boolean.class, boolean.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
    Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    fail("Failed to throw out IllegalArgumentException in the case of the Boolean return type");
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 85 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class InvalidDownCallTests method test_mismatchedCharArgWithCharLayout.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*")
public void test_mismatchedCharArgWithCharLayout() throws Throwable {
    MethodType mt = MethodType.methodType(char.class, char.class, char.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_CHAR, C_SHORT);
    Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFrom2Chars").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    fail("Failed to throw out IllegalArgumentException in the case of the mismatched layout");
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)939 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)939 Test (org.testng.annotations.Test)939 MemorySegment (jdk.incubator.foreign.MemorySegment)754 GroupLayout (jdk.incubator.foreign.GroupLayout)675 MethodType (java.lang.invoke.MethodType)605 ResourceScope (jdk.incubator.foreign.ResourceScope)549 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)541 Addressable (jdk.incubator.foreign.Addressable)432 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 VarHandle (java.lang.invoke.VarHandle)318 SequenceLayout (jdk.incubator.foreign.SequenceLayout)240 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 MemoryAddress (jdk.incubator.foreign.MemoryAddress)127 VaList (jdk.incubator.foreign.VaList)4