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.
-
1How 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++.David Ching– David Ching2015-08-12 21:06:02 +00:00Commented Aug 12, 2015 at 21:06
Add a comment
|
1 Answer
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