Skip to content

Commit 38c7325

Browse files
committed
Keep track of name and number of results in prepared_lua_function_t
Keep track of function name in prepared_lua_function_t to be able to output a better error message if something goes wrong in this function. Also stores the number of results we are expecting from this Lua function in prepared_lua_function_t. We'll need this later.
1 parent b6d02a4 commit 38c7325

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/output-flex.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,17 @@ static char const osm2pgsql_table_name[] = "osm2pgsql.table";
6969
static char const osm2pgsql_object_metatable[] = "osm2pgsql.object_metatable";
7070

7171
prepared_lua_function_t::prepared_lua_function_t(lua_State *lua_state,
72-
char const *name)
72+
char const *name,
73+
int nresults)
7374
{
7475
int const index = lua_gettop(lua_state);
7576

7677
lua_getfield(lua_state, 1, name);
7778

7879
if (lua_type(lua_state, -1) == LUA_TFUNCTION) {
7980
m_index = index;
81+
m_name = name;
82+
m_nresults = nresults;
8083
return;
8184
}
8285

@@ -1012,9 +1015,10 @@ void output_flex_t::call_lua_function(prepared_lua_function_t func,
10121015
get_options()->extra_attributes); // the single argument
10131016

10141017
luaX_set_context(lua_state(), this);
1015-
if (luaX_pcall(lua_state(), 1, 0)) {
1016-
throw std::runtime_error{"Failed to execute lua processing function:"
1017-
" {}"_format(lua_tostring(lua_state(), -1))};
1018+
if (luaX_pcall(lua_state(), 1, func.nresults())) {
1019+
throw std::runtime_error{
1020+
"Failed to execute Lua function 'osm2pgsql.{}':"
1021+
" {}"_format(func.name(), lua_tostring(lua_state(), -1))};
10181022
}
10191023
}
10201024

src/output-flex.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,30 @@ class prepared_lua_function_t
4343
/**
4444
* Get function with the name "osm2pgsql.name" from Lua and put pointer
4545
* to it on the Lua stack.
46+
*
47+
* \param lua_state Current Lua state.
48+
* \param name Name of the function.
49+
* \param nresults The number of results this function is supposed to have.
4650
*/
47-
prepared_lua_function_t(lua_State *lua_state, const char *name);
51+
prepared_lua_function_t(lua_State *lua_state, const char *name,
52+
int nresults = 0);
4853

4954
/// Return the index of the function on the Lua stack.
5055
int index() const noexcept { return m_index; }
5156

57+
/// The name of the function.
58+
char const* name() const noexcept { return m_name; }
59+
60+
/// The number of results this function is expected to have.
61+
int nresults() const noexcept { return m_nresults; }
62+
5263
/// Is this function defined in the users Lua code?
5364
operator bool() const noexcept { return m_index != 0; }
5465

5566
private:
67+
char const *m_name = nullptr;
5668
int m_index = 0;
69+
int m_nresults = 0;
5770
};
5871

5972
class output_flex_t : public output_t

0 commit comments

Comments
 (0)