summaryrefslogtreecommitdiff
path: root/plugins/MirLua/Modules
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2017-10-05 21:24:06 +0300
committeraunsane <aunsane@gmail.com>2017-10-05 21:24:29 +0300
commit4989d780b22b6a40866c417c16dda0ae55d46dad (patch)
tree8aa483b5a82a3dc6cf19f85912e62e51f0c2b6b6 /plugins/MirLua/Modules
parent07af3bc24151d8aee445da24eddbac5ac1592c03 (diff)
MirLua: simplified print
m_json reworked Encode
Diffstat (limited to 'plugins/MirLua/Modules')
-rw-r--r--plugins/MirLua/Modules/m_json/src/main.cpp60
-rw-r--r--plugins/MirLua/Modules/m_json/src/metatable.cpp28
2 files changed, 62 insertions, 26 deletions
diff --git a/plugins/MirLua/Modules/m_json/src/main.cpp b/plugins/MirLua/Modules/m_json/src/main.cpp
index fd7c40b1ff..3774442d7f 100644
--- a/plugins/MirLua/Modules/m_json/src/main.cpp
+++ b/plugins/MirLua/Modules/m_json/src/main.cpp
@@ -13,15 +13,57 @@ static int lua_Decode(lua_State *L)
static int lua_Encode(lua_State *L)
{
- luaL_checktype(L, 1, LUA_TTABLE);
-
- JSONNode *node = json_new(JSON_NODE);
- lua_pushnil(L);
- lua_pushvalue(L, 1);
- lua2json(L, *node);
- lua_pop(L, 2);
- JSON *mt = new (L) JSON(node);
- luaL_setmetatable(L, MT_JSON);
+ switch (lua_type(L, 1))
+ {
+ case LUA_TNIL:
+ lua_pushliteral(L, "null");
+ break;
+ case LUA_TBOOLEAN:
+ lua_pushstring(L, lua_toboolean(L, 1) ? "true" : "false");
+ break;
+ case LUA_TNUMBER:
+ {
+ if (lua_isinteger(L, 1)) {
+ lua_pushfstring(L, "%I", lua_tointeger(L, 1));
+ break;
+ }
+ char decpoint = lua_getlocaledecpoint();
+ if (decpoint != '.') {
+ char p[2] = { decpoint };
+ luaL_gsub(L, lua_tostring(L, 1), p, ".");
+ }
+ else
+ lua_pushfstring(L, "%f", lua_tonumber(L, 1));
+ break;
+ }
+ case LUA_TSTRING:
+ lua_pushfstring(L, "\"%s\"", lua_tostring(L, 1));
+ break;
+ case LUA_TTABLE:
+ {
+ JSONNode node;
+ lua_pushnil(L);
+ lua_pushvalue(L, 1);
+ lua2json(L, node);
+ lua_pop(L, 2);
+ lua_pushstring(L, node.write().c_str());
+ break;
+ }
+ case LUA_TUSERDATA:
+ {
+ JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;
+ lua_pushstring(L, node.write().c_str());
+ break;
+ }
+ case LUA_TLIGHTUSERDATA:
+ if (lua_touserdata(L, 1) == NULL)
+ {
+ lua_pushliteral(L, "null");
+ break;
+ }
+ default:
+ luaL_argerror(L, 1, luaL_typename(L, 1));
+ }
return 1;
}
diff --git a/plugins/MirLua/Modules/m_json/src/metatable.cpp b/plugins/MirLua/Modules/m_json/src/metatable.cpp
index 976ccd9b16..d7bfab5583 100644
--- a/plugins/MirLua/Modules/m_json/src/metatable.cpp
+++ b/plugins/MirLua/Modules/m_json/src/metatable.cpp
@@ -16,8 +16,8 @@ void lua2json(lua_State *L, JSONNode &node)
case LUA_TNUMBER:
{
lua_Integer val = lua_tointeger(L, -1);
- if (lua_isinteger(L, -1) && val >= INT_MIN && val <= INT_MAX)
- node = (int)val;
+ if (lua_isinteger(L, -1) && val >= LONG_MIN && val <= LONG_MIN)
+ node = (long)val;
else
node = lua_tonumber(L, -1);
break;
@@ -29,17 +29,13 @@ void lua2json(lua_State *L, JSONNode &node)
node.set_name((char*)name);
lua_pushnil(L);
- while (lua_next(L, -2) != 0)
- {
+ while (lua_next(L, -2) != 0) {
JSONNode child;
- if (!lua_isnumber(L, -2))
- {
- if (node.type() == JSON_ARRAY)
- {
+ if (!lua_isnumber(L, -2)) {
+ if (node.type() == JSON_ARRAY) {
node.cast(JSON_NODE);
node.set_name((char*)name);
}
-
const char *key = lua_tostring(L, -2);
child.set_name(key);
}
@@ -87,17 +83,15 @@ static int json__newindex(lua_State *L)
const char *key = lua_tostring(L, 2);
JSONNode child = node[key];
- if (child.isnull())
- {
+ if (child.isnull()) {
child.set_name(key);
lua2json(L, child);
node << child;
+ return 0;
}
- else
- {
- lua2json(L, child);
- node[key] = child;
- }
+
+ lua2json(L, child);
+ node[key] = child;
return 0;
}
@@ -129,7 +123,7 @@ static int json__gc(lua_State *L)
return 0;
}
-static const struct luaL_Reg jsonApi[] =
+const struct luaL_Reg jsonApi[] =
{
{ "__index", json__index },
{ "__newindex", json__newindex },