//===----------------------------------------------------------------------===//
#include "Lua.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Utility/FileSpec.h"
#include "llvm/Support/FormatVariadic.h"
using namespace lldb_private;
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();
+}
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;
--- /dev/null
+# 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!