From 3bfd3b9c9062048fde854b4c45ab62c29624d8f8 Mon Sep 17 00:00:00 2001 From: aunsane Date: Mon, 28 May 2018 21:26:54 +0300 Subject: MirLua: removed global Load & Unload --- plugins/MirLua/src/Modules/m_json.cpp | 120 +++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 46 deletions(-) (limited to 'plugins/MirLua/src/Modules/m_json.cpp') diff --git a/plugins/MirLua/src/Modules/m_json.cpp b/plugins/MirLua/src/Modules/m_json.cpp index 24e32cab09..907e0f582b 100644 --- a/plugins/MirLua/src/Modules/m_json.cpp +++ b/plugins/MirLua/src/Modules/m_json.cpp @@ -17,7 +17,7 @@ static void lua2json(lua_State *L, JSONNode &node) case LUA_TNUMBER: { lua_Integer val = lua_tointeger(L, -1); - if (lua_isinteger(L, -1) && val >= LONG_MIN && val <= LONG_MIN) + if (lua_isinteger(L, -1) && val >= LONG_MIN && val <= LONG_MAX) node = (long)val; else node = lua_tonumber(L, -1); @@ -48,6 +48,21 @@ static void lua2json(lua_State *L, JSONNode &node) break; } + case LUA_TLIGHTUSERDATA: + if (lua_touserdata(L, -1)) + luaL_argerror(L, -1, luaL_typename(L, -1)); + node.nullify(); + break; + case LUA_TUSERDATA: + { + ptrA name(mir_strdup(node.name())); + JSONNode *other = *(JSONNode**)luaL_checkudata(L, -1, MT_JSON); + node = other->duplicate(); + node.set_name((char*)name); + break; + } + default: + luaL_argerror(L, -1, luaL_typename(L, -1)); } } @@ -167,73 +182,86 @@ static int lua_Decode(lua_State *L) return 1; } +/***********************************************/ + static int lua_Encode(lua_State *L) { + //JSONNode node; + //lua_pushnil(L); + //lua_pushvalue(L, 1); + //lua2json(L, node); + //lua_pop(L, 2); + //lua_pushstring(L, node.write().c_str()); + 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)); + 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; } - char decpoint = lua_getlocaledecpoint(); - if (decpoint != '.') { - char p[2] = { decpoint }; - luaL_gsub(L, lua_tostring(L, 1), p, "."); + 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; } - 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 = *(JSONNode**)luaL_checkudata(L, 1, MT_JSON); - lua_pushstring(L, node->write().c_str()); - break; - } - case LUA_TLIGHTUSERDATA: - if (lua_touserdata(L, 1) == nullptr) + case LUA_TUSERDATA: { - lua_pushliteral(L, "null"); + JSONNode *node = *(JSONNode**)luaL_checkudata(L, 1, MT_JSON); + lua_pushstring(L, node->write().c_str()); break; } - default: - luaL_argerror(L, 1, luaL_typename(L, 1)); - } + case LUA_TLIGHTUSERDATA: + if (lua_touserdata(L, 1) == nullptr) + { + lua_pushliteral(L, "null"); + break; + } + default: + luaL_argerror(L, 1, luaL_typename(L, 1)); + } - return 1; -} + return 1; + } static const luaL_Reg methods[] = { { "Decode", lua_Decode }, { "Encode", lua_Encode }, + { "null", nullptr }, + { nullptr, nullptr } }; LUAMOD_API int luaopen_m_json(lua_State *L) { luaL_newlib(L, methods); + lua_pushlightuserdata(L, nullptr); + lua_setfield(L, -2, "null"); luaL_newmetatable(L, MT_JSON); luaL_setfuncs(L, jsonApi, 0); -- cgit v1.2.3