Search in sources :

Example 51 with Addressable

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

the class InvalidDownCallTests method test_mismatchedByteArgWithShortLayout.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Mismatched size .*")
public void test_mismatchedByteArgWithShortLayout() throws Throwable {
    MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_SHORT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Bytes").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    fail("Failed to throw out IllegalArgumentException in the case of the mismatched layout");
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 52 with Addressable

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

the class MultiCallTests method test_twoCallsWithSameFuncDescriptor.

@Test
public void test_twoCallsWithSameFuncDescriptor() throws Throwable {
    MethodType mt = MethodType.methodType(int.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    int result = (int) mh.invokeExact(112, 123);
    Assert.assertEquals(result, 235);
    mh = clinker.downcallHandle(functionSymbol, mt, fd);
    result = (int) mh.invokeExact(235, 439);
    Assert.assertEquals(result, 674);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 53 with Addressable

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

the class MultiCallTests method test_twoCallsWithDiffFuncDescriptor.

@Test
public void test_twoCallsWithDiffFuncDescriptor() throws Throwable {
    MethodType mt1 = MethodType.methodType(int.class, int.class, int.class);
    FunctionDescriptor fd1 = FunctionDescriptor.of(C_INT, C_INT, C_INT);
    Addressable functionSymbol1 = nativeLibLookup.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol1, mt1, fd1);
    int result = (int) mh.invokeExact(112, 123);
    Assert.assertEquals(result, 235);
    MethodType mt2 = MethodType.methodType(int.class, int.class, int.class, int.class);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT);
    Addressable functionSymbol2 = nativeLibLookup.lookup("add3Ints").get();
    mh = clinker.downcallHandle(functionSymbol2, mt2, fd2);
    result = (int) mh.invokeExact(112, 123, 235);
    Assert.assertEquals(result, 470);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 54 with Addressable

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

the class MultiCallTests method test_multiCallsWithSameArgLayouts.

@Test
public void test_multiCallsWithSameArgLayouts() throws Throwable {
    MethodType mt1 = MethodType.methodType(int.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT);
    Addressable functionSymbol1 = nativeLibLookup.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol1, mt1, fd);
    int intResult = (int) mh.invokeExact(112, 123);
    Assert.assertEquals(intResult, 235);
    mh = clinker.downcallHandle(functionSymbol1, mt1, fd);
    intResult = (int) mh.invokeExact(234, 567);
    Assert.assertEquals(intResult, 801);
    MethodType mt2 = MethodType.methodType(void.class, int.class, int.class);
    FunctionDescriptor fd2 = FunctionDescriptor.ofVoid(C_INT, C_INT);
    Addressable functionSymbol2 = nativeLibLookup.lookup("add2IntsReturnVoid").get();
    mh = clinker.downcallHandle(functionSymbol2, mt2, fd2);
    mh.invokeExact(454, 398);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 55 with Addressable

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

the class MultiThreadingTests2 method test_twoThreadsWithDiffFuncDescriptor.

@Test
public void test_twoThreadsWithDiffFuncDescriptor() throws Throwable {
    Thread thr1 = new Thread() {

        public void run() {
            try {
                MethodType mt = MethodType.methodType(int.class, int.class, int.class);
                FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT);
                Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
                int result = (int) mh.invokeExact(112, 123);
                Assert.assertEquals(result, 235);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    Thread thr2 = new Thread() {

        public void run() {
            try {
                MethodType mt = MethodType.methodType(int.class, int.class, int.class, int.class);
                FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT);
                Addressable functionSymbol = nativeLibLookup.lookup("add3Ints").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
                int result = (int) mh.invokeExact(112, 123, 235);
                Assert.assertEquals(result, 470);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    thr1.setUncaughtExceptionHandler(this);
    thr2.setUncaughtExceptionHandler(this);
    thr1.start();
    thr2.start();
    thr1.join();
    thr2.join();
    if (initException != null) {
        throw new RuntimeException(initException);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)432 MethodType (java.lang.invoke.MethodType)432 Addressable (jdk.incubator.foreign.Addressable)432 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)432 Test (org.testng.annotations.Test)432 MemorySegment (jdk.incubator.foreign.MemorySegment)314 ResourceScope (jdk.incubator.foreign.ResourceScope)275 GroupLayout (jdk.incubator.foreign.GroupLayout)270 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)270 VarHandle (java.lang.invoke.VarHandle)128 SequenceLayout (jdk.incubator.foreign.SequenceLayout)96 MemoryAddress (jdk.incubator.foreign.MemoryAddress)63