[Dev-luatex] Callbacks in Lua wrapper of mplib

Marcel Krüger tex at 2krueger.de
Thu Nov 8 16:02:06 CET 2018

 ---- On Wed, 07 Nov 2018 13:00:50 +0100 luigi scarso <luigi.scarso at gmail.com> wrote ----  
 > On Wed, Nov 7, 2018 at 12:18 PM Marcel Krüger <tex at 2krueger.de> wrote: 
 > Hi, 
 >  I recently discovered that the Lua wrapper of mplib registers all callbacks globally 
 >  such that creating multiple mplib instances with different callbacks breaks. 
 > why not this ? 
 >  \directlua{      local current_instance        local custom_find_file =  custom_find_file or {}      local mp_find_file_driver = function(name, mode, type) print("I am ",current_instance) if custom_find_file[current_instance] then return custom_find_file[current_instance](name, mode, type) else  return  "unregistered" end end       custom_find_file['mp1'] = function(name, mode, type) return name end      custom_find_file['mp2'] = function(name, mode, type) if name == 'cmr10' then print'This should not happen' end return name end  
 >       local mp1 = mplib.new{         find_file = mp_find_file_driver      } 
 >       local mp2 = mplib.new{          find_file = mp_find_file_driver        }      current_instance = "mp1"      mp1:execute[[        input cmr10;      ]] 
 >       current_instance = "mp2"      mp2:execute[[        input cmr10;      ]] 
 >     }    \bye 
That's similar to the workaround I'm using right now, but it seems odd: 
The callbacks are given as in the initializer table of a single instance, 
so you do not expect them to affect other instances. Especially this means that if anyone 
uses "short-living" MPLib instances, this person will probably not expect this behaviour 
and just pass the find_file method directly. 
This means that every user of "long-living" MPLib instances has to fear that every 
called library might change this global state. 
If this happens, there does not even seem to be a way to change it back, so the safest  
approach would be to call mplib.new and create and destroy a temporary instance 
every time `:execute` or `:finish` is called. 
So if this is the intended behaviour, IMO a note should be added to 
the documentation. Especially because every non-function entry in 
the initializer  only affects this instance. 
But I still think it should be changed: The current behaviour seems unintuitive and inconsistant. 
Also manually keeping track of the current instance seems at least odd if mplib can access this information anyway. 
Additionally the change would make the Lua API of mplib more similar to the  C API. 
Best regards 
Marcel Krüger 

More information about the dev-luatex mailing list