latex3 / latex2e

The LaTeX2e kernel
https://www.latex-project.org/
LaTeX Project Public License v1.3c
1.83k stars 251 forks source link

luadef/luafunction allocations in ltluatex #1100

Closed davidcarlisle closed 8 months ago

davidcarlisle commented 1 year ago

Brief outline of the bug

ltluatex allocates \luadef tokens in the classic style using a tex count \e@alloc@luafunction@count to count the allocations. the luatex manual and generic code shows an allocation using the first free slot in the Lua table,

  local lft = lua.get_functions_table()
  lft[\string#lft+1] = function()
 ...

The end result is that (for example) pgf and luapstricks over-write the same slots

see

https://tex.stackexchange.com/a/689524/1090

I don't think it is feasible to force generic code to use \e@alloc so I think the allocators should ensure the count being allocated is bigger than the table, something like

local function new_luafunction(name)
-- new
  if tex_count[luafunction_count_name] < table.getn(lua.get_functions_table()) then
  tex_setcount("global", luafunction_count_name,
                         table.getn(lua.get_functions_table())
-- end addition          
  tex_setcount("global", luafunction_count_name,
                         tex_count[luafunction_count_name] + 1)
  if tex_count[luafunction_count_name] > 65534 then
    luatexbase_error("No room for a new luafunction register")
  end
  luatexbase_log("Lua function " .. (name or "") .. " = " ..
                 tex_count[luafunction_count_name])
  return tex_count[luafunction_count_name]
end

it would probably have been better not to introduce \e@alloc@luafunction@count and always use the table size but it may be difficult to remove, so keeping them in sync seems the best compromise

github-actions[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity.

muzimuzhi commented 10 months ago

Is this issue already "fixed-in-dev"?

Nope. Sorry for the noise.

github-actions[bot] commented 8 months ago

This issue has been automatically marked as stale because it has not had recent activity.