Skip to content

Commit

Permalink
RethrowExceptionNested + new string methods + lexer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xanathar committed May 16, 2016
1 parent 4557874 commit 41d53e8
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 47 deletions.
Expand Up @@ -83,6 +83,7 @@ void Do(string code, Action<DynValue, RegCollMethods> asserts)
catch (ScriptRuntimeException ex)
{
Debug.WriteLine(ex.DecoratedMessage);
ex.Rethrow();
throw;
}
finally
Expand Down
Expand Up @@ -83,6 +83,7 @@ void Do(string code, Action<DynValue, RegCollMethods> asserts)
catch (ScriptRuntimeException ex)
{
Debug.WriteLine(ex.DecoratedMessage);
ex.Rethrow();
throw;
}
finally
Expand Down
Expand Up @@ -83,6 +83,7 @@ void Do(string code, Action<DynValue, RegCollMethods> asserts)
catch (ScriptRuntimeException ex)
{
Debug.WriteLine(ex.DecoratedMessage);
ex.Rethrow();
throw;
}
finally
Expand Down
4 changes: 2 additions & 2 deletions src/MoonSharp.Interpreter.Tests/EndToEnd/CoroutineTests.cs
Expand Up @@ -118,7 +118,7 @@ function b()
Assert.AreEqual(DataType.Boolean, ret.Tuple[0].Type);
Assert.AreEqual(false, ret.Tuple[0].Boolean);
Assert.AreEqual(DataType.String, ret.Tuple[1].Type);
Assert.AreEqual("attempt to yield across a CLR-call boundary", ret.Tuple[1].String);
Assert.IsTrue(ret.Tuple[1].String.EndsWith("attempt to yield across a CLR-call boundary"));
}

[Test]
Expand All @@ -131,7 +131,7 @@ function checkresume(step, ex, ey)
local x, y = coroutine.resume(c)
assert(x == ex, 'Step ' .. step .. ': ' .. tostring(ex) .. ' was expected, got ' .. tostring(x));
assert(y == ey, 'Step ' .. step .. ': ' .. tostring(ey) .. ' was expected, got ' .. tostring(y));
assert(y:endsWith(ey), 'Step ' .. step .. ': ' .. tostring(ey) .. ' was expected, got ' .. tostring(y));
end
Expand Down
6 changes: 6 additions & 0 deletions src/MoonSharp.Interpreter.Tests/TapRunner.cs
Expand Up @@ -32,8 +32,14 @@ public class TapRunner
{
string m_File;

/// <summary>
/// Prints the specified string.
/// </summary>
/// <param name="str">The string.</param>
public void Print(string str)
{
// System.Diagnostics.Debug.WriteLine(str);

Assert.IsFalse(str.Trim().StartsWith("not ok"), string.Format("TAP fail ({0}) : {1}", m_File, str));
}

Expand Down
6 changes: 3 additions & 3 deletions src/MoonSharp.Interpreter/CoreLib/BasicModule.cs
Expand Up @@ -42,9 +42,9 @@ public static DynValue assert(ScriptExecutionContext executionContext, CallbackA
if (!v.CastToBool())
{
if (message.IsNil())
throw new ScriptRuntimeException("assertion failed!") { DoNotDecorateMessage = true };
throw new ScriptRuntimeException("assertion failed!"); // { DoNotDecorateMessage = true };
else
throw new ScriptRuntimeException(message.ToPrintString()) { DoNotDecorateMessage = true };
throw new ScriptRuntimeException(message.ToPrintString()); // { DoNotDecorateMessage = true };
}

return DynValue.NewTupleNested(args.GetArray());
Expand Down Expand Up @@ -84,7 +84,7 @@ 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 };
throw new ScriptRuntimeException(message.String); // { DoNotDecorateMessage = true };
}


Expand Down
4 changes: 2 additions & 2 deletions src/MoonSharp.Interpreter/CoreLib/IO/FileUserDataBase.cs
Expand Up @@ -119,7 +119,7 @@ public DynValue write(ScriptExecutionContext executionContext, CallbackArguments

return UserData.Create(this);
}
catch (ScriptRuntimeException)
catch (ScriptRuntimeException sre)
{
throw;
}
Expand All @@ -139,7 +139,7 @@ public DynValue close(ScriptExecutionContext executionContext, CallbackArguments
else
return DynValue.NewTuple(DynValue.Nil, DynValue.NewString(msg));
}
catch (ScriptRuntimeException)
catch (ScriptRuntimeException sre)
{
throw;
}
Expand Down
37 changes: 37 additions & 0 deletions src/MoonSharp.Interpreter/CoreLib/StringModule.cs
Expand Up @@ -255,6 +255,43 @@ public static DynValue sub(ScriptExecutionContext executionContext, CallbackArgu

return DynValue.NewString(s);
}

[MoonSharpModuleMethod]
public static DynValue startsWith(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue arg_s1 = args.AsType(0, "startsWith", DataType.String, true);
DynValue arg_s2 = args.AsType(1, "startsWith", DataType.String, true);

if (arg_s1.IsNil() || arg_s2.IsNil())
return DynValue.False;

return DynValue.NewBoolean(arg_s1.String.StartsWith(arg_s2.String));
}

[MoonSharpModuleMethod]
public static DynValue endsWith(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue arg_s1 = args.AsType(0, "endsWith", DataType.String, true);
DynValue arg_s2 = args.AsType(1, "endsWith", DataType.String, true);

if (arg_s1.IsNil() || arg_s2.IsNil())
return DynValue.False;

return DynValue.NewBoolean(arg_s1.String.EndsWith(arg_s2.String));
}

[MoonSharpModuleMethod]
public static DynValue contains(ScriptExecutionContext executionContext, CallbackArguments args)
{
DynValue arg_s1 = args.AsType(0, "contains", DataType.String, true);
DynValue arg_s2 = args.AsType(1, "contains", DataType.String, true);

if (arg_s1.IsNil() || arg_s2.IsNil())
return DynValue.False;

return DynValue.NewBoolean(arg_s1.String.Contains(arg_s2.String));
}

}


Expand Down
32 changes: 31 additions & 1 deletion src/MoonSharp.Interpreter/Errors/InterpreterException.cs
Expand Up @@ -9,6 +9,16 @@ namespace MoonSharp.Interpreter
/// </summary>
public class InterpreterException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="InterpreterException"/> class.
/// </summary>
/// <param name="ex">The ex.</param>
protected InterpreterException(Exception ex, string message)
: base(message, ex)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="InterpreterException"/> class.
/// </summary>
Expand Down Expand Up @@ -55,19 +65,39 @@ protected InterpreterException(string format, params object[] args)
/// </summary>
public string DecoratedMessage { get; internal set; }


/// <summary>
/// Gets or sets a value indicating whether the message should not be decorated
/// </summary>
public bool DoNotDecorateMessage { get; set; }


internal void DecorateMessage(Script script, SourceRef sref, int ip = -1)
{
if (sref != null)
if (DoNotDecorateMessage)
{
this.DecoratedMessage = this.Message;
}
else if (sref != null)
{
this.DecoratedMessage = string.Format("{0}: {1}", sref.FormatLocation(script), this.Message);
}
else
{
this.DecoratedMessage = string.Format("bytecode:{0}: {1}", ip, this.Message);
}
}


/// <summary>
/// Rethrows this instance if
/// </summary>
/// <returns></returns>
public virtual void Rethrow()
{
}



}
}
28 changes: 23 additions & 5 deletions src/MoonSharp.Interpreter/Errors/ScriptRuntimeException.cs
Expand Up @@ -18,8 +18,20 @@ public class ScriptRuntimeException : InterpreterException
public ScriptRuntimeException(Exception ex)
: base(ex)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ScriptRuntimeException"/> class.
/// </summary>
/// <param name="ex">The ex.</param>
public ScriptRuntimeException(ScriptRuntimeException ex)
: base(ex, ex.DecoratedMessage)
{
this.DecoratedMessage = Message;
this.DoNotDecorateMessage = true;
}


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

}

/// <summary>
/// Gets or sets a value indicating whether the message should not be decorated
/// </summary>
public bool DoNotDecorateMessage { get; set; }

/// <summary>
/// Creates a ScriptRuntimeException with a predefined error message specifying that
/// an arithmetic operation was attempted on non-numbers
Expand Down Expand Up @@ -496,5 +503,16 @@ public static ScriptRuntimeException AccessInstanceMemberOnStatics(IUserDataDesc
{
return new ScriptRuntimeException("attempt to access instance member {0}.{1} from a static userdata", typeDescr.Name, desc.Name);
}

/// <summary>
/// Rethrows this instance if
/// </summary>
/// <returns></returns>
public override void Rethrow()
{
if (Script.GlobalOptions.RethrowExceptionNested)
throw new ScriptRuntimeException(this);
}

}
}
18 changes: 18 additions & 0 deletions src/MoonSharp.Interpreter/Errors/SyntaxErrorException.cs
Expand Up @@ -42,12 +42,30 @@ internal SyntaxErrorException(Script script, SourceRef sref, string message)
DecorateMessage(script, sref);
}

