11using System ;
2+ using System . Diagnostics . CodeAnalysis ;
23using System . Runtime . CompilerServices ;
34using Laylua . Marshaling ;
45using Laylua . Moon ;
6+ using Qommon ;
57
68namespace Laylua ;
79
@@ -95,7 +97,9 @@ internal LuaStackValue(LuaThread thread, int index)
9597 private void ThrowIfInvalid ( )
9698 {
9799 if ( Type == LuaType . None )
100+ {
98101 throw new InvalidOperationException ( $ "The index of this { nameof ( LuaStackValue ) } is not valid.") ;
102+ }
99103 }
100104
101105 internal static void ValidateOwnership ( LuaThread thread , LuaStackValue value )
@@ -145,15 +149,25 @@ internal static void ValidateOwnership(LuaThread thread, LuaStackValue value)
145149 /// </returns>
146150 public bool TryGetValue < T > ( out T ? value )
147151 {
148- if ( Type == LuaType . None )
149- {
150- value = default ;
151- return false ;
152- }
153-
152+ ThrowIfInvalid ( ) ;
154153 return _thread . Marshaler . TryGetValue ( _thread , Index , out value ) ;
155154 }
156155
156+ /// <summary>
157+ /// Attempts to get the .NET value of this stack value.
158+ /// </summary>
159+ /// <typeparam name="T"> The expected type of the value. </typeparam>
160+ /// <param name="value"> The output value. </param>
161+ /// <param name="error"> The conversion error, if conversion failed. </param>
162+ /// <returns>
163+ /// <see langword="true"/> if successful.
164+ /// </returns>
165+ public bool TryGetValue < T > ( out T ? value , [ NotNullWhen ( false ) ] out LuaConversionError ? error )
166+ {
167+ ThrowIfInvalid ( ) ;
168+ return _thread . Marshaler . TryGetValue ( _thread , Index , out value , out error ) ;
169+ }
170+
157171 /// <summary>
158172 /// Attempts to get the .NET value of this stack value, applying the specified conversion rules.
159173 /// </summary>
@@ -165,15 +179,29 @@ public bool TryGetValue<T>(out T? value)
165179 /// </returns>
166180 public bool TryGetValue < T > ( LuaAllowedValueConversions allowedConversions , out T ? value )
167181 {
168- if ( Type == LuaType . None )
169- {
170- value = default ;
171- return false ;
172- }
173-
182+ ThrowIfInvalid ( ) ;
174183 return _thread . Marshaler . TryGetValue ( _thread , Index , allowedConversions , out value ) ;
175184 }
176185
186+ /// <summary>
187+ /// Attempts to get the .NET value of this stack value, applying the specified conversion rules.
188+ /// </summary>
189+ /// <typeparam name="T"> The expected type of the value. </typeparam>
190+ /// <param name="allowedConversions"> The conversion rules to apply. </param>
191+ /// <param name="value"> The output value. </param>
192+ /// <param name="error"> The conversion error, if conversion failed. </param>
193+ /// <returns>
194+ /// <see langword="true"/> if successful.
195+ /// </returns>
196+ public bool TryGetValue < T > (
197+ LuaAllowedValueConversions allowedConversions ,
198+ out T ? value ,
199+ [ NotNullWhen ( false ) ] out LuaConversionError ? error )
200+ {
201+ ThrowIfInvalid ( ) ;
202+ return _thread . Marshaler . TryGetValue ( _thread , Index , allowedConversions , out value , out error ) ;
203+ }
204+
177205 /// <summary>
178206 /// Sets the value of this stack value.
179207 /// </summary>
0 commit comments