neo-devpack-dotnet icon indicating copy to clipboard operation
neo-devpack-dotnet copied to clipboard

Nested try/finally blocks only execute inner most finally block

Open devhawk opened this issue 2 years ago • 2 comments

ConvertReturnStatement pushes an ENDTRY operation instead of a JUMP if the return call is inside at least one try/finally block. This works fine if there is only a single try/finally block. If there are nested try/finally blocks, only the inner most finally block is executed. Any other finally blocks are skipped

Example contract Method:

public static void Test2()
{
    try
    {
        Runtime.Log("outer try");

        try
        {
            Runtime.Log("inner try");
            return;
        }
        finally
        {
            Runtime.Log("inner finally");
        }
    }
    finally
    {
        Runtime.Log("outer finally");
    }
}

expected debug output:

Runtime.Log: SampleRegistrar outer try
Runtime.Log: SampleRegistrar inner try
Runtime.Log: SampleRegistrar inner finally
Runtime.Log: SampleRegistrar outer finally

actual debug output:

Runtime.Log: SampleRegistrar outer try
Runtime.Log: SampleRegistrar inner try
Runtime.Log: SampleRegistrar inner finally

devhawk avatar Jun 21 '23 19:06 devhawk

In the TS devpack, I'm planning on emitting an endtry/noop pair for each nested try/catch block except the last one. For the last one, I'm going to emit the endtry to return target. Alternatively, we could emit an endtry/noop pair for each nested try/catch block and then add a JUMP to return target at the end.

devhawk avatar Jun 21 '23 20:06 devhawk

In the TS devpack, I'm planning on emitting an endtry/noop pair for each nested try/catch block except the last one. For the last one, I'm going to emit the endtry to return target. Alternatively, we could emit an endtry/noop pair for each nested try/catch block and then add a JUMP to return target at the end.

Note, there's no need to emit a noop. In the TS compiler, I'm emitting an endtry for each nested try block. For each endtry except the last, I'm just targeting the next endtry. The final endtry targets the return target.

devhawk avatar Jun 22 '23 18:06 devhawk