Permalink
Browse files

Add default __eq implementation

* Renamed SWIG_Lua_equal to SWIG_Lua_class_equal
* If class has no __eq implemented, then default __eq is provided.
  Default __eq compares actual pointers stored inside Lua userdata
  • Loading branch information...
v-for-vandal committed May 16, 2014
1 parent 68347cb commit 2b4c49d017f526f6622cd54547ab9fab31941176
Showing with 41 additions and 18 deletions.
  1. +11 −0 Examples/test-suite/lua/cpp_basic_runme.lua
  2. +29 −17 Lib/lua/luarun.swg
  3. +1 −1 Lib/lua/luaruntime.swg
@@ -86,3 +86,14 @@ f4_name = tostring(f4)
assert( f2_name == "Foo" )
assert( f3_name == "Foo" )
assert( f4_name == "FooSubSub" )
+
+-- Test __eq implementation supplied by default
+
+-- eq_f1 and eq_f2 must be different userdata with same Foo* pointer. If eq_f1 and eq_f2 are the same userdata (e.g.)
+-- > eq_f1 = smth
+-- > eq_f2 = eq_f1
+-- then default Lua equality comparison kicks in and considers them equal. Access to global_fptr is actually a
+-- function call (internally) and it returns new userdata each time.
+eq_f1 = cb.Bar.global_fptr
+eq_f2 = cb.Bar.global_fptr
+assert( eq_f1 == eq_f2 )
View
@@ -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)
{
@@ -1471,6 +1488,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);
+ const int 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 +1817,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
@@ -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)

0 comments on commit 2b4c49d

Please sign in to comment.