Sign in to follow this  
Matthew Shockley

Quick Help

Recommended Posts

Why does this return a function value when its called. It should return the table.
static int Object(lua_State* L) {
	const char *val = lua_tostring(L, -1);
	lua_newtable(L);
    lua_pushvalue(L, -1);
    lua_setmetatable(L, -2);
    lua_pushliteral(L, "__index");
    lua_pushcfunction(L, __index);
    lua_pushliteral(L, "__tostring");
    lua_pushcfunction(L, __tostring);
	Triangle Test;
	Test.Pointer = (int*)lua_topointer(L, -5);
	AddObject(Test);
    lua_settable(L, -5);
	return 1;
}

Share this post


Link to post
Share on other sites
apefish    176
Quote:
Original post by Matthew Shockley
Why does this return a function value when its called. It should return the table.

*** Source Snippet Removed ***


It looks like you are only calling settable once at the end, you should call it twice (as it looks like you are putting two things in your table. This returns a function because the thing on top is a function (because you didn't take it off and put in the table). The function you are returning is __index (__tostring got settabled).

Could you comment it a bit so we can see what your doing? Your style is much diff from mine, and Lua API is not very self-documenting.

Lets see if I understand correctly:

Takes one string argument (I would use positive indicies for args, in case they pass one extra or something), which is unused right now.

You create a table, make it it's own metatable, and then put an __index and __tostring cfunction in it (you should use settable immediately after pushing a name/val pair, so you have minimum stuff to think about on the stack).

Then you are doing something wierd with a traingle (comments please?). Keep in mind that topointer is NOT reversible, and not garbage collected.

The settable should go after the pushes, as I mentioned above.

Do I comprehend?

Share this post


Link to post
Share on other sites
m_switch    234
I meant to mention this earlier, C identifiers that begin with double underscores are reserved for the compiler implementation and should not be used. In other words, don't name your C functions __index or __tostring.

See this for more info.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this