22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . Diagnostics . CodeAnalysis ;
5- using System . IO ;
65using System . Runtime . CompilerServices ;
7- using System . Runtime . InteropServices ;
8- using System . Text ;
96using Laylua . Marshaling ;
107using Laylua . Moon ;
118using Qommon ;
12- using Qommon . Pooling ;
139
1410namespace 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