Merge pull request #3631 from lethosor/lua-new-catch-alloc-errors

df.new(): catch errors thrown by allocate()
develop
Myk 2023-08-04 14:39:02 -07:00 committed by GitHub
commit 98e0ce19fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

@ -46,6 +46,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## Internals ## Internals
## Lua ## Lua
- ``new()``: improved error handling so that certain errors that were previously uncatchable (creating objects with members with unknown vtables) are now catchable with ``pcall()``
## Removed ## Removed

@ -685,7 +685,8 @@ static int meta_new(lua_State *state)
type_identity *id = get_object_identity(state, 1, "df.new()", true); type_identity *id = get_object_identity(state, 1, "df.new()", true);
void *ptr; void *ptr = nullptr;
std::string err_context;
// Support arrays of primitive types // Support arrays of primitive types
if (argc == 2) if (argc == 2)
@ -703,11 +704,22 @@ static int meta_new(lua_State *state)
} }
else else
{ {
ptr = id->allocate(); try {
ptr = id->allocate();
}
catch (std::exception &e) {
if (e.what()) {
err_context = e.what();
}
}
} }
if (!ptr) if (!ptr)
luaL_error(state, "Cannot allocate %s", id->getFullName().c_str()); luaL_error(state, "Cannot allocate %s%s%s",
id->getFullName().c_str(),
err_context.empty() ? "" : ": ",
err_context.c_str()
);
if (lua_isuserdata(state, 1)) if (lua_isuserdata(state, 1))
{ {