Commit c7633030 authored by seny's avatar seny
Browse files

Cosmetics & minor optimizations

parent e59ca80a
...@@ -173,6 +173,10 @@ static size_t decodeArray(lua_State *L, const char *buf, size_t pos, size_t size ...@@ -173,6 +173,10 @@ static size_t decodeArray(lua_State *L, const char *buf, size_t pos, size_t size
done: done:
lua_pushinteger(L, len); lua_pushinteger(L, len);
lua_setfield(L, -2, "__array"); lua_setfield(L, -2, "__array");
if (lua_isnil(L, hidx)) return pos;
lua_pushvalue(L, hidx);
lua_insert(L, -2);
lua_call(L, 1, 1); /* Transform value */
return pos; return pos;
} }
...@@ -181,7 +185,7 @@ static size_t decodeObject(lua_State *L, const char *buf, size_t pos, size_t siz ...@@ -181,7 +185,7 @@ static size_t decodeObject(lua_State *L, const char *buf, size_t pos, size_t siz
pos = decodeDelimiter(L, buf, pos, size, '{', 0); pos = decodeDelimiter(L, buf, pos, size, '{', 0);
pos = decodeDelimiter(L, buf, pos, size, '}', &res); pos = decodeDelimiter(L, buf, pos, size, '}', &res);
lua_newtable(L); lua_newtable(L);
if (res) return pos; if (res) goto done;
checkStack(L); checkStack(L);
do { do {
pos = decodeString(L, buf, pos, size); pos = decodeString(L, buf, pos, size);
...@@ -191,6 +195,11 @@ static size_t decodeObject(lua_State *L, const char *buf, size_t pos, size_t siz ...@@ -191,6 +195,11 @@ static size_t decodeObject(lua_State *L, const char *buf, size_t pos, size_t siz
pos = decodeDelimiter(L, buf, pos, size, ',', &res); pos = decodeDelimiter(L, buf, pos, size, ',', &res);
} while (res); } while (res);
pos = decodeDelimiter(L, buf, pos, size, '}', 0); pos = decodeDelimiter(L, buf, pos, size, '}', 0);
done:
if (lua_isnil(L, hidx)) return pos;
lua_pushvalue(L, hidx);
lua_insert(L, -2);
lua_call(L, 1, 1); /* Transform value */
return pos; return pos;
} }
...@@ -281,23 +290,14 @@ static size_t decodeValue(lua_State *L, const char *buf, size_t pos, size_t size ...@@ -281,23 +290,14 @@ static size_t decodeValue(lua_State *L, const char *buf, size_t pos, size_t size
if (pos >= size) luaL_error(L, "value expected at position %d", pos + 1); if (pos >= size) luaL_error(L, "value expected at position %d", pos + 1);
switch (buf[pos]) { switch (buf[pos]) {
case '"': case '"':
pos = decodeString(L, buf, pos, size); return decodeString(L, buf, pos, size);
break;
case '[': case '[':
pos = decodeArray(L, buf, pos, size, hidx); return decodeArray(L, buf, pos, size, hidx);
break;
case '{': case '{':
pos = decodeObject(L, buf, pos, size, hidx); return decodeObject(L, buf, pos, size, hidx);
break;
default: default:
pos = decodeLiteral(L, buf, pos, size); return decodeLiteral(L, buf, pos, size);
break;
} }
if (!lua_istable(L, -1) || lua_isnil(L, hidx)) return pos;
lua_pushvalue(L, hidx);
lua_insert(L, -2);
lua_call(L, 1, 1); /* Call handler */
return pos;
} }
int json__decode(lua_State *L) { int json__decode(lua_State *L) {
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
** THE SOFTWARE. ** THE SOFTWARE.
*/ */
#include <stdarg.h>
#include <string.h> #include <string.h>
#include <locale.h> #include <locale.h>
#include "json.h" #include "json.h"
...@@ -237,14 +236,12 @@ static int encodeValue(lua_State *L, Box *box, int idx, const char *ev, int ridx ...@@ -237,14 +236,12 @@ static int encodeValue(lua_State *L, Box *box, int idx, const char *ev, int ridx
int json__encode(lua_State *L) { int json__encode(lua_State *L) {
Box *box; Box *box;
const char *ev;
int nerr = 0; int nerr = 0;
const char *ev = luaL_optstring(L, 2, "__toJSON");
luaL_checkany(L, 1); luaL_checkany(L, 1);
ev = luaL_optstring(L, 2, "__toJSON");
lua_settop(L, 2); lua_settop(L, 2);
lua_newtable(L); lua_newtable(L);
box = newBox(L); if (!encodeValue(L, box = newBox(L), 1, ev, 3, &nerr)) {
if (!encodeValue(L, box, 1, ev, 3, &nerr)) {
lua_concat(L, nerr); lua_concat(L, nerr);
return luaL_argerror(L, 1, lua_tostring(L, -1)); return luaL_argerror(L, 1, lua_tostring(L, -1));
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment