| title | clonefunction |
|---|
Returns a copy of func that runs the same code, but is a separate function: clone == func is always false.
clonefunction(func: function): functionWorks on Lclosures/CClosures. The bytecode is identical, so getfunctionhash(clone) == getfunctionhash(func).
Mainly used to grab a clean copy of a function before some other script hooks it.
The clone keeps the original behavior, so calls through it bypass the hook entirely:
local cleanFunction = clonefunction(someFunction)
-- another script may hookfunction(someFunction, ...) later,
-- cleanFunction still calls the original.| Parameter | Type | Description |
|---|---|---|
func |
function |
The function to clone. |
function - same code as func, with its own memory address.
Each call to clonefunction returns a new function - clone the same one 10 times, and you get 10 separate copies of it.
local Old = clonefunction(print)
Old = hookfunction(print, function(...)
if not checkcaller() then
Old(...)
end
warn(...)
end)
print("Hello, World!") -- warnlocal cloned = clonefunction(print)
local p = print
print(cloned == p) -- falselocal Lua_Closure = newlclosure(function()
return "Hello, World!"
end)
local cloned_function = clonefunction(Lua_Closure)
if dumpbytecode(Lua_Closure) == dumpbytecode(cloned_function) then
print("same bytecode")
end