# HG changeset patch # User jbe # Date 1406410843 -7200 # Node ID ece4e6f683c9ded59d0eb2f33dfe6133dac0075d # Parent 402fce94f98c4837f686929b181352ac1abd3271 Fixed namespace problems in JSON library code diff -r 402fce94f98c -r ece4e6f683c9 libraries/json/json.c --- a/libraries/json/json.c Sat Jul 26 05:27:42 2014 +0200 +++ b/libraries/json/json.c Sat Jul 26 23:40:43 2014 +0200 @@ -5,22 +5,23 @@ #define JSON_UPVAL_ARYLEN lua_upvalueindex(1) #define JSON_UPVAL_NULLS lua_upvalueindex(2) +#define JSON_UPVAL_METATABLE lua_upvalueindex(3) -#define JSON_VALUE 0 -#define JSON_OBJECT_KEY 1 -#define JSON_OBJECT_KEY_TERMINATOR 2 -#define JSON_OBJECT_VALUE 3 -#define JSON_OBJECT_SEPARATOR 4 -#define JSON_ARRAY_VALUE 5 -#define JSON_ARRAY_SEPARATOR 6 -#define JSON_END 7 +#define JSON_STATE_VALUE 0 +#define JSON_STATE_OBJECT_KEY 1 +#define JSON_STATE_OBJECT_KEY_TERMINATOR 2 +#define JSON_STATE_OBJECT_VALUE 3 +#define JSON_STATE_OBJECT_SEPARATOR 4 +#define JSON_STATE_ARRAY_VALUE 5 +#define JSON_STATE_ARRAY_SEPARATOR 6 +#define JSON_STATE_END 7 static int json_import(lua_State *L) { const char *str; size_t total; size_t pos = 0; size_t level = 0; - int mode = JSON_VALUE; + int mode = JSON_STATE_VALUE; char c; luaL_Buffer luabuf; char *cbuf; @@ -33,14 +34,14 @@ while (c = str[pos], c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f') pos++; switch (c) { case 0: - if (mode == JSON_END) return 1; + if (mode == JSON_STATE_END) return 1; json_import_unexpected_eof: lua_pushnil(L); if (level == 0) lua_pushliteral(L, "Empty string"); else lua_pushliteral(L, "Unexpected end of JSON document"); return 2; case '{': - if (mode != JSON_VALUE && mode != JSON_OBJECT_VALUE && mode != JSON_ARRAY_VALUE) + if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE) goto json_import_syntax_error; pos++; lua_newtable(L); @@ -48,11 +49,11 @@ lua_pushvalue(L, -2); lua_pushvalue(L, -2); lua_rawset(L, JSON_UPVAL_NULLS); - mode = JSON_OBJECT_KEY; + mode = JSON_STATE_OBJECT_KEY; level++; goto json_import_loop; case '[': - if (mode != JSON_VALUE && mode != JSON_OBJECT_VALUE && mode != JSON_ARRAY_VALUE) + if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE) goto json_import_syntax_error; pos++; lua_newtable(L); @@ -61,15 +62,15 @@ lua_pushvalue(L, -2); lua_rawset(L, JSON_UPVAL_NULLS); lua_pushinteger(L, 0); // length of array (since it may contain nil's) - mode = JSON_ARRAY_VALUE; + mode = JSON_STATE_ARRAY_VALUE; level++; goto json_import_loop; case '}': - if (mode != JSON_OBJECT_KEY && mode != JSON_OBJECT_SEPARATOR) + if (mode != JSON_STATE_OBJECT_KEY && mode != JSON_STATE_OBJECT_SEPARATOR) goto json_import_syntax_error; goto json_import_close; case ']': - if (mode != JSON_ARRAY_VALUE && mode != JSON_ARRAY_SEPARATOR) + if (mode != JSON_STATE_ARRAY_VALUE && mode != JSON_STATE_ARRAY_SEPARATOR) goto json_import_syntax_error; lua_pushvalue(L, -2); // use array table as key lua_insert(L, -2); // use length of array as value @@ -80,26 +81,26 @@ lua_pop(L, 1); // pop table that stores set of NULL values if (--level) { if (lua_type(L, -2) == LUA_TNUMBER) { - mode = JSON_ARRAY_VALUE; + mode = JSON_STATE_ARRAY_VALUE; } else { - mode = JSON_OBJECT_VALUE; + mode = JSON_STATE_OBJECT_VALUE; } goto json_import_process_value; } else { - mode = JSON_END; + mode = JSON_STATE_END; } goto json_import_loop; case ':': - if (mode != JSON_OBJECT_KEY_TERMINATOR) + if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR) goto json_import_syntax_error; pos++; - mode = JSON_OBJECT_VALUE; + mode = JSON_STATE_OBJECT_VALUE; goto json_import_loop; case ',': - if (mode == JSON_OBJECT_SEPARATOR) { - mode = JSON_OBJECT_KEY; - } else if (mode == JSON_ARRAY_SEPARATOR) { - mode = JSON_ARRAY_VALUE; + if (mode == JSON_STATE_OBJECT_SEPARATOR) { + mode = JSON_STATE_OBJECT_KEY; + } else if (mode == JSON_STATE_ARRAY_SEPARATOR) { + mode = JSON_STATE_ARRAY_VALUE; } else { goto json_import_syntax_error; } @@ -179,20 +180,20 @@ } json_import_process_value: switch (mode) { - case JSON_OBJECT_KEY: + case JSON_STATE_OBJECT_KEY: if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error; - mode = JSON_OBJECT_KEY_TERMINATOR; + mode = JSON_STATE_OBJECT_KEY_TERMINATOR; goto json_import_loop; - case JSON_OBJECT_VALUE: + case JSON_STATE_OBJECT_VALUE: if (lua_isnil(L, -1)) { lua_pushvalue(L, -2); lua_pushboolean(L, 1); lua_rawset(L, -5); } lua_rawset(L, -4); - mode = JSON_OBJECT_SEPARATOR; + mode = JSON_STATE_OBJECT_SEPARATOR; goto json_import_loop; - case JSON_ARRAY_VALUE: + case JSON_STATE_ARRAY_VALUE: aryidx = lua_tointeger(L, -2) + 1; if (lua_isnil(L, -1)) { lua_pushinteger(L, aryidx); @@ -202,10 +203,10 @@ lua_rawseti(L, -4, aryidx); lua_pop(L, 1); lua_pushinteger(L, aryidx); - mode = JSON_ARRAY_SEPARATOR; + mode = JSON_STATE_ARRAY_SEPARATOR; goto json_import_loop; - case JSON_VALUE: - mode = JSON_END; + case JSON_STATE_VALUE: + mode = JSON_STATE_END; goto json_import_loop; } json_import_syntax_error: