webmcp
changeset 140:641619d3fcb1
Fixed problem with unbalanced stack in json.import(...) function
author | jbe |
---|---|
date | Wed Jul 30 01:37:57 2014 +0200 (2014-07-30) |
parents | a4ce17051eff |
children | ca27aae3f1a1 |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Tue Jul 29 22:02:01 2014 +0200 1.2 +++ b/libraries/json/json.c Wed Jul 30 01:37:57 2014 +0200 1.3 @@ -162,6 +162,8 @@ 1.4 lua_pushvalue(L, -2); 1.5 lua_pushvalue(L, -2); 1.6 lua_rawset(L, json_import_shadowtbl_idx); 1.7 + // add nil as key (needed to keep stack balance) and as magic to detect arrays: 1.8 + lua_pushnil(L); 1.9 // increment level: 1.10 level++; 1.11 // expect array value (or end of array) and continue with loop: 1.12 @@ -179,6 +181,8 @@ 1.13 // if end of JSON array is not expected here, then return an error: 1.14 if (mode != JSON_STATE_ARRAY_VALUE && mode != JSON_STATE_ARRAY_SEPARATOR) 1.15 goto json_import_syntax_error; 1.16 + // pop nil key/magic: 1.17 + lua_pop(L, 1); 1.18 // continue with common code for end of JSON object and JSON array: 1.19 // common code for end of JSON object or JSON array: 1.20 json_import_close: 1.21 @@ -189,23 +193,17 @@ 1.22 // check if nested: 1.23 if (--level) { 1.24 // if nested, then check if outer(!) structure is an array or object: 1.25 - lua_getmetatable(L, c == '}' ? -4 : -3); // TODO: this approach doesn't work, because objects have an extra key on stack! 1.26 - if (lua_rawequal(L, -1, json_import_arraymt_idx)) { 1.27 + if (lua_isnil(L, -2)) { 1.28 // select array value processing: 1.29 - fprintf(stderr, "DEBUG: Process array\n"); 1.30 mode = JSON_STATE_ARRAY_VALUE; 1.31 } else { 1.32 // select object value processing: 1.33 - fprintf(stderr, "DEBUG: Process object\n"); 1.34 mode = JSON_STATE_OBJECT_VALUE; 1.35 } 1.36 - // pop metatable from stack (that was needed for type distinction): 1.37 - lua_pop(L, 1); 1.38 // store value in outer structure: 1.39 goto json_import_process_value; 1.40 } 1.41 // if not nested, then expect end of JSON document and continue with loop: 1.42 - fprintf(stderr, "DEBUG: Process document\n"); 1.43 mode = JSON_STATE_END; 1.44 goto json_import_loop; 1.45 // key terminator: 1.46 @@ -354,7 +352,7 @@ 1.47 // an array value has been read: 1.48 case JSON_STATE_ARRAY_VALUE: 1.49 // store value in outer shadow table: 1.50 - lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); 1.51 + lua_rawseti(L, -3, lua_rawlen(L, -3) + 1); 1.52 // expect value terminator (or end of object) and continue with loop: 1.53 mode = JSON_STATE_ARRAY_SEPARATOR; 1.54 goto json_import_loop;