Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

NLua

> 编程语言
Open source

Bridge between Lua and the .NET.

2.2K stars0 likes2 views
WebsiteGitHub

About

Bridge between Lua and the .NET.

Hello there!

Thank you for checking out this project.

We've made the project Open Source and MIT license so everyone can enjoy it.

To deliver a project with quality we have to spent a lot of time working on it.

⭐️ If you liked the project please star it.

We also appreaciate any Sponsor [ Patreon | PayPal ] NLua =======

NuGet
Status

Bridge between Lua world and the .NET (compatible with .NET/UWP/Mac/Linux/Android/iOS/tvOS)

Building

msbuild NLua.sln

NLua allows the usage of Lua from C#, on UWP, Windows, Linux, Mac, iOS , Android.

NLua is a fork project of LuaInterface (from Fábio Mascarenhas/Craig Presti).

Example: You can use/instantiate any .NET class without any previous registration or annotation.

csharp
	public class SomeClass
	{
		public string MyProperty {get; private set;}
		
		public SomeClass (string param1 = "defaulValue")
		{
			MyProperty = param1;
		}
		
		public int Func1 ()
		{
			return 32;
		}
		
		public string AnotherFunc (int val1, string val2)
		{
			return "Some String";
		}
		
		public static string StaticMethod (int param)
		{
			return "Return of Static Method";
		}
        }
  • Using UTF-8 Encoding:

NLua runs on top of KeraLua binding, it encodes the string using the ASCII encoding by default. If you want to use UTF-8 encoding, just set the Lua.State.Encoding property to Encoding.UTF8:

csharp

using (Lua lua = new Lua())
{
	lua.State.Encoding = Encoding.UTF8;
	lua.DoString("res = 'Файл'");
	string res = (string)lua["res"];

	Assert.AreEqual("Файл", res);
}

Creating Lua state:

csharp
	using NLua;
	
	Lua state = new Lua ()

Evaluating simple expressions:

csharp
	var res = state.DoString ("return 10 + 3*(5 + 2)")[0] as double;
	// Lua can return multiple values, for this reason DoString return a array of objects

Passing raw values to the state:

csharp
	double val = 12.0;
	state ["x"] = val; // Create a global value 'x' 
	var res = (double)state.DoString ("return 10 + x*(5 + 2)")[0];

Retrieving global values:

csharp
	state.DoString ("y = 10 + x*(5 + 2)");
	double y = (double) state ["y"]; // Retrieve the value of y

Retrieving Lua functions:

