Skip to content

Commit

Permalink
Merge branch 'implement-lua-error' of https://github.com/ixjf/moonsharp
Browse files Browse the repository at this point in the history
… into pr/ixjf-set
  • Loading branch information
xanathar committed Sep 22, 2019
2 parents 31efedd + d25a3b1 commit 22e0f74
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/MoonSharp.Interpreter/CoreLib/BasicModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using MoonSharp.Interpreter.Debugging;

namespace MoonSharp.Interpreter.CoreLib
{
Expand Down Expand Up @@ -84,7 +85,33 @@ public static DynValue collectgarbage(ScriptExecutionContext executionContext, C
public static DynValue error(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue message = args.AsType(0, "error", DataType.String, false);
throw new ScriptRuntimeException(message.String); // { DoNotDecorateMessage = true };
DynValue level = args.AsType(1, "error", DataType.Number, true);

Coroutine cor = executionContext.GetCallingCoroutine();

WatchItem[] stacktrace = cor.GetStackTrace(0, executionContext.CallingLocation);

var e = new ScriptRuntimeException(message.String);

if (level.IsNil())
{
level = DynValue.NewNumber(1); // Default
}

if (level.Number > 0 && level.Number < stacktrace.Length)
{
// Lua allows levels up to max. value of a double, while this has to be cast to int
// Probably never will be a problem, just leaving this note here
WatchItem wi = stacktrace[(int)level.Number];

e.DecorateMessage(executionContext.GetScript(), wi.Location);
}
else
{
e.DoNotDecorateMessage = true;
}

throw e;
}


Expand Down

0 comments on commit 22e0f74

Please sign in to comment.