Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 1.61 KB

File metadata and controls

69 lines (50 loc) · 1.61 KB
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): function

Works 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.

Parameters

Parameter Type Description
func function The function to clone.

Returns

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.

Example

local Old = clonefunction(print)
Old = hookfunction(print, function(...)
	if not checkcaller() then
		Old(...)
	end
	warn(...)
end)

print("Hello, World!") -- warn
local cloned = clonefunction(print)
local p = print

print(cloned == p) -- false
local 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
Always check parameters and return values when working with this library function.