2020-11-27 13:21:07 -05:00
|
|
|
#include "table.hpp"
|
2020-12-05 18:57:53 -05:00
|
|
|
#include "source.hpp"
|
2020-11-27 13:21:07 -05:00
|
|
|
|
2021-01-02 13:31:18 -05:00
|
|
|
LuaDefine(table_equal, "c") {
|
|
|
|
|
LuaArg t1, t2;
|
|
|
|
|
LuaRet eql;
|
|
|
|
|
LuaStack LS(L, t1, t2, eql);
|
|
|
|
|
LS.checktable(t1);
|
|
|
|
|
LS.checktable(t2);
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
int total1 = 0;
|
|
|
|
|
while (lua_next(L, t1.index()) != 0) {
|
|
|
|
|
lua_pushvalue(L, -2); // k v1 k
|
|
|
|
|
lua_rawget(L, t2.index()); // k v1 v2
|
2021-02-28 14:45:09 -05:00
|
|
|
if (!lua_rawequal(L, -1, -2)) {
|
2021-01-02 13:31:18 -05:00
|
|
|
LS.set(eql, false);
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 2);
|
|
|
|
|
total1 += 1;
|
|
|
|
|
}
|
|
|
|
|
int total2 = 0;
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
while (lua_next(L, t2.index()) != 0) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
total2 += 1;
|
|
|
|
|
}
|
|
|
|
|
LS.set(eql, total1 == total2);
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(table_findremove, "c") {
|
|
|
|
|
luaL_checktype(L, -2, LUA_TTABLE);
|
|
|
|
|
int src = 1;
|
|
|
|
|
int dst = 1;
|
|
|
|
|
while (true) {
|
|
|
|
|
lua_pushinteger(L, src);
|
|
|
|
|
lua_rawget(L, -3);
|
2021-02-28 14:45:09 -05:00
|
|
|
if (lua_rawequal(L, -1, -2)) {
|
2021-01-02 13:31:18 -05:00
|
|
|
src++;
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
} else if (lua_isnil(L, -1)) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
int removed = src - dst;
|
|
|
|
|
while (src > dst) {
|
|
|
|
|
lua_pushinteger(L, dst);
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
lua_rawset(L, -4);
|
|
|
|
|
dst++;
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 2);
|
|
|
|
|
lua_pushinteger(L, removed);
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
if (src > dst) {
|
|
|
|
|
lua_pushinteger(L, dst);
|
|
|
|
|
lua_insert(L, lua_gettop(L) - 1);
|
|
|
|
|
lua_rawset(L, -4);
|
|
|
|
|
} else {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
}
|
|
|
|
|
src++;
|
|
|
|
|
dst++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 14:14:38 -05:00
|
|
|
|
|
|
|
|
LuaDefine(table_push, "c") {
|
|
|
|
|
luaL_checktype(L, -2, LUA_TTABLE);
|
2021-02-28 14:45:09 -05:00
|
|
|
int len = lua_rawlen(L, -2);
|
2021-01-12 14:14:38 -05:00
|
|
|
lua_pushinteger(L, len+1);
|
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
|
lua_rawset(L, -4);
|
|
|
|
|
lua_pop(L, 2);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 13:31:18 -05:00
|
|
|
LuaDefine(table_find, "c") {
|
|
|
|
|
luaL_checktype(L, -2, LUA_TTABLE);
|
|
|
|
|
for (int i = 1; ; i++) {
|
|
|
|
|
lua_pushinteger(L, i);
|
|
|
|
|
lua_rawget(L, -3);
|
2021-02-28 14:45:09 -05:00
|
|
|
if (lua_rawequal(L, -1, -2)) {
|
2021-01-02 13:31:18 -05:00
|
|
|
lua_pop(L, 3);
|
|
|
|
|
lua_pushinteger(L, i);
|
|
|
|
|
return 1;
|
|
|
|
|
} else if (lua_isnil(L, -1)) {
|
|
|
|
|
lua_pop(L, 3);
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(table_empty, "c") {
|
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
if (lua_next(L, -2) != 0) {
|
|
|
|
|
lua_pop(L, 3);
|
|
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(table_count, "c") {
|
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
lua_Integer total = 0;
|
|
|
|
|
while (lua_next(L, -2) != 0) {
|
|
|
|
|
total += 1;
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_pushinteger(L, total);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(table_clear, "c") {
|
2020-11-27 13:21:07 -05:00
|
|
|
LuaArg tab;
|
|
|
|
|
LuaStack LS(L, tab);
|
2021-03-19 13:41:41 -04:00
|
|
|
LS.cleartable(tab);
|
2020-11-27 13:21:07 -05:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 13:31:18 -05:00
|
|
|
LuaDefine(queue_create, "c") {
|
|
|
|
|
LuaRet queue;
|
|
|
|
|
LuaStack LS(L, queue);
|
|
|
|
|
|
|
|
|
|
LS.newtable(queue);
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawset(queue, "head", 1000000);
|
|
|
|
|
LS.rawset(queue, "tail", 1000000);
|
2021-01-02 13:31:18 -05:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(queue_push, "c") {
|
|
|
|
|
LuaArg queue, elt;
|
|
|
|
|
lua_Integer head;
|
|
|
|
|
LuaStack LS(L, queue, elt);
|
|
|
|
|
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawget(head, queue, "head");
|
2021-01-02 13:31:18 -05:00
|
|
|
LS.rawset(queue, head, elt);
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawset(queue, "head", head+1);
|
2021-01-02 13:31:18 -05:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(queue_pop, "c") {
|
|
|
|
|
LuaArg queue;
|
|
|
|
|
LuaRet elt;
|
|
|
|
|
lua_Integer head, tail;
|
|
|
|
|
LuaStack LS(L, queue, elt);
|
|
|
|
|
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawget(tail, queue, "tail");
|
|
|
|
|
LS.rawget(head, queue, "head");
|
2021-01-02 13:31:18 -05:00
|
|
|
if (head == tail) {
|
|
|
|
|
LS.set(elt, LuaNil);
|
|
|
|
|
} else {
|
|
|
|
|
LS.rawget(elt, queue, tail);
|
|
|
|
|
LS.rawset(queue, tail, LuaNil);
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawset(queue, "tail", tail + 1);
|
2021-01-02 13:31:18 -05:00
|
|
|
}
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(queue_size, "c") {
|
|
|
|
|
LuaArg queue;
|
|
|
|
|
LuaRet size;
|
|
|
|
|
lua_Number head, tail;
|
|
|
|
|
LuaStack LS(L, queue, size);
|
|
|
|
|
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawget(head, queue, "head");
|
|
|
|
|
LS.rawget(tail, queue, "tail");
|
2021-01-02 13:31:18 -05:00
|
|
|
LS.set(size, head - tail);
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(queue_nth, "c") {
|
|
|
|
|
LuaArg queue, n;
|
|
|
|
|
LuaRet elt;
|
|
|
|
|
lua_Integer nth, head, tail;
|
|
|
|
|
LuaStack LS(L, queue, n, elt);
|
|
|
|
|
|
|
|
|
|
nth = LS.ckinteger(n) - 1;
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawget(head, queue, "head");
|
|
|
|
|
LS.rawget(tail, queue, "tail");
|
2021-01-02 13:31:18 -05:00
|
|
|
if ((nth < 0) || (nth + tail >= head)) {
|
|
|
|
|
luaL_error(L, "index out of range");
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(elt, queue, tail + nth);
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(table_getregistry, "f") {
|
|
|
|
|
LuaArg key;
|
|
|
|
|
LuaRet result;
|
|
|
|
|
LuaStack LS(L, key, result);
|
|
|
|
|
LS.rawget(result, LuaRegistry, key);
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|