Quellcode durchsuchen

Finish implementation of Lua 'error'

Pedro Fanha vor 6 Jahren
Ursprung
Commit
d25a3b11a1
1 geänderte Dateien mit 28 neuen und 1 gelöschten Zeilen
  1. 28 1
      src/MoonSharp.Interpreter/CoreLib/BasicModule.cs

+ 28 - 1
src/MoonSharp.Interpreter/CoreLib/BasicModule.cs

@@ -5,6 +5,7 @@ using System;
 using System.Collections.Generic;
 using System.Globalization;
 using System.Text;
+using MoonSharp.Interpreter.Debugging;
 
 namespace MoonSharp.Interpreter.CoreLib
 {
@@ -84,7 +85,33 @@ namespace MoonSharp.Interpreter.CoreLib
 		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;
 		}