restorefunction
Overview of the restorefunction function.
Errors
Cannot obtain original function to unhook.
Caused when the original function is deleted or modified on the Luau Registry, resetting the hook would lead to danger in such cases; because of it, the function will not unhook the target
closure
.
High-Level Overview
Unhooks the closure
if it has been previously hooked using hookfunction
.
Remarks
Functions that are not hooked will not error this function, unlike other implementations.
This function will revert cross-function hooks and functions that have been hooked multiple times; however, restoring a function hooked multiple times may have implicitly imposed memory leaks, as the previous hooks cannot be reversed, and the memory will still be live. Consider this before trying to unhook a deeply hooked function.
Low-Level Overview
Internally an std::map
is kept, with all hooked functions as the key and metadata on the hook as the value; the function is checked using the pure contains
function, as using brackets to index will implicitly create the value and return the default.
Once it has been checked, if it is not hooked, it will return early. However, if it is hooked, the function will then start comparing the hookType
, no hook where Lua is one end and C is the other, is possible other than with wrapping, which can be restored with ease by swapping the pointers of the current original by a copied, hidden original on the Luau Registry.
It is to note that copies of objects left on the Luau Registry do not get collected, thus incurring a memory cost. If the original function is modified, the function cannot be unhooked, as doing so could lead to dangerous memory corruption. Thus if the object is not a Closure *
or it no longer exists on the Luau registry, the function will error.
However, if it does exist, the contents of it are swapped, and if the function is a wrapped C closure, the pointer to the wrapped closure is freed. and all information is reset to that of the original
, first hook placed.
Below is the data structure used to maintain metadata on the hooked functions.
Once the function is unhooked and all the pointers are swapped, the hook is removed from the map, and all the used resources are cleaned up.
Last updated