Skip to content

Commit a5c788c

Browse files
committed
Change: Integrate LuaRuntimeExceptions
1 parent ca39d3b commit a5c788c

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

src/Lua/Exceptions.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44

55
namespace Lua;
66

7-
public class LuaException(string message) : Exception(message);
7+
public class LuaException : Exception
8+
{
9+
protected LuaException(Exception innerException) : base(innerException.Message, innerException)
10+
{
11+
}
12+
13+
public LuaException(string message) : base(message)
14+
{
15+
}
16+
17+
internal LuaException()
18+
{
19+
}
20+
}
821

922
public class LuaParseException(string? chunkName, SourcePosition position, string message) : LuaException(message)
1023
{
@@ -44,9 +57,27 @@ public static void BreakNotInsideALoop(string? chunkName, SourcePosition positio
4457
public override string Message => $"{ChunkName}:{(Position == null ? "" : $"{Position.Value}:")} {base.Message}";
4558
}
4659

47-
public class LuaRuntimeException(Traceback traceback, string message) : LuaException(message)
60+
public class LuaRuntimeException : LuaException
4861
{
49-
public Traceback LuaTraceback { get; } = traceback;
62+
public LuaRuntimeException(Traceback traceback, Exception innerException) : base(innerException)
63+
{
64+
LuaTraceback = traceback;
65+
}
66+
67+
public LuaRuntimeException(Traceback traceback, string message) : base(message)
68+
{
69+
LuaTraceback = traceback;
70+
}
71+
72+
public LuaRuntimeException(Traceback traceback, LuaValue errorObject)
73+
{
74+
LuaTraceback = traceback;
75+
ErrorObject = errorObject;
76+
}
77+
78+
public Traceback LuaTraceback { get; }
79+
80+
public LuaValue? ErrorObject { get; }
5081

5182
public static void AttemptInvalidOperation(Traceback traceback, string op, LuaValue a, LuaValue b)
5283
{
@@ -97,14 +128,4 @@ public override string ToString()
97128
}
98129
}
99130

100-
public class LuaModuleNotFoundException(string moduleName) : LuaException($"module '{moduleName}' not found");
101-
102-
public class LuaRuntimeCSharpException(Traceback traceback, Exception exception) : LuaRuntimeException(traceback, exception.Message)
103-
{
104-
public Exception Exception { get; } = exception;
105-
}
106-
107-
public class LuaRuntimeLuaValueException(Traceback traceback, LuaValue value) : LuaRuntimeException(traceback, value.ToString())
108-
{
109-
public LuaValue Value { get; } = value;
110-
}
131+
public class LuaModuleNotFoundException(string moduleName) : LuaException($"module '{moduleName}' not found");

src/Lua/LuaCoroutine.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ public override async ValueTask<int> ResumeAsync(LuaFunctionExecutionContext con
191191

192192
Volatile.Write(ref status, (byte)LuaThreadStatus.Dead);
193193
buffer.Span[0] = false;
194-
buffer.Span[1] = ex is LuaRuntimeLuaValueException luaValueException
195-
? luaValueException.Value
196-
: ex.Message;
194+
buffer.Span[1] = ex is LuaRuntimeException { ErrorObject: not null } luaEx ? luaEx.ErrorObject.Value: ex.Message;
197195
return 2;
198196
}
199197
else

src/Lua/Runtime/LuaVirtualMachine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ static bool MoveNext(ref VirtualMachineExecutionContext context, out PostOperati
960960
LuaValueArrayPool.Return1024(context.ResultsBuffer, true);
961961
if (e is not LuaRuntimeException)
962962
{
963-
var newException = new LuaRuntimeCSharpException(GetTracebacks(ref context), e);
963+
var newException = new LuaRuntimeException(GetTracebacks(ref context), e);
964964
context = default;
965965
throw newException;
966966
}

src/Lua/Standard/BasicLibrary.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public ValueTask<int> Error(LuaFunctionExecutionContext context, Memory<LuaValue
106106
? "(error object is a nil value)"
107107
: context.Arguments[0];
108108

109-
throw new LuaRuntimeLuaValueException(context.State.GetTraceback(), value);
109+
throw new LuaRuntimeException(context.State.GetTraceback(), value);
110110
}
111111

112112
public ValueTask<int> GetMetatable(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
@@ -156,7 +156,7 @@ public ValueTask<int> IPairs(LuaFunctionExecutionContext context, Memory<LuaValu
156156
buffer.Span[2] = 0;
157157
return new(3);
158158
}
159-
159+
160160
public async ValueTask<int> LoadFile(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
161161
{
162162
// Lua-CSharp does not support binary chunks, the mode argument is ignored.
@@ -240,7 +240,7 @@ public ValueTask<int> Next(LuaFunctionExecutionContext context, Memory<LuaValue>
240240
return new(1);
241241
}
242242
}
243-
243+
244244
public ValueTask<int> Pairs(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
245245
{
246246
var arg0 = context.GetArgument<LuaTable>(0);
@@ -285,9 +285,9 @@ public async ValueTask<int> PCall(LuaFunctionExecutionContext context, Memory<Lu
285285
catch (Exception ex)
286286
{
287287
buffer.Span[0] = false;
288-
if(ex is LuaRuntimeLuaValueException luaEx)
288+
if (ex is LuaRuntimeException { ErrorObject: not null } luaEx)
289289
{
290-
buffer.Span[1] = luaEx.Value;
290+
buffer.Span[1] = luaEx.ErrorObject.Value;
291291
}
292292
else
293293
{
@@ -360,7 +360,7 @@ public ValueTask<int> RawSet(LuaFunctionExecutionContext context, Memory<LuaValu
360360
arg0[arg1] = arg2;
361361
return new(0);
362362
}
363-
363+
364364
public ValueTask<int> Select(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
365365
{
366366
var arg0 = context.GetArgument(0);
@@ -398,7 +398,7 @@ public ValueTask<int> Select(LuaFunctionExecutionContext context, Memory<LuaValu
398398
return default;
399399
}
400400
}
401-
401+
402402
public ValueTask<int> SetMetatable(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
403403
{
404404
var arg0 = context.GetArgument<LuaTable>(0);
@@ -567,7 +567,7 @@ public ValueTask<int> ToString(LuaFunctionExecutionContext context, Memory<LuaVa
567567
var arg0 = context.GetArgument(0);
568568
return arg0.CallToStringAsync(context, buffer, cancellationToken);
569569
}
570-
570+
571571
public ValueTask<int> Type(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
572572
{
573573
var arg0 = context.GetArgument(0);
@@ -613,8 +613,8 @@ public async ValueTask<int> XPCall(LuaFunctionExecutionContext context, Memory<L
613613
catch (Exception ex)
614614
{
615615
methodBuffer.AsSpan().Clear();
616-
var error = (ex is LuaRuntimeLuaValueException luaEx) ? luaEx.Value : ex.Message;
617-
616+
var error = ex is LuaRuntimeException { ErrorObject: not null } luaEx ? luaEx.ErrorObject.Value : ex.Message;
617+
618618
context.State.Push(error);
619619

620620
// invoke error handler
@@ -627,7 +627,7 @@ await arg1.InvokeAsync(context with
627627

628628
buffer.Span[0] = false;
629629
buffer.Span[1] = methodBuffer[0];
630-
630+
631631

632632
return 2;
633633
}

0 commit comments

Comments
 (0)