Search in sources :

Example 86 with Symbol

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

the class PrimitiveTypeTests method test_generateNewChar_fromMemAddr.

@Test
public void test_generateNewChar_fromMemAddr() throws Throwable {
    MethodType mt = MethodType.methodType(char.class, char.class, char.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT);
    Symbol functionSymbol = nativeLib.lookup("createNewCharFrom2Chars").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
    char result = (char) mh.invokeExact('B', 'D');
    Assert.assertEquals(result, 'C');
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_SHORT.withName("short"), C_SHORT.withName("short"));
    mh = clinker.downcallHandle(memAddr, mt, fd2);
    result = (char) mh.invokeExact('B', 'D');
    Assert.assertEquals(result, 'C');
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 87 with Symbol

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

the class PrimitiveTypeTests method test_addTwoNegtiveBytes.

@Test
public void test_addTwoNegtiveBytes() 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) -7, (byte) -8);
    Assert.assertEquals(result, (byte) -15);
}
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 88 with Symbol

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

the class PrimitiveTypeTests method test_addShortAndShortFromPointer.

@Test
public void test_addShortAndShortFromPointer() throws Throwable {
    MethodType mt = MethodType.methodType(short.class, MemoryAddress.class, short.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_POINTER, C_SHORT);
    Symbol functionSymbol = nativeLib.lookup("addShortAndShortFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment shortSegmt = MemorySegment.allocateNative(C_SHORT);
    MemoryAccess.setShort(shortSegmt, (short) 24);
    short result = (short) mh.invokeExact(shortSegmt.address(), (short) 32);
    Assert.assertEquals(result, (short) 56);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_POINTER.withName("pointer"), C_SHORT.withName("short"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    shortSegmt = MemorySegment.allocateNative(C_SHORT);
    MemoryAccess.setShort(shortSegmt, (short) 22);
    result = (short) mh.invokeExact(shortSegmt.address(), (short) 33);
    Assert.assertEquals(result, (short) 55);
}
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 89 with Symbol

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

the class PrimitiveTypeTests method test_addIntAndChar_fromMemAddr.

@Test
public void test_addIntAndChar_fromMemAddr() throws Throwable {
    MethodType mt = MethodType.methodType(int.class, int.class, char.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_SHORT);
    Symbol functionSymbol = nativeLib.lookup("addIntAndChar").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
    int result = (int) mh.invokeExact(58, 'A');
    Assert.assertEquals(result, 123);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_SHORT.withName("short"));
    mh = clinker.downcallHandle(memAddr, mt, fd2);
    result = (int) mh.invokeExact(60, 'B');
    Assert.assertEquals(result, 126);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 90 with Symbol

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

the class PrimitiveTypeTests method test_addTwoFloats_fromMemAddr.

@Test
public void test_addTwoFloats_fromMemAddr() throws Throwable {
    MethodType mt = MethodType.methodType(float.class, float.class, float.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT);
    Symbol functionSymbol = nativeLib.lookup("add2Floats").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
    float result = (float) mh.invokeExact(5.74f, 6.79f);
    Assert.assertEquals(result, 12.53f, 0.01f);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_FLOAT.withName("float"), C_FLOAT.withName("float"), C_FLOAT.withName("float"));
    mh = clinker.downcallHandle(memAddr, mt, fd2);
    result = (float) mh.invokeExact(15.74f, 16.79f);
    Assert.assertEquals(result, 32.53f, 0.01f);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) 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