Skip to content

Added Wasi2Configuration #339

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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);
}
}
21 changes: 21 additions & 0 deletions tests/Wasi2Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
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.

config.WithInheritedStandardOutput();
config.WithInheritedStandardError();

store.SetWasiConfiguration(config);
}
}
Loading