Browse Source

Add: add Low level APIs for compilation

Akeit0 7 months ago
parent
commit
3eacaa79d4

+ 10 - 9
src/Lua/Internal/BomUtility.cs → src/Lua/CodeAnalysis/Compilation/BomUtility.cs

@@ -1,15 +1,22 @@
 using System.Text;
 using System.Text;
 
 
-namespace Lua.Internal;
+namespace Lua.CodeAnalysis.Compilation;
 
 
-internal static class BomUtility
+public static class BomUtility
 {
 {
     static ReadOnlySpan<byte> BomUtf8 => [0xEF, 0xBB, 0xBF];
     static ReadOnlySpan<byte> BomUtf8 => [0xEF, 0xBB, 0xBF];
     static ReadOnlySpan<byte> BomUtf16Little => [0xFF, 0xFE];
     static ReadOnlySpan<byte> BomUtf16Little => [0xFF, 0xFE];
     static ReadOnlySpan<byte> BomUtf16Big => [0xFE, 0xFF];
     static ReadOnlySpan<byte> BomUtf16Big => [0xFE, 0xFF];
     static ReadOnlySpan<byte> BomUtf32Little => [0xFF, 0xFE, 0x00, 0x00];
     static ReadOnlySpan<byte> BomUtf32Little => [0xFF, 0xFE, 0x00, 0x00];
-    static ReadOnlySpan<byte> BomUtf32Big => [0x00, 0x00, 0xFE, 0xFF];
 
 
+    /// <summary>
+    ///  Removes the BOM from the beginning of the text and returns the encoding.
+    ///  Supported encodings are UTF-8, UTF-16 (little and big endian), and UTF-32 (little endian).
+    ///  Unknown BOMs are ignored, and the encoding is set to UTF-8 by default.
+    ///  </summary>
+    ///  <param name="text">The text to check for BOM.</param>
+    ///  <param name="encoding">The encoding of the text.</param>
+    ///  <returns>The text without the BOM.</returns>
     public static ReadOnlySpan<byte> GetEncodingFromBytes(ReadOnlySpan<byte> text, out Encoding encoding)
     public static ReadOnlySpan<byte> GetEncodingFromBytes(ReadOnlySpan<byte> text, out Encoding encoding)
     {
     {
         if (text.StartsWith(BomUtf8))
         if (text.StartsWith(BomUtf8))
@@ -36,12 +43,6 @@ internal static class BomUtility
             return text.Slice(BomUtf32Little.Length);
             return text.Slice(BomUtf32Little.Length);
         }
         }
 
 
-        if (text.StartsWith(BomUtf32Big))
-        {
-            encoding = Encoding.UTF32;
-            return text.Slice(BomUtf32Big.Length);
-        }
-
         encoding = Encoding.UTF8;
         encoding = Encoding.UTF8;
         return text;
         return text;
     }
     }

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

@@ -0,0 +1,28 @@
+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);
+    }
+}