Skip to content

Commit 8c50415

Browse files
committed
Move lua_pcall wrapper function to utils
1 parent 7873b50 commit 8c50415

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

src/lua-utils.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,34 @@ bool luaX_get_table_bool(lua_State *lua_state, char const *key, int table_index,
135135
throw std::runtime_error{
136136
"{} must contain a '{}' boolean field"_format(error_msg, key)};
137137
}
138+
139+
static int pcall_error_traceback_handler(lua_State *lua_state)
140+
{
141+
assert(lua_state);
142+
143+
char const *msg = lua_tostring(lua_state, 1);
144+
if (msg == nullptr) { // is error object not a string?
145+
if (luaL_callmeta(lua_state, 1,
146+
"__tostring") && // does it have a metamethod
147+
lua_type(lua_state, -1) == LUA_TSTRING) { // that produces a string?
148+
return 1; // that is the message
149+
} else {
150+
msg = lua_pushfstring(lua_state, "(error object is a %s value)",
151+
luaL_typename(lua_state, 1));
152+
}
153+
}
154+
luaL_traceback(lua_state, lua_state, msg, 1); // append a standard traceback
155+
return 1; // return the traceback
156+
}
157+
158+
// wrapper function for lua_pcall to include a stack traceback
159+
int luaX_pcall(lua_State *lua_state, int narg, int nres)
160+
{
161+
int const base = lua_gettop(lua_state) - narg; // function index
162+
lua_pushcfunction(lua_state,
163+
pcall_error_traceback_handler); // push message handler
164+
lua_insert(lua_state, base); // put it under function and args
165+
int const status = lua_pcall(lua_state, narg, nres, base);
166+
lua_remove(lua_state, base); // remove message handler from the stack
167+
return status;
168+
}

src/lua-utils.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ char const *luaX_get_table_string(lua_State *lua_state, char const *key,
4949
bool luaX_get_table_bool(lua_State *lua_state, char const *key, int table_index,
5050
char const *error_msg, bool default_value);
5151

52+
int luaX_pcall(lua_State *lua_state, int narg, int nres);
53+
5254
#endif // OSM2PGSQL_FLEX_LUA_HPP

src/output-flex.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -983,35 +983,6 @@ void output_flex_t::add_row(table_connection_t *table_connection,
983983
}
984984
}
985985

986-
static int msghandler(lua_State *lua_state)
987-
{
988-
assert(lua_state);
989-
990-
char const *msg = lua_tostring(lua_state, 1);
991-
if (msg == nullptr) { // is error object not a string?
992-
if (luaL_callmeta(lua_state, 1, "__tostring") && // does it have a metamethod
993-
lua_type(lua_state, -1) == LUA_TSTRING) { // that produces a string?
994-
return 1; // that is the message
995-
}
996-
else {
997-
msg = lua_pushfstring(lua_state, "(error object is a %s value)",
998-
luaL_typename(lua_state, 1));
999-
}
1000-
}
1001-
luaL_traceback(lua_state, lua_state, msg, 1); // append a standard traceback
1002-
return 1; // return the traceback
1003-
}
1004-
1005-
int output_flex_t::do_pcall(int narg, int nres)
1006-
{
1007-
int const base = lua_gettop(lua_state()) - narg; // function index
1008-
lua_pushcfunction(lua_state(), msghandler); // push message handler
1009-
lua_insert(lua_state(), base); // put it under function and args
1010-
int status = lua_pcall(lua_state(), narg, nres, base);
1011-
lua_remove(lua_state(), base); // remove message handler from the stack
1012-
return status;
1013-
}
1014-
1015986
void output_flex_t::call_process_function(int index,
1016987
osmium::OSMObject const &object)
1017988
{
@@ -1025,7 +996,7 @@ void output_flex_t::call_process_function(int index,
1025996
get_options()->extra_attributes); // the single argument
1026997

1027998
luaX_set_context(lua_state(), this);
1028-
if (do_pcall(1, 0)) {
999+
if (luaX_pcall(lua_state(), 1, 0)) {
10291000
throw std::runtime_error{"Failed to execute lua processing function:"
10301001
" {}"_format(lua_tostring(lua_state(), -1))};
10311002
}

src/output-flex.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ class output_flex_t : public output_t
100100
void init_clone();
101101

102102
void call_process_function(int index, osmium::OSMObject const &object);
103-
int do_pcall(int narg, int nres);
104103

105104
void init_lua(std::string const &filename);
106105

0 commit comments

Comments
 (0)