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); var fileStream = File.Open(path, fileMode, fileAccess, FileShare.ReadWrite);
IHandle handle;
if (fileAccess == FileAccess.Read) if (fileAccess == FileAccess.Read)
{ {
if (binaryMode) if (binaryMode)
handle = new BinaryReadHandle(fileStream); BinaryReadHandle.Push(L, new(fileStream));
else else
handle = new ReadHandle(fileStream); ReadHandle.Push(L, new(fileStream));
} }
else if (fileAccess == FileAccess.Write) else if (fileAccess == FileAccess.Write)
{ {
if (binaryMode) if (binaryMode)
handle = new BinaryWriteHandle(fileStream); BinaryWriteHandle.Push(L, new(fileStream));
else else
handle = new WriteHandle(fileStream); WriteHandle.Push(L, new(fileStream));
} }
else else
{ {
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
handle.Push(L);
return 1; return 1;
} }

View file

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

View file

@ -203,18 +203,18 @@ public class HTTP : IPlugin
var stream = await response.Content.ReadAsStreamAsync(); 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 => _game.LuaRuntime.QueueEvent("http_response", LK =>
{ {
// arg 1, request id // arg 1, request id
LK.PushInteger(requestId); LK.PushInteger(requestId);
// arg 2, response data // 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.NewTable();
LK.PushString("success"); LK.PushString("success");
@ -241,13 +241,10 @@ public class HTTP : IPlugin
LK.SetTable(-3); LK.SetTable(-3);
handler.Push(LK, false); return 3;
return 2;
}); });
//_game.LuaRuntime.PushEvent("http_response", requestId, response.IsSuccessStatusCode, content, (int)response.StatusCode, response.ReasonPhrase);
}); });
L.PushInteger(requestId); L.PushInteger(requestId);
@ -342,7 +339,7 @@ public class HTTP : IPlugin
{ {
LK.PushInteger(requestId); LK.PushInteger(requestId);
handle.Push(LK, true); handle.Push(LK);
return 2; return 2;
}); });

View file

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

View file

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

View file

