Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

wasm add rethrow support #8206

Merged
merged 5 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4404,7 +4404,10 @@ private void ImportInitBlk()

private void ImportRethrow()
{
EmitTrapCall();
// rethrows can only occur from a catch handler in which case there should be an exception slot
Debug.Assert(_spilledExpressions.Count > 0 && _spilledExpressions[0].Name == "ExceptionSlot");

ThrowOrRethrow(_spilledExpressions[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not do things like preserving the original stacktrace that rethrow is expected to do... . I guess good enough for now.

}

private void ImportSizeOf(int token)
Expand Down Expand Up @@ -4451,6 +4454,11 @@ private void ImportThrow()
{
var exceptionObject = _stack.Pop();

ThrowOrRethrow(exceptionObject);
}

void ThrowOrRethrow(StackEntry exceptionObject)
{
if (RhpThrowEx.Handle.Equals(IntPtr.Zero))
{
RhpThrowEx = Module.AddFunction("RhpThrowEx", LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, new LLVMTypeRef[] { LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0) }, false));
Expand Down Expand Up @@ -5390,7 +5398,6 @@ private ObjectNode.ObjectData EncodeEHInfo()
// }

builder.EmitCompressedUInt((uint)totalClauses);

// Iterate backwards to emit the innermost first, but within a try region go forwards to get the first matching catch type
int i = _exceptionRegions.Length - 1;
while (i >= 0)
Expand Down
28 changes: 28 additions & 0 deletions tests/src/Simple/HelloWasm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,8 @@ private static void TestTryCatch()
TestFilterNested();

TestCatchAndThrow();

TestRethrow();
}

private static void TestTryCatchNoException()
Expand Down Expand Up @@ -1849,6 +1851,32 @@ private static void TestFilterNested()
EndTest(exceptionFlowSequence == @"In middle catchRunning outer filterIn outer catchRunning inner filterIn inner catch");
}

private static void TestRethrow()
{
StartTest("Test rethrow");
int caught = 0;
try
{
try
{
throw new Exception("first");
}
catch
{
caught++;
throw;
}
}
catch(Exception e)
{
if (e.Message == "first")
{
caught++;
}
}
EndTest(caught == 2);
}

private static void TestCatchAndThrow()
{
StartTest("Test catch and throw different exception");
Expand Down