Search in sources :

Example 56 with Symbol

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

the class PrimitiveTypeTests method test_addDoubleAndDoubleFromPointer.

@Test
public void test_addDoubleAndDoubleFromPointer() throws Throwable {
    MethodType mt = MethodType.methodType(double.class, MemoryAddress.class, double.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_DOUBLE);
    Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoubleFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment doubleSegmt = MemorySegment.allocateNative(C_DOUBLE);
    MemoryAccess.setDouble(doubleSegmt, 159.748d);
    double result = (double) mh.invokeExact(doubleSegmt.address(), 262.795d);
    doubleSegmt.close();
    Assert.assertEquals(result, 422.543d, 0.001d);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_DOUBLE.withName("double"), C_POINTER.withName("pointer"), C_DOUBLE.withName("double"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    doubleSegmt = MemorySegment.allocateNative(C_DOUBLE.withName("double"));
    MemoryAccess.setDouble(doubleSegmt, 1159.748d);
    result = (double) mh.invokeExact(doubleSegmt.address(), 1262.795d);
    doubleSegmt.close();
    Assert.assertEquals(result, 2422.543d, 0.001d);
}
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 57 with Symbol

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

the class PrimitiveTypeTests method test_addTwoBytes.

@Test
public void test_addTwoBytes() throws Throwable {
    MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
    Symbol functionSymbol = nativeLib.lookup("add2Bytes").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    byte result = (byte) mh.invokeExact((byte) 6, (byte) 3);
    Assert.assertEquals(result, (byte) 9);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_CHAR.withName("char"), C_CHAR.withName("char"), C_CHAR.withName("char"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    result = (byte) mh.invokeExact((byte) 8, (byte) 9);
    Assert.assertEquals(result, (byte) 17);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 58 with Symbol

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

the class PrimitiveTypeTests method test_printfFromDefaultLibWithMemAddr.

@Test
public void test_printfFromDefaultLibWithMemAddr() throws Throwable {
    Symbol functionSymbol = defaultLib.lookup("printf").get();
    MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, int.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_INT, C_INT, C_INT);
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment formatMemSegment = CLinker.toCString("\n%d + %d = %d\n");
    mh.invoke(formatMemSegment.address(), 15, 27, 42);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_POINTER.withName("pointer"), C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    mh.invoke(formatMemSegment.address(), 115, 127, 242);
}
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 59 with Symbol

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

the class PrimitiveTypeTests method test_addLongAndLongFromPointer.

@Test
public void test_addLongAndLongFromPointer() throws Throwable {
    MethodType mt = MethodType.methodType(long.class, MemoryAddress.class, long.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER, longLayout);
    Symbol functionSymbol = nativeLib.lookup("addLongAndLongFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment longSegmt = MemorySegment.allocateNative(longLayout);
    MemoryAccess.setLong(longSegmt, 57424L);
    long result = (long) mh.invokeExact(longSegmt.address(), 698235L);
    longSegmt.close();
    Assert.assertEquals(result, 755659L);
    FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), C_POINTER.withName("int"), longLayout.withName("long"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    longSegmt = MemorySegment.allocateNative(longLayout.withName("long"));
    MemoryAccess.setLong(longSegmt, 11111L);
    result = (long) mh.invokeExact(longSegmt.address(), 22222L);
    longSegmt.close();
    Assert.assertEquals(result, 33333L);
}
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 60 with Symbol

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

the class StructTests method test_addByteFromPointerAndBytesFromStruct.

@Test
public void test_addByteFromPointerAndBytesFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(byte.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addByteFromPointerAndBytesFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment byteSegmt = MemorySegment.allocateNative(C_CHAR);
    MemoryAccess.setByte(byteSegmt, (byte) 12);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt, (byte) 14);
    byteHandle2.set(structSegmt, (byte) 16);
    byte result = (byte) mh.invokeExact(byteSegmt.address(), structSegmt);
    Assert.assertEquals(result, 42);
    byteSegmt.close();
    structSegmt.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)

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