Search in sources :

Example 61 with NativeSymbol

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

the class InvalidDownCallTests method test_invalidMemoryLayoutForMemoryAddress.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Unsupported layout.*")
public void test_invalidMemoryLayoutForMemoryAddress() 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) {
        throw new IllegalArgumentException("Unsupported layout");
    } else {
        NativeSymbol functionSymbol = clinker.lookup("strlen").get();
        FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, MemoryLayout.paddingLayout(64));
        MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
        fail("Failed to throw out IllegalArgumentException in the case of the invalid MemoryLayout");
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 62 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_addIntAndIntFromPointer_1.

@Test
public void test_addIntAndIntFromPointer_1() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    MemorySegment intSegmt = nativeAllocator.allocate(JAVA_INT, 215);
    int result = (int) mh.invoke(321, intSegmt);
    Assert.assertEquals(result, 536);
}
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 63 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_addTwoInts_1.

@Test
public void test_addTwoInts_1() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_INT);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    int result = (int) mh.invokeExact(112, 123);
    Assert.assertEquals(result, 235);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 64 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_addIntAndChar_1.

@Test
public void test_addIntAndChar_1() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, JAVA_CHAR);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndChar").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    int result = (int) mh.invokeExact(58, 'A');
    Assert.assertEquals(result, 123);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 65 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_generateNewCharFromPointer_1.

@Test
public void test_generateNewCharFromPointer_1() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, ADDRESS, JAVA_CHAR);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    MemorySegment charSegmt = nativeAllocator.allocate(JAVA_CHAR, 'B');
    char result = (char) mh.invoke(charSegmt, 'D');
    Assert.assertEquals(result, 'C');
}
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