diff options
author | aunsane <aunsane@gmail.com> | 2018-05-28 21:26:54 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2018-05-28 21:26:54 +0300 |
commit | 3bfd3b9c9062048fde854b4c45ab62c29624d8f8 (patch) | |
tree | 0003dc8a93986529e0618df81ce1059dc5ed00fd /plugins/MirLua/src/Modules/m_json.cpp | |
parent | d5c8ecc7a45798e8ba6674e6323244e2f2c291fb (diff) |
MirLua: removed global Load & Unload
Diffstat (limited to 'plugins/MirLua/src/Modules/m_json.cpp')
-rw-r--r-- | plugins/MirLua/src/Modules/m_json.cpp | 120 |
1 files changed, 74 insertions, 46 deletions
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); |