Skip to content

Commit 48a6bd4

Browse files
committed
Simplify setup of OSMObject Lua metatable
Uses the same mechanism as the other metatables now.
1 parent 9ba4731 commit 48a6bd4

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

src/init.lua

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,16 @@ function osm2pgsql.split_string(str, separator)
176176
return result
177177
end
178178

179-
-- This will be the metatable for the OSM objects given to the process callback
180-
-- functions.
181-
object_metatable = {
182-
__index = {
183-
grab_tag = function(data, tag)
184-
if not tag then
185-
error("Missing tag key", 2)
186-
end
187-
local v = data.tags[tag]
188-
data.tags[tag] = nil
189-
return v
179+
if osm2pgsql.OSMObject then
180+
osm2pgsql.OSMObject.__index.grab_tag = function(data, tag)
181+
if not tag then
182+
error("Missing tag key", 2)
190183
end
191-
}
192-
}
184+
local v = data.tags[tag]
185+
data.tags[tag] = nil
186+
return v
187+
end
188+
end
193189

194190
-- This is used to iterate over (multi)geometries.
195191
function osm2pgsql.Geometry.geometries(geom)

src/output-flex.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,17 +1305,17 @@ void output_flex_t::init_lua(std::string const &filename,
13051305

13061306
assert(lua_gettop(lua_state()) == 0);
13071307

1308-
// Load compiled in init.lua
1309-
if (luaL_dostring(lua_state(), lua_init())) {
1310-
throw fmt_error("Internal error in Lua setup: {}.",
1311-
lua_tostring(lua_state(), -1));
1308+
lua_getglobal(lua_state(), "osm2pgsql");
1309+
if (luaL_newmetatable(lua_state(), OSM2PGSQL_OSMOBJECT_CLASS) != 1) {
1310+
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
13121311
}
1312+
lua_pushvalue(lua_state(), -1); // Copy of new metatable
1313+
1314+
// Add metatable as osm2pgsql.OSMObject so we can access it from Lua
1315+
lua_setfield(lua_state(), -3, "OSMObject");
13131316

1314-
// Store the methods on OSM objects in its metatable.
1315-
lua_getglobal(lua_state(), "object_metatable");
1316-
lua_pushstring(lua_state(), OSM2PGSQL_OSMOBJECT_CLASS);
1317-
lua_setfield(lua_state(), -2, "__name");
1318-
lua_getfield(lua_state(), -1, "__index");
1317+
lua_pushvalue(lua_state(), -1);
1318+
lua_setfield(lua_state(), -2, "__index");
13191319
luaX_add_table_func(lua_state(), "get_bbox", lua_trampoline_app_get_bbox);
13201320
luaX_add_table_func(lua_state(), "as_linestring",
13211321
lua_trampoline_app_as_linestring);
@@ -1333,16 +1333,11 @@ void output_flex_t::init_lua(std::string const &filename,
13331333
lua_trampoline_app_as_geometrycollection);
13341334
lua_settop(lua_state(), 0);
13351335

1336-
// Store the global object "object_metatable" defined in the init.lua
1337-
// script in the registry and then remove the global object. It will
1338-
// later be used as metatable for OSM objects.
1339-
lua_pushstring(lua_state(), OSM2PGSQL_OSMOBJECT_CLASS);
1340-
lua_getglobal(lua_state(), "object_metatable");
1341-
lua_settable(lua_state(), LUA_REGISTRYINDEX);
1342-
lua_pushnil(lua_state());
1343-
lua_setglobal(lua_state(), "object_metatable");
1344-
1345-
assert(lua_gettop(lua_state()) == 0);
1336+
// Load compiled in init.lua
1337+
if (luaL_dostring(lua_state(), lua_init())) {
1338+
throw fmt_error("Internal error in Lua setup: {}.",
1339+
lua_tostring(lua_state(), -1));
1340+
}
13461341

13471342
// Load user config file
13481343
luaX_set_context(lua_state(), this);

0 commit comments

Comments
 (0)