Fwd: Inspecting objects

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

Fwd: Inspecting objects

Thomas Nelson-10
I am very sorry if this goes out twice, I sent it out yesterday and I've
not seen it come back to me on the list or show up in the archives so
I'm guessing something went wrong at my end.

I am trying to add some introspection to my luabind objects and I am
starting to suspect that I'm missing something obvious.

I would like to check from C++ to see if an object has a value set on
the lua side that is not otherwise defined in the C++.

When I make the assignment I can watch the value get set in a metatable
via set_instance_value() and stepping into the lau_gettable (see
has_item_in_lua() below) call eventually drops me into
get_instance_value(), however the value that comes back seems to always
be nil. Any thoughts on where I'm going wrong?

abbreviated code sample

Lua:
obj = MyLuabindObject; -- this object has been declared via luabind
obj.Foobar = 1; -- this property was not declared in the via luabind, it
should exist only in the lua side of the slice.

C++:
class MyCppObject: public wrap_base
{ // stuff
MyCppObject() {}

bool has_item_in_lua(const char *name)
{
// this is the code that I'm wondering about.
m_self.get(L); // should push self's userdata onto the stack

lua_pushstring(L, name); // push the name of the member I'm
looking for onto the stack
lua_gettable(L, -2); // get the member's entry from
self (via metamethod)
lua_remove(L, -2); // remove self
if (!lua_isnil(L, -1)) // test result
return true;
lua_pop(L, 1);
return false;
}
};

//-----
module(L)[
class_<MyCppObject>("MyLuabindObj")
]




------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Inspecting objects

Thomas Nelson-10
Ok... Here is what I'm seeing

When I access the object from the lua side I'm getting one env table,
coming in from C++ gives me a different one. So if I set a property on
the Lua side the value is stored in the first table and is not visible
when I inspect the second (C++ visible) table.

One item that I left out of my description yesterday is that these are
smart pointer protected objects.  Is it possible the lua side is
accessing an env attached to the smart pointer/holder rather than the
object itself?

Thoughts? inspirations? epiphanies?

TomN

On 3/17/2010 9:30 AM, Thomas Nelson wrote:
> I am very sorry if this goes out twice, I sent it out yesterday and I've
> not seen it come back to me on the list or show up in the archives so
> I'm guessing something went wrong at my end.
>
> I am trying to add some introspection to my luabind objects and I am
> starting to suspect that I'm missing something obvious.
>
>    


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Solved. Re: Fwd: Inspecting objects

Thomas Nelson-10
Solved... My bad.  It turns out it had to do with how I was exporting
the object... turns out it's helpful to wait till after you've created
the instance before you inject a back ref.

Any interest on the list for a patch with the following enhancements:
1) Export a luabind::wrap_base derived object to an arbitrary
table/namespace in the lua from C++
2) Allow methods to be overridden on a per instance basis
3) Examine an object from C++ to determine if a method has been defined
on an instance.

todos
1) When exporting via item 1, allow the programmer to specify a lua class.
Right now you can only specify a C++ class that has been exported via
luabind.  I want to be able to export as a class that has been derived
on the lua side.

2) There are some problems with the function over rides that seem to be
based pretty deeply in how lua handles method calls with a self.
so defining:

function MainMenu:OnMenu(args)

     print("boo");

end

works, however

function foo(self, args)

     print ("ack");

end

MainMenu.OnMenu = foo;

breaks.

On 3/18/2010 9:27 AM, Thomas Nelson wrote:

> Ok... Here is what I'm seeing
>
> When I access the object from the lua side I'm getting one env table,
> coming in from C++ gives me a different one. So if I set a property on
> the Lua side the value is stored in the first table and is not visible
> when I inspect the second (C++ visible) table.
>
> One item that I left out of my description yesterday is that these are
> smart pointer protected objects.  Is it possible the lua side is
> accessing an env attached to the smart pointer/holder rather than the
> object itself?
>
> Thoughts? inspirations? epiphanies?
>
> TomN
>
> On 3/17/2010 9:30 AM, Thomas Nelson wrote:
>    
>> I am very sorry if this goes out twice, I sent it out yesterday and I've
>> not seen it come back to me on the list or show up in the archives so
>> I'm guessing something went wrong at my end.
>>
>> I am trying to add some introspection to my luabind objects and I am
>> starting to suspect that I'm missing something obvious.
>>      


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Solved. Re: Fwd: Inspecting objects

