Sign in to follow this  
stu_pidd_cow

[Lua/C++] Table of closures

Recommended Posts

stu_pidd_cow    3136
I'm trying to put closures into tables but it keeps crashing. This isn't my exact code but it does pretty much the same thing:
[code]
// create new table
lua_newtable( L );
int Table = lua_gettop( L );

// add closure 1 with 2 upvalues
lua_pushstring( L, "Closure1" );
lua_pushinteger( L, 1 );
lua_pushinteger( L, 2 );
lua_pushcclosure( L, Function, 2 );
lua_settable( L, Table );

// add closure 2 with 1 upvalue
lua_pushstring( L, "Closure2" );
lua_pushinteger( L, 1 );
lua_pushcclosure( L, Function, 1 );
lua_settable( L, Table );

// set the name of the table
lua_setglobal( L, "Table1" );
[/code]

The table should work like this:
[font="Courier New"]Table1.Closure1()
Table1.Closure2()[/font]

It always crashes at the lua_settable calls. I believe the reason for this is because the stack is supposed to be arranged with the key before the value, but doing that would mess up the closure.
Anyone know how to get around this?

Thanks.

Share this post


Link to post
Share on other sites
dmail    116
"[color=#1C2837][size=2]This isn't my exact code but it does pretty much the same thing" Please post the actual code or code which displays the problem as this code is fine.[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]"[/size][/color][color=#1C2837][size=2]I believe the reason for this is because the stack is supposed to be arranged with the key before the value" That is what the stack looks like in your example.[/size][/color]

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