Skip to content

global lua

😱 Types incomplete or incorrect? 🙏 Please contribute!


methods


lua.setbytecode


function lua.setbytecode(
  n: integer,
  f: (function)?
)

Save a function in a bytecode register.

Example:

lua.setbytecode(13, function () print('A message') end)
local print_message = lua.getbytecode(13)
print_message() -- 'A message'

Reference:

lua.getbytecode


function lua.getbytecode(n: integer) -> f (function)?

Return a previously stored function from a bytecode register.

Example:

lua.setbytecode(13, function () print('A message') end)
local print_message = lua.getbytecode(13)
print_message() -- 'A message'

Reference:

lua.getcodepage


function lua.getcodepage()
 ->  integer
 ->  integer

@return - command handler

@return - graphical user interface

😱 Types incomplete or incorrect? 🙏 Please contribute!

Return two numbers, one for the command handler and one for the graphical user interface (on Microsoft Windows).

Reference:

lua.setluaname


function lua.setluaname(
  chunk_name: string?,
  index: integer
)
@param chunk_name - If you want to unset a Lua name, you can assign nil to it.

Set a Lua chunk name.

When a chunk name starts with a @ it will be displayed as a file name. This is a side effect of the way Lua implements error handling.

Reference:

@see lua.name lua.getluaname

lua.getluaname


function lua.getluaname(index: number) ->  string?

Return a Lua chunk name.

Reference:

@see lua.name lua.setluaname

lua.newtable


function lua.newtable(
  index: integer,
  hash: integer
) ->  table

Create a new empty table and push it onto the stack.

Parameter index is a hint for how many elements the table will have as a sequence; parameter hash is a hint for how many other elements the table will have. Lua may use these hints to preallocate memory for the new table. This preallocation is useful for performance when you know in advance how many elements the table will have.

Reference:

lua.getstacktop


function lua.getstacktop() ->  integer

Return a number indicating how much nesting is going on.

It is only of use as a breakpoint when checking some mechanism going haywire.

Reference:

lua.getcalllevel


function lua.getcalllevel() ->  integer

Return a number indicating how much nesting is going on.

It is only of use as a breakpoint when checking some mechanism going haywire.

Reference:

lua.get_functions_table


function lua.get_functions_table() ->  { [integer]: fun(slot: integer) }

The \directlua commands involves tokenization of its argument (after picking up an optional name or number specification). The tokenlist is then converted into a string and given to Lua to turn into a function that is called. The overhead is rather small but when you have millions of calls it can have some impact. For this reason there is a variant call available: \luafunction. This command is used as follows:

\directlua {
    local t = lua.get_functions_table()
    t[1] = function() tex.print("!") end
    t[2] = function() tex.print("?") end
}

\luafunction1
\luafunction2

Of course the functions can also be defined in a separate file. There is no limit on the number of functions apart from normal Lua limitations. Of course there is the limitation of no arguments but that would involve parsing and thereby give no gain. The function, when called in fact gets one argument, being the index, so in the following example the number 8 gets typeset.

\directlua {
    local t = lua.get_functions_table()
    t[8] = function(slot) tex.print(slot) end
}

token.set_lua("mycode", id)
token.set_lua("mycode", id, "global", "protected")

This creates a token that refers to a Lua function with an entry in the table that you can access with lua.get_functions_table. It is the companion to luadef.

Reference:

  • LuaTeX manual: 2.4.4 \luafunction, \luafunctioncall and \luadef
  • LuaTeX manual: 10.6.4 Macros
  • Corresponding C source code: llualib.c#L356-L360

fields


lua.version


lua.version : string

Version information: This library contains one read-only item:

<string> s = lua.version

This returns the Lua version identifier string.

Reference:

lua.bytecode


lua.bytecode : table<integer,(function)?>

Use the bytecode table to store Lua code chunks. The accepted values for assignments are functions and nil. Likewise, the retrieved value is either a function or nil.

The contents of the lua.bytecode array is stored inside the format file as actual Lua bytecode, so it can also be used to preload Lua code. The function must not contain any upvalues.

lua.name


lua.name : table<integer,string>

There is an array of 65536 (0-65535) potential chunk names for use with the directlua and latelua primitives.

lua.name[<number> n] = <string> s
<string> s = lua.name[<number> n]

@see lua.getluaname lua.setluaname