haskell / haskell/c2hs

Function hooks not using provided type for marshaling

Open
#251 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
211
Forks
52
PR merge metrics
No merged PRs in 30d

Description

Assuming a C function prototype like:
`bool getFooAtIndex(Ctx *ctx, uint64_t index, Foo *foo);`
where the return value indicates whether the call was successful. And when successful, the Foo structure pointed to by *foo should have it's Foo fields set appropriately. Foo is non-opaque, that is, we know the definition and we have a corresponding Haskell type and this type has a `Storable` instance.

How would we use c2hs to describe the bindings for this function?

My initial attempt looks like:
```{#fun unsafe getFooAtIndex as getFoo
{withPtr* `Ctx', fromIntegral `Word64', alloca- `Foo' peek*} -> `Bool' toBool #}
```

I then expect a `getFoo` function in Haskell with the type:
`Ctx -> Word64 -> IO (Bool, Foo)`

However, it appears c2hs doesn't use the type `Foo` provided in the third param description of the function hook to determine how much space to allocate or the type to return. So as is, this won't compile.

I _can_ get the bindings to build if I swap out `peek*` for a custom out-marshaller which converts a `Ptr ()` to a `Ptr Foo`. However, my concern is that if `alloca` thinks the type is `Ptr ()` then it is not using `Foo`'s `size` (from `Storable`) to allocate memory. Maybe worth noting that while there is a`Storable` instance for `Foo`, there is no `Pointer` instance.

Any ideas or suggestions where I could find an example to examine?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.