A direct C# port of Lua 5.5.1 for embedding Lua in .NET applications, with support for both JIT and Native AOT deployment. The public API follows the original C API and is exposed through LinkBridge.Lua.LuaApi.
The implementation is self-contained and has no native Lua library, P/Invoke, runtime code generation or NuGet runtime dependencies. It uses unsafe code to preserve Lua's pointer-based runtime model.
- .NET 9 SDK or newer
AllowUnsafeBlocksenabled in consuming projects
dotnet build Lua551.sln -c ReleaseTo consume the project directly, add a project reference:
<ItemGroup>
<ProjectReference Include="path/to/Lua551.csproj" />
</ItemGroup>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>dotnet pack Lua551.csproj -c Release -o artifactsGitHub Actions builds Lua551.NET.1.0.0.nupkg on every push and pull request and uploads it as a workflow artifact.
Public releases are published to nuget.org through Trusted Publishing. Create a GitHub release with a tag such as v1.0.0; the release workflow uses the tag as the package version, runs the tests and Native AOT smoke check, then publishes the package.
dotnet add package Lua551.NET --version 1.0.0using System;
using LinkBridge.Lua;
using static LinkBridge.Lua.LuaApi;
unsafe
{
lua_State* state = luaL_newstate();
if (state == null) throw new InvalidOperationException("Could not create a Lua state.");
try
{
luaL_openlibs(state);
int status = luaL_loadstring(state, CSTR("return 6 * 7"u8));
if (status == LUA_OK) status = lua_pcall(state, 0, 1, 0);
if (status != LUA_OK) throw new InvalidOperationException(CStr(lua_tostring(state, -1)));
Console.WriteLine(lua_tointeger(state, -1)); // 42
}
finally
{
lua_close(state);
}
}The library is compatible with .NET Native AOT and does not rely on reflection or runtime code generation. The repository includes a smoke application that creates a Lua state, opens the standard libraries, evaluates return 6 * 7 and checks the result.
dotnet publish tests/Lua551.AotSmoke/Lua551.AotSmoke.csproj -c Release -r win-x64The published executable is run in CI to verify that the native binary starts and executes Lua code successfully.
dotnet test tests/Lua551.Tests/Lua551.Tests.csproj -c ReleaseThe test project runs the official Lua 5.5.1 correctness suite in portable basic mode (_U=true) through an xUnit integration test.
The upstream suite is stored unchanged under tests/official/lua-5.5.1-tests. On Windows, the runner skips only the /dev/null and /dev/full flush block because those POSIX devices do not exist. Complete and internal modes need native helper libraries and are not run by CI.
- The API intentionally stays close to Lua's C API, including function pointers and unmanaged memory.
- Default module paths currently follow Windows conventions.
- Applications are responsible for closing each created Lua state.
This project and the original Lua sources are distributed under the MIT license. See LICENSE and LICENSE_LUA.txt.