summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorAlexander Lantsev <aunsane@gmail.com>2016-06-19 18:42:13 +0000
committerAlexander Lantsev <aunsane@gmail.com>2016-06-19 18:42:13 +0000
commit1b6c4e6e4a16ed49570e1787641e5af79dba23f7 (patch)
tree620245ae2a5ded115b26f79a6f422881a9340365 /plugins
parent6593f9613819dbc42ffff76a4d7a6d5156b46eae (diff)
MirLua: almost work m_json
git-svn-id: http://svn.miranda-ng.org/main/trunk@17013 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins')
-rw-r--r--plugins/MirLua/Modules/JSON/src/json.cpp33
-rw-r--r--plugins/MirLua/Modules/JSON/src/metatable.cpp140
-rw-r--r--plugins/MirLua/Modules/JSON/src/stdafx.h34
-rw-r--r--plugins/MirLua/Modules/m_json/m_json.vcxproj (renamed from plugins/MirLua/Modules/JSON/JSON.vcxproj)6
-rw-r--r--plugins/MirLua/Modules/m_json/src/main.cpp48
-rw-r--r--plugins/MirLua/Modules/m_json/src/metatable.cpp120
-rw-r--r--plugins/MirLua/Modules/m_json/src/stdafx.cxx (renamed from plugins/MirLua/Modules/JSON/src/stdafx.cxx)0
-rw-r--r--plugins/MirLua/Modules/m_json/src/stdafx.h37
8 files changed, 208 insertions, 210 deletions
diff --git a/plugins/MirLua/Modules/JSON/src/json.cpp b/plugins/MirLua/Modules/JSON/src/json.cpp
deleted file mode 100644
index c8680c753c..0000000000
--- a/plugins/MirLua/Modules/JSON/src/json.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
-#include "stdafx.h"
-
-static int lua_decode(lua_State *L)
-{
- const char *string = luaL_checkstring(L, 1);
- new (L) MT(json_parse(string));
- luaL_setmetatable(L, MT_JSON);
- return 1;
-}
-
-int json__call(lua_State *L);
-static int lua_encode(lua_State *L)
-{
- return json__call(L);
-}
-
-static const luaL_Reg methods[] =
-{
- { "decode", lua_decode },
- { "encode", lua_encode },
- { NULL, NULL }
-};
-
-LUA_LIBRARY_EXPORT(json)
-{
- luaL_newlib(L, methods);
-
- luaL_newmetatable(L, MT_JSON);
- luaL_setfuncs(L, jsonApi, 0);
- lua_pop(L, 1);
-
- return 1;
-} \ No newline at end of file
diff --git a/plugins/MirLua/Modules/JSON/src/metatable.cpp b/plugins/MirLua/Modules/JSON/src/metatable.cpp
deleted file mode 100644
index 7d66533aff..0000000000
--- a/plugins/MirLua/Modules/JSON/src/metatable.cpp
+++ /dev/null
@@ -1,140 +0,0 @@
-#include "stdafx.h"
-
-void table2json(lua_State *L, int idx, JSONNode &node)
-{
- idx = lua_absindex(L, idx);
- lua_pushnil(L);
- while (lua_next(L, idx) != 0)
- {
- JSONNode nnode;
- if (lua_type(L, -2) == LUA_TSTRING) nnode.set_name(lua_tostring(L, -2));
-
- switch (lua_type(L, -1))
- {
- case LUA_TSTRING:
- nnode = lua_tostring(L, -1);
- break;
- case LUA_TBOOLEAN:
- nnode = lua_toboolean(L, -1) != 0;
- break;
- case LUA_TNUMBER:
- nnode = lua_tonumber(L, -1);
- break;
- case LUA_TNIL:
- nnode.nullify();
- break;
- case LUA_TTABLE:
- table2json(L, -1, nnode);
- }
- node << nnode;
- lua_pop(L, 1);
- }
-}
-
-int json_pushvalue(lua_State *L, JSONNode &node)
-{
- switch (node.type())
- {
- case JSON_NULL:
- lua_pushnil(L);
- break;
- case JSON_STRING:
- lua_pushstring(L, node.as_string().c_str());
- break;
- case JSON_NUMBER:
- lua_pushnumber(L, node.as_int());
- break;
- case JSON_BOOL:
- lua_pushboolean(L, node.as_bool());
- break;
- case JSON_ARRAY:
- case JSON_NODE:
- new (L) MT(node);
- luaL_setmetatable(L, MT_JSON);
- }
- return 1;
-}
-
-int json__call(lua_State *L)
-{
- if (lua_istable(L, 1))
- {
- MT *mt = new (L) MT(json_new(JSON_NODE));
- table2json(L, 1, *mt->node);
- luaL_setmetatable(L, MT_JSON);
- return 1;
- }
- return 0;
-}
-
-static int json__index(lua_State *L)
-{
- JSONNode *node = ((MT*)luaL_checkudata(L, 1, MT_JSON))->node;
- switch (lua_type(L, 2))
- {
- case LUA_TNUMBER:
- return json_pushvalue(L, (*node)[(size_t)lua_tonumber(L, 2) - 1]);
- case LUA_TSTRING:
- return json_pushvalue(L, (*node)[lua_tostring(L, 2)]);
- }
- return 0;
-}
-
-static int json__newindex(lua_State *L)
-{
- JSONNode &node = *((MT*)luaL_checkudata(L, 1, MT_JSON))->node;
-
- JSONNode &jNode = lua_type(L, 2) == LUA_TNUMBER ? node[(size_t)lua_tonumber(L, 2) - 1] : node[lua_tostring(L, 2)];
-
- switch (lua_type(L, 3))
- {
- case LUA_TSTRING:
- jNode = lua_tostring(L, 3);
- break;
- case LUA_TBOOLEAN:
- jNode = lua_toboolean(L, 3) != 0;
- break;
- case LUA_TNUMBER:
- jNode = lua_tonumber(L, 3);
- break;
- case LUA_TNIL:
- jNode.nullify();
- break;
- case LUA_TTABLE:
- JSONNode tmpNode(JSON_NODE);
- tmpNode.set_name(lua_tostring(L, 2));
- table2json(L, 3, tmpNode);
- node[lua_tostring(L, 2)] = tmpNode;
- }
-
- return 0;
-}
-
-static int json__len(lua_State *L)
-{
- lua_pushnumber(L, ((MT*)luaL_checkudata(L, 1, MT_JSON))->node->size());
- return 1;
-}
-
-static int json__tostring(lua_State *L)
-{
- lua_pushstring(L, ((MT*)luaL_checkudata(L, 1, MT_JSON))->node->write().c_str());
- return 1;
-}
-
-static int json__gc(lua_State *L)
-{
- ((MT*)luaL_checkudata(L, 1, MT_JSON))->~MT();
- return 0;
-}
-
-static const struct luaL_Reg jsonApi[] =
-{
- { "__call", json__call },
- { "__index", json__index },
- { "__newindex", json__newindex },
- { "__len", json__len },
- { "__tostring", json__tostring },
- { "__gc", json__gc },
- { NULL, NULL }
-};
diff --git a/plugins/MirLua/Modules/JSON/src/stdafx.h b/plugins/MirLua/Modules/JSON/src/stdafx.h
deleted file mode 100644
index 98bbbbbeeb..0000000000
--- a/plugins/MirLua/Modules/JSON/src/stdafx.h
+++ /dev/null
@@ -1,34 +0,0 @@
-#pragma once
-
-#define LUA_LIBRARY_EXPORT(x) EXTERN_C int __declspec(dllexport) luaopen_##x(lua_State* L)
-
-
-#include <Windows.h>
-#include <lua.hpp>
-#include <m_system_cpp.h>
-//#include "..\..\..\src\mlua_metatable.h"
-
-#include <m_core.h>
-#include <m_json.h>
-#include <m_string.h>
-
-struct MT
-{
- JSONNode *node;
- bool bDelete;
-
- MT(JSONNode &refNode, bool bCopy = false) : node(bCopy ? json_copy(&refNode) : &refNode), bDelete(bCopy) {}
- MT(JSONNode *n, bool bD = true) : node(n), bDelete(bD) {}
- ~MT()
- {
- if (bDelete) json_delete(node);
- }
-
- __inline void* operator new(size_t size, lua_State *L)
- {
- return lua_newuserdata(L, size);
- }
-};
-
-extern const luaL_Reg jsonApi[];
-#define MT_JSON "JSON_METATABLE" \ No newline at end of file
diff --git a/plugins/MirLua/Modules/JSON/JSON.vcxproj b/plugins/MirLua/Modules/m_json/m_json.vcxproj
index 7cfa1391ad..a16b5b743e 100644
--- a/plugins/MirLua/Modules/JSON/JSON.vcxproj
+++ b/plugins/MirLua/Modules/m_json/m_json.vcxproj
@@ -19,10 +19,10 @@
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
- <ProjectName>json</ProjectName>
- <ProjectGuid>{8a645067-7f45-4e65-aafc-afda81c29a1d}</ProjectGuid>
+ <ProjectName>m_json</ProjectName>
+ <ProjectGuid>{99712BCA-F86F-4214-967D-1298DF0F4A13}</ProjectGuid>
</PropertyGroup>
<ImportGroup Label="PropertySheets">
<Import Project="$(ProjectDir)..\module.props" />
</ImportGroup>
-</Project>
+</Project> \ No newline at end of file
diff --git a/plugins/MirLua/Modules/m_json/src/main.cpp b/plugins/MirLua/Modules/m_json/src/main.cpp
new file mode 100644
index 0000000000..fe987d36f3
--- /dev/null
+++ b/plugins/MirLua/Modules/m_json/src/main.cpp
@@ -0,0 +1,48 @@
+#include "stdafx.h"
+
+static int lua_Decode(lua_State *L)
+{
+ const char *string = luaL_checkstring(L, 1);
+
+ JSONNode *node = json_parse(string);
+ new (L) JSON(node);
+ luaL_setmetatable(L, MT_JSON);
+
+ return 1;
+}
+
+static int lua_Encode(lua_State *L)
+{
+ luaL_checktype(L, 1, LUA_TTABLE);
+
+ JSONNode *node = json_new(JSON_NODE);
+ lua_pushnil(L);
+ while (lua_next(L, 1) != 0)
+ {
+ lua2json(L, *node);
+ lua_pop(L, 1);
+ }
+ JSON *mt = new (L) JSON(node);
+ luaL_setmetatable(L, MT_JSON);
+
+ return 1;
+}
+
+static const luaL_Reg methods[] =
+{
+ { "Dcode", lua_Decode },
+ { "Encode", lua_Encode },
+
+ { NULL, NULL }
+};
+
+extern "C" LUAMOD_API int luaopen_m_json(lua_State *L)
+{
+ luaL_newlib(L, methods);
+
+ luaL_newmetatable(L, MT_JSON);
+ luaL_setfuncs(L, jsonApi, 0);
+ lua_pop(L, 1);
+
+ return 1;
+} \ No newline at end of file
diff --git a/plugins/MirLua/Modules/m_json/src/metatable.cpp b/plugins/MirLua/Modules/m_json/src/metatable.cpp
new file mode 100644
index 0000000000..b7b1c37f24
--- /dev/null
+++ b/plugins/MirLua/Modules/m_json/src/metatable.cpp
@@ -0,0 +1,120 @@
+#include "stdafx.h"
+
+void lua2json(lua_State *L, JSONNode &node)
+{
+ const char *name = lua_tostring(L, -2);
+
+ switch (lua_type(L, -1))
+ {
+ case LUA_TNIL:
+ node.nullify();
+ break;
+ case LUA_TSTRING:
+ node = lua_tostring(L, -1);
+ break;
+ case LUA_TBOOLEAN:
+ node = lua_toboolean(L, -1) != 0;
+ break;
+ case LUA_TNUMBER:
+ node = lua_tonumber(L, -1);
+ break;
+ case LUA_TTABLE:
+ {
+ node.cast(JSON_NODE);
+ if (name)
+ node.set_name(name);
+
+ lua_pushnil(L);
+ while (lua_next(L, -2) != 0)
+ {
+ JSONNode child;
+ lua2json(L, child);
+ node << child;
+
+ lua_pop(L, 1);
+ }
+ }
+ }
+}
+
+static int json__index(lua_State *L)
+{
+ JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;
+
+ switch (node.type())
+ {
+ case JSON_NULL:
+ lua_pushnil(L);
+ break;
+ case JSON_STRING:
+ lua_pushstring(L, node.as_string().c_str());
+ break;
+ case JSON_NUMBER:
+ lua_pushnumber(L, node.as_int());
+ break;
+ case JSON_BOOL:
+ lua_pushboolean(L, node.as_bool());
+ break;
+ case JSON_NODE:
+ case JSON_ARRAY:
+ new (L) JSONNode(node);
+ luaL_setmetatable(L, MT_JSON);
+ }
+
+ return 1;
+}
+
+static int json__newindex(lua_State *L)
+{
+ JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;
+ const char *key = lua_tostring(L, 2);
+
+ JSONNode child = node[key];
+ if (child.isnull())
+ {
+ child.set_name(key);
+ node << child;
+ }
+ lua2json(L, child);
+ node[key] = child;
+
+ return 0;
+}
+
+static int json__len(lua_State *L)
+{
+ JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;
+
+ lua_pushnumber(L, node.size());
+
+ return 1;
+}
+
+static int json__tostring(lua_State *L)
+{
+ JSONNode &node = *((JSON*)luaL_checkudata(L, 1, MT_JSON))->node;
+
+ lua_pushstring(L, node.write().c_str());
+
+ return 1;
+}
+
+static int json__gc(lua_State *L)
+{
+ JSON *json = (JSON*)luaL_checkudata(L, 1, MT_JSON);
+
+ delete json;
+
+ return 0;
+}
+
+static const struct luaL_Reg jsonApi[] =
+{
+ { "__index", json__index },
+ { "__newindex", json__newindex },
+ { "__len", json__len },
+ { "__tostring", json__tostring },
+ { "__gc", json__gc },
+
+ { NULL, NULL }
+};
diff --git a/plugins/MirLua/Modules/JSON/src/stdafx.cxx b/plugins/MirLua/Modules/m_json/src/stdafx.cxx
index 1577c4e3bc..1577c4e3bc 100644
--- a/plugins/MirLua/Modules/JSON/src/stdafx.cxx
+++ b/plugins/MirLua/Modules/m_json/src/stdafx.cxx
diff --git a/plugins/MirLua/Modules/m_json/src/stdafx.h b/plugins/MirLua/Modules/m_json/src/stdafx.h
new file mode 100644
index 0000000000..d8ecdcacb5
--- /dev/null
+++ b/plugins/MirLua/Modules/m_json/src/stdafx.h
@@ -0,0 +1,37 @@
+#pragma once
+
+#include <Windows.h>
+#include <lua.hpp>
+#include <m_system_cpp.h>
+//#include "..\..\..\src\mlua_metatable.h"
+
+#include <m_core.h>
+#include <m_json.h>
+#include <m_string.h>
+
+struct JSON
+{
+ JSONNode *node;
+ bool bDelete;
+
+ JSON(JSONNode &refNode, bool bCopy = false)
+ : node(bCopy ? json_copy(&refNode) : &refNode), bDelete(bCopy) { }
+ JSON(JSONNode *n, bool bD = true)
+ : node(n), bDelete(bD) { }
+ ~JSON()
+ {
+ if (bDelete)
+ json_delete(node);
+ }
+
+ __inline void* operator new(size_t size, lua_State *L)
+ {
+ return lua_newuserdata(L, size);
+ }
+};
+
+void lua2json(lua_State *L, JSONNode &node);
+
+extern const luaL_Reg jsonApi[];
+
+#define MT_JSON "JSON" \ No newline at end of file