I'm just getting started with Luabind and C++. My goal is pretty simple:
I want to create a Lua function that takes a C++ object pointer as an argument and stores the object in a Lua variable. Each time the Lua function is called, it should first check if the object pointer passed to it is the same object pointer as the one stored during the previous call.
Here is the full code:
extern "C" {
#include "lua.h"
#include "lualib.h"
}
#include <luabind/luabind.hpp>
class Obj {
};
int main(int argc, char **argv) {
lua_State* cLuaState = luaL_newstate();
luabind::open(cLuaState);
luaL_openlibs(cLuaState);
luabind::module(cLuaState) [
luabind::class_<Obj>("Obj")
];
luaL_dostring(cLuaState, "\
function func(v)\n\
print (\"Before: x is same as v?\")\n\
print (x == v)\n\
x = v\n\
print (\"After: x is same as v?\")\n\
print (x == v)\n\
end");
Obj* o = new Obj();
try {
luabind::call_function<void>(cLuaState, "func", o);
luabind::call_function<void>(cLuaState, "func", o);
} catch (std::exception &e) {
std::cout << "Exception thrown: " << e.what() << std::endl;
return 1;
}
return 0;
}
When I run this program, I would expect to see the following output:
Before: x is same as v?
false
Setting v
After: x is same as v?
true
Before: x is same as v?
true
Setting v
After: x is same as v?
true
However, when I run the program, it crashes during the second call to the Lua function during the comparison between 'x' and 'v'. This is the actual resulting output from the program:
Before: x is same as v?
false
Setting v
After: x is same as v?
true
Before: x is same as v?
Exception thrown: lua runtime error
*** Exited with return code: 1 ***
As can be seen, the comparison works during the first function call both before and after 'v' is set. However, the first comparison fails during the second function called.
I must be missing something incredibly obvious, but I really can't figure out what it is. Can anyone see what I'm doing wrong? Any advice is much appreciated!
Thank you very much, Martin
I found the solution. It seems that for some reason, Lua wants to use an overloaded C++ equals operator to perform the comparison on the two objects instead of performing the comparison of the pointers themselves. By creating an overloaded equality operator that simply compares the addresses of two objects and binding it to Lua, I can make it work.
I'm still not sure why Lua would only do the comparison this way on the second function call and not the first, but at least I now have a workable solution.
The full modified working version is below:
extern "C" {
#include "lua.h"
#include "lualib.h"
}
#include <luabind/luabind.hpp>
#include <luabind/operator.hpp>
class Obj {
};
bool operator==(const Obj& a, const Obj& b) {
return &a == &b;
}
int main(int argc, char **argv) {
lua_State* cLuaState = luaL_newstate();
luabind::open(cLuaState);
luaL_openlibs(cLuaState);
luabind::module(cLuaState) [
luabind::class_<Obj>("Obj")
.def(luabind::const_self == luabind::const_self)
];
luaL_dostring(cLuaState, "\
function func(v)\n\
print (\"Before: x is same as v?\")\n\
print (x == v)\n\
x = v\n\
print (\"After: x is same as v?\")\n\
print (x == v)\n\
end");
Obj* o = new Obj();
try {
luabind::call_function<void>(cLuaState, "func", o);
luabind::call_function<void>(cLuaState, "func", o);
} catch (std::exception &e) {
std::cout << "Exception thrown: " << e.what() << std::endl;
return 1;
}
return 0;
}