get lua output?

BlitzMax Forums/BlitzMax Programming/get lua output?

UNZ(Posted 2013) [#1]
Hi,

I have seen several posts in which
lua_tostring(luaState, - 1)

is used to get the result of a lua process.
But it returns nothing. luaL_dostring() and luaL_dofile() on the other hand print directly to stdout. But it would be more convenient if I could get it as a string.

example:



Derron(Posted 2013) [#2]
	Method DumpError()
		Print "#################################"
		WriteStdout "LUA ERROR in Engine "+Self.id+"~n"
		WriteStdout lua_tostring( getLuaState(),-1 )+"~n"
		Print "#################################"
	End Method


Code works like expected...
it gets called after function calls
If lua_pcall( getLuaState(),args.length,1,0 ) Then DumpError()


Only problematic thing is: if your lua scripts include other scripts and access eg. blitzmax objects, the lua error does not tell the correct line of the error... "line: 222 in includes..." (which does not exist).


bye
Ron


UNZ(Posted 2013) [#3]
I don't want the error output but the normal output.


Derron(Posted 2013) [#4]
You could give each lua-script access to a Blitzmax-Print - which could be intercepted by you or logged or ...

so: expose a "function doPrint(text:string)" to your script, this script
a) print text
b) lines.addLast(text)


edit: you can even redefine your "lua print"
--backup if you need default behaviour
printOldMode = print

print = function(...)
  for arg,_ in ipairs({...}) do
    -- call the blitzmax exposed "blitzmaxPrint"
  end
end


edit2, just tried:

Blitzmax:
WriteStdout lua_tostring( getLuaState(),-1 )+"~n"

which did return the thing the lua script had output:

Lua:
print "lua file loaded"

console:
lua file loaded


edit3:
just modified your code


Like said you will have to "log" all BlitzPrint-files in a list. After script execution you just print all list entries...
This is because by default the "print" of lua uses stdout which cannot be rewritten without patchin the lua sources - or overwriting the default "print"-function (do this in your lua script - or like i have done in edit3).

Another idea is to:
overwrite the print-function inline. This new function stores all "print"-lines.
At the end of the script you "return allLinesConcat".

This way your "get result" will work. But disadvantage is: if the script has errors in it... it wont come to the line of "return ...".



bye
Ron


UNZ(Posted 2013) [#5]
thx.
Lua is really cool!

EDIT:
I just expanded the example a little: