# HG changeset patch # User jbe # Date 1407687845 -7200 # Node ID fbfdd4f979d5deff090f792a9271cec84ff7ab13 # Parent 26132a017925f6d9b2616f5125425416bf5ee60b Finished non-recursive implementation of json.export(...) diff -r 26132a017925 -r fbfdd4f979d5 libraries/json/json.c --- a/libraries/json/json.c Sun Aug 10 18:16:08 2014 +0200 +++ b/libraries/json/json.c Sun Aug 10 18:24:05 2014 +0200 @@ -1146,6 +1146,8 @@ lua_rawget(L, json_export_shadowtbl_idx); if (lua_isnil(L, -1)) lua_pop(L, 1); else lua_replace(L, json_export_luacontainer_idx); + // reset keycount variable: + keycount = 0; // check if type of table is still undetermined: if (tabletype == JSON_TABLETYPE_UNKNOWN) { // if yes, iterate over all keys: @@ -1194,16 +1196,15 @@ if (lua_type(L, -2) == LUA_TSTRING) keycount++; } } - // create a sorted list of all string keys in memory: + // allocate memory for C structure containing string keys and container iteration state: + container = lua_newuserdata(L, sizeof(json_container_t) + (keycount-1) * sizeof(json_key_t)); + // store reference on designated stack position: + lua_replace(L, json_export_ccontainer_idx); + // initialize C structure for container state: + container->type = JSON_TABLETYPE_OBJECT; + container->count = keycount; + container->pos = 0; if (keycount) { - // allocate memory for C structure containing string keys and container iteration state: - container = lua_newuserdata(L, sizeof(json_container_t) + (keycount-1) * sizeof(json_key_t)); - // store reference on designated stack position: - lua_replace(L, json_export_ccontainer_idx); - // initialize C structure for container state: - container->type = JSON_TABLETYPE_OBJECT; - container->count = keycount; - container->pos = 0; // copy all string keys to the C structure and reset container->pos again: for (lua_pushnil(L); lua_next(L, json_export_luacontainer_idx); lua_pop(L, 1)) { if (lua_type(L, -2) == LUA_TSTRING) {