lua-users home
lua-l archive

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


On Wed, Sep 30, 2015 at 06:27:26AM -0300, Soni L. wrote:
<snip>
> In Lua you have 2 options:
> 
> 1) Use function upvalues. (for local access)
> 
> 2) Use the module table. (for shared access)
> 
> There's no such thing as a "registry" in Lua, it only exists in C (and
> debug). I want to bring Lua on par with the C API and vice-versa.
> 
> I still don't see how the registry is needed, when the Lua side doesn't need
> one.

Because in Lua code the compiler takes care of assigning symbolic names to
upvalue slots. Have you tried using upvalues as replacements for the
registry? I would be surprised if you didn't quickly realize how cumbersome
it can become. Imagine removing symbolic function names from C and replacing
them with gotos and line numbers. It would become really tedious and error
prone.

If Lua had a precompiler and a special runtime loader that translated
special string identifiers to fixed offsets, you could get the best of both
worlds. But without that, you have to manually implement the mapping, and in
particular keep the mappings synchronized between the code accessing the
values and the code which instantiates the closures. For some cases this is
trivial with a small auxiliary library replacing the luaL_Reg interfaces,
but it's not easily generalizable.

Also, there are some values you don't want to be accessible from Lua code. I
try to write all of my modules so that Lua code could never cause an
out-of-bounds memory access or otherwise fubar the program state. But often
that means implementing specialized routines in C and storing sensitive data
structures out-of-reach of Lua code. For this and other reasons you'd still
want something like the registry.