Skip to content

Specify how to handle lambdas, disable warnings #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lua.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/tilkinsc/Lua.NET</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<NoWarn>CS8981</NoWarn>
<NoWarn>
CS8981, <!-- Only contains lowercase function names -->
IDE1006, <!-- Naming rule violation -->
CA1401, <!-- P/Invokes should not be visible -->
SYSLIB1054 <!-- Use LibraryImportAttribute instead of DllImportAttribute to generate p/invoke marshalling code at compile time -->
</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,34 @@ Hardcoded to only use doubles and 64-bit integers.

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.
Do not use non-static lambdas, as C# is liable to GC them.
You may get away with non-static lambdas if you track their references.
When you track lambda references they should be cleaned up with the destruction of the lua_State.
A system to support lambdas may be added in the future.

Acceptable lua_CFunction Pointers:
```C#
[UnmanagedCallersOnly]
public static int lfunc(lua_State L)
{
...
return 0;
}
...
lua_pushcfunction(L, lfunc);
```
```C#
lua_pushcfunction(L, static (L) => {
...
return 0;
});
```

# 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.
If using custom shared libraries, you will need to replace the respective 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

Expand All @@ -41,6 +61,7 @@ namespace TestLua;
public class Test1
{

[UnmanagedCallersOnly]
public static int lfunc(lua_State L)
{
lua_getglobal(L, "print");
Expand Down Expand Up @@ -74,6 +95,7 @@ Example Usage LuaJIT:
// ...
// <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
// </PropertyGroup>
using System.Runtime.InteropServices;
using LuaNET.LuaJIT;
using static LuaNET.LuaJIT.Lua;

Expand All @@ -82,6 +104,7 @@ namespace TestLua;
public class Test2
{

[UnmanagedCallersOnly]
public static int lfunc(lua_State L)
{
lua_getglobal(L, "print");
Expand Down Expand Up @@ -152,7 +175,8 @@ public unsafe class Test3
}

}

```
```C#
// test4.csproj
// <PropertyGroup>
// ...
Expand Down
Loading