Search in sources :

Example 51 with FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_addTwoFloats_2.

@Test
public void test_addTwoFloats_2() throws Throwable {
    MethodType mt = MethodType.methodType(float.class, float.class, float.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Floats").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    float result = (float) mh.invokeExact(5.74f, 6.79f);
    Assert.assertEquals(result, 12.53f, 0.01f);
}
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 FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_generateNewChar_fromMemAddr_2.

@Test
public void test_generateNewChar_fromMemAddr_2() throws Throwable {
    MethodType mt = MethodType.methodType(char.class, char.class, char.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT);
    Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFrom2Chars").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
    char result = (char) mh.invokeExact('B', 'D');
    Assert.assertEquals(result, 'C');
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 53 with FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_addTwoFloats_fromMemAddr_2.

@Test
public void test_addTwoFloats_fromMemAddr_2() throws Throwable {
    MethodType mt = MethodType.methodType(float.class, float.class, float.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Floats").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
    float result = (float) mh.invokeExact(5.74f, 6.79f);
    Assert.assertEquals(result, 12.53f, 0.01f);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 54 with FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_addTwoLongs_2.

@Test
public void test_addTwoLongs_2() throws Throwable {
    MethodType mt = MethodType.methodType(long.class, long.class, long.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Longs").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    long result = (long) mh.invokeExact(57424L, 698235L);
    Assert.assertEquals(result, 755659L);
}
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 FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_addTwoIntsReturnVoid_2.

@Test
public void test_addTwoIntsReturnVoid_2() throws Throwable {
    MethodType mt = MethodType.methodType(void.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    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)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)939 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)939 Test (org.testng.annotations.Test)939 MemorySegment (jdk.incubator.foreign.MemorySegment)754 GroupLayout (jdk.incubator.foreign.GroupLayout)675 MethodType (java.lang.invoke.MethodType)605 ResourceScope (jdk.incubator.foreign.ResourceScope)549 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)541 Addressable (jdk.incubator.foreign.Addressable)432 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 VarHandle (java.lang.invoke.VarHandle)318 SequenceLayout (jdk.incubator.foreign.SequenceLayout)240 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 MemoryAddress (jdk.incubator.foreign.MemoryAddress)127 VaList (jdk.incubator.foreign.VaList)4