Search in sources :

Example 31 with Addressable

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

the class InvalidDownCallTests method test_invalidByteTypeOnReturn.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The return type must be .*")
public void test_invalidByteTypeOnReturn() throws Throwable {
    MethodType mt = MethodType.methodType(Byte.class, byte.class, byte.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Bytes").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    fail("Failed to throw out IllegalArgumentException in the case of the Byte 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 32 with Addressable

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

the class InvalidDownCallTests method test_mismatchedArity.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".* inconsistent with the arity .*")
public void test_mismatchedArity() throws Throwable {
    MethodType mt = MethodType.methodType(int.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    fail("Failed to throw out IllegalArgumentException in the case of the mismatched arity");
}
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 33 with Addressable

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

the class PrimitiveTypeTests2 method test_addFloatAndFloatFromPointer_2.

@Test
public void test_addFloatAndFloatFromPointer_2() throws Throwable {
    MethodType mt = MethodType.methodType(float.class, float.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_POINTER);
    Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    MemorySegment floatSegmt = MemorySegment.allocateNative(C_FLOAT, resourceScope);
    MemoryAccess.setFloat(floatSegmt, 6.79f);
    float result = (float) mh.invokeExact(5.74f, floatSegmt.address());
    Assert.assertEquals(result, 12.53f, 0.01f);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 34 with Addressable

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

the class StructTests1 method test_addByteAndBytesFromNestedStruct_1.

@Test
public void test_addByteAndBytesFromNestedStruct_1() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), C_CHAR.withName("elem2"));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 11);
        MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 22);
        MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 33);
        byte result = (byte) mh.invokeExact((byte) 46, structSegmt);
        Assert.assertEquals(result, 112);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 35 with Addressable

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

the class StructTests1 method test_addByteAndBytesFromStructWithNestedByteArray_1.

@Test
public void test_addByteAndBytesFromStructWithNestedByteArray_1() throws Throwable {
    SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, C_CHAR);
    GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), C_CHAR.withName("elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 11);
        MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 12);
        MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 13);
        byte result = (byte) mh.invokeExact((byte) 14, structSegmt);
        Assert.assertEquals(result, 50);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) SequenceLayout(jdk.incubator.foreign.SequenceLayout) 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