Comments (6)
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.
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.
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.
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.
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.
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)
- Segfault from protected_function args push HOT 2
- std::terminate called on attempting to index a nil value HOT 2
- Doesn't build with Clang 18.1.0 HOT 9
- Not able to build with CMake HOT 3
- How to pass Lua Closures into C++ Functions in Sol3 ? HOT 4
- Test failure on x86
- Custom module loaders not working? HOT 1
- A const C++ container can be modified in Lua (want it to be truly const)
- Compilation Error when Member Variables set in new_usertype. HOT 1
- A bunch of errors when using usertypes HOT 1
- Is it possible to have out params / replace type and data referenced by sol::object? HOT 2
- Passing nil as userdata to C++ to reset a variable HOT 3
- On MSVC std::filesystem::path cannot be used as Lua type HOT 1
- Crash during destruction when using meta_function::new_index and coroutines
- Accessing table field with the wrong user type causes a segmentation fault
- Cannot directly bind b2Vec2 to a table entry HOT 1
- SOL_BASE_CLASSES / SOL_DERIVED_CLASSES should be mentioned in the documentation
- Generating definition files
- Why sol3 is slower than Luabridge3 when both using LuaJIT, HOT 1
- add_package_loader without lua_CFunction
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sol2.