private SyntaxErrorException(SyntaxErrorException syntaxErrorException)
: base(syntaxErrorException, syntaxErrorException.DecoratedMessage)
{
this.Token = syntaxErrorException.Token;
this.DecoratedMessage = Message;
}

internal void DecorateMessage(Script script)
{
if (Token != null)
{
DecorateMessage(script, Token.GetSourceRef(false));
}
}

/// <summary>
/// Rethrows this instance if
/// </summary>
/// <returns></returns>
public override void Rethrow()
{
if (Script.GlobalOptions.RethrowExceptionNested)
throw new SyntaxErrorException(this);
}

}
}
Expand Up @@ -264,5 +264,6 @@ public Script OwnerScript
{
get { return this.GetScript(); }
}

}
}
Expand Up @@ -250,7 +250,11 @@ private DynValue Processing_Loop(int instructionPtr)
{
FillDebugData(ex, instructionPtr);

if (!(ex is ScriptRuntimeException)) throw;
if (!(ex is ScriptRuntimeException))
{
ex.Rethrow();
throw;
}

if (m_Debug.DebuggerAttached != null)
{
Expand Down Expand Up @@ -296,10 +300,12 @@ private DynValue Processing_Loop(int instructionPtr)
}
else if ((csi.Flags & CallStackItemFlags.EntryPoint) != 0)
{
ex.Rethrow();
throw;
}
}

ex.Rethrow();
throw;
}

Expand Down
Expand Up @@ -153,13 +153,16 @@ private static MethodInfo InstantiateMethodInfo(MethodInfo mi, Type extensionTyp

private static Type GetGenericMatch(Type extensionType, Type extendedType)
{
extensionType = extensionType.GetGenericTypeDefinition();

foreach (Type t in extendedType.GetAllImplementedTypes())
if (!extensionType.IsGenericParameter)
{
if (t.IsGenericType && t.GetGenericTypeDefinition() == extensionType)
extensionType = extensionType.GetGenericTypeDefinition();

foreach (Type t in extendedType.GetAllImplementedTypes())
{
return t;
if (t.IsGenericType && t.GetGenericTypeDefinition() == extensionType)
{
return t;
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/MoonSharp.Interpreter/REPL/ReplInterpreter.cs
Expand Up @@ -95,16 +95,18 @@ public virtual DynValue Evaluate(string input)
if (forced || !ex.IsPrematureStreamTermination)
{
m_CurrentCommand = "";
ex.Rethrow();
throw;
}
else
{
return null;
}
}
catch (ScriptRuntimeException)
catch (ScriptRuntimeException sre)
{
m_CurrentCommand = "";
sre.Rethrow();
throw;
}
catch (Exception)
Expand Down
6 changes: 5 additions & 1 deletion src/MoonSharp.Interpreter/ScriptGlobalOptions.cs
Expand Up @@ -28,6 +28,10 @@ internal ScriptGlobalOptions()
/// </value>
public IPlatformAccessor Platform { get; set; }


/// <summary>
/// Gets or sets a value indicating whether interpreter exceptions should be
/// re-thrown as nested exceptions.
/// </summary>
public bool RethrowExceptionNested { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/MoonSharp.Interpreter/Tree/Fast_Interface/Loader_Fast.cs
Expand Up @@ -26,6 +26,7 @@ internal static DynamicExprExpression LoadDynamicExpr(Script script, SourceCode
catch (SyntaxErrorException ex)
{
ex.DecorateMessage(script);
ex.Rethrow();
throw;
}
}
Expand Down Expand Up @@ -70,6 +71,7 @@ internal static int LoadChunk(Script script, SourceCode source, ByteCode bytecod
catch (SyntaxErrorException ex)
{
ex.DecorateMessage(script);
ex.Rethrow();
throw;
}
}
Expand Down Expand Up @@ -104,6 +106,7 @@ internal static int LoadFunction(Script script, SourceCode source, ByteCode byte
catch (SyntaxErrorException ex)
{
ex.DecorateMessage(script);
ex.Rethrow();
throw;
}

Expand Down

0 comments on commit 41d53e8

Please sign in to comment.