Skip to content

newcclosure

Do not implement this with coroutines

Many executors seem to be implementing this function using coroutine functions in Luau. Such functions will not pass sUNC checks.

The wrapped function must be yieldable, meaning that the function should be able to call task.wait, for example.

Error spoofing

Luau and C errors are different. You must ensure that errors from functions wrapped with newcclosure appear as C closure errors!

Upvalues

The function returned by newcclosure must have no upvalues.

newcclosure takes any Lua function and wraps it into a C closure. When the returned function is called, it invokes the original Luau closure with the provided arguments, then passes the closure's returned values back to the caller.

function newcclosure<A..., R...>(functionToWrap: (A...) -> R...): (A...) -> R...

Parameters

Parameter Description
functionToWrap A function to be wrapped.

Examples

Example 1

Basic C closure wrapping example with newcclosure
local dummy_function = function(...)
    return ...
end

print(iscclosure(dummy_function)) -- Output: false

local wrapped_function = newcclosure(dummy_function)

print(iscclosure(wrapped_function)) -- Output: true

local function_results = wrapped_function("Hello")
print(function_results) -- Output: Hello

Example 2

This example illustrates how Luau functions wrapped as a C closure should also be yieldable, therefore also showcasing how coroutine implementations of newcclosure would not work.

Yieldable C functions made with newcclosure
local dummy_yielding_function = newcclosure(function()
    print("Before")
    task.wait(1.5)
    print("After")
end)

dummy_yielding_function()
-- Output:
-- Before
-- yield for 1.5 seconds
-- After