Add default __eq implementation #176

Merged
merged 3 commits into from Jun 2, 2014

Conversation

Projects
None yet
3 participants
Contributor

v-for-vandal commented May 16, 2014

  • 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

This pull request is done as response to Eric Wing proposal "[Swig-devel] Proposal: SWIG should supply a default __eq for Lua, and operator== doesn't work in %extend"

A few more tests should probably be added. I am still unsure that this change wont break existing code.

@ojwb ojwb commented on an outdated diff May 18, 2014

Lib/lua/luarun.swg
@@ -1444,6 +1461,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);
@ojwb

ojwb May 18, 2014

Member

For compatibility with pre-C99 C, variables should be declared at the start of the block.

Contributor

v-for-vandal commented May 23, 2014

Ok, I have moved variable to the beginning of the block. But, again, there are so much variables declared in the middle of functions that I don't even dare to compile it with -ansi -pedantic.

Owner

wsfulton commented May 24, 2014

I'll wait for you to add the equality tests to the patch before merging.

Also you probably ought to check when an explicit operator== is available that it works. I'm not sure if there is a runtime test already for this in Lua. There are a few testcases to choose from:

$ grep -l operator== Examples/test-suite/*
Examples/test-suite/li_std_list.i
Examples/test-suite/li_std_map.i
Examples/test-suite/operator_overload_break.i
Examples/test-suite/operator_overload.i
Examples/test-suite/python_richcompare.i
Examples/test-suite/rename_scope.i
Contributor

v-for-vandal commented May 26, 2014

Reworked and integrated equality.i. Defined operator== was coreverd in operator_overload.i, but added test for it here as well.

v-for-vandal added some commits May 16, 2014

@v-for-vandal v-for-vandal 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
2b4c49d
@v-for-vandal v-for-vandal Moving variable declaration to the beginning of the block 4457d96
@v-for-vandal v-for-vandal More tests for equality operator overloading 46d7645
Contributor

v-for-vandal commented May 29, 2014

@wsfulton Tests for Lua have successfully passed. Ruby test has failed, but I don't see how my commits could possibly caused this.

Owner

wsfulton commented May 29, 2014

Yes, Travis had a funny moment testing Ruby. I just restarted the Ruby build and now it is green.

@wsfulton wsfulton added a commit that referenced this pull request Jun 2, 2014

@wsfulton wsfulton Merge pull request #176 from v-for-vandal/lua_eq
Add default __eq implementation for Lua
c17f777

@wsfulton wsfulton merged commit c17f777 into swig:master Jun 2, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment