GithubHelp home page GithubHelp logo

libffi-scala-native's Introduction

libffi Bindings for Scala Native

circleci shield

Scala Native's C foreign function interface, while very simple to use, still lacks a few key features. This library provides an alternative to the built-in FFI by using libffi โ€” the same underlying library used by JNA.

The only reason you might want to use this is if you need to bind to library functions that receive struct arguments by value, or return a struct by value. Presumably, the need for this library will eventually go away.

Usage

In a Scala Native project, add a resolver to the repository where this library is published, and add a %%% dependency:

resolvers += Resolver.bintrayRepo("nadavwr", "maven"),
libraryDependencies += "com.github.nadavwr" %%% "libffi-scala-native" % "0.5.0"

Every parameter type must have an instance of FfiType[_] in implicit scope. Primitive instances are already defined, so in practice you will only need to do this for structs.

For example, to match libc's div_t struct

typedef struct {
	int quot;		/* quotient */
	int rem;		/* remainder */
} div_t;

we will need to define FfiType[DivT] as follows:

import com.github.nadavwr.ffi._
  
type DivT = CStruct2[CInt, CInt]
implicit class DivOps(private val ptr: Ptr[DivT]) extends AnyVal {
  def quot: CInt = !ptr._1
  def rem: CInt = !ptr._2
}
  
implicit val ffiTypeOfDivT = FfiType.struct[DivT]("div_t", FfiType[CInt], FfiType[CInt])

We can then define bindings for libc's div function, which returns a struct:

val libc = Module.open("libc.dylib") // "libc.so" for Linux
val div = libc.prepare[CInt, CInt, DivT]("div")
val num = stackalloc[CInt]; !num = 10
val denom = stackalloc[CInt]; !denom = 4
val result = stackalloc[DivT]
div(num, denom)(result) // last argument points to result
  
println(s"div(10, 4) = ${result.quot} (${result.rem})")
assert(result.quot == 2)
assert(result.rem == 2)
  
// alternative "inline" allocation style
val result2 = div(num, denom)(stackalloc[DivT])

Limitations

  • The return value and all arguments must be preallocated, and provided to bound functions as pointers.
  • There is no facility to unload shared libraries once they've been loaded.
  • Expect a performance drop typical of libffi. Depending on your needs, this may be negligible.

libffi-scala-native's People

Contributors

nadavwr avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.