ByteArray# as a foreign import argument?

Shao, Cheng cheng.shao at tweag.io
Thu Oct 10 21:32:22 UTC 2019


I've just manually checked with a `data ByteArray = ByteArray Int Int
ByteArray#` example, indeed GHC doesn't support marshalling it. That
being said, I'm still curious about the meaning of that blob of code
I've just pasted.

On Fri, Oct 11, 2019 at 5:28 AM chessai . <chessai1996 at gmail.com> wrote:
>
> Can you do that? I know with the UnliftedFFI you can use ByteArray# and if you have something like uint8_t*, it will make sure that the pointer lines up with the payload of the ByteArray#. You could always just wrap it in Data.Primitive.ByteArray.ByteArray, no? That is to say, I don't think GHC can do what you're asking.
>
> On Thu, Oct 10, 2019, 4:10 PM Shao, Cheng <cheng.shao at tweag.io> wrote:
>>
>> Yes, and specifically, the lifted ByteArray type as described in the
>> comments, not the unlifted ByteArray# type.
>>
>> On Fri, Oct 11, 2019 at 4:00 AM Csaba Hruska <csaba.hruska at gmail.com> wrote:
>> >
>> > Sorry, maybe I got it wrong. Are you asking if is there any package that passes ByteArrays via FFI?
>> >
>> > On Thu, Oct 10, 2019 at 8:54 PM Csaba Hruska <csaba.hruska at gmail.com> wrote:
>> >>
>> >> It's a primitive type.
>> >> https://gitlab.haskell.org/ghc/ghc/blob/master/compiler/prelude/primops.txt.pp#L1388
>> >> https://gitlab.haskell.org/ghc/ghc/wikis/commentary/prim-ops
>> >>
>> >> Cheers,
>> >> Csaba
>> >>
>> >> On Thu, Oct 10, 2019 at 8:16 PM Shao, Cheng <cheng.shao at tweag.io> wrote:
>> >>>
>> >>> Hello devs,
>> >>>
>> >>> I've been trying to figure out how to pass lifted types as foreign
>> >>> types, then encountered the following code in the `DsCCall` module
>> >>> (https://gitlab.haskell.org/ghc/ghc/blob/master/compiler/deSugar/DsCCall.hs#L172):
>> >>>
>> >>> ```
>> >>>   -- Byte-arrays, both mutable and otherwise; hack warning
>> >>>   -- We're looking for values of type ByteArray, MutableByteArray
>> >>>   --    data ByteArray          ix = ByteArray        ix ix ByteArray#
>> >>>   --    data MutableByteArray s ix = MutableByteArray ix ix
>> >>> (MutableByteArray# s)
>> >>>   | is_product_type &&
>> >>>     data_con_arity == 3 &&
>> >>>     isJust maybe_arg3_tycon &&
>> >>>     (arg3_tycon ==  byteArrayPrimTyCon ||
>> >>>      arg3_tycon ==  mutableByteArrayPrimTyCon)
>> >>>   = do case_bndr <- newSysLocalDs arg_ty
>> >>>        vars@[_l_var, _r_var, arr_cts_var] <- newSysLocalsDs data_con_arg_tys
>> >>>        return (Var arr_cts_var,
>> >>>                \ body -> Case arg case_bndr (exprType body) [(DataAlt
>> >>> data_con,vars,body)]
>> >>>               )
>> >>> ```
>> >>>
>> >>> It seems we allow a "ByteArray" type as a foreign import argument, if
>> >>> the third field of the datacon is a ByteArray# or MutableByteArray#.
>> >>> But I can't find such a ByteArray type definition in today's common
>> >>> packages. What's the rationale for this piece of code?
>> >>>
>> >>> Cheers,
>> >>> Cheng
>> >>> _______________________________________________
>> >>> ghc-devs mailing list
>> >>> ghc-devs at haskell.org
>> >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>> _______________________________________________
>> ghc-devs mailing list
>> ghc-devs at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


More information about the ghc-devs mailing list