Search in sources :

Example 6 with VarHandle

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();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 7 with VarHandle

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();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 8 with VarHandle

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()));
}
Also used : VarHandle(java.lang.invoke.VarHandle) VarHandleDesc(java.lang.invoke.VarHandle.VarHandleDesc)

Example 9 with VarHandle

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());
}
Also used : VarHandle(java.lang.invoke.VarHandle) ClassDesc(java.lang.constant.ClassDesc) Test(org.testng.annotations.Test)

Example 10 with VarHandle

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));
}
Also used : VarHandle(java.lang.invoke.VarHandle)

Aggregations

VarHandle (java.lang.invoke.VarHandle)400 Test (org.testng.annotations.Test)388 MethodHandle (java.lang.invoke.MethodHandle)319 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)318 GroupLayout (jdk.incubator.foreign.GroupLayout)318 MemorySegment (jdk.incubator.foreign.MemorySegment)318 ResourceScope (jdk.incubator.foreign.ResourceScope)254 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)254 MethodType (java.lang.invoke.MethodType)192 Addressable (jdk.incubator.foreign.Addressable)128 NativeSymbol (jdk.incubator.foreign.NativeSymbol)126 MemoryAddress (jdk.incubator.foreign.MemoryAddress)78 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)64 Test (org.junit.Test)8 VarHandleDesc (java.lang.invoke.VarHandle.VarHandleDesc)2 ClassDesc (java.lang.constant.ClassDesc)1 Field (java.lang.reflect.Field)1