> On Mon, Feb 3, 2020 at 12:41 AM Darren Duncan <dar...@darrenduncan.net
> <mailto:dar...@darrenduncan.net>> wrote:
>
> Raku has the Native Call interface
> https://docs.raku.org/language/nativecall
> which is what you use instead. -- Darren Duncan
>
> On 2020-02-02 6:36 p.m., wes park wrote:
> > HI
> >
> > In perl5 we can use the underline C library for example JSON C
> with XS interface.
> > In perl6 how can we implement it?
> >
> > Thanks
> > Wes
>
>
>
> --
> Aureliano Guedes
> skype: aureliano.guedes
> contato: (11) 94292-6110
> whatsapp +5511942926110
On 2020-02-03 03:32, Aureliano Guedes wrote:
This is nice.
Sorry for my ignorance but what is the advantage in use CArray
insteadĀ of a usual raku array?
https://docs.raku.org/language/nativecall#CArray_methods
Hi Wes,
CArray allows you to create an array that is left alone
by Perl6 magic. And Native Call knows how to
create pointers to it as well.
A few tips on CArray.
It is nice to have things a bit more readable with
declared constants, A few examples:
constant BYTE := uint8;
constant WCHAR := uint16;
constant DWORD := uint32;
constant WCHARS := CArray[WCHAR];
To declare a CArray, constrain it as to the bit
collections (byte, word, dword, etc) and then
populate the array.
Note that you have to use the `.new` method to create
the array. After that you can add to the end at will.
The following is a CArray of BYTES (note the "S" at the end)
pre-populated with 0xFF (256) $nSize times. You can
leave BYTES off as CArray will figure it out from
[BYTE].
my BYTES $lpBuffer = CArray[BYTE].new( 0xFF xx $nSize );
To pre-populated WORDS with 0x00 (nuls) 24 times
would be
my $lpSubKey = CArray[uint16].new( 0x0000, 24 );
You can put things into a CArray one at a time too:
my $lpSubKey = CArray[uint8].new( 6,3,8 );
To populate a sting, first know if it is utf8 or utf16.
`.enclode.list` will create the proper utf structure
for you (little endian for utf16) .
my $cs = CArray[WCHAR].new($String.encode.list, 0);
Note the `, 0` at the end. This attaches the mandatory nul
required at the end of all C strings by n1570.
Tip: is asked for the byte count in $cs, note that it is words
not bytes, so double the number. Quadruple for DWORDS
my $csbytes = $cs.elems * 2;
Note, when analyzing things in a CArray that Raku will
pull cell value the out of the CArray (called unboxing)
and analyze them in perl space. This means that bytes
will be temporarily be seen as Int's (note the capitol
"I") and not a UInt.
This can cause some confusion as a byte containing
0xFF will be temporarily seen as "-1". The original
byte is not modified.
Also note the Int is a Larry Wall magic integer that
have no upper byte limit. It can be very useful.
Hope I have not confused.
-T