Search in sources :

Example 11 with NativeSymbol

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

the class MultiCallTests method test_twoCallsWithDiffFuncDescriptor.

@Test
public void test_twoCallsWithDiffFuncDescriptor() throws Throwable {
    FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT);
    NativeSymbol functionSymbol1 = nativeLibLookup.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol1, fd1);
    int result = (int) mh.invokeExact(112, 123);
    Assert.assertEquals(result, 235);
    FunctionDescriptor fd2 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT);
    NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add3Ints").get();
    mh = clinker.downcallHandle(functionSymbol2, fd2);
    result = (int) mh.invokeExact(112, 123, 235);
    Assert.assertEquals(result, 470);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 12 with NativeSymbol

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

the class MultiCallTests method test_twoCallsWithSameFuncDescriptor.

@Test
public void test_twoCallsWithSameFuncDescriptor() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    int result = (int) mh.invokeExact(112, 123);
    Assert.assertEquals(result, 235);
    mh = clinker.downcallHandle(functionSymbol, fd);
    result = (int) mh.invokeExact(235, 439);
    Assert.assertEquals(result, 674);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 13 with NativeSymbol

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

the class MultiThreadingTests2 method test_twoThreadsWithDiffFuncDescriptor.

@Test
public void test_twoThreadsWithDiffFuncDescriptor() throws Throwable {
    Thread thr1 = new Thread() {

        public void run() {
            try {
                FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT);
                NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
                int result = (int) mh.invokeExact(112, 123);
                Assert.assertEquals(result, 235);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    Thread thr2 = new Thread() {

        public void run() {
            try {
                FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT);
                NativeSymbol functionSymbol = nativeLibLookup.lookup("add3Ints").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
                int result = (int) mh.invokeExact(112, 123, 235);
                Assert.assertEquals(result, 470);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    thr1.setUncaughtExceptionHandler(this);
    thr2.setUncaughtExceptionHandler(this);
    thr1.start();
    thr2.start();
    thr1.join();
    thr2.join();
    if (initException != null) {
        throw new RuntimeException(initException);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 14 with NativeSymbol

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

the class StructTests1 method test_addDoubleAndDoublesFromNestedStruct_1.

@Test
public void test_addDoubleAndDoublesFromNestedStruct_1() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_DOUBLE.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_DOUBLE, 0, 31.789D);
        structSegmt.set(JAVA_DOUBLE, 8, 33.456D);
        structSegmt.set(JAVA_DOUBLE, 16, 35.123D);
        double result = (double) mh.invokeExact(37.864D, structSegmt);
        Assert.assertEquals(result, 138.232D, 0.001D);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 15 with NativeSymbol

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

the class StructTests1 method test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder_1.

@Test
public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
    GroupLayout byteStruct = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, byteStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BYTE.bitSize() * 3));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_BYTE, 0, (byte) 12);
        structSegmt.set(JAVA_BYTE, 1, (byte) 14);
        structSegmt.set(JAVA_BYTE, 2, (byte) 16);
        structSegmt.set(JAVA_BYTE, 3, (byte) 18);
        structSegmt.set(JAVA_BYTE, 4, (byte) 20);
        byte result = (byte) mh.invokeExact((byte) 22, structSegmt);
        Assert.assertEquals(result, 102);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) 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)334 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)334 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 Test (org.testng.annotations.Test)334 MemorySegment (jdk.incubator.foreign.MemorySegment)291 ResourceScope (jdk.incubator.foreign.ResourceScope)274 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)271 GroupLayout (jdk.incubator.foreign.GroupLayout)270 VarHandle (java.lang.invoke.VarHandle)126 SequenceLayout (jdk.incubator.foreign.SequenceLayout)96 MemoryAddress (jdk.incubator.foreign.MemoryAddress)34 VaList (jdk.incubator.foreign.VaList)4