summaryrefslogtreecommitdiff
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)
184/* Helper function for opt_viewport */ 184/* Helper function for opt_viewport */
185static void check_tablevalue(lua_State *L, const char* key, int tablepos, void* res, bool unsigned_val) 185static void check_tablevalue(lua_State *L, const char* key, int tablepos, void* res, bool unsigned_val)
186{ 186{
187 lua_pushstring(L, key); /* Push the key on the stack */ 187 lua_getfield(L, tablepos, key); /* Find table[key] */
188 lua_gettable(L, tablepos); /* Find table[key] (pops key off the stack) */
189 188
190 if(!lua_isnoneornil(L, -1)) 189 if(!lua_isnoneornil(L, -1))
191 { 190 {
@@ -204,11 +203,22 @@ static struct viewport* opt_viewport(lua_State *L, int narg, struct viewport* al
204 return alt; 203 return alt;
205 204
206 int tablepos = lua_gettop(L); 205 int tablepos = lua_gettop(L);
206 struct viewport *vp;
207 207
208 lua_pushliteral(L, "vp"); /* push 'vp' on the stack */ 208 lua_getfield(L, tablepos, "vp"); /* get table['vp'] */
209 struct viewport *vp = (struct viewport*)lua_newuserdata(L, sizeof(struct viewport)); /* allocate memory and push it as udata on the stack */ 209 if(lua_isnoneornil(L, -1))
210 memset(vp, 0, sizeof(struct viewport)); /* Init viewport values to 0 */ 210 {
211 lua_settable(L, tablepos); /* table['vp'] = vp (pops key & value off the stack) */ 211 lua_pop(L, 1); /* Pop nil off stack */
212
213 vp = (struct viewport*) lua_newuserdata(L, sizeof(struct viewport)); /* Allocate memory and push it as udata on the stack */
214 memset(vp, 0, sizeof(struct viewport)); /* Init viewport values to 0 */
215 lua_setfield(L, tablepos, "vp"); /* table['vp'] = vp (pops value off the stack) */
216 }
217 else
218 {
219 vp = (struct viewport*) lua_touserdata(L, -1); /* Reuse viewport struct */
220 lua_pop(L, 1); /* We don't need the value on stack */
221 }
212 222
213 luaL_checktype(L, narg, LUA_TTABLE); 223 luaL_checktype(L, narg, LUA_TTABLE);
214 224