Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'HAS' Embedded C-Structs not working as documented #6572

Open
p6rt opened this issue Oct 5, 2017 · 5 comments
Open

'HAS' Embedded C-Structs not working as documented #6572

p6rt opened this issue Oct 5, 2017 · 5 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Oct 5, 2017

Migrated from rt.perl.org#132222 (status was 'open')

Searchable as RT132222$

@p6rt
Copy link
Author

p6rt commented Oct 5, 2017

From @dwarring

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.

@p6rt
Copy link
Author

p6rt commented Oct 5, 2017

From @skids

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.

@p6rt
Copy link
Author

p6rt commented Oct 5, 2017

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Oct 5, 2017

From @dwarring

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​:

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.

@p6rt p6rt added the Bug label Jan 5, 2020
@dogbert17
Copy link

It seems to work a bit better now:

dogbert@dogbert-VirtualBox ~ $ perl6 -v
This is Rakudo version 2020.05.1-285-g8134470 built on MoarVM version 2020.05-85-g02c8cf7
implementing Raku 6.d.
dogbert@dogbert-VirtualBox ~ $ perl6 gist.pl6 
0
True
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants