lua-users home
lua-l archive

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


2014-05-21 0:36 GMT+02:00 Thiago L. <fakedme@gmail.com>:
>
> On 20/05/2014 18:24, Coroutines wrote:
>>
>> On Tue, May 20, 2014 at 2:22 PM, Thiago L. <fakedme@gmail.com> wrote:
>>>
>>> So I wanna do a t:[var]() operator (which's pretty much t:name() but it
>>> takes a var instead of a name) and I have no idea of how to do it...
>>
>> a[var](a)
>>
>>      -- that guy  :p
>>
> I want bytecode...
>

local mt = {}
function mt:__pow(var) return self[var] end

local t={a='a', b='b'}
function t:A() print(t.a) end
function t:B() print(t.b) end

setmetatable(t,mt)
local c

c='A'
(t^c)()
c='B'
(t^c)()

Lua 5.2 bytecode for those last four lines:

    16    [11]    LOADK        2 -4    ; "A"
    17    [12]    POW          3 1 2
    18    [12]    CALL         3 1 1
    19    [13]    LOADK        2 -5    ; "B"
    20    [14]    POW          3 1 2
    21    [14]    CALL         3 1 1