Advertisement

[Lua/C++] Table of closures

Started by July 14, 2011 04:14 AM
1 comment, last by stu_pidd_cow 13 years, 4 months ago
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:

// 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" );


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.
"[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.
[color=#1C2837][size=2]

[color=#1C2837][size=2]"[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.
Advertisement
Ah, it turns out I was putting in the wrong integer into lua_pushcclosure. Works now. Thanks for the help.

This topic is closed to new replies.

Advertisement