Fork me on GitHub
#clj-yaml
<
2023-08-22
>
Ingy döt Net17:08:46

Is there a preferred FFI library for clojure? I want to swap out the snakeyaml usage in yamlscript/clojure for libfyaml. The result would need to work with graalvm.

borkdude17:08:28

For FFI you can use https://github.com/IGJoshua/coffi but I don't think this works with #graalvm and requires JDK19. Worth a shot though, I'm not sure. Graal has their own FFI stuff. You can read more about that here: https://yyhh.org/blog/2021/02/writing-c-code-in-javaclojure-graalvm-specific-programming/ Also @UDRJMEFSN has done some stuff in the FFI area that works for both Clojure and GraalVM. @U7RJTCH6J has too. Might be best to ping them somewhere, perhaps in the #clojure channel.

borkdude17:08:12

I've personally only used the GraalVM FFI stuff directly

phronmophobic17:08:35

There are lots of good options for using C libraries from clojure. Happy to answer any questions.

phronmophobic17:08:31

My experience is that JNA is the easiest to use at the moment.

phronmophobic17:08:05

I was able to compile a JNA demo with graalvm, https://github.com/phronmophobic/jna-native-image. It's only a demo project, but my impression is that more complicated JNA usage should also work.

phronmophobic17:08:43

There's a lot I really like about coffi, but one of their design decisions is to serialize/deserialize data for every ffi call. This makes sense for some APIs, but doesn't work for other APIs.

phronmophobic17:08:12

Also, as mentioned, coffi's ffi is coupled to specific JVM versions.

phronmophobic17:08:37

dtype-next's ffi is very solid, https://cnuernber.github.io/dtype-next/tech.v3.datatype.ffi.html. It works with multiple ffi implementations (including graalvm). I do seem to remember that there are issues with calling c functions with structs by value. I'm not sure if that's still a problem, but the issue is still open, https://github.com/cnuernber/dtype-next/issues/69. That may or may not be important for libfyaml (many APIs don't need this and there are workarounds).

phronmophobic18:08:49

My humble suggestion is to create a wrapper using https://github.com/phronmophobic/clong + JNA. The documentation for creating wrappers is a bit sparse, but there are several examples. I'd be happy to elaborate on recommendations for writing wrappers if you're interested.

Ingy döt Net18:08:58

Thanks @U7RJTCH6J and pleasure to meet you!

👋 2
Ingy döt Net18:08:14

I'll certainly ping you if needed.

👍 2
Ingy döt Net18:08:22

It's likely that serialization for this won't matter, but I'll look into all the options here

Ingy döt Net17:08:08

I wrote a port of YAMLScript in Clojure using snakeyaml to parse YAML into (clojure object) events. It's really simple: https://github.com/yaml/yamlscript/blob/clojure/clojure/src/yamlscript/parser.clj

Ingy döt Net17:08:05

I think a better yaml module for clojure could be written using snake for just the parsing like this, and everything else in clojure code.

Ingy döt Net17:08:22

If anyone is interested in that let me know.

Ingy döt Net17:08:01

also with FFI we could use libfyaml for parsing which is much more solid than snake.