From e3fb1a09a817c27f1dc3ba5c620fa48841d46512 Mon Sep 17 00:00:00 2001 From: Akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Sun, 29 Jun 2025 12:26:07 +0900 Subject: [PATCH] refactor: rename LuaCompiler.UnDump to Prototype.FromByteCode and move bytecode conversion methods --- .../Lua.Unity/Editor/LuacAssetEditor.cs | 2 +- .../CodeAnalysis/Compilation/LuaCompiler.cs | 28 ---------------- src/Lua/Runtime/Prototype.cs | 33 +++++++++++++++++++ 3 files changed, 34 insertions(+), 29 deletions(-) delete mode 100644 src/Lua/CodeAnalysis/Compilation/LuaCompiler.cs diff --git a/src/Lua.Unity/Assets/Lua.Unity/Editor/LuacAssetEditor.cs b/src/Lua.Unity/Assets/Lua.Unity/Editor/LuacAssetEditor.cs index dc60ed4c..74993177 100644 --- a/src/Lua.Unity/Assets/Lua.Unity/Editor/LuacAssetEditor.cs +++ b/src/Lua.Unity/Assets/Lua.Unity/Editor/LuacAssetEditor.cs @@ -20,7 +20,7 @@ public override void OnInspectorGUI() 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(); diff --git a/src/Lua/CodeAnalysis/Compilation/LuaCompiler.cs b/src/Lua/CodeAnalysis/Compilation/LuaCompiler.cs deleted file mode 100644 index b56c6122..00000000 --- a/src/Lua/CodeAnalysis/Compilation/LuaCompiler.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Lua.Runtime; - -namespace Lua.CodeAnalysis.Compilation -{ - public static class LuaCompiler - { - /// - /// Lua bytecode signature. If the bytes start with this signature, they are considered as Lua bytecode. - /// - public static ReadOnlySpan LuaByteCodeSignature => Header.LuaSignature; - - /// - /// Converts a Lua bytecode to a Prototype object. - /// - /// binary bytecode - /// chunk name - /// - public static Prototype UnDump(ReadOnlySpan span, ReadOnlySpan name) => Parser.UnDump(span, name); - - /// - /// Converts a Prototype object to a Lua bytecode. - /// - /// Prototype object - /// true if the bytecode should be in little endian format, false if it should be in big endian format - /// binary bytecode - public static byte[] Dump(Prototype prototype, bool useLittleEndian = true) => Parser.Dump(prototype, useLittleEndian); - } -} \ No newline at end of file diff --git a/src/Lua/Runtime/Prototype.cs b/src/Lua/Runtime/Prototype.cs index 46c57c2b..8377c79c 100644 --- a/src/Lua/Runtime/Prototype.cs +++ b/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 @@ UpValueDesc[] upValues public readonly int LineDefined = lineDefined, LastLineDefined = lastLineDefined; public readonly int ParameterCount = parameterCount, MaxStackSize = maxStackSize; public readonly bool HasVariableArguments = hasVariableArguments; + + + /// + /// Lua bytecode signature. If the bytes start with this signature, they are considered as Lua bytecode. + /// + public static ReadOnlySpan LuaByteCodeSignature => Header.LuaSignature; + + /// + /// Converts a Lua bytecode to a Prototype object. + /// + /// binary bytecode + /// chunk name + /// + public static Prototype FromByteCode(ReadOnlySpan span, ReadOnlySpan name) => Parser.UnDump(span, name); + + /// + /// Converts a Prototype object to a Lua bytecode. + /// + /// true if the bytecode should be in little endian format, false if it should be in big endian format + /// binary bytecode + public byte[] ToByteCode(bool useLittleEndian = true) => Parser.Dump(this, useLittleEndian); + + /// + /// Writes the Lua bytecode to a buffer writer. + /// + /// the buffer writer to write the bytecode to + /// true if the bytecode should be in little endian format, false if it should be in big endian format + public void WriteByteCode(IBufferWriter bufferWriter, bool useLittleEndian = true) + { + Parser.Dump(this, bufferWriter, useLittleEndian); + } } \ No newline at end of file