Open-source project
NLua/NLua avatar
NLua/NLua

NLua: embedding Lua in C# with no registration, an ASCII default and a one-line sandbox

Bridge between Lua and the .NET.

2,247 stars327 forksC#MIT

At a glance

What is it?
NLua is a bridge that lets C# code run Lua scripts and lets those scripts instantiate .NET classes. This piece covers the call patterns, the encoding trap, the public-members rule, Unity setup and the thin sandboxing advice.
Who is it for?
Use NLua when you want Lua scripts inside a C# application and are fine exposing any public .NET member to them, including on Unity and Xamarin.iOS with the extra setup noted above. Skip it if your scripts are untrusted and you expect a hardened sandbox, since the README offers only an import override.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 74 days ago.
What is it written in?
Mainly C#, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 26, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • NLua/NLua2,247 ★ · +2
Star history
Repository2026-10-122026-11-26
NLua/NLua2.2k2.2k

2 Stars in the last 45 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

Any .NET class is callable from Lua without registration or attributes

NLua allows the usage of Lua from C#, on UWP, Windows, Linux, Mac, iOS and Android. Its central promise is that you can use or instantiate any .NET class without any previous registration or annotation. You do not write wrapper classes or tag methods with attributes. A plain C# class like SomeClass, with a constructor taking a default string, an instance method Func1 returning 32 and a static StaticMethod, is reachable as it stands.

That convenience has a flip side. Nothing marks which parts of your assembly are meant for scripts, so the surface a script can reach is whatever is public. Design the C# types you hand over with that in mind, and keep script-facing classes small instead of passing your domain model in whole.

NLua is a fork of LuaInterface, the project by Fábio Mascarenhas and Craig Presti, and it runs on top of the KeraLua binding.

DoString returns an array because Lua functions return many values

Creating a state takes one line, using NLua; Lua state = new Lua (). Evaluating an expression is done with DoString, and the result is always an object array:

code
var res = state.DoString ("return 10 + 3*(5 + 2)")[0] as double;

Lua can return multiple values, which is why you index [0]. Numbers come back as double, so the cast is (double), not (int). A mismatch there throws an InvalidCastException at runtime, not a compile error.

Globals cross the boundary through the indexer. Writing state ["x"] = val creates a global named x, and reading state ["y"] retrieves one. Lua functions come back as LuaFunction objects:

code
var scriptFunc = state ["ScriptFunc"] as LuaFunction;
var res = (int)scriptFunc.Call (3, 5).First ();

Call also returns an array. Notice the (int) cast on a function result, where plain expressions returned double in the earlier example. Check the type of each value you pull out, because the README samples do not use a single cast throughout.

KeraLua encodes strings as ASCII until you change it

NLua runs on KeraLua, and the README states that it encodes strings using ASCII by default. Non-ASCII text such as Cyrillic will not round-trip until you switch encodings on the state:

code
lua.State.Encoding = Encoding.UTF8;

The README test does this and then runs lua.DoString("res = 'Файл'"), reads lua["res"] and asserts equality with the same word. Without the encoding line, expect that comparison to fail.

This is the first thing to check if a game or tool built on NLua shows garbled names for users with non-English paths or text. Set the property before you run any script that carries non-ASCII literals.

The example wraps Lua in a using block, which signals that the Lua object holds native state and should be disposed. The README does not describe what leaks if you skip that.

Lua gets .NET assemblies through LoadCLRPackage and import

Passing an existing object is simple: state ["obj"] = obj creates a global of the .NET type, and it can be any object from the base class library or from your assemblies.

To create objects inside Lua you need the CLR package:

code
state.LoadCLRPackage ();

Then import works inside scripts, for example import ('MyAssembly', 'MyNamespace') and import ('System.Web'). After that you create objects by calling the class name with parentheses, such as SomeClass() or WebClient(), and you can leave out default constructor values.

