olsak / OpTeX

OpTeX - LuaTeX format with extended Plain TeX macros
http://petr.olsak.net/optex/
35 stars 13 forks source link

Simplify Lua function allocation #97

Closed vlasakm closed 2 years ago

vlasakm commented 2 years ago

This changes to the classic appending to Lua table:

table[#table + 1] = something
olsak commented 2 years ago

Where the initial value of luafnalloc is set? (if we remove the line local luafnalloc = 0).

vlasakm commented 2 years ago

We set function_table to be a reference to table that LuaTeX has built in.

The details are more intricate, but the # operator essentially returns the biggest number n such that table[1], ... table[n] are all not nil or 0.

Because the table is empty initially, we get the initial zero and then we can add values with the classic Lua idiom:

table[#table+1] = value

So the sequence:

table[1] = value
table[2] = value
...

And we can get the number of elements in that table with #table. I think this is better because we don't keep any state that has to be potentially synchronised.

I am not sure if I remember it correctly, but I have seen something allocate a Lua function in a similar way I suggest above. If then somebody would allocate with OpTeX, and because the OpTeX counter wouldn't be updated, OpTeX would perform the allocation of the same register.

The approach above doesn't have the synchronization problem. But on the other side, it would be problematic if someone would start randomly deleting the Lua functions. Unlike LaTeX, OpTeX doesn't hand out the allocation numbers so this situation is even more unlikely.

olsak commented 2 years ago

I done it in my last commit.

vlasakm commented 2 years ago

Closed by https://github.com/olsak/OpTeX/commit/d0c577aabc2aca528574993b5f1018078c98a8c0