Skip to content

Commit 41d53e8

Browse files
committed
RethrowExceptionNested + new string methods + lexer fixes
1 parent 4557874 commit 41d53e8

20 files changed

Lines changed: 179 additions & 47 deletions

src/MoonSharp.Interpreter.Tests/EndToEnd/CollectionsBaseGenRegisteredTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void Do(string code, Action<DynValue, RegCollMethods> asserts)
8383
catch (ScriptRuntimeException ex)
8484
{
8585
Debug.WriteLine(ex.DecoratedMessage);
86+
ex.Rethrow();
8687
throw;
8788
}
8889
finally

src/MoonSharp.Interpreter.Tests/EndToEnd/CollectionsBaseInterfGenRegisteredTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void Do(string code, Action<DynValue, RegCollMethods> asserts)
8383
catch (ScriptRuntimeException ex)
8484
{
8585
Debug.WriteLine(ex.DecoratedMessage);
86+
ex.Rethrow();
8687
throw;
8788
}
8889
finally

src/MoonSharp.Interpreter.Tests/EndToEnd/CollectionsRegisteredTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void Do(string code, Action<DynValue, RegCollMethods> asserts)
8383
catch (ScriptRuntimeException ex)
8484
{
8585
Debug.WriteLine(ex.DecoratedMessage);
86+
ex.Rethrow();
8687
throw;
8788
}
8889
finally

src/MoonSharp.Interpreter.Tests/EndToEnd/CoroutineTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function b()
118118
Assert.AreEqual(DataType.Boolean, ret.Tuple[0].Type);
119119
Assert.AreEqual(false, ret.Tuple[0].Boolean);
120120
Assert.AreEqual(DataType.String, ret.Tuple[1].Type);
121-
Assert.AreEqual("attempt to yield across a CLR-call boundary", ret.Tuple[1].String);
121+
Assert.IsTrue(ret.Tuple[1].String.EndsWith("attempt to yield across a CLR-call boundary"));
122122
}
123123

124124
[Test]
@@ -131,7 +131,7 @@ function checkresume(step, ex, ey)
131131
local x, y = coroutine.resume(c)
132132
133133
assert(x == ex, 'Step ' .. step .. ': ' .. tostring(ex) .. ' was expected, got ' .. tostring(x));
134-
assert(y == ey, 'Step ' .. step .. ': ' .. tostring(ey) .. ' was expected, got ' .. tostring(y));
134+
assert(y:endsWith(ey), 'Step ' .. step .. ': ' .. tostring(ey) .. ' was expected, got ' .. tostring(y));
135135
end
136136
137137

