@@ -69,8 +69,8 @@ static char const osm2pgsql_table_name[] = "osm2pgsql.table";
6969static char const osm2pgsql_object_metatable[] = " osm2pgsql.object_metatable" ;
7070
7171prepared_lua_function_t ::prepared_lua_function_t (lua_State *lua_state,
72- char const *name ,
73- int nresults)
72+ calling_context context ,
73+ char const *name, int nresults)
7474{
7575 int const index = lua_gettop (lua_state);
7676
@@ -80,6 +80,7 @@ prepared_lua_function_t::prepared_lua_function_t(lua_State *lua_state,
8080 m_index = index;
8181 m_name = name;
8282 m_nresults = nresults;
83+ m_calling_context = context;
8384 return ;
8485 }
8586
@@ -540,6 +541,12 @@ std::size_t output_flex_t::get_way_nodes()
540541
541542int output_flex_t::app_get_bbox ()
542543{
544+ if (m_calling_context != calling_context::process_node &&
545+ m_calling_context != calling_context::process_way) {
546+ throw std::runtime_error{" The function get_bbox() can only be called"
547+ " from process_node() or process_way()" };
548+ }
549+
543550 if (lua_gettop (lua_state ()) > 1 ) {
544551 throw std::runtime_error{" No parameter(s) needed for get_box()" };
545552 }
@@ -726,9 +733,9 @@ void output_flex_t::setup_flex_table_columns(flex_table_t *table)
726733
727734int output_flex_t::app_define_table ()
728735{
729- if (m_context_node || m_context_way || m_context_relation ) {
730- throw std::runtime_error{" Tables have to be defined before calling any "
731- " of the process callbacks" };
736+ if (m_calling_context != calling_context::main ) {
737+ throw std::runtime_error{" Database tables have to be defined in the "
738+ " main Lua code, not in any of the callbacks" };
732739 }
733740
734741 luaL_checktype (lua_state (), 1 , LUA_TTABLE);
@@ -791,8 +798,17 @@ int output_flex_t::table_tostring()
791798
792799int output_flex_t::table_add_row ()
793800{
801+ if (m_calling_context != calling_context::process_node &&
802+ m_calling_context != calling_context::process_way &&
803+ m_calling_context != calling_context::process_relation) {
804+ throw std::runtime_error{
805+ " The function add_row() can only be called from the "
806+ " process_node/way/relation() functions" };
807+ }
808+
794809 if (lua_gettop (lua_state ()) != 2 ) {
795- throw std::runtime_error{" Need two parameters: The osm2pgsql.table and the row data" };
810+ throw std::runtime_error{
811+ " Need two parameters: The osm2pgsql.table and the row data" };
796812 }
797813
798814 auto &table_connection =
@@ -823,9 +839,6 @@ int output_flex_t::table_add_row()
823839 " Trying to add relation to table '{}'" _format (table.name ())};
824840 }
825841 add_row (&table_connection, *m_context_relation);
826- } else {
827- throw std::runtime_error{" The add_row() function can only be called "
828- " from inside a process function" };
829842 }
830843
831844 return 0 ;
@@ -1009,6 +1022,8 @@ void output_flex_t::call_lua_function(prepared_lua_function_t func,
10091022{
10101023 std::lock_guard<std::mutex> guard{lua_mutex};
10111024
1025+ m_calling_context = func.context ();
1026+
10121027 lua_pushvalue (lua_state (), func.index ()); // the function to call
10131028 push_osm_object_to_lua_stack (
10141029 lua_state (), object,
@@ -1020,6 +1035,8 @@ void output_flex_t::call_lua_function(prepared_lua_function_t func,
10201035 " Failed to execute Lua function 'osm2pgsql.{}':"
10211036 " {}" _format (func.name (), lua_tostring (lua_state (), -1 ))};
10221037 }
1038+
1039+ m_calling_context = calling_context::main;
10231040}
10241041
10251042void output_flex_t::pending_way (osmid_t id)
@@ -1318,10 +1335,12 @@ void output_flex_t::init_lua(std::string const &filename)
13181335 // Check whether the process_* functions are available and store them on
13191336 // the Lua stack for fast access later
13201337 lua_getglobal (lua_state (), " osm2pgsql" );
1321- m_process_node = prepared_lua_function_t {lua_state (), " process_node" };
1322- m_process_way = prepared_lua_function_t {lua_state (), " process_way" };
1323- m_process_relation =
1324- prepared_lua_function_t {lua_state (), " process_relation" };
1338+ m_process_node = prepared_lua_function_t {
1339+ lua_state (), calling_context::process_node, " process_node" };
1340+ m_process_way = prepared_lua_function_t {
1341+ lua_state (), calling_context::process_way, " process_way" };
1342+ m_process_relation = prepared_lua_function_t {
1343+ lua_state (), calling_context::process_relation, " process_relation" };
13251344
13261345 lua_remove (lua_state (), 1 ); // global "osm2pgsql"
13271346}
0 commit comments