Search in sources :

Example 76 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_add3CharStructs_returnStruct.

@Test
public void test_add3CharStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"));
    VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
    VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2"));
    VarHandle charHandle3 = structLayout.varHandle(char.class, PathElement.groupElement("elem3"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add3CharStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt1, 'A');
    charHandle2.set(structSegmt1, 'B');
    charHandle3.set(structSegmt1, 'C');
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt2, 'B');
    charHandle2.set(structSegmt2, 'C');
    charHandle3.set(structSegmt2, 'D');
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(charHandle1.get(resultSegmt), 'B');
    Assert.assertEquals(charHandle2.get(resultSegmt), 'D');
    Assert.assertEquals(charHandle3.get(resultSegmt), 'F');
    structSegmt1.close();
    structSegmt2.close();
    resultSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 77 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class PrimitiveTypeTests method test_memoryAllocFreeFromDefaultLib_fromMemAddr.

@Test
public void test_memoryAllocFreeFromDefaultLib_fromMemAddr() throws Throwable {
    Symbol allocSymbol = defaultLib.lookup("malloc").get();
    MemoryAddress allocMemAddrFromSymbol = allocSymbol.address();
    MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
    FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
    MethodHandle allocHandle = clinker.downcallHandle(allocMemAddrFromSymbol, allocMethodType, allocFuncDesc);
    MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
    long allocMemAddrValue = allocMemAddr.toRawLongValue();
    MemorySegment memSeg = MemorySegment.ofNativeRestricted();
    MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 15);
    Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 15);
    Symbol freeSymbol = defaultLib.lookup("free").get();
    MemoryAddress freeMemAddr = freeSymbol.address();
    MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
    FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
    MethodHandle freeHandle = clinker.downcallHandle(freeMemAddr, freeMethodType, freeFuncDesc);
    freeHandle.invokeExact(allocMemAddr);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 78 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class PrimitiveTypeTests method test_strlenFromDefaultLibWithMemAddr_fromMemAddr.

@Test
public void test_strlenFromDefaultLibWithMemAddr_fromMemAddr() throws Throwable {
    Symbol strlenSymbol = defaultLib.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, mt, fd);
    MemorySegment funcMemSegment = CLinker.toCString("JEP389 DOWNCALL TEST SUITES");
    long strLength = (long) mh.invokeExact(funcMemSegment.address());
    Assert.assertEquals(strLength, 27);
    FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), C_POINTER.withName("pointer"));
    mh = clinker.downcallHandle(memAddr, mt, fd2);
    strLength = (long) mh.invokeExact(funcMemSegment.address());
    Assert.assertEquals(strLength, 27);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 79 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class PrimitiveTypeTests method test_addTwoShorts.

@Test
public void test_addTwoShorts() throws Throwable {
    MethodType mt = MethodType.methodType(short.class, short.class, short.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT);
    Symbol functionSymbol = nativeLib.lookup("add2Shorts").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    short result = (short) mh.invokeExact((short) 24, (short) 32);
    Assert.assertEquals(result, (short) 56);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_SHORT.withName("short"), C_SHORT.withName("short"), C_SHORT.withName("short"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    result = (short) mh.invokeExact((short) 11, (short) 22);
    Assert.assertEquals(result, (short) 33);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 80 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class PrimitiveTypeTests method test_addBoolAndBoolFromPointerWithOr.

@Test
public void test_addBoolAndBoolFromPointerWithOr() throws Throwable {
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_POINTER);
    Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolFromPointerWithOr").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment intSegmt = MemorySegment.allocateNative(C_INT);
    MemoryAccess.setInt(intSegmt, 1);
    boolean result = (boolean) mh.invokeExact(false, intSegmt.address());
    intSegmt.close();
    Assert.assertEquals(result, true);
    FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_INT.withName("int"), C_POINTER.withName("pointer"));
    mh = clinker.downcallHandle(functionSymbol, mt, fd2);
    intSegmt = MemorySegment.allocateNative(C_INT);
    MemoryAccess.setInt(intSegmt, 0);
    result = (boolean) mh.invokeExact(true, intSegmt.address());
    intSegmt.close();
    Assert.assertEquals(result, true);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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)173 MethodType (java.lang.invoke.MethodType)173 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)173 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 Test (org.testng.annotations.Test)173 MemorySegment (jdk.incubator.foreign.MemorySegment)149 GroupLayout (jdk.incubator.foreign.GroupLayout)135 VarHandle (java.lang.invoke.VarHandle)64 SequenceLayout (jdk.incubator.foreign.SequenceLayout)48 MemoryAddress (jdk.incubator.foreign.MemoryAddress)30