# HG changeset patch # User jbe # Date 1406752353 -7200 # Node ID 5354d836e6fb8d48eccb9c0b027abb4593e30d03 # Parent da5ab2c226dc46919e2da1249c73d9cf579a1994 Minor bugfix in json_newindex function of JSON library (should return nothing) diff -r da5ab2c226dc -r 5354d836e6fb libraries/json/json.c --- a/libraries/json/json.c Wed Jul 30 22:19:34 2014 +0200 +++ b/libraries/json/json.c Wed Jul 30 22:32:33 2014 +0200 @@ -431,10 +431,10 @@ static int json_path(lua_State *L, int type_mode) { int stacktop; // stack index of top of stack (after shifting) int idx = 2 + json_path_idxshift; // stack index of current argument to process - // insert json_shadowtbl into stack at position 1 (shifting the arguments): + // insert shadowtbl into stack at position 1 (shifting the arguments): json_regfetch(L, shadowtbl); lua_insert(L, 1); - // insert json_nullmark into stack at position 2 (shifting the arguments): + // insert nullmark into stack at position 2 (shifting the arguments): json_pushlightref(L, nullmark); lua_insert(L, 2); // store stack index of top of stack: @@ -550,13 +550,13 @@ static int json_setnull(lua_State *L) { // stack shall contain two function arguments: lua_settop(L, 2); - // push json_unknownmt onto stack position 3: + // push unknownmt onto stack position 3: json_regfetch(L, unknownmt); - // push json_objectmt onto stack position 4: + // push objectmt onto stack position 4: json_regfetch(L, objectmt); - // push json_arraymt onto stack position 5: + // push arraymt onto stack position 5: json_regfetch(L, arraymt); - // push json_shadowtbl onto stack position 6: + // push shadowtbl onto stack position 6: json_regfetch(L, shadowtbl); // set metatable if necessary (leaves unknown number of elements on stack): if ( @@ -594,7 +594,7 @@ static int json_len(lua_State *L) { // stack shall contain one function argument: lua_settop(L, 1); - // push corresponding shadow table (for first argument) on top of stack: + // try to get corresponding shadow table for first argument: json_regfetch(L, shadowtbl); lua_pushvalue(L, 1); lua_rawget(L, -2); @@ -608,28 +608,42 @@ #define json_index_shadowtbl_idx 4 static int json_index(lua_State *L) { + // stack shall contain two function arguments: lua_settop(L, 2); - json_pushlightref(L, nullmark); // on stack position 3 + // push nullmark onto stack position 3: + json_pushlightref(L, nullmark); + // push shadowtbl onto stack position 4: json_regfetch(L, shadowtbl); + // get corresponding shadow table for first argument: lua_pushvalue(L, 1); lua_rawget(L, json_index_shadowtbl_idx); + // throw error if no shadow table was found: if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); + // use key passed as second argument to lookup value in shadow table: lua_pushvalue(L, 2); lua_rawget(L, -2); + // if value is null-marker, then push nil onto stack: if (lua_rawequal(L, -1, json_index_nullmark_idx)) lua_pushnil(L); + // return either looked up value, or nil return 1; } static int json_newindex(lua_State *L) { + // stack shall contain three function arguments: lua_settop(L, 3); + // get corresponding shadow table for first argument: json_regfetch(L, shadowtbl); lua_pushvalue(L, 1); lua_rawget(L, -2); + // throw error if no shadow table was found: if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); + // replace first argument with shadow table: lua_replace(L, 1); + // reset stack and use second and third argument to write to shadow table: lua_settop(L, 3); lua_rawset(L, 1); - return 1; + // return nothing: + return 0; } // special Lua stack indicies for json_pairs_iterfunc function: