< prev index next >

test/jdk/java/foreign/TestUpcallHighArity.java

Print this page
*** 31,12 ***
--- 31,14 ---
   * @run testng/othervm/native
   *   --enable-native-access=ALL-UNNAMED
   *   TestUpcallHighArity
   */
  
+ import jdk.incubator.foreign.Addressable;
  import jdk.incubator.foreign.CLinker;
  import jdk.incubator.foreign.FunctionDescriptor;
+ import jdk.incubator.foreign.NativeSymbol;
  import jdk.incubator.foreign.SymbolLookup;
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;

*** 47,17 ***
  import java.lang.invoke.MethodHandles;
  import java.lang.invoke.MethodType;
  import java.util.List;
  import java.util.concurrent.atomic.AtomicReference;
  
- import static jdk.incubator.foreign.CLinker.*;
  import static org.testng.Assert.assertEquals;
  
  public class TestUpcallHighArity extends CallGeneratorHelper {
      static final MethodHandle MH_do_upcall;
      static final MethodHandle MH_passAndSave;
!     static final CLinker LINKER = CLinker.getInstance();
  
      // struct S_PDI { void* p0; double p1; int p2; };
      static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(
          C_POINTER.withName("p0"),
          C_DOUBLE.withName("p1"),
--- 49,16 ---
  import java.lang.invoke.MethodHandles;
  import java.lang.invoke.MethodType;
  import java.util.List;
  import java.util.concurrent.atomic.AtomicReference;
  
  import static org.testng.Assert.assertEquals;
  
  public class TestUpcallHighArity extends CallGeneratorHelper {
      static final MethodHandle MH_do_upcall;
      static final MethodHandle MH_passAndSave;
!     static final CLinker LINKER = CLinker.systemCLinker();
  
      // struct S_PDI { void* p0; double p1; int p2; };
      static final MemoryLayout S_PDI_LAYOUT = MemoryLayout.structLayout(
          C_POINTER.withName("p0"),
          C_DOUBLE.withName("p1"),

*** 68,16 ***
          try {
              System.loadLibrary("TestUpcallHighArity");
              SymbolLookup lookup = SymbolLookup.loaderLookup();
              MH_do_upcall = LINKER.downcallHandle(
                  lookup.lookup("do_upcall").get(),
!                 MethodType.methodType(void.class, MemoryAddress.class,
-                     MemorySegment.class, int.class, double.class, MemoryAddress.class,
-                     MemorySegment.class, int.class, double.class, MemoryAddress.class,
-                     MemorySegment.class, int.class, double.class, MemoryAddress.class,
-                     MemorySegment.class, int.class, double.class, MemoryAddress.class),
-                 FunctionDescriptor.ofVoid(C_POINTER,
                      S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,
                      S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,
                      S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,
                      S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER)
              );
--- 69,11 ---
          try {
              System.loadLibrary("TestUpcallHighArity");
              SymbolLookup lookup = SymbolLookup.loaderLookup();
              MH_do_upcall = LINKER.downcallHandle(
                  lookup.lookup("do_upcall").get(),
!                     FunctionDescriptor.ofVoid(C_POINTER,
                      S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,
                      S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,
                      S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER,
                      S_PDI_LAYOUT, C_INT, C_DOUBLE, C_POINTER)
              );

*** 106,13 ***
          AtomicReference<Object[]> capturedArgs = new AtomicReference<>();
          MethodHandle target = MethodHandles.insertArguments(MH_passAndSave, 1, capturedArgs)
                                           .asCollector(Object[].class, upcallType.parameterCount())
                                           .asType(upcallType);
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
!             MemoryAddress upcallStub = LINKER.upcallStub(target, upcallDescriptor, scope);
              Object[] args = new Object[upcallType.parameterCount() + 1];
!             args[0] = upcallStub.address();
              List<MemoryLayout> argLayouts = upcallDescriptor.argumentLayouts();
              for (int i = 1; i < args.length; i++) {
                  args[i] = makeArg(argLayouts.get(i - 1), null, false);
              }
  
--- 102,13 ---
          AtomicReference<Object[]> capturedArgs = new AtomicReference<>();
          MethodHandle target = MethodHandles.insertArguments(MH_passAndSave, 1, capturedArgs)
                                           .asCollector(Object[].class, upcallType.parameterCount())
                                           .asType(upcallType);
          try (ResourceScope scope = ResourceScope.newConfinedScope()) {
!             NativeSymbol upcallStub = LINKER.upcallStub(target, upcallDescriptor, scope);
              Object[] args = new Object[upcallType.parameterCount() + 1];
!             args[0] = upcallStub;
              List<MemoryLayout> argLayouts = upcallDescriptor.argumentLayouts();
              for (int i = 1; i < args.length; i++) {
                  args[i] = makeArg(argLayouts.get(i - 1), null, false);
              }
  

*** 121,11 ***
              Object[] capturedArgsArr = capturedArgs.get();
              for (int i = 0; i < capturedArgsArr.length; i++) {
                  if (upcallType.parameterType(i) == MemorySegment.class) {
                      assertStructEquals((MemorySegment) capturedArgsArr[i], (MemorySegment) args[i + 1], argLayouts.get(i));
                  } else {
!                     assertEquals(capturedArgsArr[i], args[i + 1]);
                  }
              }
          }
      }
  
--- 117,11 ---
              Object[] capturedArgsArr = capturedArgs.get();
              for (int i = 0; i < capturedArgsArr.length; i++) {
                  if (upcallType.parameterType(i) == MemorySegment.class) {
                      assertStructEquals((MemorySegment) capturedArgsArr[i], (MemorySegment) args[i + 1], argLayouts.get(i));
                  } else {
!                     assertEquals(capturedArgsArr[i], args[i + 1], "For index " + i);
                  }
              }
          }
      }
  
< prev index next >