Search in sources :

Example 31 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_addTwoFloats_1.

@Test
public void test_addTwoFloats_1() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, JAVA_FLOAT);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Floats").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    float result = (float) mh.invokeExact(5.74f, 6.79f);
    Assert.assertEquals(result, 12.53f, 0.01f);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 32 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_addLongAndLongFromPointer_1.

@Test
public void test_addLongAndLongFromPointer_1() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_LONG);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    MemorySegment longSegmt = nativeAllocator.allocate(JAVA_LONG, 57424L);
    long result = (long) mh.invoke(longSegmt, 698235L);
    Assert.assertEquals(result, 755659L);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 33 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_memoryAllocFreeFromDefaultLib_1.

@Test
public void test_memoryAllocFreeFromDefaultLib_1() throws Throwable {
    /* Temporarily disable the default library loading on AIX till we figure out a way
		 * around to handle the case as the official implementation in OpenJDK17 doesn't
		 * help to load the static libray (libc.a).
		 */
    if (!isAixOS) {
        NativeSymbol allocSymbol = clinker.lookup("malloc").get();
        FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(ADDRESS, JAVA_LONG);
        MethodHandle allocHandle = clinker.downcallHandle(allocSymbol, allocFuncDesc);
        MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
        allocMemAddr.set(JAVA_INT, 0, 15);
        Assert.assertEquals(allocMemAddr.get(JAVA_INT, 0), 15);
        NativeSymbol freeSymbol = clinker.lookup("free").get();
        FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(ADDRESS);
        MethodHandle freeHandle = clinker.downcallHandle(freeSymbol, freeFuncDesc);
        freeHandle.invoke(allocMemAddr);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 34 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_addTwoDoubles_1.

@Test
public void test_addTwoDoubles_1() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Doubles").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    double result = (double) mh.invokeExact(159.748d, 262.795d);
    Assert.assertEquals(result, 422.543d, 0.001d);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 35 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_printfFromDefaultLibWithMemAddr_1.

@Test
public void test_printfFromDefaultLibWithMemAddr_1() throws Throwable {
    /* Temporarily disable the default library loading on AIX till we figure out a way
		 * around to handle the case as the official implementation in OpenJDK17 doesn't
		 * help to load the static libray (libc.a).
		 */
    if (!isAixOS) {
        NativeSymbol functionSymbol = clinker.lookup("printf").get();
        FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT);
        MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
        MemorySegment formatSegmt = nativeAllocator.allocateUtf8String("\n%d + %d = %d\n");
        mh.invoke(formatSegmt, 15, 27, 42);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)334 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)334 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 Test (org.testng.annotations.Test)334 MemorySegment (jdk.incubator.foreign.MemorySegment)291 ResourceScope (jdk.incubator.foreign.ResourceScope)274 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)271 GroupLayout (jdk.incubator.foreign.GroupLayout)270 VarHandle (java.lang.invoke.VarHandle)126 SequenceLayout (jdk.incubator.foreign.SequenceLayout)96 MemoryAddress (jdk.incubator.foreign.MemoryAddress)34 VaList (jdk.incubator.foreign.VaList)4