Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong result after catch(...) #130

Open
jinq0123 opened this issue Aug 3, 2016 · 3 comments
Open

Wrong result after catch(...) #130

jinq0123 opened this issue Aug 3, 2016 · 3 comments

Comments

@jinq0123
Copy link

jinq0123 commented Aug 3, 2016

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" }
>
@SteveKChiu
Copy link
Owner

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.

@jinq0123
Copy link
Author

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.

@jinq0123
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants