JDevlieghere updated this revision to Diff 233220.
JDevlieghere added a comment.

Reuse IOHandlerEditline


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D71234/new/

https://reviews.llvm.org/D71234

Files:
  lldb/include/lldb/Core/IOHandler.h
  lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
  lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp

Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
@@ -10,32 +10,110 @@
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Core/StreamFile.h"
+#include "lldb/Interpreter/CommandReturnObject.h"
 #include "lldb/Utility/Stream.h"
 #include "lldb/Utility/StringList.h"
-
+#include "lldb/Utility/Timer.h"
 #include "llvm/Support/Threading.h"
 
+#ifndef LLDB_DISABLE_LIBEDIT
+#include "lldb/Host/Editline.h"
+#endif
+
+#include "lua.hpp"
+
+#define LUA_PROMPT ">>> "
+
 #include <mutex>
 
 using namespace lldb;
 using namespace lldb_private;
 
+class Lua {
+public:
+  Lua() : m_lua_state(luaL_newstate()) {
+    assert(m_lua_state);
+    luaL_openlibs(m_lua_state);
+  }
+  ~Lua() {
+    assert(m_lua_state);
+    luaL_openlibs(m_lua_state);
+  }
+
+  lua_State *State() { return m_lua_state; }
+
+private:
+  lua_State *m_lua_state = nullptr;
+};
+
+class IOHandlerLuaInterpreter : public IOHandlerDelegate,
+                                public IOHandlerEditline {
+public:
+  IOHandlerLuaInterpreter(Debugger &debugger)
+      : IOHandlerEditline(debugger, IOHandler::Type::LuaInterpreter, "lua",
+                          ">>> ", "..> ", true, debugger.GetUseColor(), 0,
+                          *this, nullptr),
+        m_lua() {}
+
+  void IOHandlerInputComplete(IOHandler &io_handler,
+                              std::string &data) override {
+    int error =
+        luaL_loadbuffer(m_lua.State(), data.c_str(), data.size(), "line") ||
+        lua_pcall(m_lua.State(), 0, 0, 0);
+    if (error) {
+      fprintf(GetOutputFILE(), "%s\n", lua_tostring(m_lua.State(), -1));
+      // Pop error message from the stack.
+      lua_pop(m_lua.State(), 1);
+    }
+  }
+
+  ~IOHandlerLuaInterpreter() override {}
+
+private:
+  Lua m_lua;
+};
+
 ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger)
     : ScriptInterpreter(debugger, eScriptLanguageLua) {}
 
 ScriptInterpreterLua::~ScriptInterpreterLua() {}
 
 bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command,
-                                          CommandReturnObject *,
-                                          const ExecuteScriptOptions &) {
-  m_debugger.GetErrorStream().PutCString(
-      "error: the lua script interpreter is not yet implemented.\n");
+                                          CommandReturnObject *result,
+                                          const ExecuteScriptOptions &options) {
+  Lua l;
+
+  // FIXME: Redirecting stdin, stdout and stderr.
+  int error =
+      luaL_loadbuffer(l.State(), command.data(), command.size(), "line") ||
+      lua_pcall(l.State(), 0, 0, 0);
+  if (error == 0)
+    return true;
+
+  result->AppendErrorWithFormatv("lua failed attempting to evaluate '{0}'\n",
+                                 command);
   return false;
 }
 
 void ScriptInterpreterLua::ExecuteInterpreterLoop() {
-  m_debugger.GetErrorStream().PutCString(
-      "error: the lua script interpreter is not yet implemented.\n");
+  static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
+  Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
+
+  Debugger &debugger = m_debugger;
+
+  // At the moment, the only time the debugger does not have an input file
+  // handle is when this is called directly from lua, in which case it is
+  // both dangerous and unnecessary (not to mention confusing) to try to embed
+  // a running interpreter loop inside the already running lua interpreter
+  // loop, so we won't do it.
+
+  if (!debugger.GetInputFile().IsValid())
+    return;
+
+  IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(debugger));
+  if (io_handler_sp) {
+    debugger.PushIOHandler(io_handler_sp);
+  }
 }
 
 void ScriptInterpreterLua::Initialize() {
Index: lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
+++ lldb/source/Plugins/ScriptInterpreter/Lua/CMakeLists.txt
@@ -4,4 +4,8 @@
   LINK_LIBS
     lldbCore
     lldbInterpreter
-  )
\ No newline at end of file
+  )
+
+find_package(Lua REQUIRED)
+target_include_directories(lldbPluginScriptInterpreterLua PRIVATE ${LUA_INCLUDE_DIR})
+target_link_libraries(lldbPluginScriptInterpreterLua PRIVATE ${LUA_LIBRARIES})
Index: lldb/include/lldb/Core/IOHandler.h
===================================================================
--- lldb/include/lldb/Core/IOHandler.h
+++ lldb/include/lldb/Core/IOHandler.h
@@ -52,6 +52,7 @@
     REPL,
     ProcessIO,
     PythonInterpreter,
+    LuaInterpreter,
     PythonCode,
     Other
   };
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to