clj-yaml

Ingy döt Net 2023-08-22T17:37:46.941029Z

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.

borkdude 2023-08-22T17:42:28.789489Z

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 @chris441 has done some stuff in the FFI area that works for both Clojure and GraalVM. @smith.adriane has too. Might be best to ping them somewhere, perhaps in the #clojure channel.

borkdude 2023-08-22T17:43:12.286349Z

I've personally only used the GraalVM FFI stuff directly

Ingy döt Net 2023-08-22T17:46:00.712799Z

thanks!

phronmophobic 2023-08-22T17:47:35.894439Z

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

phronmophobic 2023-08-22T17:49:31.769889Z

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

phronmophobic 2023-08-22T17:49:40.989929Z

https://github.com/java-native-access/jna

phronmophobic 2023-08-22T17:51:05.063559Z

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.

phronmophobic 2023-08-22T17:52:43.024099Z

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.

phronmophobic 2023-08-22T17:53:12.815169Z

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

phronmophobic 2023-08-22T17:56:37.941729Z

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).

phronmophobic 2023-08-22T18:00:49.896909Z

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 Net 2023-08-22T18:31:58.363319Z

Thanks @smith.adriane and pleasure to meet you!

👋 1
Ingy döt Net 2023-08-22T18:32:14.877559Z

I'll certainly ping you if needed.

👍 1
Ingy döt Net 2023-08-22T18:34:22.145519Z

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

Ingy döt Net 2023-08-22T17:40:08.426199Z

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 Net 2023-08-22T17:41:05.589199Z

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 Net 2023-08-22T17:41:22.044519Z

If anyone is interested in that let me know.

Ingy döt Net 2023-08-22T17:42:01.684539Z

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

phronmophobic 2023-08-22T17:44:47.859839Z

@smith.adriane has joined the channel