@@ -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