How do I make this work?
Update: After searching Github, which includes the Raku spec-test, and here, I haven't found any examples of passing a CArray[of-structs]. Here there is a post by Christoph from 2017 which gives a "work around".
Christoph's solution likely works but would be better in NativeCall, if there is nothing better.
On Github there is a Rakudo test that uses a int TakeAStructArray(Struct **structs)
, which might help if you can write a C function to repackage its args to forward to a TakeAnArrayOfStruct( struct Struct[])
.
Below, JJMerelo seconds my suspicion that this fails due to a bug in Rakudo.
I have a C function that uses a timespec struct similar to that used in the NativeCall docs:
struct TS {
int show2( struct TS ts[2] ) { printf( "show2: (1) %ld %ld (2) %ld %ld\n", ts[0].ot, ts[0].one, ts[1].ot, ts[1].one); return 0; } which works fine when called from C.
Calling from Raku (moar) doesn't work:
class TS is repr('CStruct') {
has long $.ot;
has long $.one;
}
sub show2( CArray[TS] --> int32) is native(
'/home/rir/Raku/try-CArray/libshow.so'
) {*}
my $A = CArray[TS].new;
$A[1] = TS.new( :ot(50), :one(60));
$A[0] = TS.new( :ot(30), :one(40));
show2( $A);
say " s/b 30 40 50 60\n";
No errors, just results like:
show2: (1) 94658691693328 94658695469968 (2) 0 0
s/b 30 40 50 60
Analogous functions int show2long( long i[2] )
and int showTS(int show1( struct TS *ts )
work.