Skip to content

Marshal allows you to conveniently create native library bindings with FFM API.

License

Notifications You must be signed in to change notification settings

Over-Run/marshal

Repository files navigation

Marshal

Java CI with Gradle

Marshal allows you to conveniently create native library bindings with FFM API.

See wiki for more information.

This library requires JDK 22 or newer.

Overview

import overrun.marshal.*;
import overrun.marshal.gen.*;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentAllocator;
import java.lang.invoke.MethodHandles;

/**
 * GLFW constants and functions
 */
interface GLFW {
    /**
     * The instance of the loaded library
     */
    GLFW INSTANCE = Downcall.load(MethodHandles.lookup(), "libglfw3.so");

    /**
     * A field
     */
    int GLFW_KEY_A = 65;

    /**
     * Sets the swap interval.
     *
     * @param interval the interval
     */
    void glfwSwapInterval(int interval);

    /**
     * Gets the window position.
     * Marks the array parameter as a place to store the value returned by C
     *
     * @param window the window
     * @param posX   the position x
     * @param posY   the position y
     */
    void glfwGetWindowPos(MemorySegment window, @Ref int[] posX, @Ref int[] posY);

    /**
     * Creates a window.
     * Requires a segment allocator to allocate the string;
     * if the first parameter is not segment allocator, then the memory stack is used
     *
     * @param allocator the segment allocator
     * @param width     the width
     * @param height    the height
     * @param title     the title
     * @param monitor   the monitor
     * @param share     the share
     * @return the window
     */
    MemorySegment glfwCreateWindow(SegmentAllocator allocator, int width, int height, String title, MemorySegment monitor, MemorySegment share);
}

class Main {
    public static void main(String[] args) {
        GLFW glfw = GLFW.INSTANCE;
        int key = GLFW.GLFW_KEY_A;
        glfw.glfwSwapInterval(1);

        // Arena
        try (var arena = Arena.ofConfined()) {
            MemorySegment windowHandle = glfw.glfwCreateWindow(arena,
                800,
                600,
                "The title",
                MemorySegment.NULL,
                MemorySegment.NULL);

            // ref by array
            int[] ax = {0}, ay = {0};
            glfw.glfwGetWindowPos(windowHandle, ax, ay);
        }
    }
}

Import

Import as a Gradle dependency:

dependencies {
    implementation("io.github.over-run:marshal:0.1.0-alpha.28-jdk22")
}

and add this VM argument to enable native access: --enable-native-access=io.github.overrun.marshal
or this if you don't use modules: --enable-native-access=ALL-UNNAMED

Additions

About

Marshal allows you to conveniently create native library bindings with FFM API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages