Lua.NET 5.0.3

dotnet add package Lua.NET --version 5.0.3
NuGet\Install-Package Lua.NET -Version 5.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lua.NET" Version="5.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Lua.NET --version 5.0.3
#r "nuget: Lua.NET, 5.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Lua.NET as a Cake Addin
#addin nuget:?package=Lua.NET&version=5.0.3

// Install Lua.NET as a Cake Tool
#tool nuget:?package=Lua.NET&version=5.0.3

Lua.NET

Logo

C# .NET Core 8.0 Lua.NET contains full bindings to Lua5.1.5, Lua5.2.4, Lua5.3.6, Lua.5.4.6 and LuaJIT

https://github.com/tilkinsc/Lua.NET
Copyright © Cody Tilkins 2024 MIT License

Supports Lua5.4 Lua5.3 Lua5.2 Lua5.1 and LuaJIT

Hardcoded to only use doubles and 64-bit integers.

Design Considerations / Usage

The delegates you pass to functions such as lua_pushcfunction(...) should be static. Otherwise, the lifetime of your functions should exceed the lifetime of the lua_State. Do not use lambdas, as C# is liable to GC them. A system to support lambdas may be added in the future, but it requires tracking lambda references.

Shared Libraries

No shared library has to be built for this library, as its included for you. Custom DLLs are supported when building from source; they must retain ABI compatibility with Lua. If using custom shared libraries, you will need to replace the repsective shared library Nuget gives you locally in your project in bin/{configuration}/{target}/runtimes/{platform-rid}/native. The name must be the same as the one you are replacing. The ideal way to handle this is by rolling your own nuget package clone of this repository. The reason for this is a shortcoming of runtime dlls not being copied over in project references (as opposed to package reference from nuget). All of this can be avoided if you change the DllName inside the respective source, however, that solution adds complexity to your project. Nuget packages are easy.

Examples

Example Usage Lua5.4.6:

// test1.csproj
// <PropertyGroup>
//     ...
//     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
// </PropertyGroup>
using LuaNET.Lua54;
using static LuaNET.Lua54.Lua;

namespace TestLua;

public class Test1
{
	
	public static int lfunc(lua_State L)
	{
		lua_getglobal(L, "print");
		lua_pushstring(L, "lol");
		lua_pcallk(L, 1, 0, 0, IntPtr.Zero, null);
		return 0;
	}
	
	public static void Main(string[] args)
	{
		lua_State L = luaL_newstate();
		if (L == 0)
		{
			Console.WriteLine("Unable to create context!");
		}
		luaL_openlibs(L);
		lua_pushcfunction(L, lfunc);
		Console.WriteLine(lua_pcallk(L, 0, 0, 0, null, null));
		lua_close(L);
		
		Console.WriteLine("Success");
	}
	
}

Example Usage LuaJIT:

// test2.csproj
// <PropertyGroup>
//     ...
//     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
// </PropertyGroup>
using LuaNET.LuaJIT;
using static LuaNET.LuaJIT.Lua;

namespace TestLua;

public class Test2
{
	
	public static int lfunc(lua_State L)
	{
		lua_getglobal(L, "print");
		lua_pushstring(L, "lol");
		lua_pcall(L, 1, 0, 0);
		return 0;
	}
	
	public static void Main(string[] args)
	{
		lua_State L = luaL_newstate();
		if (L == 0)
		{
			Console.WriteLine("Unable to create context!");
		}
		luaL_openlibs(L);
		lua_pushcfunction(L, lfunc);
		Console.WriteLine(lua_pcall(L, 0, 0, 0));
		lua_close(L);
		
		Console.WriteLine("Success");
	}
	
}

Example Usage NativeAOT DLL Library:

// test3.csproj
// <PropertyGroup>
//     ...
//     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
//     <PublishAot>true</PublishAot>
// </PropertyGroup>
//
// dotnet publish -r win-x64 -c Release
// This will emit test3.dll inside of bin/.../native and bin/.../publish
// I use the publish one

using System.Runtime.InteropServices;
using LuaNET.LuaJIT;
using static LuaNET.LuaJIT.Lua;

namespace TestLua;

public unsafe class Test3
{
	
	[UnmanagedCallersOnly]
	public static int l_GetHello(lua_State L)
	{
		lua_pushstring(L, "Hello World!");
		return 1;
	}
	
	public static luaL_Reg[] test2Lib = new luaL_Reg[]
	{
		AsLuaLReg("GetHello", &l_GetHello),
		AsLuaLReg(null, null)
	};
	
	[UnmanagedCallersOnly(EntryPoint = "luaopen_test2")]
	public static int luaopen_test2(lua_State L)
	{
		luaL_newlib(L, test2Lib);
		lua_setglobal(L, "test2");
		return 1;
	}
	
}

// test4.csproj
// <PropertyGroup>
//     ...
//     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
// </PropertyGroup>
using LuaNET.LuaJIT;
using static LuaNET.LuaJIT.Lua;

namespace test4;

public class Test4
{
	
	public static void Main(string[] args)
	{
		lua_State L = luaL_newstate();
		if (L == 0)
		{
			Console.WriteLine("Unable to create context!");
		}
		luaL_openlibs(L);
		lua_getglobal(L, "require");
		lua_pushstring(L, "test2");
		int result = lua_pcall(L, 1, 0, 0);
		if (result != LUA_OK)
		{
			string? err = luaL_checkstring(L, 1);
			if (err != null)
			{
				Console.WriteLine($"1 Result: {err}");
			}
		}
		lua_getglobal(L, "test2");
		lua_getglobal(L, "print");
		lua_getfield(L, 1, "GetHello");
		result = lua_pcall(L, 0, 1, 0);
		if (result != LUA_OK)
		{
			string? err = luaL_checkstring(L, 1);
			if (err != null)
			{
				Console.WriteLine($"2 Result: {err}");
			}
		}
		result = lua_pcall(L, 1, 0, 0);
		if (result != LUA_OK)
		{
			string? err = luaL_checkstring(L, 1);
			if (err != null)
			{
				Console.WriteLine($"3 Result: {err}");
			}
		}
		lua_pop(L, 1);
		
		lua_close(L);
		
		Console.WriteLine("Success!");
	}
	
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.3 104 4/3/2024
4.1.0 150 1/24/2024
4.0.1 318 7/1/2023
3.0.0 149 6/21/2023
2.1.0 377 12/26/2022
2.0.0 592 6/27/2022
1.3.0 412 6/25/2022
1.2.0 403 6/25/2022
1.1.0 415 6/24/2022
1.0.0 401 6/20/2022