Skip to content

Commit a60175d

Browse files
committed
Add conversion error propagation
1 parent e6fb3db commit a60175d

34 files changed

Lines changed: 1991 additions & 547 deletions

src/Laylua/Library/LuaStackValue.cs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Runtime.CompilerServices;
34
using Laylua.Marshaling;
45
using Laylua.Moon;
6+
using Qommon;
57

68
namespace 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>

src/Laylua/Library/Marshaler/DefaultLuaMarshaler.MarshalingCompatibility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,6 @@ private static bool HasTableMaterializationCapability(Type targetType)
344344
}
345345

346346
var handler = LuaTypeRegistry.GetHandler(targetType);
347-
return handler != null;
347+
return handler != null && handler is not DynamicUserDataHandler;
348348
}
349349
}

0 commit comments

Comments
 (0)