The call syntax differs by member kind. Instance methods use a colon, as in obj:Func1(). Static methods use a dot on the class name, as in SomeClass.StaticMethod(4). Properties use a dot too, as in obj.MyProperty. Mixing up colon and dot is the usual script error.

The example fetches a page with client:DownloadString('http://nlua.org'), which shows that a loaded script can make network calls.

Non-public members fail, and Xamarin.iOS strips unused classes

The README says all methods, events and properties must be public, and NLua will fail to call non-public members. A private helper that works in your C# tests will not be reachable from a script, so expose what scripts need explicitly.

On Xamarin.iOS there is a second constraint. You must apply Preserve to the classes you want to use inside NLua. Otherwise the linker removes a class from the final binary when nothing in C# references it, and a script that calls it fails at runtime. Because scripts only name the class in text, the linker cannot see that it is used.

The symptom is a release build that behaves differently from a debug build, since linking is more aggressive in release. The README links Xamarin's linker guide for the details. Add Preserve to every class a script reaches, and test a release build on a device before you ship.

The sandbox advice is to overwrite import and nothing more

The README says there are many ways to sandbox scripts and strongly recommends plain Lua for it. Its concrete example rewrites the import function before the user script loads, so an attempt to import a .NET assembly does nothing:

code
import = function () end

It links the Lua-Sandbox page on lua-users.org for further reading.

That one line closes the door to new assemblies and stops there. The README does not discuss objects you have already passed in, so any public member of an object you assigned into state remains callable. It does not cover infinite loops, memory use or access to Lua's standard libraries either. If your scripts come from untrusted users, treat this as a starting point to extend with the linked material, not a finished security boundary.

Because import only exists after LoadCLRPackage, the override matters only when you call that method.

Unity setup means unpacking NuGet zips and fixing per-platform liblua54 settings

NLua builds with msbuild NLua.sln, and the repository contains further solution files for UWP, Android, Mac, netstandard2.0 and net8.0. The most hands-on workflow is Unity.

You either build for .NET 2.0 or download KeraLua and NLua from NuGet using the Download package link. Rename the .nupkg files to .zip, create Plugins/KeraLua and Plugins/NLua under the Assets directory, and extract the netstandard2.0 contents into them. The KeraLua tree includes native runtimes such as liblua54.so under linux-x64 and liblua54.dylib under osx.

Then you open each liblua54 file in the Unity inspector and set Platform settings for both editor and standalone to match its folder, and apply. Skip that and Unity may load the wrong native library or none at all.

The README is cut off after step 5, so later steps, if any, are not visible. The latest tag is v1.7.9 from 2026-05-01, and the last push was 2026-09-13.

Editorial conclusion

Use NLua when you want Lua scripts inside a C# application and are fine exposing any public .NET member to them, including on Unity and Xamarin.iOS with the extra setup noted above. Skip it if your scripts are untrusted and you expect a hardened sandbox, since the README offers only an import override. Before shipping, set Lua.State.Encoding to UTF-8 if you handle non-ASCII text, and test your scripts against the exact KeraLua runtime for each target platform.

Frequently asked questions

What kind of code is Lua when you use it with NLua?

Lua is the scripting language that NLua runs inside a C# process. You pass script text to DoString, for example "return 10 + 3*(5 + 2)", and NLua returns the results as an array of objects.

What is a Lua Lua in NLua?

In NLua the Lua class is the interpreter state you create with new Lua (). You run scripts with DoString and read or write globals with the indexer, such as state ["x"].

What does Lua mean for NLua users with non-English text?

NLua encodes strings as ASCII by default through KeraLua. Set Lua.State.Encoding to Encoding.UTF8 so text such as Cyrillic round-trips correctly.

Official sources

  1. License: MIT
  2. NLua/NLua on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/nlua-nlua.svg)](https://hysenlabs.com/projects/nlua-nlua)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/nlua-nlua.svg)](https://hysenlabs.com/projects/nlua-nlua)

Was this useful?