I am writing code for this. Side C:
struct bar* create_bar_ptr(unsigned char a) { printf("GET A CHAR => %#0x = %c\n", a, a); struct bar* bar = (struct bar*)malloc(sizeof(struct bar)); for (size_t i = 0;i < 5;i ++) { struct foo* tf = (struct foo*)malloc(sizeof(struct foo)); tf->a = a + i; bar->f[i] = tf; } printf("CREATE BAR PTR OK\n"); return bar; }
Since Rakudo does not support getting the stack variable from the C side, you should use malloc to place the struct bar on the heap.
Then compile the code with gcc, for example gcc -shared -fPIC -o libshasa.so xxx.c
And this is the side of Perl6:
use NativeCall; class foo is repr('CStruct') { has uint8 $.a; } class bar is repr('CStruct') { # Here you should use !!HAS!!, not has HAS Pointer[foo] $.f1; HAS Pointer[foo] $.f2; HAS Pointer[foo] $.f3; HAS Pointer[foo] $.f4; HAS Pointer[foo] $.f5; } sub create_bar_ptr(uint8) returns Pointer[bar] is native('./libshasa.so') { * } my Pointer[bar] $p = create_bar_ptr(uint8.new(97)); say $p.deref."f{$_}"().deref.a for 1 .. 5;
It is output:
GET A CHAR => 0x61 = a CREATE BAR PTR OK 97 98 99 100 101
araraloren
source share