Skip to content

Instantly share code, notes, and snippets.

@creationix
Created April 20, 2016 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/1d52c2723279105f2b0ccfdd0298a3fb to your computer and use it in GitHub Desktop.
Save creationix/1d52c2723279105f2b0ccfdd0298a3fb to your computer and use it in GitHub Desktop.
luajit -ble 'local a = 1;function b() return a + 1; end;return b()'
-- BYTECODE -- 0x41ab0238:1-1
0001 UGET 0 0 ; a
0002 ADDVN 0 0 0 ; 1
0003 RET1 0 2
-- BYTECODE -- 0x41aa48b8:0-1
0001 KSHORT 0 1
0002 FNEW 1 0 ; 0x41ab0238:1
0003 GSET 1 1 ; "b"
0004 GGET 1 1 ; "b"
0005 UCLO 0 => 0006
0006 => CALLT 1 1
@creationix
Copy link
Author

Or without the temporary global assignment:

luajit -ble 'local a = 1;return(function () return a + 1; end)()'
-- BYTECODE -- 0x402ea7f0:1-1
0001    UGET     0   0      ; a
0002    ADDVN    0   0   0  ; 1
0003    RET1     0   2

-- BYTECODE -- 0x402f7dd0:0-1
0001    KSHORT   0   1
0002    FNEW     1   0      ; 0x402ea7f0:1
0003    UCLO     0 => 0004
0004 => CALLT    1   1

@daurnimator
Copy link

Example from no frills:

$ luajit -ble 'do local p,q; r=function() return p,q end; end'
-- BYTECODE -- 0x41ad1190:1-1
0001    UGET     0   0      ; p
0002    UGET     1   1      ; q
0003    RET      0   3

-- BYTECODE -- 0x41acf970:0-1
0001    KNIL     0   1
0002    FNEW     2   0      ; 0x41ad1190:1
0003    GSET     2   1      ; "r"
0004    UCLO     0 => 0005
0005 => RET0     0   1

@creationix
Copy link
Author

luajit -ble 'local a=1; (function()a=a+1 end)();return a'
-- BYTECODE -- 0x406d21e8:1-1
0001    UGET     0   0      ; a
0002    ADDVN    0   0   0  ; 1
0003    USETV    0   0      ; a
0004    RET0     0   1

-- BYTECODE -- 0x406c68b8:0-1
0001    KSHORT   0   1
0002    FNEW     1   0      ; 0x406d21e8:1
0003    CALL     1   1   1
0004    UCLO     0 => 0005
0005 => RET1     0   2

@daurnimator
Copy link

 $ echo 'local a=1; (function()a=a+1 end)();return a' | luac -l -l -p -

main <stdin:0,0> (5 instructions at 0xb48790)
0+ params, 2 slots, 1 upvalue, 1 local, 1 constant, 1 function
    1   [1] LOADK       0 -1    ; 1
    2   [1] CLOSURE     1 0 ; 0xb48a60
    3   [1] CALL        1 1 1
    4   [1] RETURN      0 2
    5   [1] RETURN      0 1
constants (1) for 0xb48790:
    1   1
locals (1) for 0xb48790:
    0   a   2   6
upvalues (1) for 0xb48790:
    0   _ENV    1   0

function <stdin:1,1> (4 instructions at 0xb48a60)
0 params, 2 slots, 1 upvalue, 0 locals, 1 constant, 0 functions
    1   [1] GETUPVAL    0 0 ; a
    2   [1] ADD         0 0 -1  ; - 1
    3   [1] SETUPVAL    0 0 ; a
    4   [1] RETURN      0 1
constants (1) for 0xb48a60:
    1   1
locals (0) for 0xb48a60:
upvalues (1) for 0xb48a60:
    0   a   1   0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment