Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Sized ints/nums in structs don't work (WAS: returning CStructs doesnt work) #9

Closed
FROGGS opened this Issue · 6 comments

2 participants

@FROGGS
Collaborator

I have a test script that acesses functions from libSDL, that work good so far.
But there are functions that return structs like the following:

typedef struct SDL_version {
    Uint8 major;
    Uint8 minor;
    Uint8 patch;
} SDL_version;

This one doesn't look that complicated so lets try to run a test:

use lib 'lib';
use SDL;

say "SDL::init:      ", SDL::init( 1 );
say "SDL::was_init:  ", SDL::was_init( 0 );

SDL::delay( 200 );
say "SDL::get_ticks: ", SDL::get_ticks( );

my $version = SDL::linked_version( );
say "SDL::Version: ",   $version.major; # <--- this is the thing I care about
say "SDL::Version: ",   $version.minor;
say "SDL::Version: ",   $version.patch;

This one should be put as SDL.pm6in the lib subdir:

module SDL;

use NativeCall;

class SDL_version is repr('CStruct') {
    has int8 $.major;
    has int8 $.minor;
    has int8 $.patch;
}

our sub init( int32 )       returns Int     is native('libSDL') is symbol('SDL_Init')       { * }
our sub was_init( int32 )   returns Int     is native('libSDL') is symbol('SDL_WasInit')    { * }
our sub get_ticks( )        returns Int     is native('libSDL') is symbol('SDL_GetTicks')   { * }
our sub delay( int32 )                  is native('libSDL') is symbol('SDL_Delay')      { * }
our sub linked_version( )   returns SDL_version is native('libSDL') is symbol('SDL_Linked_Version') { * }

1;

The expected output is:

SDL::init:      0
SDL::was_init:  1
SDL::get_ticks: (around 200)
SDL::Version: 1  # <---
SDL::Version: 2
SDL::Version: (like 7 to 15 )

but actually I get:

SDL::init:      0
SDL::was_init:  1
SDL::get_ticks: 206
SDL::Version: -4294049279 # <---
SDL::Version: 0
SDL::Version: 0

So it looks like it can't figure out where the Uint8's are...
I'm currently get stucked and don't know where to look.

@arnsholt
Collaborator

The problem is that we don't actually support sized ints quite properly yet. I'm working on it, but my supply of tuits is low for the time being.

One possible workaround is to define the version struct as simply with { has int $!version } and then chunk out the chars manually with bitshifts and masks, but that might result in a segfault if you're unlucky.

@FROGGS
Collaborator

Hi, to let you know, doing the following instead:

class SDL_version is repr('CStruct') {
    has int $!version;
    method major() {
        return sprintf( "%X", $!version );
    }
}

gives me FFFFFFFF000E0201 for SDL_version.major. So reading from the back (01, 02, 0E) looks quiet good.

@FROGGS
Collaborator

Would be great if I could help out a bit, since I need way more structures than the simple one above.
I can remember structures that stores pixel data of surfaes for example.

Can you give me any hint what file needs to be twiddled with to get this working properly?

EDIT: this is a resource to start what has to be done: http://perl.active-venture.com/pod/perlpacktut-packing.html

@FROGGS
Collaborator

found it... I'll let you know if I got something to show...

@arnsholt
Collaborator

Yeah, that should do the trick for now. I'll update and close the issue once I merge the sized stuff.

As for the other stuff, what kind of features is it you need?

@arnsholt
Collaborator

The dyncall-sized-num branch has finally landed in NQP, so this problem should be history now. If you run into more problems with this, it'll be bugs in the sized member handling.

@arnsholt arnsholt closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.