Search in sources :

Example 6 with Addressable

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

the class PrimitiveTypeTests2 method test_generateNewChar_2.

@Test
public void test_generateNewChar_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();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, 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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 7 with Addressable

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

the class PrimitiveTypeTests2 method test_addBoolAndBoolFromPointerWithOr_2.

@Test
public void test_addBoolAndBoolFromPointerWithOr_2() throws Throwable {
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_POINTER);
    Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPointerWithOr").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    MemorySegment boolSegmt = MemorySegment.allocateNative(C_CHAR, resourceScope);
    MemoryAccess.setByte(boolSegmt, (byte) 1);
    boolean result = (boolean) mh.invokeExact(false, boolSegmt.address());
    Assert.assertEquals(result, true);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 8 with Addressable

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

the class PrimitiveTypeTests2 method test_strlenFromDefaultLibWithMemAddr_fromMemAddr_2.

@Test
public void test_strlenFromDefaultLibWithMemAddr_fromMemAddr_2() 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) {
        Addressable strlenSymbol = defaultLibLookup.lookup("strlen").get();
        MemoryAddress memAddr = strlenSymbol.address();
        MethodType mt = MethodType.methodType(long.class, MemoryAddress.class);
        FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER);
        MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
        MemorySegment funcMemSegment = CLinker.toCString("JEP389 DOWNCALL TEST SUITES", resourceScope);
        long strLength = (long) mh.invokeExact(funcMemSegment.address());
        Assert.assertEquals(strLength, 27);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 9 with Addressable

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

the class PrimitiveTypeTests2 method test_addTwoIntsReturnVoid_fromMemAddr_2.

@Test
public void test_addTwoIntsReturnVoid_fromMemAddr_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();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
    mh.invokeExact(454, 398);
}
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 10 with Addressable

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

the class PrimitiveTypeTests2 method test_generateNewCharFromPointer_2.

@Test
public void test_generateNewCharFromPointer_2() throws Throwable {
    MethodType mt = MethodType.methodType(char.class, MemoryAddress.class, char.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_POINTER, C_SHORT);
    Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    MemorySegment shortSegmt = MemorySegment.allocateNative(C_SHORT, resourceScope);
    MemoryAccess.setChar(shortSegmt, 'B');
    char result = (char) mh.invokeExact(shortSegmt.address(), 'D');
    Assert.assertEquals(result, 'C');
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) 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)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