use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.
the class StructTests1 method test_addByteAndBytesFromStructPointer_1.
@Test
public void test_addByteAndBytesFromStructPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"));
VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS);
NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
byteHandle1.set(structSegmt, (byte) 11);
byteHandle2.set(structSegmt, (byte) 12);
byte result = (byte) mh.invoke((byte) 13, structSegmt);
Assert.assertEquals(result, 36);
}
}
use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.
the class StructTests1 method test_addShortAndShortsFromNestedStruct_withoutLayoutName_1.
@Test
public void test_addShortAndShortsFromNestedStruct_withoutLayoutName_1() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT);
GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_SHORT);
FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").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 StructTests2 method test_add2FloatStructs_returnStruct_2.
@Test
public void test_add2FloatStructs_returnStruct_2() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"));
VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
floatHandle1.set(structSegmt1, 25.12F);
floatHandle2.set(structSegmt1, 11.23F);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
floatHandle1.set(structSegmt2, 24.34F);
floatHandle2.set(structSegmt2, 13.45F);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2);
Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
}
}
use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.
the class StructTests2 method test_addLongAndLongsFromStructPointer_2.
@Test
public void test_addLongAndLongsFromStructPointer_2() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"));
VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS);
NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructPointer").get();
MethodHandle mh = clinker.downcallHandle(fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
longHandle1.set(structSegmt, 224466880022L);
longHandle2.set(structSegmt, 446688002244L);
long result = (long) mh.invoke(functionSymbol, 668800224466L, structSegmt);
Assert.assertEquals(result, 1339955106732L);
}
}
use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.
the class StructTests2 method test_addCharAndCharsFromNestedStruct_2.
@Test
public void test_addCharAndCharsFromNestedStruct_2() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"));
GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_CHAR.withName("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct").get();
MethodHandle mh = clinker.downcallHandle(fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
structSegmt.set(JAVA_CHAR, 0, 'E');
structSegmt.set(JAVA_CHAR, 2, 'F');
structSegmt.set(JAVA_CHAR, 4, 'G');
char result = (char) mh.invokeExact(functionSymbol, 'H', structSegmt);
Assert.assertEquals(result, 'W');
}
}
Aggregations