Closures
Hook, wrap, clone and inspect functions.
- loadstring
- hookfunction
- hookmetamethod
- Metamethod argument guard
- restorefunction
- isfunctionhooked
- newcclosure
- newlclosure
- wrapclosure
- clonefunction
- iscclosure
- islclosure
- isexecutorclosure
- isnewcclosure
- isnewlclosure
- checkcaller
- isexecutorthread
- checkcallstack
- setstackhidden
- getfunctionhash
- getfunctionbytecode
- Function calls
- trampoline_call
- FrameSpec
- ThreadOptions
Closure functions let you compile source, wrap or clone functions, inspect their type and replace their behavior. Use Filters to select which calls a replacement handles.
loadstring
loadstring(source: string, chunkName: string? = "="): (function?, string?)loadstring compiles a Luau source string into a function that you can call later. It does not run the source during compilation.
Also available as load_string.
Parameters
sourcestring- Luau source, including embedded bytes.
chunkNamestring?- Optional.Source name used in diagnostics. Default
"=".
Returns
function?, string?Compiled closure, or nil followed by the compiler diagnostic.
Example
hookfunction
hookfunction(target: function, replacement: function, filter: FilterBase? = nil): functionhookfunction replaces a function with another function and returns its previous behavior. Existing references to the target call the replacement.
Also available as hook_function, hookfunc, replaceclosure, replacefunction, replacefunc, detourfunction, detour_function, hook_func and replace_closure.
Parameters
targetfunction- The function whose calls will be replaced.
replacementfunction- The function to call instead of the target.
filterFilterBase?- Optional.A filter selecting which calls use the replacement. Default
nil.
Returns
functionPrevious callable behavior; retain it before calling the target from the replacement.
Example
hookmetamethod
hookmetamethod(object: any, method: string, replacement: function, arg_guard: boolean? = true, filter: FilterBase? = nil): functionhookmetamethod replaces a named metamethod on an object and returns the previous metamethod. Values that share the same metatable also share this change.
Also available as hookmetafield, hook_meta_method, hookmetafish and hook_metamethod.
Parameters
objectany- Value whose metamethod is resolved.
methodstring- Metamethod field name.
replacementfunction- Receives the original arguments.
arg_guardboolean?- Optional.Checks the argument count and receiver type before a supported native metamethod calls the replacement. Default
true. filterFilterBase?- Optional.An additional native call filter. Calls must match both the argument guard, when active, and this filter. Default
nil.
Returns
functionPrevious metamethod callable.
Example
Metamethod argument guard
The guard counts the receiver as argument 1 and checks these minimum argument counts. Extra arguments are allowed, and an explicit nil still counts as an argument.
Argument 1 must have the same Luau value type as object. Userdata must also have the same native tag and metatable. This matches a receiver's type rather than its identity or Instance class, so another compatible receiver can pass. __namecall additionally requires an active namecall method. The guard does not check property names or every argument's value; add a Filter for narrower matching. Metamethods outside the table receive no automatic guard.
restorefunction
restorefunction(target: function): ()restorefunction removes the replacement from a function and restores its original behavior.
Also available as restore_function, restorefunc and restoreclosure.
Parameters
targetfunction- Public target function to restore.
Returns
()No return values.
Example
isfunctionhooked
isfunctionhooked(fn: function): booleanisfunctionhooked checks whether a function currently has an active replacement.
Also available as is_function_hooked and ishooked.
Parameters
fnfunction- Function to inspect.
Returns
booleantrue when an active replacement is registered for the function.
Example
newcclosure
newcclosure(fn: function, debugName: string? = nil): functionnewcclosure wraps a function in a C closure. Calling the wrapper forwards the arguments to the original function and returns its results, including when the original function yields.
Also available as new_cclosure, new_c_closure and newcc.
Parameters
fnfunction- The function to wrap and call through the C closure.
debugNamestring?- Optional.Optional wrapper debug name. Default
nil.
Returns
functionA new forwarding C closure.
Example
newlclosure
newlclosure(fn: function, debugName: string? = "newlclosureWrapper"): functionnewlclosure wraps a function in a new Luau closure that forwards its arguments and return values.
Also available as new_lclosure, new_l_closure and newlc.
Parameters
fnfunction- The function to wrap and call through the Luau closure.
debugNamestring?- Optional.Source name for the generated Luau wrapper. Default
"newlclosureWrapper".
Returns
functionNew Luau wrapper that forwards all arguments and results.
Example
wrapclosure
wrapclosure(fn: function): function | stringwrapclosure creates a wrapper with the same closure kind as the supplied function: a C wrapper for a C function, or a Luau wrapper otherwise.
Also available as wrap_closure.
Parameters
fnfunction- The function to wrap, preserving its C or Luau closure kind.
Returns
function | stringWrapper on success; the protected constructor error value on failure.
Example
clonefunction
clonefunction(fn: function): functionclonefunction creates a separate function object with the same behavior as the supplied function at the time of cloning.
Also available as clonefunc and clone_function.
Parameters
fnfunction- Function to clone.
Returns
functionDistinct callable snapshot.
Example
iscclosure
iscclosure(fn: function): booleaniscclosure checks whether a function is a C closure, including a built-in function or a wrapper made by newcclosure.
Also available as is_c_closure.
Parameters
fnfunction- Function to inspect.
Returns
booleantrue for a C closure; otherwise false.
Example
islclosure
islclosure(fn: function): booleanislclosure checks whether a function is a Luau closure.
Also available as is_l_closure.
Parameters
fnfunction- Function to inspect.
Returns
booleantrue for a Luau closure; otherwise false.
Example
isexecutorclosure
isexecutorclosure(fn: function): booleanisexecutorclosure checks whether a function belongs to Lynx.
Also available as isourclosure, isXclosure, checkclosure, is_our_closure, is_executor_closure, is_krnl_closure, is_protosmasher_closure, issynapsefunction and is_synapse_function.
Parameters
fnfunction- Function to inspect.
Returns
booleantrue when the function is marked as Lynx-owned; otherwise false.
Example
isnewcclosure
isnewcclosure(fn: function): booleanisnewcclosure checks whether a function is a newcclosure wrapper owned by the current VM.
Also available as is_new_cclosure, is_new_cc, iscustomcclosure and is_custom_cclosure.
Parameters
fnfunction- Function to inspect.
Returns
booleantrue for a recognized newcclosure wrapper in the current VM; otherwise false.
Example
isnewlclosure
isnewlclosure(fn: function): booleanisnewlclosure checks for the environment marker used by newlclosure wrappers.
Also available as is_new_lclosure and is_new_lc.
Parameters
fnfunction- Function to inspect.
Returns
booleantrue when the function environment contains a function-valued wrap field.
Example
checkcaller
checkcaller(): booleancheckcaller checks whether the current call is running on a Lynx-owned thread.
Also available as check_caller, is_lynx_caller, issynapsethread and check_calling_script.
Returns
booleantrue when the calling thread is marked as Lynx-owned; otherwise false.
Example
isexecutorthread
isexecutorthread(thread: thread? = coroutine.running()): booleanisexecutorthread checks whether a thread belongs to Lynx. If you omit the thread, it checks the calling thread.
Also available as is_executor_thread, is_executor_state and isexecutorstate.
Parameters
threadthread?- Optional.The thread to check. Omit it to check the caller. Default
coroutine.running().
Returns
booleanWhether that thread carries the module ownership mark.
Example
checkcallstack
checkcallstack(level: number? = 2): booleancheckcallstack checks whether the call chain from a chosen stack level consists of Lynx-owned functions.
Also available as check_callstack and check_call_stack.
Parameters
levelnumber?- Optional.First frame level to include. Default
2.
Returns
booleantrue when the call chain passes the ownership check; otherwise false.
Example
setstackhidden
setstackhidden(target: function | number, hidden: boolean? = true): ()setstackhidden controls whether a function appears in the stack information returned by Lynx debug tools.
Also available as set_stack_hidden.
Parameters
targetfunction | number- The function to show or hide, or its stack level.
hiddenboolean?- Optional.Whether the function is hidden. Default
true.
Returns
()No return values.
Example
getfunctionhash
getfunctionhash(fn: function): stringgetfunctionhash returns a SHA-384 hash for a Luau or native function.
Parameters
fnfunction- Luau or native closure to hash.
Returns
stringThe function digest as a 96-character hexadecimal string.
Example
getfunctionbytecode
getfunctionbytecode(fn: function): string?getfunctionbytecode returns the compiled bytecode of a supported Luau function.
Also available as get_function_bytecode and dumpfunction.
Parameters
fnfunction- Function whose loaded prototype is serialized.
Returns
string?Serialized bytecode, or nil.
Example
Function calls
trampoline_call
trampoline_call(callback: (...any) -> ...any, frames: {FrameSpec}, options: ThreadOptions, ...any): (boolean, ...any)trampoline_call calls a function with a chosen thread context and stack frames, returning its success state and results.
Also available as trampolinecall.
Parameters
callback(...any) -> ...any- The function to call.
frames{FrameSpec}- The stack frames to use for the call.
optionsThreadOptions- The thread context to use for the call.
......any- Arguments passed to the callback or signal.
Returns
(boolean, ...any)A success boolean followed by results, or false followed by an error.
FrameSpec
ThreadOptions
Written for Lynx 0.747
Something unclear? Tell us on Discord