lua_CFunction equivalence

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

lua_CFunction equivalence

William Ahern
If I do

        lua_pushcfunction(L, &foo);
        lua_pushcfunction(L, &foo);

        rc = lua_equal(L, -1, -2);

is the value of `rc' guaranteed to be 1 in Lua 5.1, Lua 5.2, and LuaJIT?
That is, is it part of the language that C functions without upvalues are
compared according to their C function pointer values?


Reply | Threaded
Open this post in threaded view
|

Re: lua_CFunction equivalence

William Ahern
On Thu, Jul 25, 2013 at 08:34:35PM -0700, William Ahern wrote:

> If I do
>
> lua_pushcfunction(L, &foo);
> lua_pushcfunction(L, &foo);
>
> rc = lua_equal(L, -1, -2);
>
> is the value of `rc' guaranteed to be 1 in Lua 5.1, Lua 5.2, and LuaJIT?
> That is, is it part of the language that C functions without upvalues are
> compared according to their C function pointer values?
>

D'uh. The answer is no. I abstained from checking with actual code because I
assumed it would work, but my assumption was wrong, at least for Lua 5.1.


Reply | Threaded
Open this post in threaded view
|

Re: lua_CFunction equivalence

Thomas Jericke
On 07/26/2013 03:45 AM, William Ahern wrote:

> On Thu, Jul 25, 2013 at 08:34:35PM -0700, William Ahern wrote:
>> If I do
>>
>> lua_pushcfunction(L, &foo);
>> lua_pushcfunction(L, &foo);
>>
>> rc = lua_equal(L, -1, -2);
>>
>> is the value of `rc' guaranteed to be 1 in Lua 5.1, Lua 5.2, and LuaJIT?
>> That is, is it part of the language that C functions without upvalues are
>> compared according to their C function pointer values?
>>
> D'uh. The answer is no. I abstained from checking with actual code because I
> assumed it would work, but my assumption was wrong, at least for Lua 5.1.
>
>
I also thought it must always be 0. Because you push the c function
twice, means you have two references in the Lua world, and then you
compare the references and not the values. I don't see why Lua should
check if this C function was already pushed before at the second call of
lua_pushcfunction. It would be quite an overhead.

As the manual says: lua_pushcfunction(L, f) is just a makro for
lua_pushcclosure(L, f, 0)

--
Thomas

Reply | Threaded
Open this post in threaded view
|

Re: lua_CFunction equivalence

Duncan Cross
In reply to this post by William Ahern
On Fri, Jul 26, 2013 at 4:34 AM, William Ahern
<[hidden email]> wrote:

> If I do
>
>         lua_pushcfunction(L, &foo);
>         lua_pushcfunction(L, &foo);
>
>         rc = lua_equal(L, -1, -2);
>
> is the value of `rc' guaranteed to be 1 in Lua 5.1, Lua 5.2, and LuaJIT?
> That is, is it part of the language that C functions without upvalues are
> compared according to their C function pointer values?
>
>

Only since Lua 5.2, which introduced the concept of "light C
functions" for specfically this case of C functions with no upvalues.

-Duncan