>From Parrot, I'm trying to access a struct within a struct. Given an
SDL_Surface named surface, the relevant C code is:
int bpp = surface->format->BytesPerPixel;
Given surface, a working UnManagedStruct representing an SDL_Surface (to
which I can draw -- that part works), I'd expect the following PIR to be
equivalent:
.local int bpp
bpp = surface[ 'format'; 'BytesPerPixel' ]
However this complains about the unknown key 'BytesPerPixel'.
The attached test patch demonstrates what I think is a problem. Running
gdb and looking through unmanagedstruct.pmc makes me wonder if the
PMC_val of the nested struct is ever actually assigned to the enclosing
struct.
Then again, I'm not a great C programmer and it is late here. I could
be doing things *completely* wrongly, in which case I'll write a
documentation patch to make things more clear after someone explains it
to me.
-- c
> int bpp = surface->format->BytesPerPixel;
> The attached test patch demonstrates what I think is a problem.
Yes, the code didn't handle this case where we are creating nested
structures, which are chained by pointers. Should be fixed now, i.e. the
test succeeds now.
Thanks, applied.
[NB: when extending nci_test.c please also provide an entry for
libnci.def]
leo
> Yes, the code didn't handle this case where we are creating nested
> structures, which are chained by pointers. Should be fixed now, i.e. the
> test succeeds now.
Thanks, that does make the test pass here.
Now for the case when applying a layout to an UnManagedStruct created
and returned from a C function, will this overwrite nested structs and
struct pointers?
Maybe the assignment belongs in ManagedStruct and UnManagedStruct should
grab the data from the right location according to the layout
initializer.
This test's a little harder to write, but if you call SDL_SetVideoMode,
apply the SDL_Surface layout to the return value, and try to access
surface->layout->BytesPerPixel, you'll see what I mean.
-- c
>> Yes, the code didn't handle this case where we are creating nested
>> structures, which are chained by pointers. Should be fixed now, i.e. the
>> test succeeds now.
> Thanks, that does make the test pass here.
Welcome.
> Now for the case when applying a layout to an UnManagedStruct created
> and returned from a C function, will this overwrite nested structs and
> struct pointers?
An UnManagedStruct layout doesn't have it's own memory. It's always
external. So when you *get* foo->bar->baz from NCI, the inner or both
structs are unmanaged (depending on who owns the struct).
Should work. And there is a test for it IIRC.
leo