Search in sources :

Example 1 with VaList

use of jdk.incubator.foreign.VaList in project openj9 by eclipse.

the class VaListTests method test_vprintfFromDefaultLibWithVaList.

@Test
public void test_vprintfFromDefaultLibWithVaList() throws Throwable {
    /* Disable the test on Windows given a misaligned access exception coming from
		 * java.base/java.lang.invoke.MemoryAccessVarHandleBase triggered by CLinker.toCString()
		 * is also captured on OpenJDK/Hotspot.
		 */
    if (!isWinOS) {
        NativeSymbol functionSymbol = clinker.lookup("vprintf").get();
        FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, ADDRESS);
        try (ResourceScope scope = ResourceScope.newConfinedScope()) {
            SegmentAllocator nativeAllocator = SegmentAllocator.nativeAllocator(scope);
            MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("%d * %d = %d\n");
            VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 7).addVarg(JAVA_INT, 8).addVarg(JAVA_INT, 56), scope);
            MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
            mh.invoke(formatSegmt, vaList);
        }
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VaList(jdk.incubator.foreign.VaList) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 2 with VaList

use of jdk.incubator.foreign.VaList in project openj9 by eclipse.

the class VaListTests method test_addDoublesWithVaList.

@Test
public void test_addDoublesWithVaList() throws Throwable {
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoublesFromVaList").get();
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_INT, ADDRESS);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_DOUBLE, 150.1001D).addVarg(JAVA_DOUBLE, 160.2002D).addVarg(JAVA_DOUBLE, 170.1001D).addVarg(JAVA_DOUBLE, 180.2002D), scope);
        MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
        double result = (double) mh.invoke(4, vaList);
        Assert.assertEquals(result, 660.6006D);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VaList(jdk.incubator.foreign.VaList) ResourceScope(jdk.incubator.foreign.ResourceScope) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 3 with VaList

use of jdk.incubator.foreign.VaList in project openj9 by eclipse.

the class VaListTests method test_addLongsWithVaList.

@Test
public void test_addLongsWithVaList() throws Throwable {
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongsFromVaList").get();
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, ADDRESS);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_LONG, 700000L).addVarg(JAVA_LONG, 800000L).addVarg(JAVA_LONG, 900000L).addVarg(JAVA_LONG, 1000000L), scope);
        MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
        long result = (long) mh.invoke(4, vaList);
        Assert.assertEquals(result, 3400000L);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VaList(jdk.incubator.foreign.VaList) ResourceScope(jdk.incubator.foreign.ResourceScope) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 4 with VaList

use of jdk.incubator.foreign.VaList in project openj9 by eclipse.

the class VaListTests method test_addIntsWithVaList.

@Test
public void test_addIntsWithVaList() throws Throwable {
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntsFromVaList").get();
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        VaList vaList = VaList.make(vaListBuilder -> vaListBuilder.addVarg(JAVA_INT, 700).addVarg(JAVA_INT, 800).addVarg(JAVA_INT, 900).addVarg(JAVA_INT, 1000), scope);
        MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
        int result = (int) mh.invoke(4, vaList);
        Assert.assertEquals(result, 3400);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VaList(jdk.incubator.foreign.VaList) ResourceScope(jdk.incubator.foreign.ResourceScope) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)4 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)4 NativeSymbol (jdk.incubator.foreign.NativeSymbol)4 ResourceScope (jdk.incubator.foreign.ResourceScope)4 VaList (jdk.incubator.foreign.VaList)4 Test (org.testng.annotations.Test)4 MemorySegment (jdk.incubator.foreign.MemorySegment)1 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)1