[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
Wed Nov 4 12:50:05 PST 2020


tammela created this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.
tammela requested review of this revision.
Herald added a subscriber: JDevlieghere.

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`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90787

Files:
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h


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.302945.patch
Type: text/x-patch
Size: 1748 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20201104/4db71cc7/attachment.bin>


More information about the lldb-commits mailing list