Optimized stream handlers

This commit is contained in:
Alessandro Proto 2023-01-26 21:52:05 +01:00
parent d1d7bc1463
commit 8636bf4ab1
10 changed files with 246 additions and 336 deletions

View file

@ -538,28 +538,25 @@ public class FileSystem : IPlugin
var fileStream = File.Open(path, fileMode, fileAccess, FileShare.ReadWrite);
IHandle handle;
if (fileAccess == FileAccess.Read)
{
if (binaryMode)
handle = new BinaryReadHandle(fileStream);
BinaryReadHandle.Push(L, new(fileStream));
else
handle = new ReadHandle(fileStream);
ReadHandle.Push(L, new(fileStream));
}
else if (fileAccess == FileAccess.Write)
{
if (binaryMode)
handle = new BinaryWriteHandle(fileStream);
BinaryWriteHandle.Push(L, new(fileStream));
else
handle = new WriteHandle(fileStream);
WriteHandle.Push(L, new(fileStream));
}
else
{
throw new InvalidOperationException();
}
handle.Push(L);
return 1;
}

View file

@ -385,9 +385,7 @@ public class GPU : IPlugin
var buffer = new uint[_game.Width * _game.Height];
_game.Drawing.Canvas.GetData(buffer);
var handle = new GPUBuffer(buffer);
handle.Push(L);
GPUBuffer.Push(L, buffer);
return 1;
}
@ -413,9 +411,7 @@ public class GPU : IPlugin
var buffer = new uint[_game.Width * _game.Height];
var handle = new GPUBuffer(buffer);
handle.Push(L);
GPUBuffer.Push(L, buffer);
return 1;
}

View file

@ -203,18 +203,18 @@ public class HTTP : IPlugin
var stream = await response.Content.ReadAsStreamAsync();
IHandle handler;
if ((bool)options["binary"])
handler = new BinaryReadHandle(stream);
else
handler = new ReadHandle(stream);
_game.LuaRuntime.QueueEvent("http_response", LK =>
{
// arg 1, request id
LK.PushInteger(requestId);
// arg 2, response data
if ((bool)options["binary"])
BinaryReadHandle.Push(LK, new(stream));
else
ReadHandle.Push(LK, new(stream));
// arg 3, response info
LK.NewTable();
LK.PushString("success");
@ -241,13 +241,10 @@ public class HTTP : IPlugin
LK.SetTable(-3);
handler.Push(LK, false);
return 2;
return 3;
});
//_game.LuaRuntime.PushEvent("http_response", requestId, response.IsSuccessStatusCode, content, (int)response.StatusCode, response.ReasonPhrase);
});
L.PushInteger(requestId);
@ -342,7 +339,7 @@ public class HTTP : IPlugin
{
LK.PushInteger(requestId);
handle.Push(LK, true);
handle.Push(LK);
return 2;
});

View file

