webmcp
changeset 148:5354d836e6fb
Minor bugfix in json_newindex function of JSON library (should return nothing)
author | jbe |
---|---|
date | Wed Jul 30 22:32:33 2014 +0200 (2014-07-30) |
parents | da5ab2c226dc |
children | 5229687c7601 |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Wed Jul 30 22:19:34 2014 +0200 1.2 +++ b/libraries/json/json.c Wed Jul 30 22:32:33 2014 +0200 1.3 @@ -431,10 +431,10 @@ 1.4 static int json_path(lua_State *L, int type_mode) { 1.5 int stacktop; // stack index of top of stack (after shifting) 1.6 int idx = 2 + json_path_idxshift; // stack index of current argument to process 1.7 - // insert json_shadowtbl into stack at position 1 (shifting the arguments): 1.8 + // insert shadowtbl into stack at position 1 (shifting the arguments): 1.9 json_regfetch(L, shadowtbl); 1.10 lua_insert(L, 1); 1.11 - // insert json_nullmark into stack at position 2 (shifting the arguments): 1.12 + // insert nullmark into stack at position 2 (shifting the arguments): 1.13 json_pushlightref(L, nullmark); 1.14 lua_insert(L, 2); 1.15 // store stack index of top of stack: 1.16 @@ -550,13 +550,13 @@ 1.17 static int json_setnull(lua_State *L) { 1.18 // stack shall contain two function arguments: 1.19 lua_settop(L, 2); 1.20 - // push json_unknownmt onto stack position 3: 1.21 + // push unknownmt onto stack position 3: 1.22 json_regfetch(L, unknownmt); 1.23 - // push json_objectmt onto stack position 4: 1.24 + // push objectmt onto stack position 4: 1.25 json_regfetch(L, objectmt); 1.26 - // push json_arraymt onto stack position 5: 1.27 + // push arraymt onto stack position 5: 1.28 json_regfetch(L, arraymt); 1.29 - // push json_shadowtbl onto stack position 6: 1.30 + // push shadowtbl onto stack position 6: 1.31 json_regfetch(L, shadowtbl); 1.32 // set metatable if necessary (leaves unknown number of elements on stack): 1.33 if ( 1.34 @@ -594,7 +594,7 @@ 1.35 static int json_len(lua_State *L) { 1.36 // stack shall contain one function argument: 1.37 lua_settop(L, 1); 1.38 - // push corresponding shadow table (for first argument) on top of stack: 1.39 + // try to get corresponding shadow table for first argument: 1.40 json_regfetch(L, shadowtbl); 1.41 lua_pushvalue(L, 1); 1.42 lua_rawget(L, -2); 1.43 @@ -608,28 +608,42 @@ 1.44 #define json_index_shadowtbl_idx 4 1.45 1.46 static int json_index(lua_State *L) { 1.47 + // stack shall contain two function arguments: 1.48 lua_settop(L, 2); 1.49 - json_pushlightref(L, nullmark); // on stack position 3 1.50 + // push nullmark onto stack position 3: 1.51 + json_pushlightref(L, nullmark); 1.52 + // push shadowtbl onto stack position 4: 1.53 json_regfetch(L, shadowtbl); 1.54 + // get corresponding shadow table for first argument: 1.55 lua_pushvalue(L, 1); 1.56 lua_rawget(L, json_index_shadowtbl_idx); 1.57 + // throw error if no shadow table was found: 1.58 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.59 + // use key passed as second argument to lookup value in shadow table: 1.60 lua_pushvalue(L, 2); 1.61 lua_rawget(L, -2); 1.62 + // if value is null-marker, then push nil onto stack: 1.63 if (lua_rawequal(L, -1, json_index_nullmark_idx)) lua_pushnil(L); 1.64 + // return either looked up value, or nil 1.65 return 1; 1.66 } 1.67 1.68 static int json_newindex(lua_State *L) { 1.69 + // stack shall contain three function arguments: 1.70 lua_settop(L, 3); 1.71 + // get corresponding shadow table for first argument: 1.72 json_regfetch(L, shadowtbl); 1.73 lua_pushvalue(L, 1); 1.74 lua_rawget(L, -2); 1.75 + // throw error if no shadow table was found: 1.76 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.77 + // replace first argument with shadow table: 1.78 lua_replace(L, 1); 1.79 + // reset stack and use second and third argument to write to shadow table: 1.80 lua_settop(L, 3); 1.81 lua_rawset(L, 1); 1.82 - return 1; 1.83 + // return nothing: 1.84 + return 0; 1.85 } 1.86 1.87 // special Lua stack indicies for json_pairs_iterfunc function: