Skip to content

Instantly share code, notes, and snippets.

@CaptainPRICE
Last active June 23, 2017 19:07
Show Gist options
  • Save CaptainPRICE/8ac3af2dd753d62d8df2512156e33f73 to your computer and use it in GitHub Desktop.
Save CaptainPRICE/8ac3af2dd753d62d8df2512156e33f73 to your computer and use it in GitHub Desktop.
An example of how you can detour a user-defined function (UDF) in Expression 2.
@persist Function:table
if (first())
{
Function = table()
function call(ID)
{
timer("function_" + ID, first() ? 0 : 1000)
}
call(1)
}
else
{
local C = clkName()
local D = C:match("^function_([0-9]+)$")
if (D:count())
{
local I = D[1, string]:toNumber()
local Signature = ""
switch (I)
{
case 1,
Signature = format("void=%s()", C)
function function_1()
{
print("The first function.")
}
print(Signature)
break
case 2,
Signature = format("number=%s()", C)
function function_1()
{
print("Detoured the first function.")
#return 2
}
function function_2() { function_1() }
print(Signature)
break
default,
print("Done.")
stopAllTimers()
timer(toString(I - 1), 1000)
exit()
break
}
Function[I, string] = C
Function[C, string] = Signature
call(I + 1)
}
else
{
local Detour = Function[C:toNumber(), string]
print("Calling detour: " + Detour)
Detour()
}
}
@persist UserFunction:string
UserFunction = "userFunction"
# Our user-defined function (UDF):
function string userFunction()
{
return "Hello World"
}
print(UserFunction()[string]) # Hello World
# Detouring the "userFunction" UDF (think of it as a new "userFunction" UDF):
function string userFunction()
{
# Do something evil...
local S = "!"
local I = 1
while (perf(100)) { S += S:repeat(I) I++ }
return "Detoured userFunction!"
}
print(UserFunction()[string]) # Detoured userFunction!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment