Browse Source

refactor: rename LuaCompiler.UnDump to Prototype.FromByteCode and move bytecode conversion methods

Akeit0 5 months ago
parent
commit
e3fb1a09a8

+ 1 - 1
src/Lua.Unity/Assets/Lua.Unity/Editor/LuacAssetEditor.cs

@@ -20,7 +20,7 @@ namespace Lua.Unity.Editor
             if (asset == null) asset = (LuacAsset)serializedObject.targetObject;
             if (bytes == null || !asset.bytes.AsSpan().SequenceEqual(bytes))
             {
-                var prototype = LuaCompiler.UnDump(asset.bytes.AsSpan(), asset.name);
+                var prototype = Prototype.FromByteCode(asset.bytes.AsSpan(), asset.name);
                 if (sb == null)
                     sb = new StringBuilder();
                 sb.Clear();

+ 0 - 28
src/Lua/CodeAnalysis/Compilation/LuaCompiler.cs

@@ -1,28 +0,0 @@
-using Lua.Runtime;
-
-namespace Lua.CodeAnalysis.Compilation
-{
-    public static class LuaCompiler
-    {
-        /// <summary>
-        ///  Lua bytecode signature. If the bytes start with this signature, they are considered as Lua bytecode.
-        /// </summary>
-        public static ReadOnlySpan<byte> LuaByteCodeSignature => Header.LuaSignature;
-
-        /// <summary>
-        ///  Converts a Lua bytecode to a Prototype object.
-        /// </summary>
-        /// <param name="span">binary bytecode</param>
-        /// <param name="name">chunk name</param>
-        /// <returns></returns>
-        public static Prototype UnDump(ReadOnlySpan<byte> span, ReadOnlySpan<char> name) => Parser.UnDump(span, name);
-
-        /// <summary>
-        ///  Converts a Prototype object to a Lua bytecode.
-        ///  </summary>
-        ///  <param name="prototype">Prototype object</param>
-        ///  <param name="useLittleEndian">true if the bytecode should be in little endian format, false if it should be in big endian format</param>
-        /// <returns>binary bytecode</returns>
-        public static byte[] Dump(Prototype prototype, bool useLittleEndian = true) => Parser.Dump(prototype, useLittleEndian);
-    }
-}

+ 33 - 0
src/Lua/Runtime/Prototype.cs

@@ -1,4 +1,6 @@
 using Lua.CodeAnalysis;
+using Lua.CodeAnalysis.Compilation;
+using System.Buffers;
 
 namespace Lua.Runtime;
 
@@ -29,4 +31,35 @@ public sealed class Prototype(
     public readonly int LineDefined = lineDefined, LastLineDefined = lastLineDefined;
     public readonly int ParameterCount = parameterCount, MaxStackSize = maxStackSize;
     public readonly bool HasVariableArguments = hasVariableArguments;
+
+
+    /// <summary>
+    ///  Lua bytecode signature. If the bytes start with this signature, they are considered as Lua bytecode.
+    /// </summary>
+    public static ReadOnlySpan<byte> LuaByteCodeSignature => Header.LuaSignature;
+
+    /// <summary>
+    ///  Converts a Lua bytecode to a Prototype object.
+    /// </summary>
+    /// <param name="span">binary bytecode</param>
+    /// <param name="name">chunk name</param>
+    /// <returns></returns>
+    public static Prototype FromByteCode(ReadOnlySpan<byte> span, ReadOnlySpan<char> name) => Parser.UnDump(span, name);
+
+    /// <summary>
+    ///  Converts a Prototype object to a Lua bytecode.
+    ///  </summary>
+    ///  <param name="useLittleEndian">true if the bytecode should be in little endian format, false if it should be in big endian format</param>
+    /// <returns>binary bytecode</returns>
+    public byte[] ToByteCode(bool useLittleEndian = true) => Parser.Dump(this, useLittleEndian);
+
+    /// <summary>
+    ///  Writes the Lua bytecode to a buffer writer.
+    /// </summary>
+    /// <param name="bufferWriter">the buffer writer to write the bytecode to</param>
+    /// <param name="useLittleEndian">true if the bytecode should be in little endian format, false if it should be in big endian format</param>
+    public void WriteByteCode(IBufferWriter<byte> bufferWriter, bool useLittleEndian = true)
+    {
+        Parser.Dump(this, bufferWriter, useLittleEndian);
+    }
 }