4

I use luaVM in another programming language (Vala) and want to pass the code from vala to lua so that lua returns the result of executing this code as a string.

> s2="print (5+5)"
> s2
print (5+5)
> loadstring(s2)
function: 0x55834153f080
> func = loadstring(s2)
> func()
10

tried a lot of things, but I did not succeed, that is, I need instead of 10 was a variable of type string containing 10. So I could do vm.to_string(-1) of Vala and to obtain "10"

2
  • 3
    Try return tostring(5+5) Commented Jan 7, 2019 at 8:30
  • Remember that you can also call the code in one line with local str = loadstring(s2)(). Commented Jan 7, 2019 at 21:55

1 Answer 1

4

As Egor stated you can cast your result to a string before returning it using tostring.

I would also add you may want to use dostring not loadstring.

A load function in lua means to compile and not run the chunk, instead it returns a function when called will run the chunk.(loadfile, loadstring)

A do function will compile and run the contents.(dofile, dostring)

The details can be found here: Lua: 8 – Compilation, Execution, and Errors

Like dofile, loadfile also loads a Lua chunk from a file, but it does not run the chunk. Instead, it only compiles the chunk and returns the compiled chunk as a function.

This section speak more directly to loadfile but the page covers loadstring.


if dostring is not defined it can be like so:

function dostring(s)
    return assert(loadstring(s))()
end

If you are using a version of lua later then 5.1 loadstring becomes load

Sign up to request clarification or add additional context in comments.

7 Comments

I think I need to share the code for better understanding. The interface consists of 2 TextView. On the idea you write lua code on the right and see the result on the left. But now the result of the execution is displayed in the console, and on the right only: [string "print (5..."]:2: ')' expected (to close '(' at line 1) near <eof> pastebin.com/uhQhR0vT
It seems as if now the last element of string in vm is its errors when the string has not yet been entered to the end. And the correct result of execution gets to the console. I think this is so because on the left it is impossible to see the error associated with the input print(5), (because it is not an error). There either "[string " print (5..."]: 2:') ' expected (to close '(' at line 1" or "[string "print (5))..."]: 2:') 'unexpected symbol near ')'"
line 49 return tostring dostring("..inputCode..") should look something like this return tostring(dostring("..inputCode..") or 'nil'). you are missing () from your tostring function around the dostring
dostring("..inputCode..") or 'nil' the or nil returns a default value to the tostring function if the inputCode does not return a value. This will prevent an errors from tostring when this happens. the value could be anything it does not need to be nil it could be 0 if that is better
All still the same, recorded a short screencast of how it looks. youtu.be/2SvNORmAu7A
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.