Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling duplicated keys in a ROTable #3028

Closed
TerryE opened this issue Jan 31, 2020 · 3 comments
Closed

Handling duplicated keys in a ROTable #3028

TerryE opened this issue Jan 31, 2020 · 3 comments

Comments

@TerryE
Copy link
Collaborator

TerryE commented Jan 31, 2020

Current Behaviour

The standard Lua architecture enforces key uniqueness in table entries. NodeMCU adds compile-time constant table implementation know as ROTables which can be statically compiled into Lua modules as vector structures and stored in Flash Memory rather than RAM. Because the Lua next() exploits this uniqueness by finding the previous entry and then chaining to the next field, if the ROTable contains duplicated entries then the enumeration algo goes into an infinite loop.

luac.cross also generates a ROTable index to the functions in a compiled LFS, but it currently does not do a uniqueness check on module names.

Proposed Changes

My thought is that we shouldn't try to add runtime detection of duplicate keys, but rather document that keys in declared ROTable must be unique.

luac.cross should also check for repeated module names within an LFS and raise an error on detection of a duplicate, so that module entries are unique.

Possible future issue

At the moment the module name is based on the basename of the lua file name, so for example lua_examples/luaOTA/_init.lua creates the module _init which can be executed by require "_init" or LFS._init(). With the increased use of LFS modules, the chance of name clash increases. Perhaps we need to think about a convention for namespace separation.

@nwf
Copy link
Member

nwf commented Feb 2, 2020

I think your proposed changes are sufficient.

The uniqueness constraint on ROTable keys is, presumably, post-linker? That is, it's fine for two modules to declare "foo" keys so long as the C strings are commoned up in the .rodata section, yes?

@TerryE
Copy link
Collaborator Author

TerryE commented Feb 2, 2020

You can have as many occurrences of a key as you want, e.g. __index, but only one per table.

@TerryE
Copy link
Collaborator Author

TerryE commented Apr 26, 2020

luac.cross will throw a compile error if two modules have a common name, e.g. _init. I suggest we treat any namespace issues as a future enhancement.

@TerryE TerryE closed this as completed Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants