Skip to content
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

Add Lua natives to the library #5

Merged
merged 4 commits into from
Jan 5, 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
6 changes: 3 additions & 3 deletions .github/actions/dotnet/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ runs:
- name: Build
run: dotnet build $Env:SOLUTION_PATH -c Release --no-restore -p:NoWarn=1591
shell: pwsh
# - name: Test
# run: dotnet test $Env:SOLUTION_PATH --no-restore --verbosity minimal
# shell: pwsh
- name: Test
run: dotnet test $Env:SOLUTION_PATH --no-restore --verbosity minimal
shell: pwsh
- name: Pack
run: |
Invoke-Expression "dotnet pack $Env:SOLUTION_PATH -c Release -o ./artifacts --no-restore --no-build $($Env:VERSION_SUFFIX ? "--version-suffix=$Env:VERSION_SUFFIX" : $null)"
Expand Down
9 changes: 3 additions & 6 deletions src/Laylua.Playground/Laylua.Playground.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../Laylua.targets"/>
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>

<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
<RootNamespace>Laylua.Console</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Laylua\Laylua.csproj"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Laylua.Natives" Version="1.0.0-alpha.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Laylua.Tests/Laylua.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Laylua.Natives" Version="1.0.0-alpha.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
Expand Down
11 changes: 11 additions & 0 deletions src/Laylua.Tests/Tests/Moon/ErrorHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,15 @@ public void LuaError_LuaFunction_ProtectedLongJmp_GetsCaughtWithLuaException()
// Act & Assert
Assert.Throws<LuaException>(() => function.Call());
}

[Test]
public void LuaError_NestedLuaFunction_ProtectedLongJmp_GetsCaughtWithLuaException()
{
// Arrange
Lua.SetGlobal("print", (Action<int>) Console.WriteLine);
Lua.SetGlobal("func", (string code) => Lua.Execute(code));

// Act & Assert
Assert.Throws<LuaException>(() => Lua.Execute(@"print(func('print(\'abc\')'))"));
}
}
1 change: 1 addition & 0 deletions src/Laylua/Laylua.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<!-- </PropertyGroup>-->

<ItemGroup>
<PackageReference Include="Laylua.Natives" Version="1.0.0-alpha.1" PrivateAssets="analyzers;build" />
<PackageReference Include="Qommon" Version="4.0.1"/>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ public static IntPtr CreateLuaHookFunctionWrapper(LuaHookFunction function)
return asmPtr;
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Span<byte> AllocFunctionWrapperAsm(out IntPtr asmPtr)
{
Expand All @@ -272,7 +271,9 @@ private static void WriteFunctionWrapperPointers(Span<byte> asmSpan,
#endif
)
{
// var functionPtr = Marshal.GetFunctionPointerForDelegate(function);
#if TRACE_PANIC
var functionPtr = Marshal.GetFunctionPointerForDelegate(function);
#endif
var functionWrapperPtr = Marshal.GetFunctionPointerForDelegate(functionWrapper);

// MemoryMarshal.Write(asmSpan.Slice(21), ref functionPtr);
Expand Down
4 changes: 3 additions & 1 deletion src/Laylua/Moon/Native/Laylua/LayluaNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@
var asmSpan = Alloc(PanicAsmBytes, out var asmPtr);

var returnAddr = asmPtr + 0x1A;
MemoryMarshal.Write(asmSpan[3..], ref returnAddr);

Check warning on line 129 in src/Laylua/Moon/Native/Laylua/LayluaNative.cs

View workflow job for this annotation

GitHub Actions / build

The 'ref' modifier for argument 2 corresponding to 'in' parameter is equivalent to 'in'. Consider using 'in' instead.

RuntimeHelpers.PrepareMethod(panicMethodHandle);
var panicPtr = panicMethodHandle.GetFunctionPointer();
MemoryMarshal.Write(asmSpan[18..], ref panicPtr);

Check warning on line 133 in src/Laylua/Moon/Native/Laylua/LayluaNative.cs

View workflow job for this annotation

GitHub Actions / build

The 'ref' modifier for argument 2 corresponding to 'in' parameter is equivalent to 'in'. Consider using 'in' instead.

#if TRACE_PANIC
// Console.WriteLine("Stack State at 0x{0:X}", (IntPtr) stackState);
Expand Down Expand Up @@ -219,6 +219,8 @@

private static void InitializePanicHooks()
{
var libraryHandle = NativeLibrary.Load("lua54", typeof(LayluaNative).Assembly, null);

static IntPtr PrepareHookAsm(IntPtr export, IntPtr throwPanicExceptionPtr)
{
delegate* unmanaged[Cdecl]<lua_State*, void*, void*> mPtrDel = &SetPanicJump;
Expand All @@ -230,7 +232,7 @@
var asmSpan = Alloc(HookAsmBytes, out var asmPtr);

var panicJmpAddr = asmPtr + 202 + 10 + 216 * 2 + 136 + 50 + 19;
MemoryMarshal.Write(asmSpan.Slice(29 + 216), ref panicJmpAddr);

Check warning on line 235 in src/Laylua/Moon/Native/Laylua/LayluaNative.cs

View workflow job for this annotation

GitHub Actions / build

The 'ref' modifier for argument 2 corresponding to 'in' parameter is equivalent to 'in'. Consider using 'in' instead.
MemoryMarshal.Write(asmSpan.Slice(29 + 10 + 216), ref panicJmpAddr);
MemoryMarshal.Write(asmSpan.Slice(45 + 10 + 216), ref mPtr);
MemoryMarshal.Write(asmSpan.Slice(533), ref export);
Expand Down Expand Up @@ -263,7 +265,7 @@
if (delegateType == null)
throw new InvalidOperationException($"No matching panic delegate '{delegateName}' found.");

if (!NativeLibrary.TryGetExport(NativeLibrary.Load(OperatingSystem.IsWindows() ? "lua54" : "liblua54.so"), exportName, out var exportPtr))
if (!NativeLibrary.TryGetExport(libraryHandle, exportName, out var exportPtr))
{
if (method.GetCustomAttribute<OptionalExportAttribute>() == null)
throw new InvalidOperationException($"No export '{exportName}' found.");
Expand Down
7 changes: 7 additions & 0 deletions src/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="quahu" value="https://www.myget.org/F/quahu/api/v3/index.json" />
</packageSources>
</configuration>
Loading