Browse Source

fix: improve LuaValue handling and simplify object conversion

Akeit0 7 months ago
parent
commit
6d473fdd97

+ 26 - 5
src/Lua/LuaValue.cs

@@ -396,13 +396,33 @@ public readonly struct LuaValue : IEquatable<LuaValue>
         return true;
         return true;
     }
     }
 
 
+    public static LuaValue FromObject(object obj)
+    {
+        return obj switch
+        {
+            null => Nil,
+            LuaValue luaValue => luaValue,
+            bool boolValue => boolValue,
+            double doubleValue => doubleValue,
+            string stringValue => stringValue,
+            LuaFunction luaFunction => luaFunction,
+            LuaTable luaTable => luaTable,
+            LuaThread luaThread => luaThread,
+            ILuaUserData userData => FromUserData(userData),
+            int intValue => intValue,
+            long longValue => longValue,
+            float floatValue => floatValue,
+            _ => new LuaValue(obj)
+        };
+    }
+
     public static LuaValue FromUserData(ILuaUserData userData)
     public static LuaValue FromUserData(ILuaUserData userData)
     {
     {
-        return new (userData);
+        return new(userData);
     }
     }
 
 
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
-    internal LuaValue(object obj)
+    LuaValue(object obj)
     {
     {
         Type = LuaValueType.LightUserData;
         Type = LuaValueType.LightUserData;
         referenceValue = obj;
         referenceValue = obj;
@@ -552,8 +572,8 @@ public readonly struct LuaValue : IEquatable<LuaValue>
             _ => "",
             _ => "",
         };
         };
     }
     }
-    
-    public  string TypeToString()
+
+    public string TypeToString()
     {
     {
         return Type switch
         return Type switch
         {
         {
@@ -569,6 +589,7 @@ public readonly struct LuaValue : IEquatable<LuaValue>
             _ => "",
             _ => "",
         };
         };
     }
     }
+
     public static string ToString(LuaValueType type)
     public static string ToString(LuaValueType type)
     {
     {
         return type switch
         return type switch
@@ -635,7 +656,7 @@ public readonly struct LuaValue : IEquatable<LuaValue>
             var stack = context.Thread.Stack;
             var stack = context.Thread.Stack;
             stack.Push(metamethod);
             stack.Push(metamethod);
             stack.Push(this);
             stack.Push(this);
-            return LuaVirtualMachine.Call(context.Thread,stack.Count-2, stack.Count - 2, cancellationToken);
+            return LuaVirtualMachine.Call(context.Thread, stack.Count - 2, stack.Count - 2, cancellationToken);
         }
         }
         else
         else
         {
         {

+ 1 - 1
src/Lua/Runtime/LuaThreadAccessExtensions.cs

@@ -187,7 +187,7 @@ public static class LuaThreadAccessAccessExtensions
         {
         {
             if (luaTable.TryGetValue(key, out var value))
             if (luaTable.TryGetValue(key, out var value))
             {
             {
-                return new(value);
+                return value;
             }
             }
         }
         }
 
 

+ 1 - 1
src/Lua/Standard/DebugLibrary.cs

@@ -378,7 +378,7 @@ public class DebugLibrary
             return new(context.Return(LuaValue.Nil));
             return new(context.Return(LuaValue.Nil));
         }
         }
 
 
-        return new(context.Return(new LuaValue(upValues[n1 - 1])));
+        return new(context.Return( LuaValue.FromObject(upValues[n1 - 1])));
     }
     }
 
 
     public ValueTask<int> UpValueJoin(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
     public ValueTask<int> UpValueJoin(LuaFunctionExecutionContext context, CancellationToken cancellationToken)

+ 1 - 1
src/Lua/Standard/StringLibrary.cs

@@ -426,7 +426,7 @@ public sealed class StringLibrary
         var regex = StringHelper.ToRegex(pattern);
         var regex = StringHelper.ToRegex(pattern);
         var matches = regex.Matches(s);
         var matches = regex.Matches(s);
 
 
-        return new(context.Return(new CSharpClosure("iterator", [new LuaValue(matches), 0], static (context, cancellationToken) =>
+        return new(context.Return(new CSharpClosure("iterator", [LuaValue.FromObject(matches), 0], static (context, cancellationToken) =>
         {
         {
             var upValues = context.GetCsClosure()!.UpValues;
             var upValues = context.GetCsClosure()!.UpValues;
             var matches = upValues[0].Read<MatchCollection>();
             var matches = upValues[0].Read<MatchCollection>();