use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests method test_addDoubleAndDoublesFromStructWithNestedDoubleArray.
@Test
public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray() throws Throwable {
SequenceLayout doubleArray = MemoryLayout.ofSequence(2, C_DOUBLE);
GroupLayout structLayout = MemoryLayout.ofStruct(doubleArray.withName("array_elem1"), C_DOUBLE.withName("elem2"));
MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setDoubleAtOffset(structSegmt, 0, 111.111D);
MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D);
MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D);
double result = (double) mh.invokeExact(444.444D, structSegmt);
Assert.assertEquals(result, 1111.11D, 0.001D);
structSegmt.close();
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests method test_addBoolAndBoolsFromStructWithNestedBoolArray.
@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray() throws Throwable {
SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT);
GroupLayout structLayout = MemoryLayout.ofStruct(intArray.withName("array_elem1"), C_INT.withName("elem2"));
MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 0);
MemoryAccess.setIntAtOffset(structSegmt, 4, 1);
MemoryAccess.setIntAtOffset(structSegmt, 8, 0);
int result = (int) mh.invokeExact(0, structSegmt);
Assert.assertEquals(result, 1);
structSegmt.close();
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests method test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName.
@Test
public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName() throws Throwable {
SequenceLayout byteArray = MemoryLayout.ofSequence(2, C_CHAR);
GroupLayout structLayout = MemoryLayout.ofStruct(byteArray, C_CHAR);
MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedByteArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 11);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 12);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 13);
byte result = (byte) mh.invokeExact((byte) 14, structSegmt);
Assert.assertEquals(result, 50);
structSegmt.close();
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests method test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName.
@Test
public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName() throws Throwable {
GroupLayout intStruct = MemoryLayout.ofStruct(C_INT, C_INT);
SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_INT);
MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111);
MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222);
MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333);
MemoryAccess.setIntAtOffset(structSegmt, 12, 4444444);
MemoryAccess.setIntAtOffset(structSegmt, 16, 5555555);
int result = (int) mh.invokeExact(6666666, structSegmt);
Assert.assertEquals(result, 23333331);
structSegmt.close();
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests method test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder.
@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder() throws Throwable {
SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT);
GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), intArray.withName("array_elem2"));
MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 0);
MemoryAccess.setIntAtOffset(structSegmt, 4, 1);
MemoryAccess.setIntAtOffset(structSegmt, 8, 0);
int result = (int) mh.invokeExact(0, structSegmt);
Assert.assertEquals(result, 1);
structSegmt.close();
}
Aggregations