vinniefalco/LuaBridge

Throw an error from a Property with location information included

Opened this issue · 1 comments

This is a followup to #293, which I have closed because it does not accurately describe the issue. I would like to throw an error out of c++ and have it appear as a runtime error in Lua with file and line number information. If I throw the error from a function created with addFunction, it works. However, if I throw the error from the same c++ function registered withaddProperty, I do not get the file and line number info.

I am using Lua 5.2.4, which I compile and embed directly into my program, along with LuaBridge.

Here is a test case that compiles and illustrates the issue:

static void throw_error()
{
   throw std::runtime_error("runtime error");
}

static void standalone_error()
{
   throw_error();
}

class error_class
{
public:
   int class_error() const
   {
      ::throw_error();
      return 0;
   }
};

void lua_connect(lua_State *L)
{
   luabridge::getGlobalNamespace(L)
      .beginNamespace("test")
         .addFunction("standalone_error", standalone_error)
         .beginClass<error_class>("error_class")
            .addConstructor<void (*) (void)>()
            .addFunction("class_error", &error_class::class_error)
            .addProperty("property_error", &error_class::class_error)
         .endClass()
      .endNamespace();
}

Here are three different ways to encounter this runtime error in Lua. Obviously, the first one it hits aborts the script, so you have to comment out the ones you don't want to see.

test.standalone_error() --> reports file and line number along with the error
local errorclass = test.error_class()
local x = errorclass:class_error() --> reports file and line number along with the error
local y = errorclass.property_error --> reports the error but no file or line number information

This is fixed in luabridge3