summaryrefslogtreecommitdiff
path: root/apps/plugins/lua
diff options
context:
space:
mode:
authorWilliam Wilgus <me.theuser@yahoo.com>2018-09-14 01:13:22 +0200
committerWilliam Wilgus <me.theuser@yahoo.com>2018-09-14 01:13:22 +0200
commitdc6f23ec36979c6807988092cc9f696dcd6a2c81 (patch)
tree4d3463a96e27c029f129c3974c0dc7ca1bdf3b1a /apps/plugins/lua
parent733c20d5d3d696a60a7fd7098ca45cb545e78043 (diff)
downloadrockbox-dc6f23ec36979c6807988092cc9f696dcd6a2c81.zip
rockbox-dc6f23ec36979c6807988092cc9f696dcd6a2c81.tar.gz
rockbox-dc6f23ec36979c6807988092cc9f696dcd6a2c81.tar.bz2
rockbox-dc6f23ec36979c6807988092cc9f696dcd6a2c81.tar.xz
lua optimize integer and string consts in rocklib
use a table approach for registering integer / string constants Change-Id: Idbccae9c2203de1c694f6dd5a7014a7fccedae9b
Diffstat (limited to 'apps/plugins/lua')
-rw-r--r--apps/plugins/lua/rocklib.c86
1 files changed, 59 insertions, 27 deletions
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 94de2b5..ad8577c 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -421,8 +421,19 @@ static const luaL_Reg rocklib[] =
extern const luaL_Reg rocklib_aux[];
extern const luaL_Reg rocklib_img[];
-#define RB_CONSTANT(x) lua_pushinteger(L, x); lua_setfield(L, -2, #x);
-#define RB_STRING_CONSTANT(x) lua_pushstring(L, x); lua_setfield(L, -2, #x);
+#define RB_CONSTANT(x) {#x, x}
+#define RB_STRING_CONSTANT(x) {#x, x}
+
+struct lua_int_reg {
+ char const* name;
+ int value;
+};
+
+struct lua_str_reg {
+ char const* name;
+ char const* value;
+};
+
/*
** Open Rockbox library
*/
@@ -432,41 +443,62 @@ LUALIB_API int luaopen_rock(lua_State *L)
luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux);
luaL_register(L, LUA_ROCKLIBNAME, rocklib_img);
- RB_CONSTANT(HZ);
+ static const struct lua_int_reg rlib_const_int[] =
+ {
+ /* useful integer constants */
+ RB_CONSTANT(HZ),
- RB_CONSTANT(LCD_WIDTH);
- RB_CONSTANT(LCD_HEIGHT);
- RB_CONSTANT(LCD_DEPTH);
+ RB_CONSTANT(LCD_WIDTH),
+ RB_CONSTANT(LCD_HEIGHT),
+ RB_CONSTANT(LCD_DEPTH),
- RB_CONSTANT(FONT_SYSFIXED);
- RB_CONSTANT(FONT_UI);
+ RB_CONSTANT(FONT_SYSFIXED),
+ RB_CONSTANT(FONT_UI),
- RB_CONSTANT(PLAYLIST_PREPEND);
- RB_CONSTANT(PLAYLIST_INSERT);
- RB_CONSTANT(PLAYLIST_INSERT_LAST);
- RB_CONSTANT(PLAYLIST_INSERT_FIRST);
- RB_CONSTANT(PLAYLIST_INSERT_SHUFFLED);
- RB_CONSTANT(PLAYLIST_REPLACE);
- RB_CONSTANT(PLAYLIST_INSERT_LAST_SHUFFLED);
+ RB_CONSTANT(PLAYLIST_PREPEND),
+ RB_CONSTANT(PLAYLIST_INSERT),
+ RB_CONSTANT(PLAYLIST_INSERT_LAST),
+ RB_CONSTANT(PLAYLIST_INSERT_FIRST),
+ RB_CONSTANT(PLAYLIST_INSERT_SHUFFLED),
+ RB_CONSTANT(PLAYLIST_REPLACE),
+ RB_CONSTANT(PLAYLIST_INSERT_LAST_SHUFFLED),
#ifdef HAVE_TOUCHSCREEN
- RB_CONSTANT(TOUCHSCREEN_POINT);
- RB_CONSTANT(TOUCHSCREEN_BUTTON);
+ RB_CONSTANT(TOUCHSCREEN_POINT),
+ RB_CONSTANT(TOUCHSCREEN_BUTTON),
#endif
- RB_CONSTANT(SCREEN_MAIN);
+ RB_CONSTANT(SCREEN_MAIN),
#ifdef HAVE_REMOTE_LCD
- RB_CONSTANT(SCREEN_REMOTE);
+ RB_CONSTANT(SCREEN_REMOTE),
#endif
+ {NULL, 0}
+ };
+
+ static const struct lua_int_reg* rlci = rlib_const_int;
+ for (; rlci->name; rlci++) {
+ lua_pushinteger(L, rlci->value);
+ lua_setfield(L, -2, rlci->name);
+ }
- /* some useful paths constants */
- RB_STRING_CONSTANT(ROCKBOX_DIR);
- RB_STRING_CONSTANT(HOME_DIR);
- RB_STRING_CONSTANT(PLUGIN_DIR);
- RB_STRING_CONSTANT(PLUGIN_APPS_DATA_DIR);
- RB_STRING_CONSTANT(PLUGIN_GAMES_DATA_DIR);
- RB_STRING_CONSTANT(PLUGIN_DATA_DIR);
- RB_STRING_CONSTANT(VIEWERS_DATA_DIR);
+ static const struct lua_str_reg rlib_const_str[] =
+ {
+ /* some useful paths constants */
+ RB_STRING_CONSTANT(ROCKBOX_DIR),
+ RB_STRING_CONSTANT(HOME_DIR),
+ RB_STRING_CONSTANT(PLUGIN_DIR),
+ RB_STRING_CONSTANT(PLUGIN_APPS_DATA_DIR),
+ RB_STRING_CONSTANT(PLUGIN_GAMES_DATA_DIR),
+ RB_STRING_CONSTANT(PLUGIN_DATA_DIR),
+ RB_STRING_CONSTANT(VIEWERS_DATA_DIR),
+ {NULL,NULL}
+ };
+
+ static const struct lua_str_reg* rlcs = rlib_const_str;
+ for (; rlcs->name; rlcs++) {
+ lua_pushstring(L, rlcs->value);
+ lua_setfield(L, -2, rlcs->name);
+ }
rli_init(L);