diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dd72fe..7da232e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.19) project(lua_shell) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 17) include(FindLua) diff --git a/lua.hpp b/lua.hpp index 0b45488..d009166 100644 --- a/lua.hpp +++ b/lua.hpp @@ -33,6 +33,7 @@ namespace lua { sol::lib::io, sol::lib::package ); + //lua_state_.set_panic(sol::c_call); } template @@ -52,9 +53,9 @@ namespace lua { } void RunText(std::string text) { - auto script = lua_state_.script(text); - if (!script.valid()) { - sol::error e = script; + auto result = lua_state_.script(text); + if (!result.valid()) { + sol::error e = result; throw(lua_exception(e.what())); } } @@ -68,5 +69,13 @@ namespace lua { throw(lua_exception(e.what())); } } + + inline void Panic(sol::optional maybe_msg) { + std::cerr << "Lua is in a panic state application is closing\n"; + if (maybe_msg) { + const std::string& msg = maybe_msg.value(); + std::cerr << "\tError message: " << msg << std::endl; + } + } }; } // lua diff --git a/main.cpp b/main.cpp index fe95080..53d1e9d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,10 @@ #include #include +#include +#include #include "lua.hpp" -lua::lua_state lua_; - -void go() { +void go(lua::lua_state& lua_) { std::cout << "lua> "; std::string command; std::getline(std::cin, command); @@ -14,16 +14,26 @@ void go() { lua_ = std::move(lua::lua_state()); } else { try { - lua_.RunText(command); + std::ofstream writer("lua_shell.tmp"); + if (writer.is_open()) { + writer << command; + writer.close(); + lua_.ExcecuteFile("lua_shell.tmp"); + std::filesystem::remove("lua_shell.tmp"); + } else { + std::cout << "Couldn't execute"; + } } catch (lua::lua_exception &e) { std::cout << e.what() << "\n"; } } - go(); + go(lua_); } int main() { std::cout << "Lua interactive shell started.\n\n"; - go(); + + lua::lua_state lua_; + go(lua_); return EXIT_FAILURE; }