Akeit0 7 months ago
parent
commit
a00b966af3

+ 14 - 14
sandbox/Benchmark/Benchmark.csproj

@@ -1,20 +1,20 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
-  <PropertyGroup>
-    <OutputType>Exe</OutputType>
-    <TargetFramework>net8.0</TargetFramework>
-    <ImplicitUsings>enable</ImplicitUsings>
-    <Nullable>enable</Nullable>
-  </PropertyGroup>
+    <PropertyGroup>
+        <OutputType>Exe</OutputType>
+        <TargetFramework>net8.0</TargetFramework>
+        <ImplicitUsings>enable</ImplicitUsings>
+        <Nullable>enable</Nullable>
+    </PropertyGroup>
 
-  <ItemGroup>
-    <PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
-    <PackageReference Include="MoonSharp" Version="2.0.0" />
-    <PackageReference Include="NLua" Version="1.7.3" />
-  </ItemGroup>
+    <ItemGroup>
+        <PackageReference Include="BenchmarkDotNet" Version="0.14.0"/>
+        <PackageReference Include="MoonSharp" Version="2.0.0"/>
+        <PackageReference Include="NLua" Version="1.7.3"/>
+    </ItemGroup>
 
-  <ItemGroup>
-    <ProjectReference Include="..\..\src\Lua\Lua.csproj" />
-  </ItemGroup>
+    <ItemGroup>
+        <ProjectReference Include="..\..\src\Lua\Lua.csproj"/>
+    </ItemGroup>
 
 </Project>

+ 18 - 18
src/Lua.SourceGenerator/Lua.SourceGenerator.csproj

@@ -1,23 +1,23 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
-  <PropertyGroup>
-    <TargetFramework>netstandard2.0</TargetFramework>
-    <LangVersion>12</LangVersion>
-    <ImplicitUsings>enable</ImplicitUsings>
-    <Nullable>enable</Nullable>
-    <IsRoslynComponent>true</IsRoslynComponent>
-    <AnalyzerLanguage>cs</AnalyzerLanguage>
-    <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
-    <IncludeBuildOutput>false</IncludeBuildOutput>
-    <DevelopmentDependency>true</DevelopmentDependency>
-    <IncludeSymbols>false</IncludeSymbols>
-    <SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
-  </PropertyGroup>
+    <PropertyGroup>
+        <TargetFramework>netstandard2.0</TargetFramework>
+        <LangVersion>12</LangVersion>
+        <ImplicitUsings>enable</ImplicitUsings>
+        <Nullable>enable</Nullable>
+        <IsRoslynComponent>true</IsRoslynComponent>
+        <AnalyzerLanguage>cs</AnalyzerLanguage>
+        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
+        <IncludeBuildOutput>false</IncludeBuildOutput>
+        <DevelopmentDependency>true</DevelopmentDependency>
+        <IncludeSymbols>false</IncludeSymbols>
+        <SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
+    </PropertyGroup>
 
-  <ItemGroup>
-    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" />
-	
-	  <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
-  </ItemGroup>
+    <ItemGroup>
+        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0"/>
+
+        <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
+    </ItemGroup>
 
 </Project>

+ 1 - 1
src/Lua.SourceGenerator/LuaObjectMetamethod.cs

@@ -20,4 +20,4 @@ internal enum LuaObjectMetamethod
     Pairs,
     IPairs,
     ToString,
-}
+}

+ 1 - 0
src/Lua/Internal/HexConverter.cs

@@ -100,6 +100,7 @@ public static class HexConverter
                     return (c - 'a' + 10);
             }
         }
+
         return 0;
     }
 }

+ 2 - 2
src/Lua/Internal/LuaValueDictionary.cs

