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');
}
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);
}
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);
}
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);
}
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");
}
Aggregations