Skip to content

Commit 6a15ebd

Browse files
authored
Merge pull request #2238 from joto/lua-misc
Misc Lua refactorings
2 parents 0de33f5 + c337135 commit 6a15ebd

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/lua-utils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ void *luaX_get_context(lua_State *lua_state) noexcept
5858

5959
#endif
6060

61+
void luaX_pushstring(lua_State *lua_state, std::string_view str) noexcept
62+
{
63+
lua_pushlstring(lua_state, str.data(), str.size());
64+
}
65+
6166
void luaX_add_table_str(lua_State *lua_state, char const *key,
6267
char const *value) noexcept
6368
{

src/lua-utils.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
#include <cassert>
1919
#include <cstdint>
20+
#include <string_view>
2021
#include <utility>
2122

2223
void luaX_set_context(lua_State *lua_state, void *ptr) noexcept;
2324
void *luaX_get_context(lua_State *lua_state) noexcept;
2425

26+
void luaX_pushstring(lua_State *lua_state, std::string_view str) noexcept;
27+
2528
void luaX_add_table_str(lua_State *lua_state, char const *key,
2629
char const *value) noexcept;
2730
void luaX_add_table_str(lua_State *lua_state, char const *key,

src/output-flex.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <mutex>
4848
#include <stdexcept>
4949
#include <string>
50+
#include <string_view>
5051

5152
// Mutex used to coordinate access to Lua code
5253
static std::mutex lua_mutex;
@@ -97,9 +98,6 @@ TRAMPOLINE(expire_output_schema, schema)
9798
TRAMPOLINE(expire_output_table, table)
9899
TRAMPOLINE(expire_output_tostring, __tostring)
99100

100-
static char const *const osm2pgsql_object_metatable =
101-
"osm2pgsql.object_metatable";
102-
103101
prepared_lua_function_t::prepared_lua_function_t(lua_State *lua_state,
104102
calling_context context,
105103
char const *name, int nresults)
@@ -125,6 +123,8 @@ prepared_lua_function_t::prepared_lua_function_t(lua_State *lua_state,
125123

126124
namespace {
127125

126+
std::string_view const osm2pgsql_object_metatable = "osm2pgsql.OSMObject";
127+
128128
void push_osm_object_to_lua_stack(lua_State *lua_state,
129129
osmium::OSMObject const &object)
130130
{
@@ -191,7 +191,7 @@ void push_osm_object_to_lua_stack(lua_State *lua_state,
191191
lua_rawset(lua_state, -3);
192192

193193
// Set the metatable of this object
194-
lua_pushlightuserdata(lua_state, (void *)osm2pgsql_object_metatable);
194+
luaX_pushstring(lua_state, osm2pgsql_object_metatable);
195195
lua_gettable(lua_state, LUA_REGISTRYINDEX);
196196
lua_setmetatable(lua_state, -2);
197197
}
@@ -530,7 +530,7 @@ int output_flex_t::table_tostring()
530530
auto const &table = get_table_from_param();
531531

532532
std::string const str{fmt::format("osm2pgsql.Table[{}]", table.name())};
533-
lua_pushstring(lua_state(), str.c_str());
533+
luaX_pushstring(lua_state(), str);
534534

535535
return 1;
536536
}
@@ -689,8 +689,8 @@ int output_flex_t::table_insert()
689689
} catch (not_null_exception const &e) {
690690
copy_mgr->rollback_line();
691691
lua_pushboolean(lua_state(), false);
692-
lua_pushstring(lua_state(), "null value in not null column.");
693-
lua_pushstring(lua_state(), e.column().name().c_str());
692+
lua_pushliteral(lua_state(), "null value in not null column.");
693+
luaX_pushstring(lua_state(), e.column().name());
694694
push_osm_object_to_lua_stack(lua_state(), object);
695695
table_connection.increment_not_null_error_counter();
696696
return 4;
@@ -730,14 +730,14 @@ int output_flex_t::table_columns()
730730
int output_flex_t::table_name()
731731
{
732732
auto const &table = get_table_from_param();
733-
lua_pushstring(lua_state(), table.name().c_str());
733+
luaX_pushstring(lua_state(), table.name());
734734
return 1;
735735
}
736736

737737
int output_flex_t::table_schema()
738738
{
739739
auto const &table = get_table_from_param();
740-
lua_pushstring(lua_state(), table.schema().c_str());
740+
luaX_pushstring(lua_state(), table.schema());
741741
return 1;
742742
}
743743

@@ -758,7 +758,7 @@ int output_flex_t::expire_output_tostring()
758758
expire_output.minzoom(), expire_output.maxzoom(),
759759
expire_output.filename(), expire_output.schema(),
760760
expire_output.table());
761-
lua_pushstring(lua_state(), str.c_str());
761+
luaX_pushstring(lua_state(), str);
762762

763763
return 1;
764764
}
@@ -783,23 +783,23 @@ int output_flex_t::expire_output_filename()
783783
{
784784
auto const &expire_output = get_expire_output_from_param();
785785

786-
lua_pushstring(lua_state(), expire_output.filename().c_str());
786+
luaX_pushstring(lua_state(), expire_output.filename());
787787
return 1;
788788
}
789789

790790
int output_flex_t::expire_output_schema()
791791
{
792792
auto const &expire_output = get_expire_output_from_param();
793793

794-
lua_pushstring(lua_state(), expire_output.schema().c_str());
794+
luaX_pushstring(lua_state(), expire_output.schema());
795795
return 1;
796796
}
797797

798798
int output_flex_t::expire_output_table()
799799
{
800800
auto const &expire_output = get_expire_output_from_param();
801801

802-
lua_pushstring(lua_state(), expire_output.table().c_str());
802+
luaX_pushstring(lua_state(), expire_output.table());
803803
return 1;
804804
}
805805

@@ -1338,7 +1338,7 @@ void output_flex_t::init_lua(std::string const &filename,
13381338

13391339
luaX_add_table_int(lua_state(), "stage", 1);
13401340

1341-
lua_pushstring(lua_state(), "properties");
1341+
lua_pushliteral(lua_state(), "properties");
13421342
lua_createtable(lua_state(), 0, (int)properties.size());
13431343
for (auto const &property : properties) {
13441344
luaX_add_table_str(lua_state(), property.first.c_str(),
@@ -1370,6 +1370,8 @@ void output_flex_t::init_lua(std::string const &filename,
13701370

13711371
// Store the methods on OSM objects in its metatable.
13721372
lua_getglobal(lua_state(), "object_metatable");
1373+
luaX_pushstring(lua_state(), osm2pgsql_object_metatable);
1374+
lua_setfield(lua_state(), -2, "__name");
13731375
lua_getfield(lua_state(), -1, "__index");
13741376
luaX_add_table_func(lua_state(), "get_bbox", lua_trampoline_app_get_bbox);
13751377
luaX_add_table_func(lua_state(), "as_linestring",
@@ -1391,7 +1393,7 @@ void output_flex_t::init_lua(std::string const &filename,
13911393
// Store the global object "object_metatable" defined in the init.lua
13921394
// script in the registry and then remove the global object. It will
13931395
// later be used as metatable for OSM objects.
1394-
lua_pushlightuserdata(lua_state(), (void *)osm2pgsql_object_metatable);
1396+
luaX_pushstring(lua_state(), osm2pgsql_object_metatable);
13951397
lua_getglobal(lua_state(), "object_metatable");
13961398
lua_settable(lua_state(), LUA_REGISTRYINDEX);
13971399
lua_pushnil(lua_state());

0 commit comments

Comments
 (0)