-
Notifications
You must be signed in to change notification settings - Fork 55
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
martindevans
wants to merge
2
commits into
bytecodealliance:main
Choose a base branch
from
martindevans:wasip2-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
store.SetWasiConfiguration(config); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
andinput-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
. Thewasmtime
project links to this project as a way to embedwasmtime
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 invokingwasmtime
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 useswasmtime-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 aroundwasmtime
where all of the WASI interfaces are implemented by thewasmtime
runtime. Which is not a very good use case, because why not directly usewasmtime
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.