Skip to content

jextract-swift: allow $direct access to properties when we know their offset/layout, and no side effects #30

Open
@ktoso

Description

@ktoso

This is a follow up to the "call the accessors" #24 part of properties.

We may consider implementing direct field access when we'd know the layout / offset / size of a field. These are direct memory access so could be more efficient than calling the Swift accessor methods, however they can only be safe to call when the fields have no.

This is likely a "later" task, as it'd only be a specific performance optimization vs. accessing by accessors, but it's worth exploring eventually.


    // --------------------------------------------------------------------------------------------------------
    // ==== len

    private static final OfLong len$LAYOUT = (OfLong)$LAYOUT.select(groupElement("len"));

    private static class len$property {
        public static final FunctionDescriptor DESC_GET = FunctionDescriptor.of(
                /* -> */ManualJavaKitExample.SWIFT_INT,
                /* self = */ ManualJavaKitExample.SWIFT_POINTER
        );
        public static final FunctionDescriptor DESC_SET = FunctionDescriptor.ofVoid(
                /* self = */ ManualJavaKitExample.SWIFT_POINTER,
                ManualJavaKitExample.SWIFT_INT
        );

        private static final String BASE_NAME = "$s14JavaKitExample12MySwiftClassC3lenSiv";
        public static final MemorySegment ADDR_GET = ManualJavaKitExample.findOrThrow(BASE_NAME + "g");
        public static final MemorySegment ADDR_SET = ManualJavaKitExample.findOrThrow(BASE_NAME + "s");

        public static final MethodHandle HANDLE_GET = Linker.nativeLinker().downcallHandle(ADDR_GET, DESC_GET);
        public static final MethodHandle HANDLE_SET = Linker.nativeLinker().downcallHandle(ADDR_SET, DESC_SET);
    }

    public static final OfLong len$layout() {
        return len$LAYOUT;
    }

    private static final long len$OFFSET = 8; // FIXME: we don't know yet

    public static final long len$offset() {
        return len$OFFSET;
    }

    public static FunctionDescriptor len$get$descriptor() {
        return len$property.DESC_GET;
    }
    public static MethodHandle len$get$handle() {
        return len$property.HANDLE_GET;
    }
    public static MemorySegment len$get$address() {
        return len$property.ADDR_GET;
    }

    public static long getLen(MemorySegment self) {
        var mh$ = len$property.HANDLE_GET;
        try {
            if (TRACE_DOWNCALLS) {
                traceDowncall("len$getter", self);
            }
            return (long) mh$.invokeExact(self);
        } catch (Throwable ex$) {
            throw new AssertionError("should not reach here", ex$);
        }
    }

    public static void getLen$direct(MemorySegment self) {
        // FIXME: we don't know the right offset yet, we need to get told in the .swiftinterface
        self.get(len$LAYOUT, len$OFFSET);
    }

    public static long getLen$direct(MemorySegment self) {
        return self.get(len$LAYOUT, len$OFFSET);
    }


    public static FunctionDescriptor len$set$descriptor() {
        return len$property.DESC_SET;
    }
    public static MethodHandle len$set$handle() {
        return len$property.HANDLE_SET;
    }
    public static MemorySegment len$set$address() {
        return len$property.ADDR_SET;
    }


    /**
     * Setter for field:
     * {@snippet lang = Swift :
     * var len: Int { set }
     * }
     */
    public static void setLen(MemorySegment self, long fieldValue) {
        var mh$ = len$property.HANDLE_SET;
        try {
            if (TRACE_DOWNCALLS) {
                traceDowncall("len$setter", self, fieldValue);
            }
            mh$.invokeExact(self, fieldValue);
        } catch (Throwable ex$) {
            throw new AssertionError("should not reach here", ex$);
        }
    }
    public static void setLen$direct(MemorySegment self, long fieldValue) {
        // FIXME: we don't know the right offset yet, we need to get told in the .swiftinterface
        self.set(len$LAYOUT, len$OFFSET, fieldValue);
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions