lua-users home
lua-l archive

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



On 21/05/2014 08:13, Luiz Henrique de Figueiredo 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...
The SELF instruction already supports this, but the parser does not.
Consider this code:
        local t={}
        t.f=print
        local v="f"
        t:x(1)

The bytecode is
	1	[1]	NEWTABLE 	0 0 0
	2	[2]	GETTABUP 	1 0 -2	; _ENV "print"
	3	[2]	SETTABLE 	0 -1 1	; "f" -
	4	[3]	LOADK    	1 -1	; "f"
	5	[4]	SELF     	2 0 -3	; "x"
	6	[4]	LOADK    	4 -4	; 1
	7	[4]	CALL     	2 3 1
	8	[4]	RETURN   	0 1

Changing the SELF instruction to
	5	[4]	SELF     	2 0 1
gives the right result, that is, calls t:[v](1). The third operand in SELF
is the location of the key. In the original code, it's the constant #3, "x".
In the modified code, it's the register #1, which is the local variable v,
and that contains "f".

I think there are patchs around for the t:[var]() syntax.

Well this works: f = loadstring(string.dump(function(t,k,...) return t:t(...) end):gsub('\203%z\64%z','\203\64\0\0')); f(_G,'print')

Now I just need a bytecode lib and a Lua parser + lexer + etc in (pure-)Lua...