summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2017-09-13 00:54:02 +0300
committeraunsane <aunsane@gmail.com>2017-09-13 00:54:24 +0300
commit2b8358694bdbe12eb2506e41d9dcfe8c6e13ba69 (patch)
treea33c81a1b7e05951ceb8103485ad2204e13470f0 /plugins
parent8b5a61afd06177b7774dee8c03f18f01188a3117 (diff)
MirLua: m_enumerale: chenged syntax
Diffstat (limited to 'plugins')
-rw-r--r--plugins/MirLua/Modules/m_enumerable/src/main.cpp323
1 files changed, 159 insertions, 164 deletions
diff --git a/plugins/MirLua/Modules/m_enumerable/src/main.cpp b/plugins/MirLua/Modules/m_enumerable/src/main.cpp
index 8494c1385a..f12b8fcc0c 100644
--- a/plugins/MirLua/Modules/m_enumerable/src/main.cpp
+++ b/plugins/MirLua/Modules/m_enumerable/src/main.cpp
@@ -1,106 +1,52 @@
#include "stdafx.h"
-#define MT_ENUMERABLE "ENUMERABLE"
-
-/***********************************************/
-
-static int lua_WhereIterator(lua_State *L)
+static int lua__call(lua_State *L)
{
- int enumeratorRef = lua_tointeger(L, lua_upvalueindex(1));
- int whereRef = lua_tointeger(L, lua_upvalueindex(2));
+ int enumeratorRef = *(int*)lua_touserdata(L, 1);
int top = lua_gettop(L);
- while (true)
- {
- lua_rawgeti(L, LUA_REGISTRYINDEX, enumeratorRef);
- lua_call(L, 0, LUA_MULTRET);
- int nres = lua_gettop(L) - top;
- if (lua_isnoneornil(L, top + 1))
- {
- lua_pop(L, nres);
- break;
- }
-
- lua_rawgeti(L, LUA_REGISTRYINDEX, whereRef);
- for (int i = top; i < top + nres; i++)
- lua_pushvalue(L, i + 1);
- lua_call(L, nres, 1);
- if (lua_toboolean(L, -1))
- {
- lua_pop(L, 1);
- return nres;
- }
- lua_pop(L, nres + 1);
- }
-
- lua_pushnil(L);
+ lua_rawgeti(L, LUA_REGISTRYINDEX, enumeratorRef);
+ lua_call(L, 0, LUA_MULTRET);
+ int nres = lua_gettop(L) - top;
- return 1;
+ return nres;
}
-static int lua_Where(lua_State *L)
+static int lua__pairs(lua_State *L)
{
- int enumeratorRef = *(int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
- luaL_checktype(L, 2, LUA_TFUNCTION);
+ int enumeratorRef = *(int*)lua_touserdata(L, 1);
+
+ lua_rawgeti(L, LUA_REGISTRYINDEX, enumeratorRef);
- lua_pushinteger(L, enumeratorRef);
- lua_pushvalue(L, 2);
- lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
- lua_pushcclosure(L, lua_WhereIterator, 2);
- enumeratorRef = luaL_ref(L, LUA_REGISTRYINDEX);
- int *udata = (int*)lua_newuserdata(L, sizeof(int));
- *udata = enumeratorRef;
- luaL_setmetatable(L, MT_ENUMERABLE);
-
return 1;
}
-static int lua_SelectIterator(lua_State *L)
+static int lua__gc(lua_State *L)
{
- int enumeratorRef = lua_tointeger(L, lua_upvalueindex(1));
- int selectRef = lua_tointeger(L, lua_upvalueindex(2));
+ int enumeratorRef = *(int*)lua_touserdata(L, 1);
- int top = lua_gettop(L);
- lua_rawgeti(L, LUA_REGISTRYINDEX, enumeratorRef);
- lua_call(L, 0, LUA_MULTRET);
- int nres = lua_gettop(L) - top;
- if (lua_isnoneornil(L, top + 1))
- {
- lua_pop(L, nres);
- return 0;
- }
-
- lua_rawgeti(L, LUA_REGISTRYINDEX, selectRef);
- lua_insert(L, top + 1);
- lua_call(L, nres, LUA_MULTRET);
- nres = lua_gettop(L) - top;
+ luaL_unref(L, LUA_REGISTRYINDEX, enumeratorRef);
- return nres;
+ return 0;
}
-static int lua_Select(lua_State *L)
+static luaL_Reg enumerableMeta[] =
{
- int enumeratorRef = *(int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
- luaL_checktype(L, 2, LUA_TFUNCTION);
+ { "__call", lua__call },
+ { "__pairs", lua__pairs },
+ { "__gc", lua__gc },
- lua_pushinteger(L, enumeratorRef);
- lua_pushvalue(L, 2);
- lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
- lua_pushcclosure(L, lua_SelectIterator, 2);
- enumeratorRef = luaL_ref(L, LUA_REGISTRYINDEX);
- int *udata = (int*)lua_newuserdata(L, sizeof(int));
- *udata = enumeratorRef;
- luaL_setmetatable(L, MT_ENUMERABLE);
+ { NULL, NULL }
+};
- return 1;
-}
+/***********************************************/
static int lua_Any(lua_State *L)
{
- int enumeratorRef = *(int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
- bool hasFunc = lua_gettop(L) >= 2;
- if(hasFunc)
- luaL_checktype(L, 2, LUA_TFUNCTION);
+ int enumeratorRef = *(int*)lua_touserdata(L, lua_upvalueindex(1));
+
+ if (lua_type(L, 1) > LUA_TNONE)
+ luaL_checktype(L, 1, LUA_TFUNCTION);
int top = lua_gettop(L);
while (true)
@@ -114,9 +60,9 @@ static int lua_Any(lua_State *L)
break;
}
- if (hasFunc)
+ if (lua_isfunction(L, 1))
{
- lua_pushvalue(L, 2);
+ lua_pushvalue(L, 1);
lua_insert(L, top + 1);
lua_call(L, nres, 1);
nres = lua_gettop(L) - top;
@@ -143,10 +89,10 @@ static int lua_Any(lua_State *L)
static int lua_First(lua_State *L)
{
- int enumeratorRef = *(int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
- bool hasFunc = lua_gettop(L) >= 2;
- if (hasFunc)
- luaL_checktype(L, 2, LUA_TFUNCTION);
+ int enumeratorRef = *(int*)lua_touserdata(L, lua_upvalueindex(1));
+
+ if (lua_type(L, 1) > LUA_TNONE)
+ luaL_checktype(L, 1, LUA_TFUNCTION);
int top = lua_gettop(L);
while (true)
@@ -160,9 +106,9 @@ static int lua_First(lua_State *L)
break;
}
- if (hasFunc)
+ if (lua_isfunction(L, 1))
{
- lua_pushvalue(L, 2);
+ lua_pushvalue(L, 1);
for (int i = top; i < top + nres; i++)
lua_pushvalue(L, i + 1);
lua_call(L, nres, 1);
@@ -185,10 +131,10 @@ static int lua_First(lua_State *L)
static int lua_Last(lua_State *L)
{
- int enumeratorRef = *(int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
- bool hasFunc = lua_gettop(L) >= 2;
- if (hasFunc)
- luaL_checktype(L, 2, LUA_TFUNCTION);
+ int enumeratorRef = *(int*)lua_touserdata(L, lua_upvalueindex(1));
+
+ if (lua_type(L, 1) > LUA_TNONE)
+ luaL_checktype(L, 1, LUA_TFUNCTION);
int top = lua_gettop(L);
lua_pushnil(L);
@@ -204,9 +150,9 @@ static int lua_Last(lua_State *L)
break;
}
- if (hasFunc)
+ if (lua_isfunction(L, 1))
{
- lua_pushvalue(L, 2);
+ lua_pushvalue(L, 1);
for (int i = top + nres; i < top + nres + newres; i++)
lua_pushvalue(L, i + 1);
lua_call(L, newres, 1);
@@ -228,7 +174,7 @@ static int lua_Last(lua_State *L)
static int lua_Count(lua_State *L)
{
- int enumeratorRef = *(int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
+ int enumeratorRef = *(int*)lua_touserdata(L, lua_upvalueindex(1));
int count = 0;
while (true)
@@ -252,7 +198,7 @@ static int lua_Count(lua_State *L)
static int lua_ToArray(lua_State *L)
{
- int enumeratorRef = *(int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
+ int enumeratorRef = *(int*)lua_touserdata(L, lua_upvalueindex(1));
lua_newtable(L);
for (int i = 1;; i++)
@@ -275,11 +221,11 @@ static int lua_ToArray(lua_State *L)
static int lua_ToTable(lua_State *L)
{
- int enumeratorRef = *(int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
- luaL_checktype(L, 2, LUA_TFUNCTION);
- bool hasValueSelectorFunc = lua_gettop(L) >= 3;
- if (hasValueSelectorFunc)
- luaL_checktype(L, 3, LUA_TFUNCTION);
+ int enumeratorRef = *(int*)lua_touserdata(L, lua_upvalueindex(1));
+ luaL_checktype(L, 1, LUA_TFUNCTION);
+
+ if (lua_type(L, 2) > LUA_TNONE)
+ luaL_checktype(L, 2, LUA_TFUNCTION);
lua_newtable(L);
int top = lua_gettop(L);
@@ -294,14 +240,14 @@ static int lua_ToTable(lua_State *L)
break;
}
- lua_pushvalue(L, 2);
+ lua_pushvalue(L, 1);
for (int i = top; i < top + nres; i++)
lua_pushvalue(L, i + 1);
lua_call(L, nres, 1);
- if (hasValueSelectorFunc)
+ if (lua_isfunction(L, 2))
{
- lua_pushvalue(L, 3);
+ lua_pushvalue(L, 2);
for (int i = top; i < top + nres; i++)
lua_pushvalue(L, i + 1);
lua_call(L, nres, 1);
@@ -316,35 +262,130 @@ static int lua_ToTable(lua_State *L)
return 1;
}
+static int lua_Where(lua_State *L);
+static int lua_Select(lua_State *L);
+
+static luaL_Reg enumerableApi[] =
+{
+ { "Where", lua_Where },
+ { "Select", lua_Select },
+
+ { "Any", lua_Any },
+ { "First", lua_First },
+ { "Last", lua_Last },
+ { "Count", lua_Count },
+
+ { "ToArray", lua_ToArray },
+ { "ToTable", lua_ToTable },
+
+ { NULL, NULL }
+};
+
/***********************************************/
-static int lua__call(lua_State *L)
+static void CreateMetatable(lua_State *L)
{
- int *enumeratorRef = (int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
+ lua_newtable(L);
+ lua_pushvalue(L, -1);
+ lua_pushvalue(L, -3);
+ luaL_setfuncs(L, enumerableApi, 1);
+ lua_setfield(L, -2, "__index");
+ luaL_setfuncs(L, enumerableMeta, 0);
+}
+
+static int lua_WhereIterator(lua_State *L)
+{
+ int enumeratorRef = lua_tointeger(L, lua_upvalueindex(1));
+ int whereRef = lua_tointeger(L, lua_upvalueindex(2));
int top = lua_gettop(L);
- lua_rawgeti(L, LUA_REGISTRYINDEX, *enumeratorRef);
- lua_call(L, 0, LUA_MULTRET);
- int nres = lua_gettop(L) - top;
+ while (true)
+ {
+ lua_rawgeti(L, LUA_REGISTRYINDEX, enumeratorRef);
+ lua_call(L, 0, LUA_MULTRET);
+ int nres = lua_gettop(L) - top;
+ if (lua_isnoneornil(L, top + 1))
+ {
+ lua_pop(L, nres);
+ luaL_unref(L, LUA_REGISTRYINDEX, whereRef);
+ break;
+ }
- return nres;
+ lua_rawgeti(L, LUA_REGISTRYINDEX, whereRef);
+ for (int i = top; i < top + nres; i++)
+ lua_pushvalue(L, i + 1);
+ lua_call(L, nres, 1);
+ if (lua_toboolean(L, -1))
+ {
+ lua_pop(L, 1);
+ return nres;
+ }
+ lua_pop(L, nres + 1);
+ }
+
+ lua_pushnil(L);
+
+ return 1;
}
-static int lua__pairs(lua_State *L)
+static int lua_Where(lua_State *L)
{
- int *enumeratorRef = (int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
+ int enumeratorRef = *(int*)lua_touserdata(L, lua_upvalueindex(1));
+ luaL_checktype(L, 1, LUA_TFUNCTION);
- lua_rawgeti(L, LUA_REGISTRYINDEX, *enumeratorRef);
+ lua_pushinteger(L, enumeratorRef);
+ lua_pushvalue(L, 1);
+ lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
+ lua_pushcclosure(L, lua_WhereIterator, 2);
+ enumeratorRef = luaL_ref(L, LUA_REGISTRYINDEX);
+ int *udata = (int*)lua_newuserdata(L, sizeof(int));
+ *udata = enumeratorRef;
+ CreateMetatable(L);
+ lua_setmetatable(L, -2);
return 1;
}
-static int lua__gc(lua_State *L)
+static int lua_SelectIterator(lua_State *L)
{
- int *enumeratorRef = (int*)luaL_checkudata(L, 1, MT_ENUMERABLE);
- luaL_unref(L, LUA_REGISTRYINDEX, *enumeratorRef);
+ int enumeratorRef = lua_tointeger(L, lua_upvalueindex(1));
+ int selectRef = lua_tointeger(L, lua_upvalueindex(2));
- return 0;
+ int top = lua_gettop(L);
+ lua_rawgeti(L, LUA_REGISTRYINDEX, enumeratorRef);
+ lua_call(L, 0, LUA_MULTRET);
+ int nres = lua_gettop(L) - top;
+ if (lua_isnoneornil(L, top + 1))
+ {
+ lua_pop(L, nres);
+ luaL_unref(L, LUA_REGISTRYINDEX, selectRef);
+ return 0;
+ }
+
+ lua_rawgeti(L, LUA_REGISTRYINDEX, selectRef);
+ lua_insert(L, top + 1);
+ lua_call(L, nres, LUA_MULTRET);
+ nres = lua_gettop(L) - top;
+
+ return nres;
+}
+
+static int lua_Select(lua_State *L)
+{
+ int enumeratorRef = *(int*)lua_touserdata(L, lua_upvalueindex(1));
+ luaL_checktype(L, 1, LUA_TFUNCTION);
+
+ lua_pushinteger(L, enumeratorRef);
+ lua_pushvalue(L, 1);
+ lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
+ lua_pushcclosure(L, lua_SelectIterator, 2);
+ enumeratorRef = luaL_ref(L, LUA_REGISTRYINDEX);
+ int *udata = (int*)lua_newuserdata(L, sizeof(int));
+ *udata = enumeratorRef;
+ CreateMetatable(L);
+ lua_setmetatable(L, -2);
+
+ return 1;
}
/***********************************************/
@@ -381,67 +422,21 @@ static int lua__new(lua_State *L)
}
int enumeratorRef = luaL_ref(L, LUA_REGISTRYINDEX);
-
int *udata = (int*)lua_newuserdata(L, sizeof(int));
*udata = enumeratorRef;
- luaL_setmetatable(L, MT_ENUMERABLE);
+ CreateMetatable(L);
+ lua_setmetatable(L, -2);
return 1;
}
/***********************************************/
-static luaL_Reg enumerableApi[] =
-{
- { "Where", lua_Where },
- { "Select", lua_Select },
-
- { "Any", lua_Any },
- { "First", lua_First },
- { "Last", lua_Last },
- { "Count", lua_Count },
-
- { "ToArray", lua_ToArray },
- { "ToTable", lua_ToTable },
-
- { NULL, NULL }
-};
-
-static luaL_Reg enumerableMeta[] =
-{
- { "__call", lua__call },
- { "__pairs", lua__pairs },
- { "__gc", lua__gc },
-
- { NULL, NULL }
-};
-
-static luaL_Reg methods[] =
-{
- { "new", lua__new },
-
- { NULL, NULL }
-};
-
-/***********************************************/
-
extern "C" LUAMOD_API int luaopen_m_enumerable(lua_State *L)
{
- luaL_newlib(L, methods);
-
- luaL_newmetatable(L, MT_ENUMERABLE);
- luaL_setfuncs(L, enumerableMeta, 0);
- lua_pushvalue(L, -1);
- lua_setfield(L, -2, "__index");
- luaL_setfuncs(L, enumerableApi, 0);
- lua_pop(L, 1);
-
lua_createtable(L, 0, 1);
lua_pushcfunction(L, lua__new);
lua_setfield(L, -2, "new");
- lua_pushvalue(L, -1);
- lua_setglobal(L, MT_ENUMERABLE);
- lua_pop(L, 1);
return 1;
}