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

Commit

Permalink
wasm add rethrow support (#8206)
Browse files Browse the repository at this point in the history
  • Loading branch information
yowl authored Aug 26, 2020
1 parent e185c47 commit 266ae09
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
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]);
}

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 @@ -1640,6 +1640,8 @@ private static void TestTryCatch()
TestFilterNested();

TestCatchAndThrow();

TestRethrow();
}

private static void TestTryCatchNoException()
Expand Down Expand Up @@ -1852,6 +1854,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

0 comments on commit 266ae09

Please sign in to comment.