lua-intf
lua-intf copied to clipboard
Wrong result after catch(...)
I am trying to catch exception from LuaRef::toValue().
#include <LuaIntf/LuaIntf.h>
using LuaIntf::LuaRef;
std::tuple<LuaRef, std::string> TestToStr(const LuaRef& luaVal)
{
try{
std::string s(luaVal.toValue<const char*>());
return std::make_tuple(LuaRef::fromValue(
luaVal.state(), "Hello, " + s), "OK");
}
catch(...)
{
}
return std::make_tuple(LuaRef(luaVal.state(), nullptr), "Exception");
}
extern "C"
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__CODEGEARC__)
__declspec(dllexport)
#endif
int luaopen_lfb(lua_State* L)
{
using namespace LuaIntf;
LuaRef mod = LuaRef::createTable(L);
LuaBinding(mod)
.addFunction("test_to_str", &TestToStr);
mod.pushToStack();
return 1;
}
But the result is wrong after catch.
E:\Git\Lua\lua-flatbuffers_jinq0123\test>lua53pp
Lua 5.3.2 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> package.cpath = package.cpath .. ";../bin/Debug/?.dll"
> lfb = require("lfb")
> lfb.test_to_str(true)
Exception
>
Expected:
nil Exception
Got:
Exception
Why?
Lua 5.3.2 is build as cpp.
More test:
> inspect = require("inspect")
> t = {lfb.test_to_str(true)}
(null)
> inspect(t)
{
[2] = "Exception"
}
> t2 = {lfb.test_to_str(1234)}
> inspect(t2)
{ "Hello, 1234", "OK" }
>
It seems normal.
null is special, assign null to table will not create the entry.
try
err, desc = lfb.test_to_str(true)
to get the first arg.
Maybe it's the problem of Lua console.
If delete try/catch:
std::tuple<LuaRef, std::string> TestToStr(const LuaRef& luaVal)
{
return std::make_tuple(LuaRef(luaVal.state(), nullptr), "Exception");
}
the console will output as expected:
> lfb.test_to_str(true)
nil Exception
If any exception, the console outputs differently.
In lua.c:
/*
** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
** print any results.
*/
static void doREPL (lua_State *L) {
...
while ((status = loadline(L)) != -1) {
if (status == LUA_OK)
status = docall(L, 0, LUA_MULTRET);
if (status == LUA_OK) l_print(L);
else report(L, status);
}
...
}
If any exception happened, the status will be bad.