lua-users home
lua-l archive

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




On 04/08/16 04:47 PM, Nagaev Boris wrote:
On Thu, Aug 4, 2016 at 7:54 PM, Soni L. <fakedme@gmail.com> wrote:
Can we get _SELF and _SUPER?

local x = 1
function f()
   local x = 2
   print(_SUPER.x) --> 1
end
print(f())

pcall(function(x)
   if x < 0 then error() end
   if x == 0 then return end
   print(x)
   _SELF(x-1)
end, 3) --> 3; 2; 1;

For performance, they would be translated by the compiler. Passing _SUPER
around would copy all upvalues into a table, but for simplicity changing
this table wouldn't change upvalues (e.g. no current syntactic construct can
create a metatable).

_SELF and _SUPER would be indexable and callable.
Let me check that I understand you correctly.

_SELF would be callable and _SUPER would be indexable.

No you can use _SELF.name as an alias for the local `name` (simple compiler optimizations can turn `_SELF.name` (as well as `_SELF["name"]`) into just `name`).

You can also call _SUPER.

You can also do _SUPER._SUPER.

Note that the main chunk wouldn't have _SUPER (but it would have _SELF).


Can _SUPER be indexed with a value not known at compile time?

local xx = 1
do
   print(_SUPER[string.rep("x", 2)])
end

Except for the do..end and _SUPER part, yes, that would work.

This wouldn't:

local xx = 1
_SELF[string.rep("x", 2)] = 3
print(xx) --> 1, not 3

Because for dynamic assignments _SELF would need a metatable. No current Lua opcode ever produces a table that already has a metatable. _SELF and _SUPER are special names however, which means they can behave specially (e.g. one behaviour for _SELF.x and other compile-time constants, another behaviour for _SELF[x] and other runtime values).

Another option would be to introduce "indirection" opcodes. E.g.

_SELF[x] = other

would translate into

GETTABLE (internal) [x] reg -- plain old gettable, I forgot the syntax; (internal) would be a reserved table the compiler generates for things using _SELF/_SUPER.
MOVEI reg [other] -- R(R(A)) =: R(B)

Note the R(R(A)) part.


(No idea what to say next. Idk, I'm just rambling I guess .-.)

--
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.



--
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.