Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Lua/Standard/Internal/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Lua.Standard.Internal;

public class ConsoleHelper
{
public static bool SupportStandardConsole => LuaPlatformUtility.IsSandBox;

private static Stream? _inputStream;
private static TextReader? _inputReader;

public static Stream OpenStandardInput()
{
if (SupportStandardConsole)
{
return Console.OpenStandardInput();
}
_inputStream ??= new MemoryStream();
_inputReader ??= new StreamReader(_inputStream);
return _inputStream;
}

public static int Read()
{
if (SupportStandardConsole)
{
return Console.Read();
}
return _inputReader?.Read() ?? 0;
}

public static Stream OpenStandardOutput()
{
return Console.OpenStandardOutput();
}

public static Stream OpenStandardError()
{
return Console.OpenStandardError();
}
}
31 changes: 31 additions & 0 deletions src/Lua/Standard/Internal/LuaPlatformUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Lua.Standard.Internal;

public class LuaPlatformUtility
{
public static bool IsSandBox => SupportStdio;
public static bool SupportStdio => _supportStdioTryLazy.Value;

private static Lazy<bool> _supportStdioTryLazy = new Lazy<bool>(() =>
{
try
{
#if NET6_0_OR_GREATER
var isDesktop = System.OperatingSystem.IsWindows() ||
System.OperatingSystem.IsLinux() ||
System.OperatingSystem.IsMacOS();
if (!isDesktop)
{
return false;
}
#endif
_ = Console.OpenStandardInput();
_ = Console.OpenStandardOutput();
return true;
}
catch (Exception)
{
return false;
}
});

}
11 changes: 6 additions & 5 deletions src/Lua/Standard/OpenLibsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lua.Runtime;
using Lua.Standard.Internal;

namespace Lua.Standard;

Expand Down Expand Up @@ -39,16 +40,16 @@ public static void OpenCoroutineLibrary(this LuaState state)

public static void OpenIOLibrary(this LuaState state)
{

var io = new LuaTable(0, IOLibrary.Instance.Functions.Length);
foreach (var func in IOLibrary.Instance.Functions)
{
io[func.Name] = func;
}

io["stdio"] = new LuaValue(new FileHandle(Console.OpenStandardInput()));
io["stdout"] = new LuaValue(new FileHandle(Console.OpenStandardOutput()));
io["stderr"] = new LuaValue(new FileHandle(Console.OpenStandardError()));

io["stdio"] = new LuaValue(new FileHandle(ConsoleHelper.OpenStandardInput()));
io["stdout"] = new LuaValue(new FileHandle(ConsoleHelper.OpenStandardOutput()));
io["stderr"] = new LuaValue(new FileHandle(ConsoleHelper.OpenStandardError()));

state.Environment["io"] = io;
state.LoadedModules["io"] = io;
}
Expand Down