Closed glebm closed 11 months ago
As far as I know, it is not possible at the moment to get the info. Sol does not store it anywhere in accessible format.
But you can always try to store the info yourself. One possibility for example is to make some kind of wrapper that adds both the function and the signature in some format to the table here https://github.com/diasurgical/devilutionX/blob/5821d2305caeac6add7aef95a91b5a0cf1871501/Source/lua/modules/audio.cpp#L21
I dont have the time or knowhow to implement it, but the basic idea I have is the following, where each function is wrapped in a helper macro that in addition to storing the function in the table, stores the signature of the stored function. This is a quick example I made that more or less works with function pointers, but not lambdas.
https://godbolt.org/z/jcWaKjoKa
I've implemented this for C++ functions manually (https://github.com/diasurgical/devilutionX/blob/master/Source/lua/metadoc.hpp).
Can this be done somehow automatically for functions defined in Lua? There is this StackOverflow answer here https://stackoverflow.com/a/24216007/181228 but I'm not sure how to port it to sol or how to even know if the function is a native Lua function or a binding to a C++ one.
how to even know if the function is a native Lua function or a binding to a C++ one.
I dont know if sol has a function for that, but you can use https://www.lua.org/manual/5.4/manual.html#lua_iscfunction
Can this be done somehow automatically for functions defined in Lua?
Well, all functions you bind from sol will be C++ functions that do not have any useful signature in lua.
There is this StackOverflow answer here https://stackoverflow.com/a/24216007/181228 but I'm not sure how to port it to sol
You can use it as is for lua functions. It wouldnt work for the C++ functions anyways.
Thanks, I've now added the code for Lua functions (https://github.com/diasurgical/devilutionX/pull/6784) from that StackOverflow answer.
Now the only issue is that we don't show anything for stdlib functions. The standard library is pretty small so I guess that can be done manually as well.
Since it looks like nothing more can be done at the moment, closing.
Is this the correct way to call lua_iscfunction
?
sol::object value = ...;
cons bool isC = lua_iscfunction(value.lua_state(), value.registry_index()) != 0;
It seems to always return false for me.
Ah, it needs a stack index. I did this, not sure if it's the best way:
value.push(value.lua_state());
const bool isC = lua_iscfunction(value.lua_state(), -1) != 0;
lua_pop(value.lua_state(), 1);
We use sol2 to provide a basic autocomplete in the game's terminal:
https://github.com/diasurgical/devilutionX/assets/216339/fc322f33-75fc-49f8-9949-9a4dc39ba81e
Code: https://github.com/diasurgical/devilutionX/blob/master/Source/lua/autocomplete.cpp
It'd be neat if the autocomplete could show some information about the function's arguments. Is there a way to get that via sol2?