From 553cdc584329923d8668c4aac9794cd8f1d9e246 Mon Sep 17 00:00:00 2001 From: Not Zed Date: Tue, 9 May 2023 12:26:56 +0930 Subject: [PATCH] Update to JDK 20 foreign-abi preview API. --- src/notzed.nativez/bin/export-api | 2 +- .../classes/au/notzed/nativez/ByteArray.java | 31 ++++---- .../au/notzed/nativez/DoubleArray.java | 23 +++--- .../classes/au/notzed/nativez/FloatArray.java | 23 +++--- .../classes/au/notzed/nativez/Frame.java | 19 +++-- .../au/notzed/nativez/HandleArray.java | 37 +++++---- .../classes/au/notzed/nativez/IntArray.java | 25 +++--- .../classes/au/notzed/nativez/LongArray.java | 19 +++-- .../classes/au/notzed/nativez/Memory.java | 71 ++++++++++------- .../classes/au/notzed/nativez/Native.java | 50 ++++++------ .../classes/au/notzed/nativez/Pointer.java | 9 +-- .../au/notzed/nativez/PointerArray.java | 38 ++++----- .../classes/au/notzed/nativez/ShortArray.java | 23 +++--- src/notzed.nativez/lib/code.api | 78 +++++++++---------- src/notzed.nativez/lib/code.pm | 2 +- src/notzed.nativez/lib/method.pm | 10 +-- src/notzed.nativez/lib/types.api | 50 ++++++------ src/notzed.nativez/native/native.make | 5 +- 18 files changed, 253 insertions(+), 262 deletions(-) diff --git a/src/notzed.nativez/bin/export-api b/src/notzed.nativez/bin/export-api index 6ecd12d..ad662a1 100755 --- a/src/notzed.nativez/bin/export-api +++ b/src/notzed.nativez/bin/export-api @@ -121,7 +121,7 @@ my @cmd = ( "-fplugin-arg-libexport-output=$var->{workdir}/$apibase.pm", # "-fplugin-arg-libexport-verbose=$var->{verbose}", '-O0', - '-o', '/dev/null', + '-o', "$var->{workdir}/$apibase.pch", @includes, $apih ); diff --git a/src/notzed.nativez/classes/au/notzed/nativez/ByteArray.java b/src/notzed.nativez/classes/au/notzed/nativez/ByteArray.java index 72aa439..2b86b8d 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/ByteArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/ByteArray.java @@ -20,9 +20,6 @@ package au.notzed.nativez; import java.lang.foreign.*; import java.util.AbstractList; -import java.util.function.Function; -import java.util.function.BiFunction; -import java.util.List; public class ByteArray extends AbstractList implements Pointer { final MemorySegment segment; @@ -35,32 +32,34 @@ public class ByteArray extends AbstractList implements Pointer { return new ByteArray(segment); } - public static ByteArray createArray(MemoryAddress address, long length, MemorySession scope) { - return create(MemorySegment.ofAddress(address, length, scope)); + public static ByteArray create(MemorySegment address, long length, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), length, scope)); } - public static ByteArray createArray(MemoryAddress address, MemorySession scope) { - return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope)); + public static ByteArray create(MemorySegment address, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope)); } - public static ByteArray createArray(long length, SegmentAllocator alloc) { + public static ByteArray create(long length, SegmentAllocator alloc) { return create(alloc.allocateArray(Memory.BYTE, length)); } - public static ByteArray create(String value, SegmentAllocator alloc) { + public static ByteArray create(SegmentAllocator alloc, String value) { return create(alloc.allocateUtf8String(value)); } - public static ByteArray create(String value, MemorySession scope) { - return create(scope.allocateUtf8String(value)); - } + //public static ByteArray create(SegmentScope scope, String value) { + // return create(scope.allocateUtf8String(value)); + //} - public final MemoryAddress address() { - return segment.address(); + @Override + public final MemorySegment address() { + return segment; } - public final MemorySession scope() { - return segment.session(); + @Override + public final SegmentScope scope() { + return segment.scope(); } @Override diff --git a/src/notzed.nativez/classes/au/notzed/nativez/DoubleArray.java b/src/notzed.nativez/classes/au/notzed/nativez/DoubleArray.java index 9df27ed..72ed513 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/DoubleArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/DoubleArray.java @@ -20,9 +20,6 @@ package au.notzed.nativez; import java.lang.foreign.*; import java.util.AbstractList; -import java.util.function.Function; -import java.util.function.BiFunction; -import java.util.List; public class DoubleArray extends AbstractList implements Pointer { final MemorySegment segment; @@ -35,15 +32,15 @@ public class DoubleArray extends AbstractList implements Pointer { return new DoubleArray(segment); } - public static DoubleArray createArray(MemoryAddress address, long length, MemorySession scope) { - return create(MemorySegment.ofAddress(address, length * Memory.DOUBLE.byteSize(), scope)); + public static DoubleArray create(MemorySegment address, long length, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), length * Memory.DOUBLE.byteSize(), scope)); } - public static DoubleArray createArray(MemoryAddress address, MemorySession scope) { - return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope)); + public static DoubleArray create(MemorySegment address, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope)); } - public static DoubleArray createArray(long length, SegmentAllocator alloc) { + public static DoubleArray create(long length, SegmentAllocator alloc) { return create(alloc.allocateArray(Memory.DOUBLE, length)); } @@ -51,12 +48,14 @@ public class DoubleArray extends AbstractList implements Pointer { return create(alloc.allocateArray(Memory.DOUBLE, values)); } - public final MemoryAddress address() { - return segment.address(); + @Override + public final MemorySegment address() { + return segment; } - public final MemorySession scope() { - return segment.session(); + @Override + public final SegmentScope scope() { + return segment.scope(); } @Override diff --git a/src/notzed.nativez/classes/au/notzed/nativez/FloatArray.java b/src/notzed.nativez/classes/au/notzed/nativez/FloatArray.java index 13a510a..e49b984 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/FloatArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/FloatArray.java @@ -20,9 +20,6 @@ package au.notzed.nativez; import java.lang.foreign.*; import java.util.AbstractList; -import java.util.function.Function; -import java.util.function.BiFunction; -import java.util.List; public class FloatArray extends AbstractList implements Pointer { final MemorySegment segment; @@ -35,15 +32,15 @@ public class FloatArray extends AbstractList implements Pointer { return new FloatArray(segment); } - public static FloatArray createArray(MemoryAddress address, long length, MemorySession scope) { - return create(MemorySegment.ofAddress(address, length * Memory.FLOAT.byteSize(), scope)); + public static FloatArray create(MemorySegment address, long length, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), length * Memory.FLOAT.byteSize(), scope)); } - public static FloatArray createArray(MemoryAddress address, MemorySession scope) { - return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope)); + public static FloatArray create(MemorySegment address, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope)); } - public static FloatArray createArray(long length, SegmentAllocator alloc) { + public static FloatArray create(SegmentAllocator alloc, int length) { return create(alloc.allocateArray(Memory.FLOAT, length)); } @@ -51,12 +48,14 @@ public class FloatArray extends AbstractList implements Pointer { return create(alloc.allocateArray(Memory.FLOAT, values)); } - public final MemoryAddress address() { - return segment.address(); + @Override + public final MemorySegment address() { + return segment; } - public final MemorySession scope() { - return segment.session(); + @Override + public final SegmentScope scope() { + return segment.scope(); } @Override diff --git a/src/notzed.nativez/classes/au/notzed/nativez/Frame.java b/src/notzed.nativez/classes/au/notzed/nativez/Frame.java index bf65a66..4480287 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/Frame.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/Frame.java @@ -33,20 +33,18 @@ import static java.lang.foreign.ValueLayout.OfAddress; */ public class Frame implements AutoCloseable, SegmentAllocator { - private final MemorySession scope; - private final SegmentAllocator alloc; + private final Arena alloc; Frame() { - this.scope = MemorySession.openConfined(); - this.alloc = SegmentAllocator.newNativeArena(scope); + this.alloc = Arena.openConfined(); } public static Frame frame() { return new Frame(); } - public MemorySession scope() { - return scope; + public SegmentScope scope() { + return alloc.scope(); } @Override @@ -56,7 +54,7 @@ public class Frame implements AutoCloseable, SegmentAllocator { @Override public void close() { - scope.close(); + alloc.close(); } public MemorySegment allocateInt() { @@ -83,7 +81,7 @@ public class Frame implements AutoCloseable, SegmentAllocator { return allocateArray(Memory.POINTER, count); } - public MemorySegment allocateArray(OfAddress type, MemoryAddress[] value) { + public MemorySegment allocateArray(OfAddress type, MemorySegment[] value) { MemorySegment m = allocateArray(type, value.length); for (int i=0;i MemorySegment copy(MemoryAddress value) { + public MemorySegment copy(MemorySegment value) { MemorySegment mem = allocateAddress(); MemoryAccess.setAddress(mem, value); return mem; @@ -165,6 +163,7 @@ public class Frame implements AutoCloseable, SegmentAllocator { } } + /* public static MemorySegment copy(MemorySegment ctx, String string) { if (string != null) { SegmentAllocator alloc = ctx.session(); @@ -188,5 +187,5 @@ public class Frame implements AutoCloseable, SegmentAllocator { } else { return Memory.NULL; } - } + }*/ } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/HandleArray.java b/src/notzed.nativez/classes/au/notzed/nativez/HandleArray.java index e1e0ade..ecd9a1c 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/HandleArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/HandleArray.java @@ -20,9 +20,7 @@ package au.notzed.nativez; import java.lang.foreign.*; import java.util.AbstractList; -import java.util.function.Function; import java.util.function.BiFunction; -import java.util.List; /** * A HandleArray is a typed PointerArray @@ -32,36 +30,36 @@ import java.util.List; */ public class HandleArray extends AbstractList implements Pointer { public final MemorySegment segment; - final MemorySession scope; - BiFunction create; + final SegmentScope scope; + BiFunction create; - private HandleArray(MemorySegment segment, BiFunction create, MemorySession scope) { + private HandleArray(MemorySegment segment, BiFunction create, SegmentScope scope) { this.segment = segment; this.create = create; this.scope = scope; } - public static HandleArray create(MemorySegment segment, BiFunction create) { - return new HandleArray<>(segment, create, segment.session()); + public static HandleArray create(MemorySegment segment, BiFunction create) { + return new HandleArray<>(segment, create, segment.scope()); } - public static HandleArray create(MemorySegment segment, BiFunction create, MemorySession scope) { + public static HandleArray create(MemorySegment segment, BiFunction create, SegmentScope scope) { return new HandleArray<>(segment, create, scope); } - public static HandleArray createArray(long size, SegmentAllocator alloc, BiFunction create) { + public static HandleArray create(long size, BiFunction create, SegmentAllocator alloc) { return create(alloc.allocateArray(Memory.POINTER, size), create); } - public static HandleArray createArray(long size, SegmentAllocator alloc, BiFunction create, MemorySession scope) { + public static HandleArray create(long size, BiFunction create, SegmentAllocator alloc, SegmentScope scope) { return create(alloc.allocateArray(Memory.POINTER, size), create, scope); } - public static HandleArray createArray(MemoryAddress address, long size, BiFunction create, MemorySession scope) { - return create(MemorySegment.ofAddress(address, size * Memory.POINTER.byteSize(), scope), create); + public static HandleArray create(MemorySegment address, long size, BiFunction create, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), size * Memory.POINTER.byteSize(), scope), create); } - public static HandleArray create(SegmentAllocator alloc, BiFunction create, T... values) { + public static HandleArray create(BiFunction create, SegmentAllocator alloc, T... values) { HandleArray array = create(alloc.allocateArray(Memory.POINTER, values.length), create); for (int i=0;i extends AbstractList implements P } @Override - public final MemoryAddress address() { - return segment.address(); + public final MemorySegment address() { + return segment; } - public final MemorySession scope() { - return segment.session(); + @Override + public final SegmentScope scope() { + return segment.scope(); } @Override @@ -107,11 +106,11 @@ public class HandleArray extends AbstractList implements P } public T getAtIndex(long index) { - MemoryAddress ptr = segment.getAtIndex(Memory.POINTER, index); + MemorySegment ptr = segment.getAtIndex(Memory.POINTER, index); return ptr != null ? create.apply(ptr, scope) : null; } public void setAtIndex(long index, T value) { - segment.setAtIndex(Memory.POINTER, index, value != null ? value.address() : MemoryAddress.NULL); + segment.setAtIndex(Memory.POINTER, index, value != null ? value.address() : MemorySegment.NULL); } } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/IntArray.java b/src/notzed.nativez/classes/au/notzed/nativez/IntArray.java index c4378a2..1451c0d 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/IntArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/IntArray.java @@ -19,9 +19,6 @@ package au.notzed.nativez; import java.lang.foreign.*; import java.util.AbstractList; -import java.util.function.Function; -import java.util.function.BiFunction; -import java.util.List; public class IntArray extends AbstractList implements Pointer { @@ -35,15 +32,15 @@ public class IntArray extends AbstractList implements Pointer { return new IntArray(segment); } - public static IntArray createArray(MemoryAddress address, long length, MemorySession scope) { - return create(MemorySegment.ofAddress(address, length * Memory.INT.byteSize(), scope)); + public static IntArray create(MemorySegment address, long length, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), length * Memory.INT.byteSize(), scope)); } - public static IntArray createArray(MemoryAddress address, MemorySession scope) { - return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope)); + public static IntArray create(MemorySegment address, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope)); } - public static IntArray createArray(long length, SegmentAllocator alloc) { + public static IntArray create(long length, SegmentAllocator alloc) { return create(alloc.allocateArray(Memory.INT, length)); } @@ -59,16 +56,14 @@ public class IntArray extends AbstractList implements Pointer { return create(segment.asSlice(offset * Memory.INT.byteSize(), length * Memory.INT.byteSize())); } - public final MemorySegment segment() { + @Override + public final MemorySegment address() { return segment; } - public final MemoryAddress address() { - return segment.address(); - } - - public final MemorySession scope() { - return segment.session(); + @Override + public final SegmentScope scope() { + return segment.scope(); } @Override diff --git a/src/notzed.nativez/classes/au/notzed/nativez/LongArray.java b/src/notzed.nativez/classes/au/notzed/nativez/LongArray.java index 7ed18f8..0f2b6a0 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/LongArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/LongArray.java @@ -20,9 +20,6 @@ package au.notzed.nativez; import java.lang.foreign.*; import java.util.AbstractList; -import java.util.function.Function; -import java.util.function.BiFunction; -import java.util.List; public class LongArray extends AbstractList implements Pointer { final MemorySegment segment; @@ -35,11 +32,11 @@ public class LongArray extends AbstractList implements Pointer { return new LongArray(segment); } - public static LongArray createArray(MemoryAddress address, long length, MemorySession scope) { - return create(MemorySegment.ofAddress(address, length * Memory.LONG.byteSize(), scope)); + public static LongArray create(MemorySegment address, long length, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), length * Memory.LONG.byteSize(), scope)); } - public static LongArray createArray(long length, SegmentAllocator alloc) { + public static LongArray create(long length, SegmentAllocator alloc) { return create(alloc.allocateArray(Memory.LONG, length)); } @@ -47,12 +44,14 @@ public class LongArray extends AbstractList implements Pointer { return create(alloc.allocateArray(Memory.LONG, values)); } - public final MemoryAddress address() { - return segment.address(); + @Override + public final MemorySegment address() { + return segment; } - public final MemorySession scope() { - return segment.session(); + @Override + public final SegmentScope scope() { + return segment.scope(); } @Override diff --git a/src/notzed.nativez/classes/au/notzed/nativez/Memory.java b/src/notzed.nativez/classes/au/notzed/nativez/Memory.java index 841284e..908df69 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/Memory.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/Memory.java @@ -34,11 +34,12 @@ public class Memory { public static final OfDouble DOUBLE = JAVA_DOUBLE.withBitAlignment(64); public static final OfAddress POINTER = ADDRESS.withBitAlignment(64); - static final MemorySession sharedScope = MemorySession.openShared(); // cleaner? + static final SegmentScope sharedScope = SegmentScope.auto(); // cleaner? // Note: currently can't create zero-length segments - static final MemorySegment NULL = MemorySegment.ofAddress(MemoryAddress.NULL, 1, MemorySession.global()); + @Deprecated + static final MemorySegment NULL = MemorySegment.NULL; - public static MemorySession sharedScope() { + public static SegmentScope sharedScope() { return sharedScope; } @@ -47,31 +48,31 @@ public class Memory { } public static MethodHandle downcall(String name, FunctionDescriptor desc) { - return SymbolLookup.loaderLookup().lookup(name) + return SymbolLookup.loaderLookup().find(name) .map(sym -> Linker.nativeLinker().downcallHandle(sym, desc)) .orElse(null); } - public static MethodHandle downcall(Addressable sym, FunctionDescriptor desc) { + public static MethodHandle downcall(MemorySegment sym, FunctionDescriptor desc) { return Linker.nativeLinker().downcallHandle(sym, desc); } - - public static MethodHandle downcall(String name, MemoryAddress sym, FunctionDescriptor desc, MemorySession scope) { - return sym != MemoryAddress.NULL +/* + public static MethodHandle downcall(MemorySegment sym, FunctionDescriptor desc, SegmentScope scope) { + return sym != MemorySegment.NULL ? Linker.nativeLinker().downcallHandle(MemorySegment.ofAddress(sym, 0, scope), desc) : null; } - public static MethodHandle downcall(String name, FunctionDescriptor desc, Function resolve, MemorySession scope) { - MemoryAddress sym = resolve.apply(name); - return sym != MemoryAddress.NULL + public static MethodHandle downcall(String name, FunctionDescriptor desc, Function resolve, SegmentScope scope) { + MemorySegment sym = resolve.apply(name); + return sym != MemorySegment.NULL ? Linker.nativeLinker().downcallHandle(MemorySegment.ofAddress(sym, 0, scope), desc) : null; } - +*/ static final MethodHandles.Lookup lookup = MethodHandles.lookup(); - public static MemorySegment upcall(Object instance, FunctionDescriptor desc, MemorySession scope) { + public static MemorySegment upcall(Object instance, FunctionDescriptor desc, SegmentScope scope) { try { java.lang.reflect.Method m = instance.getClass().getMethods()[0]; MethodHandle handle = lookup.findVirtual(instance.getClass(), "call", MethodType.methodType(m.getReturnType(), m.getParameterTypes())) @@ -82,7 +83,7 @@ public class Memory { } } - public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String signature, FunctionDescriptor desc, MemorySession scope) { + public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String signature, FunctionDescriptor desc, SegmentScope scope) { try { java.lang.reflect.Method m = instance.getClass().getMethods()[0]; MethodHandle handle = lookup.findVirtual(instance.getClass(), m.getName(), MethodType.fromMethodDescriptorString(signature, Memory.class.getClassLoader())) @@ -93,7 +94,7 @@ public class Memory { } } - public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String method, String signature, FunctionDescriptor desc, MemorySession scope) { + public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String method, String signature, FunctionDescriptor desc, SegmentScope scope) { try { MethodHandle handle = lookup.findVirtual(instance.getClass(), method, MethodType.fromMethodDescriptorString(signature, Memory.class.getClassLoader())) .bindTo(instance); @@ -103,16 +104,30 @@ public class Memory { } } - public static Addressable address(Addressable v) { - return v != null ? v.address() : MemoryAddress.NULL; + // TODO: the null check has overhead so only use where necessary +/* + public static MemorySegment address(MemorySegment v) { + return v; + } + + public static MemorySegment address(Pointer v) { + return v.address(); + } + + public static MemorySegment address(FunctionPointer v) { + return v.symbol(); + } + */ + public static MemorySegment address(MemorySegment v) { + return v != null ? v : MemorySegment.NULL; } - public static Addressable address(Pointer v) { - return v != null ? v.address() : MemoryAddress.NULL; + public static MemorySegment address(Pointer v) { + return v != null ? v.address() : MemorySegment.NULL; } - public static Addressable address(FunctionPointer v) { - return v != null ? v.symbol().address() : MemoryAddress.NULL; + public static MemorySegment address(FunctionPointer v) { + return v != null ? v.symbol() : MemorySegment.NULL; } //public static long length(Pointer v) { @@ -146,20 +161,20 @@ public class Memory { } } - public static String copyString(MemoryAddress string) { - return string != MemoryAddress.NULL ? string.getUtf8String(0) : null; + public static String copyString(MemorySegment string) { + return string != MemorySegment.NULL ? string.getUtf8String(0) : null; } public static MemorySegment copyString(String string, SegmentAllocator alloc) { return (string != null) ? alloc.allocateUtf8String(string) : Memory.NULL; } - public static String[] copyStringArray(MemoryAddress list, long len) { - if (list != MemoryAddress.NULL) { + public static String[] copyStringArray(MemorySegment list, long len) { + if (list != MemorySegment.NULL) { String[] array = new String[(int)len]; for (int i = 0; i < array.length; i++) { - MemoryAddress fu = list.getAtIndex(Memory.POINTER, i); - array[i] = fu != MemoryAddress.NULL ? fu.getUtf8String(0) : null; + MemorySegment fu = list.getAtIndex(Memory.POINTER, i); + array[i] = fu != MemorySegment.NULL ? fu.getUtf8String(0) : null; } return array; } else { @@ -171,7 +186,7 @@ public class Memory { if (array != null) { MemorySegment list = alloc.allocateArray(Memory.POINTER, array.length); for (int i = 0; i < array.length; i++) { - list.setAtIndex(Memory.POINTER, i, array[i] != null ? alloc.allocateUtf8String(array[i]) : MemoryAddress.NULL); + list.setAtIndex(Memory.POINTER, i, array[i] != null ? alloc.allocateUtf8String(array[i]) : MemorySegment.NULL); } return list; } else { diff --git a/src/notzed.nativez/classes/au/notzed/nativez/Native.java b/src/notzed.nativez/classes/au/notzed/nativez/Native.java index cf1dbff..421d427 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/Native.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/Native.java @@ -16,14 +16,8 @@ */ package au.notzed.nativez; -import java.io.StringReader; -import java.lang.invoke.*; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.function.Function; -import java.util.function.IntFunction; import java.lang.foreign.*; import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; @@ -36,7 +30,7 @@ import java.lang.System.Logger.Level; *

* Handles instantiation and provides helper functions for native access. *

- * Work in progress. + * Work in progress / obsolete? *

* For better safety the 'p' field should be the CHandle, and addr() would * call get(). Otherwise one must not release ANY object which might ever @@ -47,11 +41,11 @@ import java.lang.System.Logger.Level; */ public class Native implements Pointer { - private final MemoryAddress p; + private final MemorySegment p; private final static boolean dolog = true; - protected Native(MemoryAddress p) { + protected Native(MemorySegment p) { this.p = p; } @@ -59,12 +53,14 @@ public class Native implements Pointer { return System.getLogger("notzed.native"); } - public MemoryAddress address() { + @Override + public MemorySegment address() { return p; } - public MemorySession scope() { - return MemorySession.global(); + @Override + public SegmentScope scope() { + return p.scope(); } /* ********************************************************************** */ @@ -80,10 +76,10 @@ public class Native implements Pointer { */ static private final ReferenceQueue references = new ReferenceQueue<>(); - private static T createInstance(Class jtype, MemoryAddress p) { + private static T createInstance(Class jtype, MemorySegment p) { cleanerStep(); try { - Class[] params = {MemoryAddress.class}; + Class[] params = {MemorySegment.class}; Constructor cc = jtype.getDeclaredConstructor(params); cc.setAccessible(true); @@ -96,7 +92,7 @@ public class Native implements Pointer { } /* - public static T resolve(Class jtype, MemoryAddress p) { + public static T resolve(Class jtype, MemorySegment p) { T o; //if (dolog) @@ -114,13 +110,13 @@ public class Native implements Pointer { } return o; }*/ - public static T resolve(Class jtype, MemoryAddress p, Function create) { + public static T resolve(Class jtype, MemorySegment p, Function create) { T o; boolean step = false; //if (dolog) // log().log(Level.DEBUG, () -> String.format(" resolv $%016x %s", Memory.toLong(p), create)); - if (p.toRawLongValue() == 0) + if (p.address() == 0) return null; // Instantiation needs to be synchronized for obvious reasons. @@ -142,7 +138,7 @@ public class Native implements Pointer { } { T x = o; - log().log(Level.DEBUG, () -> String.format(fmt, p.toRawLongValue(), x.getClass().getName())); + log().log(Level.DEBUG, () -> String.format(fmt, p.address(), x.getClass().getName())); } } @@ -181,7 +177,7 @@ public class Native implements Pointer { if (ref != null) { if (dolog) - log().log(Level.DEBUG, () -> String.format(" force $%016x %s", p.toRawLongValue(), getClass().getName())); + log().log(Level.DEBUG, () -> String.format(" force $%016x %s", p.address(), getClass().getName())); ref.enqueue(); } @@ -246,11 +242,11 @@ public class Native implements Pointer { private static class CHandle extends WeakReference { - protected MemoryAddress p; + protected MemorySegment p; final Class jtype; CHandle next; - CHandle(Native referent, ReferenceQueue references, MemoryAddress p) { + CHandle(Native referent, ReferenceQueue references, MemorySegment p) { super(referent, references); this.p = p; this.jtype = referent.getClass(); @@ -260,9 +256,9 @@ public class Native implements Pointer { try { if (p != null) { if (dolog) - log().log(Level.DEBUG, () -> String.format(" releas $%016x %s", p.toRawLongValue(), jtype.getName())); + log().log(Level.DEBUG, () -> String.format(" releas $%016x %s", p.address(), jtype.getName())); - Method mm = jtype.getDeclaredMethod("release", MemoryAddress.class); + Method mm = jtype.getDeclaredMethod("release", MemorySegment.class); mm.setAccessible(true); mm.invoke(null, p); } @@ -301,7 +297,7 @@ public class Native implements Pointer { /** * Sigh, memoryaddress has a miserable hashCode(), it's even worse than Long.hashCode() */ - public static final int hashCode(MemoryAddress p) { + public static final int hashCode(MemorySegment p) { return p.hashCode() >>> 5; } } @@ -334,7 +330,7 @@ public class Native implements Pointer { while (h != null) { Native o = h.get(); System.out.printf(" $%016x: %s %-40s %s\n", - h.p.toRawLongValue(), + h.p.address(), o == null ? "dead" : "live", h.jtype.getName(), o); @@ -404,7 +400,7 @@ public class Native implements Pointer { size += 1; } - public CHandle get(MemoryAddress p) { + public CHandle get(MemorySegment p) { int i = CHandle.hashCode(p) & mask; CHandle h = table[i]; @@ -413,7 +409,7 @@ public class Native implements Pointer { return h; } - public CHandle remove(MemoryAddress p) { + public CHandle remove(MemorySegment p) { int i = CHandle.hashCode(p) & mask; CHandle h = table[i]; CHandle a = null; diff --git a/src/notzed.nativez/classes/au/notzed/nativez/Pointer.java b/src/notzed.nativez/classes/au/notzed/nativez/Pointer.java index 8837fcc..c9b8503 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/Pointer.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/Pointer.java @@ -7,11 +7,8 @@ import java.lang.foreign.*; * Because you can't implement foriegn.Addressable for some silly reason */ public interface Pointer { - MemoryAddress address(); - default MemorySession scope() { - return MemorySession.global(); + MemorySegment address(); + default SegmentScope scope() { + return SegmentScope.global(); } - //default long length() { - // return 1; - //} } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/PointerArray.java b/src/notzed.nativez/classes/au/notzed/nativez/PointerArray.java index 7d45387..b706e66 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/PointerArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/PointerArray.java @@ -20,11 +20,8 @@ package au.notzed.nativez; import java.lang.foreign.*; import java.util.AbstractList; -import java.util.function.Function; -import java.util.function.BiFunction; -import java.util.List; -public class PointerArray extends AbstractList implements Pointer { +public class PointerArray extends AbstractList implements Pointer { final MemorySegment segment; private PointerArray(MemorySegment segment) { @@ -35,24 +32,29 @@ public class PointerArray extends AbstractList implements Pointer return new PointerArray(segment); } - public static PointerArray createArray(MemoryAddress address, long length, MemorySession scope) { - return create(MemorySegment.ofAddress(address, length, scope)); + public static PointerArray create(MemorySegment address, long length, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), length, scope)); } - public static PointerArray createArray(long length, SegmentAllocator alloc) { + public static PointerArray create(long length, SegmentAllocator alloc) { return create(alloc.allocateArray(Memory.POINTER, length)); } - public static PointerArray create(Frame alloc, MemoryAddress... values) { - return create(alloc.allocateArray(Memory.POINTER, values)); + public static PointerArray create(SegmentAllocator alloc, MemorySegment... values) { + MemorySegment m = alloc.allocateArray(Memory.POINTER, values.length); + for (int i=0;i implements Pointer } @Override - public MemoryAddress get(int index) { + public MemorySegment get(int index) { return getAtIndex(index); } @Override - public MemoryAddress set(int index, MemoryAddress value) { - MemoryAddress old = getAtIndex(index); + public MemorySegment set(int index, MemorySegment value) { + MemorySegment old = getAtIndex(index); setAtIndex(index, value); return old; } @@ -76,11 +78,11 @@ public class PointerArray extends AbstractList implements Pointer return segment.byteSize() / Memory.POINTER.byteSize(); } - public MemoryAddress getAtIndex(long index) { + public MemorySegment getAtIndex(long index) { return segment.getAtIndex(Memory.POINTER, index); } - public void setAtIndex(long index, MemoryAddress value) { + public void setAtIndex(long index, MemorySegment value) { segment.setAtIndex(Memory.POINTER, index, value); } } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/ShortArray.java b/src/notzed.nativez/classes/au/notzed/nativez/ShortArray.java index 57fb90d..dc751d4 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/ShortArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/ShortArray.java @@ -20,9 +20,6 @@ package au.notzed.nativez; import java.lang.foreign.*; import java.util.AbstractList; -import java.util.function.Function; -import java.util.function.BiFunction; -import java.util.List; public class ShortArray extends AbstractList implements Pointer { final MemorySegment segment; @@ -35,15 +32,15 @@ public class ShortArray extends AbstractList implements Pointer { return new ShortArray(segment); } - public static ShortArray createArray(MemoryAddress address, long length, MemorySession scope) { - return create(MemorySegment.ofAddress(address, length * Memory.SHORT.byteSize(), scope)); + public static ShortArray create(MemorySegment address, long length, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), length * Memory.SHORT.byteSize(), scope)); } - public static ShortArray createArray(MemoryAddress address, MemorySession scope) { - return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope)); + public static ShortArray create(MemorySegment address, SegmentScope scope) { + return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope)); } - public static ShortArray createArray(long length, SegmentAllocator alloc) { + public static ShortArray create(long length, SegmentAllocator alloc) { return create(alloc.allocateArray(Memory.SHORT, length)); } @@ -51,12 +48,14 @@ public class ShortArray extends AbstractList implements Pointer { return create(alloc.allocateArray(Memory.SHORT, values)); } - public final MemoryAddress address() { - return segment.address(); + @Override + public final MemorySegment address() { + return segment; } - public final MemorySession scope() { - return segment.session(); + @Override + public final SegmentScope scope() { + return segment.scope(); } @Override diff --git a/src/notzed.nativez/lib/code.api b/src/notzed.nativez/lib/code.api index f735f0f..6741734 100644 --- a/src/notzed.nativez/lib/code.api +++ b/src/notzed.nativez/lib/code.api @@ -53,8 +53,8 @@ code method { # callback function/types downcall {{ - public static FunctionPointer<{rename}> downcall(MemoryAddress addr$, MemorySession scope$) { - MemorySegment symbol$ = MemorySegment.ofAddress(addr$, 0, scope$); + public static FunctionPointer<{rename}> downcall(MemorySegment addr$, SegmentScope scope$) { + MemorySegment symbol$ = MemorySegment.ofAddress(addr$.address(), 0, scope$); MethodHandle {rename}$FH = Memory.downcall(symbol$, descriptor()); return new FunctionPointer<{rename}>( symbol$, @@ -78,13 +78,13 @@ code method { }} upcall {{ - public static FunctionPointer<{rename}> upcall({rename} target$, MemorySession scope$) { + public static FunctionPointer<{rename}> upcall({rename} target$, SegmentScope scope$) { interface Trampoline { {java-result} call({native-arguments}); } Trampoline trampoline = ({native-arguments}) -> { // frame? scope? - try (MemorySession upcallScope$ = MemorySession.openConfined()) { + try (Arena upcallScope$ = Arena.openConfined()) { {trampoline-result-define}target$.call({java-call}); {trampoline-result-return} } @@ -132,10 +132,10 @@ import au.notzed.nativez.*; {imports} public class {name} { - {name}(Function resolve, MemorySession scope) { + {name}(Function resolve, SegmentScope scope) { {init} } - public static {name} create(Function resolve, MemorySession scope) { + public static {name} create(Function resolve, SegmentScope scope) { return new {name}(resolve, scope); } {defines} @@ -171,8 +171,8 @@ public class {rename} implements Pointer { return new {rename}(segment); } - public static {rename} create(MemoryAddress address, MemorySession scope) { - return MemoryAddress.NULL != address ? create(MemorySegment.ofAddress(address, LAYOUT.byteSize(), scope)) : null; + public static {rename} create(MemorySegment address, SegmentScope scope) { + return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), LAYOUT.byteSize(), scope)) : null; } public static {rename} create(SegmentAllocator frame) { @@ -180,17 +180,13 @@ public class {rename} implements Pointer { } @Override - public final MemoryAddress address() { - return segment.address(); + public final MemorySegment address() { + return segment; } @Override - public final MemorySession scope() { - return segment.session(); - } - - public final MemorySegment segment() { - return segment; + public final SegmentScope scope() { + return segment.scope(); } {defines} @@ -221,20 +217,20 @@ public class {rename} implements Pointer, Array<{rename}> { return new {rename}(segment); } - public static {rename} create(MemoryAddress address, MemorySession scope) { - return MemoryAddress.NULL != address ? create(MemorySegment.ofAddress(address, LAYOUT.byteSize(), scope)) : null; + public static {rename} create(MemorySegment address, SegmentScope scope) { + return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), LAYOUT.byteSize(), scope)) : null; } public static {rename} create(SegmentAllocator frame) { return create(frame.allocate(LAYOUT)); } - public static {rename} createArray(MemoryAddress address, MemorySession scope) { - return MemoryAddress.NULL != address ? create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope)) : null; + public static {rename} createArray(MemorySegment address, SegmentScope scope) { + return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope)) : null; } - public static {rename} createArray(MemoryAddress address, long length, MemorySession scope) { - return MemoryAddress.NULL != address ? create(MemorySegment.ofAddress(address, LAYOUT.byteSize() * length, scope)) : null; + public static {rename} createArray(MemorySegment address, long length, SegmentScope scope) { + return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), LAYOUT.byteSize() * length, scope)) : null; } public static {rename} createArray(long length, SegmentAllocator alloc) { @@ -242,17 +238,13 @@ public class {rename} implements Pointer, Array<{rename}> { } @Override - public final MemoryAddress address() { - return segment.address(); + public final MemorySegment address() { + return segment; } @Override - public final MemorySession scope() { - return segment.session(); - } - - public final MemorySegment segment() { - return segment; + public final SegmentScope scope() { + return segment.scope(); } @Override @@ -291,30 +283,30 @@ import au.notzed.nativez.*; public class {rename} implements Pointer { - MemoryAddress address; - MemorySession scope; + MemorySegment address; + SegmentScope scope; - private {rename}(MemoryAddress address, MemorySession scope) { + private {rename}(MemorySegment address, SegmentScope scope) { this.address = address; this.scope = scope; {init} } - public static {rename} create(MemoryAddress address, MemorySession scope) { - return MemoryAddress.NULL != address ? new {rename}(address, scope) : null; + public static {rename} create(MemorySegment address, SegmentScope scope) { + return MemorySegment.NULL != address ? new {rename}(address, scope) : null; } - public static HandleArray<{rename}> createArray(long count, SegmentAllocator alloc) { - return HandleArray.createArray(count, alloc, {rename}::create); + public static HandleArray<{rename}> createArray(SegmentAllocator alloc, long count) { + return HandleArray.create(count, {rename}::create, alloc); } @Override - public MemoryAddress address() { + public MemorySegment address() { return address; } @Override - public MemorySession scope() { + public SegmentScope scope() { return scope; } @@ -355,7 +347,7 @@ code getset { public {type} get{rename}AtIndex(long index) { // option a: resolve an offset segment and asme as above with set=segment=segment // option b: an indexed varhandle - MemorySegment segment = segment().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize()); + MemorySegment segment = address().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize()); return {tojava}; } }} @@ -366,7 +358,7 @@ code getset { }} seti set=value=value set=segment=segment {{ public void set{rename}AtIndex(long index, {type} value) { - MemorySegment segment = segment().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize()); + MemorySegment segment = address().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize()); {setnative}; } }} @@ -383,7 +375,7 @@ code getset-frame { public {type} get{rename}AtIndex(long index) { // option a: resolve an offset segment and asme as above with set=segment=segment // option b: an indexed varhandle - MemorySegment segment = segment().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize()); + MemorySegment segment = address().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize()); return {tojava}; } }} @@ -394,7 +386,7 @@ code getset-frame { }} seti set=value=value set=segment=segment {{ public void set{rename}AtIndex(Frame frame$, long index, {type} value) { - MemorySegment segment = segment().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize()); + MemorySegment segment = address().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize()); {setnative}; } }} diff --git a/src/notzed.nativez/lib/code.pm b/src/notzed.nativez/lib/code.pm index 57af37c..65e8a9e 100644 --- a/src/notzed.nativez/lib/code.pm +++ b/src/notzed.nativez/lib/code.pm @@ -96,7 +96,7 @@ sub scanFields { $match->{name} = $m->{name}; $match->{rename} = $m->{rename}; - $match->{segment} = 'segment()'; + $match->{segment} = 'address()'; $match->{scope} = 'scope()'; push @members, { field=>$m, type=>$type, match=>$match }; diff --git a/src/notzed.nativez/lib/method.pm b/src/notzed.nativez/lib/method.pm index 263b632..1b4cced 100644 --- a/src/notzed.nativez/lib/method.pm +++ b/src/notzed.nativez/lib/method.pm @@ -14,7 +14,7 @@ sub fieldScope { } elsif ($m->{scope} eq 'instance') { 'scope()'; } else { - 'MemorySession.global()'; + 'SegmentScope.global()'; } } sub fieldScopeAction { @@ -56,7 +56,7 @@ sub new { $info->{rename} = $c->{rename}; my @list = map { apply('{type} {name}', $_) } grep { $_->{field}->{output} } @members; - push @list, 'MemorySession scope$' if ($c->{scope} =~ m/explicit/); + push @list, 'SegmentScope scope$' if ($c->{scope} =~ m/explicit/); $info->{'java-arguments'} = join ', ', @list; $info->{'native-arguments'} = join ', ', map { apply('{carrier} {name}', $_) } @members; @@ -67,7 +67,7 @@ sub new { my $m = $_->{field}; if ($m->{instance}) { - "(java.lang.foreign.Addressable)address()"; + "address()"; } elsif ($m->{implied}) { $m->{implied}; } else { @@ -92,7 +92,7 @@ sub new { } # for java upcalls, they have an explicit scope always - $info->{'java-call'} = join ', ', map { code::formatTemplate('{tojava}', { %{$_->{match}}, value=>'{name}', scope=>'upcallScope$' }) } grep { $_->{field}->{output} } @members; + $info->{'java-call'} = join ', ', map { code::formatTemplate('{tojava}', { %{$_->{match}}, value=>'{name}', scope=>'upcallScope$.scope()' }) } grep { $_->{field}->{output} } @members; $info->{'java-signature'} = code::formatFunctionSignature($api, [$result, @members]); $info->{'function-descriptor'} = code::formatFunctionDescriptor($api, [$result, @members]); @@ -107,7 +107,7 @@ sub new { } @members; $info->{'native-output-define'} = join "\n\t\t", map { apply('{carrieri} {name};', $_) } @output; - $info->{'native-output-init'} = join ";\n\t\t\t", map { apply('{type} {name}$h = {type}.createArray(1, frame$);', $_) } @output; + $info->{'native-output-init'} = join ";\n\t\t\t", map { apply('{type} {name}$h = {type}.create(1, frame$);', $_) } @output; $info->{'native-output-copy'} = join ";\n\t\t\t", map { apply('{name} = {name}$h.get(0);', $_) } @output; # also required for some tonative types? diff --git a/src/notzed.nativez/lib/types.api b/src/notzed.nativez/lib/types.api index 351e045..26728d3 100644 --- a/src/notzed.nativez/lib/types.api +++ b/src/notzed.nativez/lib/types.api @@ -50,10 +50,10 @@ type /^\[(?\d+)u64:(?[uif]\d+)\]$/ type {{ !$m->{array} ? 'PointerArray' : 'HandleArray<{typei}>' }} layout {{ 'MemoryLayout.sequenceLayout({length}, Memory.POINTER)' }} - tojava {{ !$m->{array} ? 'PointerArray.createArray({value}, {length}, {scope})' : 'HandleArray.createArray({value}, {length}, {typei}::create, {scope})' }} + tojava {{ !$m->{array} ? 'PointerArray.create({value}, {length}, {scope})' : 'HandleArray.create({value}, {length}, {typei}::create, {scope})' }} getsegment {{ '(MemorySegment){name}$SH.invokeExact({segment})' }} - getnative {{ !$m->{array} ? 'PointerArray.create({getsegment})' : 'HandleArray.create({getsegment}, (a, s) -> {typei}.createArray(a, Long.MAX_VALUE, s))' }} + getnative {{ !$m->{array} ? 'PointerArray.create({getsegment})' : 'HandleArray.create({getsegment}, (a, s) -> {typei}.create(a, Long.MAX_VALUE, s))' }} setnative; varhandle {{ 'final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}"));'."\n". @@ -61,8 +61,8 @@ type /^\[(?\d+)u64:(?[uif]\d+)\]$/ }} typei {{ ucfirst($typeSizes{$match->{ctype}}).'Array' }} - getnativei {{ '{typei}.createArray((MemoryAddress){name}$EH.get({segment}, {index}), Long.MAX_VALUE, {scope})' }} - setnativei {{ '{name}$EH.set({segment}, {index}, (Addressable)Memory.address({value}))' }} + getnativei {{ '{typei}.create((MemorySegment){name}$EH.get({segment}, {index}), Long.MAX_VALUE, {scope})' }} + setnativei {{ '{name}$EH.set({segment}, {index}, Memory.address({value}))' }} } @@ -92,8 +92,8 @@ type /^\[(?\d+)(?[uif]\d+)\]$/ copy= template=code:getbyvalue,code:getsetelement { layout {{ "MemoryLayout.sequenceLayout({length}, Memory.".uc($typeSizes{$match->{ctype}}).")" }} type {{ ucfirst($typeSizes{$match->{ctype}})."Array" }} - tojava {{ "{type}.createArray({value}, $match->{length}, {scope})" }} - tonative {{ "(Addressable)Memory.address({value})" }} + tojava {{ "{type}.create({value}, $match->{length}, {scope})" }} + tonative {{ "Memory.address({value})" }} lea {{ "(MemorySegment)$m->{name}\$SH.invokeExact({segment})" }} getnative {{ "{type}.create({lea})" }} setnative; @@ -113,7 +113,7 @@ type /^\[(?\d+)\[(?\d+)(?[uif]\d+)\]\]$/ layout {{ "MemoryLayout.sequenceLayout({length0}, MemoryLayout.sequenceLayout({length1}, Memory.".uc($typeSizes{$match->{ctype}})."))" }} type {{ ucfirst($typeSizes{$match->{ctype}})."Array" }} tojava {{ "{type}.create({value})" }} - tonative {{ "(Addressable)Memory.address({value})" }} + tonative {{ "Memory.address({value})" }} getnative {{ "{type}.create((MemorySegment)$m->{name}\$SH.invokeExact({segment}))" }} setnative; @@ -134,7 +134,7 @@ type /^u64:/ select=raw copy= { # pointer is an 'in' only pointer to pointer type /^u64:u64:/ select=raw-in copy= { - tonative {{ '(Addressable)frame$.allocate(Memory.POINTER, {value})' }} + tonative {{ 'frame$.allocate(Memory.POINTER, {value})' }} } # function pointer @@ -149,8 +149,8 @@ type /^u64:u64:(?[uif]\d+)$/ copy= { type {{ !$m->{array} ? 'PointerArray' : 'HandleArray<{typei}>' }} tojava {{ !$m->{array} - ? 'PointerArray.createArray({value}, {length}, {scope})' - : 'HandleArray.createArray({value}, {length}, (a, s) -> {typei}.createArray(a, {length}, s), {scope})' + ? 'PointerArray.create({value}, {length}, {scope})' + : 'HandleArray.create({value}, {length}, (a, s) -> {typei}.create(a, {length}, s), {scope})' }} typei {{ ucfirst($typeSizes{$match->{ctype}})."Array" }} } @@ -161,11 +161,11 @@ type /^u64:u64:\$\{(\w+)\}$/ copy= { type {{ !$m->{array} ? 'PointerArray' : 'HandleArray<{typei}>' }} tojava {{ !$m->{array} - ? 'PointerArray.createArray({value}, {length}, {scope})' - : 'HandleArray.createArray({value}, {length}, {typei}::create, {scope})' + ? 'PointerArray.create({value}, {length}, {scope})' + : 'HandleArray.create({value}, {length}, {typei}::create, {scope})' }} # tojavai ... ? - carrieri {{ "MemoryAddress" }} + carrieri {{ "MemorySegment" }} typei {{ "$data->{$m->{type}}->{rename}" }} tojavai {{ "{typei}.create({value}, {scope})" }} } @@ -176,8 +176,8 @@ type /^u64:u64:v$/ copy= { type {{ !$m->{array} ? 'PointerArray' : 'HandleArray' }} tojava {{ !$m->{array} - ? 'PointerArray.createArray({value}, {length}, {scope})' - : 'HandleArray.createArray({value}, {length}, PointerArray::create, {scope})' + ? 'PointerArray.create({value}, {length}, {scope})' + : 'HandleArray.create({value}, {length}, PointerArray::create, {scope})' }} } @@ -185,7 +185,7 @@ type /^u64:u64:v$/ copy= { # TODO: length, multiple flags? type /^u64:(?[ui]8)$/ select=array copy= { type {{ ucfirst($typeSizes{$match->{ctype}}).'Array' }} - tojava {{ '{type}.createArray({value}, Long.MAX_VALUE, {scope})' }} + tojava {{ '{type}.create({value}, Long.MAX_VALUE, {scope})' }} carrieri {{ "{typei}" }} typei {{ $typeSizes{$match->{ctype}} }} tojavai {{ "({typei}){value}" }} @@ -203,10 +203,10 @@ type /^u64:(?[ui]8)$/ select=segment copy= { # *i8 with no flag = String type /^u64:(?i8)$/ copy= { type {{ 'String' }} - tonative {{ '(Addressable)({value} != null ? frame$.copy({value}) : MemoryAddress.NULL)' }} + tonative {{ '(MemorySegment)({value} != null ? frame$.copy({value}) : MemorySegment.NULL)' }} setnative {{ '{name}$VH.set({segment}, {copynative})' }} - copynative {{ 'segment.session().allocateUtf8String({value})' }} + copynative {{ 'SegmentAllocator.nativeAllocator(segment.scope()).allocateUtf8String({value})' }} tojava {{ '({value}).getUtf8String(0L)' }} } @@ -214,8 +214,8 @@ type /^u64:(?i8)$/ copy= { # *Primitive fallback type /^u64:(?[uif]\d+)$/ copy= { type {{ ucfirst($typeSizes{$match->{ctype}})."Array" }} - tonative {{ '(Addressable)Memory.address({value})' }} - tojava {{ ucfirst($typeSizes{$match->{ctype}})."Array.createArray({value}, Long.MAX_VALUE, {scope})" }} + tonative {{ 'Memory.address({value})' }} + tojava {{ ucfirst($typeSizes{$match->{ctype}})."Array.create({value}, Long.MAX_VALUE, {scope})" }} carrieri {{ "{typei}" }} typei {{ $typeSizes{$match->{ctype}} }} tojavai {{ "({typei}){value}" }} @@ -226,7 +226,7 @@ type /^u64:(?[uif]\d+)$/ copy= { type /^u64:\$\{(\w+)\}$/ select=array-size copy= { type {{ "$data->{$m->{type}}->{rename}" }} length {{ 'get'.($m->{'array-size'}->{rename}).'()' }} - tojava {{ "$data->{$m->{type}}->{rename}.createArray({value}, {length}, {scope})" }} + tojava {{ "$data->{$m->{type}}->{rename}.create({value}, {length}, {scope})" }} } # *type struct? handle? @@ -261,7 +261,7 @@ type /^\$\{(\w+)\}$/ byvalue template=code:getbyvalue { carrier {{ 'MemorySegment' }} type {{ "$data->{$m->{type}}->{rename}" }} tojava {{ '{type}.create({value})' }} - tonative {{ '({value}).segment()' }} + tonative {{ '({value}).address()' }} getnative {{ '{type}.create((MemorySegment){name}$SH.invokeExact({segment}))' }} setnative; varhandle {{ 'final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}"));'."\n" }} @@ -277,7 +277,7 @@ type template=code:getset { type copy= { layout {{ 'Memory.POINTER' }} - carrier {{ 'MemoryAddress' }} - type {{ 'MemoryAddress' }} - tonative {{ '(Addressable)Memory.address({value})' }} + carrier {{ 'MemorySegment' }} + type {{ 'MemorySegment' }} + tonative {{ 'Memory.address({value})' }} } diff --git a/src/notzed.nativez/native/native.make b/src/notzed.nativez/native/native.make index 48d90af..14cbfb9 100644 --- a/src/notzed.nativez/native/native.make +++ b/src/notzed.nativez/native/native.make @@ -1,8 +1,9 @@ notzed.nativez_NATIVE_LIBRARIES = export -notzed.nativez_COMMANDS=export-api export-defines generate-api -notzed.nativez_LIBRARIES=api.pm code.api code.pm config.pm method.pm tokenise.pm types.api +notzed.nativez_COMMANDS=export-api export-defines generate-api generate-api2 +notzed.nativez_LIBRARIES=api.pm code.api code.pm config.pm method.pm tokenise.pm types.api \ + Format.pm API.pm # JMOD target $(notzed.nativez_libdir)/%: src/notzed.nativez/lib/% -- 2.39.2