xref: /llvm-project/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp (revision 45c971f7eef18ef2b77a5f64133dbd7bd5939d5f)
1 //===-- Lua.cpp -----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Lua.h"
10 #include "llvm/Support/FormatVariadic.h"
11 
12 using namespace lldb_private;
13 using namespace lldb;
14 
15 llvm::Error Lua::Run(llvm::StringRef buffer) {
16   int error =
17       luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer") ||
18       lua_pcall(m_lua_state, 0, 0, 0);
19   if (!error)
20     return llvm::Error::success();
21 
22   llvm::Error e = llvm::make_error<llvm::StringError>(
23       llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)),
24       llvm::inconvertibleErrorCode());
25   // Pop error message from the stack.
26   lua_pop(m_lua_state, 1);
27   return e;
28 }
29