Skip to content

Commit 5f50aa1

Browse files
committed
Merge pull request moonsharp-devs#280 from Aberro/master
2 parents 441de74 + 9aa7e88 commit 5f50aa1

13 files changed

Lines changed: 247 additions & 47 deletions

File tree

src/MoonSharp.Interpreter/CoreLib/BasicModule.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public static DynValue assert(ScriptExecutionContext executionContext, CallbackA
4242

4343
if (!v.CastToBool())
4444
{
45+
var cor = executionContext.GetCallingCoroutine();
4546
if (message.IsNil())
46-
throw new ScriptRuntimeException("assertion failed!"); // { DoNotDecorateMessage = true };
47+
throw new ScriptRuntimeException("assertion failed!") { CallStack = cor.GetStackTrace(0) }; // { DoNotDecorateMessage = true };
4748
else
48-
throw new ScriptRuntimeException(message.ToPrintString()); // { DoNotDecorateMessage = true };
49+
throw new ScriptRuntimeException(message.ToPrintString()) { CallStack = cor.GetStackTrace(0) }; // { DoNotDecorateMessage = true };
4950
}
5051

5152
return DynValue.NewTupleNested(args.GetArray());
@@ -91,7 +92,7 @@ public static DynValue error(ScriptExecutionContext executionContext, CallbackAr
9192

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

94-
var e = new ScriptRuntimeException(message.String);
95+
var e = new ScriptRuntimeException(message.String) { CallStack = stacktrace };
9596

9697
if (level.IsNil())
9798
{
@@ -229,8 +230,13 @@ public static DynValue tonumber(ScriptExecutionContext executionContext, Callbac
229230
if (e.Type != DataType.String)
230231
return DynValue.Nil;
231232

232-
double d;
233-
if (double.TryParse(e.String, NumberStyles.Any, CultureInfo.InvariantCulture, out d))
233+
if (e.String.StartsWith("0x"))
234+
{
235+
if (long.TryParse(e.String.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out long l))
236+
return DynValue.NewNumber(l);
237+
return DynValue.Nil;
238+
}
239+
if (double.TryParse(e.String, NumberStyles.Any, CultureInfo.InvariantCulture, out double d))
234240
{
235241
return DynValue.NewNumber(d);
236242
}

src/MoonSharp.Interpreter/CoreLib/DebugModule.cs

Lines changed: 137 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -297,47 +297,153 @@ public static DynValue traceback(ScriptExecutionContext executionContext, Callba
297297
//}
298298

299299

300-
//[MoonSharpMethod]
301-
//public static DynValue getinfo(ScriptExecutionContext executionContext, CallbackArguments args)
302-
//{
303-
// Coroutine cor = executionContext.GetCallingCoroutine();
304-
// int vfArgIdx = 0;
305-
306-
// if (args[0].Type == DataType.Thread)
307-
// cor = args[0].Coroutine;
300+
[MoonSharpModuleMethod]
301+
public static DynValue getinfo(ScriptExecutionContext executionContext, CallbackArguments args)
302+
{
303+
Coroutine cor = executionContext.GetCallingCoroutine();
304+
int vfArgIdx = 0;
308305

309-
// DynValue vf = args[vfArgIdx+0];
310-
// DynValue vwhat = args[vfArgIdx+1];
306+
if (args[0].Type == DataType.Thread)
307+
cor = args[0].Coroutine;
311308

312-
// args.AsType(vfArgIdx + 1, "getinfo", DataType.String, true);
309+
DynValue vf = args[vfArgIdx+0];
310+
DynValue vwhat = args[vfArgIdx+1];
311+
312+
//args.AsType(vfArgIdx + 1, "getinfo", DataType.String, true);
313313

314-
// string what = vwhat.CastToString() ?? "nfSlu";
314+
string what = vwhat.CastToString() ?? "nfSlu";
315315

316-
// DynValue vt = DynValue.NewTable(executionContext.GetScript());
317-
// Table t = vt.Table;
316+
DynValue vt = DynValue.NewTable(executionContext.GetScript());
317+
Table t = vt.Table;
318+
if (vf.Type == DataType.Function || vf.Type == DataType.ClrFunction)
319+
{
320+
GetInfoAboutFunction(t, vf, what, executionContext);
321+
return vt;
322+
}
323+
else if (vf.Type == DataType.Number)
324+
{
325+
var stackTrace = cor.GetStackTrace((int)vf.Number);
326+
if (stackTrace.Length <= 0)
327+
return DynValue.Nil;
328+
var info = stackTrace[0];
329+
if (what.Contains("S"))
330+
{
331+
var source = executionContext.OwnerScript.GetSourceCode(info.Location.SourceIdx);
332+
if (source != null)
333+
{
334+
t["source"] = source != null ? source.Name : "[C]";
335+
t["short_src"] = source.Name.Substring(0, Math.Min(source.Name.Length, 60));
336+
t["what"] = "Lua";
337+
}
338+
else
339+
{
340+
t["source"] = "[C]";
341+
t["short_src"] = "[C]";
342+
t["what"] = "C";
343+
}
344+
}
318345

319-
// if (vf.Type == DataType.Function)
320-
// {
321-
// Closure f = vf.Function;
322-
// executionContext.GetInfoForFunction
323-
// }
324-
// else if (vf.Type == DataType.ClrFunction)
325-
// {
346+
if (what.Contains("L"))
347+
{
326348

327-
// }
328-
// else if (vf.Type == DataType.Number || vf.Type == DataType.String)
329-
// {
349+
}
330350

331-
// }
332-
// else
333-
// {
334-
// args.AsType(vfArgIdx + 0, "getinfo", DataType.Number, true);
335-
// }
351+
if (what.Contains("n"))
352+
{
336353

337-
// return vt;
354+
}
338355

356+
if (what.Contains("u"))
357+
{
339358

340-
//}
359+
}
360+
t["linedefined"] = info.Location.FromLine;
361+
t["lastlinedefined"] = info.Location.ToLine;
362+
return vt;
363+
}
364+
return DynValue.Nil;
365+
}
366+
367+
private static void GetInfoAboutFunction(Table infoTable, DynValue function, string what, ScriptExecutionContext executionContext)
368+
{
369+
if (function.Type == DataType.Function)
370+
{
371+
Closure f = function.Function;
372+
var sourceRef = executionContext.GetFunctionSourceCodeRef(f);
373+
var source = executionContext.OwnerScript.GetSourceCode(sourceRef.SourceIdx);
374+
if (what.Contains("S"))
375+
{
376+
infoTable["source"] = source.Name;
377+
infoTable["short_src"] = source.Name.Substring(0, Math.Min(source.Name.Length, 60));
378+
379+
infoTable["what"] = "Lua";
380+
}
381+
382+
if (what.Contains("L"))
383+
{
384+
var lines = new Table(executionContext.OwnerScript);
385+
for (int i = sourceRef.FromLine; i <= sourceRef.ToLine; i++)
386+
lines.Append(DynValue.NewString(source.Lines[i]));
387+
infoTable["activelines"] = lines;
388+
}
389+
390+
if (what.Contains("n"))
391+
{
392+
var name = executionContext.GetFunctionName(f);
393+
infoTable["name"] = name;
394+
var symbolRef = executionContext.FindSymbolByName(name);
395+
switch (symbolRef.Type)
396+
{
397+
case SymbolRefType.Global:
398+
infoTable["namewhat"] = "global";
399+
break;
400+
case SymbolRefType.Local:
401+
infoTable["namewhat"] = "local";
402+
break;
403+
case SymbolRefType.DefaultEnv:
404+
infoTable["namewhat"] = "field";
405+
break;
406+
}
407+
}
408+
409+
if (what.Contains("u"))
410+
{
411+
infoTable["nups"] = f.GetUpvaluesCount();
412+
}
413+
414+
infoTable["linedefined"] = sourceRef.FromLine;
415+
infoTable["lastlinedefined"] = sourceRef.ToLine;
416+
}
417+
else if (function.Type == DataType.ClrFunction)
418+
{
419+
CallbackFunction f = function.Callback;
420+
if (what.Contains("S"))
421+
{
422+
infoTable["source"] = "[C]";
423+
infoTable["short_src"] = "[C]";
424+
infoTable["what"] = "C";
425+
}
426+
427+
if (what.Contains("n"))
428+
{
429+
var symbolRef = executionContext.FindSymbolByName(f.Name);
430+
infoTable["name"] = f.Name;
431+
switch (symbolRef.Type)
432+
{
433+
case SymbolRefType.Global:
434+
infoTable["namewhat"] = "global";
435+
break;
436+
case SymbolRefType.Local:
437+
infoTable["namewhat"] = "local";
438+
break;
439+
case SymbolRefType.DefaultEnv:
440+
infoTable["namewhat"] = "field";
441+
break;
442+
}
443+
}
444+
}
445+
446+
}
341447

342448
}
343449
}

src/MoonSharp.Interpreter/CoreLib/StringLib/KopiLua_StrLib.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class capture_
101101
};
102102

103103

104-
public const int MAXCCALLS = 200;
104+
public const int MAXCCALLS = 1000;
105105
public const char L_ESC = '%';
106106
public const string SPECIALS = "^$*+?.([%-";
107107

@@ -959,7 +959,7 @@ public static int str_format(LuaState L)
959959
case 's':
960960
{
961961
uint l;
962-
CharPtr s = LuaLCheckLString(L, arg, out l);
962+
CharPtr s = LuaLCheckLString(L, arg, out l, true);
963963
if ((strchr(form, '.') == null) && l >= 100)
964964
{
965965
/* no precision and string is too long to be formatted;

src/MoonSharp.Interpreter/CoreLib/TableIteratorsModule.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class TableIteratorsModule
2020
public static DynValue ipairs(ScriptExecutionContext executionContext, CallbackArguments args)
2121
{
2222
DynValue table = args[0];
23+
if(table == null || table.Type == DataType.Nil)
24+
throw new System.ApplicationException("attempt to index nil");
2325

2426
DynValue meta = executionContext.GetMetamethodTailCall(table, "__ipairs", args.GetArray());
2527

src/MoonSharp.Interpreter/DataTypes/DynValue.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,12 @@ public override bool Equals(object obj)
650650
return Table == other.Table;
651651
case DataType.Tuple:
652652
case DataType.TailCallRequest:
653-
return Tuple == other.Tuple;
653+
if (Tuple.Length != other.Tuple.Length)
654+
return false;
655+
for(int i = 0; i < Tuple.Length; i++)
656+
if (!Equals(Tuple[i], other.Tuple[i]))
657+
return false;
658+
return true;
654659
case DataType.Thread:
655660
return Coroutine == other.Coroutine;
656661
case DataType.UserData:
@@ -709,6 +714,13 @@ public string CastToString()
709714
}
710715
else if (rv.Type == DataType.String)
711716
{
717+
if (rv.String.StartsWith("0x"))
718+
{
719+
if (long.TryParse(rv.String, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out long value))
720+
return value;
721+
return null;
722+
}
723+
712724
double num;
713725
if (double.TryParse(rv.String, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
714726
return num;

src/MoonSharp.Interpreter/Errors/InterpreterException.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Text;
34
using MoonSharp.Interpreter.Debugging;
45

56
namespace MoonSharp.Interpreter
@@ -86,11 +87,36 @@ internal void DecorateMessage(Script script, SourceRef sref, int ip = -1)
8687
}
8788
else if (sref != null)
8889
{
89-
this.DecoratedMessage = string.Format("{0}: {1}", sref.FormatLocation(script), this.Message);
90+
var buffer = new StringBuilder($"{sref.FormatLocation(script)}: {this.Message}");
91+
if (CallStack != null)
92+
{
93+
buffer.AppendLine();
94+
foreach (var item in CallStack)
95+
if (item.Location != null)
96+
buffer.AppendLine($"{item.Location.FormatLocation(script)}: {item.Name}");
97+
else
98+
buffer.AppendLine($"unknown: {item.Name}");
99+
}
100+
else
101+
new object();
102+
103+
this.DecoratedMessage = buffer.ToString();
90104
}
91105
else
92106
{
93-
this.DecoratedMessage = string.Format("bytecode:{0}: {1}", ip, this.Message);
107+
var buffer = new StringBuilder($"bytecode:{ip}: {this.Message}");
108+
if (CallStack != null)
109+
{
110+
buffer.AppendLine();
111+
foreach (var item in CallStack)
112+
if (item.Location != null)
113+
buffer.AppendLine($"{item.Location.FormatLocation(script)}: {item.Name}");
114+
else
115+
buffer.AppendLine($"unknown: {item.Name}");
116+
}
117+
else
118+
new object();
119+
this.DecoratedMessage = buffer.ToString();
94120
}
95121
}
96122
}

src/MoonSharp.Interpreter/Execution/ScriptExecutionContext.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ public object AdditionalData
5454
}
5555
}
5656

57+
public SourceRef GetFunctionSourceCodeRef(Closure fn)
58+
{
59+
var i = fn.EntryPointByteCodeLocation;
60+
return m_Processor.FindMeta(ref i).SourceCodeRef;
61+
}
62+
63+
public string GetFunctionName(Closure fn)
64+
{
65+
var i = fn.EntryPointByteCodeLocation;
66+
return m_Processor.FindMeta(ref i).Name;
67+
}
5768

5869
/// <summary>
5970
/// Gets the metatable associated with the given value.

src/MoonSharp.Interpreter/Execution/VM/Processor/Processor_Errors.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ namespace MoonSharp.Interpreter.Execution.VM
44
{
55
sealed partial class Processor
66
{
7+
public SourceRef GetCurrentSourceRef()
8+
{
9+
if (m_SavedInstructionPtr >= 0 && m_SavedInstructionPtr < m_RootChunk.Code.Count)
10+
{
11+
return m_RootChunk.Code[m_SavedInstructionPtr].SourceCodeRef;
12+
}
13+
14+
return null;
15+
}
716
private SourceRef GetCurrentSourceRef(int instructionPtr)
817
{
918
if (instructionPtr >= 0 && instructionPtr < m_RootChunk.Code.Count)

src/MoonSharp.Interpreter/Interop/LuaStateInterop/CharPtr.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,13 @@ public CharPtr(IntPtr ptr)
134134
public static CharPtr operator +(CharPtr ptr, uint offset) { return new CharPtr(ptr.chars, ptr.index + (int)offset); }
135135
public static CharPtr operator -(CharPtr ptr, uint offset) { return new CharPtr(ptr.chars, ptr.index - (int)offset); }
136136

137-
public void inc() { this.index++; }
138-
public void dec() { this.index--; }
137+
public CharPtr Copy()
138+
{
139+
return new CharPtr(this);
140+
}
141+
142+
public CharPtr inc() { this.index++; return this; }
143+
public CharPtr dec() { this.index--; return this; }
139144
public CharPtr next() { return new CharPtr(this.chars, this.index + 1); }
140145
public CharPtr prev() { return new CharPtr(this.chars, this.index - 1); }
141146
public CharPtr add(int ofs) { return new CharPtr(this.chars, this.index + ofs); }

0 commit comments

Comments
 (0)