Comments (4)
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.
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.
@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.
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)
- c2hs paper: Access Forbidden HOT 2
- Syntax error! The symbol `__attribute__' does not fit here. for time.h on OSX HOT 3
- Boolean struct members are incorrectly marshalled HOT 3
- Darwin: The symbol `_9' does not fit here. HOT 1
- Hackage version of c2hs fails to build with GHC-8.8.4 or 8.10.2 HOT 6
- pseudo block c and enum, creates wrong line counting
- Release schedule HOT 1
- Loosen bounds on language-c HOT 1
- Lexical error ! The character '#' does not fit here HOT 8
- Hackage tarball lacks tests
- Placeholder: removing `/=` from `Eq` HOT 2
- Haddock comments on enum constructors
- c2hs fails to produce the correct alignment HOT 14
- One more error with macos and time.h HOT 2
- capi calling convention
- Fails on Darwin with "clang: error: no input files"
- Paper Link Broken HOT 1
- _Bool type is broken
- Release a version where `cabal test` succeeds
- The symbol `[' does not fit here.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c2hs.