From: Pedro Tammela Date: Thu, 5 Nov 2020 20:53:16 +0000 (+0000) Subject: [LLDB-lua] modify Lua's 'print' to respect 'io.stdout' X-Git-Tag: llvmorg-13-init~6924 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ca17571051d4e0a63e702371984dbd3671261f79;p=platform%2Fupstream%2Fllvm.git [LLDB-lua] modify Lua's 'print' to respect 'io.stdout' This patch changes the implementation of Lua's `print()` function to respect `io.stdout`. The original implementation uses `lua_writestring()` internally, which is hardcoded to `stdout`. Reviewed By: JDevlieghere Differential Revision: https://reviews.llvm.org/D90787 --- diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp index 2db44f2..dc3fd84 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp @@ -14,6 +14,34 @@ using namespace lldb_private; using namespace lldb; +static int lldb_print(lua_State *L) { + int n = lua_gettop(L); + lua_getglobal(L, "io"); + lua_getfield(L, -1, "stdout"); + lua_getfield(L, -1, "write"); + for (int i = 1; i <= n; i++) { + lua_pushvalue(L, -1); // write() + lua_pushvalue(L, -3); // io.stdout + luaL_tolstring(L, i, nullptr); + lua_pushstring(L, i != n ? "\t" : "\n"); + lua_call(L, 3, 0); + } + return 0; +} + +Lua::Lua() : m_lua_state(luaL_newstate()) { + assert(m_lua_state); + luaL_openlibs(m_lua_state); + luaopen_lldb(m_lua_state); + lua_pushcfunction(m_lua_state, lldb_print); + lua_setglobal(m_lua_state, "print"); +} + +Lua::~Lua() { + assert(m_lua_state); + lua_close(m_lua_state); +} + llvm::Error Lua::Run(llvm::StringRef buffer) { int error = luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer") || diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h index ff055d0..83f836d 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h @@ -25,16 +25,8 @@ int luaopen_lldb(lua_State *L); class Lua { public: - Lua() : m_lua_state(luaL_newstate()) { - assert(m_lua_state); - luaL_openlibs(m_lua_state); - luaopen_lldb(m_lua_state); - } - - ~Lua() { - assert(m_lua_state); - lua_close(m_lua_state); - } + Lua(); + ~Lua(); llvm::Error Run(llvm::StringRef buffer); llvm::Error LoadModule(llvm::StringRef filename); diff --git a/lldb/test/Shell/ScriptInterpreter/Lua/print.test b/lldb/test/Shell/ScriptInterpreter/Lua/print.test new file mode 100644 index 0000000..fd457ec --- /dev/null +++ b/lldb/test/Shell/ScriptInterpreter/Lua/print.test @@ -0,0 +1,23 @@ +# REQUIRES: lua +# UNSUPPORTED: lldb-repro +# +# RUN: rm -rf %t.stderr %t.stdout +# RUN: cat %s | %lldb --script-language lua 2> %t.stderr > %t.stdout +# RUN: cat %t.stdout | FileCheck %s --check-prefix STDOUT +# RUN: cat %t.stderr | FileCheck %s --check-prefix STDERR +script +file = lldb.SBFile(2, "w", false) +lldb.debugger:SetOutputFile(file) +print(95000 + 126, nil, 'a') +quit +script +print({}) +quit + +# STDOUT: 95126 nil a +# STDOUT-NOT: table: {{0x[[:xdigit:]]+}} +# STDERR: table: {{0x[[:xdigit:]]+}} + +# RUN: rm -rf %t.stderr %t.stdout +# RUN: %lldb --script-language lua -o 'script print(95000 + 126, nil, "a")' 2> %t.stderr > %t.stdout +# RUN: cat %t.stdout | FileCheck %s --check-prefix STDOUT