-->
Page 1 of 3

Q: How to execute Lua in a String and return String result

PostPosted: Sat Nov 29, 2014 6:01 pm
by gerardwr
Need some help from the Lua experts here.

For my web browser example (http://www.esp8266.com/viewtopic.php?f=19&t=611&start=40) I need a way to execute a Lua statement thats in a string variable AND return the result in a string. Even better, some kind of multiline string as input, and a multistring as output.

I had a look at loadstring ( http://www.gammon.com.au/scripts/doc.php?lua=assert). That executes my string nicely.
Code: Select all> s=assert (loadstring ("print 'hello, world' print(tmr.now())")) ()
hello, world
344158447
> =s
nil
>


But….. the result is "just" printed, and not assigned to the variable s as I had hoped :?

I had a QUICK look to node.input, knowing that it would not return a result on the command line, I naively did anyway. Yep, no dice:
Code: Select all> node.input("print('test')")
> s=node.input("print('test')")
> =s
nil
>


Just to be clear : if the Lua statement is executed OK, I want the result of the code returned into a variable. If the Lua statement contains an error I would like the error-line returned into a string variable (ref. the error line in the code below):
Code: Select all> s=assert (loadstring ("rint 'hello, world' print(tmr.now())")) ()
lua: [string "rint 'hello, world' print(tmr.now())"]:1: attempt to call global 'rint' (a nil value)
>


Put my nose in the right direction, please?

Re: Q: How to execute Lua in a String and return String resu

PostPosted: Sat Nov 29, 2014 8:56 pm
by zeroday
Code: Select allnode.output

is a api to re-direct the output, it is originally designed for telnet.
but I think it may be of some use in your code

Re: Q: How to execute Lua in a String and return String resu

PostPosted: Sat Nov 29, 2014 9:30 pm
by zeroday
Or, do not use print, but use string lib
Code: Select alls="hello world"..tostring(tmr.now())

Re: Q: How to execute Lua in a String and return String resu

PostPosted: Sun Nov 30, 2014 6:52 am
by gerardwr
Thanks zero day, I will try the "high road" using node.input and node.output first.

The tostring() code is usable for function that return a numerical value but I need code that interprets any Lua statement, and I image that tostring is not capable to do that.

Can't test right now, my HW is on the fritz ;)