Skip to content

Commit

Permalink
Merge pull request #2238 from joto/lua-misc
Browse files Browse the repository at this point in the history
Misc Lua refactorings
  • Loading branch information
lonvia authored Aug 19, 2024
2 parents 0de33f5 + c337135 commit 6a15ebd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/lua-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ void *luaX_get_context(lua_State *lua_state) noexcept

#endif

void luaX_pushstring(lua_State *lua_state, std::string_view str) noexcept
{
lua_pushlstring(lua_state, str.data(), str.size());
}

void luaX_add_table_str(lua_State *lua_state, char const *key,
char const *value) noexcept
{
Expand Down
3 changes: 3 additions & 0 deletions src/lua-utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

#include <cassert>
#include <cstdint>
#include <string_view>
#include <utility>

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

void luaX_pushstring(lua_State *lua_state, std::string_view str) noexcept;

void luaX_add_table_str(lua_State *lua_state, char const *key,
char const *value) noexcept;
void luaX_add_table_str(lua_State *lua_state, char const *key,
Expand Down
32 changes: 17 additions & 15 deletions src/output-flex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <mutex>
#include <stdexcept>
#include <string>
#include <string_view>

// Mutex used to coordinate access to Lua code
static std::mutex lua_mutex;
Expand Down Expand Up @@ -97,9 +98,6 @@ TRAMPOLINE(expire_output_schema, schema)
TRAMPOLINE(expire_output_table, table)
TRAMPOLINE(expire_output_tostring, __tostring)

static char const *const osm2pgsql_object_metatable =
"osm2pgsql.object_metatable";

prepared_lua_function_t::prepared_lua_function_t(lua_State *lua_state,
calling_context context,
char const *name, int nresults)
Expand All @@ -125,6 +123,8 @@ prepared_lua_function_t::prepared_lua_function_t(lua_State *lua_state,

namespace {

std::string_view const osm2pgsql_object_metatable = "osm2pgsql.OSMObject";

void push_osm_object_to_lua_stack(lua_State *lua_state,
osmium::OSMObject const &object)
{
Expand Down Expand Up @@ -191,7 +191,7 @@ void push_osm_object_to_lua_stack(lua_State *lua_state,
lua_rawset(lua_state, -3);

// Set the metatable of this object
lua_pushlightuserdata(lua_state, (void *)osm2pgsql_object_metatable);
luaX_pushstring(lua_state, osm2pgsql_object_metatable);
lua_gettable(lua_state, LUA_REGISTRYINDEX);
lua_setmetatable(lua_state, -2);
}
Expand Down Expand Up @@ -530,7 +530,7 @@ int output_flex_t::table_tostring()
auto const &table = get_table_from_param();

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

return 1;
}
Expand Down Expand Up @@ -689,8 +689,8 @@ int output_flex_t::table_insert()
} catch (not_null_exception const &e) {
copy_mgr->rollback_line();
lua_pushboolean(lua_state(), false);
lua_pushstring(lua_state(), "null value in not null column.");
lua_pushstring(lua_state(), e.column().name().c_str());
lua_pushliteral(lua_state(), "null value in not null column.");
luaX_pushstring(lua_state(), e.column().name());
push_osm_object_to_lua_stack(lua_state(), object);
table_connection.increment_not_null_error_counter();
return 4;
Expand Down Expand Up @@ -730,14 +730,14 @@ int output_flex_t::table_columns()
int output_flex_t::table_name()
{
auto const &table = get_table_from_param();
lua_pushstring(lua_state(), table.name().c_str());
luaX_pushstring(lua_state(), table.name());
return 1;
}

int output_flex_t::table_schema()
{
auto const &table = get_table_from_param();
lua_pushstring(lua_state(), table.schema().c_str());
luaX_pushstring(lua_state(), table.schema());
return 1;
}

Expand All @@ -758,7 +758,7 @@ int output_flex_t::expire_output_tostring()
expire_output.minzoom(), expire_output.maxzoom(),
expire_output.filename(), expire_output.schema(),
expire_output.table());
lua_pushstring(lua_state(), str.c_str());
luaX_pushstring(lua_state(), str);

return 1;
}
Expand All @@ -783,23 +783,23 @@ int output_flex_t::expire_output_filename()
{
auto const &expire_output = get_expire_output_from_param();

lua_pushstring(lua_state(), expire_output.filename().c_str());
luaX_pushstring(lua_state(), expire_output.filename());
return 1;
}

int output_flex_t::expire_output_schema()
{
auto const &expire_output = get_expire_output_from_param();

lua_pushstring(lua_state(), expire_output.schema().c_str());
luaX_pushstring(lua_state(), expire_output.schema());
return 1;
}

int output_flex_t::expire_output_table()
{
auto const &expire_output = get_expire_output_from_param();

lua_pushstring(lua_state(), expire_output.table().c_str());
luaX_pushstring(lua_state(), expire_output.table());
return 1;
}

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

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

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

// Store the methods on OSM objects in its metatable.
lua_getglobal(lua_state(), "object_metatable");
luaX_pushstring(lua_state(), osm2pgsql_object_metatable);
lua_setfield(lua_state(), -2, "__name");
lua_getfield(lua_state(), -1, "__index");
luaX_add_table_func(lua_state(), "get_bbox", lua_trampoline_app_get_bbox);
luaX_add_table_func(lua_state(), "as_linestring",
Expand All @@ -1391,7 +1393,7 @@ void output_flex_t::init_lua(std::string const &filename,
// Store the global object "object_metatable" defined in the init.lua
// script in the registry and then remove the global object. It will
// later be used as metatable for OSM objects.
lua_pushlightuserdata(lua_state(), (void *)osm2pgsql_object_metatable);
luaX_pushstring(lua_state(), osm2pgsql_object_metatable);
lua_getglobal(lua_state(), "object_metatable");
lua_settable(lua_state(), LUA_REGISTRYINDEX);
lua_pushnil(lua_state());
Expand Down

0 comments on commit 6a15ebd

Please sign in to comment.