@@ -366,13 +366,13 @@ namespace Lua.Internal
         }
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public bool TryGetNext(LuaValue key, out KeyValuePair<LuaValue,LuaValue> pair)
+        public bool TryGetNext(LuaValue key, out KeyValuePair<LuaValue, LuaValue> pair)
         {
             ref LuaValue valRef = ref FindValue(key, out var index);
             if (!Unsafe.IsNullRef(ref valRef))
             {
                 Entry[] entries = _entries!;
-                while (++index  < _count)
+                while (++index < _count)
                 {
                     ref Entry entry = ref entries[index];
                     if (entry is { next: >= -1, value.Type: not LuaValueType.Nil })

+ 3 - 1
src/Lua/Internal/MathEx.cs

@@ -2,6 +2,7 @@ using System.Runtime.CompilerServices;
 #if NET6_0_OR_GREATER
 using System.Numerics;
 #endif
+
 namespace Lua;
 
 internal static class MathEx
@@ -77,6 +78,7 @@ internal static class MathEx
                 exp = (int)((bits & DBL_EXP_MASK) >> DBL_MANT_BITS);
                 e = exp - 1022 - 54;
             }
+
             // Set exponent to -1 so that d is in [0.5, 1).
             d = BitConverter.Int64BitsToDouble((bits & DBL_EXP_CLR_MASK) | 0x3fe0000000000000L);
         }
@@ -88,7 +90,7 @@ internal static class MathEx
     {
         return ((int)Math.Truncate(d), d % 1.0);
     }
-    
+
     /// <summary>Returns the smallest power of two greater than or equal to the input.</summary>
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     public static int NextPowerOfTwo(int x)

+ 4 - 0
src/Lua/Internal/ValueStringBuilder.cs

@@ -75,6 +75,7 @@ namespace System.Text
                 EnsureCapacity(Length + 1);
                 _chars[Length] = '\0';
             }
+
             return ref MemoryMarshal.GetReference(_chars);
         }
 
@@ -108,6 +109,7 @@ namespace System.Text
                 EnsureCapacity(Length + 1);
                 _chars[Length] = '\0';
             }
+
             return _chars.Slice(0, _pos);
         }
 
@@ -232,6 +234,7 @@ namespace System.Text
             {
                 dst[i] = c;
             }
+
             _pos += count;
         }
 
@@ -248,6 +251,7 @@ namespace System.Text
             {
                 dst[i] = *value++;
             }
+
             _pos += length;
         }
 

+ 5 - 0
src/Lua/Internal/ValueTaskEx.cs

@@ -125,6 +125,7 @@ internal static class ValueTaskEx
                 TryInvokeContinuation();
                 return;
             }
+
             TryInvokeContinuationWithIncrement(0);
         }
 
@@ -140,6 +141,7 @@ internal static class ValueTaskEx
                 TryInvokeContinuation();
                 return;
             }
+
             TryInvokeContinuationWithIncrement(1);
         }
 
@@ -187,6 +189,7 @@ internal static class ValueTaskEx
             {
                 exception.Throw();
             }
+
             var i = winArgumentIndex;
             return (winArgumentIndex, t0, t1);
         }
@@ -221,10 +224,12 @@ internal static class ValueTaskEx
             {
                 execContext = ExecutionContext.Capture();
             }
+
             if ((flags & ValueTaskSourceOnCompletedFlags.UseSchedulingContext) == ValueTaskSourceOnCompletedFlags.UseSchedulingContext)
             {
                 syncContext = SynchronizationContext.Current;
             }
+
             this.state = state;
 
             if (GetStatus(token) != ValueTaskSourceStatus.Pending)

+ 40 - 40
src/Lua/Lua.csproj

@@ -1,44 +1,44 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
-  <PropertyGroup>
-    <TargetFrameworks>net8.0; net6.0; netstandard2.1</TargetFrameworks>
-    <LangVersion>13</LangVersion>
-    <ImplicitUsings>enable</ImplicitUsings>
-    <Nullable>enable</Nullable>
-    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-
-    <!-- NuGet Packaging -->
-    <PackageId>LuaCSharp</PackageId>
-    <PackageTags>lua;interpreter</PackageTags>
-    <Description>High performance Lua interpreter implemented in C# for .NET and Unity</Description>
-  </PropertyGroup>
-
-  <ItemGroup>
-    <PackageReference Include="PolySharp" Version="1.14.1">
-      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
-      <PrivateAssets>all</PrivateAssets>
-    </PackageReference>
-
-    <None Include="..\Lua.SourceGenerator\bin\$(Configuration)\netstandard2.0\Lua.SourceGenerator.dll"
-          PackagePath="analyzers\dotnet\cs"
-          Pack="true"
-          Visible="false" />
-  </ItemGroup>
-
-  <ItemGroup Condition="$(TargetFramework) == 'netstandard2.1'">
-      <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
-  </ItemGroup>
-  
-  <ItemGroup>
-      <None Include="../../Icon.png" Pack="true" PackagePath="/" />
-      <None Include="..\..\README.md" Pack="true" PackagePath="README.md" />
-      <EmbeddedResource Include="..\..\LICENSE" />
-  </ItemGroup>
-  
-  <ItemGroup>
-    <ProjectReference Include="..\Lua.SourceGenerator\Lua.SourceGenerator.csproj"
-      OutputItemType="Analyzer"
-      ReferenceOutputAssembly="false"/>
-  </ItemGroup>
+    <PropertyGroup>
+        <TargetFrameworks>net8.0; net6.0; netstandard2.1</TargetFrameworks>
+        <LangVersion>13</LangVersion>
+        <ImplicitUsings>enable</ImplicitUsings>
+        <Nullable>enable</Nullable>
+        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+
+        <!-- NuGet Packaging -->
+        <PackageId>LuaCSharp</PackageId>
+        <PackageTags>lua;interpreter</PackageTags>
+        <Description>High performance Lua interpreter implemented in C# for .NET and Unity</Description>
+    </PropertyGroup>
+
+    <ItemGroup>
+        <PackageReference Include="PolySharp" Version="1.14.1">
+            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+            <PrivateAssets>all</PrivateAssets>
+        </PackageReference>
+
+        <None Include="..\Lua.SourceGenerator\bin\$(Configuration)\netstandard2.0\Lua.SourceGenerator.dll"
+              PackagePath="analyzers\dotnet\cs"
+              Pack="true"
+              Visible="false"/>
+    </ItemGroup>
+
+    <ItemGroup Condition="$(TargetFramework) == 'netstandard2.1'">
+        <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0"/>
+    </ItemGroup>
+
+    <ItemGroup>
+        <None Include="../../Icon.png" Pack="true" PackagePath="/"/>
+        <None Include="..\..\README.md" Pack="true" PackagePath="README.md"/>
+        <EmbeddedResource Include="..\..\LICENSE"/>
+    </ItemGroup>
+
+    <ItemGroup>
+        <ProjectReference Include="..\Lua.SourceGenerator\Lua.SourceGenerator.csproj"
+                          OutputItemType="Analyzer"
+                          ReferenceOutputAssembly="false"/>
+    </ItemGroup>
 
 </Project>

+ 1 - 1
src/Lua/LuaObjectMetamethod.cs

@@ -18,4 +18,4 @@ public enum LuaObjectMetamethod
     Pairs,
     IPairs,
     ToString,
-}
+}

+ 3 - 11
src/Lua/Runtime/UpValue.cs

@@ -1,4 +1,3 @@
-
 using System.Runtime.CompilerServices;
 
 namespace Lua.Runtime;
@@ -18,19 +17,12 @@ public sealed class UpValue
 
     public static UpValue Open(LuaThread thread, int registerIndex)
     {
-        return new(thread)
-        {
-            RegisterIndex = registerIndex
-        };
+        return new(thread) { RegisterIndex = registerIndex };
     }
 
     public static UpValue Closed(LuaValue value)
     {
-        return new(null)
-        {
-            IsClosed = true,
-            value = value
-        };
+        return new(null) { IsClosed = true, value = value };
     }
 
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -45,7 +37,7 @@ public sealed class UpValue
             return Thread!.Stack.Get(RegisterIndex);
         }
     }
