Skip to content

Commit

Permalink
Update for the changes in bytecodealliance/wasmtime#5215.
Browse files Browse the repository at this point in the history
This moves the ExitStatus from TrapException/TrapAccessor to WasmtimeException.
  • Loading branch information
kpreisser committed Nov 7, 2022
1 parent b7dbedf commit 808d632
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 45 deletions.
33 changes: 1 addition & 32 deletions src/TrapException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,6 @@ public TrapCode TrapCode
}
}

/// <summary>
/// Attempt to get a WASI exit status code from this trap. May be null if no status code is available.
/// </summary>
public int? ExitStatus
{
get
{
if (TrapException.Native.wasmtime_trap_exit_status(_trap, out var exitStatus))
{
return exitStatus;
}

return null;
}
}

/// <summary>
/// Get the message string
/// </summary>
Expand Down Expand Up @@ -266,13 +250,6 @@ public TrapException(string message, Exception? inner) : base(message, inner) {
/// </summary>
public IReadOnlyList<TrapFrame>? Frames { get; protected set; }

/// <summary>
/// The exit code when the trap results from executing the WASI `proc_exit` function.
///
/// The value is null if the trap was not an exit trap.
/// </summary>
public int? ExitCode { get; private set; }

/// <summary>
/// Indentifies which type of trap this is.
/// </summary>
Expand Down Expand Up @@ -303,11 +280,7 @@ internal static TrapException FromOwnedTrap(IntPtr trap, bool delete = true)
var accessor = new TrapAccessor(trap);
try
{
var trappedException = new TrapException(accessor.Message, accessor.GetFrames(), accessor.TrapCode, callbackTrapCause)
{
ExitCode = accessor.ExitStatus
};

var trappedException = new TrapException(accessor.Message, accessor.GetFrames(), accessor.TrapCode, callbackTrapCause);
return trappedException;
}
finally
Expand Down Expand Up @@ -343,10 +316,6 @@ public void Dispose()
[DllImport(Engine.LibraryName)]
public static extern void wasm_frame_vec_delete(in FrameArray vec);

[DllImport(Engine.LibraryName)]
[return: MarshalAs(UnmanagedType.I1)]
internal static extern bool wasmtime_trap_exit_status(IntPtr trap, out int exitStatus);

[DllImport(Engine.LibraryName)]
[return: MarshalAs(UnmanagedType.I1)]
internal static extern bool wasmtime_trap_code(IntPtr trap, out TrapCode exitCode);
Expand Down
39 changes: 33 additions & 6 deletions src/WasmtimeException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,45 @@ public WasmtimeException(string message) : base(message) { }
/// <inheritdoc/>
public WasmtimeException(string message, Exception? inner) : base(message, inner) { }

/// <summary>
/// Gets the exit code when the trap results from executing the WASI <c>proc_exit</c> function.
///
/// The value is <c>null</c> if the trap was not an exit trap.
/// </summary>
public int? ExitCode { get; private set; }

/// <inheritdoc/>
protected WasmtimeException(SerializationInfo info, StreamingContext context) : base(info, context) { }

internal static WasmtimeException FromOwnedError(IntPtr error)
{
Native.wasmtime_error_message(error, out var bytes);
Native.wasmtime_error_delete(error);

unsafe
try
{
using (var message = bytes)
int? exitStatus = null;
if (Native.wasmtime_error_exit_status(error, out int localExitStatus))
{
exitStatus = localExitStatus;
}

Native.wasmtime_error_message(error, out var bytes);

using (bytes)
{
return new WasmtimeException(Encoding.UTF8.GetString(message.data, (int)message.size));
unsafe
{
var byteSpan = new ReadOnlySpan<byte>(bytes.data, checked((int)bytes.size));

return new WasmtimeException(Encoding.UTF8.GetString(byteSpan))
{
ExitCode = exitStatus
};
}
}
}
finally
{
Native.wasmtime_error_delete(error);
}
}

private static class Native
Expand All @@ -45,6 +69,9 @@ private static class Native
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_error_delete(IntPtr error);

[DllImport(Engine.LibraryName)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool wasmtime_error_exit_status(IntPtr error, out int exitStatus);
}
}
}
14 changes: 7 additions & 7 deletions tests/ExitTrapTests.cs → tests/ExitErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Wasmtime.Tests
{
public class ExitTrapTests
public class ExitErrorTests
{
[Theory]
[InlineData("ExitTrap.wat", 0)]
[InlineData("ExitTrap.wat", 1)]
[InlineData("ExitTrap.wat", -1)]
[InlineData("ExitError.wat", 0)]
[InlineData("ExitError.wat", 1)]
[InlineData("ExitError.wat", -1)]
public void ItReturnsExitCode(string path, int exitCode)
{
using var engine = new Engine();
Expand All @@ -33,18 +33,18 @@ public void ItReturnsExitCode(string path, int exitCode)
exit(exitCode);
Assert.False(bool.Parse(bool.TrueString));
}
catch (TrapException ex)
catch (WasmtimeException ex)
{
if (exitCode < 0)
{
Assert.Null(ex.ExitCode);
Assert.StartsWith("exit with invalid exit status", ex.Message);
Assert.Contains("exit with invalid exit status", ex.Message);
}
else
{
Assert.NotNull(ex.ExitCode);
Assert.Equal(exitCode, ex.ExitCode);
Assert.StartsWith($"Exited with i32 exit status {exitCode}\n", ex.Message);
Assert.Contains($"Exited with i32 exit status {exitCode}", ex.Message);
}
}
}
Expand Down
File renamed without changes.

0 comments on commit 808d632

Please sign in to comment.