use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.
the class StructTests1 method test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_1.
@Test
public void test_addShortAndShortsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"));
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct);
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), structArray.withName("struc_array_elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
structSegmt.set(JAVA_SHORT, 0, (short) 111);
structSegmt.set(JAVA_SHORT, 2, (short) 222);
structSegmt.set(JAVA_SHORT, 4, (short) 333);
structSegmt.set(JAVA_SHORT, 6, (short) 444);
structSegmt.set(JAVA_SHORT, 8, (short) 555);
short result = (short) mh.invokeExact((short) 666, structSegmt);
Assert.assertEquals(result, 2331);
}
}
use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.
the class StructTests1 method test_addShortAndShortsFromNestedStruct_reverseOrder_1.
@Test
public void test_addShortAndShortsFromNestedStruct_reverseOrder_1() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"));
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()));
FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
structSegmt.set(JAVA_SHORT, 0, (short) 31);
structSegmt.set(JAVA_SHORT, 2, (short) 33);
structSegmt.set(JAVA_SHORT, 4, (short) 35);
short result = (short) mh.invokeExact((short) 37, structSegmt);
Assert.assertEquals(result, 136);
}
}
use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.
the class MultiCallTests method test_twoCallsWithDiffReturnType.
@Test
public void test_twoCallsWithDiffReturnType() throws Throwable {
FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT);
NativeSymbol functionSymbol1 = nativeLibLookup.lookup("add2Ints").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol1, fd1);
int result = (int) mh.invokeExact(112, 123);
Assert.assertEquals(result, 235);
FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT);
NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add2IntsReturnVoid").get();
mh = clinker.downcallHandle(functionSymbol2, fd2);
mh.invokeExact(454, 398);
}
use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.
the class MultiCallTests method test_multiCallsWithMixedFuncDescriptors.
@Test
public void test_multiCallsWithMixedFuncDescriptors() throws Throwable {
FunctionDescriptor fd1 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT);
NativeSymbol functionSymbol1 = nativeLibLookup.lookup("add2Ints").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol1, fd1);
int result = (int) mh.invokeExact(112, 123);
Assert.assertEquals(result, 235);
FunctionDescriptor fd2 = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT, JAVA_INT);
NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add3Ints").get();
mh = clinker.downcallHandle(functionSymbol2, fd2);
result = (int) mh.invokeExact(112, 123, 235);
Assert.assertEquals(result, 470);
FunctionDescriptor fd3 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT);
NativeSymbol functionSymbol3 = nativeLibLookup.lookup("add2IntsReturnVoid").get();
mh = clinker.downcallHandle(functionSymbol3, fd3);
mh.invokeExact(454, 398);
mh = clinker.downcallHandle(functionSymbol1, fd1);
result = (int) mh.invokeExact(234, 567);
Assert.assertEquals(result, 801);
mh = clinker.downcallHandle(functionSymbol2, fd2);
result = (int) mh.invokeExact(312, 323, 334);
Assert.assertEquals(result, 969);
mh = clinker.downcallHandle(functionSymbol3, fd3);
mh.invokeExact(539, 672);
}
use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.
the class MultiCallTests method test_multiCallsWithSameArgLayouts.
@Test
public void test_multiCallsWithSameArgLayouts() throws Throwable {
FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT);
NativeSymbol functionSymbol1 = nativeLibLookup.lookup("add2Ints").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol1, fd);
int intResult = (int) mh.invokeExact(112, 123);
Assert.assertEquals(intResult, 235);
mh = clinker.downcallHandle(functionSymbol1, fd);
intResult = (int) mh.invokeExact(234, 567);
Assert.assertEquals(intResult, 801);
FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(JAVA_INT, JAVA_INT);
NativeSymbol functionSymbol2 = nativeLibLookup.lookup("add2IntsReturnVoid").get();
mh = clinker.downcallHandle(functionSymbol2, fd2);
mh.invokeExact(454, 398);
}
Aggregations