@ -7,14 +7,10 @@ public class GPUBuffer
{
public const string ObjectType = "GPUBuffer";
private uint[] _buffer;
public GPUBuffer(uint[] buffer)
public static void Push(Lua L, uint[] buffer)
{
_buffer = buffer;
}
L.PushObject(buffer);
public void Push(Lua L)
{
if (L.NewMetaTable(ObjectType))
{
L.PushString("__index");
@ -38,8 +34,7 @@ public class GPUBuffer
L.SetTable(-3);
}
L.PushObject(_buffer);
L.SetMetaTable(ObjectType);
L.SetMetaTable(-2);
}
private static int LM_Index(IntPtr state)

View file

@ -6,15 +6,9 @@ using System.Text;
namespace Capy64.Runtime.Objects.Handlers;
public class BinaryReadHandle : IHandle
public class BinaryReadHandle
{
private readonly BinaryReader Stream;
private bool IsClosed = false;
private bool EndOfStream => Stream.BaseStream.Position == Stream.BaseStream.Length;
public BinaryReadHandle(Stream stream)
{
Stream = new BinaryReader(stream, Encoding.ASCII);
}
public const string ObjectType = "BinaryReadHandle";
private static readonly Dictionary<string, LuaFunction> functions = new()
{
@ -35,39 +29,31 @@ public class BinaryReadHandle : IHandle
["close"] = L_Close,
};
public void Push(Lua L, bool newTable = true)
public static void Push(Lua L, BinaryReader stream)
{
if (newTable)
L.NewTable();
L.PushObject(stream);
// metatable
L.NewTable();
L.PushString("__close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("__gc");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.SetMetaTable(-2);
foreach (var pair in functions)
if (L.NewMetaTable(ObjectType))
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.PushString("__index");
L.NewTable();
foreach (var pair in functions)
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.SetTable(-3);
}
L.SetTable(-3);
L.PushString("__close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("__gc");
L.PushCFunction(L_Close);
L.SetTable(-3);
}
L.PushString("_handle");
L.PushObject(this);
L.SetTable(-3);
}
private static BinaryReadHandle GetHandle(Lua L, bool gc = true)
{
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
return L.ToObject<BinaryReadHandle>(-1, gc);
L.SetMetaTable(-2);
}
private static int L_NextByte(IntPtr state)
@ -77,24 +63,24 @@ public class BinaryReadHandle : IHandle
var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
if (count == 1)
{
var b = h.Stream.ReadByte();
var b = stream.ReadByte();
L.PushInteger(b);
return 1;
}
else
{
var bs = h.Stream.ReadBytes(count);
var bs = stream.ReadBytes(count);
foreach (var b in bs)
{
L.PushInteger(b);
@ -107,15 +93,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadInt16();
var b = stream.ReadInt16();
L.PushInteger(b);
return 1;
}
@ -124,15 +110,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadInt32();
var b = stream.ReadInt32();
L.PushInteger(b);
return 1;
}
@ -141,15 +127,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadInt64();
var b = stream.ReadInt64();
L.PushInteger(b);
return 1;
}
@ -158,15 +144,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadSByte();
var b = stream.ReadSByte();
L.PushInteger(b);
return 1;
}
@ -175,15 +161,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadUInt16();
var b = stream.ReadUInt16();
L.PushInteger(b);
return 1;
}
@ -192,15 +178,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadUInt32();
var b = stream.ReadUInt32();
L.PushInteger(b);
return 1;
}
@ -209,15 +195,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadUInt64();
var b = stream.ReadUInt64();
L.PushInteger((long)b);
return 1;
}
@ -226,15 +212,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadHalf();
var b = stream.ReadHalf();
L.PushNumber((double)b);
return 1;
}
@ -243,15 +229,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadSingle();
var b = stream.ReadSingle();
L.PushNumber((double)b);
return 1;
}
@ -260,15 +246,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadDouble();
var b = stream.ReadDouble();
L.PushNumber((double)b);
return 1;
}
@ -277,27 +263,27 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
if (count == 1)
{
var b = h.Stream.ReadChar();
var b = stream.ReadChar();
L.PushString(b.ToString());
return 1;
}
else
{
var bs = h.Stream.ReadChars(count);
var bs = stream.ReadChars(count);
foreach (var b in bs)
{
L.PushString(b.ToString());
@ -310,27 +296,27 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
if (count == 1)
{
var b = h.Stream.ReadChar();
var b = stream.ReadChar();
L.PushString(b.ToString());
return 1;
}
else
{
var bs = h.Stream.ReadBytes(count);
var bs = stream.ReadBytes(count);
L.PushBuffer(bs);
return 1;
}
@ -340,15 +326,15 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
if (h.EndOfStream)
if (stream.BaseStream.Position == stream.BaseStream.Length)
return 0;
var b = h.Stream.ReadBoolean();
var b = stream.ReadBoolean();
L.PushBoolean(b);
return 1;
}
@ -357,14 +343,12 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, true);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, true);
if (h is null || h.IsClosed)
if (stream is null)
return 0;
h.Stream.Close();
h.IsClosed = true;
stream.Dispose();
return 0;
}

View file

