Declaring an array inside a Perl 6 NativeCall CStr

2019-06-17 10:46发布

Is there any way to declare an array of objects inside a CStruct?

struct my_struct {
    int foo;
    int bar;
    char somestring[80];
};

class My::Struct is repr('CStruct') {
    has int32 $.foo;
    has int32 $.bar;
    ???
}

A CArray[uint8] would be a char * pointer, not actually reserving space inside the struct.

Instead of My::Struct.new, I could probably make the memory myself (instead of My::Struct.new(), I use a buf8.allocate(xxx) and keep a handle so the GC doesn't reap it, nativecast it to My::Struct), then I have to use pointer math to find the string inside the struct, etc. but it seems like there should be an easier way.

Even if it wasn't fully implemented, a simple way to say "Put 80 bytes here and here's a Pointer to it" would be very nice.

1条回答
成全新的幸福
2楼-- · 2019-06-17 11:15

Here is my ugly work-around:

class My::Struct is repr('CStruct') {
    has int32 $.foo is rw;
    has int32 $.bar is rw;
    has int64 $.h0; # 10 int64s = 80 bytes
    has int64 $.h1;
    has int64 $.h2;
    has int64 $.h3;
    has int64 $.h4;
    has int64 $.h5;
    has int64 $.h6;
    has int64 $.h7;
    has int64 $.h8;
    has int64 $.h9;

    method somestring {
        nativecast(Str, Pointer.new(nativecast(Pointer, self)+8))
    }

    sub strcpy(Pointer, Blob, --> Pointer) is native {}

    method set-somestring(Str:D $s) {
        my $buf = "$s\0".encode;
        die "too long" if $buf.bytes > 80;            
        strcpy(Pointer.new(nativecast(Pointer, self)+8), $buf);
    }
}
查看更多
登录 后发表回答