Problem with passing pointers ( touserdata )

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with passing pointers ( touserdata )

Michael Bauroth
Hi,

I'm using an ARM port of Lua (Lua5.1-CE). Don't know if this matters. Additionally I'm using Lunar / Luna to register a class and some functions to Lua, e.g.

const char FComponent::className[] = "Component";
FLuna< FComponent >::RegType FComponent::methods[] = {
    method( FComponent, SetPosition ),
    method( FComponent, SetDimensions ),
    method( FComponent, SetBackground ),
    method( FComponent, SetParent ),
    { 0, 0 }
};

Creating the object from Lua works well, e.g.

comp = Component:new()
print( comp )

result in Lua is: Button (000ADE70)
pointer in C++: 0x000ade70

Both sides (C++ and Lua) show the same pointer value.
When I use this value in the next step to send it back from Lua to C++, the value differs in C++ !?

comp = Component:new()
print( comp )
comp:SetParent( comp )

--> comp is 000ADE70

int FComponent::SetParent( lua_State *L )
{
    void* value = ( void*)lua_touserdata( L, 1 );
    return 0;
}

--> value is 0x000adf28


What happens here? Am I missing something?
Help / hints would be very appreciated.

Thanks in advance.
Micha


Reply | Threaded
Open this post in threaded view
|

Re: Problem with passing pointers ( touserdata )

Peter Cawley
Many C++ binding frameworks do not store the object itself in a
userdata, but instead a pointer to the object. As lua_touserdata gives
a pointer to the userdata, you'll likely need to do something along
the lines of:

FComponent* value = *(FComponent**)lua_touserdata(L, 1);

2008/11/5 Michael Bauroth <[hidden email]>:
> Hi,
>
> I'm using an ARM port of Lua (Lua5.1-CE). Don't know if this matters.
> Additionally I'm using Lunar / Luna to register a class and some functions
> to Lua, e.g.
>
> const char FComponent::className[] = "Component";
> FLuna< FComponent >::RegType FComponent::methods[] = {
>    method( FComponent, SetPosition ),
>    method( FComponent, SetDimensions ),
>    method( FComponent, SetBackground ),
>    method( FComponent, SetParent ),
>    { 0, 0 }
> };
>
> Creating the object from Lua works well, e.g.
>
> comp = Component:new()
> print( comp )
>
> result in Lua is: Button (000ADE70)
> pointer in C++: 0x000ade70
>
> Both sides (C++ and Lua) show the same pointer value.
> When I use this value in the next step to send it back from Lua to C++, the
> value differs in C++ !?
>
> comp = Component:new()
> print( comp )
> comp:SetParent( comp )
>
> --> comp is 000ADE70
>
> int FComponent::SetParent( lua_State *L )
> {
>    void* value = ( void*)lua_touserdata( L, 1 );
>    return 0;
> }
>
> --> value is 0x000adf28
>
>
> What happens here? Am I missing something?
> Help / hints would be very appreciated.
>
> Thanks in advance.
> Micha
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Problem with passing pointers ( touserdata )

Michael Bauroth
Hi Peter,

it works like a charm now! :)

Many thanks for your quick response.

Keeps to mention, that a previous checking for nil pointers prevents the system from runtime errors (was my second fault here), e.g.

if ( !lua_isnil( L, 1 ) )
{
  FComponent* value = *( FComponent** )lua_touserdata( L, 1 );
}

Best Regards
Micha

Peter Cawley schrieb:
Many C++ binding frameworks do not store the object itself in a
userdata, but instead a pointer to the object. As lua_touserdata gives
a pointer to the userdata, you'll likely need to do something along
the lines of:

FComponent* value = *(FComponent**)lua_touserdata(L, 1);

2008/11/5 Michael Bauroth <[hidden email]>:
Hi,

I'm using an ARM port of Lua (Lua5.1-CE). Don't know if this matters.
Additionally I'm using Lunar / Luna to register a class and some functions
to Lua, e.g.

const char FComponent::className[] = "Component";
FLuna< FComponent >::RegType FComponent::methods[] = {
   method( FComponent, SetPosition ),
   method( FComponent, SetDimensions ),
   method( FComponent, SetBackground ),
   method( FComponent, SetParent ),
   { 0, 0 }
};

Creating the object from Lua works well, e.g.

comp = Component:new()
print( comp )

result in Lua is: Button (000ADE70)
pointer in C++: 0x000ade70

Both sides (C++ and Lua) show the same pointer value.
When I use this value in the next step to send it back from Lua to C++, the
value differs in C++ !?

comp = Component:new()
print( comp )
comp:SetParent( comp )

--> comp is 000ADE70

int FComponent::SetParent( lua_State *L )
{
   void* value = ( void*)lua_touserdata( L, 1 );
   return 0;
}

--> value is 0x000adf28


What happens here? Am I missing something?
Help / hints would be very appreciated.

Thanks in advance.
Micha