use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_printfFromDefaultLibWithMemAddr_2.
@Test
public void test_printfFromDefaultLibWithMemAddr_2() throws Throwable {
/* Temporarily disable the default library loading on AIX till we figure out a way
* around to handle the case as the official implementation in OpenJDK17 doesn't
* help to load the static libray (libc.a).
*/
if (!isAixOS) {
Addressable functionSymbol = defaultLibLookup.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, allocator, mt, fd);
MemorySegment formatMemSegment = CLinker.toCString("\n%d + %d = %d\n", resourceScope);
mh.invoke(formatMemSegment.address(), 15, 27, 42);
}
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_addTwoInts_2.
@Test
public void test_addTwoInts_2() throws Throwable {
MethodType mt = MethodType.methodType(int.class, int.class, int.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT);
Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
int result = (int) mh.invokeExact(112, 123);
Assert.assertEquals(result, 235);
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_addTwoBoolsWithOr_fromMemAddr_2.
@Test
public void test_addTwoBoolsWithOr_fromMemAddr_2() throws Throwable {
MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
boolean result = (boolean) mh.invokeExact(true, false);
Assert.assertEquals(result, true);
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_addTwoLongs_fromMemAddr_2.
@Test
public void test_addTwoLongs_fromMemAddr_2() throws Throwable {
MethodType mt = MethodType.methodType(long.class, long.class, long.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout);
Addressable functionSymbol = nativeLibLookup.lookup("add2Longs").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
long result = (long) mh.invokeExact(57424L, 698235L);
Assert.assertEquals(result, 755659L);
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addIntFromPointerAndIntsFromStruct_returnIntPointer.
@Test
public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment intSegmt = MemorySegment.allocateNative(C_INT);
MemoryAccess.setInt(intSegmt, 1122333);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt, 4455666);
intHandle2.set(structSegmt, 7788999);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(intSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_INT.byteSize());
VarHandle intHandle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
int result = (int) intHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 13366998);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), intSegmt.address().toRawLongValue());
intSegmt.close();
structSegmt.close();
resultSegmt.close();
}
Aggregations