Browse Source

refactor: rename opcode methods for clarity

Akeit0 6 months ago
parent
commit
0d18be2462

+ 98 - 60
src/Lua/Runtime/LuaThreadAccessExtensions.cs

@@ -74,109 +74,147 @@ public static class LuaThreadAccessAccessExtensions
         return new LuaReturnValuesReader(stack, stack.Count - argumentCount);
         return new LuaReturnValuesReader(stack, stack.Count - argumentCount);
     }
     }
 
 
+    public static async ValueTask<LuaValue> Add(this LuaThreadAccess access, LuaValue x, LuaValue y, CancellationToken cancellationToken = default)
+    {
+        if (x.TryReadDouble(out var numX) && y.TryReadDouble(out var numY))
+        {
+            return numX + numY;
+        }
 
 
-    public static async ValueTask<LuaValue> Arithmetic(this LuaThreadAccess access, LuaValue x, LuaValue y, OpCode opCode, CancellationToken cancellationToken = default)
+        access.ThrowIfInvalid();
+        return await LuaVirtualMachine.ExecuteBinaryOperationMetaMethod(access.Thread, x, y, OpCode.Add, cancellationToken);
+    }
+
+    public static async ValueTask<LuaValue> Sub(this LuaThreadAccess access, LuaValue x, LuaValue y, CancellationToken cancellationToken = default)
     {
     {
-        [MethodImpl(MethodImplOptions.NoInlining)]
-        static double Mod(double a, double b)
+        if (x.TryReadDouble(out var numX) && y.TryReadDouble(out var numY))
         {
         {
-            var mod = a % b;
-            if ((b > 0 && mod < 0) || (b < 0 && mod > 0))
-            {
-                mod += b;
-            }
+            return numX - numY;
+        }
+
+        access.ThrowIfInvalid();
+        return await LuaVirtualMachine.ExecuteBinaryOperationMetaMethod(access.Thread, x, y, OpCode.Sub, cancellationToken);
+    }
 
 
-            return mod;
+    public static async ValueTask<LuaValue> Mul(this LuaThreadAccess access, LuaValue x, LuaValue y, CancellationToken cancellationToken = default)
+    {
+        if (x.TryReadDouble(out var numX) && y.TryReadDouble(out var numY))
+        {
+            return numX * numY;
         }
         }
 
 
-        [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        static double ArithmeticOperation(OpCode code, double a, double b)
+        access.ThrowIfInvalid();
+        return await LuaVirtualMachine.ExecuteBinaryOperationMetaMethod(access.Thread, x, y, OpCode.Mul, cancellationToken);
+    }
+
+    public static async ValueTask<LuaValue> Div(this LuaThreadAccess access, LuaValue x, LuaValue y, CancellationToken cancellationToken = default)
+    {
+        if (x.TryReadDouble(out var numX) && y.TryReadDouble(out var numY))
         {
         {
-            return code switch
-            {
-                OpCode.Add => a + b,
-                OpCode.Sub => a - b,
-                OpCode.Mul => a * b,
-                OpCode.Div => a / b,
-                OpCode.Mod => Mod(a, b),
-                OpCode.Pow => Math.Pow(a, b),
-                _ => throw new InvalidOperationException($"Unsupported arithmetic operation: {code}"),
-            };
+            return numX / numY;
         }
         }
 
 
+        access.ThrowIfInvalid();
+        return await LuaVirtualMachine.ExecuteBinaryOperationMetaMethod(access.Thread, x, y, OpCode.Div, cancellationToken);
+    }
 
 
+    public static async ValueTask<LuaValue> Mod(this LuaThreadAccess access, LuaValue x, LuaValue y, CancellationToken cancellationToken = default)
+    {
         if (x.TryReadDouble(out var numX) && y.TryReadDouble(out var numY))
         if (x.TryReadDouble(out var numX) && y.TryReadDouble(out var numY))
         {
         {
-            return ArithmeticOperation(opCode, numX, numY);
+            return LuaVirtualMachine.Mod(numX, numY);
         }
         }
 
 
         access.ThrowIfInvalid();
         access.ThrowIfInvalid();
-        return await LuaVirtualMachine.ExecuteBinaryOperationMetaMethod(access.Thread, x, y, opCode, cancellationToken);
+        return await LuaVirtualMachine.ExecuteBinaryOperationMetaMethod(access.Thread, x, y, OpCode.Mod, cancellationToken);
     }
     }
 
 
-    public static async ValueTask<LuaValue> Unary(this LuaThreadAccess access, LuaValue value, OpCode opCode, CancellationToken cancellationToken = default)
+    public static async ValueTask<LuaValue> Pow(this LuaThreadAccess access, LuaValue x, LuaValue y, CancellationToken cancellationToken = default)
     {
     {
-        if (opCode == OpCode.Unm)
+        if (x.TryReadDouble(out var numX) && y.TryReadDouble(out var numY))
         {
         {
-            if (value.TryReadDouble(out var numB))
-            {
-                return -numB;
-            }
+            return Math.Pow(numX, numY);
         }
         }
-        else if (opCode == OpCode.Len)
+
+        access.ThrowIfInvalid();
+        return await LuaVirtualMachine.ExecuteBinaryOperationMetaMethod(access.Thread, x, y, OpCode.Pow, cancellationToken);
+    }
+
+
+    public static async ValueTask<LuaValue> Unm(this LuaThreadAccess access, LuaValue value, CancellationToken cancellationToken = default)
+    {
+        if (value.TryReadDouble(out var numB))
         {
         {
-            if (value.TryReadString(out var str))
-            {
-                return str.Length;
-            }
+            return -numB;
+        }
 
 
-            if (value.TryReadTable(out var table))
-            {
-                return table.ArrayLength;
-            }
+        access.ThrowIfInvalid();
+        return await LuaVirtualMachine.ExecuteUnaryOperationMetaMethod(access.Thread, value, OpCode.Unm, cancellationToken);
+    }
+
+    public static async ValueTask<LuaValue> Len(this LuaThreadAccess access, LuaValue value, CancellationToken cancellationToken = default)
+    {
+        if (value.TryReadString(out var str))
+        {
+            return str.Length;
         }
         }
-        else
+
+        if (value.TryReadTable(out var table))
         {
         {
-            throw new InvalidOperationException($"Unsupported unary operation: {opCode}");
+            return table.ArrayLength;
         }
         }
 
 
         access.ThrowIfInvalid();
         access.ThrowIfInvalid();
-        return await LuaVirtualMachine.ExecuteUnaryOperationMetaMethod(access.Thread, value, opCode, cancellationToken);
+        return await LuaVirtualMachine.ExecuteUnaryOperationMetaMethod(access.Thread, value, OpCode.Len, cancellationToken);
     }
     }
 
 
 
 
-    public static async ValueTask<bool> Compare(this LuaThreadAccess access, LuaValue x, LuaValue y, OpCode opCode, CancellationToken cancellationToken = default)
+    public static async ValueTask<bool> LessThan(this LuaThreadAccess access, LuaValue x, LuaValue y, CancellationToken cancellationToken = default)
     {
     {
-        if (opCode is not (OpCode.Eq or OpCode.Lt or OpCode.Le))
+        if (x.TryReadNumber(out var numX) && y.TryReadNumber(out var numY))
         {
         {
-            throw new InvalidOperationException($"Unsupported compare operation: {opCode}");
+            return numX < numY;
         }
         }
 
 
-        if (opCode == OpCode.Eq)
+        if (x.TryReadString(out var strX) && y.TryReadString(out var strY))
         {
         {
-            if (x == y)
-            {
-                return true;
-            }
+            var c = StringComparer.Ordinal.Compare(strX, strY);
+            return c < 0;
         }
         }
-        else
+
+        access.ThrowIfInvalid();
+        return await LuaVirtualMachine.ExecuteCompareOperationMetaMethod(access.Thread, x, y, OpCode.Lt, cancellationToken);
+    }
+
+    public static async ValueTask<bool> LessThanOrEquals(this LuaThreadAccess access, LuaValue x, LuaValue y, CancellationToken cancellationToken = default)
+    {
+        if (x.TryReadNumber(out var numX) && y.TryReadNumber(out var numY))
         {
         {
-            if (x.TryReadNumber(out var numX) && y.TryReadNumber(out var numY))
-            {
-                return opCode == OpCode.Lt ? numX < numY : numX <= numY;
-            }
+            return numX <= numY;
+        }
 
 
-            if (x.TryReadString(out var strX) && y.TryReadString(out var strY))
-            {
-                var c = StringComparer.Ordinal.Compare(strX, strY);
-                return opCode == OpCode.Lt ? c < 0 : c <= 0;
-            }
+        if (x.TryReadString(out var strX) && y.TryReadString(out var strY))
+        {
+            var c = StringComparer.Ordinal.Compare(strX, strY);
+            return c <= 0;
         }
         }
 
 
         access.ThrowIfInvalid();
         access.ThrowIfInvalid();
-        return await LuaVirtualMachine.ExecuteCompareOperationMetaMethod(access.Thread, x, y, opCode, cancellationToken);
+        return await LuaVirtualMachine.ExecuteCompareOperationMetaMethod(access.Thread, x, y, OpCode.Le, cancellationToken);
     }
     }
 
 
+    public static async ValueTask<bool> Equals(this LuaThreadAccess access, LuaValue x, LuaValue y, CancellationToken cancellationToken = default)
+    {
+        if (x == y)
+        {
+            return true;
+        }
+
+        access.ThrowIfInvalid();
+        return await LuaVirtualMachine.ExecuteCompareOperationMetaMethod(access.Thread, x, y, OpCode.Eq, cancellationToken);
+    }
+
+
     public static async ValueTask<LuaValue> GetTable(this LuaThreadAccess access, LuaValue table, LuaValue key, CancellationToken cancellationToken = default)
     public static async ValueTask<LuaValue> GetTable(this LuaThreadAccess access, LuaValue table, LuaValue key, CancellationToken cancellationToken = default)
     {
     {
         if (table.TryReadTable(out var luaTable))
         if (table.TryReadTable(out var luaTable))

+ 11 - 0
src/Lua/Runtime/LuaVirtualMachine.cs

@@ -865,6 +865,17 @@ public static partial class LuaVirtualMachine
         throw new LuaRuntimeException(context.Thread, $"OpCode {opcode} is not implemented");
         throw new LuaRuntimeException(context.Thread, $"OpCode {opcode} is not implemented");
     }
     }
 
 
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    internal static double Mod(double a, double b)
+    {
+        var mod = a % b;
+        if ((b > 0 && mod < 0) || (b < 0 && mod > 0))
+        {
+            mod += b;
+        }
+
+        return mod;
+    }
 
 
     static void SelfPostOperation(VirtualMachineExecutionContext context, Span<LuaValue> results)
     static void SelfPostOperation(VirtualMachineExecutionContext context, Span<LuaValue> results)
     {
     {

+ 8 - 8
tests/Lua.Tests/LuaApiTests.cs

@@ -39,7 +39,7 @@ public class LuaApiTests
         var a = result[0].Read<LuaTable>();
         var a = result[0].Read<LuaTable>();
         var b = result[1].Read<LuaTable>();
         var b = result[1].Read<LuaTable>();
 
 
-        var c = await access.Arithmetic(a, b, OpCode.Add);
+        var c = await access.Add(a, b);
         var table = c.Read<LuaTable>();
         var table = c.Read<LuaTable>();
         Assert.Multiple(() =>
         Assert.Multiple(() =>
         {
         {
@@ -72,7 +72,7 @@ public class LuaApiTests
 
 
         var result = await access.DoStringAsync(source);
         var result = await access.DoStringAsync(source);
         var a = result[0].Read<LuaTable>();
         var a = result[0].Read<LuaTable>();
-        var c = await access.Unary(a, OpCode.Unm);
+        var c = await access.Unm(a);
         var table = c.Read<LuaTable>();
         var table = c.Read<LuaTable>();
         Assert.Multiple(() =>
         Assert.Multiple(() =>
         {
         {
@@ -111,9 +111,9 @@ public class LuaApiTests
         var a = result[0].Read<LuaTable>();
         var a = result[0].Read<LuaTable>();
         var b = result[1].Read<LuaTable>();
         var b = result[1].Read<LuaTable>();
         var c = result[2].Read<LuaTable>();
         var c = result[2].Read<LuaTable>();
-        var ab = await access.Compare(a, b, OpCode.Eq);
+        var ab = await access.Equals(a, b);
         Assert.False(ab);
         Assert.False(ab);
-        var ac = await access.Compare(a, c, OpCode.Eq);
+        var ac = await access.Equals(a, c);
         Assert.True(ac);
         Assert.True(ac);
     }
     }
 
 
@@ -234,10 +234,10 @@ return a,b,c
         var a = result[0];
         var a = result[0];
         var b = result[1];
         var b = result[1];
         var c = result[2];
         var c = result[2];
-        var d = await access.Arithmetic(b, c, OpCode.Add);
+        var d = await access.Add(b, c);
         Assert.True(d.TryRead(out string s));
         Assert.True(d.TryRead(out string s));
         Assert.That(s, Is.EqualTo("abc"));
         Assert.That(s, Is.EqualTo("abc"));
-        d = await access.Unary(b, OpCode.Unm);
+        d = await access.Unm(b);
         Assert.True(d.TryRead(out s));
         Assert.True(d.TryRead(out s));
         Assert.That(s, Is.EqualTo("abb"));
         Assert.That(s, Is.EqualTo("abb"));
         d = await access.Concat([c, b]);
         d = await access.Concat([c, b]);
@@ -277,10 +277,10 @@ return a,b,c
         var a = result[0];
         var a = result[0];
         var b = result[1];
         var b = result[1];
         var c = result[2];
         var c = result[2];
-        var d = await access.Arithmetic(b, c, OpCode.Add);
+        var d = await access.Add(b, c);
         Assert.True(d.TryRead(out string s));
         Assert.True(d.TryRead(out string s));
         Assert.That(s, Is.EqualTo("abc"));
         Assert.That(s, Is.EqualTo("abc"));
-        d = await access.Unary(b, OpCode.Unm);
+        d = await access.Unm(b);
         Assert.True(d.TryRead(out s));
         Assert.True(d.TryRead(out s));
         Assert.That(s, Is.EqualTo("abb"));
         Assert.That(s, Is.EqualTo("abb"));
         d = await access.Concat([c, b]);
         d = await access.Concat([c, b]);