lua-users home
lua-l archive

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


On 25 June 2015 at 03:11, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>> As I replied on IRC; I think that lexically scoped metatables for the
>> base types are a much better idea.
>> I even coded up a proof of concept:
>> https://gist.github.com/daurnimator/dedd793e6f9b1f8d6b0c
>> (obviously if such a feature made it into PUC-lua, the `debug` calls
>> would be unnessecary)
>
> Do you have an idea about how to implement it efficiently and correctly?

Not exactly.
reading through the code it seems messy to get the current environment....
the implementation of the OP_GETTABUP instruction seems to be a good
place to start.
But I've spent a bit too long looking at this now.

At the least, it would involve
  - Removing the `mt` field from the lua_State
  - lua_getmetatable
(http://www.lua.org/source/5.3/lapi.c.html#lua_getmetatable) in the
`default` case re-written to go via _ENV

Thinking about this in terms of the code made me think more about how
I'd want this to exist.
I think you'd want the whole metatable to be per env (rather than just
__index as I had in my gist).
This brings up the question of where they should be accessible....
  - `_ENV.metatables` no. that would mean every environment would need it
  - `getmetatable(_ENV)`: interesting....
  - `registry[_ENV]` issues with garbage collection
  - `registry.metatables[_ENV]`: too much indirection?
but then again, the metatables are barely accessible as it is (you
need to use debug.getmetatable);
so maybe you don't need it easily accessible