How to define fixed-length strings in a Perl6 NativeCall structure?

I have a third-party C library that defines a structure similar to:

struct myStruct { int a; int b; char str1[32]; char str2[32]; }; 

And a function that takes a pointer to this structure and populates it. I need my own Perl6 call to provide this structure and then read the results.

So far, I have a framework defined in Perl6, like:

 class myStruct is repr('CStruct') { has int32 $.a; has int32 $.b; has Str $.str1; # Option A: This won't work as Perl won't know what length to allocate has CArray[uint8] $.str2; # Option B: This makes more sense, but again how to define length? # Also, would this allocate the array in place, or # reference an array that is separately allocated (and therefore not valid)? } 

And a native call like:

  sub fillStruct (myStruct) is native ('test_lib') {...}
 my $ struct = myStruct.new ();
 fillStruct ($ struct);  # Gives a seg fault with any variation I've tried so far

How can I do this job?

+5
source share
2 answers

As others have said, there is currently no way to achieve this.

I resorted to defining new C functions (s) as a workaround. The function effectively acts as an access method, returning only those fields that I need as discrete NativeCall pointers.

I hope that at some point, the community will begin to properly support this cause.

0
source

At the time of writing, this does not seem to be processing.
As a workaround, I would like the macro to generate 32 int8 by appropriately placing the field names.

-1
source

Source: https://habr.com/ru/post/1272211/


All Articles