[Lldb-commits] [lldb] 572b9f4 - [lldb/Lua] Support loading Lua modules

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Fri Jan 10 10:23:02 PST 2020


Author: Jonas Devlieghere
Date: 2020-01-10T10:22:30-08:00
New Revision: 572b9f468ad6844795fec29a7e671ba64d82e8c2

URL: https://github.com/llvm/llvm-project/commit/572b9f468ad6844795fec29a7e671ba64d82e8c2
DIFF: https://github.com/llvm/llvm-project/commit/572b9f468ad6844795fec29a7e671ba64d82e8c2.diff

LOG: [lldb/Lua] Support loading Lua modules

Implements the command script import command for Lua.

Differential revision: https://reviews.llvm.org/D71825

Added: 
    lldb/test/Shell/ScriptInterpreter/Lua/Inputs/testmodule.lua
    lldb/test/Shell/ScriptInterpreter/Lua/command_script_import.test

Modified: 
    lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
    lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
    lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
    lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
index 1dd0a9eade0c..ecee8cc674f8 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
@@ -7,6 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "Lua.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Utility/FileSpec.h"
 #include "llvm/Support/FormatVariadic.h"
 
 using namespace lldb_private;
@@ -26,3 +28,32 @@ llvm::Error Lua::Run(llvm::StringRef buffer) {
   lua_pop(m_lua_state, 1);
   return e;
 }
+
+llvm::Error Lua::LoadModule(llvm::StringRef filename) {
+  FileSpec file(filename);
+  if (!FileSystem::Instance().Exists(file)) {
+    return llvm::make_error<llvm::StringError>("invalid path",
+                                               llvm::inconvertibleErrorCode());
+  }
+
+  ConstString module_extension = file.GetFileNameExtension();
+  if (module_extension != ".lua") {
+    return llvm::make_error<llvm::StringError>("invalid extension",
+                                               llvm::inconvertibleErrorCode());
+  }
+
+  int error = luaL_loadfile(m_lua_state, filename.data()) ||
+              lua_pcall(m_lua_state, 0, 1, 0);
+  if (error) {
+    llvm::Error e = llvm::make_error<llvm::StringError>(
+        llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
+        llvm::inconvertibleErrorCode());
+    // Pop error message from the stack.
+    lua_pop(m_lua_state, 1);
+    return e;
+  }
+
+  ConstString module_name = file.GetFileNameStrippingExtension();
+  lua_setglobal(m_lua_state, module_name.GetCString());
+  return llvm::Error::success();
+}

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
index adc6c6118436..f2984a925dfe 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
@@ -37,6 +37,7 @@ class Lua {
   }
 
   llvm::Error Run(llvm::StringRef buffer);
+  llvm::Error LoadModule(llvm::StringRef filename);
 
 private:
   lua_State *m_lua_state;

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
index e46851c45092..701d68d1ec08 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
@@ -83,6 +83,18 @@ void ScriptInterpreterLua::ExecuteInterpreterLoop() {
   debugger.PushIOHandler(io_handler_sp);
 }
 
+bool ScriptInterpreterLua::LoadScriptingModule(
+    const char *filename, bool init_session, lldb_private::Status &error,
+    StructuredData::ObjectSP *module_sp) {
+
+  if (llvm::Error e = m_lua->LoadModule(filename)) {
+    error.SetErrorStringWithFormatv("lua failed to import '{0}': {1}\n",
+                                    filename, llvm::toString(std::move(e)));
+    return false;
+  }
+  return true;
+}
+
 void ScriptInterpreterLua::Initialize() {
   static llvm::once_flag g_once_flag;
 

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
index 550e1035567c..4e922151385b 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
@@ -25,6 +25,11 @@ class ScriptInterpreterLua : public ScriptInterpreter {
 
   void ExecuteInterpreterLoop() override;
 
+  virtual bool
+  LoadScriptingModule(const char *filename, bool init_session,
+                      lldb_private::Status &error,
+                      StructuredData::ObjectSP *module_sp = nullptr) override;
+
   // Static Functions
   static void Initialize();
 

diff  --git a/lldb/test/Shell/ScriptInterpreter/Lua/Inputs/testmodule.lua b/lldb/test/Shell/ScriptInterpreter/Lua/Inputs/testmodule.lua
new file mode 100644
index 000000000000..fcf4eb05bd2b
--- /dev/null
+++ b/lldb/test/Shell/ScriptInterpreter/Lua/Inputs/testmodule.lua
@@ -0,0 +1,7 @@
+local mymodule = {}
+
+function mymodule.foo()
+  print("Hello World!")
+end
+
+return mymodule

diff  --git a/lldb/test/Shell/ScriptInterpreter/Lua/command_script_import.test b/lldb/test/Shell/ScriptInterpreter/Lua/command_script_import.test
new file mode 100644
index 000000000000..6a0692d33efb
--- /dev/null
+++ b/lldb/test/Shell/ScriptInterpreter/Lua/command_script_import.test
@@ -0,0 +1,13 @@
+# REQUIRES: lua
+# RUN: %lldb --script-language lua -o 'command script import %S/Inputs/testmodule.lua' -o 'script testmodule.foo()' 2>&1 | FileCheck %s
+# CHECK: Hello World!
+
+# RUN: mkdir -p %t
+# RUN: cp %S/Inputs/testmodule.lua %t/testmodule.notlua
+# RUN: %lldb --script-language lua -o 'command script import %t/testmodule.notlua' -o 'script testmodule.foo()' 2>&1 | FileCheck %s --check-prefix EXTENSION
+# EXTENSION: error: module importing failed: lua failed to import '{{.*}}testmodule.notlua': invalid extension
+# EXTENSION-NOT: Hello World!
+
+# RUN: %lldb --script-language lua -o 'command script import %S/Inputs/bogus' -o 'script testmodule.foo()' 2>&1 | FileCheck %s --check-prefix NONEXISTING
+# NONEXISTING: error: module importing failed: lua failed to import '{{.*}}bogus': invalid path
+# NONEXISTING-NOT: Hello World!


        


More information about the lldb-commits mailing list