]> uap-core.de Git - rssreader.git/commitdiff
fix MemorySegment access
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 28 Jun 2025 05:47:23 +0000 (07:47 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 28 Jun 2025 05:47:23 +0000 (07:47 +0200)
ui-java/src/main/java/de/unixwork/ui/CUtils.java
ui-java/src/main/java/de/unixwork/ui/Toolkit.java

index 49e364d4cb6bd2fa54ab282dc66c16eb6edf832a..0e74ac81a472ea9d09e15558067edb922de3403b 100644 (file)
@@ -17,7 +17,7 @@ public class CUtils {
         byte[] utf8 = s.getBytes(StandardCharsets.UTF_8);
         try {
             long size = utf8.length + 1;
-            MemorySegment cstr = (MemorySegment) tk.malloc.invoke(size);
+            MemorySegment cstr = ((MemorySegment) tk.malloc.invoke(size)).reinterpret(size);
             ByteBuffer buf = cstr.asByteBuffer();
             buf.put(utf8);
             buf.put((byte) 0);
index 60e3deafa795ff4eeb1adf81f552d366e3e155de..fcc1d595255009bdd43c77637a2be1dddcf8284c 100644 (file)
@@ -343,19 +343,38 @@ public class Toolkit {
      * list getvaluefunc wrapper
      */
     public static MemorySegment getValue(MemorySegment list, MemorySegment elm, int row, int col, MemorySegment userdata, MemorySegment freeValue) {
+        if(userdata == MemorySegment.NULL) {
+            return MemorySegment.NULL;
+        }
+        userdata = userdata.reinterpret(16); // long[2]
         long ctxPtr = userdata.getAtIndex(ValueLayout.JAVA_LONG, 0);
         long converterIndex = userdata.getAtIndex(ValueLayout.JAVA_LONG, 1);
 
-        long listCtxPtr = list.getAtIndex(ValueLayout.JAVA_LONG, 0);
-        long listIndex = list.getAtIndex(ValueLayout.JAVA_LONG, 1);
+        ListFuncs listfuncs = ListFuncs.getInstance();
+
+        MemorySegment listdata = null; // long[2]
+        try {
+            listdata = ((MemorySegment)listfuncs.list_get_data.invoke(list)).reinterpret(16);
+        } catch (Throwable e) {
+            throw new RuntimeException(e);
+        }
+        long listCtxPtr = listdata.getAtIndex(ValueLayout.JAVA_LONG, 0);
+        long listIndex = listdata.getAtIndex(ValueLayout.JAVA_LONG, 1);
         Context listCtx = Toolkit.getInstance().getContext(listCtxPtr);
         UiList uilist = listCtx.getList((int)listIndex);
 
+        Object listElm = null;
+        try {
+            listElm = uilist.get((int) row);
+        } catch (Throwable e) {
+            return MemorySegment.NULL;
+        }
+
         Context ctx = Toolkit.getInstance().getContext(ctxPtr);
         ListValueConverter conv = ctx.getValueConverter((int)converterIndex);
-        Object value = conv.getValue(null, col);
+        Object value = conv.getValue(listElm, col);
         if(value instanceof String) {
-            freeValue.setAtIndex(ValueLayout.JAVA_BOOLEAN, 0, true);
+            freeValue.reinterpret(1).setAtIndex(ValueLayout.JAVA_BOOLEAN, 0, true);
             return CUtils.cstring((String)value);
         }