Skip Menu |
Report information
Id: 132222
Status: open
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: david.warring <david.warring [at] gmail.com>
Cc:
AdminCc:

Severity: (no value)
Tag: Bug
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Subject: [BUG] 'HAS' Embedded C-Structs not working as documented
From: David Warring <david.warring [...] gmail.com>
Date: Thu, 5 Oct 2017 19:19:12 +1300
To: rakudobug [...] perl.org
Download (untitled) / with headers
text/plain 659b

class Point is repr('CStruct') {
    has uint16 $.x;
    has uint16 $.y;
}

class MyStruct2 is repr('CStruct') {
    HAS Point $.point;  # <-- embedded 
    has int32 $.flags;
}

my $s = MyStruct2.new;
say $s.flags;
say $s.point.defined;
say $s.point.x;

Produces:

0
False
Cannot look up attributes in a Point type object
 in block <unit> at /tmp/tst.pl line 15

Ie, the embedded struct is not automatically created and defined, when a new containing struct is created.

Rakudo version 2017.09-199-gc91c40115 built on MoarVM version 2017.09.1-62-g89ca8eb0 
implementing Perl 6.c.
Download (untitled) / with headers
text/plain 970b
On Wed, 04 Oct 2017 23:19:19 -0700, david.warring wrote: Show quoted text
> From > https://docs.perl6.org/language/nativecall#Embedding_CStructs_and_CUnions > > class Point is repr('CStruct') { > has uint16 $.x; > has uint16 $.y; > } > > class MyStruct2 is repr('CStruct') { > HAS Point $.point; # <-- embedded > has int32 $.flags; > } > > my $s = MyStruct2.new; > say $s.flags; > say $s.point.defined; > say $s.point.x; > > Produces: > > 0 > False > Cannot look up attributes in a Point type object > in block <unit> at /tmp/tst.pl line 15 > > Ie, the embedded struct is not automatically created and defined, when a > new containing struct is created. > > Rakudo version 2017.09-199-gc91c40115 built on MoarVM version > 2017.09.1-62-g89ca8eb0 > implementing Perl 6.c.
This is more an NYI than a bug... the code for that is more or less a first-pass draft. There are workarounds (see my xcb module), but they require surrendering your sanity to dark forces.
To: perl6-bugs-followup [...] perl.org
Date: Fri, 6 Oct 2017 07:59:52 +1300
From: David Warring <david.warring [...] gmail.com>
Subject: Re: [perl #132222] [BUG] 'HAS' Embedded C-Structs not working as documented
Download (untitled) / with headers
text/plain 1.6k
Thanks for that. As a worka-around Rakudo seems to do a better job, if I give it a helping hand viz a TWEAK method:

use NativeCall;
class Point is repr('CStruct') {
    has uint8 $.x;
    has uint8 $.y;
}

class MyStruct2 is repr('CStruct') {
    HAS Point $.point;  # <-- embedded 
    has int8 $.flags;
    method TWEAK {
        $!point := Point.new;
    }
}

say .name, ': ', .inlined for MyStruct2.^attributes;

my $s = nativecast(MyStruct2, buf8.new: 42,69,11);
say $s.flags;
say $s.point.defined;
say $s.point.x;
say $s.point.y;

Produces:

$!point: 1
$!flags: 0
11
True
42
69



On Fri, Oct 6, 2017 at 5:12 AM, Brian S. Julin via RT <perl6-bugs-followup@perl.org> wrote:
Show quoted text
On Wed, 04 Oct 2017 23:19:19 -0700, david.warring wrote:
> From
> https://docs.perl6.org/language/nativecall#Embedding_CStructs_and_CUnions
>
> class Point is repr('CStruct') {
>     has uint16 $.x;
>     has uint16 $.y;
> }
>
> class MyStruct2 is repr('CStruct') {
>     HAS Point $.point;  # <-- embedded
>     has int32 $.flags;
> }
>
> my $s = MyStruct2.new;
> say $s.flags;
> say $s.point.defined;
> say $s.point.x;
>
> Produces:
>
> 0
> False
> Cannot look up attributes in a Point type object
>  in block <unit> at /tmp/tst.pl line 15
>
> Ie, the embedded struct is not automatically created and defined, when a
> new containing struct is created.
>
> Rakudo version 2017.09-199-gc91c40115 built on MoarVM version
> 2017.09.1-62-g89ca8eb0
> implementing Perl 6.c.


This is more an NYI than a bug... the code for that is more or less
a first-pass draft.  There are workarounds (see my xcb module), but they
require surrendering your sanity to dark forces.




This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org