@ -6,14 +6,9 @@ using System.Text;
namespace Capy64.Runtime.Objects.Handlers;
public class BinaryWriteHandle : IHandle
public class BinaryWriteHandle
{
private readonly BinaryWriter Stream;
private bool IsClosed = false;
public BinaryWriteHandle(Stream stream)
{
Stream = new BinaryWriter(stream, Encoding.ASCII);
}
public const string ObjectType = "BinaryWriteHandle";
private static readonly Dictionary<string, LuaFunction> functions = new()
{
@ -35,39 +30,31 @@ public class BinaryWriteHandle : IHandle
["close"] = L_Close,
};
public void Push(Lua L, bool newTable = true)
public static void Push(Lua L, BinaryWriter stream)
{
if (newTable)
L.NewTable();
L.PushObject(stream);
// metatable
L.NewTable();
L.PushString("__close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("__gc");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.SetMetaTable(-2);
foreach (var pair in functions)
if (L.NewMetaTable(ObjectType))
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.PushString("__index");
L.NewTable();
foreach (var pair in functions)
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.SetTable(-3);
}
L.SetTable(-3);
L.PushString("__close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("__gc");
L.PushCFunction(L_Close);
L.SetTable(-3);
}
L.PushString("_handle");
L.PushObject(this);
L.SetTable(-3);
}
private static BinaryWriteHandle GetHandle(Lua L, bool gc = true)
{
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
return L.ToObject<BinaryWriteHandle>(-1, gc);
L.SetMetaTable(-2);
}
private static int L_WriteByte(IntPtr state)
@ -102,12 +89,12 @@ public class BinaryWriteHandle : IHandle
return 0;
}
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write(buffer);
stream.Write(buffer);
return 0;
}
@ -118,12 +105,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write((short)b);
stream.Write((short)b);
return 0;
}
@ -134,12 +121,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write((int)b);
stream.Write((int)b);
return 0;
}
@ -150,12 +137,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write(b);
stream.Write(b);
return 0;
}
@ -166,12 +153,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write((sbyte)b);
stream.Write((sbyte)b);
return 0;
}
@ -182,12 +169,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write((ushort)b);
stream.Write((ushort)b);
return 0;
}
@ -198,12 +185,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write((uint)b);
stream.Write((uint)b);
return 0;
}
@ -214,12 +201,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write((ulong)b);
stream.Write((ulong)b);
return 0;
}
@ -230,12 +217,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckNumber(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write((Half)b);
stream.Write((Half)b);
return 0;
}
@ -246,12 +233,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckNumber(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write((float)b);
stream.Write((float)b);
return 0;
}
@ -262,12 +249,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckNumber(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write((double)b);
stream.Write((double)b);
return 0;
}
@ -308,12 +295,12 @@ public class BinaryWriteHandle : IHandle
return 0;
}
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write(buffer);
stream.Write(buffer);
return 0;
}
@ -324,12 +311,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckString(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write(b);
stream.Write(b);
return 0;
}
@ -341,12 +328,12 @@ public class BinaryWriteHandle : IHandle
L.CheckType(2, LuaType.Boolean);
var b = L.ToBoolean(2);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Write(b);
stream.Write(b);
return 0;
}
@ -355,12 +342,12 @@ public class BinaryWriteHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (stream is null)
L.Error("handle is closed");
h.Stream.Flush();
stream.Flush();
return 0;
}
@ -369,14 +356,12 @@ public class BinaryWriteHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, true);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, true);
if (h is null || h.IsClosed)
return 0;
if (stream is null)
L.Error("handle is closed");
h.Stream.Close();
h.IsClosed = true;
stream.Dispose();
return 0;
}

View file

@ -1,8 +0,0 @@
using KeraLua;
namespace Capy64.Runtime.Objects.Handlers;
public interface IHandle
{
public void Push(Lua L, bool newTable = true);
}

View file

