use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_1.
@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable {
GroupLayout boolStruct = MemoryLayout.structLayout(C_CHAR, C_CHAR);
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct);
GroupLayout structLayout = MemoryLayout.structLayout(structArray, C_CHAR);
MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 1);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 1);
MemoryAccess.setByteAtOffset(structSegmt, 3, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 4, (byte) 1);
boolean result = (boolean) mh.invokeExact(false, structSegmt);
Assert.assertEquals(result, true);
}
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_addFloatAndFloatsFromStructPointer_1.
@Test
public void test_addFloatAndFloatsFromStructPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(float.class, float.class, MemoryAddress.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_POINTER);
Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
floatHandle1.set(structSegmt, 35.11F);
floatHandle2.set(structSegmt, 46.22F);
float result = (float) mh.invokeExact(79.33F, structSegmt.address());
Assert.assertEquals(result, 160.66F, 0.01F);
}
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_1.
@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_1() throws Throwable {
SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, C_CHAR);
GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 1);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 0);
boolean result = (boolean) mh.invokeExact(false, structSegmt);
Assert.assertEquals(result, true);
}
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class InvalidDownCallTests method test_mismatchedIntArgLayoutWithShortType.
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*")
public void test_mismatchedIntArgLayoutWithShortType() throws Throwable {
MethodType mt = MethodType.methodType(short.class, short.class, short.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_INT, C_SHORT);
Addressable functionSymbol = nativeLibLookup.lookup("add2Shorts").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
fail("Failed to throw out IllegalArgumentException in the case of the mismatched int layout");
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class InvalidDownCallTests method test_invalidFloatTypeArgument.
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "The passed-in argument type at index .*")
public void test_invalidFloatTypeArgument() throws Throwable {
MethodType mt = MethodType.methodType(float.class, Float.class, float.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT);
Addressable functionSymbol = nativeLibLookup.lookup("add2Floats").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
fail("Failed to throw out IllegalArgumentException in the case of the Float argument");
}
Aggregations