public static DynValue error(ScriptExecutionContext executionContext, CallbackArguments args) { DynValue message = args.AsType(0, "error", DataType.String, false); 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; }
public static DynValue traceback(ScriptExecutionContext executionContext, CallbackArguments args) { var vMessage = args[0]; var vLevel = args[1]; double defaultSkip = 1.0; var cor = executionContext.GetCallingCoroutine(); if (vMessage.Type == DataType.Thread) { cor = vMessage.Coroutine; vMessage = args[1]; vLevel = args[2]; defaultSkip = 0.0; } if (vMessage.IsNotNil() && vMessage.Type != DataType.String && vMessage.Type != DataType.Number) { return(vMessage); } string message = vMessage.CastToString(); int skip = (int)((vLevel.CastToNumber()) ?? defaultSkip); return(DynValue.NewString(message ?? "")); }
public static DynValue status(ScriptExecutionContext executionContext, CallbackArguments args) { DynValue handle = args.AsType(0, "status", DataType.Thread); Coroutine running = executionContext.GetCallingCoroutine(); CoroutineState cs = handle.Coroutine.State; switch (cs) { case CoroutineState.Main: case CoroutineState.Running: return((handle.Coroutine == running) ? DynValue.NewString("running") : DynValue.NewString("normal")); case CoroutineState.NotStarted: case CoroutineState.Suspended: case CoroutineState.ForceSuspended: return(DynValue.NewString("suspended")); case CoroutineState.Dead: return(DynValue.NewString("dead")); default: throw new InternalErrorException("Unexpected coroutine state {0}", cs); } }
public static DynValue assert(ScriptExecutionContext executionContext, CallbackArguments args) { DynValue v = args[0]; DynValue message = args[1]; if (!v.CastToBool()) { var cor = executionContext.GetCallingCoroutine(); if (message.IsNil()) { throw new ScriptRuntimeException("assertion failed!") { CallStack = cor.GetStackTrace(0) } } ; // { DoNotDecorateMessage = true }; else { throw new ScriptRuntimeException(message.ToPrintString()) { CallStack = cor.GetStackTrace(0) } }; // { DoNotDecorateMessage = true }; } return(DynValue.NewTupleNested(args.GetArray())); }
public static DynValue running(ScriptExecutionContext executionContext, CallbackArguments args) { Coroutine C = executionContext.GetCallingCoroutine(); return(DynValue.NewTuple(DynValue.NewCoroutine(C), DynValue.NewBoolean(C.State == CoroutineState.Main))); }
public static DynValue getinfo(ScriptExecutionContext executionContext, CallbackArguments args) { Coroutine cor = executionContext.GetCallingCoroutine(); int vfArgIdx = 0; if (args[0].Type == DataType.Thread) { cor = args[0].Coroutine; vfArgIdx++; } DynValue vf = args[vfArgIdx + 0]; DynValue vwhat = args[vfArgIdx + 1]; if (vf.Type == DataType.Void || vf.Type == DataType.Nil) { vf = DynValue.NewNumber(1); } args.AsType(vfArgIdx + 1, "getinfo", DataType.String, true); string what = vwhat.CastToString() ?? "nfSlu"; DynValue vt = DynValue.NewTable(executionContext.GetScript()); if (vf.Type == DataType.Number) { WatchItem[] stacktrace = cor.GetStackTrace((int)vf.Number, executionContext.CallingLocation); if (stacktrace.Length == 0) { return(DynValue.NewNil()); } WatchItem wi = stacktrace[0]; if (what.Contains("n")) { vt.Table.Set("name", DynValue.NewString(wi.Name ?? "")); } if (what.Contains("f")) { vt.Table.Set("func", wi.Value ?? DynValue.NewNil()); } if (what.Contains("S")) { string source = (wi.Value != null && wi.Value.Type == DataType.Function) ? executionContext.GetScript().GetSourceCode(executionContext.CallingLocation.SourceIdx).Name : "[C]"; vt.Table.Set("source", DynValue.NewString("=" + source)); vt.Table.Set("short_src", DynValue.NewString(source.Length >= 60 ? source.Substring(0, 60) : source)); vt.Table.Set("what", DynValue.NewString(wi.Name == null ? "main" : ((wi.Value != null && wi.Value.Type == DataType.Function) ? "Lua" : "C"))); } if (what.Contains("l")) { vt.Table.Set("currentline", DynValue.NewNumber(wi.Location != null ? wi.Location.FromLine : executionContext.CallingLocation.FromLine)); } if (what.Contains("u")) { vt.Table.Set("nups", DynValue.NewNumber((wi.Value != null && wi.Value.Type == DataType.Function) ? wi.Value.Function.GetUpvaluesCount() : 0)); } } else { args.AsType(vfArgIdx + 0, "getinfo", DataType.Number, true); } return(vt); }
public static DynValue traceback(ScriptExecutionContext executionContext, CallbackArguments args) { StringBuilder sb = new StringBuilder(); DynValue vmessage = args[0]; DynValue vlevel = args[1]; double defaultSkip = 1.0; Coroutine cor = executionContext.GetCallingCoroutine(); if (vmessage.Type == DataType.Thread) { cor = vmessage.Coroutine; vmessage = args[1]; vlevel = args[2]; defaultSkip = 0.0; } if (vmessage.IsNotNil() && vmessage.Type != DataType.String && vmessage.Type != DataType.Number) { return(vmessage); } string message = vmessage.CastToString(); int skip = (int)((vlevel.CastToNumber()) ?? defaultSkip); WatchItem[] stacktrace = cor.GetStackTrace(Math.Max(0, skip)); if (message != null) { sb.AppendLine(message); } sb.AppendLine("stack traceback:"); foreach (WatchItem wi in stacktrace) { string name; if (wi.Name == null) { if (wi.RetAddress < 0) { name = "main chunk"; } else { name = "?"; } } else { name = "function '" + wi.Name + "'"; } string loc = wi.Location != null?wi.Location.FormatLocation(executionContext.GetScript()) : "[clr]"; sb.AppendFormat("\t{0}: in {1}\n", loc, name); } return(DynValue.NewString(sb)); }
public static DynValue traceback(ScriptExecutionContext executionContext, CallbackArguments args) { var sb = new StringBuilder(); var vmessage = args[0]; var vlevel = args[1]; var defaultSkip = 1.0; var cor = executionContext.GetCallingCoroutine(); if (vmessage.Type == DataType.Thread) { cor = vmessage.Coroutine; vmessage = args[1]; vlevel = args[2]; defaultSkip = 0.0; } if (vmessage.IsNotNil() && vmessage.Type != DataType.String && vmessage.Type != DataType.Number) { return vmessage; } var message = vmessage.CastToString(); var skip = (int) ((vlevel.CastToNumber()) ?? defaultSkip); var stacktrace = cor.GetStackTrace(Math.Max(0, skip)); if (message != null) sb.AppendLine(message); sb.AppendLine("stack traceback:"); foreach (var wi in stacktrace) { string name; if (wi.Name == null) if (wi.RetAddress < 0) name = "main chunk"; else name = "?"; else name = "function '" + wi.Name + "'"; var loc = wi.Location != null ? wi.Location.FormatLocation(executionContext.GetScript()) : "[clr]"; sb.AppendFormat("\t{0}: in {1}\n", loc, name); } return DynValue.NewString(sb); }
public static DynValue running(ScriptExecutionContext executionContext, CallbackArguments args) { Coroutine C = executionContext.GetCallingCoroutine(); return DynValue.NewTuple(DynValue.NewCoroutine(C), DynValue.NewBoolean(C.State == CoroutineState.Main)); }
public static DynValue status(ScriptExecutionContext executionContext, CallbackArguments args) { DynValue handle = args.AsType(0, "status", DataType.Thread); Coroutine running = executionContext.GetCallingCoroutine(); CoroutineState cs = handle.Coroutine.State; switch (cs) { case CoroutineState.Main: case CoroutineState.Running: return (handle.Coroutine == running) ? DynValue.NewString("running") : DynValue.NewString("normal"); case CoroutineState.NotStarted: case CoroutineState.Suspended: return DynValue.NewString("suspended"); case CoroutineState.Dead: return DynValue.NewString("dead"); default: throw new InternalErrorException("Unexpected coroutine state {0}", cs); } }
public static DynValue getinfo(ScriptExecutionContext executionContext, CallbackArguments args) { Coroutine cor = executionContext.GetCallingCoroutine(); int vfArgIdx = 0; if (args[0].Type == DataType.Thread) { cor = args[0].Coroutine; } DynValue vf = args[vfArgIdx + 0]; DynValue vwhat = args[vfArgIdx + 1]; //args.AsType(vfArgIdx + 1, "getinfo", DataType.String, true); string what = vwhat.CastToString() ?? "nfSlu"; DynValue vt = DynValue.NewTable(executionContext.GetScript()); Table t = vt.Table; if (vf.Type == DataType.Function || vf.Type == DataType.ClrFunction) { GetInfoAboutFunction(t, vf, what, executionContext); return(vt); } else if (vf.Type == DataType.Number) { var stackTrace = cor.GetStackTrace((int)vf.Number); if (stackTrace.Length <= 0) { return(DynValue.Nil); } var info = stackTrace[0]; if (what.Contains("S")) { var source = executionContext.OwnerScript.GetSourceCode(info.Location.SourceIdx); if (source != null) { t["source"] = source != null ? source.Name : "[C]"; t["short_src"] = source.Name.Substring(0, Math.Min(source.Name.Length, 60)); t["what"] = "Lua"; } else { t["source"] = "[C]"; t["short_src"] = "[C]"; t["what"] = "C"; } } if (what.Contains("L")) { } if (what.Contains("n")) { } if (what.Contains("u")) { } t["linedefined"] = info.Location.FromLine; t["lastlinedefined"] = info.Location.ToLine; return(vt); } return(DynValue.Nil); }