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

df.new(): catch errors thrown by allocate() #3631

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## Internals

## 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

Expand Down
18 changes: 15 additions & 3 deletions library/LuaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ static int meta_new(lua_State *state)

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
if (argc == 2)
Expand All @@ -703,11 +704,22 @@ static int meta_new(lua_State *state)
}
else
{
ptr = id->allocate();
try {
ptr = id->allocate();
}
catch (std::exception &e) {
if (e.what()) {
err_context = e.what();
}
}
}

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))
{
Expand Down