Get Lua Output from StdIO?

BlitzMax Forums/BlitzMax Programming/Get Lua Output from StdIO?

Gabriel(Posted 2007) [#1]
Is there any way to get the output from calling a Lua function? I think this would go through StdIO?

IE: I want to run a lua script ( just a string ) which says "print 10"

And I want to get Lua's response to that as a string ("10") and do something with it. Is that possible?


N(Posted 2007) [#2]
So you don't want to return values, you want to pipe console output into something? If you need to return multiple values, you can do that with Lua anyway, so I'm not sure why you'd bother going through the trouble of that.

When you call a function using lua_call/pcall, you specify the amount of return values you want. When the function has closed, the return values are on the top of the stack (and I believe any unsatisfied amount of returns will be padded with nils), so really all you need to do in order to get the return value is pop the data. Problem with this is that I'm not sure if that's what you want (and if it isn't, why?).


fredborg(Posted 2007) [#3]
You can make your own print function, it will overwrite the default lua function.


Gabriel(Posted 2007) [#4]
Essentially I'm just trying to make a simple Lua interpreter, and want to redirect the console output into my own gadget. Overriding the print function and/or multiple return values could probably be made to suffice but if there's a way to get the console output and use that, I'd much prefer it.

I'm thinking that the answer might lie in recompiling the Lua module itself, overriding printf and fprintf with a #define and then writing custom functions to send the data elsewhere if the stream is stdout ( or indeed stderr ) but I'm not sure how to go about that, and I'm hoping there might be another ( easier ) way.