Browse Source

improve os

Akeit0 6 months ago
parent
commit
7132f3db4a

+ 1 - 1
src/Lua/LuaState.cs

@@ -44,7 +44,7 @@ public sealed class LuaState
 
     public ILuaFileSystem FileSystem => Platform.FileSystem ?? throw new InvalidOperationException("FileSystem is not set. Please set it before using LuaState.");
 
-    public ILuaOperatingSystem OperatingSystem   => Platform.OperatingSystem ?? throw new InvalidOperationException("OperatingSystem is not set. Please set it before using LuaState.");
+    public ILuaOsEnvironment OsEnvironment   => Platform.OsEnvironment ?? throw new InvalidOperationException("OperatingSystem is not set. Please set it before using LuaState.");
 
     public ILuaStandardIO StandardIO => Platform.StandardIO;
 

+ 4 - 4
src/Lua/Platforms/ILuaOperatingSystem.cs → src/Lua/Platforms/ILuaOsEnvironment.cs

@@ -3,7 +3,7 @@
 /// <summary>
 /// Interface for operating system operations beyond file system
 /// </summary>
-public interface ILuaOperatingSystem
+public interface ILuaOsEnvironment
 {
     /// <summary>
     /// Get environment variable value
@@ -13,12 +13,12 @@ public interface ILuaOperatingSystem
     /// <summary>
     /// Exit the application with specified code
     /// </summary>
-    void Exit(int exitCode);
+    ValueTask Exit(int exitCode, CancellationToken cancellationToken);
 
     /// <summary>
-    /// Get current process start time for clock calculations
+    /// Get current process start time for clock calculations (units: seconds)
     /// </summary>
-    DateTime GetProcessStartTime();
+    double GetTotalProcessorTime();
 
     /// <summary>
     /// Get current UTC time

+ 2 - 2
src/Lua/Platforms/ILuaPlatform.cs

@@ -14,9 +14,9 @@ public interface ILuaPlatform
     ILuaFileSystem FileSystem { get; }
     
     /// <summary>
-    /// Gets the operating system abstraction for this platform
+    /// Gets the operating system environment abstraction for this platform
     /// </summary>
-    ILuaOperatingSystem OperatingSystem { get; }
+    ILuaOsEnvironment OsEnvironment { get; }
     
     /// <summary>
     /// Gets the standard I/O implementation for this platform

+ 3 - 3
src/Lua/Platforms/LuaPlatform.cs

@@ -7,9 +7,9 @@ namespace Lua.Platforms;
 ///  Platform abstraction for Lua.
 /// </summary>
 /// <param name="FileSystem"></param>
-/// <param name="OperatingSystem"></param>
+/// <param name="OsEnvironment"></param>
 /// <param name="StandardIO"></param>
-public sealed record LuaPlatform(ILuaFileSystem FileSystem , ILuaOperatingSystem OperatingSystem,ILuaStandardIO StandardIO): ILuaPlatform
+public sealed record LuaPlatform(ILuaFileSystem FileSystem , ILuaOsEnvironment OsEnvironment,ILuaStandardIO StandardIO): ILuaPlatform
 {
     /// <summary>
     /// Standard console platform implementation.
@@ -17,6 +17,6 @@ public sealed record LuaPlatform(ILuaFileSystem FileSystem , ILuaOperatingSystem
     /// </summary>
     public static  LuaPlatform Default => new( 
         FileSystem: new FileSystem(),
-        OperatingSystem: new OperatingSystem(),
+        OsEnvironment: new SystemOsEnvironment(),
         StandardIO:  new ConsoleStandardIO());
 }

+ 6 - 5
src/Lua/Platforms/OperatingSystem.cs → src/Lua/Platforms/SystemOsEnvironment.cs

@@ -3,23 +3,24 @@
 namespace Lua.Platforms
 {
     /// <summary>
-    /// Default implementation of ILuaOperatingSystem
+    /// Default implementation of ILuaEnvironment
     /// </summary>
-    public sealed class OperatingSystem : ILuaOperatingSystem
+    public sealed class SystemOsEnvironment : ILuaOsEnvironment
     {
         public string? GetEnvironmentVariable(string name)
         {
             return Environment.GetEnvironmentVariable(name);
         }
 
-        public void Exit(int exitCode)
+        public ValueTask Exit(int exitCode ,CancellationToken cancellationToken)
         {
             Environment.Exit(exitCode);
+            return default;
         }
 
-        public DateTime GetProcessStartTime()
+        public double GetTotalProcessorTime()
         {
-            return Process.GetCurrentProcess().StartTime;
+            return Process.GetCurrentProcess().TotalProcessorTime.TotalSeconds;
         }
 
         public DateTime GetCurrentUtcTime()

+ 12 - 16
src/Lua/Standard/OperatingSystemLibrary.cs

@@ -28,8 +28,7 @@ public sealed class OperatingSystemLibrary
 
     public ValueTask<int> Clock(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
     {
-        var os = context.State.OperatingSystem;
-        return new(context.Return(DateTimeHelper.GetUnixTime(os.GetCurrentUtcTime(), os.GetProcessStartTime())));
+        return new(context.Return(context.State.OsEnvironment.GetTotalProcessorTime()));
     }
 
     public ValueTask<int> Date(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
@@ -46,7 +45,7 @@ public sealed class OperatingSystemLibrary
         }
         else
         {
-            now = context.State.OperatingSystem.GetCurrentUtcTime();
+            now = context.State.OsEnvironment.GetCurrentUtcTime();
         }
 
         var isDst = false;
@@ -56,8 +55,8 @@ public sealed class OperatingSystemLibrary
         }
         else
         {
-            var offset = context.State.OperatingSystem.GetLocalTimeZoneOffset();
-            now =   now.Add(offset);
+            var offset = context.State.OsEnvironment.GetLocalTimeZoneOffset();
+            now = now.Add(offset);
             isDst = now.IsDaylightSavingTime();
         }
 
@@ -105,39 +104,36 @@ public sealed class OperatingSystemLibrary
         }
     }
 
-    public ValueTask<int> Exit(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
+    public async ValueTask<int> Exit(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
     {
         // Ignore 'close' parameter
-
+        int exitCode = 0;
         if (context.HasArgument(0))
         {
             var code = context.Arguments[0];
 
             if (code.TryRead<bool>(out var b))
             {
-                context.State.OperatingSystem.Exit(b ? 0 : 1);
+                exitCode = b ? 0 : 1;
             }
             else if (code.TryRead<int>(out var d))
             {
-                context.State.OperatingSystem.Exit(d);
+                exitCode = d;
             }
             else
             {
                 LuaRuntimeException.BadArgument(context.Thread, 1, LuaValueType.Nil, code.Type);
             }
         }
-        else
-        {
-            context.State.OperatingSystem.Exit(0);
-        }
 
-        return new(context.Return());
+        await context.State.OsEnvironment.Exit(exitCode, cancellationToken);
+        throw new InvalidOperationException("Unreachable code.. reached.");
     }
 
     public ValueTask<int> GetEnv(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
     {
         var variable = context.GetArgument<string>(0);
-        return new(context.Return(context.State.OperatingSystem.GetEnvironmentVariable(variable) ?? LuaValue.Nil));
+        return new(context.Return(context.State.OsEnvironment.GetEnvironmentVariable(variable) ?? LuaValue.Nil));
     }
 
     public ValueTask<int> Remove(LuaFunctionExecutionContext context, CancellationToken cancellationToken)
@@ -186,7 +182,7 @@ public sealed class OperatingSystemLibrary
         }
         else
         {
-            return new(context.Return(DateTimeHelper.GetUnixTime(context.State.OperatingSystem.GetCurrentUtcTime())));
+            return new(context.Return(DateTimeHelper.GetUnixTime(context.State.OsEnvironment.GetCurrentUtcTime())));
         }
     }