Merge pull request #176 from v-for-vandal/lua_eq

Add default __eq implementation for Lua
This commit is contained in:
William S Fulton 2014-06-02 19:52:07 +01:00
commit c17f77750a
6 changed files with 157 additions and 18 deletions

View file

@ -1037,6 +1037,23 @@ SWIGINTERN int SWIG_Lua_class_disown(lua_State *L)
return 0;
}
/* lua callable function to compare userdata's value
the issue is that two userdata may point to the same thing
but to lua, they are different objects */
SWIGRUNTIME int SWIG_Lua_class_equal(lua_State *L)
{
int result;
swig_lua_userdata *usr1,*usr2;
if (!lua_isuserdata(L,1) || !lua_isuserdata(L,2)) /* just in case */
return 0; /* nil reply */
usr1=(swig_lua_userdata*)lua_touserdata(L,1); /* get data */
usr2=(swig_lua_userdata*)lua_touserdata(L,2); /* get data */
/*result=(usr1->ptr==usr2->ptr && usr1->type==usr2->type); only works if type is the same*/
result=(usr1->ptr==usr2->ptr);
lua_pushboolean(L,result);
return 1;
}
/* populate table at the top of the stack with metamethods that ought to be inherited */
SWIGINTERN void SWIG_Lua_populate_inheritable_metamethods(lua_State *L)
{
@ -1439,6 +1456,7 @@ SWIGINTERN void SWIG_Lua_add_class_user_metamethods(lua_State *L, swig_lua_class
int metatable_index;
int metamethods_info_index;
int tostring_undefined;
int eq_undefined = 0;
SWIG_Lua_get_class_metatable(L, clss->fqname);
metatable_index = lua_gettop(L);
@ -1471,6 +1489,18 @@ SWIGINTERN void SWIG_Lua_add_class_user_metamethods(lua_State *L, swig_lua_class
lua_pop(L,1); /* remove copy of the key */
}
/* Special handling for __eq method */
lua_pushstring(L, "__eq");
lua_pushvalue(L,-1);
lua_rawget(L,metatable_index);
eq_undefined = lua_isnil(L,-1);
lua_pop(L,1);
if( eq_undefined ) {
lua_pushcfunction(L, SWIG_Lua_class_equal);
lua_rawset(L, metatable_index);
} else {
lua_pop(L,1); /* remove copy of the key */
}
/* Warning: __index and __newindex are SWIG-defined. For user-defined operator[]
* a __getitem/__setitem method should be defined
*/
@ -1788,23 +1818,6 @@ SWIGRUNTIME int SWIG_Lua_type(lua_State *L)
return 1;
}
/* lua callable function to compare userdata's value
the issue is that two userdata may point to the same thing
but to lua, they are different objects */
SWIGRUNTIME int SWIG_Lua_equal(lua_State *L)
{
int result;
swig_lua_userdata *usr1,*usr2;
if (!lua_isuserdata(L,1) || !lua_isuserdata(L,2)) /* just in case */
return 0; /* nil reply */
usr1=(swig_lua_userdata*)lua_touserdata(L,1); /* get data */
usr2=(swig_lua_userdata*)lua_touserdata(L,2); /* get data */
/*result=(usr1->ptr==usr2->ptr && usr1->type==usr2->type); only works if type is the same*/
result=(usr1->ptr==usr2->ptr);
lua_pushboolean(L,result);
return 1;
}
/* -----------------------------------------------------------------------------
* global variable support code: class/struct typemap functions
* ----------------------------------------------------------------------------- */

View file

@ -40,7 +40,7 @@ SWIGEXPORT int SWIG_init(lua_State* L) /* default Lua action */
#if ((SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUA) && (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC)) || defined(SWIG_LUA_ELUA_EMULATE)
/* add a global fn */
SWIG_Lua_add_function(L,"swig_type",SWIG_Lua_type);
SWIG_Lua_add_function(L,"swig_equals",SWIG_Lua_equal);
SWIG_Lua_add_function(L,"swig_equals",SWIG_Lua_class_equal);
#endif
#if (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC)