
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
110 lines
3.5 KiB
C++
110 lines
3.5 KiB
C++
//===-- ScriptInterpreterLua.cpp --------------------------------*- 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "ScriptInterpreterLua.h"
|
|
#include "Lua.h"
|
|
#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"
|
|
|
|
using namespace lldb;
|
|
using namespace lldb_private;
|
|
|
|
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 {
|
|
if (llvm::Error error = m_lua.Run(data)) {
|
|
*GetOutputStreamFileSP() << llvm::toString(std::move(error));
|
|
}
|
|
}
|
|
|
|
private:
|
|
Lua m_lua;
|
|
};
|
|
|
|
ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger)
|
|
: ScriptInterpreter(debugger, eScriptLanguageLua) {}
|
|
|
|
ScriptInterpreterLua::~ScriptInterpreterLua() {}
|
|
|
|
bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command,
|
|
CommandReturnObject *result,
|
|
const ExecuteScriptOptions &options) {
|
|
Lua l;
|
|
if (llvm::Error e = l.Run(command)) {
|
|
result->AppendErrorWithFormatv(
|
|
"lua failed attempting to evaluate '{0}': {1}\n", command,
|
|
llvm::toString(std::move(e)));
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void ScriptInterpreterLua::ExecuteInterpreterLoop() {
|
|
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));
|
|
debugger.PushIOHandler(io_handler_sp);
|
|
}
|
|
|
|
void ScriptInterpreterLua::Initialize() {
|
|
static llvm::once_flag g_once_flag;
|
|
|
|
llvm::call_once(g_once_flag, []() {
|
|
PluginManager::RegisterPlugin(GetPluginNameStatic(),
|
|
GetPluginDescriptionStatic(),
|
|
lldb::eScriptLanguageLua, CreateInstance);
|
|
});
|
|
}
|
|
|
|
void ScriptInterpreterLua::Terminate() {}
|
|
|
|
lldb::ScriptInterpreterSP
|
|
ScriptInterpreterLua::CreateInstance(Debugger &debugger) {
|
|
return std::make_shared<ScriptInterpreterLua>(debugger);
|
|
}
|
|
|
|
lldb_private::ConstString ScriptInterpreterLua::GetPluginNameStatic() {
|
|
static ConstString g_name("script-lua");
|
|
return g_name;
|
|
}
|
|
|
|
const char *ScriptInterpreterLua::GetPluginDescriptionStatic() {
|
|
return "Lua script interpreter";
|
|
}
|
|
|
|
lldb_private::ConstString ScriptInterpreterLua::GetPluginName() {
|
|
return GetPluginNameStatic();
|
|
}
|
|
|
|
uint32_t ScriptInterpreterLua::GetPluginVersion() { return 1; }
|