Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lldb/Lua] Implement a Simple Lua Script Interpreter Prototype
This implements a very elementary Lua script interpreter. It supports running a single command as well as running interactively. It uses editline if available. It's still missing a bunch of stuff though. Some things that I intentionally ingored for now are that I/O isn't properly hooked up (so every print goes to stdout) and the non-editline support which is not handling a bunch of corner cases. The latter is a matter of reusing existing code in the Python interpreter. Discussion on the mailing list: http://lists.llvm.org/pipermail/lldb-dev/2019-December/015812.html Differential revision: https://reviews.llvm.org/D71234
- Loading branch information
1 parent
2a42a5a
commit 2861324
Showing
14 changed files
with
235 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ class IOHandler { | |
REPL, | ||
ProcessIO, | ||
PythonInterpreter, | ||
LuaInterpreter, | ||
PythonCode, | ||
Other | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
add_lldb_library(lldbPluginScriptInterpreterLua PLUGIN | ||
Lua.cpp | ||
ScriptInterpreterLua.cpp | ||
|
||
LINK_LIBS | ||
lldbCore | ||
lldbInterpreter | ||
) | ||
) | ||
|
||
target_include_directories(lldbPluginScriptInterpreterLua PUBLIC ${LUA_INCLUDE_DIR}) | ||
target_link_libraries(lldbPluginScriptInterpreterLua INTERFACE ${LUA_LIBRARIES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===-- Lua.cpp -----------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Lua.h" | ||
#include "llvm/Support/FormatVariadic.h" | ||
|
||
using namespace lldb_private; | ||
|
||
llvm::Error Lua::Run(llvm::StringRef buffer) { | ||
int error = | ||
luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer") || | ||
lua_pcall(m_lua_state, 0, 0, 0); | ||
if (!error) | ||
return llvm::Error::success(); | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===-- ScriptInterpreterLua.h ----------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef liblldb_Lua_h_ | ||
#define liblldb_Lua_h_ | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/Error.h" | ||
|
||
#include "lua.hpp" | ||
|
||
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); | ||
} | ||
|
||
llvm::Error Run(llvm::StringRef buffer); | ||
|
||
private: | ||
lua_State *m_lua_state; | ||
}; | ||
|
||
} // namespace lldb_private | ||
|
||
#endif // liblldb_Lua_h_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# REQUIRES: lua | ||
# RUN: %lldb --script-language lua -o 'script print(1000+100+10+1)' 2>&1 | FileCheck %s | ||
# CHECK: 1111 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
if (LLDB_ENABLE_PYTHON) | ||
add_subdirectory(Python) | ||
endif() | ||
if (LLDB_ENABLE_LUA) | ||
add_subdirectory(Lua) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
add_lldb_unittest(ScriptInterpreterLuaTests | ||
LuaTests.cpp | ||
ScriptInterpreterTests.cpp | ||
|
||
LINK_LIBS | ||
lldbHost | ||
lldbPluginScriptInterpreterLua | ||
lldbPluginPlatformLinux | ||
LLVMTestingSupport | ||
LINK_COMPONENTS | ||
Support | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===-- LuaTests.cpp ------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Plugins/ScriptInterpreter/Lua/Lua.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace lldb_private; | ||
|
||
TEST(LuaTest, RunValid) { | ||
Lua lua; | ||
llvm::Error error = lua.Run("foo = 1"); | ||
EXPECT_FALSE(static_cast<bool>(error)); | ||
} | ||
|
||
TEST(LuaTest, RunInvalid) { | ||
Lua lua; | ||
llvm::Error error = lua.Run("nil = foo"); | ||
EXPECT_TRUE(static_cast<bool>(error)); | ||
EXPECT_EQ(llvm::toString(std::move(error)), | ||
"[string \"buffer\"]:1: unexpected symbol near 'nil'\n"); | ||
} |
62 changes: 62 additions & 0 deletions
62
lldb/unittests/ScriptInterpreter/Lua/ScriptInterpreterTests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//===-- LuaTests.cpp ------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "Plugins/Platform/Linux/PlatformLinux.h" | ||
#include "Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h" | ||
#include "lldb/Core/Debugger.h" | ||
#include "lldb/Host/FileSystem.h" | ||
#include "lldb/Host/HostInfo.h" | ||
#include "lldb/Interpreter/CommandReturnObject.h" | ||
#include "lldb/Target/Platform.h" | ||
#include "lldb/Utility/Reproducer.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace lldb_private; | ||
using namespace lldb_private::repro; | ||
using namespace lldb; | ||
|
||
namespace { | ||
class ScriptInterpreterTest : public ::testing::Test { | ||
public: | ||
void SetUp() override { | ||
llvm::cantFail(Reproducer::Initialize(ReproducerMode::Off, llvm::None)); | ||
FileSystem::Initialize(); | ||
HostInfo::Initialize(); | ||
|
||
// Pretend Linux is the host platform. | ||
platform_linux::PlatformLinux::Initialize(); | ||
ArchSpec arch("powerpc64-pc-linux"); | ||
Platform::SetHostPlatform( | ||
platform_linux::PlatformLinux::CreateInstance(true, &arch)); | ||
} | ||
void TearDown() override { | ||
platform_linux::PlatformLinux::Terminate(); | ||
HostInfo::Terminate(); | ||
FileSystem::Terminate(); | ||
Reproducer::Terminate(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
TEST_F(ScriptInterpreterTest, Plugin) { | ||
EXPECT_EQ(ScriptInterpreterLua::GetPluginNameStatic(), "script-lua"); | ||
EXPECT_EQ(ScriptInterpreterLua::GetPluginDescriptionStatic(), | ||
"Lua script interpreter"); | ||
} | ||
|
||
TEST_F(ScriptInterpreterTest, ExecuteOneLine) { | ||
DebuggerSP debugger_sp = Debugger::CreateInstance(); | ||
ASSERT_TRUE(debugger_sp); | ||
|
||
ScriptInterpreterLua script_interpreter(*debugger_sp); | ||
CommandReturnObject result; | ||
EXPECT_TRUE(script_interpreter.ExecuteOneLine("foo = 1", &result)); | ||
EXPECT_FALSE(script_interpreter.ExecuteOneLine("nil = foo", &result)); | ||
EXPECT_TRUE(result.GetErrorData().startswith( | ||
"error: lua failed attempting to evaluate 'nil = foo'")); | ||
} |