use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class StructTests method test_add3IntStructs_returnStruct.
@Test
public void test_add3IntStructs_returnStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"), C_INT.withName("elem3"));
VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
VarHandle intHandle3 = structLayout.varHandle(int.class, PathElement.groupElement("elem3"));
MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Symbol functionSymbol = nativeLib.lookup("add3IntStructs_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt1, 11223344);
intHandle2.set(structSegmt1, 55667788);
intHandle3.set(structSegmt1, 99001122);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt2, 33445566);
intHandle2.set(structSegmt2, 77889900);
intHandle3.set(structSegmt2, 44332211);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals(intHandle1.get(resultSegmt), 44668910);
Assert.assertEquals(intHandle2.get(resultSegmt), 133557688);
Assert.assertEquals(intHandle3.get(resultSegmt), 143333333);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class StructTests method test_addDoubleFromPointerAndDoublesFromStruct.
@Test
public void test_addDoubleFromPointerAndDoublesFromStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(double.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("addDoubleFromPointerAndDoublesFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment doubleSegmt = MemorySegment.allocateNative(C_DOUBLE);
MemoryAccess.setDouble(doubleSegmt, 112.123D);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
doubleHandle1.set(structSegmt, 118.456D);
doubleHandle2.set(structSegmt, 119.789D);
double result = (double) mh.invokeExact(doubleSegmt.address(), structSegmt);
Assert.assertEquals(result, 350.368D, 0.001D);
doubleSegmt.close();
structSegmt.close();
}
use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class Test_VarHandle method describeConstableTestGeneral.
private void describeConstableTestGeneral(String testName, VarHandle handle) throws Throwable {
VarHandleDesc desc = handle.describeConstable().orElseThrow();
/*
* verify that descriptor can be resolved. Otherwise will throw
* ReflectiveOperationException
*/
VarHandle resolvedHandle = (VarHandle) desc.resolveConstantDesc(MethodHandles.lookup());
logger.debug(testName + ": Descriptor of original class varType is: " + handle.varType().descriptorString());
logger.debug(testName + ": Descriptor of VarHandleDesc varType is: " + resolvedHandle.varType().descriptorString());
Assert.assertTrue(handle.varType().equals(resolvedHandle.varType()));
Assert.assertTrue(handle.coordinateTypes().equals(resolvedHandle.coordinateTypes()));
}
use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class Test_DynamicConstantDesc method testDynamicConstantDescResolveConstantDescVarHandleArray.
@Test(groups = { "level.sanity" })
public void testDynamicConstantDescResolveConstantDescVarHandleArray() throws Throwable {
ClassDesc arrayClassDesc = int[].class.describeConstable().orElseThrow();
/* describe and resolve constant */
DynamicConstantDesc<VarHandle> desc = DynamicConstantDesc.ofNamed(ConstantDescs.BSM_VARHANDLE_ARRAY, "af", ConstantDescs.CD_VarHandle, new ConstantDesc[] { arrayClassDesc });
VarHandle resolvedClass = (VarHandle) desc.resolveConstantDesc(MethodHandles.lookup());
DynamicConstantDesc<VarHandle> resDesc = resolvedClass.describeConstable().orElseThrow();
/* test if descriptors are equals */
logger.debug("testDynamicConstantDescResolveConstantDescVarHandleArray" + ": original is: " + desc.toString() + " resolved desc is: " + resDesc.toString());
/* test equivalence manually, descriptor names will not be equal */
Assert.assertTrue(Arrays.equals(desc.bootstrapArgs(), resDesc.bootstrapArgs()));
Assert.assertTrue(desc.bootstrapMethod().equals(resDesc.bootstrapMethod()));
Assert.assertEquals(resDesc.constantName(), "_");
Assert.assertEquals(desc.constantType(), resDesc.constantType());
}
use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class Test_DynamicConstantDesc method testDynamicConstantDescResolveConstantDescVarHandleField.
/*
* Convert DynamicConstantDesc<VarHandle> to a static VarHandle to DynamicConstantDesc<VarHandle> and verify
* that the descriptors are equal.
*/
private void testDynamicConstantDescResolveConstantDescVarHandleField(String testName, DirectMethodHandleDesc bsm, String name, ConstantDesc[] bsmargs) throws Throwable {
/* describe and resolve constant */
DynamicConstantDesc<VarHandle> desc = DynamicConstantDesc.ofNamed(bsm, name, ConstantDescs.CD_VarHandle, bsmargs);
VarHandle resolvedClass = (VarHandle) desc.resolveConstantDesc(MethodHandles.lookup());
DynamicConstantDesc<VarHandle> resDesc = resolvedClass.describeConstable().orElseThrow();
/* test if descriptors are equals */
logger.debug(testName + ": original is: " + desc.toString() + " resolved is: " + resDesc.toString());
Assert.assertTrue(desc.equals(resDesc));
}
Aggregations