Search in sources :

Example 21 with Addressable

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

the class PrimitiveTypeTests2 method test_addTwoBoolsWithOr_fromMemAddr_2.

@Test
public void test_addTwoBoolsWithOr_fromMemAddr_2() 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();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
    boolean result = (boolean) mh.invokeExact(true, false);
    Assert.assertEquals(result, true);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 22 with Addressable

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

the class PrimitiveTypeTests2 method test_addTwoLongs_fromMemAddr_2.

@Test
public void test_addTwoLongs_fromMemAddr_2() throws Throwable {
    MethodType mt = MethodType.methodType(long.class, long.class, long.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Longs").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
    long result = (long) mh.invokeExact(57424L, 698235L);
    Assert.assertEquals(result, 755659L);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 23 with Addressable

use of jdk.incubator.foreign.Addressable 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 24 with Addressable

use of jdk.incubator.foreign.Addressable 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 25 with Addressable

use of jdk.incubator.foreign.Addressable 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)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)432 MethodType (java.lang.invoke.MethodType)432 Addressable (jdk.incubator.foreign.Addressable)432 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)432 Test (org.testng.annotations.Test)432 MemorySegment (jdk.incubator.foreign.MemorySegment)314 ResourceScope (jdk.incubator.foreign.ResourceScope)275 GroupLayout (jdk.incubator.foreign.GroupLayout)270 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)270 VarHandle (java.lang.invoke.VarHandle)128 SequenceLayout (jdk.incubator.foreign.SequenceLayout)96 MemoryAddress (jdk.incubator.foreign.MemoryAddress)63