@ -6,14 +6,9 @@ using System.Text;
namespace Capy64.Runtime.Objects.Handlers; namespace Capy64.Runtime.Objects.Handlers;
public class BinaryWriteHandle : IHandle public class BinaryWriteHandle
{ {
private readonly BinaryWriter Stream; public const string ObjectType = "BinaryWriteHandle";
private bool IsClosed = false;
public BinaryWriteHandle(Stream stream)
{
Stream = new BinaryWriter(stream, Encoding.ASCII);
}
private static readonly Dictionary<string, LuaFunction> functions = new() private static readonly Dictionary<string, LuaFunction> functions = new()
{ {
@ -35,39 +30,31 @@ public class BinaryWriteHandle : IHandle
["close"] = L_Close, ["close"] = L_Close,
}; };
public void Push(Lua L, bool newTable = true) public static void Push(Lua L, BinaryWriter stream)
{ {
if (newTable) L.PushObject(stream);
L.NewTable();
// metatable if (L.NewMetaTable(ObjectType))
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)
{ {
L.PushString(pair.Key); L.PushString("__index");
L.PushCFunction(pair.Value); 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.SetTable(-3);
} }
L.PushString("_handle"); L.SetMetaTable(-2);
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);
} }
private static int L_WriteByte(IntPtr state) private static int L_WriteByte(IntPtr state)
@ -102,12 +89,12 @@ public class BinaryWriteHandle : IHandle
return 0; 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"); L.Error("handle is closed");
h.Stream.Write(buffer); stream.Write(buffer);
return 0; return 0;
} }
@ -118,12 +105,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2); 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"); L.Error("handle is closed");
h.Stream.Write((short)b); stream.Write((short)b);
return 0; return 0;
} }
@ -134,12 +121,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2); 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"); L.Error("handle is closed");
h.Stream.Write((int)b); stream.Write((int)b);
return 0; return 0;
} }
@ -150,12 +137,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2); 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"); L.Error("handle is closed");
h.Stream.Write(b); stream.Write(b);
return 0; return 0;
} }
@ -166,12 +153,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2); 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"); L.Error("handle is closed");
h.Stream.Write((sbyte)b); stream.Write((sbyte)b);
return 0; return 0;
} }
@ -182,12 +169,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2); 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"); L.Error("handle is closed");
h.Stream.Write((ushort)b); stream.Write((ushort)b);
return 0; return 0;
} }
@ -198,12 +185,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2); 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"); L.Error("handle is closed");
h.Stream.Write((uint)b); stream.Write((uint)b);
return 0; return 0;
} }
@ -214,12 +201,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckInteger(2); 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"); L.Error("handle is closed");
h.Stream.Write((ulong)b); stream.Write((ulong)b);
return 0; return 0;
} }
@ -230,12 +217,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckNumber(2); 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"); L.Error("handle is closed");
h.Stream.Write((Half)b); stream.Write((Half)b);
return 0; return 0;
} }
@ -246,12 +233,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckNumber(2); 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"); L.Error("handle is closed");
h.Stream.Write((float)b); stream.Write((float)b);
return 0; return 0;
} }
@ -262,12 +249,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckNumber(2); 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"); L.Error("handle is closed");
h.Stream.Write((double)b); stream.Write((double)b);
return 0; return 0;
} }
@ -308,12 +295,12 @@ public class BinaryWriteHandle : IHandle
return 0; 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"); L.Error("handle is closed");
h.Stream.Write(buffer); stream.Write(buffer);
return 0; return 0;
} }
@ -324,12 +311,12 @@ public class BinaryWriteHandle : IHandle
var b = L.CheckString(2); 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"); L.Error("handle is closed");
h.Stream.Write(b); stream.Write(b);
return 0; return 0;
} }
@ -341,12 +328,12 @@ public class BinaryWriteHandle : IHandle
L.CheckType(2, LuaType.Boolean); L.CheckType(2, LuaType.Boolean);
var b = L.ToBoolean(2); 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"); L.Error("handle is closed");
h.Stream.Write(b); stream.Write(b);
return 0; return 0;
} }
@ -355,12 +342,12 @@ public class BinaryWriteHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); 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"); L.Error("handle is closed");
h.Stream.Flush(); stream.Flush();
return 0; return 0;
} }
@ -369,14 +356,12 @@ public class BinaryWriteHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
var h = GetHandle(L, true); var stream = L.CheckObject<BinaryWriter>(1, ObjectType, true);
if (h is null || h.IsClosed) if (stream is null)
return 0; L.Error("handle is closed");
h.Stream.Close(); stream.Dispose();
h.IsClosed = true;
return 0; 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; namespace Capy64.Runtime.Objects.Handlers;
public class ReadHandle : IHandle public class ReadHandle
{ {
private readonly StreamReader Stream; public const string ObjectType = "ReadHandle";
private bool IsClosed = false;
public ReadHandle(Stream stream)
{
Stream = new StreamReader(stream);
}
private static readonly Dictionary<string, LuaFunction> functions = new() private static readonly Dictionary<string, LuaFunction> functions = new()
{ {
@ -22,57 +17,49 @@ public class ReadHandle : IHandle
["close"] = L_Close, ["close"] = L_Close,
}; };
public void Push(Lua L, bool newTable = true) public static void Push(Lua L, StreamReader stream)
{ {
if (newTable) L.PushObject(stream);
L.NewTable();
// metatable if (L.NewMetaTable(ObjectType))
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)
{ {
L.PushString(pair.Key); L.PushString("__index");
L.PushCFunction(pair.Value); 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.SetTable(-3);
} }
L.PushString("_handle"); L.SetMetaTable(-2);
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);
} }
private static int L_ReadAll(IntPtr state) private static int L_ReadAll(IntPtr state)
{ {
var L = Lua.FromIntPtr(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"); L.Error("handle is closed");
if (h.Stream.EndOfStream) if (handle.EndOfStream)
{ {
L.PushNil(); L.PushNil();
return 1; return 1;
} }
var content = h.Stream.ReadToEnd(); var content = handle.ReadToEnd();
L.PushString(content); L.PushString(content);
return 1; return 1;
@ -82,12 +69,12 @@ public class ReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(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"); L.Error("handle is closed");
var line = h.Stream.ReadLine(); var line = handle.ReadLine();
if (line is null) if (line is null)
L.PushNil(); L.PushNil();
@ -104,12 +91,12 @@ public class ReadHandle : IHandle
var count = (int)L.OptNumber(2, 1); var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer"); 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"); L.Error("handle is closed");
if (h.Stream.EndOfStream) if (handle.EndOfStream)
{ {
L.PushNil(); L.PushNil();
return 1; return 1;
@ -117,7 +104,7 @@ public class ReadHandle : IHandle
var chunk = new char[count]; var chunk = new char[count];
h.Stream.Read(chunk, 0, count); handle.Read(chunk, 0, count);
L.PushString(new string(chunk)); L.PushString(new string(chunk));
@ -128,14 +115,12 @@ public class ReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); 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; return 0;
h.Stream.Close(); handle.Dispose();
h.IsClosed = true;
return 0; return 0;
} }

View file

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

View file

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