将结构数组传递给 Perl 6 NativeCall 函数

Cur*_*mes 6 nativecall raku

我正在尝试使用 NativeCall 与一些 C 函数进行交互。

我有一个简单的 C 结构体和一个需要它们的数组的函数。

struct foo {
    int x;
    char *s;
};

struct foo foo_array[3];

foo_array[0].x = 12;
foo_array[0].s = "foo";
foo_array[1].x = 27;
foo_array[1].s = "bar";
foo_array[2].x = -1;

void somefunc(foo_array);
Run Code Online (Sandbox Code Playgroud)

我尝试了很多方法,但似乎无法完全正确。

class foo is repr('CStruct') {
    has int32 $.x;
    has Str $.s
};

sub somefunc(CArray[foo]) is native { * }

my @foo-array := CArray[foo].new;

@foo-array[0] = ???
@foo-array[1] = ???
@foo-array[2] = ???

somefunc(@foo-array);
Run Code Online (Sandbox Code Playgroud)

如何正确创建 foo 类的对象并设置它们的值,以及如何制作适合传递的数组?

Chr*_*oph 5

据我所知,没有内置的方法可以做到这一点。不过,有足够的绳子上吊自杀 建立一个解决方法

role StructArray[Mu:U \T where .REPR eq 'CStruct'] does Positional[T] {
    has $.bytes;
    has $.elems;

    method new(UInt \n) {
        self.bless(bytes => buf8.allocate(n * nativesizeof T), elems => n);
    }

    method AT-POS(UInt \i where ^$!elems) {
        nativecast(T, Pointer.new(nativecast(Pointer, $!bytes) + i * nativesizeof T));
    }

    method pointer {
        nativecast(Pointer[T], $!bytes);
    }
}
Run Code Online (Sandbox Code Playgroud)

这应该允许以下工作:

my @foo-array := StructArray[foo].new(10); # 'array' with 10 elements
@foo-array[0].x = 42;
Run Code Online (Sandbox Code Playgroud)

@foo-array.pointer通过传递给 类型的参数可以与 C 函数进行交互Pointer[foo]。由于结构也通过指针传递,因此您也可以传递@foo-array[0]给类型参数foo以获得相同的效果。