Håk*_*and 5 perl6 nativecall raku
这是" 如何在Perl 6中声明固定大小的本机数组? " 的后续问题.
在那个问题中讨论了如何将固定大小的数组合并到一个CStruct.在这个答案有人建议使用HAS内联CArray的CStruct.当我测试这个想法时,我遇到了一些在问题下面的评论部分无法解决的奇怪行为,所以我决定把它写成一个新问题.这是我的C测试库代码:
slib.c:
#include <stdio.h>
struct myStruct
{
int A;
int B[3];
int C;
};
void use_struct (struct myStruct *s) {
printf("sizeof(struct myStruct): %ld\n", sizeof( struct myStruct ));
printf("sizeof(struct myStruct *): %ld\n", sizeof( struct myStruct *));
printf("A = %d\n", s->A);
printf("B[0] = %d\n", s->B[0]);
printf("B[1] = %d\n", s->B[1]);
printf("B[2] = %d\n", s->B[2]);
printf("C = %d\n", s->C);
}
Run Code Online (Sandbox Code Playgroud)
要使用以下方法生成共享库:
gcc -c -fpic slib.c
gcc -shared -o libslib.so slib.o
Run Code Online (Sandbox Code Playgroud)
然后,Perl 6代码:
p.p6:
use v6;
use NativeCall;
class myStruct is repr('CStruct') {
has int32 $.A is rw;
HAS int32 @.B[3] is CArray is rw;
has int32 $.C is rw;
}
sub use_struct(myStruct $s) is native("./libslib.so") { * };
my $s = myStruct.new();
$s.A = 1;
$s.B[0] = 2;
$s.B[1] = 3;
$s.B[2] = 4;
$s.C = 5;
say "Expected size of Perl 6 struct: ", (nativesizeof(int32) * 5);
say "Actual size of Perl 6 struct: ", nativesizeof( $s );
say 'Number of elements of $s.B: ', $s.B.elems;
say "B[0] = ", $s.B[0];
say "B[1] = ", $s.B[1];
say "B[2] = ", $s.B[2];
say "Calling library function..";
say "--------------------------";
use_struct( $s );
Run Code Online (Sandbox Code Playgroud)
脚本的输出是:
Expected size of Perl 6 struct: 20
Actual size of Perl 6 struct: 24
Number of elements of $s.B: 3
B[0] = 2
B[1] = 3
B[2] = 4
Calling library function..
--------------------------
sizeof(struct myStruct): 20
sizeof(struct myStruct *): 8
A = 1
B[0] = 0 # <-- Expected 2
B[1] = 653252032 # <-- Expected 3
B[2] = 22030 # <-- Expected 4
C = 5
Run Code Online (Sandbox Code Playgroud)
问题:
为什么nativesizeof( $s )给24(而不是20的预期值)?
B从C函数打印时,为什么结构中数组的内容不符合预期?
注意:
我使用的是Ubuntu 18.04和Perl 6 Rakudo版本2018.04.01,但也测试了2018.05版本
小智 5
你的代码是正确的.我刚刚在MoarVM中修复了这个bug,并为rakudo添加了测试,类似于你的代码:
在C:
typedef struct {
int a;
int b[3];
int c;
} InlinedArrayInStruct;
Run Code Online (Sandbox Code Playgroud)
在Perl 6中:
class InlinedArrayInStruct is repr('CStruct') {
has int32 $.a is rw;
HAS int32 @.b[3] is CArray;
has int32 $.c is rw;
}
Run Code Online (Sandbox Code Playgroud)
请参阅以下补丁:https : //github.com/MoarVM/MoarVM/commit/ac3d3c76954fa3c1b1db14ea999bf3248c2eda1c https://github.com/rakudo/rakudo/commit/f8b79306cc1900b7991490eef822480f304a56d9
如果您不是直接从github的最新源构建rakudo(以及NQP和MoarVM),您可能必须等待将在此处出现的2018.08版本:https://rakudo.org/files
| 归档时间: |
|
| 查看次数: |
138 次 |
| 最近记录: |