-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for exception handling
- Loading branch information
1 parent
df3a7cf
commit c1c3f34
Showing
38 changed files
with
1,452 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace WebAssembly.Instructions; | ||
|
||
/// <summary> | ||
/// Tests the <see cref="Try"/> instruction. | ||
/// </summary> | ||
[TestClass] | ||
public class CatchAllTests | ||
{ | ||
/// <summary> | ||
/// Tests compilation and execution of the <see cref="CatchAll"/> instruction when there are no exceptions. | ||
/// </summary> | ||
[TestMethod] | ||
public void CatchAll_NoException() | ||
{ | ||
Assert.AreEqual<int>(0, AssemblyBuilder.CreateInstance<dynamic>("Test", WebAssemblyValueType.Int32, | ||
new Try(), | ||
new Int32Constant(0), | ||
new Return(), | ||
new CatchAll(), | ||
new Int32Constant(1), | ||
new Return(), | ||
new End(), | ||
|
||
new Int32Constant(2), | ||
new End() | ||
).Test()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests compilation and execution of the <see cref="CatchAll"/> instruction when there is an exception. | ||
/// </summary> | ||
[TestMethod] | ||
public void CatchAll_Catch() | ||
{ | ||
Assert.AreEqual<int>(0, AssemblyBuilder.CreateInstance<dynamic>("Test", WebAssemblyValueType.Int32, | ||
new Try(), | ||
new Int32Constant(0), | ||
new Return(), | ||
new CatchAll(), | ||
new Int32Constant(1), | ||
new Return(), | ||
new End(), | ||
|
||
new Int32Constant(2), | ||
new End() | ||
).Test()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace WebAssembly.Instructions; | ||
|
||
/// <summary> | ||
/// Tests the <see cref="Catch"/> instruction. | ||
/// </summary> | ||
[TestClass] | ||
public class CatchTests | ||
{ | ||
/// <summary> | ||
/// Tests compilation and execution of the <see cref="Catch"/> instruction when there are no exceptions. | ||
/// </summary> | ||
[TestMethod] | ||
public void Catch_NoException() | ||
{ | ||
Assert.AreEqual<int>(0, AssemblyBuilder.CreateInstance<dynamic>("Test", WebAssemblyValueType.Int32, | ||
new Instruction[] | ||
{ | ||
new Try(), | ||
new Int32Constant(0), | ||
new Return(), | ||
new Catch(0), | ||
new Int32Constant(1), | ||
new Return(), | ||
new End(), | ||
|
||
new Int32Constant(2), | ||
new End() | ||
}, | ||
module => | ||
{ | ||
module.Types.Add(new WebAssemblyType | ||
{ | ||
Parameters = new[] | ||
{ | ||
WebAssemblyValueType.Int32 | ||
}, | ||
}); | ||
|
||
module.Tags.Add(new WebAssemblyTag | ||
{ | ||
TypeIndex = 1 | ||
}); | ||
} | ||
).Test()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests compilation and execution of the <see cref="Catch"/> instruction when there is an exception. | ||
/// </summary> | ||
[TestMethod] | ||
public void Catch_Exception() | ||
{ | ||
Assert.AreEqual<int>(1, AssemblyBuilder.CreateInstance<dynamic>("Test", WebAssemblyValueType.Int32, | ||
new Instruction[] | ||
{ | ||
new Try(), | ||
new Int32Constant(0), | ||
new Throw(0), | ||
new Catch(0), | ||
new Int32Constant(1), | ||
new Return(), | ||
new End(), | ||
|
||
new Int32Constant(2), | ||
new End() | ||
}, | ||
module => | ||
{ | ||
module.Types.Add(new WebAssemblyType | ||
{ | ||
Parameters = new[] | ||
{ | ||
WebAssemblyValueType.Int32 | ||
}, | ||
}); | ||
|
||
module.Tags.Add(new WebAssemblyTag | ||
{ | ||
TypeIndex = 1 | ||
}); | ||
} | ||
).Test()); | ||
} | ||
|
||
/// <summary> | ||
/// Tests compilation and execution of the <see cref="Catch"/> instruction when there is an exception of the wrong type. | ||
/// </summary> | ||
[TestMethod] | ||
public void Catch_MultipleExceptionTags() | ||
{ | ||
Assert.AreEqual<int>(2, AssemblyBuilder.CreateInstance<dynamic>("Test", WebAssemblyValueType.Int32, | ||
new Instruction[] | ||
{ | ||
new Try(), | ||
new Int32Constant(0), | ||
new Throw(1), | ||
new Catch(0), | ||
new Int32Constant(1), | ||
new Return(), | ||
new Catch(1), | ||
new Int32Constant(2), | ||
new Return(), | ||
new End(), | ||
|
||
new Int32Constant(3), | ||
new End() | ||
}, | ||
module => | ||
{ | ||
module.Types.Add(new WebAssemblyType | ||
{ | ||
Parameters = new[] | ||
{ | ||
WebAssemblyValueType.Int32 | ||
}, | ||
}); | ||
|
||
module.Tags.Add(new WebAssemblyTag | ||
{ | ||
TypeIndex = 1 | ||
}); | ||
|
||
module.Tags.Add(new WebAssemblyTag | ||
{ | ||
TypeIndex = 1 | ||
}); | ||
} | ||
).Test()); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Tests compilation and execution of the <see cref="Rethrow"/> instruction. | ||
/// </summary> | ||
[TestMethod] | ||
public void Catch_TryInCatch() | ||
{ | ||
Assert.AreEqual(1, AssemblyBuilder.CreateInstance<dynamic>("Test", WebAssemblyValueType.Int32, | ||
new Instruction[] | ||
{ | ||
new Try(), | ||
|
||
new Throw(0), | ||
|
||
new Catch(0), | ||
|
||
new Try(), | ||
new Throw(1), | ||
new Catch(1), | ||
new Int32Constant(1), | ||
new Return(), | ||
new End(), | ||
|
||
new End(), | ||
|
||
new Int32Constant(0), | ||
new End() | ||
}, | ||
module => | ||
{ | ||
module.Types.Add(new WebAssemblyType | ||
{ | ||
Parameters = Array.Empty<WebAssemblyValueType>() | ||
}); | ||
|
||
module.Tags.Add(new WebAssemblyTag | ||
{ | ||
TypeIndex = 1 | ||
}); | ||
|
||
module.Tags.Add(new WebAssemblyTag | ||
{ | ||
TypeIndex = 1 | ||
}); | ||
} | ||
).Test()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using WebAssembly.Runtime; | ||
|
||
namespace WebAssembly.Instructions; | ||
|
||
/// <summary> | ||
/// Tests the <see cref="Rethrow"/> instruction. | ||
/// </summary> | ||
[TestClass] | ||
public class RethrowTests | ||
{ | ||
/// <summary> | ||
/// Tests compilation and execution of the <see cref="Rethrow"/> instruction. | ||
/// </summary> | ||
[TestMethod] | ||
public void Rethrow_Exception() | ||
{ | ||
Assert.ThrowsException<WebAssemblyException>(() => | ||
{ | ||
AssemblyBuilder.CreateInstance<dynamic>("Test", WebAssemblyValueType.Int32, | ||
new Instruction[] | ||
{ | ||
new Try(), | ||
new Throw(0), | ||
new Catch(0), | ||
new Rethrow(0), | ||
new End(), | ||
|
||
new Int32Constant(0), | ||
new End() | ||
}, | ||
module => | ||
{ | ||
module.Types.Add(new WebAssemblyType | ||
{ | ||
Parameters = Array.Empty<WebAssemblyValueType>() | ||
}); | ||
|
||
module.Tags.Add(new WebAssemblyTag | ||
{ | ||
TypeIndex = 1 | ||
}); | ||
} | ||
).Test(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using WebAssembly.Runtime; | ||
|
||
namespace WebAssembly.Instructions; | ||
|
||
/// <summary> | ||
/// Tests the <see cref="Throw"/> instruction. | ||
/// </summary> | ||
[TestClass] | ||
public class ThrowTests | ||
{ | ||
/// <summary> | ||
/// Tests compilation and execution of the <see cref="Throw"/> instruction. | ||
/// </summary> | ||
[TestMethod] | ||
public void Throw_Exception() | ||
{ | ||
Assert.ThrowsException<WebAssemblyException>(() => | ||
{ | ||
AssemblyBuilder.CreateInstance<dynamic>("Test", WebAssemblyValueType.Int32, | ||
new Instruction[] | ||
{ | ||
new Throw(0), | ||
|
||
new End() | ||
}, | ||
module => | ||
{ | ||
module.Types.Add(new WebAssemblyType | ||
{ | ||
Parameters = Array.Empty<WebAssemblyValueType>() | ||
}); | ||
|
||
module.Tags.Add(new WebAssemblyTag | ||
{ | ||
TypeIndex = 1 | ||
}); | ||
} | ||
).Test(); | ||
}); | ||
} | ||
} |
Oops, something went wrong.