# HG changeset patch # User jbe # Date 1406664121 -7200 # Node ID a4ce17051eff38e7e71cbea248d8ecf3a25d11a5 # Parent 8a533f37003801359aaeb1161cfe486d31713fa5 Work on JSON library (problem with stack still existent) diff -r 8a533f370038 -r a4ce17051eff libraries/json/json.c --- a/libraries/json/json.c Mon Jul 28 23:39:23 2014 +0200 +++ b/libraries/json/json.c Tue Jul 29 22:02:01 2014 +0200 @@ -189,13 +189,14 @@ // check if nested: if (--level) { // if nested, then check if outer(!) structure is an array or object: - lua_pushvalue(L, c == '}' ? -4 : -3); - lua_getmetatable(L, -1); - if (lua_touserdata(L, -1) == &json_arraymt) { + lua_getmetatable(L, c == '}' ? -4 : -3); // TODO: this approach doesn't work, because objects have an extra key on stack! + if (lua_rawequal(L, -1, json_import_arraymt_idx)) { // select array value processing: + fprintf(stderr, "DEBUG: Process array\n"); mode = JSON_STATE_ARRAY_VALUE; } else { // select object value processing: + fprintf(stderr, "DEBUG: Process object\n"); mode = JSON_STATE_OBJECT_VALUE; } // pop metatable from stack (that was needed for type distinction): @@ -204,6 +205,7 @@ goto json_import_process_value; } // if not nested, then expect end of JSON document and continue with loop: + fprintf(stderr, "DEBUG: Process document\n"); mode = JSON_STATE_END; goto json_import_loop; // key terminator: @@ -530,13 +532,12 @@ return 1; } -/* - static int json_index(lua_State *L) { lua_settop(L, 2); + json_regfetch(L, json_shadowtbl); lua_pushvalue(L, 1); - lua_rawget(L, JSON_UPVAL_SHADOWTBL); - if (lua_isnil(L, -1)) return 1; + lua_rawget(L, -2); + if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); lua_pushvalue(L, 2); lua_rawget(L, -2); if (lua_rawequal(L, -1, JSON_UPVAL_NULLMARK)) lua_pushnil(L); @@ -545,22 +546,29 @@ static int json_newindex(lua_State *L) { lua_settop(L, 3); + json_regfetch(L, json_shadowtbl); lua_pushvalue(L, 1); - lua_rawget(L, JSON_UPVAL_SHADOWTBL); + lua_rawget(L, -1); if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); lua_replace(L, 1); + lua_settop(L, 3); lua_rawset(L, 1); return 1; } +#define json_pairs_iterfunc_nullmark_idx 3 +#define json_pairs_iterfunc_shadowtbl_idx 4 + static int json_pairs_iterfunc(lua_State *L) { lua_settop(L, 2); + json_regfetch(L, json_nullmark); // on stack position 3 + json_regfetch(L, json_shadowtbl); lua_pushvalue(L, 1); - lua_rawget(L, JSON_UPVAL_SHADOWTBL); + lua_rawget(L, json_pairs_iterfunc_shadowtbl_idx); if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); lua_pushvalue(L, 2); if (!lua_next(L, -2)) return 0; - if (lua_rawequal(L, -1, JSON_UPVAL_NULLMARK)) { + if (lua_rawequal(L, -1, json_pairs_iterfunc_nullmark_idx)) { lua_pop(L, 1); lua_pushnil(L); } @@ -568,36 +576,39 @@ } static int json_pairs(lua_State *L) { - lua_pushvalue(L, JSON_UPVAL_PAIRS_ITERFUNC); + lua_pushcfunction(L, json_pairs_iterfunc); lua_pushvalue(L, 1); lua_pushnil(L); return 3; } +#define json_ipairs_iterfunc_nullmark_idx 3 +#define json_ipairs_iterfunc_shadowtbl_idx 4 + static int json_ipairs_iterfunc(lua_State *L) { int idx; lua_settop(L, 2); + json_regfetch(L, json_nullmark); // on stack position 3 + json_regfetch(L, json_shadowtbl); idx = lua_tointeger(L, 2) + 1; lua_pushvalue(L, 1); - lua_rawget(L, JSON_UPVAL_SHADOWTBL); + lua_rawget(L, json_ipairs_iterfunc_shadowtbl_idx); if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); lua_rawgeti(L, -1, idx); if (lua_isnil(L, -1)) return 0; lua_pushinteger(L, idx); - if (lua_rawequal(L, -2, JSON_UPVAL_NULLMARK)) lua_pushnil(L); + if (lua_rawequal(L, -2, json_ipairs_iterfunc_nullmark_idx)) lua_pushnil(L); else lua_pushvalue(L, -2); return 2; } static int json_ipairs(lua_State *L) { - lua_pushvalue(L, JSON_UPVAL_IPAIRS_ITERFUNC); + lua_pushcfunction(L, json_ipairs_iterfunc); lua_pushvalue(L, 1); lua_pushinteger(L, 0); return 3; } -*/ - static const struct luaL_Reg json_module_functions[] = { {"object", json_object}, {"array", json_array}, @@ -611,12 +622,10 @@ static const struct luaL_Reg json_metatable_functions[] = { {"__len", json_len}, - /* {"__index", json_index}, {"__newindex", json_newindex}, {"__pairs", json_pairs}, {"__ipairs", json_ipairs}, - */ {NULL, NULL} };