Skip to content

siffiejoe/lua-rotable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rotable

Tables in Lua use quite some dynamic memory. To reduce this memory consumption on small embedded microcontrollers the eLua project introduced rotables (read-only tables) that can be stored in ROM and behave mostly like ordinary Lua tables (except that you can't mutate them, of course). These rotables are particularly useful for module tables which usually don't change anyway and mostly contain strings as keys and C function pointers as values.

This project provides a limited imitation of eLua's rotables without the need to patch Lua. On the plus side, more recent Lua versions (5.2 and 5.3) are supported. The other features of eLua's Lua Tiny RAM patch and the Emergency GC patch have been incorporated in recent Lua versions anyway.

API

/*  [-0, +1, m]  */
void rotable_newlib( lua_State* L, luaL_Reg const l[] );

This function creates and pushes a rotable onto the Lua stack. If you don't have the auxiliary library available, you can use the type rotable_Reg instead of luaL_Reg in the above signature. A rotable is a small userdata (so you do have some dynamic memory allocation per rotable) that contains a pointer to the given luaL_Reg array and behaves mostly like a table. If the string keys in the luaL_Reg array are sorted lexicographically in ascending order (which is highly recommended), access is more efficient because a binary search is used. Otherwise every access uses a linear search for the key.

In contrast to the original rotables in the eLua project, this implementation only supports string keys and C function values. If you need anything else, you can consider using a rotable as fallback using an __index metamethod on a normal table.

/*  [-0, +1, m]  */
void rotable_newidx( lua_State* L, luaL_Reg const l[] );

Since userdata values can't be used as __index meta methods, this function creates and pushes a custom C closure that looks up keys in the given luaL_Reg array. It can be used as __index meta method instead.

License

rotable is copyrighted free software distributed under the MIT license (the same license as Lua 5.1). The full license text follows:

rotable (c) 2017 Philipp Janda

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Save memory with custom module "tables"

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published