@ -5,14 +5,9 @@ using System.IO;
namespace Capy64.Runtime.Objects.Handlers;
public class ReadHandle : IHandle
public class ReadHandle
{
private readonly StreamReader Stream;
private bool IsClosed = false;
public ReadHandle(Stream stream)
{
Stream = new StreamReader(stream);
}
public const string ObjectType = "ReadHandle";
private static readonly Dictionary<string, LuaFunction> functions = new()
{
@ -22,57 +17,49 @@ public class ReadHandle : IHandle
["close"] = L_Close,
};
public void Push(Lua L, bool newTable = true)
public static void Push(Lua L, StreamReader stream)
{
if (newTable)
L.NewTable();
L.PushObject(stream);
// metatable
L.NewTable();
L.PushString("__close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("__gc");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.SetMetaTable(-2);
foreach (var pair in functions)
if (L.NewMetaTable(ObjectType))
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.PushString("__index");
L.NewTable();
foreach (var pair in functions)
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.SetTable(-3);
}
L.SetTable(-3);
L.PushString("__close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("__gc");
L.PushCFunction(L_Close);
L.SetTable(-3);
}
L.PushString("_handle");
L.PushObject(this);
L.SetTable(-3);
}
private static ReadHandle GetHandle(Lua L, bool gc = true)
{
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
return L.ToObject<ReadHandle>(-1, gc);
L.SetMetaTable(-2);
}
private static int L_ReadAll(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var handle = L.CheckObject<StreamReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (handle is null)
L.Error("handle is closed");
if (h.Stream.EndOfStream)
if (handle.EndOfStream)
{
L.PushNil();
return 1;
}
var content = h.Stream.ReadToEnd();
var content = handle.ReadToEnd();
L.PushString(content);
return 1;
@ -82,12 +69,12 @@ public class ReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var handle = L.CheckObject<StreamReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (handle is null)
L.Error("handle is closed");
var line = h.Stream.ReadLine();
var line = handle.ReadLine();
if (line is null)
L.PushNil();
@ -104,12 +91,12 @@ public class ReadHandle : IHandle
var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
var h = GetHandle(L, false);
var handle = L.CheckObject<StreamReader>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (handle is null)
L.Error("handle is closed");
if (h.Stream.EndOfStream)
if (handle.EndOfStream)
{
L.PushNil();
return 1;
@ -117,7 +104,7 @@ public class ReadHandle : IHandle
var chunk = new char[count];
h.Stream.Read(chunk, 0, count);
handle.Read(chunk, 0, count);
L.PushString(new string(chunk));
@ -128,14 +115,12 @@ public class ReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, true);
var handle = L.CheckObject<StreamReader>(1, ObjectType, true);
if (h is null || h.IsClosed)
if (handle is null)
return 0;
h.Stream.Close();
h.IsClosed = true;
handle.Dispose();
return 0;
}

View file

@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace Capy64.Runtime.Objects.Handlers;
public class WebSocketHandle : IHandle
public class WebSocketHandle
{
private ClientWebSocket _client;
private long _requestId;
@ -28,10 +28,9 @@ public class WebSocketHandle : IHandle
["closeAsync"] = L_CloseAsync,
};
public void Push(Lua L, bool newTable = true)
public void Push(Lua L)
{
if (newTable)
L.NewTable();
L.NewTable();
// metatable
L.NewTable();

View file

@ -5,19 +5,9 @@ using System.IO;
namespace Capy64.Runtime.Objects.Handlers;
public class WriteHandle : IHandle
public class WriteHandle
{
private readonly StreamWriter Stream;
private bool IsClosed = false;
public WriteHandle(Stream stream)
{
Stream = new StreamWriter(stream);
}
public WriteHandle(StreamWriter stream)
{
Stream = stream;
}
public const string ObjectType = "WriteHandle";
private static readonly Dictionary<string, LuaFunction> functions = new()
{
@ -27,53 +17,45 @@ public class WriteHandle : IHandle
["close"] = L_Close,
};
public void Push(Lua L, bool newTable = true)
public static void Push(Lua L, StreamWriter stream)
{
if (newTable)
L.NewTable();
L.PushObject(stream);
// metatable
L.NewTable();
L.PushString("__close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("__gc");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.SetMetaTable(-2);
foreach (var pair in functions)
if (L.NewMetaTable(ObjectType))
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.PushString("__index");
L.NewTable();
foreach (var pair in functions)
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.SetTable(-3);
}
L.SetTable(-3);
L.PushString("__close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("__gc");
L.PushCFunction(L_Close);
L.SetTable(-3);
}
L.PushString("_handle");
L.PushObject(this);
L.SetTable(-3);
}
private static WriteHandle GetHandle(Lua L, bool gc = true)
{
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
return L.ToObject<WriteHandle>(-1, gc);
L.SetMetaTable(-2);
}
private static int L_Write(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var handle = L.CheckObject<StreamWriter>(1, ObjectType, false);
var content = L.CheckString(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
if (handle is null)
L.Error("handle is closed");
h.Stream.Write(content);
handle.Write(content);
return 0;
}
@ -82,14 +64,14 @@ public class WriteHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var handle = L.CheckObject<StreamWriter>(1, ObjectType, false);
var content = L.CheckString(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
if (handle is null)
L.Error("handle is closed");
h.Stream.WriteLine(content);
handle.WriteLine(content);
return 0;
}
@ -98,12 +80,12 @@ public class WriteHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
var handle = L.CheckObject<StreamWriter>(1, ObjectType, false);
if (h is null || h.IsClosed)
if (handle is null)
L.Error("handle is closed");
h.Stream.Flush();
handle.Flush();
return 0;
}
@ -112,14 +94,12 @@ public class WriteHandle : IHandle
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, true);
var handle = L.CheckObject<StreamWriter>(1, ObjectType, true);
if (h is null || h.IsClosed)
if (handle is null)
return 0;
h.Stream.Close();
h.IsClosed = true;
handle.Dispose();
return 0;
}