@@ -79,7 +79,7 @@ TRAMPOLINE(table_add_row, add_row)
7979TRAMPOLINE(table_columns, columns)
8080TRAMPOLINE(table_tostring, __tostring)
8181
82- static char const *const osm2pgsql_table_name = "osm2pgsql.table ";
82+ static char const *const osm2pgsql_table_name = "osm2pgsql.Table ";
8383static char const *const osm2pgsql_object_metatable =
8484 " osm2pgsql.object_metatable" ;
8585
@@ -1711,6 +1711,31 @@ output_flex_t::output_flex_t(
17111711 }
17121712}
17131713
1714+ /* *
1715+ * Define the osm2pgsql.Table class/metatable.
1716+ */
1717+ static void init_table_class (lua_State *lua_state)
1718+ {
1719+ lua_getglobal (lua_state, " osm2pgsql" );
1720+ if (luaL_newmetatable (lua_state, osm2pgsql_table_name) != 1 ) {
1721+ throw std::runtime_error{" Internal error: Lua newmetatable failed." };
1722+ }
1723+ lua_pushvalue (lua_state, -1 ); // Copy of new metatable
1724+
1725+ // Add metatable as osm2pgsql.Table so we can access it from Lua
1726+ lua_setfield (lua_state, -3 , " Table" );
1727+
1728+ // Now add functions to metatable
1729+ lua_pushvalue (lua_state, -1 );
1730+ lua_setfield (lua_state, -2 , " __index" );
1731+ luaX_add_table_func (lua_state, " __tostring" , lua_trampoline_table_tostring);
1732+ luaX_add_table_func (lua_state, " add_row" , lua_trampoline_table_add_row);
1733+ luaX_add_table_func (lua_state, " name" , lua_trampoline_table_name);
1734+ luaX_add_table_func (lua_state, " schema" , lua_trampoline_table_schema);
1735+ luaX_add_table_func (lua_state, " cluster" , lua_trampoline_table_cluster);
1736+ luaX_add_table_func (lua_state, " columns" , lua_trampoline_table_columns);
1737+ }
1738+
17141739void output_flex_t::init_lua (std::string const &filename)
17151740{
17161741 m_lua_state.reset (luaL_newstate (),
@@ -1739,19 +1764,7 @@ void output_flex_t::init_lua(std::string const &filename)
17391764
17401765 lua_setglobal (lua_state (), " osm2pgsql" );
17411766
1742- // Define "osmpgsql.table" metatable
1743- if (luaL_newmetatable (lua_state (), osm2pgsql_table_name) != 1 ) {
1744- throw std::runtime_error{" Internal error: Lua newmetatable failed." };
1745- }
1746- lua_pushvalue (lua_state (), -1 );
1747- lua_setfield (lua_state (), -2 , " __index" );
1748- luaX_add_table_func (lua_state (), " __tostring" ,
1749- lua_trampoline_table_tostring);
1750- luaX_add_table_func (lua_state (), " add_row" , lua_trampoline_table_add_row);
1751- luaX_add_table_func (lua_state (), " name" , lua_trampoline_table_name);
1752- luaX_add_table_func (lua_state (), " schema" , lua_trampoline_table_schema);
1753- luaX_add_table_func (lua_state (), " cluster" , lua_trampoline_table_cluster);
1754- luaX_add_table_func (lua_state (), " columns" , lua_trampoline_table_columns);
1767+ init_table_class (lua_state ());
17551768
17561769 // Clean up stack
17571770 lua_settop (lua_state (), 0 );
0 commit comments