csharp
	state.DoString (@"
	function ScriptFunc (val1, val2)
		if val1 > val2 then
			return val1 + 1
		else
			return val2 - 1
		end
	end
	");
	var scriptFunc = state ["ScriptFunc"] as LuaFunction;
	var res = (int)scriptFunc.Call (3, 5).First ();
	// LuaFunction.Call will also return a array of objects, since a Lua function
	// can return multiple values

##Using the .NET objects.##

Passing .NET objects to the state:

csharp
	SomeClass obj = new SomeClass ("Param");
	state ["obj"] = obj; // Create a global value 'obj' of .NET type SomeClass 
	// This could be any .NET object, from BCL or from your assemblies

Using .NET assemblies inside Lua:

To access any .NET assembly to create objects, events etc inside Lua you need to ask NLua to use CLR as a Lua package. To do this just use the method LoadCLRPackage and use the import function inside your Lua script to load the Assembly.

csharp
	state.LoadCLRPackage ();
	state.DoString (@" import ('MyAssembly', 'MyNamespace') 
			   import ('System.Web') ");
	// import will load any .NET assembly and they will be available inside the Lua context.

Creating .NET objects: To create object you only need to use the class name with the ().

csharp
state.DoString (@"
	 obj2 = SomeClass() -- you can suppress default values.
	 client = WebClient()
	");

Calling instance methods: To call instance methods you need to use the : notation, you can call methods from objects passed to Lua or to objects created inside the Lua context.

csharp
	state.DoString (@"
	local res1 = obj:Func1()
	local res2 = obj2:AnotherFunc (10, 'hello')
	local res3 = client:DownloadString('http://nlua.org')
	");

Calling static methods: You can call static methods using only the class name and the . notation from Lua.

csharp
	state.DoString (@"
	local res4 = SomeClass.StaticMethod(4)
	");

Calling properties: You can get (or set) any property using . notation from Lua.

csharp
	state.DoString (@"
	local res5 = obj.MyProperty
	");

All methods, events or property need to be public available, NLua will fail to call non-public members.

If you are using Xamarin.iOS you need to Preserve the class you want to use inside NLua, otherwise the Linker will remove the class from final binary if the class is not in use.

##Sandboxing##

There is many ways to sandbox scripts inside your application. I strongly recommend you to use plain Lua to do your sandbox. You can re-write the import function before load the user script and if the user try to import a .NET assembly nothing will happen.

csharp
	state.DoString (@"
		import = function () end
	");

Lua-Sandbox user-list

Unity Integration

  1. Build for .Net 2.0 or Download KeraLua and NLua from NuGet (use the Download package link)
  2. if Download: change the file extensions from .nupkg to .zip
  3. create folders in your Unity project's Assets directory Plugins/KeraLua and Plugins/NLua
  4. extract the netstandard2.0 contents of those zip files into the newly created unity folders. Your tree should look roughly like this (with KeraLua runtimes customized to your build targets)
…
  1. Go through each KeraLua runtime liblua54 files in the Unity inspector and set the correct Platform settings for both editor and standalone based on the folder. Don't forget to apply

Notes for unity

  • you will likely want to append to package.path you can do it with something like this lua.DoString("package.path = package.path .. \";" + Application.persistentDataPath + "/?/?.lua;" + Application.persistentDataPath + "/?.lua\"");
  • you will want to expose Debug.Log to lua (and override the print function to see lua logs)
  • numbers will come back as double type
  • fields set on the lua instance may be null and you will need to handle it

Copyright (c) 2026 Vinicius Jarina ([email protected])

NLua 1.4.x

NLua huge cleanup and refactor after a few years.

  • Moved to .NET C# style.
  • Using KeraLua as nuget dependencie.
  • Droped support for KopiLua/Silverlight/Windows Phone

NLua 1.3.2

  • Migration to unified Xamarin.iOS (iOS)
  • Added __call method to call Actions/Funcs from Lua as Lua functions.
  • Fixed #116 problem accessing base class method
  • Fixed #117 problem with same method in class and base class
  • Fixed #125 calling methods with params keyword.

NLua 1.3.2

  • Added support to WinRT (Windows Phone 8)
  • Added support to Unity3D
  • Update Lua 5.2.3 with latest patches
  • Fixed support to Unicode strings (UTF-8)
  • Fixed x86/x64 issue.
  • Fixed overload issue
  • Fixed support to Debug and DebugHook APIs
  • Added support to operators call
  • Fixed access to keys with .
  • Fixed issue with ValueTypes

NLua 1.3.0

  • Update Lua to 5.2.3
  • Update to Xamarin components store. (http://components.xamarin.com/view/NLua)

NLua 1.2.0

  • NuGet Package (https://www.nuget.org/packages/NLua/)
  • Port to Android 15+ (armeabi, v7a, x86)
  • Updated Lua 5.2.2 (patch 7)
  • Lot of Bug fixes.

NLua 1.1.0

  • Port to WP7 (Thanks to Mangatome)
  • NLua now using Lua 5.2.2
  • Bug fixes.

NLua 1.0.0

  • Forked from LuaInterface 2.0.4
  • Added iOS support using KeraLua (C# P/Invoke Lua)

###Help NLua###

  • Contributing

  • NLua uses the Mono Code-Style http://www.mono-project.com/Coding_Guidelines .
  • Please, do not change the line-end or re-indent the code.
  • Run the tests before you push.
  • Avoid pushing style changes (unless they are really needed), renaming and move code.

Old History

LuaInterface

Copyright (c) 2003-2006 Fabio Mascarenhas de Queiroz

Maintainer: Craig Presti, [email protected]

lua51.dll and lua51.exe are Copyright (c) 2005 Tecgraf, PUC-Rio

Getting started with NLua:

  • Look at src/TestNLua/TestLua to see an example of usage from C# (optionally you can run this from inside the NLua solution using the debugger).
    Also provides a good example of how to override .NET methods of Lua and usage of NLua from within your .NET application.

  • Look at samples/testluaform.lua to see examples of how to use .NET inside Lua

  • More installation and usage instructions in the doc/guide.pdf file.

What's new in LuaInterface 2.0.3

  • Fix: Private methods accessible via LuaInterface
  • Fix: Method overload lookup failures
  • Fix: Lua DoFile memory leaks when file not found (submitted by Paul Moore)
  • Fix: Lua Dispose not freeing memory (submitted by Paul Moore)
  • Fix: Better support for accessing indexers
  • Fix: Parsing error for MBCS characters (qingrui.li)
  • Fix: Dispose errors originating from LuaTable, LuaFunction, LuaUserData
  • Fix: LuaInterface no longer disposes the state when passed one via the overloaded constructor
  • Added: LoadString and LoadFile (submitted by Paul Moore)
  • Added: Overloaded DoString
  • Added: Lua debugging support (rostermeier)

What's new in LuaInterface 2.0.1

  • Apparently the 2.0 built binaries had an issue for some users, this is just a rebuild with the lua sources pulled into the LuaInterface.zip

What's new in LuaInterface 2.0

  • The base lua5.1.2 library is now built as entirely manged code. LuaInterface is now pure CIL
  • Various adapters to connect the older x86 version of lua are no longer needed
  • Performance fixes contributed by Toby Lawrence, Oliver Nemoz and Craig Presti

What's new in LuaInterface 1.5.3

  • Internal lua panics (due to API violations) now throw LuaExceptions into .net
  • If .net code throws an exception into Lua and lua does not handle it, the original exception is forwarded back out to .net land.
  • Fix bug in the Lua 5.1.1 gmatch C code - it was improperly assuming gmatch only works with tables.

What's new in LuaInterface 1.5.2

  • Overriding C# methods from Lua is fixed (broken with .net 2.0!)
  • Registering static C# functions for Lua is fixed (broken with Lua-5.1.1)
  • Rebuilt to fix linking problems with the binaries included in 1.5.1
  • RegisterFunction has been leaking things onto the stack

What's new in LuaInterface 1.5.1

Fix a serious bug w.r.t. garbage collection - made especially apparent with the new lua5.1 switch: If you were very unlucky with timing sometimes Lua would loose track of pointers to CLR functions.

When I added support for static methods, I allowed the user to use either a colon or a dot to separate the method from the class name. This was not correct - it broke disambigu

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#c-sharpdotnetlualua-functions

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言