Pārlūkot izejas kodu

fix: getmetatable for all types

Akeit0 7 mēneši atpakaļ
vecāks
revīzija
0c3dd1c871
2 mainītis faili ar 17 papildinājumiem un 14 dzēšanām
  1. 9 2
      src/Lua/Internal/FastListCore.cs
  2. 8 12
      src/Lua/Standard/BasicLibrary.cs

+ 9 - 2
src/Lua/Internal/FastListCore.cs

@@ -1,3 +1,4 @@
+using System.Diagnostics;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
@@ -8,6 +9,7 @@ namespace Lua.Internal;
 /// </summary>
 /// <typeparam name="T">Element type</typeparam>
 [StructLayout(LayoutKind.Auto)]
+[DebuggerDisplay("Count = {Length}")]
 public struct FastListCore<T>
 {
     const int InitialCapacity = 8;
@@ -40,6 +42,7 @@ public struct FastListCore<T>
         array![tailIndex - 1] = default!;
         tailIndex--;
     }
+
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     public void RemoveAtSwapBack(int index)
     {
@@ -104,12 +107,16 @@ public struct FastListCore<T>
     }
 
     public readonly Span<T> AsSpan() => array == null ? Span<T>.Empty : array.AsSpan(0, tailIndex);
+
+    [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
+    private readonly Span<T> Span => AsSpan();
+
     public readonly T[]? AsArray() => array;
 
     readonly void CheckIndex(int index)
     {
-        if (array == null||index < 0 || index > tailIndex) ThrowIndexOutOfRange();
+        if (array == null || index < 0 || index > tailIndex) ThrowIndexOutOfRange();
     }
-    
+
     static void ThrowIndexOutOfRange() => throw new IndexOutOfRangeException();
 }

+ 8 - 12
src/Lua/Standard/BasicLibrary.cs

@@ -108,19 +108,15 @@ public sealed class BasicLibrary
     {
         var arg0 = context.GetArgument(0);
 
-        if (arg0.TryRead<LuaTable>(out var table))
+        if (context.State.TryGetMetatable(arg0, out var metatable))
         {
-            if (table.Metatable == null)
-            {
-                context.Return(LuaValue.Nil);
-            }
-            else if (table.Metatable.TryGetValue(Metamethods.Metatable, out var metatable))
+            if (metatable.TryGetValue(Metamethods.Metatable, out var metaMetatable))
             {
-                context.Return(metatable);
+                context.Return(metaMetatable);
             }
             else
             {
-                context.Return(table.Metatable);
+                context.Return(metatable);
             }
         }
         else
@@ -153,7 +149,7 @@ public sealed class BasicLibrary
     {
         // Lua-CSharp does not support binary chunks, the mode argument is ignored.
         var arg0 = context.GetArgument<string>(0);
-        var mode  = context.HasArgument(1)
+        var mode = context.HasArgument(1)
             ? context.GetArgument<string>(1)
             : "bt";
         var arg2 = context.HasArgument(2)
@@ -165,7 +161,7 @@ public sealed class BasicLibrary
         {
             var bytes = await File.ReadAllBytesAsync(arg0, cancellationToken);
             var fileName = "@" + Path.GetFileName(arg0);
-            return context.Return(context.State.Compile(bytes, fileName,mode,arg2));
+            return context.Return(context.State.Compile(bytes, fileName, mode, arg2));
         }
         catch (Exception ex)
         {
@@ -181,7 +177,7 @@ public sealed class BasicLibrary
         var name = context.HasArgument(1)
             ? context.GetArgument<string>(1)
             : null;
-        
+
         var mode = context.HasArgument(2)
             ? context.GetArgument<string>(2)
             : "bt";
@@ -259,7 +255,7 @@ public sealed class BasicLibrary
         }
         catch (Exception ex)
         {
-            if (ex is LuaRuntimeException  luaEx)
+            if (ex is LuaRuntimeException luaEx)
             {
                 return context.Return(false, luaEx.ErrorObject);
             }