Working on diff xmission

This commit is contained in:
2021-08-23 23:34:30 -04:00
parent 7581ac7278
commit 2be6c2c58e
10 changed files with 702 additions and 149 deletions

View File

@@ -1,5 +1,6 @@
#include "luastack.hpp"
#include <iostream>
#include <cassert>
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
LuaNilMarker LuaNil;
@@ -234,7 +235,7 @@ void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
}
// Repair the special fields.
LS.settabletype(classtab, TAB_CLASS);
LS.settabletype(classtab, LUA_TT_CLASS);
LS.rawset(classtab, "__index", classtab);
LS.rawset(classtab, "__class", classname);
@@ -242,23 +243,48 @@ void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
LS.result();
}
void LuaStack::movesortablekey(LuaSlot key, lua_State *L) {
void LuaStack::makeclass(LuaSlot tab, const char *name) {
push_any_value(name);
LuaSpecial classname(lua_gettop(L_));
makeclass(tab, classname);
lua_pop(L_, 1);
}
std::string LuaStack::classname(LuaSlot tab) {
std::string result;
if (istable(tab)) {
lua_pushstring(L_, "__class");
lua_rawget(L_, tab);
if (lua_type(L_, -1) == LUA_TSTRING) {
size_t len;
const char *s = lua_tolstring(L_, -1, &len);
result = std::string(s, len);
}
lua_pop(L_, 1);
}
return result;
}
void LuaStack::movesortablekey(LuaSlot key, LuaStack &otherstack, LuaSlot otherslot) {
int type = lua_type(L_, key);
switch (type) {
case LUA_TBOOLEAN:
lua_pushboolean(L, lua_toboolean(L_, key));
lua_pushboolean(otherstack.L_, lua_toboolean(L_, key));
lua_replace(otherstack.L_, otherslot);
break;
case LUA_TNUMBER:
lua_pushnumber(L, lua_tonumber(L_, key));
lua_pushnumber(otherstack.L_, lua_tonumber(L_, key));
lua_replace(otherstack.L_, otherslot);
break;
case LUA_TSTRING: {
size_t len;
const char *str = lua_tolstring(L_, key, &len);
lua_pushlstring(L, str, len);
lua_pushlstring(otherstack.L_, str, len);
lua_replace(otherstack.L_, otherslot);
break;
}
default:
luaL_error(L, "movesortablekey: not a sortable key");
luaL_error(L_, "movesortablekey: not a sortable key");
}
}
@@ -297,20 +323,29 @@ void LuaStack::check_nret(int xnret, int otop, int nret) const {
}
}
LuaStack::TableType LuaStack::gettabletype(LuaSlot tab) {
int LuaStack::gettabletype(LuaSlot tab) const {
uint16_t bits = lua_getflagbits(L_, tab.index());
return TableType(bits & 0x000F);
return LUA_TT_GENERAL + (bits & 0x000F);
}
void LuaStack::settabletype(LuaSlot tab, TableType t) {
lua_modflagbits(L_, tab.index(), 0x000F, t);
void LuaStack::settabletype(LuaSlot tab, int t) const {
assert((t >= LUA_TT_GENERAL) && (t <= LUA_TT_CLASS));
int offset = (t - LUA_TT_GENERAL);
lua_modflagbits(L_, tab.index(), 0x000F, offset);
}
bool LuaStack::getvisited(LuaSlot tab) {
int LuaStack::xtype(LuaSlot slot) const {
int t = lua_type(L_, slot);
if (t != LUA_TTABLE) return t;
uint16_t bits = lua_getflagbits(L_, slot);
return LUA_TT_GENERAL + (bits & 0x000F);
}
bool LuaStack::getvisited(LuaSlot tab) const {
uint16_t bits = lua_getflagbits(L_, tab.index());
return (bits & 0x0010);
}
void LuaStack::setvisited(LuaSlot tab, bool visited) {
void LuaStack::setvisited(LuaSlot tab, bool visited) const {
lua_modflagbits(L_, tab.index(), 0x0010, visited ? 0x0010 : 0);
}