raku - 本机 localtime() 段错误
问题描述
localtime
在尝试公开Perl 6 中的功能时,我似乎做错了什么:
use NativeCall;
my class TimeStruct is repr<CStruct> {
has int32 $!tm_sec;
has int32 $!tm_min;
has int32 $!tm_hour;
has int32 $!tm_mday;
has int32 $!tm_mon;
has int32 $!tm_year;
has int32 $!tm_wday;
has int32 $!tm_yday;
has int32 $!tm_isdst;
has Str $!tm_zone;
has long $!tm_gmtoff;
}
sub localtime(uint32 $epoch --> TimeStruct) is native {*}
dd localtime(time); # segfault
在 下运行perl6-lldb-m
,我得到:
Process 82758 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x5ae5dda1)
frame #0: 0x00007fffe852efb4 libsystem_c.dylib`_st_localsub + 13
libsystem_c.dylib`_st_localsub:
-> 0x7fffe852efb4 <+13>: movq (%rdi), %rax
0x7fffe852efb7 <+16>: movq %rax, -0x20(%rbp)
0x7fffe852efbb <+20>: movq 0x8e71d3e(%rip), %rbx ; lclptr
0x7fffe852efc2 <+27>: testq %rbx, %rbx
Target 0: (moar) stopped.
我在这里做错了什么明显的事情吗?
更新:最终的工作解决方案:
class TimeStruct is repr<CStruct> {
has int32 $.tm_sec; # *must* be public attributes
has int32 $.tm_min;
has int32 $.tm_hour;
has int32 $.tm_mday;
has int32 $.tm_mon;
has int32 $.tm_year;
has int32 $.tm_wday;
has int32 $.tm_yday;
has int32 $.tm_isdst;
has long $.tm_gmtoff; # these two were
has Str $.time_zone; # in the wrong order
}
sub localtime(int64 $epoch is rw --> TimeStruct) is native {*}
my int64 $epoch = time; # needs a separate definition somehow
dd localtime($epoch);
解决方案
localtime()
需要一个类型的指针time_t*
作为参数。假设time_t
并且uint32_t
是您特定平台上的兼容类型,
sub localtime(uint32 $epoch is rw --> TimeStruct) is native {*}
my uint32 $t = time;
dd localtime($t);
应该这样做(尽管除非您将属性公开,否则您将看不到任何东西)。
我有点惊讶您time_t
不是 64 位类型,并且刚刚 googleapple time.h
过,我还怀疑您的 struct 声明中的最后两个属性的顺序错误......
推荐阅读
- python - 使用 loc 的 Python 数据框意外显示错误
- vscode-remote - 创建 .devcontainer 存储库
- air - 气体传感器灵敏度特性
- algorithm - 如何在 dna 序列排序算法中从右上到左下填充矩阵
- javascript - 从字符串数组中查找最长的公共前缀
- python - 如何像使用 pwntools 获取 ELF 一样在 Python 中获取 PE 的核心文件?
- ios - 链接到旧版本的 iOS
- stripe-payments - 如何在 shopware 6 中使用 store-api 设置付款详情
- javascript - React useState 更改对象数组中数组中的所有元素
- html - 使用 column-count 会导致 flex div 将元素分成两半