Skip to content

Lua to .NET Value Conversion

Kevin Zhao edited this page Jan 25, 2018 · 3 revisions

Lua to .NET Value Conversion

Lua has nine different types of values, and they are listed here. They are automatically converted to .NET values as below:

  • nil: null.
  • boolean: bool.
  • light userdata: N/A, as this is never used.
  • number: Either long or double, depending on whether the number is in fact an integer.
  • string: string.
  • table: LuaTable.
  • function: LuaFunction.
  • userdata: N/A, as this internally is converted into .NET objects.
  • thread: LuaThread.

Reference Values

Tables, functions, and threads are references, as they internally store a reference to the object inside the Lua registry. This means that they are tied to a specific Lua environment, and cannot be used outside of it. Therefore, the following code would fail with an ArgumentException:

using (var lua = new Lua())
using (var lua2 = new Lua()) {
     var table = lua.CreateTable();
     lua2["table"] = table;
}

All reference objects are cached in a weak table, enabling them to be efficiently reused. So long as these .NET objects exist, the corresponding Lua objects can never be collected by the garbage collector. If they are collected by the .NET garbage collector, then Triton will free the references from the Lua registry at the next opportune time, typically when a call occurs. This will then enable the Lua garbage collector to collect the corresponding Lua objects.

Clone this wiki locally