Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Unwrap exception when calling entrypoint #74235

Merged
merged 2 commits into from
Aug 20, 2022
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 @@ -85,6 +85,9 @@ public static void CallEntrypoint(JSMarshalerArgument* arguments_buffer)
}
catch (Exception ex)
{
if (ex is TargetInvocationException refEx && refEx.InnerException != null)
ex = refEx.InnerException;

arg_exc.ToJS(ex);
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/tests/BuildWasmApps/Wasm.Build.Tests/WasmBuildAppTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public static int Main()
}
}", buildArgs, host, id);

[Theory]
[MemberData(nameof(MainMethodTestData), parameters: new object[] { /*aot*/ false, RunHost.All })]
public void ExceptionFromMain(BuildArgs buildArgs, RunHost host, string id)
=> TestMain("main_exception", """
using System;
using System.Threading.Tasks;

public class TestClass {
public static int Main() => throw new Exception("MessageFromMyException");
}
""", buildArgs, host, id, expectedExitCode: 71, expectedOutput: "Error: MessageFromMyException");

private static string s_bug49588_ProgramCS = @"
using System;
public class TestClass {
Expand Down Expand Up @@ -165,7 +177,9 @@ protected void TestMain(string projectName,
RunHost host,
string id,
string extraProperties = "",
bool? dotnetWasmFromRuntimePack = null)
bool? dotnetWasmFromRuntimePack = null,
int expectedExitCode = 42,
string expectedOutput = "Hello, World!")
{
buildArgs = buildArgs with { ProjectName = projectName };
buildArgs = ExpandBuildArgs(buildArgs, extraProperties);
Expand All @@ -179,8 +193,8 @@ protected void TestMain(string projectName,
InitProject: () => File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), programText),
DotnetWasmFromRuntimePack: dotnetWasmFromRuntimePack));

RunAndTestWasmApp(buildArgs, expectedExitCode: 42,
test: output => Assert.Contains("Hello, World!", output), host: host, id: id);
RunAndTestWasmApp(buildArgs, expectedExitCode: expectedExitCode,
test: output => Assert.Contains(expectedOutput, output), host: host, id: id);
}
}
}