summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2009-05-25 19:05:53 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2009-05-25 19:05:53 +0000
commitcd5e98db08ee25cbabcafbd005b7eb9820a15137 (patch)
tree1464ee9f5ec08da3bc144ed3d88a3b96bdabdfa3 /apps
parentb50f18d50c8acad1c178fbbf8c4d0351cfbebdea (diff)
downloadrockbox-cd5e98db08ee25cbabcafbd005b7eb9820a15137.tar.gz
rockbox-cd5e98db08ee25cbabcafbd005b7eb9820a15137.zip
Lua: re-use the viewport pointer; also use the shorter lua_getfield() & lua_setfield() notations (thanks Antoine Cellerier)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21080 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/lua/rocklib.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 1560f19d9e..22e86f91b7 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -184,8 +184,7 @@ static inline void rli_init(lua_State *L)
/* Helper function for opt_viewport */
static void check_tablevalue(lua_State *L, const char* key, int tablepos, void* res, bool unsigned_val)
{
- lua_pushstring(L, key); /* Push the key on the stack */
- lua_gettable(L, tablepos); /* Find table[key] (pops key off the stack) */
+ lua_getfield(L, tablepos, key); /* Find table[key] */
if(!lua_isnoneornil(L, -1))
{
@@ -204,11 +203,22 @@ static struct viewport* opt_viewport(lua_State *L, int narg, struct viewport* al
return alt;
int tablepos = lua_gettop(L);
+ struct viewport *vp;
- lua_pushliteral(L, "vp"); /* push 'vp' on the stack */
- struct viewport *vp = (struct viewport*)lua_newuserdata(L, sizeof(struct viewport)); /* allocate memory and push it as udata on the stack */
- memset(vp, 0, sizeof(struct viewport)); /* Init viewport values to 0 */
- lua_settable(L, tablepos); /* table['vp'] = vp (pops key & value off the stack) */
+ lua_getfield(L, tablepos, "vp"); /* get table['vp'] */
+ if(lua_isnoneornil(L, -1))
+ {
+ lua_pop(L, 1); /* Pop nil off stack */
+
+ vp = (struct viewport*) lua_newuserdata(L, sizeof(struct viewport)); /* Allocate memory and push it as udata on the stack */
+ memset(vp, 0, sizeof(struct viewport)); /* Init viewport values to 0 */
+ lua_setfield(L, tablepos, "vp"); /* table['vp'] = vp (pops value off the stack) */
+ }
+ else
+ {
+ vp = (struct viewport*) lua_touserdata(L, -1); /* Reuse viewport struct */
+ lua_pop(L, 1); /* We don't need the value on stack */
+ }
luaL_checktype(L, narg, LUA_TTABLE);