src/MoonSharp.Interpreter.Tests/TapRunner.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ public class TapRunner
3232
{
3333
string m_File;
3434

35+
/// <summary>
36+
/// Prints the specified string.
37+
/// </summary>
38+
/// <param name="str">The string.</param>
3539
public void Print(string str)
3640
{
41+
// System.Diagnostics.Debug.WriteLine(str);
42+
3743
Assert.IsFalse(str.Trim().StartsWith("not ok"), string.Format("TAP fail ({0}) : {1}", m_File, str));
3844
}
3945

src/MoonSharp.Interpreter/CoreLib/BasicModule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public static DynValue assert(ScriptExecutionContext executionContext, CallbackA
4242
if (!v.CastToBool())
4343
{
4444
if (message.IsNil())
45-
throw new ScriptRuntimeException("assertion failed!") { DoNotDecorateMessage = true };
45+
throw new ScriptRuntimeException("assertion failed!"); // { DoNotDecorateMessage = true };
4646
else
47-
throw new ScriptRuntimeException(message.ToPrintString()) { DoNotDecorateMessage = true };
47+
throw new ScriptRuntimeException(message.ToPrintString()); // { DoNotDecorateMessage = true };
4848
}
4949

5050
return DynValue.NewTupleNested(args.GetArray());
@@ -84,7 +84,7 @@ public static DynValue collectgarbage(ScriptExecutionContext executionContext, C
8484
public static DynValue error(ScriptExecutionContext executionContext, CallbackArguments args)
8585
{
8686
DynValue message = args.AsType(0, "error", DataType.String, false);
87-
throw new ScriptRuntimeException(message.String) { DoNotDecorateMessage = true };
87+
throw new ScriptRuntimeException(message.String); // { DoNotDecorateMessage = true };
8888
}
8989

9090

src/MoonSharp.Interpreter/CoreLib/IO/FileUserDataBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public DynValue write(ScriptExecutionContext executionContext, CallbackArguments
119119

120120
return UserData.Create(this);
121121
}
122-
catch (ScriptRuntimeException)
122+
catch (ScriptRuntimeException sre)
123123
{
124124
throw;
125125
}
@@ -139,7 +139,7 @@ public DynValue close(ScriptExecutionContext executionContext, CallbackArguments
139139
else
140140
return DynValue.NewTuple(DynValue.Nil, DynValue.NewString(msg));
141141
}
142-
catch (ScriptRuntimeException)
142+
catch (ScriptRuntimeException sre)
143143
{
144144
throw;
145145
}

src/MoonSharp.Interpreter/CoreLib/StringModule.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,43 @@ public static DynValue sub(ScriptExecutionContext executionContext, CallbackArgu
255255

256256
return DynValue.NewString(s);
257257
}
258+
259+
[MoonSharpModuleMethod]
260+
public static DynValue startsWith(ScriptExecutionContext executionContext, CallbackArguments args)
261+
{
262+
DynValue arg_s1 = args.AsType(0, "startsWith", DataType.String, true);
263+
DynValue arg_s2 = args.AsType(1, "startsWith", DataType.String, true);
264+
265+
if (arg_s1.IsNil() || arg_s2.IsNil())
266+
return DynValue.False;
267+
268+
return DynValue.NewBoolean(arg_s1.String.StartsWith(arg_s2.String));
269+
}
270+
271+
[MoonSharpModuleMethod]
272+
public static DynValue endsWith(ScriptExecutionContext executionContext, CallbackArguments args)
273+
{
274+
DynValue arg_s1 = args.AsType(0, "endsWith", DataType.String, true);
275+
DynValue arg_s2 = args.AsType(1, "endsWith", DataType.String, true);
276+
277+
if (arg_s1.IsNil() || arg_s2.IsNil())
278+
return DynValue.False;
279+
280+
return DynValue.NewBoolean(arg_s1.String.EndsWith(arg_s2.String));
281+
}
282+
283+
[MoonSharpModuleMethod]
284+
public static DynValue contains(ScriptExecutionContext executionContext, CallbackArguments args)
285+
{
286+
DynValue arg_s1 = args.AsType(0, "contains", DataType.String, true);
287+
DynValue arg_s2 = args.AsType(1, "contains", DataType.String, true);
288+
289+
if (arg_s1.IsNil() || arg_s2.IsNil())
290+
return DynValue.False;
291+
292+
return DynValue.NewBoolean(arg_s1.String.Contains(arg_s2.String));
293+
}
294+
258295
}
259296

260297

src/MoonSharp.Interpreter/Errors/InterpreterException.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ namespace MoonSharp.Interpreter
99
/// </summary>
1010
public class InterpreterException : Exception
1111
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="InterpreterException"/> class.
14+
/// </summary>
15+
/// <param name="ex">The ex.</param>
16+
protected InterpreterException(Exception ex, string message)
17+
: base(message, ex)
18+
{
19+
20+
}
21+
1222
/// <summary>
1323
/// Initializes a new instance of the <see cref="InterpreterException"/> class.
1424
/// </summary>
@@ -55,19 +65,39 @@ protected InterpreterException(string format, params object[] args)
5565
/// </summary>
5666
public string DecoratedMessage { get; internal set; }
5767

68+
69+
/// <summary>
70+
/// Gets or sets a value indicating whether the message should not be decorated
71+
/// </summary>
72+
public bool DoNotDecorateMessage { get; set; }
73+
74+
5875
internal void DecorateMessage(Script script, SourceRef sref, int ip = -1)
5976
{
60-
if (sref != null)
77+
if (DoNotDecorateMessage)
78+
{
79+
this.DecoratedMessage = this.Message;
80+
}
81+
else if (sref != null)
6182
{
6283
this.DecoratedMessage = string.Format("{0}: {1}", sref.FormatLocation(script), this.Message);
6384
}
6485
else
6586
{
6687
this.DecoratedMessage = string.Format("bytecode:{0}: {1}", ip, this.Message);
6788
}
89+
}
6890

91+
92+
/// <summary>
93+
/// Rethrows this instance if
94+
/// </summary>
95+
/// <returns></returns>
96+
public virtual void Rethrow()
97+
{
6998
}
7099

71100

101+
72102
}
73103
}

src/MoonSharp.Interpreter/Errors/ScriptRuntimeException.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ public class ScriptRuntimeException : InterpreterException
1818
public ScriptRuntimeException(Exception ex)
1919
: base(ex)
2020
{
21+
}
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="ScriptRuntimeException"/> class.
25+
/// </summary>
26+
/// <param name="ex">The ex.</param>
27+
public ScriptRuntimeException(ScriptRuntimeException ex)
28+
: base(ex, ex.DecoratedMessage)
29+
{
30+
this.DecoratedMessage = Message;
31+
this.DoNotDecorateMessage = true;
2132
}
2233

34+
2335
/// <summary>
2436
/// Initializes a new instance of the <see cref="ScriptRuntimeException"/> class.
2537
/// </summary>
@@ -41,11 +53,6 @@ public ScriptRuntimeException(string format, params object[] args)
4153

4254
}
4355

44-
/// <summary>
45-
/// Gets or sets a value indicating whether the message should not be decorated
46-
/// </summary>
47-
public bool DoNotDecorateMessage { get; set; }
48-
4956
/// <summary>
5057
/// Creates a ScriptRuntimeException with a predefined error message specifying that
5158
/// an arithmetic operation was attempted on non-numbers
@@ -496,5 +503,16 @@ public static ScriptRuntimeException AccessInstanceMemberOnStatics(IUserDataDesc
496503
{
497504
return new ScriptRuntimeException("attempt to access instance member {0}.{1} from a static userdata", typeDescr.Name, desc.Name);
498505
}
506+
507+
/// <summary>
508+
/// Rethrows this instance if
509+
/// </summary>
510+
/// <returns></returns>
511+
public override void Rethrow()
512+
{
513+
if (Script.GlobalOptions.RethrowExceptionNested)
514+
throw new ScriptRuntimeException(this);
515+
}
516+
499517
}
500518
}

0 commit comments

Comments
 (0)