lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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

The Lua code...


will execute as if it were coded as...

local __val = IndexHandler(ud, "SomeFunc")

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-----
[]On Behalf Of W. Scott
Sent: Thursday, February 26, 2004 9:07 AM
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:

That describes in detail what I am trying to do, but I must be missing

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
	 A() : iSomeVal(0){}
        int iSomeVal;
	int doSomething(int i);

And I want to be able to do this in lua:


myA.iSomeVal = 10;

local x = myA.iSomeVal;


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

    lua_pushliteral(L, "__index");
    lua_pushvalue(L, methodtable);
    lua_settable(L, metatable);

Where methodtable is a table filled with cfunctions and upvalues something


works great, but I am at a loss at how to implement the reading of member
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...