Skip to content

Jire/easyffm

Repository files navigation

easyFFM

Easy declarative foreign function & memory API, utilizing Java 19's FFM preview

Currently a work-in-progress. Snapshots only. Not for use in production.

Define a library and functions

interface User32 {
    fun GetKeyState(virtKey: Int): Short
}

Use a library's functions

val user32 = foreignLibrary(User32::class)
val keyState = user32.GetKeyState(KeyEvent.VK_SPACE)
println("Key state: $keyState")

Define a struct

@FieldOrder(["x", "y"])
interface Point : ForeignStruct {
    var x: Int
    var y: Int
}

Create a struct and use it

val point = foreignStruct(Point::class)
point.y = 6
println("x=${point.x}   y=${point.y}") // x=0   y=6

You can use structs in functions, too

interface Mouse {
    fun position(): Point
    fun move(point: Point)
}

Structs support arrays and nesting

@FieldOrder(["points"])
interface Points : ForeignStruct {
    @StructArray(10) // array size
    val points: Array<Point>
}

About

Easy declarative foreign function & memory API, utilizing Java 19's FFM preview

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages