[Lldb-commits] [PATCH] D90787: [LLDB-lua] modify Lua's 'print' to respect 'io.stdout'

Pedro Tammela via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 5 04:59:45 PST 2020


tammela updated this revision to Diff 303088.
tammela added a comment.

Add test case


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90787/new/

https://reviews.llvm.org/D90787

Files:
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
  lldb/test/Shell/ScriptInterpreter/Lua/print.test


Index: lldb/test/Shell/ScriptInterpreter/Lua/print.test
===================================================================
--- /dev/null
+++ 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
Index: lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
+++ lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
@@ -25,16 +25,8 @@
 
 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);
Index: lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
@@ -14,6 +14,35 @@
 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") ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90787.303088.patch
Type: text/x-patch
Size: 2668 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20201105/1a9843ca/attachment.bin>


More information about the lldb-commits mailing list