use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_addTwoIntsReturnVoid_fromMemAddr_2.
@Test
public void test_addTwoIntsReturnVoid_fromMemAddr_2() throws Throwable {
MethodType mt = MethodType.methodType(void.class, int.class, int.class);
FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT);
Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
mh.invokeExact(454, 398);
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_generateNewCharFromPointer_2.
@Test
public void test_generateNewCharFromPointer_2() throws Throwable {
MethodType mt = MethodType.methodType(char.class, MemoryAddress.class, char.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_POINTER, C_SHORT);
Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
MemorySegment shortSegmt = MemorySegment.allocateNative(C_SHORT, resourceScope);
MemoryAccess.setChar(shortSegmt, 'B');
char result = (char) mh.invokeExact(shortSegmt.address(), 'D');
Assert.assertEquals(result, 'C');
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_memoryAllocFreeFromDefaultLib_fromMemAddr_2.
@Test
public void test_memoryAllocFreeFromDefaultLib_fromMemAddr_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 allocSymbol = defaultLibLookup.lookup("malloc").get();
MemoryAddress allocMemAddrFromSymbol = allocSymbol.address();
MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
MethodHandle allocHandle = clinker.downcallHandle(allocMemAddrFromSymbol, allocator, allocMethodType, allocFuncDesc);
MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
MemorySegment memSeg = allocMemAddr.asSegment(10L, resourceScope);
MemoryAccess.setIntAtOffset(memSeg, 0, 15);
Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, 0), 15);
Addressable freeSymbol = defaultLibLookup.lookup("free").get();
MemoryAddress freeMemAddr = freeSymbol.address();
MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
MethodHandle freeHandle = clinker.downcallHandle(freeMemAddr, allocator, freeMethodType, freeFuncDesc);
freeHandle.invokeExact(allocMemAddr);
}
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_addTwoInts_fromMemAddr_2.
@Test
public void test_addTwoInts_fromMemAddr_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();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, 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_addTwoDoubles_2.
@Test
public void test_addTwoDoubles_2() throws Throwable {
MethodType mt = MethodType.methodType(double.class, double.class, double.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, C_DOUBLE);
Addressable functionSymbol = nativeLibLookup.lookup("add2Doubles").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
double result = (double) mh.invokeExact(159.748d, 262.795d);
Assert.assertEquals(result, 422.543d, 0.001d);
}
Aggregations