Search in sources :

Example 91 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class PrimitiveTypeTests method test_addByteAndByteFromPointer.

@Test
public void test_addByteAndByteFromPointer() throws Throwable {
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_POINTER);
    Symbol functionSymbol = nativeLib.lookup("addByteAndByteFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment charSegmt = MemorySegment.allocateNative(C_CHAR);
    MemoryAccess.setByte(charSegmt, (byte) 3);
    byte result = (byte) mh.invokeExact((byte) 6, charSegmt.address());
    charSegmt.close();
    Assert.assertEquals(result, (byte) 9);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_CHAR.withName("char"), C_CHAR.withName("char"), C_POINTER.withName("pointer"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    charSegmt = MemorySegment.allocateNative(C_CHAR);
    MemoryAccess.setByte(charSegmt, (byte) 7);
    result = (byte) mh.invokeExact((byte) 8, charSegmt.address());
    charSegmt.close();
    Assert.assertEquals(result, (byte) 15);
}
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 92 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName.

@Test
public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName() throws Throwable {
    SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT);
    GroupLayout structLayout = MemoryLayout.ofStruct(intArray, C_INT);
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedIntArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111);
    MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222);
    MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333);
    int result = (int) mh.invokeExact(4444444, structSegmt);
    Assert.assertEquals(result, 11111110);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 93 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_add2LongStructs_returnStruct.

@Test
public void test_add2LongStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2"));
    VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
    VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2LongStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    longHandle1.set(structSegmt1, 987654321987L);
    longHandle2.set(structSegmt1, 123456789123L);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    longHandle1.set(structSegmt2, 224466880022L);
    longHandle2.set(structSegmt2, 113355779911L);
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(longHandle1.get(resultSegmt), 1212121202009L);
    Assert.assertEquals(longHandle2.get(resultSegmt), 236812569034L);
    structSegmt1.close();
    structSegmt2.close();
    resultSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 94 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addShortAndShortsFromStructWithNestedStructArray_withoutLayoutName.

@Test
public void test_addShortAndShortsFromStructWithNestedStructArray_withoutLayoutName() throws Throwable {
    GroupLayout shortStruct = MemoryLayout.ofStruct(C_SHORT, C_SHORT);
    SequenceLayout structArray = MemoryLayout.ofSequence(2, shortStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_SHORT);
    MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setShortAtOffset(structSegmt, 0, (short) 111);
    MemoryAccess.setShortAtOffset(structSegmt, 2, (short) 222);
    MemoryAccess.setShortAtOffset(structSegmt, 4, (short) 333);
    MemoryAccess.setShortAtOffset(structSegmt, 6, (short) 444);
    MemoryAccess.setShortAtOffset(structSegmt, 8, (short) 555);
    short result = (short) mh.invokeExact((short) 666, structSegmt);
    Assert.assertEquals(result, 2331);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 95 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addIntAndIntsFromNestedStruct_withoutLayoutName.

@Test
public void test_addIntAndIntsFromNestedStruct_withoutLayoutName() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_INT, C_INT);
    GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout, C_INT);
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setIntAtOffset(structSegmt, 0, 21222324);
    MemoryAccess.setIntAtOffset(structSegmt, 4, 25262728);
    MemoryAccess.setIntAtOffset(structSegmt, 8, 29303132);
    int result = (int) mh.invokeExact(33343536, structSegmt);
    Assert.assertEquals(result, 109131720);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)173 MethodType (java.lang.invoke.MethodType)173 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)173 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 Test (org.testng.annotations.Test)173 MemorySegment (jdk.incubator.foreign.MemorySegment)149 GroupLayout (jdk.incubator.foreign.GroupLayout)135 VarHandle (java.lang.invoke.VarHandle)64 SequenceLayout (jdk.incubator.foreign.SequenceLayout)48 MemoryAddress (jdk.incubator.foreign.MemoryAddress)30