[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: class binding question
- From: "Virgil Smith" <Virgil@...>
- Date: Thu, 26 Feb 2004 10:32:15 -0600
I may be completely misinterpreting your statements. However, it sounds
like your problem is that you called a "member function" from your __index
handler and then returned nil (or 0 return values). The reason that this is
a problem is that __index should return the appropriate table member
(whether it is a function or not), Lua will then "execute a call" on the
returned value if that is indicated by the code being handled.
For instance, assuming ud is a userdata with a metatable containing an
__index member that happens to be a function -- which is referred to here as
IndexHandler.....
The Lua code...
ud.SomeFunc()
will execute as if it were coded as...
local __val = IndexHandler(ud, "SomeFunc")
__val()
If this is a complete misinterpretation and/or is just not helpful then I
ask you to please give some more detail explaining your problem.
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of W. Scott
Dillman
Sent: Thursday, February 26, 2004 9:07 AM
To: lua@bazar2.conectiva.com.br
Subject: class binding question
I have a pretty noob question but I have several ways to solve my problem
and I think I just
need a nudge in the right direction.
I found Virgil Smith's post here:
http://lua-users.org/lists/lua-l/2003-12/msg00483.html
That describes in detail what I am trying to do, but I must be missing
something.
Here is what I am trying to do ( pretty common ), I know I can use all the
binding libraries
and add-ons out there, but I prefer to write my own.
Lets say I have a C++ class like the following:
class A
{
public:
A() : iSomeVal(0){}
int iSomeVal;
int doSomething(int i);
}
And I want to be able to do this in lua:
A("myA");
myA.iSomeVal = 10;
local x = myA.iSomeVal;
myA:doSomething(x);
I can get pieces of the above to work, but I am missing something somewhere.
If I use a method table to register my object and methods, something like
this:
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
Where methodtable is a table filled with cfunctions and upvalues something
like:
myA:doSomething(x)
works great, but I am at a loss at how to implement the reading of member
data
because something like:
local x = myA.iSomeVal;
returns whatever is in the table field, so if I store it as a fuction; x now
holds a function
reference, so I'm unsure how to implement it. Of course if I call:
local x = myA.iSomeVal();
It works, but is messy.
If I take the other route and do something like:
lua_pushliteral(L, "__index");
lua_pushcfunction(L, &myGetter);
lua_settable(L, metatable);
And do my decision making about function or member in the 'myGetter'
function, it works
well but lua throws an error after the execution of the statement, if I call
a function
complaining of a nil value, even though it executes fine.
Any advice would be great...
Thanks,
Scott