Search in sources :

Example 6 with NativeSymbol

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

the class StructTests1 method test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_1.

@Test
public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
    GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray_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_SHORT, 0, (short) 111);
        structSegmt.set(JAVA_SHORT, 2, (short) 222);
        structSegmt.set(JAVA_SHORT, 4, (short) 333);
        structSegmt.set(JAVA_SHORT, 6, (short) 444);
        structSegmt.set(JAVA_SHORT, 8, (short) 555);
        short result = (short) mh.invokeExact((short) 666, structSegmt);
        Assert.assertEquals(result, 2331);
    }
}
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)

Example 7 with NativeSymbol

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

the class StructTests1 method test_addShortAndShortsFromNestedStruct_reverseOrder_1.

@Test
public void test_addShortAndShortsFromNestedStruct_reverseOrder_1() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct_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_SHORT, 0, (short) 31);
        structSegmt.set(JAVA_SHORT, 2, (short) 33);
        structSegmt.set(JAVA_SHORT, 4, (short) 35);
        short result = (short) mh.invokeExact((short) 37, structSegmt);
        Assert.assertEquals(result, 136);
    }
}
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 8 with NativeSymbol

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

the class MultiCallTests method test_twoCallsWithDiffReturnType.

@Test
public void test_twoCallsWithDiffReturnType() 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.ofVoid(JAVA_INT, JAVA_INT);
    NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add2IntsReturnVoid").get();
    mh = clinker.downcallHandle(functionSymbol2, fd2);
    mh.invokeExact(454, 398);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 9 with NativeSymbol

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

the class MultiCallTests method test_multiCallsWithMixedFuncDescriptors.

@Test
public void test_multiCallsWithMixedFuncDescriptors() 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);
    FunctionDescriptor fd3 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT);
    NativeSymbol functionSymbol3 = nativeLibLookup.lookup("add2IntsReturnVoid").get();
    mh = clinker.downcallHandle(functionSymbol3, fd3);
    mh.invokeExact(454, 398);
    mh = clinker.downcallHandle(functionSymbol1, fd1);
    result = (int) mh.invokeExact(234, 567);
    Assert.assertEquals(result, 801);
    mh = clinker.downcallHandle(functionSymbol2, fd2);
    result = (int) mh.invokeExact(312, 323, 334);
    Assert.assertEquals(result, 969);
    mh = clinker.downcallHandle(functionSymbol3, fd3);
    mh.invokeExact(539, 672);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 10 with NativeSymbol

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

the class MultiCallTests method test_multiCallsWithSameArgLayouts.

@Test
public void test_multiCallsWithSameArgLayouts() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT);
    NativeSymbol functionSymbol1 = nativeLibLookup.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol1, fd);
    int intResult = (int) mh.invokeExact(112, 123);
    Assert.assertEquals(intResult, 235);
    mh = clinker.downcallHandle(functionSymbol1, fd);
    intResult = (int) mh.invokeExact(234, 567);
    Assert.assertEquals(intResult, 801);
    FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT);
    NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add2IntsReturnVoid").get();
    mh = clinker.downcallHandle(functionSymbol2, fd2);
    mh.invokeExact(454, 398);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) 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