Nigel Atkinson-2
On Fri, 2010-03-19 at 11:11 -0700, Thomas Nelson wrote:
> Solved... My bad.  It turns out it had to do with how I was exporting
> the object... turns out it's helpful to wait till after you've created
> the instance before you inject a back ref.

Glad you got it sorted!

>
> Any interest on the list for a patch with the following enhancements:
> 1) Export a luabind::wrap_base derived object to an arbitrary
> table/namespace in the lua from C++
> 2) Allow methods to be overridden on a per instance basis

I thought you could already do this?

> 3) Examine an object from C++ to determine if a method has been defined
> on an instance.

I'd interested to see how you have done it...  at the moment I use a Lua
function to do that job, which I call from C++.

>
> todos
> 1) When exporting via item 1, allow the programmer to specify a lua class.
> Right now you can only specify a C++ class that has been exported via
> luabind.  I want to be able to export as a class that has been derived
> on the lua side.
>
> 2) There are some problems with the function over rides that seem to be
> based pretty deeply in how lua handles method calls with a self.
> so defining:
>
> function MainMenu:OnMenu(args)
>
>      print("boo");
>
> end
>
> works, however
>
> function foo(self, args)
>
>      print ("ack");
>
> end
> MainMenu.OnMenu = foo;
>
> breaks.

That's weird... it should work!

What about

function MainMenu.OnMenu( self, args )

        print( "ack" )

end

I'd be very surprised if that does not work.


Nigel


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Solved. Re: Fwd: Inspecting objects

Thomas Nelson-10
Here's the patch Nigel,

I've added a method called "has_method" to the wrap_base class.  This
method calls the deceptively named "do_call_member_selection" which
actually doesn't call a member selection, it instead leaves it on the
top of the stack. We check the result pop it and then return whether or
not it was a function.

It would be easy enough to expand that to check for any sort of member
and will save you a trip to the interpreter from your C code.


On 3/19/2010 2:56 PM, Nigel Atkinson wrote:

> On Fri, 2010-03-19 at 11:11 -0700, Thomas Nelson wrote:
>    
>> Solved... My bad.  It turns out it had to do with how I was exporting
>> the object... turns out it's helpful to wait till after you've created
>> the instance before you inject a back ref.
>>      
> Glad you got it sorted!
>
>    
>> Any interest on the list for a patch with the following enhancements:
>> 1) Export a luabind::wrap_base derived object to an arbitrary
>> table/namespace in the lua from C++
>> 2) Allow methods to be overridden on a per instance basis
>>      
> I thought you could already do this?
>
>    
>> 3) Examine an object from C++ to determine if a method has been defined
>> on an instance.
>>      
> I'd interested to see how you have done it...  at the moment I use a Lua
> function to do that job, which I call from C++.
>
>    
>> todos
>> 1) When exporting via item 1, allow the programmer to specify a lua class.
>> Right now you can only specify a C++ class that has been exported via
>> luabind.  I want to be able to export as a class that has been derived
>> on the lua side.
>>
>> 2) There are some problems with the function over rides that seem to be
>> based pretty deeply in how lua handles method calls with a self.
>> so defining:
>>
>> function MainMenu:OnMenu(args)
>>
>>       print("boo");
>>
>> end
>>
>> works, however
>>
>> function foo(self, args)
>>
>>       print ("ack");
>>
>> end
>> MainMenu.OnMenu = foo;
>>
>> breaks.
>>      
> That's weird... it should work!
>
> What about
>
> function MainMenu.OnMenu( self, args )
>
> print( "ack" )
>
> end
>
> I'd be very surprised if that does not work.
>
>
> Nigel
>
>    

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

