webmcp
changeset 139:a4ce17051eff
Work on JSON library (problem with stack still existent)
author | jbe |
---|---|
date | Tue Jul 29 22:02:01 2014 +0200 (2014-07-29) |
parents | 8a533f370038 |
children | 641619d3fcb1 |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Mon Jul 28 23:39:23 2014 +0200 1.2 +++ b/libraries/json/json.c Tue Jul 29 22:02:01 2014 +0200 1.3 @@ -189,13 +189,14 @@ 1.4 // check if nested: 1.5 if (--level) { 1.6 // if nested, then check if outer(!) structure is an array or object: 1.7 - lua_pushvalue(L, c == '}' ? -4 : -3); 1.8 - lua_getmetatable(L, -1); 1.9 - if (lua_touserdata(L, -1) == &json_arraymt) { 1.10 + lua_getmetatable(L, c == '}' ? -4 : -3); // TODO: this approach doesn't work, because objects have an extra key on stack! 1.11 + if (lua_rawequal(L, -1, json_import_arraymt_idx)) { 1.12 // select array value processing: 1.13 + fprintf(stderr, "DEBUG: Process array\n"); 1.14 mode = JSON_STATE_ARRAY_VALUE; 1.15 } else { 1.16 // select object value processing: 1.17 + fprintf(stderr, "DEBUG: Process object\n"); 1.18 mode = JSON_STATE_OBJECT_VALUE; 1.19 } 1.20 // pop metatable from stack (that was needed for type distinction): 1.21 @@ -204,6 +205,7 @@ 1.22 goto json_import_process_value; 1.23 } 1.24 // if not nested, then expect end of JSON document and continue with loop: 1.25 + fprintf(stderr, "DEBUG: Process document\n"); 1.26 mode = JSON_STATE_END; 1.27 goto json_import_loop; 1.28 // key terminator: 1.29 @@ -530,13 +532,12 @@ 1.30 return 1; 1.31 } 1.32 1.33 -/* 1.34 - 1.35 static int json_index(lua_State *L) { 1.36 lua_settop(L, 2); 1.37 + json_regfetch(L, json_shadowtbl); 1.38 lua_pushvalue(L, 1); 1.39 - lua_rawget(L, JSON_UPVAL_SHADOWTBL); 1.40 - if (lua_isnil(L, -1)) return 1; 1.41 + lua_rawget(L, -2); 1.42 + if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.43 lua_pushvalue(L, 2); 1.44 lua_rawget(L, -2); 1.45 if (lua_rawequal(L, -1, JSON_UPVAL_NULLMARK)) lua_pushnil(L); 1.46 @@ -545,22 +546,29 @@ 1.47 1.48 static int json_newindex(lua_State *L) { 1.49 lua_settop(L, 3); 1.50 + json_regfetch(L, json_shadowtbl); 1.51 lua_pushvalue(L, 1); 1.52 - lua_rawget(L, JSON_UPVAL_SHADOWTBL); 1.53 + lua_rawget(L, -1); 1.54 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.55 lua_replace(L, 1); 1.56 + lua_settop(L, 3); 1.57 lua_rawset(L, 1); 1.58 return 1; 1.59 } 1.60 1.61 +#define json_pairs_iterfunc_nullmark_idx 3 1.62 +#define json_pairs_iterfunc_shadowtbl_idx 4 1.63 + 1.64 static int json_pairs_iterfunc(lua_State *L) { 1.65 lua_settop(L, 2); 1.66 + json_regfetch(L, json_nullmark); // on stack position 3 1.67 + json_regfetch(L, json_shadowtbl); 1.68 lua_pushvalue(L, 1); 1.69 - lua_rawget(L, JSON_UPVAL_SHADOWTBL); 1.70 + lua_rawget(L, json_pairs_iterfunc_shadowtbl_idx); 1.71 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.72 lua_pushvalue(L, 2); 1.73 if (!lua_next(L, -2)) return 0; 1.74 - if (lua_rawequal(L, -1, JSON_UPVAL_NULLMARK)) { 1.75 + if (lua_rawequal(L, -1, json_pairs_iterfunc_nullmark_idx)) { 1.76 lua_pop(L, 1); 1.77 lua_pushnil(L); 1.78 } 1.79 @@ -568,36 +576,39 @@ 1.80 } 1.81 1.82 static int json_pairs(lua_State *L) { 1.83 - lua_pushvalue(L, JSON_UPVAL_PAIRS_ITERFUNC); 1.84 + lua_pushcfunction(L, json_pairs_iterfunc); 1.85 lua_pushvalue(L, 1); 1.86 lua_pushnil(L); 1.87 return 3; 1.88 } 1.89 1.90 +#define json_ipairs_iterfunc_nullmark_idx 3 1.91 +#define json_ipairs_iterfunc_shadowtbl_idx 4 1.92 + 1.93 static int json_ipairs_iterfunc(lua_State *L) { 1.94 int idx; 1.95 lua_settop(L, 2); 1.96 + json_regfetch(L, json_nullmark); // on stack position 3 1.97 + json_regfetch(L, json_shadowtbl); 1.98 idx = lua_tointeger(L, 2) + 1; 1.99 lua_pushvalue(L, 1); 1.100 - lua_rawget(L, JSON_UPVAL_SHADOWTBL); 1.101 + lua_rawget(L, json_ipairs_iterfunc_shadowtbl_idx); 1.102 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.103 lua_rawgeti(L, -1, idx); 1.104 if (lua_isnil(L, -1)) return 0; 1.105 lua_pushinteger(L, idx); 1.106 - if (lua_rawequal(L, -2, JSON_UPVAL_NULLMARK)) lua_pushnil(L); 1.107 + if (lua_rawequal(L, -2, json_ipairs_iterfunc_nullmark_idx)) lua_pushnil(L); 1.108 else lua_pushvalue(L, -2); 1.109 return 2; 1.110 } 1.111 1.112 static int json_ipairs(lua_State *L) { 1.113 - lua_pushvalue(L, JSON_UPVAL_IPAIRS_ITERFUNC); 1.114 + lua_pushcfunction(L, json_ipairs_iterfunc); 1.115 lua_pushvalue(L, 1); 1.116 lua_pushinteger(L, 0); 1.117 return 3; 1.118 } 1.119 1.120 -*/ 1.121 - 1.122 static const struct luaL_Reg json_module_functions[] = { 1.123 {"object", json_object}, 1.124 {"array", json_array}, 1.125 @@ -611,12 +622,10 @@ 1.126 1.127 static const struct luaL_Reg json_metatable_functions[] = { 1.128 {"__len", json_len}, 1.129 - /* 1.130 {"__index", json_index}, 1.131 {"__newindex", json_newindex}, 1.132 {"__pairs", json_pairs}, 1.133 {"__ipairs", json_ipairs}, 1.134 - */ 1.135 {NULL, NULL} 1.136 }; 1.137