string.dump the same function is not equal?

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

string.dump the same function is not equal?

Rodrigo Azevedo
Anyone could tell me why the code below returns 'false', when I
expected a 'true' instead. Tested with lua5.2, lua5.3 and
lua5.4-work2.

-----
local f1 = string.dump(function(x) x=x+1 return x end,true)
local f2 = string.dump(function(x) x=x+1 return x end,true)
print(f1 == f2)
-----

Thanks!
--
Rodrigo Azevedo Moreira da Silva

Reply | Threaded
Open this post in threaded view
|

Re: string.dump the same function is not equal?

Coda Highland
On Thu, Jan 31, 2019 at 12:28 PM Rodrigo Azevedo <[hidden email]> wrote:

>
> Anyone could tell me why the code below returns 'false', when I
> expected a 'true' instead. Tested with lua5.2, lua5.3 and
> lua5.4-work2.
>
> -----
> local f1 = string.dump(function(x) x=x+1 return x end,true)
> local f2 = string.dump(function(x) x=x+1 return x end,true)
> print(f1 == f2)
> -----
>
> Thanks!
> --
> Rodrigo Azevedo Moreira da Silva

Because f2 includes f1 in its upvalues, so they have different contexts.

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: string.dump the same function is not equal?

Жаботинский Евгений
Coda Highland <[hidden email]>:

On Thu, Jan 31, 2019 at 12:28 PM Rodrigo Azevedo <[hidden email]> wrote:


 Anyone could tell me why the code below returns 'false', when I
 expected a 'true' instead. Tested with lua5.2, lua5.3 and
 lua5.4-work2.

 -----
 local f1 = string.dump(function(x) x=x+1 return x end,true)
 local f2 = string.dump(function(x) x=x+1 return x end,true)
 print(f1 == f2)
 -----

 Thanks!
 --
 Rodrigo Azevedo Moreira da Silva


Because f2 includes f1 in its upvalues, so they have different contexts.

/s/ Adam

Huh? I thought only actually used vars were captured as upvalues. At least from performance / memory usage standpoint that would be common sense.

My guess is that debug information (at least line numbers) is different, but I didn't check it.

-- Evgeniy Zhabotinskiy
Reply | Threaded
Open this post in threaded view
|

Re: string.dump the same function is not equal?

Rodrigo Azevedo
In reply to this post by Coda Highland
That's not what luac says:

luac5.3 -l teste_funcs.lua
...
function <teste_funcs.lua:1,1> (3 instructions at 0x556f0d170110)
1 param, 2 slots, 0 upvalues, 1 local, 1 constant, 0 functions
    1    [1]    ADD          0 0 -1    ; - 1
    2    [1]    RETURN       0 2
    3    [1]    RETURN       0 1

function <teste_funcs.lua:2,2> (3 instructions at 0x556f0d170510)
1 param, 2 slots, 0 upvalues, 1 local, 1 constant, 0 functions
    1    [2]    ADD          0 0 -1    ; - 1
    2    [2]    RETURN       0 2
    3    [2]    RETURN       0 1

Em qui, 31 de jan de 2019 às 15:30, Coda Highland
<[hidden email]> escreveu:

>
> On Thu, Jan 31, 2019 at 12:28 PM Rodrigo Azevedo <[hidden email]> wrote:
> >
> > Anyone could tell me why the code below returns 'false', when I
> > expected a 'true' instead. Tested with lua5.2, lua5.3 and
> > lua5.4-work2.
> >
> > -----
> > local f1 = string.dump(function(x) x=x+1 return x end,true)
> > local f2 = string.dump(function(x) x=x+1 return x end,true)
> > print(f1 == f2)
> > -----
> >
> > Thanks!
> > --
> > Rodrigo Azevedo Moreira da Silva
>
> Because f2 includes f1 in its upvalues, so they have different contexts.
>
> /s/ Adam
>


--
Rodrigo Azevedo Moreira da Silva

Reply | Threaded
Open this post in threaded view
|

Re: string.dump the same function is not equal?

Rodrigo Azevedo
In reply to this post by Жаботинский Евгений
It should be something like that, in fact

----
local f1,f2 = string.dump(function(x) x=x+1 return x end,true),
string.dump(function(x) x=x+1 return x end,true)
print(f1 == f2)
----

returns 'true'. However, from Lua 5.3 manual,

string.dump (function [, strip])
Returns a string containing a binary representation (a binary chunk)
of the given function, so that a later load on this string returns a
copy of the function (but with new upvalues). If strip is a true
value, the binary representation may not include all debug information
about the function, to save space.

What 'may not include all debug information' precisely means?
instead, is there any way to avoid 'all' debug information?


Em qui, 31 de jan de 2019 às 15:42, Жаботинский Евгений
<[hidden email]> escreveu:

>
> Coda Highland <[hidden email]>:
>
> On Thu, Jan 31, 2019 at 12:28 PM Rodrigo Azevedo <[hidden email]> wrote:
>
>
>  Anyone could tell me why the code below returns 'false', when I
>  expected a 'true' instead. Tested with lua5.2, lua5.3 and
>  lua5.4-work2.
>
>  -----
>  local f1 = string.dump(function(x) x=x+1 return x end,true)
>  local f2 = string.dump(function(x) x=x+1 return x end,true)
>  print(f1 == f2)
>  -----
>
>  Thanks!
>  --
>  Rodrigo Azevedo Moreira da Silva
>
>
> Because f2 includes f1 in its upvalues, so they have different contexts.
>
> /s/ Adam
>
> Huh? I thought only actually used vars were captured as upvalues. At least from performance / memory usage standpoint that would be common sense.
>
> My guess is that debug information (at least line numbers) is different, but I didn't check it.
>
> -- Evgeniy Zhabotinskiy



--
Rodrigo Azevedo Moreira da Silva

Reply | Threaded
Open this post in threaded view
|

Re: string.dump the same function is not equal?

Luiz Henrique de Figueiredo
In reply to this post by Rodrigo Azevedo
> local f1 = string.dump(function(x) x=x+1 return x end,true)
> local f2 = string.dump(function(x) x=x+1 return x end,true)

If you save f1 and f2 to files and run luac on them you get:

function <?:1,1> (3 instructions at 0x7fc92ac03c20)
function <?:3,3> (3 instructions at 0x7fe052c03c20)

and that's the only difference.

In other words. even if you strip debug information. the line where
the function is defined is still saved.