Handle to haskell data
Hi. I want to know if it is possible via FFI and GHC to allocate haskell data structures and return some kind of handle to their internal representation in the haskell runtime to another language that calls haskell code (c/c++). Say, for example, that I want code a simple 3d-modelling program which uses haskell to create and modify objects and c to use this functionality and to present it to the user. When an object is to be created the c code would call some haskell function "Create" which returns the handle to the created object so that it can be passed along to other haskell functions that modifies the object. The c code would never have to know of the haskell datastructures representing the object (Of course, there would have to be a haskell function that returns a datastructure suited for drawing the object). I had a quick look at the FFI-spec and saw that it is possible to allocate and pass pointers to Storable types. I just don't want to reconstruct my datastructures back and forth to Storable types each time I need to do something on the haskell side. Does any of this make sense? I should probably look into the spec more before ranting so much :) I don't have much haskell experience so maybe this is just something that wouldn't come up if I would design my code properly. /David Waern
On 30-Nov-2003, David Waern <davve@dtek.chalmers.se> wrote:
Hi. I want to know if it is possible via FFI and GHC to allocate haskell data structures and return some kind of handle to their internal representation in the haskell runtime to another language that calls haskell code (c/c++).
I haven't tried it myself, but I think the answer is yes: use the StablePtr type, which in C is the HsStablePtr typedef in "HsFFI.h". -- Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit The University of Melbourne | of excellence is a lethal habit" WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
Fergus Henderson <fjh@cs.mu.oz.au> wrote,
On 30-Nov-2003, David Waern <davve@dtek.chalmers.se> wrote:
Hi. I want to know if it is possible via FFI and GHC to allocate haskell data structures and return some kind of handle to their internal representation in the haskell runtime to another language that calls haskell code (c/c++).
I haven't tried it myself, but I think the answer is yes: use the StablePtr type, which in C is the HsStablePtr typedef in "HsFFI.h".
Yes, that is exactly what StablePtrs are meant for. See also the description of the various pointer types at http://www.reid-consulting-uk.ltd.uk/docs/ffi-Common_Problems.html Manuel
On Sunday 30 November 2003 6:43 pm, David Waern wrote:
Hi. I want to know if it is possible via FFI and GHC to allocate haskell data structures and return some kind of handle to their internal representation in the haskell runtime to another language that calls haskell code (c/c++).
Use StablePtrs. These are more or less a direct pointer to the Haskell data structure with just enough magic to keep the garbage collector happy. You can read more about StablePtrs in this paper: http://www.reid-consulting-uk.ltd.uk/alastair/publications/gfpw94/index.html Since that paper was written (1994), the names of the operations and their types has changed quite a bit. See the current specification for details: http://www.cse.unsw.edu.au/~chak/haskell/ffi/ -- Alastair Reid www.haskell-consulting.com
participants (4)
-
Alastair Reid -
David Waern -
Fergus Henderson -
Manuel M T Chakravarty