luabind.diffs (8K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Solved. Re: Fwd: Inspecting objects

Daniel Wallin
In reply to this post by Thomas Nelson-10
On Fri, Mar 19, 2010 at 11:11:18AM -0700, Thomas Nelson wrote:
> Solved... My bad.  It turns out it had to do with how I was exporting
> the object... turns out it's helpful to wait till after you've created
> the instance before you inject a back ref.
>
> Any interest on the list for a patch with the following enhancements:
> 1) Export a luabind::wrap_base derived object to an arbitrary
> table/namespace in the lua from C++

What does "export" mean in this context?

> 2) Allow methods to be overridden on a per instance basis

This can be done with the code on the master branch, see:

  http://github.com/luabind/luabind/commit/4b52671

I couldn't apply the patch you sent later in this thread. Could you turn
it into a unified diff (diff -u)?

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Solved. Re: Fwd: Inspecting objects

Thomas Nelson-10
On 3/22/2010 2:36 AM, Daniel Wallin wrote:

> On Fri, Mar 19, 2010 at 11:11:18AM -0700, Thomas Nelson wrote:
>
>> Solved... My bad.  It turns out it had to do with how I was exporting
>> the object... turns out it's helpful to wait till after you've created
>> the instance before you inject a back ref.
>>
>> Any interest on the list for a patch with the following enhancements:
>> 1) Export a luabind::wrap_base derived object to an arbitrary
>> table/namespace in the lua from C++
>>
> What does "export" mean in this context?
>
Certainly, at the moment it behaves very similarly to luabind::globals
function and in fact it may be a good idea to ultimately put the
functionality there.  It is actually an interim step on the way allow me
to make a class created in C++ visible in the lua as a specified lua
subclass (I need that functionality for scene loading, but it is gonna
be a week or two before I get around to actually writing the code to do
this.)

>> 2) Allow methods to be overridden on a per instance basis
>>
> This can be done with the code on the master branch, see:
>
>    http://github.com/luabind/luabind/commit/4b52671
>
And that's what I get from working from the release.  How stable is the
trunk at the moment?  I'd rather use your code for this than mine.

> I couldn't apply the patch you sent later in this thread. Could you turn
> it into a unified diff (diff -u)?
>
Of course, It will be a couple days before I get it together though.


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Solved. Re: Fwd: Inspecting objects

Daniel Wallin
On Mon, Mar 22, 2010 at 09:17:03AM -0700, Thomas Nelson wrote:

> On 3/22/2010 2:36 AM, Daniel Wallin wrote:
> > On Fri, Mar 19, 2010 at 11:11:18AM -0700, Thomas Nelson wrote:
> >
> >> Solved... My bad.  It turns out it had to do with how I was exporting
> >> the object... turns out it's helpful to wait till after you've created
> >> the instance before you inject a back ref.
> >>
> >> Any interest on the list for a patch with the following enhancements:
> >> 1) Export a luabind::wrap_base derived object to an arbitrary
> >> table/namespace in the lua from C++
> >>
> > What does "export" mean in this context?
> >
> Certainly, at the moment it behaves very similarly to luabind::globals
> function and in fact it may be a good idea to ultimately put the
> functionality there.  It is actually an interim step on the way allow me
> to make a class created in C++ visible in the lua as a specified lua
> subclass (I need that functionality for scene loading, but it is gonna
> be a week or two before I get around to actually writing the code to do
> this.)

OK, I still don't understand what "export" means here, but I'll wait for
the unified diff and look at the code.

> >> 2) Allow methods to be overridden on a per instance basis
> >>
> > This can be done with the code on the master branch, see:
> >
> >    http://github.com/luabind/luabind/commit/4b52671
> >
> And that's what I get from working from the release.  How stable is the
> trunk at the moment?  I'd rather use your code for this than mine.

Well, it doesn't have the latest changes from the 0.9 branch, because
the v0.9.1 release hasn't happened yet, but it should be stable. In any
case, you should be able to cherry-pick that particular change.

> > I couldn't apply the patch you sent later in this thread. Could you turn
> > it into a unified diff (diff -u)?
> >
> Of course, It will be a couple days before I get it together though.

OK, thanks.

--
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user