Giter VIP home page Giter VIP logo

Comments (4)

ian-ross avatar ian-ross commented on May 28, 2024

I don't know who originally created this ticket in the old C2HS Trac, but here's my understanding of what's wanted here. Suppose we have a C header file issue46.h with contents:

typedef struct {
  int a;
  float b;
} oid;

void func(oid *obj, int aval, float bval);

then we'd like for the following C2HS code (the + syntax in the parameter list in the fun hook is new):

module Main where

#include "issue46.h"

{#pointer *oid as Oid foreign newtype#}

{#fun func as ^ {+, `Int', `Float'} -> `Oid'#}

to produce Haskell code like this:

module Main where

newtype Oid = Oid (ForeignPtr Oid)
withOid :: Oid -> (Ptr Oid -> IO b) -> IO b
withOid (Oid fptr) = withForeignPtr fptr

func :: Int -> Float -> IO Oid
func a2 a3 =
  mallocForeignPtrBytes 8 $ \oidfptr ->
  withForeignPtr oidfptr $ \oidptr ->
    let {a2' = fromIntegral a2} in
    let {a3' = realToFrac a3} in
    func'_ oidptr a2' a3' >>
    return $ Oid oidfptr

foreign import ccall safe "Issue46.chs.h func"
  func'_ :: Ptr (Oid) -> CInt -> CFloat -> IO ()

(some imports are missing).

Here, the + as a parameter in the fun hook marks that the argument to the corresponding C function is going to be used to do this sort of fast allocation and return.

Does this seem like a reasonable approach? From what I can tell, it ought to cover the requirements of the original ticket. It would be useful to know which C library the original writer of the ticket was working with, to help get some better ideas of how this might work.

from c2hs.

JohnLato avatar JohnLato commented on May 28, 2024

I may have written the original ticket, but I'm not convinced. Doesn't seem like my writing style. But it is something I've wanted before, and I think the proposed solution is pretty reasonable.

from c2hs.

ian-ross avatar ian-ross commented on May 28, 2024

@JohnLato Thanks for the comment, John. No-one else has offered an opinion, so I think I'll just go ahead and do things the way I'd planned. It seems like it should be a useful capability whoever originally asked for it.

from c2hs.

ian-ross avatar ian-ross commented on May 28, 2024

I've implemented this pretty much as outlined above. It works for a simple test case, but there's a strong chance that it will be possible to break things by some combination of the new "+" parameters and other C2HS features. I'll close the issue for now anyway.

from c2hs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.