Q: How to execute Lua in a String and return String result
Posted: Sat Nov 29, 2014 6:01 pm
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.
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:
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):
Put my nose in the right direction, please?
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?