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
+22 -26
View File
@@ -1,13 +1,9 @@
#include "scripting_functional.hpp"
#include "lua/lua_util.hpp"
#include "lua/LuaState.hpp"
#include "lua/lua_engine.hpp"
#include "../../debug/Logger.hpp"
#include "../../util/stringutil.hpp"
namespace scripting {
extern lua::LuaState* state;
}
using namespace scripting;
static debug::Logger logger("scripting_func");
@@ -17,7 +13,7 @@ runnable scripting::create_runnable(
const std::string& src,
const std::string& file
) {
auto L = state->getMainThread();
auto L = lua::get_main_thread();
try {
lua::loadbuffer(L, *env, src, file);
return lua::create_runnable(L);
@@ -27,12 +23,12 @@ runnable scripting::create_runnable(
}
}
static lua_State* processCallback(
static lua_State* process_callback(
const scriptenv& env,
const std::string& src,
const std::string& file
) {
auto L = state->getMainThread();
auto L = lua::get_main_thread();
try {
if (lua::eval(L, *env, src, file) != 0) {
return L;
@@ -49,7 +45,7 @@ wstringconsumer scripting::create_wstring_consumer(
const std::string& file
) {
return [=](const std::wstring& x){
if (auto L = processCallback(env, src, file)) {
if (auto L = process_callback(env, src, file)) {
lua::pushwstring(L, x);
lua::call_nothrow(L, 1);
}
@@ -62,11 +58,11 @@ wstringsupplier scripting::create_wstring_supplier(
const std::string& file
) {
return [=](){
if (auto L = processCallback(env, src, file)) {
if (lua_isfunction(L, -1)) {
if (auto L = process_callback(env, src, file)) {
if (lua::isfunction(L, -1)) {
lua::call_nothrow(L, 0);
}
auto str = lua::require_wstring(L, -1); lua_pop(L, 1);
auto str = lua::require_wstring(L, -1); lua::pop(L);
return str;
}
return std::wstring(L"");
@@ -79,7 +75,7 @@ wstringchecker scripting::create_wstring_validator(
const std::string& file
) {
return [=](const std::wstring& x){
if (auto L = processCallback(env, src, file)) {
if (auto L = process_callback(env, src, file)) {
lua::pushwstring(L, x);
if (lua::call_nothrow(L, 1))
return lua::toboolean(L, -1);
@@ -94,7 +90,7 @@ boolconsumer scripting::create_bool_consumer(
const std::string& file
) {
return [=](bool x){
if (auto L = processCallback(env, src, file)) {
if (auto L = process_callback(env, src, file)) {
lua::pushboolean(L, x);
lua::call_nothrow(L, 1);
}
@@ -107,11 +103,11 @@ boolsupplier scripting::create_bool_supplier(
const std::string& file
) {
return [=](){
if (auto L = processCallback(env, src, file)) {
if (lua_isfunction(L, -1)) {
if (auto L = process_callback(env, src, file)) {
if (lua::isfunction(L, -1)) {
lua::call_nothrow(L, 0);
}
bool x = lua::toboolean(L,-1); lua_pop(L, 1);
bool x = lua::toboolean(L,-1); lua::pop(L);
return x;
}
return false;
@@ -124,7 +120,7 @@ doubleconsumer scripting::create_number_consumer(
const std::string& file
) {
return [=](double x){
if (auto L = processCallback(env, src, file)) {
if (auto L = process_callback(env, src, file)) {
lua::pushnumber(L, x);
lua::call_nothrow(L, 1);
}
@@ -137,11 +133,11 @@ doublesupplier scripting::create_number_supplier(
const std::string& file
) {
return [=](){
if (auto L = processCallback(env, src, file)) {
if (lua_isfunction(L, -1)) {
if (auto L = process_callback(env, src, file)) {
if (lua::isfunction(L, -1)) {
lua::call_nothrow(L, 0);
}
auto x = lua_tonumber(L, -1);
auto x = lua::tonumber(L, -1);
lua::pop(L);
return x;
}
@@ -155,7 +151,7 @@ int_array_consumer scripting::create_int_array_consumer(
const std::string& file
) {
return [=](const int arr[], size_t len) {
if (auto L = processCallback(env, src, file)) {
if (auto L = process_callback(env, src, file)) {
for (uint i = 0; i < len; i++) {
lua::pushinteger(L, arr[i]);
}
@@ -170,12 +166,12 @@ vec2supplier scripting::create_vec2_supplier(
const std::string& file
) {
return [=]() {
if (auto L = processCallback(env, src, file)) {
if (lua_isfunction(L, -1)) {
if (auto L = process_callback(env, src, file)) {
if (lua::isfunction(L, -1)) {
lua::call_nothrow(L, 0);
}
auto y = lua_tonumber(L, -1); lua::pop(L);
auto x = lua_tonumber(L, -1); lua::pop(L);
auto y = lua::tonumber(L, -1); lua::pop(L);
auto x = lua::tonumber(L, -1); lua::pop(L);
return glm::vec2(x, y);
}
return glm::vec2(0, 0);