2

I have a simple program which runs luaL_dofile on a lua file (lets call it 'script.lua', then checks for errors, then runs the file again, and so on and so forth. If at any point the program finds an error, it writes it in a log then terminates. The problem is, script.lua gets a user's input, then runs that string with loadstring. What I don't want to happen (and what does happen) is for loadstring to create an error (because the user inputted incorrect code), then the C++ program finds this error and terminates the program. In my mind, the best way to prevent the C++ program finding the error is by removing it from the stack, but I don't know how to access the lua stack from script.lua.

1
  • 1
    How is it the C++ program sees the stack with the 'loadString' or indeed any other function that your lua script has called? Those stack entries should be popped before control returns to C++. Commented Aug 12, 2015 at 21:06

1 Answer 1

2

Apparently the problem was me being silly all along. I changed my code so that it detects the error then prints it, instead of using assert

Examples for script.lua:

This doesn't work correctly:

local input = io.read()

assert(loadstring(input))()

This does work correctly:

local input = io.read()

output, error = loadstring(input)
if (output == nil) then
    print (error)
else
    output()
end
Sign up to request clarification or add additional context in comments.

Comments

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.