Add additional FFI types and pointer operations#6131
Conversation
Includes various types that are used as phantoms in pointer types. E.G. `Ptr Nat32`. This reprsents the actual type of the pointer, but when reading/writing, operations act in terms of `Nat` with conversion.
a26e4ab to
18dfbce
Compare
|
Base tests pass. |
Some float operations had the wrong type. Small-width types were missing kinds
|
@dolio go ahead and merge if you're ready |
|
Yeah, I'm making a couple of additions before merging. Paul wanted a way to access byte array contents. |
To get a pointer to the memory of a byte array, this adds `PinnedByteArray.contents`. However, since even pinned arrays are garbage collected, it's not safe to work with these pointers while dropping references to the actual array. To rectify this problem, this also adds the `IO.keepAlive` builtin. This is a function that maintains a reference to a given value (by storing it in the continuation) while executing a block. As an incidental change, pointer casting has been given a more direct implementation that doesn't go through a 'foreign' function.
|
Okay. The new additions are
|
|
I don't know if I get the need for the Like you could literally just define it in pure Unison and it would just ignore its first argument and force the second: Or is there more to it than that? |
|
That function doesn't work. It tail calls The builtin tail calls |
|
Gotcha. Okay cool. At first I was thinking I could write something like this: Now However, I can appreciate that in the future, a fancier Unison runtime optimizer might do something else here that lets |
This PR fills in some FFI types, and adds the ability to work with raw pointers.
int8anduint8to the FFI type specs.Nat16. These don't have values themselves, but are used in other types.Ptrtype. It's parameterized by its type, and the practically usable parameters are those that correspond to C types.set : Ptr Nat16 -> Nat ->{IO} ()ptrFFI type that allows passing/returning pointers to/from FFI imports.Note, it's also possible to have pointers to pointers and so on. E.G. there are functions for working with
Ptr (Ptr Int32).I extended the dll-ffi tests to do some operations exercising the pointer ffi. But I haven't exhaustively tested all the pointer operations in any test.