Skip to content

Commit 87b6f61

Browse files
committed
Split warnings into a partial
1 parent a207ea1 commit 87b6f61

File tree

2 files changed

+135
-126
lines changed

2 files changed

+135
-126
lines changed

src/Laylua/Library/Lua.Warnings.cs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Runtime.CompilerServices;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using Laylua.Moon;
8+
using Qommon.Pooling;
9+
10+
namespace Laylua;
11+
12+
public sealed unsafe partial class Lua
13+
{
14+
/// <summary>
15+
/// Gets or sets whether <see cref="WarningEmitted"/> should fire for emitted warnings. <br/>
16+
/// See <a href="https://www.lua.org/manual/5.4/manual.html#2.3">Error Handling (Lua manual)</a> and
17+
/// <a href="https://www.lua.org/manual/5.4/manual.html#pdf-warn"><c>warn (msg1, ···) (Lua Manual)</c></a> for more information about warnings.
18+
/// </summary>
19+
/// <remarks>
20+
/// This can be controlled by the content of warning messages;
21+
/// can be disabled using "@off" and enabled using "@on".
22+
/// </remarks>
23+
public bool EmitsWarnings { get; set; } = true;
24+
25+
/// <summary>
26+
/// Fired when Lua emits a warning. <br/>
27+
/// See <a href="https://www.lua.org/manual/5.4/manual.html#2.3">Error Handling (Lua manual)</a> and
28+
/// <a href="https://www.lua.org/manual/5.4/manual.html#pdf-warn"><c>warn (msg1, ···) (Lua Manual)</c></a> for more information about warnings.
29+
/// </summary>
30+
/// <remarks>
31+
/// Subscribed event handlers must not throw any exceptions.
32+
/// </remarks>
33+
public event EventHandler<LuaWarningEmittedEventArgs> WarningEmitted
34+
{
35+
add => (_warningHandlers ??= new()).Add(value);
36+
remove => _warningHandlers?.Remove(value);
37+
}
38+
39+
private MemoryStream? _warningBuffer;
40+
private List<EventHandler<LuaWarningEmittedEventArgs>>? _warningHandlers;
41+
42+
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
43+
private static void WarningHandler(void* ud, byte* msg, int tocont)
44+
{
45+
var lua = FromExtraSpace((lua_State*) ud);
46+
var msgSpan = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(msg);
47+
if (msgSpan.StartsWith("@"u8))
48+
{
49+
ProcessControlWarningMessage(lua, msgSpan[1..]);
50+
return;
51+
}
52+
53+
if (!lua.EmitsWarnings || lua._warningHandlers == null || lua._warningHandlers.Count == 0)
54+
{
55+
return;
56+
}
57+
58+
if (tocont != 0)
59+
{
60+
(lua._warningBuffer ??= new(capacity: 128)).Write(msgSpan);
61+
return;
62+
}
63+
64+
RentedArray<char> message;
65+
var warningBuffer = lua._warningBuffer;
66+
if (warningBuffer == null || warningBuffer.Length == 0)
67+
{
68+
message = CreateWarningMessage(msgSpan);
69+
}
70+
else
71+
{
72+
warningBuffer.Write(msgSpan);
73+
_ = warningBuffer.TryGetBuffer(out var buffer);
74+
75+
message = CreateWarningMessage(buffer);
76+
77+
ClearWarningBuffer(warningBuffer);
78+
}
79+
80+
using (message)
81+
{
82+
foreach (var handler in lua._warningHandlers)
83+
{
84+
handler.Invoke(lua, new LuaWarningEmittedEventArgs(message));
85+
}
86+
}
87+
88+
static void ProcessControlWarningMessage(Lua lua, ReadOnlySpan<byte> controlMsg)
89+
{
90+
if (controlMsg.SequenceEqual("on"u8))
91+
{
92+
lua.EmitsWarnings = true;
93+
}
94+
else if (controlMsg.SequenceEqual("off"u8))
95+
{
96+
lua.EmitsWarnings = false;
97+
98+
if (lua._warningBuffer != null)
99+
{
100+
ClearWarningBuffer(lua._warningBuffer);
101+
}
102+
}
103+
}
104+
105+
static RentedArray<char> CreateWarningMessage(ReadOnlySpan<byte> msg)
106+
{
107+
var message = RentedArray<char>.Rent(Encoding.UTF8.GetCharCount(msg));
108+
_ = Encoding.UTF8.GetChars(msg, message);
109+
return message;
110+
}
111+
112+
static void ClearWarningBuffer(MemoryStream warningBuffer)
113+
{
114+
warningBuffer.Position = 0;
115+
warningBuffer.SetLength(0);
116+
}
117+
}
118+
119+
/// <inheritdoc cref="EmitWarning(ReadOnlySpan{char})"/>
120+
public void EmitWarning(string? message)
121+
{
122+
EmitWarning(message.AsSpan());
123+
}
124+
125+
/// <summary>
126+
/// Emits a Lua warning that can fire <see cref="WarningEmitted"/>. <br/>
127+
/// See <a href="https://www.lua.org/manual/5.4/manual.html#2.3">Error Handling (Lua manual)</a> and
128+
/// <a href="https://www.lua.org/manual/5.4/manual.html#pdf-warn"><c>warn (msg1, ···) (Lua Manual)</c></a> for more information about warnings.
129+
/// </summary>
130+
/// <param name="message"> The warning message. </param>
131+
public void EmitWarning(ReadOnlySpan<char> message)
132+
{
133+
lua_warning(State.L, message, false);
134+
}
135+
}

src/Laylua/Library/Lua.cs

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Diagnostics.CodeAnalysis;
5-
using System.IO;
65
using System.Runtime.CompilerServices;
7-
using System.Runtime.InteropServices;
8-
using System.Text;
96
using Laylua.Marshaling;
107
using Laylua.Moon;
118
using Qommon;
12-
using Qommon.Pooling;
139

1410
namespace Laylua;
1511

@@ -65,31 +61,6 @@ public object? this[string name]
6561
set => SetGlobal(name, value);
6662
}
6763

68-
/// <summary>
69-
/// Gets or sets whether <see cref="WarningEmitted"/> should fire for emitted warnings. <br/>
70-
/// See <a href="https://www.lua.org/manual/5.4/manual.html#2.3">Error Handling (Lua manual)</a> and
71-
/// <a href="https://www.lua.org/manual/5.4/manual.html#pdf-warn"><c>warn (msg1, ···) (Lua Manual)</c></a> for more information about warnings.
72-
/// </summary>
73-
/// <remarks>
74-
/// This can be controlled by the content of warning messages;
75-
/// can be disabled using "@off" and enabled using "@on".
76-
/// </remarks>
77-
public bool EmitsWarnings { get; set; } = true;
78-
79-
/// <summary>
80-
/// Fired when Lua emits a warning. <br/>
81-
/// See <a href="https://www.lua.org/manual/5.4/manual.html#2.3">Error Handling (Lua manual)</a> and
82-
/// <a href="https://www.lua.org/manual/5.4/manual.html#pdf-warn"><c>warn (msg1, ···) (Lua Manual)</c></a> for more information about warnings.
83-
/// </summary>
84-
/// <remarks>
85-
/// Subscribed event handlers must not throw any exceptions.
86-
/// </remarks>
87-
public event EventHandler<LuaWarningEmittedEventArgs> WarningEmitted
88-
{
89-
add => _warningHandlers.Add(value);
90-
remove => _warningHandlers.Remove(value);
91-
}
92-
9364
/// <summary>
9465
/// Gets whether this instance is disposed.
9566
/// </summary>
@@ -99,9 +70,6 @@ public event EventHandler<LuaWarningEmittedEventArgs> WarningEmitted
9970

10071
private readonly List<LuaLibrary> _openLibraries;
10172

102-
private MemoryStream? _warningBuffer;
103-
private readonly List<EventHandler<LuaWarningEmittedEventArgs>> _warningHandlers;
104-
10573
public Lua()
10674
: this(LuaMarshaler.Default)
10775
{ }
@@ -150,100 +118,6 @@ private Lua(
150118
Globals = parent.Globals;
151119
}
152120

153-
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
154-
private static void WarningHandler(void* ud, byte* msg, int tocont)
155-
{
156-
var lua = FromExtraSpace((lua_State*) ud);
157-
var msgSpan = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(msg);
158-
if (msgSpan.StartsWith("@"u8))
159-
{
160-
ProcessControlWarningMessage(lua, msgSpan[1..]);
161-
return;
162-
}
163-
164-
if (!lua.EmitsWarnings || lua._warningHandlers.Count == 0)
165-
{
166-
return;
167-
}
168-
169-
if (tocont != 0)
170-
{
171-
(lua._warningBuffer ??= new(capacity: 128)).Write(msgSpan);
172-
return;
173-
}
174-
175-
RentedArray<char> message;
176-
var warningBuffer = lua._warningBuffer;
177-
if (warningBuffer == null || warningBuffer.Length == 0)
178-
{
179-
message = CreateWarningMessage(msgSpan);
180-
}
181-
else
182-
{
183-
warningBuffer.Write(msgSpan);
184-
_ = warningBuffer.TryGetBuffer(out var buffer);
185-
186-
message = CreateWarningMessage(buffer);
187-
188-
ClearWarningBuffer(warningBuffer);
189-
}
190-
191-
using (message)
192-
{
193-
foreach (var handler in lua._warningHandlers)
194-
{
195-
handler.Invoke(lua, new LuaWarningEmittedEventArgs(message));
196-
}
197-
}
198-
199-
static void ProcessControlWarningMessage(Lua lua, ReadOnlySpan<byte> controlMsg)
200-
{
201-
if (controlMsg.SequenceEqual("on"u8))
202-
{
203-
lua.EmitsWarnings = true;
204-
}
205-
else if (controlMsg.SequenceEqual("off"u8))
206-
{
207-
lua.EmitsWarnings = false;
208-
209-
if (lua._warningBuffer != null)
210-
{
211-
ClearWarningBuffer(lua._warningBuffer);
212-
}
213-
}
214-
}
215-
216-
static RentedArray<char> CreateWarningMessage(ReadOnlySpan<byte> msg)
217-
{
218-
var message = RentedArray<char>.Rent(Encoding.UTF8.GetCharCount(msg));
219-
_ = Encoding.UTF8.GetChars(msg, message);
220-
return message;
221-
}
222-
223-
static void ClearWarningBuffer(MemoryStream warningBuffer)
224-
{
225-
warningBuffer.Position = 0;
226-
warningBuffer.SetLength(0);
227-
}
228-
}
229-
230-
/// <inheritdoc cref="EmitWarning(ReadOnlySpan{char})"/>
231-
public void EmitWarning(string? message)
232-
{
233-
EmitWarning(message.AsSpan());
234-
}
235-
236-
/// <summary>
237-
/// Emits a Lua warning that can fire <see cref="WarningEmitted"/>. <br/>
238-
/// See <a href="https://www.lua.org/manual/5.4/manual.html#2.3">Error Handling (Lua manual)</a> and
239-
/// <a href="https://www.lua.org/manual/5.4/manual.html#pdf-warn"><c>warn (msg1, ···) (Lua Manual)</c></a> for more information about warnings.
240-
/// </summary>
241-
/// <param name="message"> The warning message. </param>
242-
public void EmitWarning(ReadOnlySpan<char> message)
243-
{
244-
lua_warning(State.L, message, false);
245-
}
246-
247121
public bool OpenLibrary(LuaLibrary library)
248122
{
249123
foreach (var openLibrary in _openLibraries)

0 commit comments

Comments
 (0)