Coder Social home page Coder Social logo

Get function signature in C++ about sol2 HOT 6 CLOSED

glebm avatar glebm commented on June 9, 2024
Get function signature in C++

from sol2.

Comments (6)

Rochet2 avatar Rochet2 commented on June 9, 2024

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

#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>

template <typename Result, typename... Args>
std::vector<std::string> extractFunctionPointerSignature(Result (*)(Args...)) {
    std::vector<std::string> signature;
    signature.push_back(sol::detail::demangle<Result>());
    std::apply([&](auto... types) {
        (..., signature.push_back(sol::detail::demangle<decltype(types)>()));
    }, std::tuple<Args...>());
    return signature;
}

template <typename... Args>
std::vector<std::string> extractFunctionPointerSignature(void (*)(Args...)) {
    std::vector<std::string> signature;
    signature.push_back("void");
    std::apply([&](auto... types) {
        (..., signature.push_back(sol::detail::demangle<decltype(types)>()));
    }, std::tuple<Args...>());
    return signature;
}

template<typename Func>
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;
}

from sol2.

glebm avatar glebm commented on June 9, 2024

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.

from sol2.

Rochet2 avatar Rochet2 commented on June 9, 2024

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.

from sol2.

glebm avatar glebm commented on June 9, 2024

Thanks, I've now added the code for Lua functions (diasurgical/devilutionX#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.

from sol2.

glebm avatar glebm commented on June 9, 2024

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.

from sol2.

glebm avatar glebm commented on June 9, 2024

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

from sol2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.