diff --git a/src/MoonSharp.Interpreter/CoreLib/BasicModule.cs b/src/MoonSharp.Interpreter/CoreLib/BasicModule.cs index 9e93ae3d..980e138c 100755 --- a/src/MoonSharp.Interpreter/CoreLib/BasicModule.cs +++ b/src/MoonSharp.Interpreter/CoreLib/BasicModule.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Globalization; using System.Text; +using MoonSharp.Interpreter.Debugging; namespace MoonSharp.Interpreter.CoreLib { @@ -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; }