Closed Flamefire closed 8 years ago
Did you tried setErrorHandler?
static void stderror_out(int status, const char* message)
{
std::cerr << message << std::endl;
}
this is default error handler. output to stderr only.
If you want need throw exception at luaerror. e.g.
kaguya::State state;
state.setErrorHandler([](int status, const char* message) {throw std::runtime_error(message); });
I'm talking about throwing an error to Lua. So it can be handled in Lua. I show you an example:
state["test"] = kaguya::function([]{ lua_pushstring(state.state(), "Lua!"); lua_error(state.state()); });
state(" if(pcall(test)) then \n print("OK"); \n else \n print("ERROR") \n end");//ERROR
state("test()");//Lua!
I think It is mistaken lua code. pcall is first argument require function. It is not function result.
state["test"] = kaguya::function([&] { lua_error(state.state()); });
state(" if(pcall(test)) then \n print('OK'); \n else \n print('ERROR') \n end");//ERROR
Yes thanks. I edited the code.
What I suggest in this issue is: Wrap the call to lua_error in kaguya so it is simply state.error("Lua!")
lua_error is longjmp at default compile. I hate setjmp/longjmp with C++. memory leak. strange crash. etc..
You can use throw exception.
state["test"] = kaguya::function([&] { throw std::runtime_error("Lua!"); });
state("test()");
message to error_handler
Lua!
stack traceback:
[C]: in function 'test'
[string "test()"]:1: in main chunk
It 's no good ?
Ah, so this exception is already caught by the lua error handler? Did not know that :)
Yes. catch all exception, and notify error to LuaVM. =)
As it is possible to call C++ functions from Lua and it is possible that this goes wrong, there should be a method to throw lua errors.
2 cases:
pcall
nil
but if e.g. this is a valid return value too or it is an execution function that you don't want to just 'do nothing' it is better to throw an error, that lua can catch and that will be passed to the error handler otherwise.However I did not find a function for calling lua_error in the wrapper (besides of course executing
state("error('Foo bar!!!')")
)Is there something for this? If not could it be added? What about the
io.openFile
like behaviour that it returns the file or nil and an error message? Can this be done easily with kaguya?