Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
Writes the lua command to a tmp file now so it can do better error ch…
Browse files Browse the repository at this point in the history
…ecking
  • Loading branch information
X-rays5 committed Jun 12, 2021
1 parent 743b6df commit d0be578
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
15 changes: 12 additions & 3 deletions lua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace lua {
sol::lib::io,
sol::lib::package
);
//lua_state_.set_panic(sol::c_call<decltype(&lua_state::Panic), &lua_state::Panic>);
}

template<typename... Args>
Expand All @@ -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()));
}
}
Expand All @@ -68,5 +69,13 @@ namespace lua {
throw(lua_exception(e.what()));
}
}

inline void Panic(sol::optional<std::string> 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
22 changes: 16 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <iostream>
#include <string>
#include <fstream>
#include <filesystem>
#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);
Expand All @@ -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;
}

0 comments on commit d0be578

Please sign in to comment.