lua-users home
lua-l archive

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




On 05/08/16 11:44 PM, Sean Conner wrote:
It was thus said that the Great Soni L. once stated:
_SELF and _SUPER would make the compiler insert some things into the
bytecode that don't get erased/don't count as "debug info".

Using debug.* is literally relying on debug info. And order of
locals/upvalues. And so on. None of that is guaranteed, while the
proposal can use mechanisms not yet available and stuff.

For example, this code:

local x;
print(_SELF["x"])

Could produce a sequence of opcodes like:

R(1) = nil -- e.g. LOADNIL
R(2) = Upval[1][K(1)] -- e.g. GETTABUP
R(3) = K(2)[K(3)] -- e.g. GETTABLEK / GETTABLE with a statically
allocated table constant
R(3) = R(R(3)) -- e.g. MOVEI / move from indirect register (because the
I is on the right, and the RHS is the source)
R(2)(R(3)) -- e.g. CALL

where:

K(1) = "print"
K(2) = statically allocated table: {["x"] = 1}
K(3) = "x"

This would be for reading only. For writing, it'd use R(R(x)) = R(y)
(e.g. IMOVE) instead. For calling, it'd retrieve the current function
from the call stack or something. For passing it around, it'd copy all
locals into a table, maybe set a __call metamethod as well.
   To what end?  What problem does this solve?  What you one use this for?

   -spc


-- Example 1
print((function(x, y) return x == 0 and (y or 1) or _SELF(x-1, x*(y or 1)) end)(3)) --> 6

-- Example 2
local function a()
end
local function b()
end
local function c()
end

return function(x)
  for i, opcode in ipairs(x) do
    local op, arg1, arg2, arg3 = table.unpack(opcode)
    _SUPER[op](arg1, arg2, arg3)
  end
end

Etc. _SELF is more useful than _SUPER, all things considered.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.