diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index f7d94183..2b168e85 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -240,6 +240,8 @@ If your Lua file causes an error due to mistaken syntax, you can test it at the `init_function(name, is_first)` and `exit_function` are called at the start and end of processing (once per thread). You can use this to output statistics or even to read a small amount of external data. `is_first` will be true only the first time `init_function` is called. +Tilemaker's functions (`Find`, `Holds`, `Layer`, `Attribute` and the others below) are global variables that are already set when the profile is loaded. So a profile can keep the ones it calls most in locals at its top, which Lua reaches faster than globals: `local Find, Holds, Layer = Find, Holds, Layer`. A function the profile defines with the same name as one of tilemaker's is replaced by tilemaker's. + Other functions are described below and in RELATIONS.md. ### Relations diff --git a/src/osm_lua_processing.cpp b/src/osm_lua_processing.cpp index ba34304e..2bc544bc 100644 --- a/src/osm_lua_processing.cpp +++ b/src/osm_lua_processing.cpp @@ -400,40 +400,8 @@ lua_State *OsmLuaProcessing::newLuaState(LuaPool &pool) { return L; } -OsmLuaProcessing::OsmLuaProcessing( - OSMStore &osmStore, - const class Config &configIn, - class LayerDefinition &layers, - const string &luaFile, - const class ShpMemTiles &shpMemTiles, - class OsmMemTiles &osmMemTiles, - AttributeStore &attributeStore, - class Declutter &declutter, - bool materializeGeometries, - bool isFirst) : - osmStore(osmStore), - luaStateHolder(newLuaState(luaPool)), - luaState(luaStateHolder.L), - shpMemTiles(shpMemTiles), - osmMemTiles(osmMemTiles), - attributeStore(attributeStore), - declutter(declutter), - config(configIn), - currentTags(NULL), - layers(layers), - materializeGeometries(materializeGeometries) { - - sigusr1Handler.initialize(); - if (vectorLayerMetadataMutexes.size() <= layers.layers.size()) { - vectorLayerMetadataMutexes.resize(layers.layers.size() + 1); - } - - // ---- Initialise Lua - g_luaState = &luaState; - luaState.setErrorHandler(lua_error_handler); - luaState.dofile(luaFile.c_str()); - - osmLuaProcessing = this; +// Register the functions a profile can call as Lua globals. +static void registerLuaFunctions(kaguya::State &luaState) { luaState["Id"] = &rawId; luaState["OsmType"] = &rawOsmType; luaState["AllKeys"] = &rawAllKeys; @@ -491,6 +459,47 @@ OsmLuaProcessing::OsmLuaProcessing( luaState["FindInRelation"] = &rawFindInRelation; luaState["SetData"] = &rawSetData; luaState["GetData"] = &rawGetData; +} + +OsmLuaProcessing::OsmLuaProcessing( + OSMStore &osmStore, + const class Config &configIn, + class LayerDefinition &layers, + const string &luaFile, + const class ShpMemTiles &shpMemTiles, + class OsmMemTiles &osmMemTiles, + AttributeStore &attributeStore, + class Declutter &declutter, + bool materializeGeometries, + bool isFirst) : + osmStore(osmStore), + luaStateHolder(newLuaState(luaPool)), + luaState(luaStateHolder.L), + shpMemTiles(shpMemTiles), + osmMemTiles(osmMemTiles), + attributeStore(attributeStore), + declutter(declutter), + config(configIn), + currentTags(NULL), + layers(layers), + materializeGeometries(materializeGeometries) { + + sigusr1Handler.initialize(); + if (vectorLayerMetadataMutexes.size() <= layers.layers.size()) { + vectorLayerMetadataMutexes.resize(layers.layers.size() + 1); + } + + // ---- Initialise Lua + g_luaState = &luaState; + luaState.setErrorHandler(lua_error_handler); + osmLuaProcessing = this; + // The built-in functions are registered before the profile is loaded, so that it can keep them in + // locals at the top (local Find, Layer = Find, Layer), and again afterwards, so that they still + // replace profile functions of the same name, as before. + registerLuaFunctions(luaState); + luaState.dofile(luaFile.c_str()); + registerLuaFunctions(luaState); + supportsRemappingShapefiles = !!luaState["attribute_function"]; supportsReadingRelations = !!luaState["relation_scan_function"]; supportsPostScanRelations = !!luaState["relation_postscan_function"];