develooper Front page | perl.perl6.users | Postings from May 2020

Re: NativeCall questions

Thread Previous | Thread Next
Curt Tilmes
May 8, 2020 11:36
Re: NativeCall questions
Message ID:
On Fri, May 8, 2020 at 6:44 AM David Santiago <> wrote:
> I'm porting some personal perl5 scripts to Raku, and  one of those
> scripts  is using Inline::C.
> [...]
> Why? How do i fix it?

I haven't tried all of this, but the first thing that leaps out is
that repr('CStruct') is
not a struct -- it is a pointer to a struct.

Your 'return ed' will return the struct by value, not by reference/pointer.

To return that, you need a signature like this:
EncodedData *encode(unsigned char* data, size_t data_size)
and return &ed.

Also your struct and CStruct are defining the contents in the reverse
order.  They must
match up exactly.

> Also the read method from the IO::Handle returns a Buf. I would like
> to pass it directly to my C function. Is there any change in the
> C/raku code i should do?
> The data field from class EncodedData is type "str". Should it be
> CArray[uint8] instead?

Str is good for things that are Nul-terminated and can be safely
parsed as unicode strings.
(I generally use Str instead of str even for NativeCall stuff.)

Here you are passing in the size, though.  You have to be careful
using '.chars' to get
the size in bytes.  They might not be the same in all cases.

I would recommend just passing it in as a 'Blob'.
sub encode(Blob $data, size_t $size --> EncodedData) is
native('lib/MyLib/') {*};
my $buf = "my string".encode;
my $encoded = encode($buf, $buf.bytes);

Using str in your CStruct for the return should be fine if the
returned string is Nul-terminated
and can be safely parsed.  Otherwise define it as CArray[uint8].


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About