Skip to content

Commit 6e21140

Browse files
committed
Switch WarningEmitted to a combined delegate
1 parent 4bb982c commit 6e21140

File tree

2 files changed

+3
-14
lines changed

2 files changed

+3
-14
lines changed

src/Laylua/Library/Lua.Warnings.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43
using System.Runtime.CompilerServices;
54
using System.Runtime.InteropServices;
@@ -30,14 +29,9 @@ public sealed unsafe partial class Lua
3029
/// <remarks>
3130
/// Subscribed event handlers must not throw any exceptions.
3231
/// </remarks>
33-
public event EventHandler<LuaWarningEmittedEventArgs> WarningEmitted
34-
{
35-
add => (_warningHandlers ??= new()).Add(value);
36-
remove => _warningHandlers?.Remove(value);
37-
}
32+
public event EventHandler<LuaWarningEmittedEventArgs>? WarningEmitted;
3833

3934
private MemoryStream? _warningBuffer;
40-
private List<EventHandler<LuaWarningEmittedEventArgs>>? _warningHandlers;
4135

4236
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
4337
private static void WarningHandler(void* ud, byte* msg, int tocont)
@@ -50,7 +44,7 @@ private static void WarningHandler(void* ud, byte* msg, int tocont)
5044
return;
5145
}
5246

53-
if (!lua.EmitsWarnings || lua._warningHandlers == null || lua._warningHandlers.Count == 0)
47+
if (!lua.EmitsWarnings || lua.WarningEmitted == null)
5448
{
5549
return;
5650
}
@@ -79,10 +73,7 @@ private static void WarningHandler(void* ud, byte* msg, int tocont)
7973

8074
using (message)
8175
{
82-
foreach (var handler in lua._warningHandlers)
83-
{
84-
handler.Invoke(lua, new LuaWarningEmittedEventArgs(message));
85-
}
76+
lua.WarningEmitted?.Invoke(lua, new LuaWarningEmittedEventArgs(message));
8677
}
8778

8879
static void ProcessControlWarningMessage(Lua lua, ReadOnlySpan<byte> controlMsg)

src/Laylua/Library/Lua.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ private Lua(
9393
Marshaler = marshaler;
9494

9595
_openLibraries = new List<LuaLibrary>();
96-
_warningHandlers = new List<EventHandler<LuaWarningEmittedEventArgs>>();
9796
MainThread = LuaThread.CreateMainThread(this);
9897
Globals = LuaTable.CreateGlobalsTable(this);
9998

@@ -110,7 +109,6 @@ private Lua(
110109
Marshaler = parent.Marshaler;
111110

112111
_openLibraries = parent._openLibraries;
113-
_warningHandlers = parent._warningHandlers;
114112
MainThread = parent.MainThread;
115113
Globals = parent.Globals;
116114
}

0 commit comments

Comments
 (0)