From c412f0c52908f2c9f56d80ed17f165a73cf1d2a6 Mon Sep 17 00:00:00 2001
From: pixel <pixel>
Date: Thu, 31 May 2007 13:26:52 +0000
Subject: Implementing print inside of the Lua object, and using LuaPrinter
 object in order to redirect prints on a per-VM basis.

---
 lib/BLua.cc                   | 39 ++++++++++++++++++++++++++++++++++++++-
 lib/lua/src/LuaLib/lbaselib.c |  4 ++--
 2 files changed, 40 insertions(+), 3 deletions(-)

(limited to 'lib')

diff --git a/lib/BLua.cc b/lib/BLua.cc
index ac1f8e8..ab54be3 100644
--- a/lib/BLua.cc
+++ b/lib/BLua.cc
@@ -17,7 +17,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-/* $Id: BLua.cc,v 1.48 2007-05-30 22:36:31 pixel Exp $ */
+/* $Id: BLua.cc,v 1.49 2007-05-31 13:26:52 pixel Exp $ */
 
 #include <stdlib.h>
 #include "BLua.h"
@@ -31,6 +31,12 @@
 
 #undef isnumber
 
+void LuaPrinter::puts(const char * msg) {
+    fputs(msg, stdout);
+}
+
+LuaPrinter default_lua_printer;
+
 extern "C" {
     void do_lua_lock(lua_State * __L) {
 	Lua * L;
@@ -81,6 +87,8 @@ class LuaStatics : public Base {
     static int mkdir(lua_State *);
 
     static int globalindex(lua_State *);
+    
+    static int print(lua_State *);
 };
 
 std::map<lua_State *, Lua *> Lua::lualist;
@@ -317,6 +325,24 @@ int LuaStatics::mkdir(lua_State * _L) {
     return 0;
 }
 
+int LuaStatics::print(lua_State * _L) {
+    Lua * L = Lua::find(_L);
+    
+    int n = L->gettop();  /* number of arguments */
+    int i;
+    for (i = 1; i <= n; i++) {
+        const char *s;
+        s = lua_tostring(_L, i);  /* get result */
+        if (s == NULL)
+            L->error("`tostring' must return a string to `print'");
+        if (i > 1) fputs("\t", stdout);
+        L->puts(s);
+        L->pop();  /* pop result */
+    }
+    L->puts("\n");
+    return 0;
+}
+
 int LuaStatics::callwrap(lua_State * __L, lua_CFunction func) {
     Lua * L = Lua::find(__L);
     int n;
@@ -386,6 +412,8 @@ void Lua::setup_state(lua_State *) {
     lua_setcallwrap(L, LuaStatics::callwrap);
     lua_setcreatehook(L, LuaStatics::createhook);
     lua_setdestroyhook(L, LuaStatics::destroyhook);
+    
+    lprinter = &default_lua_printer;
 }
 
 Lua::Lua() : L(lua_open()), _protected(false), _is_thread(false) {
@@ -399,6 +427,7 @@ Lua::Lua() : L(lua_open()), _protected(false), _is_thread(false) {
     declarefunc("hex", LuaStatics::hex);
     declarefunc("getglobal", LuaStatics::getglobal);
     declarefunc("dumpvars", LuaStatics::dumpvars);
+    declarefunc("print", LuaStatics::print);
     push("BLUA_THREADS");
     newtable();
     settable(LUA_REGISTRYINDEX);
@@ -415,6 +444,14 @@ Lua::Lua(lua_State * __L) : L(__L), _protected(false), _is_thread(true) {
 
 }
 
+void Lua::SetPrinter(LuaPrinter * lp) {
+    lprinter = lp;
+}
+
+void Lua::puts(const char * msg) {
+    lprinter->puts(msg);
+}
+
 Lua * Lua::Father() {
     return father ? father : this;
 }
diff --git a/lib/lua/src/LuaLib/lbaselib.c b/lib/lua/src/LuaLib/lbaselib.c
index ea70d2e..f9c76f1 100644
--- a/lib/lua/src/LuaLib/lbaselib.c
+++ b/lib/lua/src/LuaLib/lbaselib.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lbaselib.c,v 1.5 2004-12-27 19:52:23 pixel Exp $
+** $Id: lbaselib.c,v 1.6 2007-05-31 13:26:52 pixel Exp $
 ** Basic library
 ** See Copyright Notice in lua.h
 */
@@ -514,7 +514,7 @@ static const luaL_reg base_funcs[] = {
   {"next", luaB_next},
   {"ipairs", luaB_ipairs},
   {"pairs", luaB_pairs},
-  {"print", luaB_print},
+//  {"print", luaB_print},
   {"tonumber", luaB_tonumber},
   {"tostring", luaB_tostring},
   {"type", luaB_type},
-- 
cgit v1.2.3