ReferenceClosures

newcclosure

Wraps a Luau function in a C function.

Syntax

newcclosure(func: function, debugname: string?) -> function

Also available as

new_c_closure

Parameters

ParameterTypeDescription
funcfunctionThe Luau function to wrap.
debugnamestring?Name reported by debug.info for the wrapper. Defaults to the wrapped function's own name.

Returns

TypeDescription
functionA C function that calls func with the same arguments and returns its results.

Description

The wrapper behaves exactly like the function it wraps: it receives the same arguments, returns the same values, may yield, and passes errors through unchanged, including non-string error values such as tables. To inspection it is a C function: iscclosure returns true and it has no Luau constants or upvalues to read.

Use it when a compatibility API requires a C closure while the implementation is written in Luau. The wrapper preserves the callable behavior and changes its inspection category.

Example

local wrapped = newcclosure(function(a, b)
    return a * b
end)

print(wrapped(6, 7))
print(iscclosure(wrapped))
print(iscustomcclosure(wrapped))

Notes

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