hsc2hs icon indicating copy to clipboard operation
hsc2hs copied to clipboard

Adding bytearray read/write/index constructs

Open andrewthad opened this issue 6 years ago • 9 comments

This is an initial stab at resolving https://github.com/haskell/hsc2hs/issues/19. I'm not sure what I'm supposed to do in checkValidity though.

andrewthad avatar Feb 01 '19 21:02 andrewthad

Consider this still a work in progress. This currently builds and works as expected, but I'm still exploring the design space.

andrewthad avatar Feb 06 '19 12:02 andrewthad

I'm actually pretty satisfied with where this is now. I still need to add more documentation though. Would anyone mind reviewing this?

andrewthad avatar Apr 23 '19 11:04 andrewthad

I've pushed some changes that add a changelog entry and add some of the functions to the readme. Additionally, I've added a large and fairly complete example to the readme showing how to use hsc2hs to help write a Storable instance and a Prim instance.

andrewthad avatar Jun 11 '19 15:06 andrewthad

Open question: is #readByteArray really needed, or do we only need #readByteArrayHash. To write Prim instances, we only need the latter, but the former is nice to have in situations where I don't actually want to write out a full Prim instance.

andrewthad avatar Jun 11 '19 15:06 andrewthad

@RyanGlScott Would you mind reviewing this? Some of the functions are currently not described in readme, but other than that, I think this is in good shape.

andrewthad avatar Jun 11 '19 15:06 andrewthad

I'm wholly unqualified to give any sort of insights into how this code may be improved. But backing up a bit, I'm skeptical that these should live in hsc2hs in the first place, given that these macros don't appear to be usable outside of the specific context of the primitive library. Or am I misreading how these work? (I see references to indexByteArray and the like.)

RyanGlScott avatar Jun 11 '19 15:06 RyanGlScott

Originally, I was content to let these live outside of hsc2hs. I rolled them up into a custom.h file and everything was good. But then in MR313, I started needing them in GHC itself, and when building GHC, only macros that support cross-compilation are allowed. So, custom.h cannot live there. In the aforementioned MR, I just copied the definition of the Prim typeclass into a GHC internal module so that the code generated by hsc2hs's macros would still work out.

What it comes down to is that hsc2hs needs to let users work on unpinned memory. It currently doesn't do this. The de-facto standard typeclass for working with pinned memory is the one from primitive. Maybe there's a way to avoid using a typeclass at all. I think this would involve writing the haskell type in the #readByteArrayHash call. (Something like #{readByteArrayHash struct pollfd, fd, Fd}.) Then hsc2hs could avoid relying on typeclass resolution and pick out the indexing primitive from GHC.Prim directly. I think this would work.

andrewthad avatar Jun 11 '19 16:06 andrewthad

Not a maintainer, just leaving a comment to see if we can't move things along.

Mistuke avatar Jul 26 '20 12:07 Mistuke

ping

chessai avatar Oct 28 '20 01:10 chessai