Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

triple pointer native call on perl 6

I try to wrap sd-bus with perl6, but have problem with a function call triple pointer.
from sd-bus.h

int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable); /* free the results */

try with native call

sub sd_bus_list_names(Pointer, Pointer[CArray[Str]] , Pointer[CArray[Str]] ) returns int32 is native('systemd') {*}

I call but I don't know how to dereferencies to array(@) the acquired and activable variable.

thank's, and sorry for my english

[EDIT]
dwarring reply solve my problem to derefencies Pointer[CArray[Str]]

this is a test code:

use v6;
use NativeCall;
sub strerror(int32) returns Str is native {*}
sub sd_bus_default_system(Pointer is rw) returns int32 is native('systemd') {*}
sub sd_bus_unref(Pointer) returns Pointer is native('systemd') {*}
sub sd_bus_list_names(Pointer,Pointer[CArray[Str]] is rw, Pointer[CArray[Str]] is rw ) returns int32 is native('systemd') {*}

my Pointer $bus .= new;
my int32 $error;

$error=sd_bus_default_system($bus);
if $error < 0 {
    my Str $ser = strerror(-$error);
    die "fail, can't test triple pointer, dbus return error $error $ser";
}

my Pointer[CArray[Str]] $acq .= new;
my Pointer[CArray[Str]] $act .= new;
$error=sd_bus_list_names($bus,$acq,$act);

my @love6acq;
loop (my $i = 0; $acq.deref[$i]; $i++){
    @love6acq.push: $acq.deref[$i];
}
@love6acq.say;

my @love6act;
loop (my $i = 0; $act.deref[$i]; $i++){
    @love6act.push: $act.deref[$i];
}
@love6act.say;

sd_bus_unref($bus);
like image 365
vbextreme vbextreme Avatar asked May 27 '18 14:05

vbextreme vbextreme


1 Answers

The following stand-alone experiment, works for me:

C code:

#include <stdio.h>
static char* strs[3] = { "howdy", "doody", NULL };
extern void ptr_to_strs (char ***ptr) {
  *ptr = strs;
}

Raku code:

use v6;
use LibraryMake;
use NativeCall;

sub testlib {
    state $ = do {
    my $so = get-vars('')<SO>;
    ~(%?RESOURCES{"lib/test$so"});
    }
}

sub ptr_to_strs(Pointer[CArray[Str]] $strs is rw) is native(&testlib)

my Pointer[CArray[Str]] $a .= new;
ptr_to_strs($a);
say $a.deref[0]; # howdy
say $a.deref[1]; # doody
say $a.deref[2]; # (Str)

Using this approach (but untested), you need to add is rw to the signature and create the pointers before calling:

# assuming you've already got a $bus object
sub sd_bus_list_names(Pointer, Pointer[CArray[Str]] is rw, Pointer[CArray[Str]] is rw) returns int32 is native('systemd') {*}

my Pointer[CArray[Str]] $acq .= new;
my Pointer[CArray[Str]] $act .= new;
sd_bus_list_names($bus, $acq, $act);
say $acq.deref[0]; # first acquired name
like image 173
dwarring Avatar answered Nov 11 '22 10:11

dwarring