lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 18/12/14 12:46 AM, Soni L. wrote:

On 17/12/14 01:26 PM, Soni L. wrote:

On 17/12/14 03:26 AM, Dirk Laurie wrote:
2014-12-17 4:54 GMT+02:00 Soni L. <fakedme@gmail.com>:

First of all I want to be able to _G:table.insert("something").
Secondly, I want to be able to _G:["print"]().
What exactly are these supposed to be syntax sugar for?

It's opcode sugar, actually.

_G:["print"](...) is already supported by the opcode, just use a non-constant index. It basically translates to _G["print"](_G, ...) (but _G only gets evaluated once).

_G:table.insert("something") is a bit trickier to implement. (it uses more opcodes) But think about it like this: in the same way that _G:print() translates to _G.print(_G), _G:table.insert("something") translates to _G.table.insert(_G, "something"). (add `do local _ = _G ... end` boilerplate as needed)

And ofc, _G:["table"]["insert"]("something"), I guess you can guess what this does.

Here's what I tried: https://github.com/SoniEx2/sexlua/commit/b279aebc50b5

When I try _G:_G.print("test") I get

    [soniex2@soniex-pc src]$ ./luac -l -l -o /dev/null -
    _G:_G.print("test")

    main <stdin:0,0> (5 instructions at 0x10800c0)
    0+ params, 3 slots, 1 upvalue, 0 locals, 3 constants, 0 functions
        1    [1]    GETTABUP     0 0 -1    ; _ENV "_G"
        2    [1]    SELF         0 0 -1    ; "_G"
        3    [1]    LOADK        2 -3    ; "test"
        4    [1]    CALL         1 -131 1
        5    [1]    RETURN       0 1
    constants (3) for 0x10800c0:
        1    "_G"
        2    "print"
        3    "test"
    locals (0) for 0x10800c0:
    upvalues (1) for 0x10800c0:
        0    _ENV    1    0

It's missing a GETTABLE (for the 2nd _G, with key "print" aka -2) and that CALL should be... CALL 0 2 1? Completely broken.

And when I try _G:print("test") I get

    [soniex2@soniex-pc src]$ ./luac -l -l -o /dev/null -
    _G:print("test")

        2    [1]    SELF         0 0 -2    ; "print"
        3    [1]    LOADK        2 -3    ; "test"
        4    [1]    CALL         0 3 1
        5    [1]    RETURN       0 1
    constants (3) for 0x1a0d0c0:
        1    "_G"
        2    "print"
        3    "test"
    locals (0) for 0x1a0d0c0:
    upvalues (1) for 0x1a0d0c0:
        0    _ENV    1    0

Which has wrong CALL. (0 2 1 in unmodified/vanilla Lua. I'm not sure why this is happening.)

Ugh wait I made a mistake. This one is actually right, I accidentally compared it to _G:print(), sorry. >.>

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.