如何在 Perl 6 中声明固定大小的本机数组? [英] How to declare native array of fixed size in Perl 6?
问题描述
我正在尝试在 Perl 6 中声明以下 C 结构:
I'm am trying to declare the following C struct in Perl 6:
struct myStruct
{
int A[2]; //<---NEED to declare this
int B;
int C;
};
我的问题是我不知道如何使用内置的 NativeCall api 声明 int A[2];
部分.
My problem is that I don't know how to declare the int A[2];
part using the built in NativeCall api.
所以我所拥有的是:
class myStruct is repr('CStruct') {
has CArray[int32] $.A;
has int32 $.B;
has int32 $.C;
};
但是,我知道 有 CArray[int32] $.A;
部分是错误的,因为它没有在我的结构中声明仅占用 2 个 int32
尺寸.
However, I know that the has CArray[int32] $.A;
part is wrong as it does not declare a part in my struct that takes up ONLY 2 int32
sizes.
推荐答案
更新 2:事实证明这在我第一次发布此答案时不起作用,因此发表评论.我仍然尚未对其进行测试,但它肯定可以根据 Tobias 对 使用 NativeCall 将 CStruct 中的内联 CArray 传递到共享库.\o/
Update 2: It turned out that this didn't work at the time I first posted this answer, hence the comments. I still haven't tested it but it must surely work per Tobias's answer to Passing an inlined CArray in a CStruct to a shared library using NativeCall. \o/
我还没有测试过这个,但是在使用 Rakudo 编译器 2018.05 版时应该可以工作:
I haven't tested this, but this should work when using Rakudo compiler release 2018.05:
use NativeCall;
class myStruct is repr('CStruct') {
HAS int32 @.A[2] is CArray;
has int32 $.B;
has int32 $.C;
}
HAS
而不是has
导致属性内联而不是指针;HAS
instead ofhas
causes the attribute to be inline rather than a pointer;int32
而不是int
是因为 Perl 6int
类型与 C 的int
类型,但特定于平台(通常为 64 位);int32
instead ofint
is because the Perl 6int
type isn't the same as C'sint
type but is instead platform specific (and usually 64 bit);@
而不是$
将属性标记为Positional
(支持按索引查找值")而不是标量(被视为单一事物);@
instead of$
marks the attribute as beingPositional
("supports looking up values by index") instead of scalar (which gets treated as a single thing);[2]
将位置数据整形"为具有 2 个元素;[2]
"shapes" the Positional data to have 2 elements;is CArray
绑定一个CArray
作为Positional数据的容器逻辑;is CArray
binds aCArray
as the Positional data's container logic;今年 4 月的提交连接了
是 repr('CStruct')
使用声明的属性信息来适当分配内存.This commit from April this year wired up the
is repr('CStruct')
to use the declared attribute information to appropriately allocate memory.Fwiw 我从 搜索
CArray
的 #perl6 日志,发现它已经从 搜索 Rakudo 提交以获取提交消息标题.这篇关于如何在 Perl 6 中声明固定大小的本机数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!