Closures
Function hooks, cloning and closure checks.
Compiles Luau source in the executor environment and returns a function.
local fn = loadstring("return 21 * 2", "double")
print(fn())
Redirects a function and returns a callable original. An optional filter decides which calls reach the replacement. All three names are aliases.
local function add(a, b) return a + b end
local original = hookfunction(add, function(a, b)
return original(a, b) * 10
end)
print(add(2, 3))
Wraps a Lua function as a C closure.
local fn = newcclosure(function(value)
return value + 1
end)
print(fn(1))
Returns true for closures created by newcclosure or newlclosure wrappers.
print(isnewcclosure(newcclosure(function() end)), isnewcclosure(function() end))
Restores a function replaced by hookfunction.
local original = hookfunction(print, function() end)
restorefunction(print)
print("print restored", original)
Returns whether a function is currently hooked.
hookfunction(print, function() end)
print(isfunctionhooked(print))
Creates an independent copy of a function.
local cloned = clonefunction(print)
cloned("hello from the clone")
Returns true for Lua closures. isluaclosure is an alias.
print(islclosure(function() end), isluaclosure(function() end))
Returns true for executor-created or tracked functions.
local fn = newcclosure(function() end)
print(isourclosure(fn), checkclosure(fn), isexecutorclosure(fn))
Compares two closures while following hook aliases.
local fn = function() end
local copy = clonefunction(fn)
print(compareclosures(fn, copy), comparefunctions(fn, fn))
Hooks a signal callback and returns the original.
local event = Instance.new("BindableEvent")
hooksignal(event.Event, function(value)
print("hooked", value)
end)
event:Fire(1)
Restores a signal hook.
local event = Instance.new("BindableEvent")
hooksignal(event.Event, function() end)
restoresignal(event.Event)
event:Fire()
Creates a Luau closure wrapper that preserves the original function and its environment.
local wrapped = newlclosure(function(value)
return value
end)
print(wrapped("hello"))
Compiles Luau source and returns its bytecode as a string.
local bytecode = dumpbytecode("return 1 + 1")
print(#bytecode)
Loads bytecode and executes it, returning the chunk results.
local bytecode = dumpbytecode("return 21 * 2")
print(runbytecode(bytecode)) -- 42
Returns the replacement function installed on a hooked function, or nil.
local original = hookfunction(print, function(...) end)
print(gethook(print) ~= nil) -- true
restorefunction(print)
Returns whether hooksignal is currently replacing callbacks on the signal.
print(issignalhooked(Instance.new("Part").Touched)) -- false
Returns the compiled instruction stream of a Lua function as a binary string.
local function f() return 1 end
print(#getfunctionbytecode(f) > 0)