Skip to content

Commit 5299721

Browse files
authored
Merge pull request #28 from tilkinsc/dev
Specify how to handle lambdas, disable warnings
2 parents 9ff0991 + 5b68289 commit 5299721

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Lua.NET.csproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2222
<RepositoryUrl>https://github.com/tilkinsc/Lua.NET</RepositoryUrl>
2323
<RepositoryType>git</RepositoryType>
24-
<NoWarn>CS8981</NoWarn>
24+
<NoWarn>
25+
CS8981, <!-- Only contains lowercase function names -->
26+
IDE1006, <!-- Naming rule violation -->
27+
CA1401, <!-- P/Invokes should not be visible -->
28+
SYSLIB1054 <!-- Use LibraryImportAttribute instead of DllImportAttribute to generate p/invoke marshalling code at compile time -->
29+
</NoWarn>
2530
</PropertyGroup>
2631

2732
<ItemGroup>

README.md

+28-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,34 @@ Hardcoded to only use doubles and 64-bit integers.
1616

1717
The delegates you pass to functions such as `lua_pushcfunction(...)` should be static.
1818
Otherwise, the lifetime of your functions should exceed the lifetime of the lua_State.
19-
Do not use lambdas, as C# is liable to GC them.
20-
A system to support lambdas may be added in the future, but it requires tracking lambda references.
19+
Do not use non-static lambdas, as C# is liable to GC them.
20+
You may get away with non-static lambdas if you track their references.
21+
When you track lambda references they should be cleaned up with the destruction of the lua_State.
22+
A system to support lambdas may be added in the future.
23+
24+
Acceptable lua_CFunction Pointers:
25+
```C#
26+
[UnmanagedCallersOnly]
27+
public static int lfunc(lua_State L)
28+
{
29+
...
30+
return 0;
31+
}
32+
...
33+
lua_pushcfunction(L, lfunc);
34+
```
35+
```C#
36+
lua_pushcfunction(L, static (L) => {
37+
...
38+
return 0;
39+
});
40+
```
2141

2242
# Shared Libraries
2343

2444
No shared library has to be built for this library, as its included for you.
2545
Custom DLLs are supported when building from source; they must retain ABI compatibility with Lua.
26-
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.
46+
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.
2747

2848
# Examples
2949

@@ -42,6 +62,7 @@ namespace TestLua;
4262
public class Test1
4363
{
4464

65+
[UnmanagedCallersOnly]
4566
public static int lfunc(lua_State L)
4667
{
4768
lua_getglobal(L, "print");
@@ -75,6 +96,7 @@ Example Usage LuaJIT:
7596
// ...
7697
// <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7798
// </PropertyGroup>
99+
using System.Runtime.InteropServices;
78100
using LuaNET.LuaJIT;
79101
using static LuaNET.LuaJIT.Lua;
80102

@@ -83,6 +105,7 @@ namespace TestLua;
83105
public class Test2
84106
{
85107

108+
[UnmanagedCallersOnly]
86109
public static int lfunc(lua_State L)
87110
{
88111
lua_getglobal(L, "print");
@@ -153,7 +176,8 @@ public unsafe class Test3
153176
}
154177

155178
}
156-
179+
```
180+
```C#
157181
// test4.csproj
158182
// <PropertyGroup>
159183
// ...

0 commit comments

Comments
 (0)