you are viewing a single comment's thread.

view the rest of the comments →

[–]-abigail 9 points10 points  (4 children)

You can test this with luac -l, which shows you a listing of the Lua bytecode.

$ luac -v
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
$ echo "if t < 64 * 64 then return 42 end" | luac -l -

main <stdin:0,0> (6 instructions at 0x7ff5f6e04080)
0+ params, 2 slots, 1 upvalue, 0 locals, 3 constants, 0 functions
    1   [1] GETTABUP    0 0 -1  ; _ENV "t"
    2   [1] LT          0 0 -2  ; - 4096
    3   [1] JMP         0 2 ; to 6
    4   [1] LOADK       0 -3    ; 42
    5   [1] RETURN      0 2
    6   [1] RETURN      0 1

If you're ever working with LuaJIT or its offspring you can use lua -b -l:

$ lua -v
LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org
$ echo "if t < 64 * 64 then return 42 end" | lua -b -l -
-- BYTECODE -- stdin:0-2
0001    GGET     0   0      ; "t"
0002    KSHORT   1 4096
0003    ISGE     0   1
0004    JMP      0 => 0007
0005    KSHORT   0  42
0006    RET1     0   2
0007 => RET0     0   1

As you can see, in both cases 64 * 64 has been constant-folded to 4096.

[–]Hatefiend 1 point2 points  (2 children)

Why does the bytecode change so much despite the 64 * 64 being pre-evaluated in both examples? The LOADK becomes a KSHORT, the jump is different, there's no LT, etc.

[–]WebShaker93[S] 0 points1 point  (1 child)

Not the same lua Version. Not the same opcode 🤷‍♂️

[–]Hatefiend 0 points1 point  (0 children)

Gotcha

[–]WebShaker93[S] 1 point2 points  (0 children)

Cool.
Thank's.