diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp index a908ef08673500..dc64139fa4e5c4 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp @@ -12,6 +12,7 @@ using namespace lldb_private; llvm::Error Lua::Run(llvm::StringRef buffer) { + std::lock_guard lock(m_mutex); int error = luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer") || lua_pcall(m_lua_state, 0, 0, 0); diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h index 073e7e22e7d671..ed1d159590ac5a 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h @@ -14,6 +14,8 @@ #include "lua.hpp" +#include + namespace lldb_private { extern "C" { @@ -36,6 +38,7 @@ class Lua { llvm::Error Run(llvm::StringRef buffer); private: + std::mutex m_mutex; lua_State *m_lua_state; }; diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp index b3f1689909fdbe..d5423b78b8c435 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp @@ -22,33 +22,34 @@ using namespace lldb_private; class IOHandlerLuaInterpreter : public IOHandlerDelegate, public IOHandlerEditline { public: - IOHandlerLuaInterpreter(Debugger &debugger) + IOHandlerLuaInterpreter(Debugger &debugger, + ScriptInterpreterLua &script_interpreter) : IOHandlerEditline(debugger, IOHandler::Type::LuaInterpreter, "lua", ">>> ", "..> ", true, debugger.GetUseColor(), 0, *this, nullptr), - m_lua() {} + m_script_interpreter(script_interpreter) {} void IOHandlerInputComplete(IOHandler &io_handler, std::string &data) override { - if (llvm::Error error = m_lua.Run(data)) { + if (llvm::Error error = m_script_interpreter.GetLua().Run(data)) { *GetOutputStreamFileSP() << llvm::toString(std::move(error)); } } private: - Lua m_lua; + ScriptInterpreterLua &m_script_interpreter; }; ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger) - : ScriptInterpreter(debugger, eScriptLanguageLua) {} + : ScriptInterpreter(debugger, eScriptLanguageLua), + m_lua(std::make_unique()) {} ScriptInterpreterLua::~ScriptInterpreterLua() {} bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command, CommandReturnObject *result, const ExecuteScriptOptions &options) { - Lua l; - if (llvm::Error e = l.Run(command)) { + if (llvm::Error e = m_lua->Run(command)) { result->AppendErrorWithFormatv( "lua failed attempting to evaluate '{0}': {1}\n", command, llvm::toString(std::move(e))); @@ -72,7 +73,7 @@ void ScriptInterpreterLua::ExecuteInterpreterLoop() { if (!debugger.GetInputFile().IsValid()) return; - IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(debugger)); + IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(debugger, *this)); debugger.PushIOHandler(io_handler_sp); } @@ -107,3 +108,5 @@ lldb_private::ConstString ScriptInterpreterLua::GetPluginName() { } uint32_t ScriptInterpreterLua::GetPluginVersion() { return 1; } + +Lua &ScriptInterpreterLua::GetLua() { return *m_lua; } diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h index 7f69a73f58827b..b34c7d0e821761 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h @@ -12,7 +12,7 @@ #include "lldb/Interpreter/ScriptInterpreter.h" namespace lldb_private { - +class Lua; class ScriptInterpreterLua : public ScriptInterpreter { public: ScriptInterpreterLua(Debugger &debugger); @@ -40,6 +40,11 @@ class ScriptInterpreterLua : public ScriptInterpreter { lldb_private::ConstString GetPluginName() override; uint32_t GetPluginVersion() override; + + Lua &GetLua(); + +private: + std::unique_ptr m_lua; }; } // namespace lldb_private diff --git a/lldb/test/Shell/ScriptInterpreter/Lua/persistent_state.test b/lldb/test/Shell/ScriptInterpreter/Lua/persistent_state.test new file mode 100644 index 00000000000000..4cdea152fdaea6 --- /dev/null +++ b/lldb/test/Shell/ScriptInterpreter/Lua/persistent_state.test @@ -0,0 +1,3 @@ +# REQUIRES: lua +# RUN: %lldb --script-language lua -o 'script foo = 1010' -o 'script bar = 101' -o 'script print(foo+bar)' 2>&1 | FileCheck %s +# CHECK: 1111