Skip to content

Commit 0a5163a

Browse files
authored
Add Lua natives to the library (#5)
* Add Windows and Linux natives * Add nested pcall longjmp test * Re-enable pipeline tests * Use a separate package for natives
1 parent 6e21140 commit 0a5163a

File tree

8 files changed

+32
-12
lines changed

8 files changed

+32
-12
lines changed

.github/actions/dotnet/action.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ runs:
1919
- name: Build
2020
run: dotnet build $Env:SOLUTION_PATH -c Release --no-restore -p:NoWarn=1591
2121
shell: pwsh
22-
# - name: Test
23-
# run: dotnet test $Env:SOLUTION_PATH --no-restore --verbosity minimal
24-
# shell: pwsh
22+
- name: Test
23+
run: dotnet test $Env:SOLUTION_PATH --no-restore --verbosity minimal
24+
shell: pwsh
2525
- name: Pack
2626
run: |
2727
Invoke-Expression "dotnet pack $Env:SOLUTION_PATH -c Release -o ./artifacts --no-restore --no-build $($Env:VERSION_SUFFIX ? "--version-suffix=$Env:VERSION_SUFFIX" : $null)"

src/Laylua.Playground/Laylua.Playground.csproj

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<Import Project="../Laylua.targets"/>
33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
4+
<TargetFramework>net8.0</TargetFramework>
55

66
<OutputType>Exe</OutputType>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<IsPackable>false</IsPackable>
99
<RootNamespace>Laylua.Console</RootNamespace>
1010
</PropertyGroup>
1111

12-
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
13-
<PlatformTarget>x64</PlatformTarget>
14-
</PropertyGroup>
15-
1612
<ItemGroup>
1713
<ProjectReference Include="..\Laylua\Laylua.csproj"/>
1814
</ItemGroup>
1915

2016
<ItemGroup>
17+
<PackageReference Include="Laylua.Natives" Version="1.0.0-alpha.1" />
2118
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
2219
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
2320
</ItemGroup>

src/Laylua.Tests/Laylua.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16+
<PackageReference Include="Laylua.Natives" Version="1.0.0-alpha.1" />
1617
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
1718
<PackageReference Include="NUnit" Version="4.2.2" />
1819
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />

src/Laylua.Tests/Tests/Moon/ErrorHandlingTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ public void LuaError_LuaFunction_ProtectedLongJmp_GetsCaughtWithLuaException()
4949
// Act & Assert
5050
Assert.Throws<LuaException>(() => function.Call());
5151
}
52+
53+
[Test]
54+
public void LuaError_NestedLuaFunction_ProtectedLongJmp_GetsCaughtWithLuaException()
55+
{
56+
// Arrange
57+
Lua.SetGlobal("print", (Action<int>) Console.WriteLine);
58+
Lua.SetGlobal("func", (string code) => Lua.Execute(code));
59+
60+
// Act & Assert
61+
Assert.Throws<LuaException>(() => Lua.Execute(@"print(func('print(\'abc\')'))"));
62+
}
5263
}

src/Laylua/Laylua.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<!-- </PropertyGroup>-->
1414

1515
<ItemGroup>
16+
<PackageReference Include="Laylua.Natives" Version="1.0.0-alpha.1" PrivateAssets="analyzers;build" />
1617
<PackageReference Include="Qommon" Version="4.0.1"/>
1718
</ItemGroup>
1819

src/Laylua/Moon/Native/Laylua/LayluaNative.FunctionWrappers.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ public static IntPtr CreateLuaHookFunctionWrapper(LuaHookFunction function)
253253
return asmPtr;
254254
}
255255

256-
257256
[MethodImpl(MethodImplOptions.AggressiveInlining)]
258257
private static Span<byte> AllocFunctionWrapperAsm(out IntPtr asmPtr)
259258
{
@@ -272,7 +271,9 @@ private static void WriteFunctionWrapperPointers(Span<byte> asmSpan,
272271
#endif
273272
)
274273
{
275-
// var functionPtr = Marshal.GetFunctionPointerForDelegate(function);
274+
#if TRACE_PANIC
275+
var functionPtr = Marshal.GetFunctionPointerForDelegate(function);
276+
#endif
276277
var functionWrapperPtr = Marshal.GetFunctionPointerForDelegate(functionWrapper);
277278

278279
// MemoryMarshal.Write(asmSpan.Slice(21), ref functionPtr);

src/Laylua/Moon/Native/Laylua/LayluaNative.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ internal static void ThrowPanicException(lua_State* L)
219219

220220
private static void InitializePanicHooks()
221221
{
222+
var libraryHandle = NativeLibrary.Load("lua54", typeof(LayluaNative).Assembly, null);
223+
222224
static IntPtr PrepareHookAsm(IntPtr export, IntPtr throwPanicExceptionPtr)
223225
{
224226
delegate* unmanaged[Cdecl]<lua_State*, void*, void*> mPtrDel = &SetPanicJump;
@@ -263,7 +265,7 @@ static IntPtr PrepareHookAsm(IntPtr export, IntPtr throwPanicExceptionPtr)
263265
if (delegateType == null)
264266
throw new InvalidOperationException($"No matching panic delegate '{delegateName}' found.");
265267

266-
if (!NativeLibrary.TryGetExport(NativeLibrary.Load(OperatingSystem.IsWindows() ? "lua54" : "liblua54.so"), exportName, out var exportPtr))
268+
if (!NativeLibrary.TryGetExport(libraryHandle, exportName, out var exportPtr))
267269
{
268270
if (method.GetCustomAttribute<OptionalExportAttribute>() == null)
269271
throw new InvalidOperationException($"No export '{exportName}' found.");

src/nuget.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
5+
<add key="quahu" value="https://www.myget.org/F/quahu/api/v3/index.json" />
6+
</packageSources>
7+
</configuration>

0 commit comments

Comments
 (0)