[Dev-luatex] <texmf>/luatex is coming

Jonathan Sauer Jonathan.Sauer at silverstroke.com
Wed Jan 7 11:40:42 CET 2009


Hello, 

> > First the warning, for Hans in particular: make sure you start using
> > require() for all of the external lua modules that are embedded in
> > luatex. As soon as this new subsystem is in place, you can no longer
> > use functions like socket.bind() without require-ing the 
> > module first!
> 
> ok, no problem for the scripts, but i have to figure out a 
> way to embed them in the format

I use a custom package loader and bytecode registers. A toks register
is used to map Lua module names to the number of the bytecode register
the module is stored in. When require()ing a module, this toks register
is checked:

- Module exists: The bytecode register is executed.
- Modules does not exist: A new bytecode register is reserved, the
  module loaded using loadfile(), executed and stored in the bytecode
  register. Finally, the mapping is added to the toks register.

That does it. I also have a macro to do

	<global module var> = require("<module name>")

and add code to \everyjob to do this again at the start of the TeX run
if called in IniTeX.


Maybe this helps,
Jonathan

P.S: My loader is (there is a toks register \LT:T at bytecode@files which
stores the mapping from module name to bytecode register, and a count
register \LT:C at next@bytecode which stores the number of the next unused
bytecode register; ":" and "@" are letters):

local escapeChars = {
  ['^'] = '%^',
  ['$'] = '%$',
  ['('] = '%(',
  [')'] = '%)',
  ['%'] = '%%',
  ['.'] = '%.',
  ['['] = '%[',
  [']'] = '%]',
  ['*'] = '%*',
  ['+'] = '%+',
  ['-'] = '%-',
  ['?'] = '%?'
}

local function loadfile(file, name)
  local f, e = io.open(file)
  if not f then
    return f, e
  end
  local s = f:read('*a')
  f:close()
  return loadstring(s, '@' .. name)
end

local function loader(moduleName)
  local fn = moduleName .. '.klua'
  
  local t = tex.toks['LT:T at bytecode@files']
  local f, n = t:match('|(' .. fn:gsub('.', escapeChars) ..
')|=|([0-9]+)|')
  
  if f then
    texio.write_nl('')
    texio.write('(', fn, ' [from register ', n, '])')
    return lua.bytecode[n]
  else
    rn = kpse.find_file(fn, 'tex', true)
    if rn then
      f, e = loadfile(rn, moduleName)
      if f then
        if tex.formatname == '' then
          local r = tex.count['LT:C at next@bytecode']
          tex.count['LT:C at next@bytecode'] = r + 1
          
          texio.write_nl('')
          texio.write('(', rn, ' [cached in register ', r, '])')
          lua.bytecode[r] = f
          tex.toks['LT:T at bytecode@files'] = t .. '|' .. fn .. '|=|' .. r
.. '|'
        else
          texio.write_nl('')
          texio.write('(', rn, ')')
        end
        return f
      else
        return 'Loader error: ' .. e
      end
    else
      return 'Loader error: Could not find file'
    end
  end
end

package.loaders = { loader }



More information about the dev-luatex mailing list