Skip to main content

lje (Lua extensions)

Lua-defined extensions added to `lje` and the safe environment during preinit.

Constants

NameTypeDescription
_Gtable

A reference to the original, unmodified global environment. Exposed in the safe environment so scripts can access globals that may have been shadowed or overwritten.

_Ltable

A circular reference to the safe environment itself. Useful for passing the safe environment to functions that need an explicit reference to it.

cloned_mtstable

A map of cloned metatables, keyed by type name (e.g. "Entity", "Player", "Vector"). These are deep copies of the original metatables, remapped globally so LJE scripts use them instead of the originals.

cloned_basemtstable

A map of cloned base metatables for primitive types, keyed by type name. Currently contains "string". Used by lje.use_safe_basemts and lje.restore_basemts.

insecure_mtstable

Stores the original (uncloned) base metatables when lje.use_safe_basemts is active, so they can be restored by lje.restore_basemts.

lje.includeCachetable

A per-script cache used by lje.require. Keyed by script name, then by path. Populated automatically — you generally do not need to interact with this directly.

Functions

lje.detour(origFn, detourFn)

Creates a detour by marking detourFn as special and spoofing it to appear as origFn in debug introspection. If origFn is a C or fast function, JIT compilation is disabled on detourFn to prevent crashes.

Parameters

NameTypeDescription
origFnfunction

The original function being detoured. Determines whether JIT must be disabled.

detourFnfunction

Your replacement function. Will be marked special and spoofed to look like origFn.

Returns

TypeDescription
function

detourFn, returned for convenience.

lje.require(path)

A cached version of lje.include. The first call for a given path within a script context executes the file and caches the result. Subsequent calls return the cached value immediately. Must be called within an active script context.

Parameters

NameTypeDescription
pathstring

Path to the file to include, relative to the current script.

Returns

TypeDescription
any

The return value of the included file, or the cached result from a prior call.

lje.con_printf(fmt, ...?)

A string.format-style console print with inline ANSI color support. Wrap text in $colorName{...} to colorize it.

Supported colors: black, red, green, yellow, blue, magenta, cyan, white, default.

Example:

lje.con_printf("$red{Error}: %s", message)

Parameters

NameTypeDescription
fmtstring

A string.format format string. Color codes use the syntax $colorName{text}.

...?any

Format arguments, passed to string.format.

lje.get_global(...)

Safely traverses the original global environment (_G) using rawget at each step, following a variadic path of keys. Returns nil at the first missing or non-table intermediate value.

Example:

local fn = lje.get_global("hook", "Add")

Parameters

NameTypeDescription
...string

A sequence of keys forming the path to traverse in _G.

Returns

TypeDescription
any | nil

The value at the end of the path, or nil if any step is missing or not a table.

lje.get_global_static(paths, count)

Same behavior as lje.get_global but avoids the vararg allocation. Pass a pre-built table of keys and its length. Prefer this in hot paths where allocation matters.

Example:

local path = {"hook", "Add"}
local fn = lje.get_global_static(path, 2)

Parameters

NameTypeDescription
pathstable

An array of string keys forming the traversal path. Avoid creating this table dynamically, as that defeats the purpose of this function.

countinteger

The number of keys in paths to traverse.

Returns

TypeDescription
any | nil

The value at the end of the path, or nil if any step is missing or not a table.

lje.use_safe_basemts()

Deprecated: Old system before metatable remapping - no longer necessary or recommended.

Swaps in the cloned base metatables for primitive types (currently string). The original metatables are saved in insecure_mts so they can be restored. Call lje.restore_basemts when done.

lje.restore_basemts()

Deprecated: Old system before metatable remapping - no longer necessary or recommended.

Restores the original base metatables saved by lje.use_safe_basemts. Has no effect if use_safe_basemts was not previously called.