首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用NativeCall减轻Rakudo中的bug?

如何用NativeCall减轻Rakudo中的bug?
EN

Stack Overflow用户
提问于 2019-02-05 20:11:00
回答 1查看 88关注 0票数 4

我希望能够在具有REPR CStruct/CPointer的类中使用双指针:

代码语言:javascript
复制
typedef struct CipherContext {
          void    *cipher;
    const uint8_t *key;
          size_t   key_len;
    const uint8_t *path;
          size_t   path_len;
          size_t   block_size;
          void    *handle;

          int      (*cipher_init)(void **, const uint8_t *, size_t);
          int      (*cipher_encode)(void *, const uint8_t *, uint8_t *, size_t);
          int      (*cipher_decode)(void *, const uint8_t *, uint8_t *, size_t);
          void     (*cipher_free)(void *);
    const uint8_t *(*cipher_strerror)(int);
} CipherContext;

      int            cipher_context_init(CipherContext **, const uint8_t *, size_t, const uint8_t *, size_t, size_t);
      int            cipher_context_encode(CipherContext *, const uint8_t *, uint8_t *, size_t);
      int            cipher_context_decode(CipherContext *, const uint8_t *, uint8_t *, size_t);
      void           cipher_context_free(CipherContext *);
const uint8_t       *cipher_context_strerror(int);

Perl 6代码:

代码语言:javascript
复制
method new(Blob :$key!, Str :$path!, Int :$block-size!) {
    my Pointer[::?CLASS]     $ptr     .= new;
    my Int                   $err      = cipher_context_init($ptr, $key, $key.elems, $path, $path.codes, $block-size);
    return $ptr.deref unless $err;

    my Str $errstr = cipher_context_strerror($err) || do {
        my &cipher-strerror = nativecast(:(int32 --> Str), $!cipher-strerror);
        cipher-strerror($err)
    };
    die "Failed to initialize cipher context: $errstr";
}

submethod DESTROY() {
    cipher_context_free(self)
}

短距离高尔夫:

代码语言:javascript
复制
use v6.d;
use Nativecall;

class Foo is repr('CPointer') { 
    my Pointer[::?CLASS] $foo .= nw;
}

只是我不知道怎么做,因为一个Rakudo的bug。是否有更好的方法来处理代码的C部分中的错误(这就是为什么我这样写它)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-05 22:14:48

这一失败的原因与此相同:

代码语言:javascript
复制
class Foo {...}

BEGIN Foo ~~ Bool; # <------

class Foo{
}

问题的一部分似乎是,当调用Foo方法时,Pointer.^parameterize还没有组成。

所以它还不是Any的一个子类型。(甚至Mu)

解决方法是在使用.^compose之前在BEGIN相位器中添加Pointer[::?CLASS]调用。

代码语言:javascript
复制
class Foo is repr('CPointer') {
  BEGIN ::?CLASS.^compose;

  my Pointer[::?CLASS] $foo .= new;
}

我的猜测是,真正的解决办法是将Bool.ACCEPTS(Bool:U: \topic)候选人更改为Bool.ACCEPTS(Bool:U: Mu \topic)

我认为这是因为同样的错误也失败了:

代码语言:javascript
复制
Mu ~~ Bool
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54542286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档