-    
+
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     internal ref readonly LuaValue GetValueRef()
     {

+ 21 - 21
tests/Lua.Tests/Lua.Tests.csproj

@@ -1,28 +1,28 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
-  <PropertyGroup>
-    <TargetFramework>net8.0</TargetFramework>
-    <ImplicitUsings>enable</ImplicitUsings>
-    <Nullable>enable</Nullable>
+    <PropertyGroup>
+        <TargetFramework>net8.0</TargetFramework>
+        <ImplicitUsings>enable</ImplicitUsings>
+        <Nullable>enable</Nullable>
 
-    <IsPackable>false</IsPackable>
-    <IsTestProject>true</IsTestProject>
-  </PropertyGroup>
+        <IsPackable>false</IsPackable>
+        <IsTestProject>true</IsTestProject>
+    </PropertyGroup>
 
-  <ItemGroup>
-    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
-    <PackageReference Include="NUnit" Version="3.13.3" />
-    <PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
-    <PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
-    <PackageReference Include="coverlet.collector" Version="6.0.0" />
-  </ItemGroup>
+    <ItemGroup>
+        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
+        <PackageReference Include="NUnit" Version="3.13.3"/>
+        <PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
+        <PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
+        <PackageReference Include="coverlet.collector" Version="6.0.0"/>
+    </ItemGroup>
 
-  <ItemGroup>
-    <ProjectReference Include="..\..\src\Lua\Lua.csproj" />
-    <ProjectReference Include="..\..\src\Lua.SourceGenerator\Lua.SourceGenerator.csproj">
-			<OutputItemType>Analyzer</OutputItemType>
-			<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
-		</ProjectReference>
-  </ItemGroup>
+    <ItemGroup>
+        <ProjectReference Include="..\..\src\Lua\Lua.csproj"/>
+        <ProjectReference Include="..\..\src\Lua.SourceGenerator\Lua.SourceGenerator.csproj">
+            <OutputItemType>Analyzer</OutputItemType>
+            <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
+        </ProjectReference>
+    </ItemGroup>
 
 </Project>

+ 3 - 12
tests/Lua.Tests/LuaObjectTests.cs

@@ -47,10 +47,7 @@ public class LuaObjectTests
     [Test]
     public async Task Test_Property()
     {
-        var userData = new TestUserData()
-        {
-            Property = 1
-        };
+        var userData = new TestUserData() { Property = 1 };
 
         var state = LuaState.Create();
         state.Environment["test"] = userData;
@@ -63,10 +60,7 @@ public class LuaObjectTests
     [Test]
     public async Task Test_PropertyWithName()
     {
-        var userData = new TestUserData()
-        {
-            PropertyWithName = "foo",
-        };
+        var userData = new TestUserData() { PropertyWithName = "foo", };
 
         var state = LuaState.Create();
         state.Environment["test"] = userData;
@@ -116,10 +110,7 @@ public class LuaObjectTests
     [Test]
     public async Task Test_InstanceMethodWithReturnValue()
     {
-        var userData = new TestUserData()
-        {
-            Property = 1
-        };
+        var userData = new TestUserData() { Property = 1 };
 
         var state = LuaState.Create();
         state.Environment["test"] = userData;

+ 1 - 0
tests/Lua.Tests/LuaTests.cs

@@ -20,6 +20,7 @@ public class LuaTests
     [TestCase("tests-lua/goto.lua")]
     [TestCase("tests-lua/constructs.lua")]
     [TestCase("tests-lua/locals.lua")]
+    // [TestCase("tests-lua/literals.lua")]
     //[TestCase("tests-lua/pm.lua")] string.match is not implemented
     //[TestCase("tests-lua/sort.lua")] //check for "invalid order function" is not implemented
     //[TestCase("tests-lua/calls.lua")] //  string.dump and reader function for load chunk is not implemented

+ 33 - 1
tests/Lua.Tests/MetatableTests.cs

@@ -32,7 +32,7 @@ local b = { 4, 5, 6 }
 
 setmetatable(a, metatable)
 
-return a +b
+return a + b
 ";
 
         var result = await state.DoStringAsync(source);
@@ -46,7 +46,39 @@ return a +b
             Assert.That(table[3].Read<double>(), Is.EqualTo(9));
         });
     }
+    [Test]
+    public async Task Test_Metamethod_Concat()
+    {
+        var source = @"
+metatable = {
+    __concat = function(a, b)
+        local t = { }
+        for i = 1, #a do
+            t[i] = a[i] + b[i]
+        end
+        return t
+    end
+}
 
+local a = { 1, 2, 3 }
+local b = { 4, 5, 6 }
+
+setmetatable(a, metatable)
+
+return a .. b
+";
+
+        var result = await state.DoStringAsync(source);
+        Assert.That(result, Has.Length.EqualTo(1));
+
+        var table = result[0].Read<LuaTable>();
+        Assert.Multiple(() =>
+        {
+            Assert.That(table[1].Read<double>(), Is.EqualTo(5));
+            Assert.That(table[2].Read<double>(), Is.EqualTo(7));
+            Assert.That(table[3].Read<double>(), Is.EqualTo(9));
+        });
+    }
     [Test]
     public async Task Test_Metamethod_Index()
     {