ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.18k stars 515 forks source link

Get function signature in C++ #1546

Closed glebm closed 11 months ago

glebm commented 11 months ago

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?

Rochet2 commented 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

```cpp #define SOL_ALL_SAFETIES_ON 1 #include template std::vector extractFunctionPointerSignature(Result (*)(Args...)) { std::vector signature; signature.push_back(sol::detail::demangle()); std::apply([&](auto... types) { (..., signature.push_back(sol::detail::demangle())); }, std::tuple()); return signature; } template std::vector extractFunctionPointerSignature(void (*)(Args...)) { std::vector signature; signature.push_back("void"); std::apply([&](auto... types) { (..., signature.push_back(sol::detail::demangle())); }, std::tuple()); return signature; } template auto EXTRACT_SIG(Func f) { // some template magic, here is an example that should work with function pointers // not sure how to do it for lambdas, but sol does it already somehow for lambdas so its probably possible auto sig = extractFunctionPointerSignature(f); return sol::as_table(sig); } void playSfx(int psfx) {} int playSfxLoc(int psfx, int x, int y) { return 0; } int main() { sol::state lua; lua.open_libraries(); #define WRAP(name, func) \ name, func, \ std::string("___")+name, EXTRACT_SIG(func) lua["test"] = lua.create_table_with( WRAP("playSfx", &playSfx), WRAP("playSfxLoc", &playSfxLoc) ); lua.script("print(test.playSfx, test.___playSfx and table.concat(test.___playSfx, ' '))"); lua.script("print(test.playSfxLoc, test.___playSfxLoc and table.concat(test.___playSfxLoc, ' '))"); return 0; } ```
glebm commented 11 months ago

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.

Rochet2 commented 11 months ago

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.

glebm commented 11 months ago

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.

glebm commented 11 months ago

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.

glebm commented 11 months ago

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