< prev index next >

test/jdk/java/foreign/TestVarArgs.java

Print this page
*** 31,10 ***
--- 31,11 ---
  import jdk.incubator.foreign.CLinker;
  import jdk.incubator.foreign.FunctionDescriptor;
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemorySegment;
+ import jdk.incubator.foreign.NativeSymbol;
  import jdk.incubator.foreign.ResourceScope;
  import jdk.incubator.foreign.SymbolLookup;
  import jdk.incubator.foreign.ValueLayout;
  import org.testng.annotations.DataProvider;
  import org.testng.annotations.Test;

*** 43,31 ***
  import java.lang.invoke.MethodType;
  import java.lang.invoke.VarHandle;
  import java.util.ArrayList;
  import java.util.List;
  
- import static jdk.incubator.foreign.CLinker.*;
  import static jdk.incubator.foreign.MemoryLayout.PathElement.*;
  import static org.testng.Assert.assertEquals;
  
! public class TestVarArgs {
  
      static final MemoryLayout ML_CallInfo = MemoryLayout.structLayout(
              C_POINTER.withName("writeback"), // writeback
              C_POINTER.withName("argIDs")); // arg ids
  
!     static final VarHandle VH_CallInfo_writeback = ML_CallInfo.varHandle(long.class, groupElement("writeback"));
!     static final VarHandle VH_CallInfo_argIDs = ML_CallInfo.varHandle(long.class, groupElement("argIDs"));
  
!     static final VarHandle VH_IntArray = MemoryLayout.sequenceLayout(C_INT).varHandle(int.class, sequenceElement());
  
!     static final CLinker abi = CLinker.getInstance();
      static {
          System.loadLibrary("VarArgs");
      }
  
!     static final MemoryAddress VARARGS_ADDR =
              SymbolLookup.loaderLookup()
                      .lookup("varargs").get();
  
      static final int WRITEBACK_BYTES_PER_ARG = 8;
  
--- 44,30 ---
  import java.lang.invoke.MethodType;
  import java.lang.invoke.VarHandle;
  import java.util.ArrayList;
  import java.util.List;
  
  import static jdk.incubator.foreign.MemoryLayout.PathElement.*;
  import static org.testng.Assert.assertEquals;
  
! public class TestVarArgs extends NativeTestHelper {
  
      static final MemoryLayout ML_CallInfo = MemoryLayout.structLayout(
              C_POINTER.withName("writeback"), // writeback
              C_POINTER.withName("argIDs")); // arg ids
  
!     static final VarHandle VH_CallInfo_writeback = ML_CallInfo.varHandle(groupElement("writeback"));
!     static final VarHandle VH_CallInfo_argIDs = ML_CallInfo.varHandle(groupElement("argIDs"));
  
!     static final VarHandle VH_IntArray = MemoryLayout.sequenceLayout(C_INT).varHandle(sequenceElement());
  
!     static final CLinker abi = CLinker.systemCLinker();
      static {
          System.loadLibrary("VarArgs");
      }
  
!     static final NativeSymbol VARARGS_ADDR =
              SymbolLookup.loaderLookup()
                      .lookup("varargs").get();
  
      static final int WRITEBACK_BYTES_PER_ARG = 8;
  

*** 78,32 ***
              MemorySegment callInfo = MemorySegment.allocateNative(ML_CallInfo, scope);
              MemorySegment argIDs = MemorySegment.allocateNative(MemoryLayout.sequenceLayout(args.size(), C_INT), scope);
  
              MemoryAddress callInfoPtr = callInfo.address();
  
!             VH_CallInfo_writeback.set(callInfo, writeBack.address().toRawLongValue());
!             VH_CallInfo_argIDs.set(callInfo, argIDs.address().toRawLongValue());
  
              for (int i = 0; i < args.size(); i++) {
                  VH_IntArray.set(argIDs, (long) i, args.get(i).id.ordinal());
              }
  
              List<MemoryLayout> argLayouts = new ArrayList<>();
              argLayouts.add(C_POINTER); // call info
              argLayouts.add(C_INT); // size
-             args.forEach(a -> argLayouts.add(asVarArg(a.layout)));
  
!             FunctionDescriptor desc = FunctionDescriptor.ofVoid(argLayouts.toArray(MemoryLayout[]::new));
  
              List<Class<?>> carriers = new ArrayList<>();
              carriers.add(MemoryAddress.class); // call info
              carriers.add(int.class); // size
              args.forEach(a -> carriers.add(a.carrier));
  
              MethodType mt = MethodType.methodType(void.class, carriers);
  
!             MethodHandle downcallHandle = abi.downcallHandle(VARARGS_ADDR, mt, desc);
  
              List<Object> argValues = new ArrayList<>();
              argValues.add(callInfoPtr); // call info
              argValues.add(args.size());  // size
              args.forEach(a -> argValues.add(a.value));
--- 78,32 ---
              MemorySegment callInfo = MemorySegment.allocateNative(ML_CallInfo, scope);
              MemorySegment argIDs = MemorySegment.allocateNative(MemoryLayout.sequenceLayout(args.size(), C_INT), scope);
  
              MemoryAddress callInfoPtr = callInfo.address();
  
!             VH_CallInfo_writeback.set(callInfo, writeBack.address());
!             VH_CallInfo_argIDs.set(callInfo, argIDs.address());
  
              for (int i = 0; i < args.size(); i++) {
                  VH_IntArray.set(argIDs, (long) i, args.get(i).id.ordinal());
              }
  
              List<MemoryLayout> argLayouts = new ArrayList<>();
              argLayouts.add(C_POINTER); // call info
              argLayouts.add(C_INT); // size
  
!             FunctionDescriptor desc = FunctionDescriptor.ofVoid(argLayouts.stream().toArray(MemoryLayout[]::new))
+                     .asVariadic(args.stream().map(a -> a.layout).toArray(MemoryLayout[]::new));
  
              List<Class<?>> carriers = new ArrayList<>();
              carriers.add(MemoryAddress.class); // call info
              carriers.add(int.class); // size
              args.forEach(a -> carriers.add(a.carrier));
  
              MethodType mt = MethodType.methodType(void.class, carriers);
  
!             MethodHandle downcallHandle = abi.downcallHandle(VARARGS_ADDR, desc);
  
              List<Object> argValues = new ArrayList<>();
              argValues.add(callInfoPtr); // call info
              argValues.add(args.size());  // size
              args.forEach(a -> argValues.add(a.value));

*** 138,11 ***
          private VarArg(NativeType id, ValueLayout layout, Class<?> carrier, Object value) {
              this.id = id;
              this.value = value;
              this.layout = layout;
              this.carrier = carrier;
!             this.vh = layout.varHandle(carrier);
          }
  
          static VarArg intArg(int value) {
              return new VarArg(VarArg.NativeType.INT, C_INT, int.class, value);
          }
--- 138,11 ---
          private VarArg(NativeType id, ValueLayout layout, Class<?> carrier, Object value) {
              this.id = id;
              this.value = value;
              this.layout = layout;
              this.carrier = carrier;
!             this.vh = layout.varHandle();
          }
  
          static VarArg intArg(int value) {
              return new VarArg(VarArg.NativeType.INT, C_INT, int.class, value);
          }
< prev index next >