ReferenceClosures

trampoline_call

Calls a function as if it had been called from a different place.

Syntax

trampoline_call(target: function, callstack: {CallStackEntry}, options: TrampolineOptions, ...: any) -> (boolean, ...any)

Also available as

trampolinecall

Parameters

ParameterTypeDescription
targetfunctionThe function to call.
callstack{CallStackEntry}The frames the call should appear to come from. Entry 1 is the frame nearest target; each entry is { func = function, currentline = number }.
optionsTrampolineOptionsA table, possibly empty: { thread = thread?, env = table?, script = Instance?, identity = number? }.
...anyArguments passed to target.

Returns

TypeDescription
booleantrue when the call succeeded.
...anyThe target's results, or the error value when it failed.

Description

The call runs target on a fresh thread whose call history is made of the functions you list in callstack, each stopped at the line you give. Code inside target that inspects its callers with debug.info, debug.traceback, getcallingscript or getcallstack sees those functions and lines instead of your script.

The options set the rest of the call's context. thread uses the exact thread you choose, taking its globals, script and identity without running its original coroutine body; when it is given, the other options are ignored. Otherwise env sets the run's globals table, script sets the calling script and identity sets the thread identity, an integer from 0 to 8. Supplying an empty environment does not break result handling.

Errors are caught and returned like pcall. The constructed frames are removed on every path, whether the target returns or raises. When no thread is supplied, a yieldable caller with an available game task scheduler can call a target that waits and resumes. Results preserve trailing nil values and errors preserve their original value. A supplied-thread call is synchronous and cannot yield, so its existing suspended or completed state can be restored.

Example

local function outer()
    return 1
end

local ok, where = trampoline_call(function()
    return debug.info(2, "f") == outer
end, {
    { func = outer, currentline = 10 },
}, { identity = 2 })

print(ok, where)

Notes

These are Game executor contracts. Check build requirements and session lifetimes before using them.