destroyedlolo
destroyedlolo

Reputation: 95

How to compare 2 functions passed as argument

My application is able to push a function in a Todo list by using a code like : function test() print("coucou") end

todo:pushtask( test )  -- First push
todo:pushtask( test )  -- Second push

Internally, the todo list is a C table of integer where I push a reference to the passed function took from :

int func = luaL_ref(L, LUA_REGISTRYINDEX);

But how can I detect if I'm pushing the same function ? As per my test, luaL_ref returning do different references even if the same function is pushed (let's say ref #1 and #2) How can I check if #1 and #2 are references to the same function ?

Thanks

Laurent

Upvotes: 2

Views: 102

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80931

Functions can be directly compared. To see if you have the same function you need to compare it against the other functions you already have.

luaL_ref does not do that comparison for you (nor should it).

If you want to do this you should keep, in addition to the luaL_ref (or possibly in place of) reference, a lua table with the pushed functions as the keys. This will then let you look up the new function in the table and determine (at hash table access costs instead of list traversal costs) whether that function has been added before or not.

Upvotes: 1

Related Questions