#!/usr/bin/perl6
# your code goes here
#`(
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int array_w( char array[ 128 ] )
{
for ( int i = 0 ; i < 128 ; i ++ ) {
array
[ i
] = "1234567890ABCDEFGHIJKLM" [ rand ( ) % 23 ] ; }
array[ 127 ] = 0 ;
return 0 ;
}
int array_r( char array[ 128 ] ) {
}
typedef struct Array {
long flag;
char array[ 128 ] ;
} Array;
Array getArray( )
{
Array a = { .flag = 20 } ;
array_w( a.array ) ;
return a;
}
int getArray_ptr( Array * a)
{
a-> flag = 21 ;
array_w( a-> array) ;
return a-> flag;
}
)
use NativeCall;
constant LIB= "./rw" ;
sub array_w( CArray[ int8 ] ) of int32 is native( LIB) { * }
sub array_r( CArray[ int8 ] ) of int32 is native( LIB) { * }
my CArray[ int8 ] $a .= new;
$a[ $_] = 0 for ^ 128 ;
say array_w( $a) ;
say array_r( $a) ;
class XArray is repr( 'CStruct' ) {
has long $.flag ;
has CArray[ int8 ] $.array ;
submethod TWEAK( ) {
$! array = CArray[ int8 ] .new ;
$! array[ $_] = 0 for ^ 128 ;
}
method array( ) {
my Buf $buf .= new;
$buf[ $_] = $! array[ $_] for ^ 128 ;
$buf.decode ( 'utf8' ) ;
}
}
sub getArray( ) of XArray is native( LIB) { * }
sub getArray_ptr( Array is rw) of int32 is native( LIB) { * }
my XArray $x = getArray( ) ;
say $x.array ;
IyEvdXNyL2Jpbi9wZXJsNgojIHlvdXIgY29kZSBnb2VzIGhlcmUKCiNgKAoJI2luY2x1ZGUgPHN0ZGlvLmg+CgkjaW5jbHVkZSA8c3RkbGliLmg+CgkjaW5jbHVkZSA8dGltZS5oPgoJCglpbnQgYXJyYXlfdyhjaGFyIGFycmF5WzEyOF0pCgl7CgkgICAgc3JhbmQodGltZShOVUxMKSk7CgkgICAgZm9yIChpbnQgaSA9IDA7aSA8IDEyODtpICsrKSB7CgkgICAgICAgIGFycmF5W2ldID0gIjEyMzQ1Njc4OTBBQkNERUZHSElKS0xNIltyYW5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