Comments (10)
LuaRef comparison operators will use lua_compare and will invoke metamethods. I'm not sure functions can be compared with that, and for sure they can't have a LuaRef::operator<
which seems to be needed by std::set
(it uses std::less<LuaRef>
underneat). Maybe you get better results using std::unordered_set
by also providing a KeyEqual
comparator based on LuaRef::rawequal
rather than LuaRef::operator==
(which is selected by default by std:equal_to<LuaRef>
).
I will see if i can make it work somehow.
from luabridge3.
If i replace your set with an unordered_set it works no problem. Is there a reason you need ordering ?
from luabridge3.
No, I don't need set
specifically, to me it was just a convenient way to store unique functions without possibility to have duplicates, but this only works with that Function
wrapper I wrote, which has that unique id
field, which can be used in operator<
as well. But yeah, I can use any other container, like unordered_set
or list
, I just need a way to compare functions to be able to find them in that container and delete when necessary.
So yeah, if you're able to compare LuaRef
referencing functions on your side or you can provide more information about that KeyEqual
/ rawequal
so I can use it in my code, that could already work well for me. In fact, if rawequal
is able to compare functions in Lua style, i.e. using those addresses or whatever this is I described above, I mean function: 0000024a923ade90
, that's good enough for me.
from luabridge3.
Hm, I just realized that if you wrote it works with unordered_set
, this means comparison for equality works already, otherwise how unordered_set
would check for item uniqueness, right? I just thought that error attempt to compare two function values
appears in any attempt to compare functions, including equality. But if it's not, then I might have created an issue for nothing. Let me check that.
from luabridge3.
== works on functions, but < not
from luabridge3.
You can do this, but lua_pointer will return nullptr for trivial types:
struct LuaRefLess
{
bool operator()(const luabridge::LuaRef& x, const luabridge::LuaRef& y) const
{
x.push();
auto lhs = lua_topointer(x.state(), -1);
y.push();
auto rhs = lua_topointer(y.state(), -1);
return lhs < rhs;
}
};
TEST_F(LuaRefTests, SetComparatorOperatorLessThan)
{
std::set<luabridge::LuaRef, LuaRefLess> eventHandlers;
auto subscribe = [&eventHandlers](luabridge::LuaRef handler)
{
if (handler.isFunction())
eventHandlers.insert(handler);
};
auto trigger = [&eventHandlers]
{
for (auto handler : eventHandlers)
handler();
};
luabridge::getGlobalNamespace(L)
.addFunction("subscribe", subscribe)
.addFunction("trigger", trigger);
runLua(R"(
a = function()
print('Hello')
end
b = a
subscribe(a)
subscribe(b)
subscribe("wrong")
subscribe(function()
print('World')
end)
)");
trigger();
}
from luabridge3.
Yep, I just checked and turns out there's no issue at all, looks like comparison for equality works already under the hood and thus I can delete LuaRef-functions from C++ container no problem. My bad, I misinterpreted the error, or rather haven't researched it enough. Additionally, I just checked in the intepreter and figured out this error comes from Lua itself, not from LuaBridge, as I assumed before, so yeah, this is how it is:
> f1 = function()end
> f2 = function()end
> f1 == f2
false
> f1 < f2
stdin:1: attempt to compare two function values
stack traceback:
stdin:1: in main chunk
[C]: in ?
Ok, thank you for your attention in any case, and I'm closing this issue then.
from luabridge3.
#65 < This could fix your issue and be more safe at the same time, as lua_compare might raise a lua error (and trigger the panic handler).
from luabridge3.
By looking at the changes I assume you compare pointer values there. I wanted to suggest the same, because initially I supposed pointers increase in value as function variables get initialized, but then I noticed it's not always the case, probably because of GC, so I started to think it's not that good idea as it seems that simple ==
is more reliable. But if you implemented it, I guess it can't hurt, because this should allow storing LuaRef
in set
, even in case if order of LuaRef
being added to set
won't match ordering inside of set
because of that apparent address increment inconsistency.
from luabridge3.
I'm also not sure, but i also don't want that a call in c++ on luarefs will trigger the panic handler
from luabridge3.
Related Issues (20)
- Compiler error use of deleted function luabridge::TypeResult<T>::TypeResult(...) HOT 6
- Enhancement Request: Custom error function for `lua_pcall`. HOT 11
- Error C3861 (using with Luau) HOT 3
- Can I cast types inside Lua? HOT 4
- Duplicate symbols on Win32 with default __stdcall HOT 9
- Debug names of C functions for Luau HOT 8
- Extensible class bug with inheritance
- Add support for addStaticIndexMetamethod and addStaticNewIndexMetamethod
- Using LuaBridge3 in a precompiled header on MSVC leads to an internal compiler error HOT 2
- Use of std::shared_ptr on classes without `std::enable_shared_from_this` HOT 1
- Stateful lambdas registered as functions loose state HOT 2
- Compile error with Cfunc property getter/setter HOT 2
- std::vector to 0-based Lua table HOT 5
- MSC Warning C4146 HOT 2
- How to expose C++ data (a vector, in this case) to Lua without making a copy? HOT 2
- Does LuaBridge3 Support Android for SDL2-based Games? HOT 2
- Passing a Lua Table (LuaRef) as an argument to C++ function HOT 1
- How to get the name of a Lua variable? HOT 2
- Comparison Issue Between `uint64_t` in C++ and Numeric Constant in Lua HOT 8
- NAI run lua code in string from cpp
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 luabridge3.