Search in sources :

Example 81 with Symbol

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

the class PrimitiveTypeTests method test_generateNewChar.

@Test
public void test_generateNewChar() 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();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, 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(functionSymbol, 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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 82 with Symbol

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

the class PrimitiveTypeTests method test_memoryAllocFreeFromDefaultLib.

@Test
public void test_memoryAllocFreeFromDefaultLib() throws Throwable {
    Symbol allocSymbol = defaultLib.lookup("malloc").get();
    MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
    FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
    MethodHandle allocHandle = clinker.downcallHandle(allocSymbol, allocMethodType, allocFuncDesc);
    MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
    long allocMemAddrValue = allocMemAddr.toRawLongValue();
    MemorySegment memSeg = MemorySegment.ofNativeRestricted();
    MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 15);
    Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 15);
    Symbol freeSymbol = defaultLib.lookup("free").get();
    MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
    FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
    MethodHandle freeHandle = clinker.downcallHandle(freeSymbol, freeMethodType, freeFuncDesc);
    freeHandle.invokeExact(allocMemAddr);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 83 with Symbol

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

the class PrimitiveTypeTests method test_addTwoIntsReturnVoid_fromMemAddr.

@Test
public void test_addTwoIntsReturnVoid_fromMemAddr() throws Throwable {
    MethodType mt = MethodType.methodType(void.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT);
    Symbol functionSymbol = nativeLib.lookup("add2IntsReturnVoid").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
    mh.invokeExact(454, 398);
}
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 84 with Symbol

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

the class PrimitiveTypeTests method test_addTwoNegtiveInts.

@Test
public void test_addTwoNegtiveInts() throws Throwable {
    MethodType mt = MethodType.methodType(int.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT);
    Symbol functionSymbol = nativeLib.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    int result = (int) mh.invokeExact(-112, -123);
    Assert.assertEquals(result, -235);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    result = (int) mh.invokeExact(-222, -444);
    Assert.assertEquals(result, -666);
}
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 85 with Symbol

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

the class InvalidDownCallTests method test_unsupportedStructLayout.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "GroupLayout is expected: .*")
public void test_unsupportedStructLayout() throws Throwable {
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, MemoryLayouts.BITS_64_LE);
    Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithXor").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    fail("Failed to throw out IllegalArgumentException in the case of the unsupported layout for struct");
}
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)

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