Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions src/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ internal void SetWasiConfiguration(WasiConfiguration config)
}
}

internal void SetWasiConfiguration(Wasi2Configuration config)
{
var wasi = config.Build();
Native.wasmtime_context_set_wasip2(handle, wasi.DangerousGetHandle());
wasi.SetHandleAsInvalid();
}

/// <summary>
/// Configures the relative deadline at which point WebAssembly code will trap.
/// </summary>
Expand All @@ -95,6 +102,9 @@ private static class Native
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_context_set_wasi(IntPtr handle, IntPtr config);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_context_set_wasip2(IntPtr handle, IntPtr config2);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_context_set_epoch_deadline(IntPtr handle, ulong ticksBeyondCurrent);

Expand Down Expand Up @@ -239,6 +249,16 @@ public void SetWasiConfiguration(WasiConfiguration config)
System.GC.KeepAlive(this);
}

/// <summary>
/// Configures WASI2 within the store.
/// </summary>
/// <param name="config">The WASI2 configuration to use.</param>
public void SetWasiConfiguration(Wasi2Configuration config)
{
Context.SetWasiConfiguration(config);
System.GC.KeepAlive(this);
}

/// <summary>
/// Configures the relative deadline at which point WebAssembly code will trap.
/// </summary>
Expand Down
110 changes: 110 additions & 0 deletions src/Wasi2Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.InteropServices;

namespace Wasmtime;

/// <summary>
/// Represents a WASI2 configuration.
/// </summary>
public class Wasi2Configuration
{
private bool _inheritStandardInput;
private bool _inheritStandardOutput;
private bool _inheritStandardError;

/// <summary>
/// Sets the configuration to inherit stdin.
/// </summary>
/// <returns>Returns the current configuration.</returns>
public Wasi2Configuration WithInheritedStandardInput()
{
_inheritStandardInput = true;
return this;
}

/// <summary>
/// Sets the configuration to inherit stdout.
/// </summary>
/// <returns>Returns the current configuration.</returns>
public Wasi2Configuration WithInheritedStandardOutput()
{
_inheritStandardOutput = true;
return this;
}

/// <summary>
/// Sets the configuration to inherit stderr.
/// </summary>
/// <returns>Returns the current configuration.</returns>
public Wasi2Configuration WithInheritedStandardError()
{
_inheritStandardError = true;
return this;
}

internal Handle Build()
{
var config = new Handle(Native.wasmtime_wasip2_config_new());

SetStandardIn(config);
SetStandardOut(config);
SetStandardError(config);

return config;
}

private void SetStandardIn(Handle config)
{
if (_inheritStandardInput)
Native.wasmtime_wasip2_config_inherit_stdin(config);
}

private void SetStandardOut(Handle config)
{
if (_inheritStandardOutput)
Native.wasmtime_wasip2_config_inherit_stdout(config);
}

private void SetStandardError(Handle config)
{
if (_inheritStandardError)
Native.wasmtime_wasip2_config_inherit_stderr(config);
}

internal class Handle : SafeHandleZeroOrMinusOneIsInvalid
{
public Handle(IntPtr handle)
: base(true)
{
SetHandle(handle);
}

protected override bool ReleaseHandle()
{
Native.wasmtime_wasip2_config_delete(handle);
return true;
}
}

private static class Native
{
[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_wasip2_config_new();

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_wasip2_config_delete(IntPtr config);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_wasip2_config_inherit_stdin(Handle config);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_wasip2_config_inherit_stdout(Handle config);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_wasip2_config_inherit_stderr(Handle config);

[DllImport(Engine.LibraryName)]
public static extern unsafe void wasmtime_wasip2_config_arg(Handle config, char* arg, nuint arg_len);
}
}
18 changes: 18 additions & 0 deletions tests/Wasi2Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Xunit;

namespace Wasmtime.Tests;

public class Wasi2Tests
{
[Fact]
public void ItSetsWasi2Config()
{
using var engine = new Engine();
using var store = new Store(engine);

var config = new Wasi2Configuration();
config.WithInheritedStandardInput();
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to not test the other configs?

Copy link
Contributor Author

@martindevans martindevans Aug 5, 2025

Choose a reason for hiding this comment

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

I'll have a look at adding some more tests later today

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added some more coverage (just calling other WithInheritedX methods).

I'm not really happy about these tests though - they don't actually assert anything!

Ideally I guess we'd need a WASI2.wat test file that reads some input, logs some out and logs some error output. Then we could assert that all of that is written to the correct outputs.

Choose a reason for hiding this comment

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

I looked around for an easy way to test this but could not find it and I wonder what the right approach is to test this.

The WASI preview 2 is quite an API surface. In the stdio test in wasi-testsuite a lot more functionality is tested than just stdin/stdout/stderr. On the surface, it seems difficult to test just the stdin/stdout in isolation with a simple .wat file. For example, the WASI 0.2 WIT for the CLI contains references to the output-stream and input-stream types and other WASI specs. The WASI 0.2 tests in the test-suite are compiled to .wasm via build scripts and I have not yet seen a simple stable .wat file that can only test stdin/stdout.

This however got me thinking what the proposition is of wasmtime-dotnet. The wasmtime project links to this project as a way to embed wasmtime as a library. I guess historically the use case was primarily around embedding an engine capable of executing WASM modules. But with the advent of WASI, wasmtime offers a runtime with rich functionality that can interact with its environment through these system interfaces. So, whole applications (CLI applications and web applications for example) can be compiled to wasm and executed directly by invoking wasmtime with the wasm file as argument. Kind of like an application server.

In contrast, wasmtime-dotnet is a library (NuGet package) and does not have a standalone executable. So it is not possible to directly execute a WASM application, rather you´d have to write the dotnet program yourself that uses wasmtime-dotnet.

If wasmtime-dotnet would offer such an executable, we could utilize the wasi-testsuite to test the whole WASI surface. Which would cover the stdin/stdout functionality of this pull request, and a whole lot more. But at the same time it would be ´just' a thin wrapper around wasmtime where all of the WASI interfaces are implemented by the wasmtime runtime. Which is not a very good use case, because why not directly use wasmtime in that case?

Imho the value proposition of wasmtime-dotnet is that .NET applications can enhance their applications with WASM modules/components (possibly user-supplied) in a safe way that feels native to .NET (i.e. ergonomic to use). But that also implies more control over the system interfaces. For example, if a WASM component is loaded that uses wasi-cli or wasi-sockets do we want to use .NET streams and sockets or it is offloaded completely to the wasmtime (Rust) runtime, over which the .NET application has very little control? Integration with .NET streams and sockets would be ideal imo, but is also a huge endeavour.

I'm relatively new to the WASM ecosystem so I might be very confused here but imo there should be a clear proposition for wasmtime-dotnet that can guide the development of wasmtime-dotnet, which would also direct questions such as how to test WASI functionality.


store.SetWasiConfiguration(config);
}
}
Loading