Table of Contents

Class Luau

Namespace
LuauInterop
Assembly
LuauInterop.dll

Represents a Luau instance.

public class Luau : IDisposable
Inheritance
Luau
Implements
Inherited Members

Constructors

Luau()

Initializes a new Luau instance and allocates a fresh native state.

public Luau()

Exceptions

OutOfMemoryException

Thrown if the native state could not be allocated.

Fields

Callbacks

List of callbacks registered in this instance.

public readonly List<LuauCallback> Callbacks

Field Value

List<LuauCallback>

Properties

IsDisposed

Gets a value indicating whether this instance has been disposed.

public bool IsDisposed { get; }

Property Value

bool

this[string]

Gets or sets a global variable by name.

public object? this[string name] { get; set; }

Parameters

name string

The name of the global variable.

Property Value

object

The value of the global, or null if it is nil.

State

Gets the underlying native Luau state.

public LuaState State { get; }

Property Value

LuaState

Methods

Compile(string, LuauCompileOptions?)

Compiles a Luau source string into a LuauChunk.

public LuauChunk Compile(string chunk, LuauCompileOptions? options = null)

Parameters

chunk string

The Luau source code to compile.

options LuauCompileOptions

Optional compiler options. Pass null to use defaults.

Returns

LuauChunk

A compiled LuauChunk. Dispose it after use.

CreateThread()

Creates a new Luau coroutine thread.

public LuauThread CreateThread()

Returns

LuauThread

A LuauThread that can be resumed independently.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

DoChunk(LuauChunk, string)

Executes a pre-compiled LuauChunk, returning any values it produces.

public object?[] DoChunk(LuauChunk chunk, string chunkName = "chunk")

Parameters

chunk LuauChunk

The compiled chunk to execute.

chunkName string

A name for the chunk used in error messages. Defaults to "chunk".

Returns

object[]

An array of values returned by the chunk, or an empty array if it returns nothing.

Exceptions

LuauException

Thrown if execution fails.

ObjectDisposedException

Thrown if this instance has been disposed.

DoString(string, string)

Compiles and executes a Luau source string, returning any values it produces.

public object?[] DoString(string chunk, string chunkName = "chunk")

Parameters

chunk string

The Luau source code to execute.

chunkName string

A name for the chunk used in error messages. Defaults to "chunk".

Returns

object[]

An array of values returned by the chunk, or an empty array if it returns nothing.

Exceptions

LuauException

Thrown if compilation or execution fails.

ObjectDisposedException

Thrown if this instance has been disposed.

~Luau()

protected ~Luau()

GetErrorMessage(LuaState)

Gets the error message from the top of the stack, if any, and pops it off the stack. If there is no error message, returns a default message.

public string GetErrorMessage(LuaState state)

Parameters

state LuaState

Returns

string

The error message from the top of the stack, or a default message if there is no error message.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

GetFFlag(string)

Gets the value of an FFlag.

public bool GetFFlag(string name)

Parameters

name string

The name of the FFlag to get.

Returns

bool

true if the FFlag is enabled, or false if it is disabled.

GetObject(int, LuaState)

Gets the value at the specified index on the stack, converting it to an appropriate C# type.

public object? GetObject(int index, LuaState state)

Parameters

index int

The index of the value to get.

state LuaState

The Lua state to use for retrieving the value.

Returns

object

The value at the specified index, converted to an appropriate C# type.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

GetValue(int, LuaState)

Gets the value at the specified index on the stack.

public LuauValue GetValue(int index, LuaState state)

Parameters

index int

The index of the value to get.

state LuaState

Returns

LuauValue

The value at the specified index.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

LoadString(string, LuaState, string)

Compiles a Luau source string and loads it onto the stack as a callable function, without executing it.

public LuauStatus LoadString(string chunk, LuaState targetState, string chunkName = "chunk")

Parameters

chunk string

The Luau source code to compile and load.

targetState LuaState

The Lua state to load the compiled function onto. If the current state is different from the target state, the compiled function will be moved to the target state after loading.

chunkName string

A name for the chunk used in error messages. Defaults to "chunk".

Returns

LuauStatus

OK on success, or an error status if compilation or loading fails.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

LoadString(string, string)

Compiles a Luau source string and loads it onto the stack as a callable function, without executing it.

public LuauStatus LoadString(string chunk, string chunkName = "chunk")

Parameters

chunk string

The Luau source code to compile and load.

chunkName string

A name for the chunk used in error messages. Defaults to "chunk".

Returns

LuauStatus

OK on success, or an error status if compilation or loading fails.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

OpenLibrary(LuauLibrary)

Opens the specified standard Luau libraries into this state.

public void OpenLibrary(LuauLibrary library)

Parameters

library LuauLibrary

The libraries to open.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

Pop(int, LuaState)

Pops n values off the top of the stack.

public void Pop(int n, LuaState state)

Parameters

n int

The number of values to pop.

state LuaState

The Lua state to pop values from.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

Push(LuauValue, LuaState)

Pushes a LuauValue onto the stack.

public void Push(LuauValue value, LuaState state)

Parameters

value LuauValue

The value to push.

state LuaState

The Lua state to push the value onto.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

PushCallback(LuauCallback, LuaState)

Pushes a C# function onto the stack.

public void PushCallback(LuauCallback callback, LuaState state)

Parameters

callback LuauCallback
state LuaState

PushObject(object?, LuaState)

Pushes a C# object onto the stack, converting it to an appropriate Luau type.

public void PushObject(object? value, LuaState state)

Parameters

value object

The object to push.

state LuaState

The Lua state to push the object onto.

Exceptions

ObjectDisposedException

Thrown if this instance has been disposed.

RegisterCallback(string, Func<Luau, LuaState, int>)

Registers a C# function as a global in the Luau state, making it callable from Luau code.

public LuauCallback RegisterCallback(string name, Func<Luau, LuaState, int> fn)

Parameters

name string
fn Func<Luau, LuaState, int>

Returns

LuauCallback

SetFFlag(string, bool)

Sets an FFlag.

public static void SetFFlag(string name, bool enabled)

Parameters

name string

The name of the FFlag to set.

enabled bool

A value indicating whether the FFlag should be enabled.