diff options
author | William Wilgus <me.theuser@yahoo.com> | 2018-09-14 01:13:22 +0200 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2018-09-14 01:13:22 +0200 |
commit | dc6f23ec36979c6807988092cc9f696dcd6a2c81 (patch) | |
tree | 4d3463a96e27c029f129c3974c0dc7ca1bdf3b1a | |
parent | 733c20d5d3d696a60a7fd7098ca45cb545e78043 (diff) | |
download | rockbox-dc6f23ec36979c6807988092cc9f696dcd6a2c81.tar.gz rockbox-dc6f23ec36979c6807988092cc9f696dcd6a2c81.zip |
lua optimize integer and string consts in rocklib
use a table approach for registering integer / string constants
Change-Id: Idbccae9c2203de1c694f6dd5a7014a7fccedae9b
-rw-r--r-- | apps/plugins/lua/rocklib.c | 86 |
1 files changed, 59 insertions, 27 deletions
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index 94de2b560d..ad8577cb43 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c | |||
@@ -421,8 +421,19 @@ static const luaL_Reg rocklib[] = | |||
421 | extern const luaL_Reg rocklib_aux[]; | 421 | extern const luaL_Reg rocklib_aux[]; |
422 | extern const luaL_Reg rocklib_img[]; | 422 | extern const luaL_Reg rocklib_img[]; |
423 | 423 | ||
424 | #define RB_CONSTANT(x) lua_pushinteger(L, x); lua_setfield(L, -2, #x); | 424 | #define RB_CONSTANT(x) {#x, x} |
425 | #define RB_STRING_CONSTANT(x) lua_pushstring(L, x); lua_setfield(L, -2, #x); | 425 | #define RB_STRING_CONSTANT(x) {#x, x} |
426 | |||
427 | struct lua_int_reg { | ||
428 | char const* name; | ||
429 | int value; | ||
430 | }; | ||
431 | |||
432 | struct lua_str_reg { | ||
433 | char const* name; | ||
434 | char const* value; | ||
435 | }; | ||
436 | |||
426 | /* | 437 | /* |
427 | ** Open Rockbox library | 438 | ** Open Rockbox library |
428 | */ | 439 | */ |
@@ -432,41 +443,62 @@ LUALIB_API int luaopen_rock(lua_State *L) | |||
432 | luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux); | 443 | luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux); |
433 | luaL_register(L, LUA_ROCKLIBNAME, rocklib_img); | 444 | luaL_register(L, LUA_ROCKLIBNAME, rocklib_img); |
434 | 445 | ||
435 | RB_CONSTANT(HZ); | 446 | static const struct lua_int_reg rlib_const_int[] = |
447 | { | ||
448 | /* useful integer constants */ | ||
449 | RB_CONSTANT(HZ), | ||
436 | 450 | ||
437 | RB_CONSTANT(LCD_WIDTH); | 451 | RB_CONSTANT(LCD_WIDTH), |
438 | RB_CONSTANT(LCD_HEIGHT); | 452 | RB_CONSTANT(LCD_HEIGHT), |
439 | RB_CONSTANT(LCD_DEPTH); | 453 | RB_CONSTANT(LCD_DEPTH), |
440 | 454 | ||
441 | RB_CONSTANT(FONT_SYSFIXED); | 455 | RB_CONSTANT(FONT_SYSFIXED), |
442 | RB_CONSTANT(FONT_UI); | 456 | RB_CONSTANT(FONT_UI), |
443 | 457 | ||
444 | RB_CONSTANT(PLAYLIST_PREPEND); | 458 | RB_CONSTANT(PLAYLIST_PREPEND), |
445 | RB_CONSTANT(PLAYLIST_INSERT); | 459 | RB_CONSTANT(PLAYLIST_INSERT), |
446 | RB_CONSTANT(PLAYLIST_INSERT_LAST); | 460 | RB_CONSTANT(PLAYLIST_INSERT_LAST), |
447 | RB_CONSTANT(PLAYLIST_INSERT_FIRST); | 461 | RB_CONSTANT(PLAYLIST_INSERT_FIRST), |
448 | RB_CONSTANT(PLAYLIST_INSERT_SHUFFLED); | 462 | RB_CONSTANT(PLAYLIST_INSERT_SHUFFLED), |
449 | RB_CONSTANT(PLAYLIST_REPLACE); | 463 | RB_CONSTANT(PLAYLIST_REPLACE), |
450 | RB_CONSTANT(PLAYLIST_INSERT_LAST_SHUFFLED); | 464 | RB_CONSTANT(PLAYLIST_INSERT_LAST_SHUFFLED), |
451 | 465 | ||
452 | #ifdef HAVE_TOUCHSCREEN | 466 | #ifdef HAVE_TOUCHSCREEN |
453 | RB_CONSTANT(TOUCHSCREEN_POINT); | 467 | RB_CONSTANT(TOUCHSCREEN_POINT), |
454 | RB_CONSTANT(TOUCHSCREEN_BUTTON); | 468 | RB_CONSTANT(TOUCHSCREEN_BUTTON), |
455 | #endif | 469 | #endif |
456 | 470 | ||
457 | RB_CONSTANT(SCREEN_MAIN); | 471 | RB_CONSTANT(SCREEN_MAIN), |
458 | #ifdef HAVE_REMOTE_LCD | 472 | #ifdef HAVE_REMOTE_LCD |
459 | RB_CONSTANT(SCREEN_REMOTE); | 473 | RB_CONSTANT(SCREEN_REMOTE), |
460 | #endif | 474 | #endif |
475 | {NULL, 0} | ||
476 | }; | ||
477 | |||
478 | static const struct lua_int_reg* rlci = rlib_const_int; | ||
479 | for (; rlci->name; rlci++) { | ||
480 | lua_pushinteger(L, rlci->value); | ||
481 | lua_setfield(L, -2, rlci->name); | ||
482 | } | ||
461 | 483 | ||
462 | /* some useful paths constants */ | 484 | static const struct lua_str_reg rlib_const_str[] = |
463 | RB_STRING_CONSTANT(ROCKBOX_DIR); | 485 | { |
464 | RB_STRING_CONSTANT(HOME_DIR); | 486 | /* some useful paths constants */ |
465 | RB_STRING_CONSTANT(PLUGIN_DIR); | 487 | RB_STRING_CONSTANT(ROCKBOX_DIR), |
466 | RB_STRING_CONSTANT(PLUGIN_APPS_DATA_DIR); | 488 | RB_STRING_CONSTANT(HOME_DIR), |
467 | RB_STRING_CONSTANT(PLUGIN_GAMES_DATA_DIR); | 489 | RB_STRING_CONSTANT(PLUGIN_DIR), |
468 | RB_STRING_CONSTANT(PLUGIN_DATA_DIR); | 490 | RB_STRING_CONSTANT(PLUGIN_APPS_DATA_DIR), |
469 | RB_STRING_CONSTANT(VIEWERS_DATA_DIR); | 491 | RB_STRING_CONSTANT(PLUGIN_GAMES_DATA_DIR), |
492 | RB_STRING_CONSTANT(PLUGIN_DATA_DIR), | ||
493 | RB_STRING_CONSTANT(VIEWERS_DATA_DIR), | ||
494 | {NULL,NULL} | ||
495 | }; | ||
496 | |||
497 | static const struct lua_str_reg* rlcs = rlib_const_str; | ||
498 | for (; rlcs->name; rlcs++) { | ||
499 | lua_pushstring(L, rlcs->value); | ||
500 | lua_setfield(L, -2, rlcs->name); | ||
501 | } | ||
470 | 502 | ||
471 | rli_init(L); | 503 | rli_init(L); |
472 | 504 | ||