refactor: LuaState replaced with lua_engine

This commit is contained in:
MihailRis
2024-06-11 13:18:30 +03:00
parent 0647bc6f90
commit 90bc86408b
22 changed files with 643 additions and 675 deletions
+38 -38
View File
@@ -17,29 +17,29 @@ int lua::pushvalue(lua_State* L, const dynamic::Value& value) {
using namespace dynamic;
if (auto* flag = std::get_if<bool>(&value)) {
lua_pushboolean(L, *flag);
pushboolean(L, *flag);
} else if (auto* num = std::get_if<integer_t>(&value)) {
lua_pushinteger(L, *num);
pushinteger(L, *num);
} else if (auto* num = std::get_if<number_t>(&value)) {
lua_pushnumber(L, *num);
pushnumber(L, *num);
} else if (auto* str = std::get_if<std::string>(&value)) {
lua_pushstring(L, str->c_str());
pushstring(L, *str);
} else if (auto listptr = std::get_if<List_sptr>(&value)) {
auto list = *listptr;
lua_createtable(L, list->size(), 0);
createtable(L, list->size(), 0);
for (size_t i = 0; i < list->size(); i++) {
pushvalue(L, list->get(i));
lua_rawseti(L, -2, i+1);
rawseti(L, i+1);
}
} else if (auto mapptr = std::get_if<Map_sptr>(&value)) {
auto map = *mapptr;
lua_createtable(L, 0, map->size());
createtable(L, 0, map->size());
for (auto& entry : map->values) {
pushvalue(L, entry.second);
lua_setfield(L, -2, entry.first.c_str());
setfield(L, entry.first);
}
} else {
lua_pushnil(L);
pushnil(L);
}
return 1;
}
@@ -54,47 +54,47 @@ int lua::pushwstring(lua_State* L, const std::wstring& str) {
dynamic::Value lua::tovalue(lua_State* L, int idx) {
using namespace dynamic;
auto type = lua_type(L, idx);
auto type = lua::type(L, idx);
switch (type) {
case LUA_TNIL:
case LUA_TNONE:
return dynamic::NONE;
case LUA_TBOOLEAN:
return lua_toboolean(L, idx) == 1;
return toboolean(L, idx) == 1;
case LUA_TNUMBER: {
auto number = lua_tonumber(L, idx);
auto integer = lua_tointeger(L, idx);
if (number == (lua_Number)integer) {
auto number = tonumber(L, idx);
auto integer = tointeger(L, idx);
if (number == static_cast<lua_Number>(integer)) {
return integer;
} else {
return number;
}
}
case LUA_TSTRING:
return std::string(lua_tostring(L, idx));
return std::string(tostring(L, idx));
case LUA_TTABLE: {
int len = lua_objlen(L, idx);
int len = lua::objlen(L, idx);
if (len) {
// array
auto list = create_list();
for (int i = 1; i <= len; i++) {
lua_rawgeti(L, idx, i);
rawgeti(L, i, idx);
list->put(tovalue(L, -1));
lua_pop(L, 1);
pop(L);
}
return list;
} else {
// table
auto map = create_map();
lua_pushvalue(L, idx);
lua_pushnil(L);
pushvalue(L, idx);
pushnil(L);
while (lua_next(L, -2)) {
lua_pushvalue(L, -2);
auto key = lua_tostring(L, -1);
pushvalue(L, -2);
auto key = tostring(L, -1);
map->put(key, tovalue(L, -2));
lua_pop(L, 2);
pop(L, 2);
}
lua_pop(L, 1);
pop(L);
return map;
}
}
@@ -107,38 +107,38 @@ dynamic::Value lua::tovalue(lua_State* L, int idx) {
int lua::call(lua_State* L, int argc, int nresults) {
if (lua_pcall(L, argc, nresults, 0)) {
throw luaerror(lua_tostring(L, -1));
throw luaerror(tostring(L, -1));
}
return 1;
}
int lua::call_nothrow(lua_State* L, int argc) {
if (lua_pcall(L, argc, LUA_MULTRET, 0)) {
log_error(lua_tostring(L, -1));
log_error(tostring(L, -1));
return 0;
}
return 1;
}
void lua::dump_stack(lua_State* L) {
int top = lua_gettop(L);
int top = gettop(L);
for (int i = 1; i <= top; i++) {
std::cout << std::setw(3) << i << std::setw(20) << luaL_typename(L, i) << std::setw(30);
switch (lua_type(L, i)) {
switch (lua::type(L, i)) {
case LUA_TNUMBER:
std::cout << lua_tonumber(L, i);
std::cout << tonumber(L, i);
break;
case LUA_TSTRING:
std::cout << lua_tostring(L, i);
std::cout << tostring(L, i);
break;
case LUA_TBOOLEAN:
std::cout << (lua_toboolean(L, i) ? "true" : "false");
std::cout << (toboolean(L, i) ? "true" : "false");
break;
case LUA_TNIL:
std::cout << "nil";
break;
default:
std::cout << lua_topointer(L, i);
std::cout << topointer(L, i);
break;
}
std::cout << std::endl;
@@ -146,7 +146,7 @@ void lua::dump_stack(lua_State* L) {
}
static std::shared_ptr<std::string> createLambdaHandler(lua_State* L) {
auto ptr = reinterpret_cast<ptrdiff_t>(lua_topointer(L, -1));
auto ptr = reinterpret_cast<ptrdiff_t>(topointer(L, -1));
auto name = util::mangleid(ptr);
getglobal(L, LAMBDAS_TABLE);
pushvalue(L, -2);
@@ -174,14 +174,14 @@ runnable lua::create_runnable(lua_State* L) {
scripting::common_func lua::create_lambda(lua_State* L) {
auto funcptr = createLambdaHandler(L);
return [=](const std::vector<dynamic::Value>& args) {
lua_getglobal(L, LAMBDAS_TABLE.c_str());
lua_getfield(L, -1, funcptr->c_str());
getglobal(L, LAMBDAS_TABLE);
getfield(L, *funcptr);
for (const auto& arg : args) {
pushvalue(L, arg);
}
if (call(L, args.size(), 1)) {
auto result = tovalue(L, -1);
lua_pop(L, 1);
pop(L);
return result;
}
return dynamic::Value(dynamic::NONE);
@@ -192,10 +192,10 @@ int lua::createEnvironment(lua_State* L, int parent) {
int id = nextEnvironment++;
// local env = {}
lua_createtable(L, 0, 1);
createtable(L, 0, 1);
// setmetatable(env, {__index=_G})
lua_createtable(L, 0, 1);
createtable(L, 0, 1);
if (parent == 0) {
pushglobals(L);
} else {