LuaGlue icon indicating copy to clipboard operation
LuaGlue copied to clipboard

Handling arguments default values

Open JMLX42 opened this issue 12 years ago • 6 comments

Hello,

please correct me if I'm wrong but currently C++ methods that support default values for one (or more) arguments do not work as expected if the Lua call does not provide a value for all arguments. I think the main reason is Lua is going to try to set all of the arguments anyway, thus corrupting the Lua stack.

I've encountered the problem multiple times, and defining the LUA_USE_APICHECK reveals the problem I think.

Is it the expected behavior for now? Can we fix this using lua_gettop() and pass only what's on the stack?

Thanks,

JMLX42 avatar Dec 24 '13 10:12 JMLX42

The fancy tuple unwrapping code may not like only doing partial unwrapping. And we'd open a larger hole where it'd happily pass too few arguments to functions.

Tomasu avatar Dec 24 '13 21:12 Tomasu

It's possible we can add a parameter to the method() function to pass the minimum number (X) of parameters, and LuaGlue can allow sending X or more parameters. It would complicate things a bit.

The other alternative is to just ignore the fact that those methods have optional parameters in lua.

Tomasu avatar Dec 29 '13 08:12 Tomasu

The other alternative is to just ignore the fact that those methods have optional parameters in lua.

I don't think that works. If you call a C++ method from Lua without the exact count of arguments (even counting the ones with default values) there is a segfault.

JMLX42 avatar Dec 30 '13 15:12 JMLX42

That's what I meant. Always pass the parameters.. It's annoying, but it might be the only solution.

I don't know of a good way to handle optional parameters. Even my idea of providing a min number of manditory parameters may not work due to C++ calling conventions and stuff.

Tomasu avatar Dec 30 '13 15:12 Tomasu

OK my bad.

Today the way I do it is to use overloading or decorator functions instead of default values.

JMLX42 avatar Dec 30 '13 15:12 JMLX42

I will at some point attempt to get it to work. I'm keeping it on my todo for now.

Tomasu avatar Dec 30 '13 15:12 Tomasu